blob: 7538bdc790a6bb8d31ae07bbe7b688c488805b89 [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);
Anton Korobeynikov1ada84d2009-07-16 14:24:16 +000054
55 addLegalFPImmediate(APFloat(+0.0)); // lzer
56 addLegalFPImmediate(APFloat(+0.0f)); // lzdr
57 addLegalFPImmediate(APFloat(-0.0)); // lzer + lner
58 addLegalFPImmediate(APFloat(-0.0f)); // lzdr + lndr
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000059 }
60
Anton Korobeynikov4403b932009-07-16 13:27:25 +000061 // Compute derived properties from the register classes
62 computeRegisterProperties();
63
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000064 // Set shifts properties
65 setShiftAmountFlavor(Extend);
Anton Korobeynikov48e8b3c2009-07-16 14:15:24 +000066 setShiftAmountType(MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000067
Anton Korobeynikov4403b932009-07-16 13:27:25 +000068 // Provide all sorts of operation actions
Anton Korobeynikovbf022172009-07-16 13:53:35 +000069 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
70 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
71 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000072
Anton Korobeynikov85c5c3f2009-07-16 14:22:46 +000073 setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Expand);
74 setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Expand);
75 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Anton Korobeynikov299dc782009-07-16 14:22:30 +000076
Anton Korobeynikov85c5c3f2009-07-16 14:22:46 +000077 setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Expand);
78 setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Expand);
79 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
Anton Korobeynikov23eff5c2009-07-16 14:20:08 +000080
Anton Korobeynikove0167c12009-07-16 13:35:30 +000081 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000082 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000083
84 setOperationAction(ISD::RET, MVT::Other, Custom);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000085
Anton Korobeynikov983d3a12009-07-16 14:07:24 +000086 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000087 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
88 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
89 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +000090 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
91 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
92 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
93 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +000094 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +000095 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Anton Korobeynikovc772c442009-07-16 14:08:15 +000096 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000097
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000098 setOperationAction(ISD::SDIV, MVT::i32, Expand);
99 setOperationAction(ISD::UDIV, MVT::i32, Expand);
100 setOperationAction(ISD::SDIV, MVT::i64, Expand);
101 setOperationAction(ISD::UDIV, MVT::i64, Expand);
102 setOperationAction(ISD::SREM, MVT::i32, Expand);
103 setOperationAction(ISD::UREM, MVT::i32, Expand);
104 setOperationAction(ISD::SREM, MVT::i64, Expand);
105 setOperationAction(ISD::UREM, MVT::i64, Expand);
106
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000107 // FIXME: Can we lower these 2 efficiently?
108 setOperationAction(ISD::SETCC, MVT::i32, Expand);
109 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000110 setOperationAction(ISD::SETCC, MVT::f32, Expand);
111 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000112 setOperationAction(ISD::SELECT, MVT::i32, Expand);
113 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000114 setOperationAction(ISD::SELECT, MVT::f32, Expand);
115 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000116 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
117 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000118 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
119 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000120
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +0000121 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
122 // unsigned.
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000123 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000124 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000125
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000126 // Lower some FP stuff
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000127 setOperationAction(ISD::FSIN, MVT::f32, Expand);
128 setOperationAction(ISD::FSIN, MVT::f64, Expand);
129 setOperationAction(ISD::FCOS, MVT::f32, Expand);
130 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Anton Korobeynikova89430e2009-07-16 14:25:30 +0000131 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
132 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000133
134 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000135}
136
137SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
138 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000139 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
140 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000141 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000142 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000143 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000144 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000145 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000146 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000147 default:
148 assert(0 && "unimplemented operand");
149 return SDValue();
150 }
151}
152
153//===----------------------------------------------------------------------===//
154// Calling Convention Implementation
155//===----------------------------------------------------------------------===//
156
157#include "SystemZGenCallingConv.inc"
158
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000159SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
160 SelectionDAG &DAG) {
161 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
162 switch (CC) {
163 default:
164 assert(0 && "Unsupported calling convention");
165 case CallingConv::C:
166 case CallingConv::Fast:
167 return LowerCCCArguments(Op, DAG);
168 }
169}
170
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000171SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
172 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
173 unsigned CallingConv = TheCall->getCallingConv();
174 switch (CallingConv) {
175 default:
176 assert(0 && "Unsupported calling convention");
177 case CallingConv::Fast:
178 case CallingConv::C:
179 return LowerCCCCallTo(Op, DAG, CallingConv);
180 }
181}
182
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000183/// LowerCCCArguments - transform physical registers into virtual registers and
184/// generate load operations for arguments places on the stack.
185// FIXME: struct return stuff
186// FIXME: varargs
187SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
188 SelectionDAG &DAG) {
189 MachineFunction &MF = DAG.getMachineFunction();
190 MachineFrameInfo *MFI = MF.getFrameInfo();
191 MachineRegisterInfo &RegInfo = MF.getRegInfo();
192 SDValue Root = Op.getOperand(0);
193 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
194 unsigned CC = MF.getFunction()->getCallingConv();
195 DebugLoc dl = Op.getDebugLoc();
196
197 // Assign locations to all of the incoming arguments.
198 SmallVector<CCValAssign, 16> ArgLocs;
199 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
200 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
201
202 assert(!isVarArg && "Varargs not supported yet");
203
204 SmallVector<SDValue, 16> ArgValues;
205 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
206 CCValAssign &VA = ArgLocs[i];
207 if (VA.isRegLoc()) {
208 // Arguments passed in registers
209 MVT RegVT = VA.getLocVT();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000210 TargetRegisterClass *RC;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000211 switch (RegVT.getSimpleVT()) {
212 default:
213 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
214 << RegVT.getSimpleVT()
215 << "\n";
216 abort();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000217 case MVT::i64:
218 RC = SystemZ::GR64RegisterClass;
219 break;
220 case MVT::f32:
221 RC = SystemZ::FP32RegisterClass;
222 break;
223 case MVT::f64:
224 RC = SystemZ::FP64RegisterClass;
225 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000226 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000227
228 unsigned VReg = RegInfo.createVirtualRegister(RC);
229 RegInfo.addLiveIn(VA.getLocReg(), VReg);
230 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
231
232 // If this is an 8/16/32-bit value, it is really passed promoted to 64
233 // bits. Insert an assert[sz]ext to capture this, then truncate to the
234 // right size.
235 if (VA.getLocInfo() == CCValAssign::SExt)
236 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
237 DAG.getValueType(VA.getValVT()));
238 else if (VA.getLocInfo() == CCValAssign::ZExt)
239 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
240 DAG.getValueType(VA.getValVT()));
241
242 if (VA.getLocInfo() != CCValAssign::Full)
243 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
244
245 ArgValues.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000246 } else {
247 // Sanity check
248 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000249
250 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000251 // Create the frame index object for this incoming parameter...
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000252 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
253 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000254
255 // Create the SelectionDAG nodes corresponding to a load
256 //from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000257 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
258 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN,
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000259 PseudoSourceValue::getFixedStack(FI), 0));
260 }
261 }
262
263 ArgValues.push_back(Root);
264
265 // Return the new list of results.
266 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
267 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
268}
269
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000270/// LowerCCCCallTo - functions arguments are copied from virtual regs to
271/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
272/// TODO: sret.
273SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
274 unsigned CC) {
275 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
276 SDValue Chain = TheCall->getChain();
277 SDValue Callee = TheCall->getCallee();
278 bool isVarArg = TheCall->isVarArg();
279 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000280 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000281
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000282 // Offset to first argument stack slot.
283 const unsigned FirstArgOffset = 160;
284
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000285 // Analyze operands of the call, assigning locations to each operand.
286 SmallVector<CCValAssign, 16> ArgLocs;
287 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
288
289 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
290
291 // Get a count of how many bytes are to be pushed on the stack.
292 unsigned NumBytes = CCInfo.getNextStackOffset();
293
294 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
295 getPointerTy(), true));
296
297 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
298 SmallVector<SDValue, 12> MemOpChains;
299 SDValue StackPtr;
300
301 // Walk the register/memloc assignments, inserting copies/loads.
302 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
303 CCValAssign &VA = ArgLocs[i];
304
305 // Arguments start after the 5 first operands of ISD::CALL
306 SDValue Arg = TheCall->getArg(i);
307
308 // Promote the value if needed.
309 switch (VA.getLocInfo()) {
310 default: assert(0 && "Unknown loc info!");
311 case CCValAssign::Full: break;
312 case CCValAssign::SExt:
313 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
314 break;
315 case CCValAssign::ZExt:
316 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
317 break;
318 case CCValAssign::AExt:
319 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
320 break;
321 }
322
323 // Arguments that can be passed on register must be kept at RegsToPass
324 // vector
325 if (VA.isRegLoc()) {
326 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
327 } else {
328 assert(VA.isMemLoc());
329
330 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000331 StackPtr =
332 DAG.getCopyFromReg(Chain, dl,
333 (RegInfo->hasFP(MF) ?
334 SystemZ::R11D : SystemZ::R15D),
335 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000336
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000337 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
338 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
339 StackPtr,
340 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000341
342 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000343 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000344 }
345 }
346
347 // Transform all store nodes into one single node because all store nodes are
348 // independent of each other.
349 if (!MemOpChains.empty())
350 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
351 &MemOpChains[0], MemOpChains.size());
352
353 // Build a sequence of copy-to-reg nodes chained together with token chain and
354 // flag operands which copy the outgoing args into registers. The InFlag in
355 // necessary since all emited instructions must be stuck together.
356 SDValue InFlag;
357 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
358 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
359 RegsToPass[i].second, InFlag);
360 InFlag = Chain.getValue(1);
361 }
362
363 // If the callee is a GlobalAddress node (quite common, every direct call is)
364 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
365 // Likewise ExternalSymbol -> TargetExternalSymbol.
366 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
367 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
368 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
369 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
370
371 // Returns a chain & a flag for retval copy to use.
372 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
373 SmallVector<SDValue, 8> Ops;
374 Ops.push_back(Chain);
375 Ops.push_back(Callee);
376
377 // Add argument registers to the end of the list so that they are
378 // known live into the call.
379 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
380 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
381 RegsToPass[i].second.getValueType()));
382
383 if (InFlag.getNode())
384 Ops.push_back(InFlag);
385
386 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
387 InFlag = Chain.getValue(1);
388
389 // Create the CALLSEQ_END node.
390 Chain = DAG.getCALLSEQ_END(Chain,
391 DAG.getConstant(NumBytes, getPointerTy(), true),
392 DAG.getConstant(0, getPointerTy(), true),
393 InFlag);
394 InFlag = Chain.getValue(1);
395
396 // Handle result values, copying them out of physregs into vregs that we
397 // return.
398 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
399 Op.getResNo());
400}
401
402/// LowerCallResult - Lower the result values of an ISD::CALL into the
403/// appropriate copies out of appropriate physical registers. This assumes that
404/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
405/// being lowered. Returns a SDNode with the same number of values as the
406/// ISD::CALL.
407SDNode*
408SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
409 CallSDNode *TheCall,
410 unsigned CallingConv,
411 SelectionDAG &DAG) {
412 bool isVarArg = TheCall->isVarArg();
413 DebugLoc dl = TheCall->getDebugLoc();
414
415 // Assign locations to each value returned by this call.
416 SmallVector<CCValAssign, 16> RVLocs;
417 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
418
419 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
420 SmallVector<SDValue, 8> ResultVals;
421
422 // Copy all of the result registers out of their specified physreg.
423 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000424 CCValAssign &VA = RVLocs[i];
425
426 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
427 VA.getLocVT(), InFlag).getValue(1);
428 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000429 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000430
431 // If this is an 8/16/32-bit value, it is really passed promoted to 64
432 // bits. Insert an assert[sz]ext to capture this, then truncate to the
433 // right size.
434 if (VA.getLocInfo() == CCValAssign::SExt)
435 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
436 DAG.getValueType(VA.getValVT()));
437 else if (VA.getLocInfo() == CCValAssign::ZExt)
438 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
439 DAG.getValueType(VA.getValVT()));
440
441 if (VA.getLocInfo() != CCValAssign::Full)
442 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
443
444 ResultVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000445 }
446
447 ResultVals.push_back(Chain);
448
449 // Merge everything together with a MERGE_VALUES node.
450 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
451 &ResultVals[0], ResultVals.size()).getNode();
452}
453
454
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000455SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
456 // CCValAssign - represent the assignment of the return value to a location
457 SmallVector<CCValAssign, 16> RVLocs;
458 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
459 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
460 DebugLoc dl = Op.getDebugLoc();
461
462 // CCState - Info about the registers and stack slot.
463 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
464
465 // Analize return values of ISD::RET
466 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
467
468 // If this is the first return lowered for this function, add the regs to the
469 // liveout set for the function.
470 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
471 for (unsigned i = 0; i != RVLocs.size(); ++i)
472 if (RVLocs[i].isRegLoc())
473 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
474 }
475
476 // The chain is always operand #0
477 SDValue Chain = Op.getOperand(0);
478 SDValue Flag;
479
480 // Copy the result values into the output registers.
481 for (unsigned i = 0; i != RVLocs.size(); ++i) {
482 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000483 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000484 assert(VA.isRegLoc() && "Can only return in registers!");
485
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000486 // If this is an 8/16/32-bit value, it is really should be passed promoted
487 // to 64 bits.
488 if (VA.getLocInfo() == CCValAssign::SExt)
489 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
490 else if (VA.getLocInfo() == CCValAssign::ZExt)
491 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
492 else if (VA.getLocInfo() == CCValAssign::AExt)
493 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
494
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000495 // ISD::RET => ret chain, (regnum1,val1), ...
496 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000497 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000498
499 // Guarantee that all emitted copies are stuck together,
500 // avoiding something bad.
501 Flag = Chain.getValue(1);
502 }
503
504 if (Flag.getNode())
505 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
506
507 // Return Void
508 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
509}
510
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000511SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
512 ISD::CondCode CC, SDValue &SystemZCC,
513 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000514 // FIXME: Emit a test if RHS is zero
515
516 bool isUnsigned = false;
517 SystemZCC::CondCodes TCC;
518 switch (CC) {
519 default: assert(0 && "Invalid integer condition!");
520 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000521 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000522 TCC = SystemZCC::E;
523 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000524 case ISD::SETUEQ:
525 TCC = SystemZCC::NLH;
526 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000527 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000528 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000529 TCC = SystemZCC::NE;
530 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000531 case ISD::SETUNE:
532 TCC = SystemZCC::LH;
533 break;
534 case ISD::SETO:
535 TCC = SystemZCC::O;
536 break;
537 case ISD::SETUO:
538 TCC = SystemZCC::NO;
539 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000540 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000541 if (LHS.getValueType().isFloatingPoint()) {
542 TCC = SystemZCC::NH;
543 break;
544 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000545 isUnsigned = true; // FALLTHROUGH
546 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000547 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000548 TCC = SystemZCC::LE;
549 break;
550 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000551 if (LHS.getValueType().isFloatingPoint()) {
552 TCC = SystemZCC::NL;
553 break;
554 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000555 isUnsigned = true; // FALLTHROUGH
556 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000557 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000558 TCC = SystemZCC::HE;
559 break;
560 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000561 if (LHS.getValueType().isFloatingPoint()) {
562 TCC = SystemZCC::NLE;
563 break;
564 }
565 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000566 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000567 case ISD::SETOGT:
568 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000569 break;
570 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000571 if (LHS.getValueType().isFloatingPoint()) {
572 TCC = SystemZCC::NHE;
573 break;
574 }
575 isUnsigned = true; // FALLTHROUGH
576 case ISD::SETLT:
577 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000578 TCC = SystemZCC::L;
579 break;
580 }
581
582 SystemZCC = DAG.getConstant(TCC, MVT::i32);
583
584 DebugLoc dl = LHS.getDebugLoc();
585 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
586 dl, MVT::Flag, LHS, RHS);
587}
588
589
590SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
591 SDValue Chain = Op.getOperand(0);
592 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
593 SDValue LHS = Op.getOperand(2);
594 SDValue RHS = Op.getOperand(3);
595 SDValue Dest = Op.getOperand(4);
596 DebugLoc dl = Op.getDebugLoc();
597
598 SDValue SystemZCC;
599 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
600 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
601 Chain, Dest, SystemZCC, Flag);
602}
603
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000604SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
605 SDValue LHS = Op.getOperand(0);
606 SDValue RHS = Op.getOperand(1);
607 SDValue TrueV = Op.getOperand(2);
608 SDValue FalseV = Op.getOperand(3);
609 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
610 DebugLoc dl = Op.getDebugLoc();
611
612 SDValue SystemZCC;
613 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
614
615 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
616 SmallVector<SDValue, 4> Ops;
617 Ops.push_back(TrueV);
618 Ops.push_back(FalseV);
619 Ops.push_back(SystemZCC);
620 Ops.push_back(Flag);
621
622 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
623}
624
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000625SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
626 SelectionDAG &DAG) {
627 DebugLoc dl = Op.getDebugLoc();
628 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000629 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000630
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000631 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
632 bool ExtraLoadRequired =
633 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
634
635 SDValue Result;
636 if (!IsPic && !ExtraLoadRequired) {
637 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
638 Offset = 0;
639 } else {
640 unsigned char OpFlags = 0;
641 if (ExtraLoadRequired)
642 OpFlags = SystemZII::MO_GOTENT;
643
644 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
645 }
646
647 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
648 getPointerTy(), Result);
649
650 if (ExtraLoadRequired)
651 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
652 PseudoSourceValue::getGOT(), 0);
653
654 // If there was a non-zero offset that we didn't fold, create an explicit
655 // addition for it.
656 if (Offset != 0)
657 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
658 DAG.getConstant(Offset, getPointerTy()));
659
660 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000661}
662
Anton Korobeynikovae535672009-07-16 14:19:35 +0000663// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000664SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
665 SelectionDAG &DAG) {
666 DebugLoc dl = Op.getDebugLoc();
667 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
668 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
669
670 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
671}
672
Anton Korobeynikovae535672009-07-16 14:19:35 +0000673
674// FIXME: PIC here
675// FIXME: This is just dirty hack. We need to lower cpool properly
676SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
677 SelectionDAG &DAG) {
678 DebugLoc dl = Op.getDebugLoc();
679 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
680
681 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
682 CP->getAlignment(),
683 CP->getOffset());
684
685 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
686}
687
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000688const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
689 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000690 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000691 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000692 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
693 case SystemZISD::CMP: return "SystemZISD::CMP";
694 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000695 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000696 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000697 default: return NULL;
698 }
699}
700
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000701//===----------------------------------------------------------------------===//
702// Other Lowering Code
703//===----------------------------------------------------------------------===//
704
705MachineBasicBlock*
706SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
707 MachineBasicBlock *BB) const {
708 const SystemZInstrInfo &TII = *TM.getInstrInfo();
709 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000710 assert((MI->getOpcode() == SystemZ::Select32 ||
711 MI->getOpcode() == SystemZ::SelectF32 ||
712 MI->getOpcode() == SystemZ::Select64 ||
713 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000714 "Unexpected instr type to insert");
715
716 // To "insert" a SELECT instruction, we actually have to insert the diamond
717 // control-flow pattern. The incoming instruction knows the destination vreg
718 // to set, the condition code register to branch on, the true/false values to
719 // select between, and a branch opcode to use.
720 const BasicBlock *LLVM_BB = BB->getBasicBlock();
721 MachineFunction::iterator I = BB;
722 ++I;
723
724 // thisMBB:
725 // ...
726 // TrueVal = ...
727 // cmpTY ccX, r1, r2
728 // jCC copy1MBB
729 // fallthrough --> copy0MBB
730 MachineBasicBlock *thisMBB = BB;
731 MachineFunction *F = BB->getParent();
732 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
733 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
734 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
735 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
736 F->insert(I, copy0MBB);
737 F->insert(I, copy1MBB);
738 // Update machine-CFG edges by transferring all successors of the current
739 // block to the new block which will contain the Phi node for the select.
740 copy1MBB->transferSuccessors(BB);
741 // Next, add the true and fallthrough blocks as its successors.
742 BB->addSuccessor(copy0MBB);
743 BB->addSuccessor(copy1MBB);
744
745 // copy0MBB:
746 // %FalseValue = ...
747 // # fallthrough to copy1MBB
748 BB = copy0MBB;
749
750 // Update machine-CFG edges
751 BB->addSuccessor(copy1MBB);
752
753 // copy1MBB:
754 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
755 // ...
756 BB = copy1MBB;
757 BuildMI(BB, dl, TII.get(SystemZ::PHI),
758 MI->getOperand(0).getReg())
759 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
760 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
761
762 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
763 return BB;
764}