blob: 83ba64c0c6de361dc09754eb5616cb300a64bcf0 [file] [log] [blame]
Anton Korobeynikov4403b932009-07-16 13:27:25 +00001//===-- SystemZISelLowering.cpp - SystemZ DAG Lowering Implementation -----==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SystemZTargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "systemz-lower"
15
16#include "SystemZISelLowering.h"
17#include "SystemZ.h"
18#include "SystemZTargetMachine.h"
19#include "SystemZSubtarget.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/CallingConv.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/GlobalAlias.h"
26#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/CodeGen/PseudoSourceValue.h"
32#include "llvm/CodeGen/SelectionDAGISel.h"
33#include "llvm/CodeGen/ValueTypes.h"
34#include "llvm/Support/Debug.h"
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000035#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000036#include "llvm/ADT/VectorExtras.h"
37using namespace llvm;
38
39SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
40 TargetLowering(tm), Subtarget(*tm.getSubtargetImpl()), TM(tm) {
41
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +000042 RegInfo = TM.getRegisterInfo();
43
Anton Korobeynikov4403b932009-07-16 13:27:25 +000044 // Set up the register classes.
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +000045 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
46 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000047 addRegisterClass(MVT::v2i32,SystemZ::GR64PRegisterClass);
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +000048 addRegisterClass(MVT::i128, SystemZ::GR128RegisterClass);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000049 addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000050
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000051 if (!UseSoftFloat) {
52 addRegisterClass(MVT::f32, SystemZ::FP32RegisterClass);
53 addRegisterClass(MVT::f64, SystemZ::FP64RegisterClass);
54 }
55
Anton Korobeynikov4403b932009-07-16 13:27:25 +000056 // Compute derived properties from the register classes
57 computeRegisterProperties();
58
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000059 // Set shifts properties
60 setShiftAmountFlavor(Extend);
Anton Korobeynikov48e8b3c2009-07-16 14:15:24 +000061 setShiftAmountType(MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000062
Anton Korobeynikov4403b932009-07-16 13:27:25 +000063 // Provide all sorts of operation actions
Anton Korobeynikovbf022172009-07-16 13:53:35 +000064 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
65 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
66 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000067
Anton Korobeynikove0167c12009-07-16 13:35:30 +000068 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000069 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000070
71 setOperationAction(ISD::RET, MVT::Other, Custom);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000072
Anton Korobeynikov983d3a12009-07-16 14:07:24 +000073 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000074 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
75 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
76 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +000077 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
78 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
79 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
80 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +000081 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +000082 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Anton Korobeynikovc772c442009-07-16 14:08:15 +000083 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000084
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000085 setOperationAction(ISD::SDIV, MVT::i32, Expand);
86 setOperationAction(ISD::UDIV, MVT::i32, Expand);
87 setOperationAction(ISD::SDIV, MVT::i64, Expand);
88 setOperationAction(ISD::UDIV, MVT::i64, Expand);
89 setOperationAction(ISD::SREM, MVT::i32, Expand);
90 setOperationAction(ISD::UREM, MVT::i32, Expand);
91 setOperationAction(ISD::SREM, MVT::i64, Expand);
92 setOperationAction(ISD::UREM, MVT::i64, Expand);
93
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000094 // FIXME: Can we lower these 2 efficiently?
95 setOperationAction(ISD::SETCC, MVT::i32, Expand);
96 setOperationAction(ISD::SETCC, MVT::i64, Expand);
97 setOperationAction(ISD::SELECT, MVT::i32, Expand);
98 setOperationAction(ISD::SELECT, MVT::i64, Expand);
99 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
100 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000101 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
102 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000103
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +0000104 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
105 // unsigned.
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000106 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000107 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000108}
109
110SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
111 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000112 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
113 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000114 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000115 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000116 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000117 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000118 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000119 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000120 default:
121 assert(0 && "unimplemented operand");
122 return SDValue();
123 }
124}
125
126//===----------------------------------------------------------------------===//
127// Calling Convention Implementation
128//===----------------------------------------------------------------------===//
129
130#include "SystemZGenCallingConv.inc"
131
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000132SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
133 SelectionDAG &DAG) {
134 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
135 switch (CC) {
136 default:
137 assert(0 && "Unsupported calling convention");
138 case CallingConv::C:
139 case CallingConv::Fast:
140 return LowerCCCArguments(Op, DAG);
141 }
142}
143
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000144SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
145 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
146 unsigned CallingConv = TheCall->getCallingConv();
147 switch (CallingConv) {
148 default:
149 assert(0 && "Unsupported calling convention");
150 case CallingConv::Fast:
151 case CallingConv::C:
152 return LowerCCCCallTo(Op, DAG, CallingConv);
153 }
154}
155
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000156/// LowerCCCArguments - transform physical registers into virtual registers and
157/// generate load operations for arguments places on the stack.
158// FIXME: struct return stuff
159// FIXME: varargs
160SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
161 SelectionDAG &DAG) {
162 MachineFunction &MF = DAG.getMachineFunction();
163 MachineFrameInfo *MFI = MF.getFrameInfo();
164 MachineRegisterInfo &RegInfo = MF.getRegInfo();
165 SDValue Root = Op.getOperand(0);
166 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
167 unsigned CC = MF.getFunction()->getCallingConv();
168 DebugLoc dl = Op.getDebugLoc();
169
170 // Assign locations to all of the incoming arguments.
171 SmallVector<CCValAssign, 16> ArgLocs;
172 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
173 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
174
175 assert(!isVarArg && "Varargs not supported yet");
176
177 SmallVector<SDValue, 16> ArgValues;
178 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
179 CCValAssign &VA = ArgLocs[i];
180 if (VA.isRegLoc()) {
181 // Arguments passed in registers
182 MVT RegVT = VA.getLocVT();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000183 TargetRegisterClass *RC;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000184 switch (RegVT.getSimpleVT()) {
185 default:
186 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
187 << RegVT.getSimpleVT()
188 << "\n";
189 abort();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000190 case MVT::i64:
191 RC = SystemZ::GR64RegisterClass;
192 break;
193 case MVT::f32:
194 RC = SystemZ::FP32RegisterClass;
195 break;
196 case MVT::f64:
197 RC = SystemZ::FP64RegisterClass;
198 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000199 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000200
201 unsigned VReg = RegInfo.createVirtualRegister(RC);
202 RegInfo.addLiveIn(VA.getLocReg(), VReg);
203 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
204
205 // If this is an 8/16/32-bit value, it is really passed promoted to 64
206 // bits. Insert an assert[sz]ext to capture this, then truncate to the
207 // right size.
208 if (VA.getLocInfo() == CCValAssign::SExt)
209 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
210 DAG.getValueType(VA.getValVT()));
211 else if (VA.getLocInfo() == CCValAssign::ZExt)
212 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
213 DAG.getValueType(VA.getValVT()));
214
215 if (VA.getLocInfo() != CCValAssign::Full)
216 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
217
218 ArgValues.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000219 } else {
220 // Sanity check
221 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000222
223 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000224 // Create the frame index object for this incoming parameter...
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000225 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
226 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000227
228 // Create the SelectionDAG nodes corresponding to a load
229 //from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000230 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
231 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN,
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000232 PseudoSourceValue::getFixedStack(FI), 0));
233 }
234 }
235
236 ArgValues.push_back(Root);
237
238 // Return the new list of results.
239 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
240 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
241}
242
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000243/// LowerCCCCallTo - functions arguments are copied from virtual regs to
244/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
245/// TODO: sret.
246SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
247 unsigned CC) {
248 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
249 SDValue Chain = TheCall->getChain();
250 SDValue Callee = TheCall->getCallee();
251 bool isVarArg = TheCall->isVarArg();
252 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000253 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000254
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000255 // Offset to first argument stack slot.
256 const unsigned FirstArgOffset = 160;
257
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000258 // Analyze operands of the call, assigning locations to each operand.
259 SmallVector<CCValAssign, 16> ArgLocs;
260 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
261
262 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
263
264 // Get a count of how many bytes are to be pushed on the stack.
265 unsigned NumBytes = CCInfo.getNextStackOffset();
266
267 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
268 getPointerTy(), true));
269
270 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
271 SmallVector<SDValue, 12> MemOpChains;
272 SDValue StackPtr;
273
274 // Walk the register/memloc assignments, inserting copies/loads.
275 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
276 CCValAssign &VA = ArgLocs[i];
277
278 // Arguments start after the 5 first operands of ISD::CALL
279 SDValue Arg = TheCall->getArg(i);
280
281 // Promote the value if needed.
282 switch (VA.getLocInfo()) {
283 default: assert(0 && "Unknown loc info!");
284 case CCValAssign::Full: break;
285 case CCValAssign::SExt:
286 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
287 break;
288 case CCValAssign::ZExt:
289 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
290 break;
291 case CCValAssign::AExt:
292 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
293 break;
294 }
295
296 // Arguments that can be passed on register must be kept at RegsToPass
297 // vector
298 if (VA.isRegLoc()) {
299 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
300 } else {
301 assert(VA.isMemLoc());
302
303 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000304 StackPtr =
305 DAG.getCopyFromReg(Chain, dl,
306 (RegInfo->hasFP(MF) ?
307 SystemZ::R11D : SystemZ::R15D),
308 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000309
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000310 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
311 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
312 StackPtr,
313 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000314
315 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000316 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000317 }
318 }
319
320 // Transform all store nodes into one single node because all store nodes are
321 // independent of each other.
322 if (!MemOpChains.empty())
323 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
324 &MemOpChains[0], MemOpChains.size());
325
326 // Build a sequence of copy-to-reg nodes chained together with token chain and
327 // flag operands which copy the outgoing args into registers. The InFlag in
328 // necessary since all emited instructions must be stuck together.
329 SDValue InFlag;
330 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
331 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
332 RegsToPass[i].second, InFlag);
333 InFlag = Chain.getValue(1);
334 }
335
336 // If the callee is a GlobalAddress node (quite common, every direct call is)
337 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
338 // Likewise ExternalSymbol -> TargetExternalSymbol.
339 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
340 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
341 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
342 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
343
344 // Returns a chain & a flag for retval copy to use.
345 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
346 SmallVector<SDValue, 8> Ops;
347 Ops.push_back(Chain);
348 Ops.push_back(Callee);
349
350 // Add argument registers to the end of the list so that they are
351 // known live into the call.
352 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
353 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
354 RegsToPass[i].second.getValueType()));
355
356 if (InFlag.getNode())
357 Ops.push_back(InFlag);
358
359 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
360 InFlag = Chain.getValue(1);
361
362 // Create the CALLSEQ_END node.
363 Chain = DAG.getCALLSEQ_END(Chain,
364 DAG.getConstant(NumBytes, getPointerTy(), true),
365 DAG.getConstant(0, getPointerTy(), true),
366 InFlag);
367 InFlag = Chain.getValue(1);
368
369 // Handle result values, copying them out of physregs into vregs that we
370 // return.
371 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
372 Op.getResNo());
373}
374
375/// LowerCallResult - Lower the result values of an ISD::CALL into the
376/// appropriate copies out of appropriate physical registers. This assumes that
377/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
378/// being lowered. Returns a SDNode with the same number of values as the
379/// ISD::CALL.
380SDNode*
381SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
382 CallSDNode *TheCall,
383 unsigned CallingConv,
384 SelectionDAG &DAG) {
385 bool isVarArg = TheCall->isVarArg();
386 DebugLoc dl = TheCall->getDebugLoc();
387
388 // Assign locations to each value returned by this call.
389 SmallVector<CCValAssign, 16> RVLocs;
390 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
391
392 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
393 SmallVector<SDValue, 8> ResultVals;
394
395 // Copy all of the result registers out of their specified physreg.
396 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000397 CCValAssign &VA = RVLocs[i];
398
399 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
400 VA.getLocVT(), InFlag).getValue(1);
401 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000402 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000403
404 // If this is an 8/16/32-bit value, it is really passed promoted to 64
405 // bits. Insert an assert[sz]ext to capture this, then truncate to the
406 // right size.
407 if (VA.getLocInfo() == CCValAssign::SExt)
408 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
409 DAG.getValueType(VA.getValVT()));
410 else if (VA.getLocInfo() == CCValAssign::ZExt)
411 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
412 DAG.getValueType(VA.getValVT()));
413
414 if (VA.getLocInfo() != CCValAssign::Full)
415 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
416
417 ResultVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000418 }
419
420 ResultVals.push_back(Chain);
421
422 // Merge everything together with a MERGE_VALUES node.
423 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
424 &ResultVals[0], ResultVals.size()).getNode();
425}
426
427
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000428SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
429 // CCValAssign - represent the assignment of the return value to a location
430 SmallVector<CCValAssign, 16> RVLocs;
431 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
432 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
433 DebugLoc dl = Op.getDebugLoc();
434
435 // CCState - Info about the registers and stack slot.
436 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
437
438 // Analize return values of ISD::RET
439 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
440
441 // If this is the first return lowered for this function, add the regs to the
442 // liveout set for the function.
443 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
444 for (unsigned i = 0; i != RVLocs.size(); ++i)
445 if (RVLocs[i].isRegLoc())
446 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
447 }
448
449 // The chain is always operand #0
450 SDValue Chain = Op.getOperand(0);
451 SDValue Flag;
452
453 // Copy the result values into the output registers.
454 for (unsigned i = 0; i != RVLocs.size(); ++i) {
455 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000456 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000457 assert(VA.isRegLoc() && "Can only return in registers!");
458
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000459 // If this is an 8/16/32-bit value, it is really should be passed promoted
460 // to 64 bits.
461 if (VA.getLocInfo() == CCValAssign::SExt)
462 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
463 else if (VA.getLocInfo() == CCValAssign::ZExt)
464 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
465 else if (VA.getLocInfo() == CCValAssign::AExt)
466 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
467
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000468 // ISD::RET => ret chain, (regnum1,val1), ...
469 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000470 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000471
472 // Guarantee that all emitted copies are stuck together,
473 // avoiding something bad.
474 Flag = Chain.getValue(1);
475 }
476
477 if (Flag.getNode())
478 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
479
480 // Return Void
481 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
482}
483
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000484SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
485 ISD::CondCode CC, SDValue &SystemZCC,
486 SelectionDAG &DAG) {
487 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
488
489 // FIXME: Emit a test if RHS is zero
490
491 bool isUnsigned = false;
492 SystemZCC::CondCodes TCC;
493 switch (CC) {
494 default: assert(0 && "Invalid integer condition!");
495 case ISD::SETEQ:
496 TCC = SystemZCC::E;
497 break;
498 case ISD::SETNE:
499 TCC = SystemZCC::NE;
500 break;
501 case ISD::SETULE:
502 isUnsigned = true; // FALLTHROUGH
503 case ISD::SETLE:
504 TCC = SystemZCC::LE;
505 break;
506 case ISD::SETUGE:
507 isUnsigned = true; // FALLTHROUGH
508 case ISD::SETGE:
509 TCC = SystemZCC::HE;
510 break;
511 case ISD::SETUGT:
512 isUnsigned = true;
513 case ISD::SETGT:
514 TCC = SystemZCC::H; // FALLTHROUGH
515 break;
516 case ISD::SETULT:
517 isUnsigned = true;
518 case ISD::SETLT: // FALLTHROUGH
519 TCC = SystemZCC::L;
520 break;
521 }
522
523 SystemZCC = DAG.getConstant(TCC, MVT::i32);
524
525 DebugLoc dl = LHS.getDebugLoc();
526 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
527 dl, MVT::Flag, LHS, RHS);
528}
529
530
531SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
532 SDValue Chain = Op.getOperand(0);
533 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
534 SDValue LHS = Op.getOperand(2);
535 SDValue RHS = Op.getOperand(3);
536 SDValue Dest = Op.getOperand(4);
537 DebugLoc dl = Op.getDebugLoc();
538
539 SDValue SystemZCC;
540 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
541 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
542 Chain, Dest, SystemZCC, Flag);
543}
544
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000545SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
546 SDValue LHS = Op.getOperand(0);
547 SDValue RHS = Op.getOperand(1);
548 SDValue TrueV = Op.getOperand(2);
549 SDValue FalseV = Op.getOperand(3);
550 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
551 DebugLoc dl = Op.getDebugLoc();
552
553 SDValue SystemZCC;
554 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
555
556 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
557 SmallVector<SDValue, 4> Ops;
558 Ops.push_back(TrueV);
559 Ops.push_back(FalseV);
560 Ops.push_back(SystemZCC);
561 Ops.push_back(Flag);
562
563 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
564}
565
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000566SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
567 SelectionDAG &DAG) {
568 DebugLoc dl = Op.getDebugLoc();
569 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000570 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000571
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000572 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
573 bool ExtraLoadRequired =
574 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
575
576 SDValue Result;
577 if (!IsPic && !ExtraLoadRequired) {
578 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
579 Offset = 0;
580 } else {
581 unsigned char OpFlags = 0;
582 if (ExtraLoadRequired)
583 OpFlags = SystemZII::MO_GOTENT;
584
585 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
586 }
587
588 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
589 getPointerTy(), Result);
590
591 if (ExtraLoadRequired)
592 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
593 PseudoSourceValue::getGOT(), 0);
594
595 // If there was a non-zero offset that we didn't fold, create an explicit
596 // addition for it.
597 if (Offset != 0)
598 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
599 DAG.getConstant(Offset, getPointerTy()));
600
601 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000602}
603
Anton Korobeynikovae535672009-07-16 14:19:35 +0000604// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000605SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
606 SelectionDAG &DAG) {
607 DebugLoc dl = Op.getDebugLoc();
608 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
609 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
610
611 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
612}
613
Anton Korobeynikovae535672009-07-16 14:19:35 +0000614
615// FIXME: PIC here
616// FIXME: This is just dirty hack. We need to lower cpool properly
617SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
618 SelectionDAG &DAG) {
619 DebugLoc dl = Op.getDebugLoc();
620 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
621
622 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
623 CP->getAlignment(),
624 CP->getOffset());
625
626 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
627}
628
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000629const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
630 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000631 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000632 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000633 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
634 case SystemZISD::CMP: return "SystemZISD::CMP";
635 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000636 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000637 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000638 default: return NULL;
639 }
640}
641
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000642//===----------------------------------------------------------------------===//
643// Other Lowering Code
644//===----------------------------------------------------------------------===//
645
646MachineBasicBlock*
647SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
648 MachineBasicBlock *BB) const {
649 const SystemZInstrInfo &TII = *TM.getInstrInfo();
650 DebugLoc dl = MI->getDebugLoc();
651 assert((MI->getOpcode() == SystemZ::Select32 ||
652 MI->getOpcode() == SystemZ::Select64) &&
653 "Unexpected instr type to insert");
654
655 // To "insert" a SELECT instruction, we actually have to insert the diamond
656 // control-flow pattern. The incoming instruction knows the destination vreg
657 // to set, the condition code register to branch on, the true/false values to
658 // select between, and a branch opcode to use.
659 const BasicBlock *LLVM_BB = BB->getBasicBlock();
660 MachineFunction::iterator I = BB;
661 ++I;
662
663 // thisMBB:
664 // ...
665 // TrueVal = ...
666 // cmpTY ccX, r1, r2
667 // jCC copy1MBB
668 // fallthrough --> copy0MBB
669 MachineBasicBlock *thisMBB = BB;
670 MachineFunction *F = BB->getParent();
671 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
672 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
673 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
674 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
675 F->insert(I, copy0MBB);
676 F->insert(I, copy1MBB);
677 // Update machine-CFG edges by transferring all successors of the current
678 // block to the new block which will contain the Phi node for the select.
679 copy1MBB->transferSuccessors(BB);
680 // Next, add the true and fallthrough blocks as its successors.
681 BB->addSuccessor(copy0MBB);
682 BB->addSuccessor(copy1MBB);
683
684 // copy0MBB:
685 // %FalseValue = ...
686 // # fallthrough to copy1MBB
687 BB = copy0MBB;
688
689 // Update machine-CFG edges
690 BB->addSuccessor(copy1MBB);
691
692 // copy1MBB:
693 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
694 // ...
695 BB = copy1MBB;
696 BuildMI(BB, dl, TII.get(SystemZ::PHI),
697 MI->getOperand(0).getReg())
698 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
699 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
700
701 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
702 return BB;
703}