blob: 757255e3ebb04f7057f3c11ae5b469f06d6643f5 [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 Korobeynikov98db78a2009-07-16 14:26:06 +0000131
Anton Korobeynikova89430e2009-07-16 14:25:30 +0000132 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
133 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000134 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
135 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000136
137 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000138}
139
140SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
141 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000142 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
143 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000144 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000145 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000146 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000147 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000148 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000149 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000150 default:
151 assert(0 && "unimplemented operand");
152 return SDValue();
153 }
154}
155
156//===----------------------------------------------------------------------===//
157// Calling Convention Implementation
158//===----------------------------------------------------------------------===//
159
160#include "SystemZGenCallingConv.inc"
161
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000162SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
163 SelectionDAG &DAG) {
164 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
165 switch (CC) {
166 default:
167 assert(0 && "Unsupported calling convention");
168 case CallingConv::C:
169 case CallingConv::Fast:
170 return LowerCCCArguments(Op, DAG);
171 }
172}
173
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000174SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
175 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
176 unsigned CallingConv = TheCall->getCallingConv();
177 switch (CallingConv) {
178 default:
179 assert(0 && "Unsupported calling convention");
180 case CallingConv::Fast:
181 case CallingConv::C:
182 return LowerCCCCallTo(Op, DAG, CallingConv);
183 }
184}
185
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000186/// LowerCCCArguments - transform physical registers into virtual registers and
187/// generate load operations for arguments places on the stack.
188// FIXME: struct return stuff
189// FIXME: varargs
190SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
191 SelectionDAG &DAG) {
192 MachineFunction &MF = DAG.getMachineFunction();
193 MachineFrameInfo *MFI = MF.getFrameInfo();
194 MachineRegisterInfo &RegInfo = MF.getRegInfo();
195 SDValue Root = Op.getOperand(0);
196 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
197 unsigned CC = MF.getFunction()->getCallingConv();
198 DebugLoc dl = Op.getDebugLoc();
199
200 // Assign locations to all of the incoming arguments.
201 SmallVector<CCValAssign, 16> ArgLocs;
202 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
203 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
204
205 assert(!isVarArg && "Varargs not supported yet");
206
207 SmallVector<SDValue, 16> ArgValues;
208 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
209 CCValAssign &VA = ArgLocs[i];
210 if (VA.isRegLoc()) {
211 // Arguments passed in registers
212 MVT RegVT = VA.getLocVT();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000213 TargetRegisterClass *RC;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000214 switch (RegVT.getSimpleVT()) {
215 default:
216 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
217 << RegVT.getSimpleVT()
218 << "\n";
219 abort();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000220 case MVT::i64:
221 RC = SystemZ::GR64RegisterClass;
222 break;
223 case MVT::f32:
224 RC = SystemZ::FP32RegisterClass;
225 break;
226 case MVT::f64:
227 RC = SystemZ::FP64RegisterClass;
228 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000229 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000230
231 unsigned VReg = RegInfo.createVirtualRegister(RC);
232 RegInfo.addLiveIn(VA.getLocReg(), VReg);
233 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
234
235 // If this is an 8/16/32-bit value, it is really passed promoted to 64
236 // bits. Insert an assert[sz]ext to capture this, then truncate to the
237 // right size.
238 if (VA.getLocInfo() == CCValAssign::SExt)
239 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
240 DAG.getValueType(VA.getValVT()));
241 else if (VA.getLocInfo() == CCValAssign::ZExt)
242 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
243 DAG.getValueType(VA.getValVT()));
244
245 if (VA.getLocInfo() != CCValAssign::Full)
246 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
247
248 ArgValues.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000249 } else {
250 // Sanity check
251 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000252
253 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000254 // Create the frame index object for this incoming parameter...
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000255 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
256 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000257
258 // Create the SelectionDAG nodes corresponding to a load
259 //from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000260 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
261 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN,
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000262 PseudoSourceValue::getFixedStack(FI), 0));
263 }
264 }
265
266 ArgValues.push_back(Root);
267
268 // Return the new list of results.
269 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
270 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
271}
272
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000273/// LowerCCCCallTo - functions arguments are copied from virtual regs to
274/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
275/// TODO: sret.
276SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
277 unsigned CC) {
278 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
279 SDValue Chain = TheCall->getChain();
280 SDValue Callee = TheCall->getCallee();
281 bool isVarArg = TheCall->isVarArg();
282 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000283 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000284
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000285 // Offset to first argument stack slot.
286 const unsigned FirstArgOffset = 160;
287
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000288 // Analyze operands of the call, assigning locations to each operand.
289 SmallVector<CCValAssign, 16> ArgLocs;
290 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
291
292 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
293
294 // Get a count of how many bytes are to be pushed on the stack.
295 unsigned NumBytes = CCInfo.getNextStackOffset();
296
297 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
298 getPointerTy(), true));
299
300 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
301 SmallVector<SDValue, 12> MemOpChains;
302 SDValue StackPtr;
303
304 // Walk the register/memloc assignments, inserting copies/loads.
305 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
306 CCValAssign &VA = ArgLocs[i];
307
308 // Arguments start after the 5 first operands of ISD::CALL
309 SDValue Arg = TheCall->getArg(i);
310
311 // Promote the value if needed.
312 switch (VA.getLocInfo()) {
313 default: assert(0 && "Unknown loc info!");
314 case CCValAssign::Full: break;
315 case CCValAssign::SExt:
316 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
317 break;
318 case CCValAssign::ZExt:
319 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
320 break;
321 case CCValAssign::AExt:
322 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
323 break;
324 }
325
326 // Arguments that can be passed on register must be kept at RegsToPass
327 // vector
328 if (VA.isRegLoc()) {
329 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
330 } else {
331 assert(VA.isMemLoc());
332
333 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000334 StackPtr =
335 DAG.getCopyFromReg(Chain, dl,
336 (RegInfo->hasFP(MF) ?
337 SystemZ::R11D : SystemZ::R15D),
338 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000339
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000340 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
341 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
342 StackPtr,
343 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000344
345 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000346 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000347 }
348 }
349
350 // Transform all store nodes into one single node because all store nodes are
351 // independent of each other.
352 if (!MemOpChains.empty())
353 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
354 &MemOpChains[0], MemOpChains.size());
355
356 // Build a sequence of copy-to-reg nodes chained together with token chain and
357 // flag operands which copy the outgoing args into registers. The InFlag in
358 // necessary since all emited instructions must be stuck together.
359 SDValue InFlag;
360 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
361 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
362 RegsToPass[i].second, InFlag);
363 InFlag = Chain.getValue(1);
364 }
365
366 // If the callee is a GlobalAddress node (quite common, every direct call is)
367 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
368 // Likewise ExternalSymbol -> TargetExternalSymbol.
369 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
370 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
371 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
372 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
373
374 // Returns a chain & a flag for retval copy to use.
375 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
376 SmallVector<SDValue, 8> Ops;
377 Ops.push_back(Chain);
378 Ops.push_back(Callee);
379
380 // Add argument registers to the end of the list so that they are
381 // known live into the call.
382 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
383 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
384 RegsToPass[i].second.getValueType()));
385
386 if (InFlag.getNode())
387 Ops.push_back(InFlag);
388
389 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
390 InFlag = Chain.getValue(1);
391
392 // Create the CALLSEQ_END node.
393 Chain = DAG.getCALLSEQ_END(Chain,
394 DAG.getConstant(NumBytes, getPointerTy(), true),
395 DAG.getConstant(0, getPointerTy(), true),
396 InFlag);
397 InFlag = Chain.getValue(1);
398
399 // Handle result values, copying them out of physregs into vregs that we
400 // return.
401 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
402 Op.getResNo());
403}
404
405/// LowerCallResult - Lower the result values of an ISD::CALL into the
406/// appropriate copies out of appropriate physical registers. This assumes that
407/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
408/// being lowered. Returns a SDNode with the same number of values as the
409/// ISD::CALL.
410SDNode*
411SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
412 CallSDNode *TheCall,
413 unsigned CallingConv,
414 SelectionDAG &DAG) {
415 bool isVarArg = TheCall->isVarArg();
416 DebugLoc dl = TheCall->getDebugLoc();
417
418 // Assign locations to each value returned by this call.
419 SmallVector<CCValAssign, 16> RVLocs;
420 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
421
422 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
423 SmallVector<SDValue, 8> ResultVals;
424
425 // Copy all of the result registers out of their specified physreg.
426 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000427 CCValAssign &VA = RVLocs[i];
428
429 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
430 VA.getLocVT(), InFlag).getValue(1);
431 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000432 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000433
434 // If this is an 8/16/32-bit value, it is really passed promoted to 64
435 // bits. Insert an assert[sz]ext to capture this, then truncate to the
436 // right size.
437 if (VA.getLocInfo() == CCValAssign::SExt)
438 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
439 DAG.getValueType(VA.getValVT()));
440 else if (VA.getLocInfo() == CCValAssign::ZExt)
441 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
442 DAG.getValueType(VA.getValVT()));
443
444 if (VA.getLocInfo() != CCValAssign::Full)
445 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
446
447 ResultVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000448 }
449
450 ResultVals.push_back(Chain);
451
452 // Merge everything together with a MERGE_VALUES node.
453 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
454 &ResultVals[0], ResultVals.size()).getNode();
455}
456
457
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000458SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
459 // CCValAssign - represent the assignment of the return value to a location
460 SmallVector<CCValAssign, 16> RVLocs;
461 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
462 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
463 DebugLoc dl = Op.getDebugLoc();
464
465 // CCState - Info about the registers and stack slot.
466 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
467
468 // Analize return values of ISD::RET
469 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
470
471 // If this is the first return lowered for this function, add the regs to the
472 // liveout set for the function.
473 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
474 for (unsigned i = 0; i != RVLocs.size(); ++i)
475 if (RVLocs[i].isRegLoc())
476 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
477 }
478
479 // The chain is always operand #0
480 SDValue Chain = Op.getOperand(0);
481 SDValue Flag;
482
483 // Copy the result values into the output registers.
484 for (unsigned i = 0; i != RVLocs.size(); ++i) {
485 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000486 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000487 assert(VA.isRegLoc() && "Can only return in registers!");
488
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000489 // If this is an 8/16/32-bit value, it is really should be passed promoted
490 // to 64 bits.
491 if (VA.getLocInfo() == CCValAssign::SExt)
492 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
493 else if (VA.getLocInfo() == CCValAssign::ZExt)
494 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
495 else if (VA.getLocInfo() == CCValAssign::AExt)
496 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
497
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000498 // ISD::RET => ret chain, (regnum1,val1), ...
499 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000500 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000501
502 // Guarantee that all emitted copies are stuck together,
503 // avoiding something bad.
504 Flag = Chain.getValue(1);
505 }
506
507 if (Flag.getNode())
508 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
509
510 // Return Void
511 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
512}
513
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000514SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
515 ISD::CondCode CC, SDValue &SystemZCC,
516 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000517 // FIXME: Emit a test if RHS is zero
518
519 bool isUnsigned = false;
520 SystemZCC::CondCodes TCC;
521 switch (CC) {
522 default: assert(0 && "Invalid integer condition!");
523 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000524 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000525 TCC = SystemZCC::E;
526 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000527 case ISD::SETUEQ:
528 TCC = SystemZCC::NLH;
529 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000530 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000531 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000532 TCC = SystemZCC::NE;
533 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000534 case ISD::SETUNE:
535 TCC = SystemZCC::LH;
536 break;
537 case ISD::SETO:
538 TCC = SystemZCC::O;
539 break;
540 case ISD::SETUO:
541 TCC = SystemZCC::NO;
542 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000543 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000544 if (LHS.getValueType().isFloatingPoint()) {
545 TCC = SystemZCC::NH;
546 break;
547 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000548 isUnsigned = true; // FALLTHROUGH
549 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000550 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000551 TCC = SystemZCC::LE;
552 break;
553 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000554 if (LHS.getValueType().isFloatingPoint()) {
555 TCC = SystemZCC::NL;
556 break;
557 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000558 isUnsigned = true; // FALLTHROUGH
559 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000560 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000561 TCC = SystemZCC::HE;
562 break;
563 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000564 if (LHS.getValueType().isFloatingPoint()) {
565 TCC = SystemZCC::NLE;
566 break;
567 }
568 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000569 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000570 case ISD::SETOGT:
571 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000572 break;
573 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000574 if (LHS.getValueType().isFloatingPoint()) {
575 TCC = SystemZCC::NHE;
576 break;
577 }
578 isUnsigned = true; // FALLTHROUGH
579 case ISD::SETLT:
580 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000581 TCC = SystemZCC::L;
582 break;
583 }
584
585 SystemZCC = DAG.getConstant(TCC, MVT::i32);
586
587 DebugLoc dl = LHS.getDebugLoc();
588 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
589 dl, MVT::Flag, LHS, RHS);
590}
591
592
593SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
594 SDValue Chain = Op.getOperand(0);
595 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
596 SDValue LHS = Op.getOperand(2);
597 SDValue RHS = Op.getOperand(3);
598 SDValue Dest = Op.getOperand(4);
599 DebugLoc dl = Op.getDebugLoc();
600
601 SDValue SystemZCC;
602 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
603 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
604 Chain, Dest, SystemZCC, Flag);
605}
606
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000607SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
608 SDValue LHS = Op.getOperand(0);
609 SDValue RHS = Op.getOperand(1);
610 SDValue TrueV = Op.getOperand(2);
611 SDValue FalseV = Op.getOperand(3);
612 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
613 DebugLoc dl = Op.getDebugLoc();
614
615 SDValue SystemZCC;
616 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
617
618 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
619 SmallVector<SDValue, 4> Ops;
620 Ops.push_back(TrueV);
621 Ops.push_back(FalseV);
622 Ops.push_back(SystemZCC);
623 Ops.push_back(Flag);
624
625 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
626}
627
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000628SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
629 SelectionDAG &DAG) {
630 DebugLoc dl = Op.getDebugLoc();
631 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000632 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000633
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000634 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
635 bool ExtraLoadRequired =
636 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
637
638 SDValue Result;
639 if (!IsPic && !ExtraLoadRequired) {
640 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
641 Offset = 0;
642 } else {
643 unsigned char OpFlags = 0;
644 if (ExtraLoadRequired)
645 OpFlags = SystemZII::MO_GOTENT;
646
647 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
648 }
649
650 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
651 getPointerTy(), Result);
652
653 if (ExtraLoadRequired)
654 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
655 PseudoSourceValue::getGOT(), 0);
656
657 // If there was a non-zero offset that we didn't fold, create an explicit
658 // addition for it.
659 if (Offset != 0)
660 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
661 DAG.getConstant(Offset, getPointerTy()));
662
663 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000664}
665
Anton Korobeynikovae535672009-07-16 14:19:35 +0000666// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000667SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
668 SelectionDAG &DAG) {
669 DebugLoc dl = Op.getDebugLoc();
670 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
671 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
672
673 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
674}
675
Anton Korobeynikovae535672009-07-16 14:19:35 +0000676
677// FIXME: PIC here
678// FIXME: This is just dirty hack. We need to lower cpool properly
679SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
680 SelectionDAG &DAG) {
681 DebugLoc dl = Op.getDebugLoc();
682 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
683
684 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
685 CP->getAlignment(),
686 CP->getOffset());
687
688 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
689}
690
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000691const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
692 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000693 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000694 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000695 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
696 case SystemZISD::CMP: return "SystemZISD::CMP";
697 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000698 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000699 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000700 default: return NULL;
701 }
702}
703
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000704//===----------------------------------------------------------------------===//
705// Other Lowering Code
706//===----------------------------------------------------------------------===//
707
708MachineBasicBlock*
709SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
710 MachineBasicBlock *BB) const {
711 const SystemZInstrInfo &TII = *TM.getInstrInfo();
712 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000713 assert((MI->getOpcode() == SystemZ::Select32 ||
714 MI->getOpcode() == SystemZ::SelectF32 ||
715 MI->getOpcode() == SystemZ::Select64 ||
716 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000717 "Unexpected instr type to insert");
718
719 // To "insert" a SELECT instruction, we actually have to insert the diamond
720 // control-flow pattern. The incoming instruction knows the destination vreg
721 // to set, the condition code register to branch on, the true/false values to
722 // select between, and a branch opcode to use.
723 const BasicBlock *LLVM_BB = BB->getBasicBlock();
724 MachineFunction::iterator I = BB;
725 ++I;
726
727 // thisMBB:
728 // ...
729 // TrueVal = ...
730 // cmpTY ccX, r1, r2
731 // jCC copy1MBB
732 // fallthrough --> copy0MBB
733 MachineBasicBlock *thisMBB = BB;
734 MachineFunction *F = BB->getParent();
735 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
736 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
737 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
738 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
739 F->insert(I, copy0MBB);
740 F->insert(I, copy1MBB);
741 // Update machine-CFG edges by transferring all successors of the current
742 // block to the new block which will contain the Phi node for the select.
743 copy1MBB->transferSuccessors(BB);
744 // Next, add the true and fallthrough blocks as its successors.
745 BB->addSuccessor(copy0MBB);
746 BB->addSuccessor(copy1MBB);
747
748 // copy0MBB:
749 // %FalseValue = ...
750 // # fallthrough to copy1MBB
751 BB = copy0MBB;
752
753 // Update machine-CFG edges
754 BB->addSuccessor(copy1MBB);
755
756 // copy1MBB:
757 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
758 // ...
759 BB = copy1MBB;
760 BuildMI(BB, dl, TII.get(SystemZ::PHI),
761 MI->getOperand(0).getReg())
762 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
763 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
764
765 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
766 return BB;
767}