blob: e929aa1c941b068c484418982f6882a50c2c0649 [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 Korobeynikov4403b932009-07-16 13:27:25 +0000115}
116
117SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
118 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000119 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
120 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000121 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000122 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000123 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000124 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000125 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000126 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000127 default:
128 assert(0 && "unimplemented operand");
129 return SDValue();
130 }
131}
132
133//===----------------------------------------------------------------------===//
134// Calling Convention Implementation
135//===----------------------------------------------------------------------===//
136
137#include "SystemZGenCallingConv.inc"
138
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000139SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
140 SelectionDAG &DAG) {
141 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
142 switch (CC) {
143 default:
144 assert(0 && "Unsupported calling convention");
145 case CallingConv::C:
146 case CallingConv::Fast:
147 return LowerCCCArguments(Op, DAG);
148 }
149}
150
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000151SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
152 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
153 unsigned CallingConv = TheCall->getCallingConv();
154 switch (CallingConv) {
155 default:
156 assert(0 && "Unsupported calling convention");
157 case CallingConv::Fast:
158 case CallingConv::C:
159 return LowerCCCCallTo(Op, DAG, CallingConv);
160 }
161}
162
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000163/// LowerCCCArguments - transform physical registers into virtual registers and
164/// generate load operations for arguments places on the stack.
165// FIXME: struct return stuff
166// FIXME: varargs
167SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
168 SelectionDAG &DAG) {
169 MachineFunction &MF = DAG.getMachineFunction();
170 MachineFrameInfo *MFI = MF.getFrameInfo();
171 MachineRegisterInfo &RegInfo = MF.getRegInfo();
172 SDValue Root = Op.getOperand(0);
173 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
174 unsigned CC = MF.getFunction()->getCallingConv();
175 DebugLoc dl = Op.getDebugLoc();
176
177 // Assign locations to all of the incoming arguments.
178 SmallVector<CCValAssign, 16> ArgLocs;
179 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
180 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
181
182 assert(!isVarArg && "Varargs not supported yet");
183
184 SmallVector<SDValue, 16> ArgValues;
185 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
186 CCValAssign &VA = ArgLocs[i];
187 if (VA.isRegLoc()) {
188 // Arguments passed in registers
189 MVT RegVT = VA.getLocVT();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000190 TargetRegisterClass *RC;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000191 switch (RegVT.getSimpleVT()) {
192 default:
193 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
194 << RegVT.getSimpleVT()
195 << "\n";
196 abort();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000197 case MVT::i64:
198 RC = SystemZ::GR64RegisterClass;
199 break;
200 case MVT::f32:
201 RC = SystemZ::FP32RegisterClass;
202 break;
203 case MVT::f64:
204 RC = SystemZ::FP64RegisterClass;
205 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000206 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000207
208 unsigned VReg = RegInfo.createVirtualRegister(RC);
209 RegInfo.addLiveIn(VA.getLocReg(), VReg);
210 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
211
212 // If this is an 8/16/32-bit value, it is really passed promoted to 64
213 // bits. Insert an assert[sz]ext to capture this, then truncate to the
214 // right size.
215 if (VA.getLocInfo() == CCValAssign::SExt)
216 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
217 DAG.getValueType(VA.getValVT()));
218 else if (VA.getLocInfo() == CCValAssign::ZExt)
219 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
220 DAG.getValueType(VA.getValVT()));
221
222 if (VA.getLocInfo() != CCValAssign::Full)
223 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
224
225 ArgValues.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000226 } else {
227 // Sanity check
228 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000229
230 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000231 // Create the frame index object for this incoming parameter...
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000232 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
233 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000234
235 // Create the SelectionDAG nodes corresponding to a load
236 //from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000237 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
238 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN,
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000239 PseudoSourceValue::getFixedStack(FI), 0));
240 }
241 }
242
243 ArgValues.push_back(Root);
244
245 // Return the new list of results.
246 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
247 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
248}
249
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000250/// LowerCCCCallTo - functions arguments are copied from virtual regs to
251/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
252/// TODO: sret.
253SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
254 unsigned CC) {
255 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
256 SDValue Chain = TheCall->getChain();
257 SDValue Callee = TheCall->getCallee();
258 bool isVarArg = TheCall->isVarArg();
259 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000260 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000261
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000262 // Offset to first argument stack slot.
263 const unsigned FirstArgOffset = 160;
264
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000265 // Analyze operands of the call, assigning locations to each operand.
266 SmallVector<CCValAssign, 16> ArgLocs;
267 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
268
269 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
270
271 // Get a count of how many bytes are to be pushed on the stack.
272 unsigned NumBytes = CCInfo.getNextStackOffset();
273
274 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
275 getPointerTy(), true));
276
277 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
278 SmallVector<SDValue, 12> MemOpChains;
279 SDValue StackPtr;
280
281 // Walk the register/memloc assignments, inserting copies/loads.
282 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
283 CCValAssign &VA = ArgLocs[i];
284
285 // Arguments start after the 5 first operands of ISD::CALL
286 SDValue Arg = TheCall->getArg(i);
287
288 // Promote the value if needed.
289 switch (VA.getLocInfo()) {
290 default: assert(0 && "Unknown loc info!");
291 case CCValAssign::Full: break;
292 case CCValAssign::SExt:
293 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
294 break;
295 case CCValAssign::ZExt:
296 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
297 break;
298 case CCValAssign::AExt:
299 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
300 break;
301 }
302
303 // Arguments that can be passed on register must be kept at RegsToPass
304 // vector
305 if (VA.isRegLoc()) {
306 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
307 } else {
308 assert(VA.isMemLoc());
309
310 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000311 StackPtr =
312 DAG.getCopyFromReg(Chain, dl,
313 (RegInfo->hasFP(MF) ?
314 SystemZ::R11D : SystemZ::R15D),
315 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000316
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000317 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
318 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
319 StackPtr,
320 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000321
322 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000323 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000324 }
325 }
326
327 // Transform all store nodes into one single node because all store nodes are
328 // independent of each other.
329 if (!MemOpChains.empty())
330 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
331 &MemOpChains[0], MemOpChains.size());
332
333 // Build a sequence of copy-to-reg nodes chained together with token chain and
334 // flag operands which copy the outgoing args into registers. The InFlag in
335 // necessary since all emited instructions must be stuck together.
336 SDValue InFlag;
337 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
338 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
339 RegsToPass[i].second, InFlag);
340 InFlag = Chain.getValue(1);
341 }
342
343 // If the callee is a GlobalAddress node (quite common, every direct call is)
344 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
345 // Likewise ExternalSymbol -> TargetExternalSymbol.
346 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
347 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
348 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
349 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
350
351 // Returns a chain & a flag for retval copy to use.
352 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
353 SmallVector<SDValue, 8> Ops;
354 Ops.push_back(Chain);
355 Ops.push_back(Callee);
356
357 // Add argument registers to the end of the list so that they are
358 // known live into the call.
359 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
360 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
361 RegsToPass[i].second.getValueType()));
362
363 if (InFlag.getNode())
364 Ops.push_back(InFlag);
365
366 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
367 InFlag = Chain.getValue(1);
368
369 // Create the CALLSEQ_END node.
370 Chain = DAG.getCALLSEQ_END(Chain,
371 DAG.getConstant(NumBytes, getPointerTy(), true),
372 DAG.getConstant(0, getPointerTy(), true),
373 InFlag);
374 InFlag = Chain.getValue(1);
375
376 // Handle result values, copying them out of physregs into vregs that we
377 // return.
378 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
379 Op.getResNo());
380}
381
382/// LowerCallResult - Lower the result values of an ISD::CALL into the
383/// appropriate copies out of appropriate physical registers. This assumes that
384/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
385/// being lowered. Returns a SDNode with the same number of values as the
386/// ISD::CALL.
387SDNode*
388SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
389 CallSDNode *TheCall,
390 unsigned CallingConv,
391 SelectionDAG &DAG) {
392 bool isVarArg = TheCall->isVarArg();
393 DebugLoc dl = TheCall->getDebugLoc();
394
395 // Assign locations to each value returned by this call.
396 SmallVector<CCValAssign, 16> RVLocs;
397 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
398
399 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
400 SmallVector<SDValue, 8> ResultVals;
401
402 // Copy all of the result registers out of their specified physreg.
403 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000404 CCValAssign &VA = RVLocs[i];
405
406 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
407 VA.getLocVT(), InFlag).getValue(1);
408 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000409 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000410
411 // If this is an 8/16/32-bit value, it is really passed promoted to 64
412 // bits. Insert an assert[sz]ext to capture this, then truncate to the
413 // right size.
414 if (VA.getLocInfo() == CCValAssign::SExt)
415 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
416 DAG.getValueType(VA.getValVT()));
417 else if (VA.getLocInfo() == CCValAssign::ZExt)
418 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
419 DAG.getValueType(VA.getValVT()));
420
421 if (VA.getLocInfo() != CCValAssign::Full)
422 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
423
424 ResultVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000425 }
426
427 ResultVals.push_back(Chain);
428
429 // Merge everything together with a MERGE_VALUES node.
430 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
431 &ResultVals[0], ResultVals.size()).getNode();
432}
433
434
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000435SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
436 // CCValAssign - represent the assignment of the return value to a location
437 SmallVector<CCValAssign, 16> RVLocs;
438 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
439 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
440 DebugLoc dl = Op.getDebugLoc();
441
442 // CCState - Info about the registers and stack slot.
443 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
444
445 // Analize return values of ISD::RET
446 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
447
448 // If this is the first return lowered for this function, add the regs to the
449 // liveout set for the function.
450 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
451 for (unsigned i = 0; i != RVLocs.size(); ++i)
452 if (RVLocs[i].isRegLoc())
453 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
454 }
455
456 // The chain is always operand #0
457 SDValue Chain = Op.getOperand(0);
458 SDValue Flag;
459
460 // Copy the result values into the output registers.
461 for (unsigned i = 0; i != RVLocs.size(); ++i) {
462 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000463 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000464 assert(VA.isRegLoc() && "Can only return in registers!");
465
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000466 // If this is an 8/16/32-bit value, it is really should be passed promoted
467 // to 64 bits.
468 if (VA.getLocInfo() == CCValAssign::SExt)
469 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
470 else if (VA.getLocInfo() == CCValAssign::ZExt)
471 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
472 else if (VA.getLocInfo() == CCValAssign::AExt)
473 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
474
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000475 // ISD::RET => ret chain, (regnum1,val1), ...
476 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000477 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000478
479 // Guarantee that all emitted copies are stuck together,
480 // avoiding something bad.
481 Flag = Chain.getValue(1);
482 }
483
484 if (Flag.getNode())
485 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
486
487 // Return Void
488 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
489}
490
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000491SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
492 ISD::CondCode CC, SDValue &SystemZCC,
493 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000494 // FIXME: Emit a test if RHS is zero
495
496 bool isUnsigned = false;
497 SystemZCC::CondCodes TCC;
498 switch (CC) {
499 default: assert(0 && "Invalid integer condition!");
500 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000501 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000502 TCC = SystemZCC::E;
503 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000504 case ISD::SETUEQ:
505 TCC = SystemZCC::NLH;
506 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000507 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000508 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000509 TCC = SystemZCC::NE;
510 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000511 case ISD::SETUNE:
512 TCC = SystemZCC::LH;
513 break;
514 case ISD::SETO:
515 TCC = SystemZCC::O;
516 break;
517 case ISD::SETUO:
518 TCC = SystemZCC::NO;
519 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000520 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000521 if (LHS.getValueType().isFloatingPoint()) {
522 TCC = SystemZCC::NH;
523 break;
524 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000525 isUnsigned = true; // FALLTHROUGH
526 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000527 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000528 TCC = SystemZCC::LE;
529 break;
530 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000531 if (LHS.getValueType().isFloatingPoint()) {
532 TCC = SystemZCC::NL;
533 break;
534 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000535 isUnsigned = true; // FALLTHROUGH
536 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000537 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000538 TCC = SystemZCC::HE;
539 break;
540 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000541 if (LHS.getValueType().isFloatingPoint()) {
542 TCC = SystemZCC::NLE;
543 break;
544 }
545 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000546 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000547 case ISD::SETOGT:
548 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000549 break;
550 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000551 if (LHS.getValueType().isFloatingPoint()) {
552 TCC = SystemZCC::NHE;
553 break;
554 }
555 isUnsigned = true; // FALLTHROUGH
556 case ISD::SETLT:
557 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000558 TCC = SystemZCC::L;
559 break;
560 }
561
562 SystemZCC = DAG.getConstant(TCC, MVT::i32);
563
564 DebugLoc dl = LHS.getDebugLoc();
565 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
566 dl, MVT::Flag, LHS, RHS);
567}
568
569
570SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
571 SDValue Chain = Op.getOperand(0);
572 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
573 SDValue LHS = Op.getOperand(2);
574 SDValue RHS = Op.getOperand(3);
575 SDValue Dest = Op.getOperand(4);
576 DebugLoc dl = Op.getDebugLoc();
577
578 SDValue SystemZCC;
579 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
580 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
581 Chain, Dest, SystemZCC, Flag);
582}
583
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000584SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
585 SDValue LHS = Op.getOperand(0);
586 SDValue RHS = Op.getOperand(1);
587 SDValue TrueV = Op.getOperand(2);
588 SDValue FalseV = Op.getOperand(3);
589 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
590 DebugLoc dl = Op.getDebugLoc();
591
592 SDValue SystemZCC;
593 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
594
595 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
596 SmallVector<SDValue, 4> Ops;
597 Ops.push_back(TrueV);
598 Ops.push_back(FalseV);
599 Ops.push_back(SystemZCC);
600 Ops.push_back(Flag);
601
602 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
603}
604
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000605SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
606 SelectionDAG &DAG) {
607 DebugLoc dl = Op.getDebugLoc();
608 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000609 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000610
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000611 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
612 bool ExtraLoadRequired =
613 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
614
615 SDValue Result;
616 if (!IsPic && !ExtraLoadRequired) {
617 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
618 Offset = 0;
619 } else {
620 unsigned char OpFlags = 0;
621 if (ExtraLoadRequired)
622 OpFlags = SystemZII::MO_GOTENT;
623
624 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
625 }
626
627 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
628 getPointerTy(), Result);
629
630 if (ExtraLoadRequired)
631 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
632 PseudoSourceValue::getGOT(), 0);
633
634 // If there was a non-zero offset that we didn't fold, create an explicit
635 // addition for it.
636 if (Offset != 0)
637 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
638 DAG.getConstant(Offset, getPointerTy()));
639
640 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000641}
642
Anton Korobeynikovae535672009-07-16 14:19:35 +0000643// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000644SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
645 SelectionDAG &DAG) {
646 DebugLoc dl = Op.getDebugLoc();
647 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
648 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
649
650 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
651}
652
Anton Korobeynikovae535672009-07-16 14:19:35 +0000653
654// FIXME: PIC here
655// FIXME: This is just dirty hack. We need to lower cpool properly
656SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
657 SelectionDAG &DAG) {
658 DebugLoc dl = Op.getDebugLoc();
659 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
660
661 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
662 CP->getAlignment(),
663 CP->getOffset());
664
665 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
666}
667
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000668const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
669 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000670 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000671 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000672 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
673 case SystemZISD::CMP: return "SystemZISD::CMP";
674 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000675 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000676 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000677 default: return NULL;
678 }
679}
680
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000681//===----------------------------------------------------------------------===//
682// Other Lowering Code
683//===----------------------------------------------------------------------===//
684
685MachineBasicBlock*
686SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
687 MachineBasicBlock *BB) const {
688 const SystemZInstrInfo &TII = *TM.getInstrInfo();
689 DebugLoc dl = MI->getDebugLoc();
690 assert((MI->getOpcode() == SystemZ::Select32 ||
691 MI->getOpcode() == SystemZ::Select64) &&
692 "Unexpected instr type to insert");
693
694 // To "insert" a SELECT instruction, we actually have to insert the diamond
695 // control-flow pattern. The incoming instruction knows the destination vreg
696 // to set, the condition code register to branch on, the true/false values to
697 // select between, and a branch opcode to use.
698 const BasicBlock *LLVM_BB = BB->getBasicBlock();
699 MachineFunction::iterator I = BB;
700 ++I;
701
702 // thisMBB:
703 // ...
704 // TrueVal = ...
705 // cmpTY ccX, r1, r2
706 // jCC copy1MBB
707 // fallthrough --> copy0MBB
708 MachineBasicBlock *thisMBB = BB;
709 MachineFunction *F = BB->getParent();
710 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
711 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
712 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
713 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
714 F->insert(I, copy0MBB);
715 F->insert(I, copy1MBB);
716 // Update machine-CFG edges by transferring all successors of the current
717 // block to the new block which will contain the Phi node for the select.
718 copy1MBB->transferSuccessors(BB);
719 // Next, add the true and fallthrough blocks as its successors.
720 BB->addSuccessor(copy0MBB);
721 BB->addSuccessor(copy1MBB);
722
723 // copy0MBB:
724 // %FalseValue = ...
725 // # fallthrough to copy1MBB
726 BB = copy0MBB;
727
728 // Update machine-CFG edges
729 BB->addSuccessor(copy1MBB);
730
731 // copy1MBB:
732 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
733 // ...
734 BB = copy1MBB;
735 BuildMI(BB, dl, TII.get(SystemZ::PHI),
736 MI->getOperand(0).getReg())
737 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
738 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
739
740 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
741 return BB;
742}