blob: b1d42cb18aded90fe9cf45ede222f673f637e8f9 [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 Korobeynikove37a37d2009-07-18 12:20:36 +0000107 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
108
Anton Korobeynikov0cca0692009-07-18 12:26:13 +0000109 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
110 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
111 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
112 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
113 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
114 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
115
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000116 // FIXME: Can we lower these 2 efficiently?
117 setOperationAction(ISD::SETCC, MVT::i32, Expand);
118 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000119 setOperationAction(ISD::SETCC, MVT::f32, Expand);
120 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000121 setOperationAction(ISD::SELECT, MVT::i32, Expand);
122 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000123 setOperationAction(ISD::SELECT, MVT::f32, Expand);
124 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000125 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
126 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000127 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
128 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000129
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +0000130 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
131 // unsigned.
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000132 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000133 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000134
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000135 // Lower some FP stuff
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000136 setOperationAction(ISD::FSIN, MVT::f32, Expand);
137 setOperationAction(ISD::FSIN, MVT::f64, Expand);
138 setOperationAction(ISD::FCOS, MVT::f32, Expand);
139 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000140
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000141 // We have only 64-bit bitconverts
Anton Korobeynikovbb8a0482009-07-16 14:30:29 +0000142 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
143 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000144
Anton Korobeynikova89430e2009-07-16 14:25:30 +0000145 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
146 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000147 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
148 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000149
150 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000151}
152
153SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
154 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000155 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
156 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000157 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000158 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000159 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000160 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000161 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000162 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000163 default:
164 assert(0 && "unimplemented operand");
165 return SDValue();
166 }
167}
168
169//===----------------------------------------------------------------------===//
170// Calling Convention Implementation
171//===----------------------------------------------------------------------===//
172
173#include "SystemZGenCallingConv.inc"
174
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000175SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
176 SelectionDAG &DAG) {
177 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
178 switch (CC) {
179 default:
180 assert(0 && "Unsupported calling convention");
181 case CallingConv::C:
182 case CallingConv::Fast:
183 return LowerCCCArguments(Op, DAG);
184 }
185}
186
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000187SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
188 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
189 unsigned CallingConv = TheCall->getCallingConv();
190 switch (CallingConv) {
191 default:
192 assert(0 && "Unsupported calling convention");
193 case CallingConv::Fast:
194 case CallingConv::C:
195 return LowerCCCCallTo(Op, DAG, CallingConv);
196 }
197}
198
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000199/// LowerCCCArguments - transform physical registers into virtual registers and
200/// generate load operations for arguments places on the stack.
201// FIXME: struct return stuff
202// FIXME: varargs
203SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
204 SelectionDAG &DAG) {
205 MachineFunction &MF = DAG.getMachineFunction();
206 MachineFrameInfo *MFI = MF.getFrameInfo();
207 MachineRegisterInfo &RegInfo = MF.getRegInfo();
208 SDValue Root = Op.getOperand(0);
209 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
210 unsigned CC = MF.getFunction()->getCallingConv();
211 DebugLoc dl = Op.getDebugLoc();
212
213 // Assign locations to all of the incoming arguments.
214 SmallVector<CCValAssign, 16> ArgLocs;
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000215 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000216 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
217
218 assert(!isVarArg && "Varargs not supported yet");
219
220 SmallVector<SDValue, 16> ArgValues;
221 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000222 SDValue ArgValue;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000223 CCValAssign &VA = ArgLocs[i];
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000224 MVT LocVT = VA.getLocVT();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000225 if (VA.isRegLoc()) {
226 // Arguments passed in registers
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000227 TargetRegisterClass *RC;
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000228 switch (LocVT.getSimpleVT()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000229 default:
230 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000231 << LocVT.getSimpleVT()
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000232 << "\n";
233 abort();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000234 case MVT::i64:
235 RC = SystemZ::GR64RegisterClass;
236 break;
237 case MVT::f32:
238 RC = SystemZ::FP32RegisterClass;
239 break;
240 case MVT::f64:
241 RC = SystemZ::FP64RegisterClass;
242 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000243 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000244
245 unsigned VReg = RegInfo.createVirtualRegister(RC);
246 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000247 ArgValue = DAG.getCopyFromReg(Root, dl, VReg, LocVT);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000248 } else {
249 // Sanity check
250 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000251
252 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000253 // Create the frame index object for this incoming parameter...
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000254 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000255 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000256
257 // Create the SelectionDAG nodes corresponding to a load
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000258 // from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000259 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000260 ArgValue = DAG.getLoad(LocVT, dl, Root, FIN,
261 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000262 }
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000263
264 // If this is an 8/16/32-bit value, it is really passed promoted to 64
265 // bits. Insert an assert[sz]ext to capture this, then truncate to the
266 // right size.
267 if (VA.getLocInfo() == CCValAssign::SExt)
268 ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
269 DAG.getValueType(VA.getValVT()));
270 else if (VA.getLocInfo() == CCValAssign::ZExt)
271 ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
272 DAG.getValueType(VA.getValVT()));
273
274 if (VA.getLocInfo() != CCValAssign::Full)
275 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
276
277 ArgValues.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000278 }
279
280 ArgValues.push_back(Root);
281
282 // Return the new list of results.
283 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
284 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
285}
286
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000287/// LowerCCCCallTo - functions arguments are copied from virtual regs to
288/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
289/// TODO: sret.
290SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
291 unsigned CC) {
292 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
293 SDValue Chain = TheCall->getChain();
294 SDValue Callee = TheCall->getCallee();
295 bool isVarArg = TheCall->isVarArg();
296 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000297 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000298
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000299 // Offset to first argument stack slot.
300 const unsigned FirstArgOffset = 160;
301
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000302 // Analyze operands of the call, assigning locations to each operand.
303 SmallVector<CCValAssign, 16> ArgLocs;
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000304 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000305
306 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
307
308 // Get a count of how many bytes are to be pushed on the stack.
309 unsigned NumBytes = CCInfo.getNextStackOffset();
310
311 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
312 getPointerTy(), true));
313
314 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
315 SmallVector<SDValue, 12> MemOpChains;
316 SDValue StackPtr;
317
318 // Walk the register/memloc assignments, inserting copies/loads.
319 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
320 CCValAssign &VA = ArgLocs[i];
321
322 // Arguments start after the 5 first operands of ISD::CALL
323 SDValue Arg = TheCall->getArg(i);
324
325 // Promote the value if needed.
326 switch (VA.getLocInfo()) {
327 default: assert(0 && "Unknown loc info!");
328 case CCValAssign::Full: break;
329 case CCValAssign::SExt:
330 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
331 break;
332 case CCValAssign::ZExt:
333 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
334 break;
335 case CCValAssign::AExt:
336 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
337 break;
338 }
339
340 // Arguments that can be passed on register must be kept at RegsToPass
341 // vector
342 if (VA.isRegLoc()) {
343 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
344 } else {
345 assert(VA.isMemLoc());
346
347 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000348 StackPtr =
349 DAG.getCopyFromReg(Chain, dl,
350 (RegInfo->hasFP(MF) ?
351 SystemZ::R11D : SystemZ::R15D),
352 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000353
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000354 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
355 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
356 StackPtr,
357 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000358
359 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000360 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000361 }
362 }
363
364 // Transform all store nodes into one single node because all store nodes are
365 // independent of each other.
366 if (!MemOpChains.empty())
367 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
368 &MemOpChains[0], MemOpChains.size());
369
370 // Build a sequence of copy-to-reg nodes chained together with token chain and
371 // flag operands which copy the outgoing args into registers. The InFlag in
372 // necessary since all emited instructions must be stuck together.
373 SDValue InFlag;
374 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
375 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
376 RegsToPass[i].second, InFlag);
377 InFlag = Chain.getValue(1);
378 }
379
380 // If the callee is a GlobalAddress node (quite common, every direct call is)
381 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
382 // Likewise ExternalSymbol -> TargetExternalSymbol.
383 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
384 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
385 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
386 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
387
388 // Returns a chain & a flag for retval copy to use.
389 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
390 SmallVector<SDValue, 8> Ops;
391 Ops.push_back(Chain);
392 Ops.push_back(Callee);
393
394 // Add argument registers to the end of the list so that they are
395 // known live into the call.
396 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
397 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
398 RegsToPass[i].second.getValueType()));
399
400 if (InFlag.getNode())
401 Ops.push_back(InFlag);
402
403 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
404 InFlag = Chain.getValue(1);
405
406 // Create the CALLSEQ_END node.
407 Chain = DAG.getCALLSEQ_END(Chain,
408 DAG.getConstant(NumBytes, getPointerTy(), true),
409 DAG.getConstant(0, getPointerTy(), true),
410 InFlag);
411 InFlag = Chain.getValue(1);
412
413 // Handle result values, copying them out of physregs into vregs that we
414 // return.
415 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
416 Op.getResNo());
417}
418
419/// LowerCallResult - Lower the result values of an ISD::CALL into the
420/// appropriate copies out of appropriate physical registers. This assumes that
421/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
422/// being lowered. Returns a SDNode with the same number of values as the
423/// ISD::CALL.
424SDNode*
425SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
426 CallSDNode *TheCall,
427 unsigned CallingConv,
428 SelectionDAG &DAG) {
429 bool isVarArg = TheCall->isVarArg();
430 DebugLoc dl = TheCall->getDebugLoc();
431
432 // Assign locations to each value returned by this call.
433 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000434 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs,
435 DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000436
437 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
438 SmallVector<SDValue, 8> ResultVals;
439
440 // Copy all of the result registers out of their specified physreg.
441 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000442 CCValAssign &VA = RVLocs[i];
443
444 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
445 VA.getLocVT(), InFlag).getValue(1);
446 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000447 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000448
449 // If this is an 8/16/32-bit value, it is really passed promoted to 64
450 // bits. Insert an assert[sz]ext to capture this, then truncate to the
451 // right size.
452 if (VA.getLocInfo() == CCValAssign::SExt)
453 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
454 DAG.getValueType(VA.getValVT()));
455 else if (VA.getLocInfo() == CCValAssign::ZExt)
456 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
457 DAG.getValueType(VA.getValVT()));
458
459 if (VA.getLocInfo() != CCValAssign::Full)
460 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
461
462 ResultVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000463 }
464
465 ResultVals.push_back(Chain);
466
467 // Merge everything together with a MERGE_VALUES node.
468 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
469 &ResultVals[0], ResultVals.size()).getNode();
470}
471
472
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000473SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
474 // CCValAssign - represent the assignment of the return value to a location
475 SmallVector<CCValAssign, 16> RVLocs;
476 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
477 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
478 DebugLoc dl = Op.getDebugLoc();
479
480 // CCState - Info about the registers and stack slot.
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000481 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000482
483 // Analize return values of ISD::RET
484 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
485
486 // If this is the first return lowered for this function, add the regs to the
487 // liveout set for the function.
488 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
489 for (unsigned i = 0; i != RVLocs.size(); ++i)
490 if (RVLocs[i].isRegLoc())
491 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
492 }
493
494 // The chain is always operand #0
495 SDValue Chain = Op.getOperand(0);
496 SDValue Flag;
497
498 // Copy the result values into the output registers.
499 for (unsigned i = 0; i != RVLocs.size(); ++i) {
500 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000501 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000502 assert(VA.isRegLoc() && "Can only return in registers!");
503
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000504 // If this is an 8/16/32-bit value, it is really should be passed promoted
505 // to 64 bits.
506 if (VA.getLocInfo() == CCValAssign::SExt)
507 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
508 else if (VA.getLocInfo() == CCValAssign::ZExt)
509 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
510 else if (VA.getLocInfo() == CCValAssign::AExt)
511 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
512
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000513 // ISD::RET => ret chain, (regnum1,val1), ...
514 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000515 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000516
517 // Guarantee that all emitted copies are stuck together,
518 // avoiding something bad.
519 Flag = Chain.getValue(1);
520 }
521
522 if (Flag.getNode())
523 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
524
525 // Return Void
526 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
527}
528
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000529SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
530 ISD::CondCode CC, SDValue &SystemZCC,
531 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000532 // FIXME: Emit a test if RHS is zero
533
534 bool isUnsigned = false;
535 SystemZCC::CondCodes TCC;
536 switch (CC) {
537 default: assert(0 && "Invalid integer condition!");
538 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000539 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000540 TCC = SystemZCC::E;
541 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000542 case ISD::SETUEQ:
543 TCC = SystemZCC::NLH;
544 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000545 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000546 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000547 TCC = SystemZCC::NE;
548 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000549 case ISD::SETUNE:
550 TCC = SystemZCC::LH;
551 break;
552 case ISD::SETO:
553 TCC = SystemZCC::O;
554 break;
555 case ISD::SETUO:
556 TCC = SystemZCC::NO;
557 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000558 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000559 if (LHS.getValueType().isFloatingPoint()) {
560 TCC = SystemZCC::NH;
561 break;
562 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000563 isUnsigned = true; // FALLTHROUGH
564 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000565 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000566 TCC = SystemZCC::LE;
567 break;
568 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000569 if (LHS.getValueType().isFloatingPoint()) {
570 TCC = SystemZCC::NL;
571 break;
572 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000573 isUnsigned = true; // FALLTHROUGH
574 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000575 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000576 TCC = SystemZCC::HE;
577 break;
578 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000579 if (LHS.getValueType().isFloatingPoint()) {
580 TCC = SystemZCC::NLE;
581 break;
582 }
583 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000584 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000585 case ISD::SETOGT:
586 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000587 break;
588 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000589 if (LHS.getValueType().isFloatingPoint()) {
590 TCC = SystemZCC::NHE;
591 break;
592 }
593 isUnsigned = true; // FALLTHROUGH
594 case ISD::SETLT:
595 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000596 TCC = SystemZCC::L;
597 break;
598 }
599
600 SystemZCC = DAG.getConstant(TCC, MVT::i32);
601
602 DebugLoc dl = LHS.getDebugLoc();
603 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
604 dl, MVT::Flag, LHS, RHS);
605}
606
607
608SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
609 SDValue Chain = Op.getOperand(0);
610 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
611 SDValue LHS = Op.getOperand(2);
612 SDValue RHS = Op.getOperand(3);
613 SDValue Dest = Op.getOperand(4);
614 DebugLoc dl = Op.getDebugLoc();
615
616 SDValue SystemZCC;
617 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
618 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
619 Chain, Dest, SystemZCC, Flag);
620}
621
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000622SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
623 SDValue LHS = Op.getOperand(0);
624 SDValue RHS = Op.getOperand(1);
625 SDValue TrueV = Op.getOperand(2);
626 SDValue FalseV = Op.getOperand(3);
627 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
628 DebugLoc dl = Op.getDebugLoc();
629
630 SDValue SystemZCC;
631 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
632
633 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
634 SmallVector<SDValue, 4> Ops;
635 Ops.push_back(TrueV);
636 Ops.push_back(FalseV);
637 Ops.push_back(SystemZCC);
638 Ops.push_back(Flag);
639
640 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
641}
642
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000643SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
644 SelectionDAG &DAG) {
645 DebugLoc dl = Op.getDebugLoc();
646 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000647 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000648
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000649 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
650 bool ExtraLoadRequired =
651 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
652
653 SDValue Result;
654 if (!IsPic && !ExtraLoadRequired) {
655 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
656 Offset = 0;
657 } else {
658 unsigned char OpFlags = 0;
659 if (ExtraLoadRequired)
660 OpFlags = SystemZII::MO_GOTENT;
661
662 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
663 }
664
665 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
666 getPointerTy(), Result);
667
668 if (ExtraLoadRequired)
669 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
670 PseudoSourceValue::getGOT(), 0);
671
672 // If there was a non-zero offset that we didn't fold, create an explicit
673 // addition for it.
674 if (Offset != 0)
675 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
676 DAG.getConstant(Offset, getPointerTy()));
677
678 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000679}
680
Anton Korobeynikovae535672009-07-16 14:19:35 +0000681// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000682SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
683 SelectionDAG &DAG) {
684 DebugLoc dl = Op.getDebugLoc();
685 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
686 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
687
688 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
689}
690
Anton Korobeynikovae535672009-07-16 14:19:35 +0000691
692// FIXME: PIC here
693// FIXME: This is just dirty hack. We need to lower cpool properly
694SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
695 SelectionDAG &DAG) {
696 DebugLoc dl = Op.getDebugLoc();
697 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
698
699 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
700 CP->getAlignment(),
701 CP->getOffset());
702
703 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
704}
705
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000706const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
707 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000708 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000709 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000710 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
711 case SystemZISD::CMP: return "SystemZISD::CMP";
712 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000713 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000714 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000715 default: return NULL;
716 }
717}
718
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000719//===----------------------------------------------------------------------===//
720// Other Lowering Code
721//===----------------------------------------------------------------------===//
722
723MachineBasicBlock*
724SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
725 MachineBasicBlock *BB) const {
726 const SystemZInstrInfo &TII = *TM.getInstrInfo();
727 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000728 assert((MI->getOpcode() == SystemZ::Select32 ||
729 MI->getOpcode() == SystemZ::SelectF32 ||
730 MI->getOpcode() == SystemZ::Select64 ||
731 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000732 "Unexpected instr type to insert");
733
734 // To "insert" a SELECT instruction, we actually have to insert the diamond
735 // control-flow pattern. The incoming instruction knows the destination vreg
736 // to set, the condition code register to branch on, the true/false values to
737 // select between, and a branch opcode to use.
738 const BasicBlock *LLVM_BB = BB->getBasicBlock();
739 MachineFunction::iterator I = BB;
740 ++I;
741
742 // thisMBB:
743 // ...
744 // TrueVal = ...
745 // cmpTY ccX, r1, r2
746 // jCC copy1MBB
747 // fallthrough --> copy0MBB
748 MachineBasicBlock *thisMBB = BB;
749 MachineFunction *F = BB->getParent();
750 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
751 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
752 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
753 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
754 F->insert(I, copy0MBB);
755 F->insert(I, copy1MBB);
756 // Update machine-CFG edges by transferring all successors of the current
757 // block to the new block which will contain the Phi node for the select.
758 copy1MBB->transferSuccessors(BB);
759 // Next, add the true and fallthrough blocks as its successors.
760 BB->addSuccessor(copy0MBB);
761 BB->addSuccessor(copy1MBB);
762
763 // copy0MBB:
764 // %FalseValue = ...
765 // # fallthrough to copy1MBB
766 BB = copy0MBB;
767
768 // Update machine-CFG edges
769 BB->addSuccessor(copy1MBB);
770
771 // copy1MBB:
772 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
773 // ...
774 BB = copy1MBB;
775 BuildMI(BB, dl, TII.get(SystemZ::PHI),
776 MI->getOperand(0).getReg())
777 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
778 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
779
780 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
781 return BB;
782}