blob: 096fe420cc5db940ebfeaa94589b1f394ee1db65 [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"
35#include "llvm/ADT/VectorExtras.h"
36using namespace llvm;
37
38SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
39 TargetLowering(tm), Subtarget(*tm.getSubtargetImpl()), TM(tm) {
40
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +000041 RegInfo = TM.getRegisterInfo();
42
Anton Korobeynikov4403b932009-07-16 13:27:25 +000043 // Set up the register classes.
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +000044 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
45 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
46 addRegisterClass(MVT::i128, SystemZ::GR128RegisterClass);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000047
48 // Compute derived properties from the register classes
49 computeRegisterProperties();
50
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000051 // Set shifts properties
52 setShiftAmountFlavor(Extend);
53 setShiftAmountType(MVT::i32);
54
Anton Korobeynikov4403b932009-07-16 13:27:25 +000055 // Provide all sorts of operation actions
Anton Korobeynikovbf022172009-07-16 13:53:35 +000056 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
57 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
58 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000059
Anton Korobeynikove0167c12009-07-16 13:35:30 +000060 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000061 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000062
63 setOperationAction(ISD::RET, MVT::Other, Custom);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000064
Anton Korobeynikov983d3a12009-07-16 14:07:24 +000065 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000066 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
67 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
68 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +000069 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +000070 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Anton Korobeynikovc772c442009-07-16 14:08:15 +000071 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000072
73 // FIXME: Can we lower these 2 efficiently?
74 setOperationAction(ISD::SETCC, MVT::i32, Expand);
75 setOperationAction(ISD::SETCC, MVT::i64, Expand);
76 setOperationAction(ISD::SELECT, MVT::i32, Expand);
77 setOperationAction(ISD::SELECT, MVT::i64, Expand);
78 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
79 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +000080
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +000081 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
82 // unsigned.
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +000083 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +000084 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000085}
86
87SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
88 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000089 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
90 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +000091 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000092 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000093 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +000094 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +000095 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000096 default:
97 assert(0 && "unimplemented operand");
98 return SDValue();
99 }
100}
101
102//===----------------------------------------------------------------------===//
103// Calling Convention Implementation
104//===----------------------------------------------------------------------===//
105
106#include "SystemZGenCallingConv.inc"
107
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000108SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
109 SelectionDAG &DAG) {
110 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
111 switch (CC) {
112 default:
113 assert(0 && "Unsupported calling convention");
114 case CallingConv::C:
115 case CallingConv::Fast:
116 return LowerCCCArguments(Op, DAG);
117 }
118}
119
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000120SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
121 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
122 unsigned CallingConv = TheCall->getCallingConv();
123 switch (CallingConv) {
124 default:
125 assert(0 && "Unsupported calling convention");
126 case CallingConv::Fast:
127 case CallingConv::C:
128 return LowerCCCCallTo(Op, DAG, CallingConv);
129 }
130}
131
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000132/// LowerCCCArguments - transform physical registers into virtual registers and
133/// generate load operations for arguments places on the stack.
134// FIXME: struct return stuff
135// FIXME: varargs
136SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
137 SelectionDAG &DAG) {
138 MachineFunction &MF = DAG.getMachineFunction();
139 MachineFrameInfo *MFI = MF.getFrameInfo();
140 MachineRegisterInfo &RegInfo = MF.getRegInfo();
141 SDValue Root = Op.getOperand(0);
142 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
143 unsigned CC = MF.getFunction()->getCallingConv();
144 DebugLoc dl = Op.getDebugLoc();
145
146 // Assign locations to all of the incoming arguments.
147 SmallVector<CCValAssign, 16> ArgLocs;
148 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
149 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
150
151 assert(!isVarArg && "Varargs not supported yet");
152
153 SmallVector<SDValue, 16> ArgValues;
154 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
155 CCValAssign &VA = ArgLocs[i];
156 if (VA.isRegLoc()) {
157 // Arguments passed in registers
158 MVT RegVT = VA.getLocVT();
159 switch (RegVT.getSimpleVT()) {
160 default:
161 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
162 << RegVT.getSimpleVT()
163 << "\n";
164 abort();
165 case MVT::i64:
166 unsigned VReg =
167 RegInfo.createVirtualRegister(SystemZ::GR64RegisterClass);
168 RegInfo.addLiveIn(VA.getLocReg(), VReg);
169 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
170
171 // If this is an 8/16/32-bit value, it is really passed promoted to 64
172 // bits. Insert an assert[sz]ext to capture this, then truncate to the
173 // right size.
174 if (VA.getLocInfo() == CCValAssign::SExt)
175 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
176 DAG.getValueType(VA.getValVT()));
177 else if (VA.getLocInfo() == CCValAssign::ZExt)
178 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
179 DAG.getValueType(VA.getValVT()));
180
181 if (VA.getLocInfo() != CCValAssign::Full)
182 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
183
184 ArgValues.push_back(ArgValue);
185 }
186 } else {
187 // Sanity check
188 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000189
190 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000191 // Create the frame index object for this incoming parameter...
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000192 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
193 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000194
195 // Create the SelectionDAG nodes corresponding to a load
196 //from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000197 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
198 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN,
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000199 PseudoSourceValue::getFixedStack(FI), 0));
200 }
201 }
202
203 ArgValues.push_back(Root);
204
205 // Return the new list of results.
206 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
207 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
208}
209
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000210/// LowerCCCCallTo - functions arguments are copied from virtual regs to
211/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
212/// TODO: sret.
213SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
214 unsigned CC) {
215 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
216 SDValue Chain = TheCall->getChain();
217 SDValue Callee = TheCall->getCallee();
218 bool isVarArg = TheCall->isVarArg();
219 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000220 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000221
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000222 // Offset to first argument stack slot.
223 const unsigned FirstArgOffset = 160;
224
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000225 // Analyze operands of the call, assigning locations to each operand.
226 SmallVector<CCValAssign, 16> ArgLocs;
227 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
228
229 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
230
231 // Get a count of how many bytes are to be pushed on the stack.
232 unsigned NumBytes = CCInfo.getNextStackOffset();
233
234 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
235 getPointerTy(), true));
236
237 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
238 SmallVector<SDValue, 12> MemOpChains;
239 SDValue StackPtr;
240
241 // Walk the register/memloc assignments, inserting copies/loads.
242 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
243 CCValAssign &VA = ArgLocs[i];
244
245 // Arguments start after the 5 first operands of ISD::CALL
246 SDValue Arg = TheCall->getArg(i);
247
248 // Promote the value if needed.
249 switch (VA.getLocInfo()) {
250 default: assert(0 && "Unknown loc info!");
251 case CCValAssign::Full: break;
252 case CCValAssign::SExt:
253 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
254 break;
255 case CCValAssign::ZExt:
256 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
257 break;
258 case CCValAssign::AExt:
259 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
260 break;
261 }
262
263 // Arguments that can be passed on register must be kept at RegsToPass
264 // vector
265 if (VA.isRegLoc()) {
266 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
267 } else {
268 assert(VA.isMemLoc());
269
270 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000271 StackPtr =
272 DAG.getCopyFromReg(Chain, dl,
273 (RegInfo->hasFP(MF) ?
274 SystemZ::R11D : SystemZ::R15D),
275 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000276
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000277 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
278 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
279 StackPtr,
280 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000281
282 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000283 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000284 }
285 }
286
287 // Transform all store nodes into one single node because all store nodes are
288 // independent of each other.
289 if (!MemOpChains.empty())
290 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
291 &MemOpChains[0], MemOpChains.size());
292
293 // Build a sequence of copy-to-reg nodes chained together with token chain and
294 // flag operands which copy the outgoing args into registers. The InFlag in
295 // necessary since all emited instructions must be stuck together.
296 SDValue InFlag;
297 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
298 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
299 RegsToPass[i].second, InFlag);
300 InFlag = Chain.getValue(1);
301 }
302
303 // If the callee is a GlobalAddress node (quite common, every direct call is)
304 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
305 // Likewise ExternalSymbol -> TargetExternalSymbol.
306 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
307 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
308 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
309 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
310
311 // Returns a chain & a flag for retval copy to use.
312 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
313 SmallVector<SDValue, 8> Ops;
314 Ops.push_back(Chain);
315 Ops.push_back(Callee);
316
317 // Add argument registers to the end of the list so that they are
318 // known live into the call.
319 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
320 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
321 RegsToPass[i].second.getValueType()));
322
323 if (InFlag.getNode())
324 Ops.push_back(InFlag);
325
326 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
327 InFlag = Chain.getValue(1);
328
329 // Create the CALLSEQ_END node.
330 Chain = DAG.getCALLSEQ_END(Chain,
331 DAG.getConstant(NumBytes, getPointerTy(), true),
332 DAG.getConstant(0, getPointerTy(), true),
333 InFlag);
334 InFlag = Chain.getValue(1);
335
336 // Handle result values, copying them out of physregs into vregs that we
337 // return.
338 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
339 Op.getResNo());
340}
341
342/// LowerCallResult - Lower the result values of an ISD::CALL into the
343/// appropriate copies out of appropriate physical registers. This assumes that
344/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
345/// being lowered. Returns a SDNode with the same number of values as the
346/// ISD::CALL.
347SDNode*
348SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
349 CallSDNode *TheCall,
350 unsigned CallingConv,
351 SelectionDAG &DAG) {
352 bool isVarArg = TheCall->isVarArg();
353 DebugLoc dl = TheCall->getDebugLoc();
354
355 // Assign locations to each value returned by this call.
356 SmallVector<CCValAssign, 16> RVLocs;
357 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
358
359 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
360 SmallVector<SDValue, 8> ResultVals;
361
362 // Copy all of the result registers out of their specified physreg.
363 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000364 CCValAssign &VA = RVLocs[i];
365
366 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
367 VA.getLocVT(), InFlag).getValue(1);
368 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000369 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000370
371 // If this is an 8/16/32-bit value, it is really passed promoted to 64
372 // bits. Insert an assert[sz]ext to capture this, then truncate to the
373 // right size.
374 if (VA.getLocInfo() == CCValAssign::SExt)
375 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
376 DAG.getValueType(VA.getValVT()));
377 else if (VA.getLocInfo() == CCValAssign::ZExt)
378 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
379 DAG.getValueType(VA.getValVT()));
380
381 if (VA.getLocInfo() != CCValAssign::Full)
382 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
383
384 ResultVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000385 }
386
387 ResultVals.push_back(Chain);
388
389 // Merge everything together with a MERGE_VALUES node.
390 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
391 &ResultVals[0], ResultVals.size()).getNode();
392}
393
394
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000395SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
396 // CCValAssign - represent the assignment of the return value to a location
397 SmallVector<CCValAssign, 16> RVLocs;
398 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
399 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
400 DebugLoc dl = Op.getDebugLoc();
401
402 // CCState - Info about the registers and stack slot.
403 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
404
405 // Analize return values of ISD::RET
406 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
407
408 // If this is the first return lowered for this function, add the regs to the
409 // liveout set for the function.
410 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
411 for (unsigned i = 0; i != RVLocs.size(); ++i)
412 if (RVLocs[i].isRegLoc())
413 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
414 }
415
416 // The chain is always operand #0
417 SDValue Chain = Op.getOperand(0);
418 SDValue Flag;
419
420 // Copy the result values into the output registers.
421 for (unsigned i = 0; i != RVLocs.size(); ++i) {
422 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000423 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000424 assert(VA.isRegLoc() && "Can only return in registers!");
425
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000426 // If this is an 8/16/32-bit value, it is really should be passed promoted
427 // to 64 bits.
428 if (VA.getLocInfo() == CCValAssign::SExt)
429 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
430 else if (VA.getLocInfo() == CCValAssign::ZExt)
431 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
432 else if (VA.getLocInfo() == CCValAssign::AExt)
433 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
434
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000435 // ISD::RET => ret chain, (regnum1,val1), ...
436 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000437 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000438
439 // Guarantee that all emitted copies are stuck together,
440 // avoiding something bad.
441 Flag = Chain.getValue(1);
442 }
443
444 if (Flag.getNode())
445 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
446
447 // Return Void
448 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
449}
450
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000451SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
452 ISD::CondCode CC, SDValue &SystemZCC,
453 SelectionDAG &DAG) {
454 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
455
456 // FIXME: Emit a test if RHS is zero
457
458 bool isUnsigned = false;
459 SystemZCC::CondCodes TCC;
460 switch (CC) {
461 default: assert(0 && "Invalid integer condition!");
462 case ISD::SETEQ:
463 TCC = SystemZCC::E;
464 break;
465 case ISD::SETNE:
466 TCC = SystemZCC::NE;
467 break;
468 case ISD::SETULE:
469 isUnsigned = true; // FALLTHROUGH
470 case ISD::SETLE:
471 TCC = SystemZCC::LE;
472 break;
473 case ISD::SETUGE:
474 isUnsigned = true; // FALLTHROUGH
475 case ISD::SETGE:
476 TCC = SystemZCC::HE;
477 break;
478 case ISD::SETUGT:
479 isUnsigned = true;
480 case ISD::SETGT:
481 TCC = SystemZCC::H; // FALLTHROUGH
482 break;
483 case ISD::SETULT:
484 isUnsigned = true;
485 case ISD::SETLT: // FALLTHROUGH
486 TCC = SystemZCC::L;
487 break;
488 }
489
490 SystemZCC = DAG.getConstant(TCC, MVT::i32);
491
492 DebugLoc dl = LHS.getDebugLoc();
493 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
494 dl, MVT::Flag, LHS, RHS);
495}
496
497
498SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
499 SDValue Chain = Op.getOperand(0);
500 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
501 SDValue LHS = Op.getOperand(2);
502 SDValue RHS = Op.getOperand(3);
503 SDValue Dest = Op.getOperand(4);
504 DebugLoc dl = Op.getDebugLoc();
505
506 SDValue SystemZCC;
507 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
508 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
509 Chain, Dest, SystemZCC, Flag);
510}
511
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000512SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
513 SDValue LHS = Op.getOperand(0);
514 SDValue RHS = Op.getOperand(1);
515 SDValue TrueV = Op.getOperand(2);
516 SDValue FalseV = Op.getOperand(3);
517 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
518 DebugLoc dl = Op.getDebugLoc();
519
520 SDValue SystemZCC;
521 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
522
523 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
524 SmallVector<SDValue, 4> Ops;
525 Ops.push_back(TrueV);
526 Ops.push_back(FalseV);
527 Ops.push_back(SystemZCC);
528 Ops.push_back(Flag);
529
530 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
531}
532
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000533SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
534 SelectionDAG &DAG) {
535 DebugLoc dl = Op.getDebugLoc();
536 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
537 SDValue GA = DAG.getTargetGlobalAddress(GV, getPointerTy());
538
539 // FIXME: Verify stuff for constant globals entries
540 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), GA);
541}
542
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000543
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000544SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
545 SelectionDAG &DAG) {
546 DebugLoc dl = Op.getDebugLoc();
547 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
548 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
549
550 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
551}
552
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000553const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
554 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000555 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000556 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000557 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
558 case SystemZISD::CMP: return "SystemZISD::CMP";
559 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000560 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000561 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000562 default: return NULL;
563 }
564}
565
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000566//===----------------------------------------------------------------------===//
567// Other Lowering Code
568//===----------------------------------------------------------------------===//
569
570MachineBasicBlock*
571SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
572 MachineBasicBlock *BB) const {
573 const SystemZInstrInfo &TII = *TM.getInstrInfo();
574 DebugLoc dl = MI->getDebugLoc();
575 assert((MI->getOpcode() == SystemZ::Select32 ||
576 MI->getOpcode() == SystemZ::Select64) &&
577 "Unexpected instr type to insert");
578
579 // To "insert" a SELECT instruction, we actually have to insert the diamond
580 // control-flow pattern. The incoming instruction knows the destination vreg
581 // to set, the condition code register to branch on, the true/false values to
582 // select between, and a branch opcode to use.
583 const BasicBlock *LLVM_BB = BB->getBasicBlock();
584 MachineFunction::iterator I = BB;
585 ++I;
586
587 // thisMBB:
588 // ...
589 // TrueVal = ...
590 // cmpTY ccX, r1, r2
591 // jCC copy1MBB
592 // fallthrough --> copy0MBB
593 MachineBasicBlock *thisMBB = BB;
594 MachineFunction *F = BB->getParent();
595 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
596 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
597 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
598 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
599 F->insert(I, copy0MBB);
600 F->insert(I, copy1MBB);
601 // Update machine-CFG edges by transferring all successors of the current
602 // block to the new block which will contain the Phi node for the select.
603 copy1MBB->transferSuccessors(BB);
604 // Next, add the true and fallthrough blocks as its successors.
605 BB->addSuccessor(copy0MBB);
606 BB->addSuccessor(copy1MBB);
607
608 // copy0MBB:
609 // %FalseValue = ...
610 // # fallthrough to copy1MBB
611 BB = copy0MBB;
612
613 // Update machine-CFG edges
614 BB->addSuccessor(copy1MBB);
615
616 // copy1MBB:
617 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
618 // ...
619 BB = copy1MBB;
620 BuildMI(BB, dl, TII.get(SystemZ::PHI),
621 MI->getOperand(0).getReg())
622 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
623 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
624
625 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
626 return BB;
627}