blob: be76dc50c316c7606bd1b126ad472079ce41a6f9 [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 Korobeynikov8d1837d2009-07-16 13:56:42 +000048 addRegisterClass(MVT::i128, SystemZ::GR128RegisterClass);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000049 addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000050
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000051 if (!UseSoftFloat) {
52 addRegisterClass(MVT::f32, SystemZ::FP32RegisterClass);
53 addRegisterClass(MVT::f64, SystemZ::FP64RegisterClass);
54 }
55
Anton Korobeynikov4403b932009-07-16 13:27:25 +000056 // Compute derived properties from the register classes
57 computeRegisterProperties();
58
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000059 // Set shifts properties
60 setShiftAmountFlavor(Extend);
Anton Korobeynikov48e8b3c2009-07-16 14:15:24 +000061 setShiftAmountType(MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000062
Anton Korobeynikov4403b932009-07-16 13:27:25 +000063 // Provide all sorts of operation actions
Anton Korobeynikovbf022172009-07-16 13:53:35 +000064 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
65 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
66 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000067
Anton Korobeynikov23eff5c2009-07-16 14:20:08 +000068 setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Promote);
69 setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Promote);
70 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Promote);
71 setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Promote);
72 setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Promote);
73 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Promote);
74
Anton Korobeynikove0167c12009-07-16 13:35:30 +000075 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000076 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000077
78 setOperationAction(ISD::RET, MVT::Other, Custom);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000079
Anton Korobeynikov983d3a12009-07-16 14:07:24 +000080 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000081 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
82 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
83 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +000084 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
85 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
86 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
87 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +000088 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +000089 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Anton Korobeynikovc772c442009-07-16 14:08:15 +000090 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000091
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000092 setOperationAction(ISD::SDIV, MVT::i32, Expand);
93 setOperationAction(ISD::UDIV, MVT::i32, Expand);
94 setOperationAction(ISD::SDIV, MVT::i64, Expand);
95 setOperationAction(ISD::UDIV, MVT::i64, Expand);
96 setOperationAction(ISD::SREM, MVT::i32, Expand);
97 setOperationAction(ISD::UREM, MVT::i32, Expand);
98 setOperationAction(ISD::SREM, MVT::i64, Expand);
99 setOperationAction(ISD::UREM, MVT::i64, Expand);
100
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000101 // FIXME: Can we lower these 2 efficiently?
102 setOperationAction(ISD::SETCC, MVT::i32, Expand);
103 setOperationAction(ISD::SETCC, MVT::i64, Expand);
104 setOperationAction(ISD::SELECT, MVT::i32, Expand);
105 setOperationAction(ISD::SELECT, MVT::i64, Expand);
106 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
107 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000108 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
109 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000110
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +0000111 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
112 // unsigned.
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000113 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000114 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000115
116 setOperationAction(ISD::FSIN, MVT::f32, Expand);
117 setOperationAction(ISD::FSIN, MVT::f64, Expand);
118 setOperationAction(ISD::FCOS, MVT::f32, Expand);
119 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000120}
121
122SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
123 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000124 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
125 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000126 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000127 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000128 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000129 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000130 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000131 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000132 default:
133 assert(0 && "unimplemented operand");
134 return SDValue();
135 }
136}
137
138//===----------------------------------------------------------------------===//
139// Calling Convention Implementation
140//===----------------------------------------------------------------------===//
141
142#include "SystemZGenCallingConv.inc"
143
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000144SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
145 SelectionDAG &DAG) {
146 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
147 switch (CC) {
148 default:
149 assert(0 && "Unsupported calling convention");
150 case CallingConv::C:
151 case CallingConv::Fast:
152 return LowerCCCArguments(Op, DAG);
153 }
154}
155
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000156SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
157 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
158 unsigned CallingConv = TheCall->getCallingConv();
159 switch (CallingConv) {
160 default:
161 assert(0 && "Unsupported calling convention");
162 case CallingConv::Fast:
163 case CallingConv::C:
164 return LowerCCCCallTo(Op, DAG, CallingConv);
165 }
166}
167
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000168/// LowerCCCArguments - transform physical registers into virtual registers and
169/// generate load operations for arguments places on the stack.
170// FIXME: struct return stuff
171// FIXME: varargs
172SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
173 SelectionDAG &DAG) {
174 MachineFunction &MF = DAG.getMachineFunction();
175 MachineFrameInfo *MFI = MF.getFrameInfo();
176 MachineRegisterInfo &RegInfo = MF.getRegInfo();
177 SDValue Root = Op.getOperand(0);
178 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
179 unsigned CC = MF.getFunction()->getCallingConv();
180 DebugLoc dl = Op.getDebugLoc();
181
182 // Assign locations to all of the incoming arguments.
183 SmallVector<CCValAssign, 16> ArgLocs;
184 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
185 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
186
187 assert(!isVarArg && "Varargs not supported yet");
188
189 SmallVector<SDValue, 16> ArgValues;
190 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
191 CCValAssign &VA = ArgLocs[i];
192 if (VA.isRegLoc()) {
193 // Arguments passed in registers
194 MVT RegVT = VA.getLocVT();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000195 TargetRegisterClass *RC;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000196 switch (RegVT.getSimpleVT()) {
197 default:
198 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
199 << RegVT.getSimpleVT()
200 << "\n";
201 abort();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000202 case MVT::i64:
203 RC = SystemZ::GR64RegisterClass;
204 break;
205 case MVT::f32:
206 RC = SystemZ::FP32RegisterClass;
207 break;
208 case MVT::f64:
209 RC = SystemZ::FP64RegisterClass;
210 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000211 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000212
213 unsigned VReg = RegInfo.createVirtualRegister(RC);
214 RegInfo.addLiveIn(VA.getLocReg(), VReg);
215 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
216
217 // If this is an 8/16/32-bit value, it is really passed promoted to 64
218 // bits. Insert an assert[sz]ext to capture this, then truncate to the
219 // right size.
220 if (VA.getLocInfo() == CCValAssign::SExt)
221 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
222 DAG.getValueType(VA.getValVT()));
223 else if (VA.getLocInfo() == CCValAssign::ZExt)
224 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
225 DAG.getValueType(VA.getValVT()));
226
227 if (VA.getLocInfo() != CCValAssign::Full)
228 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
229
230 ArgValues.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000231 } else {
232 // Sanity check
233 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000234
235 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000236 // Create the frame index object for this incoming parameter...
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000237 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
238 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000239
240 // Create the SelectionDAG nodes corresponding to a load
241 //from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000242 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
243 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN,
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000244 PseudoSourceValue::getFixedStack(FI), 0));
245 }
246 }
247
248 ArgValues.push_back(Root);
249
250 // Return the new list of results.
251 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
252 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
253}
254
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000255/// LowerCCCCallTo - functions arguments are copied from virtual regs to
256/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
257/// TODO: sret.
258SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
259 unsigned CC) {
260 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
261 SDValue Chain = TheCall->getChain();
262 SDValue Callee = TheCall->getCallee();
263 bool isVarArg = TheCall->isVarArg();
264 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000265 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000266
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000267 // Offset to first argument stack slot.
268 const unsigned FirstArgOffset = 160;
269
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000270 // Analyze operands of the call, assigning locations to each operand.
271 SmallVector<CCValAssign, 16> ArgLocs;
272 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
273
274 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
275
276 // Get a count of how many bytes are to be pushed on the stack.
277 unsigned NumBytes = CCInfo.getNextStackOffset();
278
279 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
280 getPointerTy(), true));
281
282 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
283 SmallVector<SDValue, 12> MemOpChains;
284 SDValue StackPtr;
285
286 // Walk the register/memloc assignments, inserting copies/loads.
287 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
288 CCValAssign &VA = ArgLocs[i];
289
290 // Arguments start after the 5 first operands of ISD::CALL
291 SDValue Arg = TheCall->getArg(i);
292
293 // Promote the value if needed.
294 switch (VA.getLocInfo()) {
295 default: assert(0 && "Unknown loc info!");
296 case CCValAssign::Full: break;
297 case CCValAssign::SExt:
298 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
299 break;
300 case CCValAssign::ZExt:
301 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
302 break;
303 case CCValAssign::AExt:
304 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
305 break;
306 }
307
308 // Arguments that can be passed on register must be kept at RegsToPass
309 // vector
310 if (VA.isRegLoc()) {
311 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
312 } else {
313 assert(VA.isMemLoc());
314
315 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000316 StackPtr =
317 DAG.getCopyFromReg(Chain, dl,
318 (RegInfo->hasFP(MF) ?
319 SystemZ::R11D : SystemZ::R15D),
320 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000321
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000322 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
323 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
324 StackPtr,
325 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000326
327 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000328 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000329 }
330 }
331
332 // Transform all store nodes into one single node because all store nodes are
333 // independent of each other.
334 if (!MemOpChains.empty())
335 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
336 &MemOpChains[0], MemOpChains.size());
337
338 // Build a sequence of copy-to-reg nodes chained together with token chain and
339 // flag operands which copy the outgoing args into registers. The InFlag in
340 // necessary since all emited instructions must be stuck together.
341 SDValue InFlag;
342 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
343 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
344 RegsToPass[i].second, InFlag);
345 InFlag = Chain.getValue(1);
346 }
347
348 // If the callee is a GlobalAddress node (quite common, every direct call is)
349 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
350 // Likewise ExternalSymbol -> TargetExternalSymbol.
351 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
352 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
353 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
354 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
355
356 // Returns a chain & a flag for retval copy to use.
357 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
358 SmallVector<SDValue, 8> Ops;
359 Ops.push_back(Chain);
360 Ops.push_back(Callee);
361
362 // Add argument registers to the end of the list so that they are
363 // known live into the call.
364 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
365 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
366 RegsToPass[i].second.getValueType()));
367
368 if (InFlag.getNode())
369 Ops.push_back(InFlag);
370
371 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
372 InFlag = Chain.getValue(1);
373
374 // Create the CALLSEQ_END node.
375 Chain = DAG.getCALLSEQ_END(Chain,
376 DAG.getConstant(NumBytes, getPointerTy(), true),
377 DAG.getConstant(0, getPointerTy(), true),
378 InFlag);
379 InFlag = Chain.getValue(1);
380
381 // Handle result values, copying them out of physregs into vregs that we
382 // return.
383 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
384 Op.getResNo());
385}
386
387/// LowerCallResult - Lower the result values of an ISD::CALL into the
388/// appropriate copies out of appropriate physical registers. This assumes that
389/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
390/// being lowered. Returns a SDNode with the same number of values as the
391/// ISD::CALL.
392SDNode*
393SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
394 CallSDNode *TheCall,
395 unsigned CallingConv,
396 SelectionDAG &DAG) {
397 bool isVarArg = TheCall->isVarArg();
398 DebugLoc dl = TheCall->getDebugLoc();
399
400 // Assign locations to each value returned by this call.
401 SmallVector<CCValAssign, 16> RVLocs;
402 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
403
404 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
405 SmallVector<SDValue, 8> ResultVals;
406
407 // Copy all of the result registers out of their specified physreg.
408 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000409 CCValAssign &VA = RVLocs[i];
410
411 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
412 VA.getLocVT(), InFlag).getValue(1);
413 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000414 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000415
416 // If this is an 8/16/32-bit value, it is really passed promoted to 64
417 // bits. Insert an assert[sz]ext to capture this, then truncate to the
418 // right size.
419 if (VA.getLocInfo() == CCValAssign::SExt)
420 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
421 DAG.getValueType(VA.getValVT()));
422 else if (VA.getLocInfo() == CCValAssign::ZExt)
423 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
424 DAG.getValueType(VA.getValVT()));
425
426 if (VA.getLocInfo() != CCValAssign::Full)
427 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
428
429 ResultVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000430 }
431
432 ResultVals.push_back(Chain);
433
434 // Merge everything together with a MERGE_VALUES node.
435 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
436 &ResultVals[0], ResultVals.size()).getNode();
437}
438
439
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000440SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
441 // CCValAssign - represent the assignment of the return value to a location
442 SmallVector<CCValAssign, 16> RVLocs;
443 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
444 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
445 DebugLoc dl = Op.getDebugLoc();
446
447 // CCState - Info about the registers and stack slot.
448 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
449
450 // Analize return values of ISD::RET
451 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
452
453 // If this is the first return lowered for this function, add the regs to the
454 // liveout set for the function.
455 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
456 for (unsigned i = 0; i != RVLocs.size(); ++i)
457 if (RVLocs[i].isRegLoc())
458 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
459 }
460
461 // The chain is always operand #0
462 SDValue Chain = Op.getOperand(0);
463 SDValue Flag;
464
465 // Copy the result values into the output registers.
466 for (unsigned i = 0; i != RVLocs.size(); ++i) {
467 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000468 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000469 assert(VA.isRegLoc() && "Can only return in registers!");
470
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000471 // If this is an 8/16/32-bit value, it is really should be passed promoted
472 // to 64 bits.
473 if (VA.getLocInfo() == CCValAssign::SExt)
474 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
475 else if (VA.getLocInfo() == CCValAssign::ZExt)
476 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
477 else if (VA.getLocInfo() == CCValAssign::AExt)
478 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
479
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000480 // ISD::RET => ret chain, (regnum1,val1), ...
481 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000482 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000483
484 // Guarantee that all emitted copies are stuck together,
485 // avoiding something bad.
486 Flag = Chain.getValue(1);
487 }
488
489 if (Flag.getNode())
490 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
491
492 // Return Void
493 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
494}
495
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000496SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
497 ISD::CondCode CC, SDValue &SystemZCC,
498 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000499 // FIXME: Emit a test if RHS is zero
500
501 bool isUnsigned = false;
502 SystemZCC::CondCodes TCC;
503 switch (CC) {
504 default: assert(0 && "Invalid integer condition!");
505 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000506 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000507 TCC = SystemZCC::E;
508 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000509 case ISD::SETUEQ:
510 TCC = SystemZCC::NLH;
511 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000512 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000513 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000514 TCC = SystemZCC::NE;
515 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000516 case ISD::SETUNE:
517 TCC = SystemZCC::LH;
518 break;
519 case ISD::SETO:
520 TCC = SystemZCC::O;
521 break;
522 case ISD::SETUO:
523 TCC = SystemZCC::NO;
524 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000525 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000526 if (LHS.getValueType().isFloatingPoint()) {
527 TCC = SystemZCC::NH;
528 break;
529 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000530 isUnsigned = true; // FALLTHROUGH
531 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000532 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000533 TCC = SystemZCC::LE;
534 break;
535 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000536 if (LHS.getValueType().isFloatingPoint()) {
537 TCC = SystemZCC::NL;
538 break;
539 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000540 isUnsigned = true; // FALLTHROUGH
541 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000542 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000543 TCC = SystemZCC::HE;
544 break;
545 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000546 if (LHS.getValueType().isFloatingPoint()) {
547 TCC = SystemZCC::NLE;
548 break;
549 }
550 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000551 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000552 case ISD::SETOGT:
553 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000554 break;
555 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000556 if (LHS.getValueType().isFloatingPoint()) {
557 TCC = SystemZCC::NHE;
558 break;
559 }
560 isUnsigned = true; // FALLTHROUGH
561 case ISD::SETLT:
562 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000563 TCC = SystemZCC::L;
564 break;
565 }
566
567 SystemZCC = DAG.getConstant(TCC, MVT::i32);
568
569 DebugLoc dl = LHS.getDebugLoc();
570 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
571 dl, MVT::Flag, LHS, RHS);
572}
573
574
575SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
576 SDValue Chain = Op.getOperand(0);
577 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
578 SDValue LHS = Op.getOperand(2);
579 SDValue RHS = Op.getOperand(3);
580 SDValue Dest = Op.getOperand(4);
581 DebugLoc dl = Op.getDebugLoc();
582
583 SDValue SystemZCC;
584 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
585 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
586 Chain, Dest, SystemZCC, Flag);
587}
588
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000589SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
590 SDValue LHS = Op.getOperand(0);
591 SDValue RHS = Op.getOperand(1);
592 SDValue TrueV = Op.getOperand(2);
593 SDValue FalseV = Op.getOperand(3);
594 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
595 DebugLoc dl = Op.getDebugLoc();
596
597 SDValue SystemZCC;
598 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
599
600 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
601 SmallVector<SDValue, 4> Ops;
602 Ops.push_back(TrueV);
603 Ops.push_back(FalseV);
604 Ops.push_back(SystemZCC);
605 Ops.push_back(Flag);
606
607 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
608}
609
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000610SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
611 SelectionDAG &DAG) {
612 DebugLoc dl = Op.getDebugLoc();
613 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000614 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000615
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000616 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
617 bool ExtraLoadRequired =
618 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
619
620 SDValue Result;
621 if (!IsPic && !ExtraLoadRequired) {
622 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
623 Offset = 0;
624 } else {
625 unsigned char OpFlags = 0;
626 if (ExtraLoadRequired)
627 OpFlags = SystemZII::MO_GOTENT;
628
629 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
630 }
631
632 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
633 getPointerTy(), Result);
634
635 if (ExtraLoadRequired)
636 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
637 PseudoSourceValue::getGOT(), 0);
638
639 // If there was a non-zero offset that we didn't fold, create an explicit
640 // addition for it.
641 if (Offset != 0)
642 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
643 DAG.getConstant(Offset, getPointerTy()));
644
645 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000646}
647
Anton Korobeynikovae535672009-07-16 14:19:35 +0000648// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000649SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
650 SelectionDAG &DAG) {
651 DebugLoc dl = Op.getDebugLoc();
652 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
653 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
654
655 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
656}
657
Anton Korobeynikovae535672009-07-16 14:19:35 +0000658
659// FIXME: PIC here
660// FIXME: This is just dirty hack. We need to lower cpool properly
661SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
662 SelectionDAG &DAG) {
663 DebugLoc dl = Op.getDebugLoc();
664 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
665
666 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
667 CP->getAlignment(),
668 CP->getOffset());
669
670 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
671}
672
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000673const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
674 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000675 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000676 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000677 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
678 case SystemZISD::CMP: return "SystemZISD::CMP";
679 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000680 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000681 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000682 default: return NULL;
683 }
684}
685
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000686//===----------------------------------------------------------------------===//
687// Other Lowering Code
688//===----------------------------------------------------------------------===//
689
690MachineBasicBlock*
691SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
692 MachineBasicBlock *BB) const {
693 const SystemZInstrInfo &TII = *TM.getInstrInfo();
694 DebugLoc dl = MI->getDebugLoc();
695 assert((MI->getOpcode() == SystemZ::Select32 ||
696 MI->getOpcode() == SystemZ::Select64) &&
697 "Unexpected instr type to insert");
698
699 // To "insert" a SELECT instruction, we actually have to insert the diamond
700 // control-flow pattern. The incoming instruction knows the destination vreg
701 // to set, the condition code register to branch on, the true/false values to
702 // select between, and a branch opcode to use.
703 const BasicBlock *LLVM_BB = BB->getBasicBlock();
704 MachineFunction::iterator I = BB;
705 ++I;
706
707 // thisMBB:
708 // ...
709 // TrueVal = ...
710 // cmpTY ccX, r1, r2
711 // jCC copy1MBB
712 // fallthrough --> copy0MBB
713 MachineBasicBlock *thisMBB = BB;
714 MachineFunction *F = BB->getParent();
715 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
716 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
717 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
718 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
719 F->insert(I, copy0MBB);
720 F->insert(I, copy1MBB);
721 // Update machine-CFG edges by transferring all successors of the current
722 // block to the new block which will contain the Phi node for the select.
723 copy1MBB->transferSuccessors(BB);
724 // Next, add the true and fallthrough blocks as its successors.
725 BB->addSuccessor(copy0MBB);
726 BB->addSuccessor(copy1MBB);
727
728 // copy0MBB:
729 // %FalseValue = ...
730 // # fallthrough to copy1MBB
731 BB = copy0MBB;
732
733 // Update machine-CFG edges
734 BB->addSuccessor(copy1MBB);
735
736 // copy1MBB:
737 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
738 // ...
739 BB = copy1MBB;
740 BuildMI(BB, dl, TII.get(SystemZ::PHI),
741 MI->getOperand(0).getReg())
742 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
743 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
744
745 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
746 return BB;
747}