blob: c1004e12c8efaaae95213d387dcfe1c3d721d77e [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());
189 // Load the argument to a virtual register
190 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
191 if (ObjSize > 8) {
192 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
193 << VA.getLocVT().getSimpleVT()
194 << "\n";
195 }
196 // Create the frame index object for this incoming parameter...
197 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
198
199 // Create the SelectionDAG nodes corresponding to a load
200 //from this parameter
201 SDValue FIN = DAG.getFrameIndex(FI, MVT::i64);
202 ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN,
203 PseudoSourceValue::getFixedStack(FI), 0));
204 }
205 }
206
207 ArgValues.push_back(Root);
208
209 // Return the new list of results.
210 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
211 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
212}
213
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000214/// LowerCCCCallTo - functions arguments are copied from virtual regs to
215/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
216/// TODO: sret.
217SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
218 unsigned CC) {
219 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
220 SDValue Chain = TheCall->getChain();
221 SDValue Callee = TheCall->getCallee();
222 bool isVarArg = TheCall->isVarArg();
223 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000224 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000225
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000226 // Offset to first argument stack slot.
227 const unsigned FirstArgOffset = 160;
228
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000229 // Analyze operands of the call, assigning locations to each operand.
230 SmallVector<CCValAssign, 16> ArgLocs;
231 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
232
233 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
234
235 // Get a count of how many bytes are to be pushed on the stack.
236 unsigned NumBytes = CCInfo.getNextStackOffset();
237
238 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
239 getPointerTy(), true));
240
241 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
242 SmallVector<SDValue, 12> MemOpChains;
243 SDValue StackPtr;
244
245 // Walk the register/memloc assignments, inserting copies/loads.
246 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
247 CCValAssign &VA = ArgLocs[i];
248
249 // Arguments start after the 5 first operands of ISD::CALL
250 SDValue Arg = TheCall->getArg(i);
251
252 // Promote the value if needed.
253 switch (VA.getLocInfo()) {
254 default: assert(0 && "Unknown loc info!");
255 case CCValAssign::Full: break;
256 case CCValAssign::SExt:
257 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
258 break;
259 case CCValAssign::ZExt:
260 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
261 break;
262 case CCValAssign::AExt:
263 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
264 break;
265 }
266
267 // Arguments that can be passed on register must be kept at RegsToPass
268 // vector
269 if (VA.isRegLoc()) {
270 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
271 } else {
272 assert(VA.isMemLoc());
273
274 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000275 StackPtr =
276 DAG.getCopyFromReg(Chain, dl,
277 (RegInfo->hasFP(MF) ?
278 SystemZ::R11D : SystemZ::R15D),
279 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000280
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000281 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
282 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
283 StackPtr,
284 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000285
286 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000287 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000288 }
289 }
290
291 // Transform all store nodes into one single node because all store nodes are
292 // independent of each other.
293 if (!MemOpChains.empty())
294 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
295 &MemOpChains[0], MemOpChains.size());
296
297 // Build a sequence of copy-to-reg nodes chained together with token chain and
298 // flag operands which copy the outgoing args into registers. The InFlag in
299 // necessary since all emited instructions must be stuck together.
300 SDValue InFlag;
301 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
302 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
303 RegsToPass[i].second, InFlag);
304 InFlag = Chain.getValue(1);
305 }
306
307 // If the callee is a GlobalAddress node (quite common, every direct call is)
308 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
309 // Likewise ExternalSymbol -> TargetExternalSymbol.
310 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
311 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
312 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
313 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
314
315 // Returns a chain & a flag for retval copy to use.
316 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
317 SmallVector<SDValue, 8> Ops;
318 Ops.push_back(Chain);
319 Ops.push_back(Callee);
320
321 // Add argument registers to the end of the list so that they are
322 // known live into the call.
323 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
324 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
325 RegsToPass[i].second.getValueType()));
326
327 if (InFlag.getNode())
328 Ops.push_back(InFlag);
329
330 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
331 InFlag = Chain.getValue(1);
332
333 // Create the CALLSEQ_END node.
334 Chain = DAG.getCALLSEQ_END(Chain,
335 DAG.getConstant(NumBytes, getPointerTy(), true),
336 DAG.getConstant(0, getPointerTy(), true),
337 InFlag);
338 InFlag = Chain.getValue(1);
339
340 // Handle result values, copying them out of physregs into vregs that we
341 // return.
342 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
343 Op.getResNo());
344}
345
346/// LowerCallResult - Lower the result values of an ISD::CALL into the
347/// appropriate copies out of appropriate physical registers. This assumes that
348/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
349/// being lowered. Returns a SDNode with the same number of values as the
350/// ISD::CALL.
351SDNode*
352SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
353 CallSDNode *TheCall,
354 unsigned CallingConv,
355 SelectionDAG &DAG) {
356 bool isVarArg = TheCall->isVarArg();
357 DebugLoc dl = TheCall->getDebugLoc();
358
359 // Assign locations to each value returned by this call.
360 SmallVector<CCValAssign, 16> RVLocs;
361 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
362
363 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
364 SmallVector<SDValue, 8> ResultVals;
365
366 // Copy all of the result registers out of their specified physreg.
367 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000368 CCValAssign &VA = RVLocs[i];
369
370 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
371 VA.getLocVT(), InFlag).getValue(1);
372 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000373 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000374
375 // If this is an 8/16/32-bit value, it is really passed promoted to 64
376 // bits. Insert an assert[sz]ext to capture this, then truncate to the
377 // right size.
378 if (VA.getLocInfo() == CCValAssign::SExt)
379 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
380 DAG.getValueType(VA.getValVT()));
381 else if (VA.getLocInfo() == CCValAssign::ZExt)
382 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
383 DAG.getValueType(VA.getValVT()));
384
385 if (VA.getLocInfo() != CCValAssign::Full)
386 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
387
388 ResultVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000389 }
390
391 ResultVals.push_back(Chain);
392
393 // Merge everything together with a MERGE_VALUES node.
394 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
395 &ResultVals[0], ResultVals.size()).getNode();
396}
397
398
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000399SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
400 // CCValAssign - represent the assignment of the return value to a location
401 SmallVector<CCValAssign, 16> RVLocs;
402 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
403 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
404 DebugLoc dl = Op.getDebugLoc();
405
406 // CCState - Info about the registers and stack slot.
407 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
408
409 // Analize return values of ISD::RET
410 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
411
412 // If this is the first return lowered for this function, add the regs to the
413 // liveout set for the function.
414 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
415 for (unsigned i = 0; i != RVLocs.size(); ++i)
416 if (RVLocs[i].isRegLoc())
417 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
418 }
419
420 // The chain is always operand #0
421 SDValue Chain = Op.getOperand(0);
422 SDValue Flag;
423
424 // Copy the result values into the output registers.
425 for (unsigned i = 0; i != RVLocs.size(); ++i) {
426 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000427 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000428 assert(VA.isRegLoc() && "Can only return in registers!");
429
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000430 // If this is an 8/16/32-bit value, it is really should be passed promoted
431 // to 64 bits.
432 if (VA.getLocInfo() == CCValAssign::SExt)
433 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
434 else if (VA.getLocInfo() == CCValAssign::ZExt)
435 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
436 else if (VA.getLocInfo() == CCValAssign::AExt)
437 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
438
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000439 // ISD::RET => ret chain, (regnum1,val1), ...
440 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000441 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000442
443 // Guarantee that all emitted copies are stuck together,
444 // avoiding something bad.
445 Flag = Chain.getValue(1);
446 }
447
448 if (Flag.getNode())
449 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
450
451 // Return Void
452 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
453}
454
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000455SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
456 ISD::CondCode CC, SDValue &SystemZCC,
457 SelectionDAG &DAG) {
458 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
459
460 // FIXME: Emit a test if RHS is zero
461
462 bool isUnsigned = false;
463 SystemZCC::CondCodes TCC;
464 switch (CC) {
465 default: assert(0 && "Invalid integer condition!");
466 case ISD::SETEQ:
467 TCC = SystemZCC::E;
468 break;
469 case ISD::SETNE:
470 TCC = SystemZCC::NE;
471 break;
472 case ISD::SETULE:
473 isUnsigned = true; // FALLTHROUGH
474 case ISD::SETLE:
475 TCC = SystemZCC::LE;
476 break;
477 case ISD::SETUGE:
478 isUnsigned = true; // FALLTHROUGH
479 case ISD::SETGE:
480 TCC = SystemZCC::HE;
481 break;
482 case ISD::SETUGT:
483 isUnsigned = true;
484 case ISD::SETGT:
485 TCC = SystemZCC::H; // FALLTHROUGH
486 break;
487 case ISD::SETULT:
488 isUnsigned = true;
489 case ISD::SETLT: // FALLTHROUGH
490 TCC = SystemZCC::L;
491 break;
492 }
493
494 SystemZCC = DAG.getConstant(TCC, MVT::i32);
495
496 DebugLoc dl = LHS.getDebugLoc();
497 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
498 dl, MVT::Flag, LHS, RHS);
499}
500
501
502SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
503 SDValue Chain = Op.getOperand(0);
504 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
505 SDValue LHS = Op.getOperand(2);
506 SDValue RHS = Op.getOperand(3);
507 SDValue Dest = Op.getOperand(4);
508 DebugLoc dl = Op.getDebugLoc();
509
510 SDValue SystemZCC;
511 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
512 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
513 Chain, Dest, SystemZCC, Flag);
514}
515
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000516SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
517 SDValue LHS = Op.getOperand(0);
518 SDValue RHS = Op.getOperand(1);
519 SDValue TrueV = Op.getOperand(2);
520 SDValue FalseV = Op.getOperand(3);
521 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
522 DebugLoc dl = Op.getDebugLoc();
523
524 SDValue SystemZCC;
525 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
526
527 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
528 SmallVector<SDValue, 4> Ops;
529 Ops.push_back(TrueV);
530 Ops.push_back(FalseV);
531 Ops.push_back(SystemZCC);
532 Ops.push_back(Flag);
533
534 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
535}
536
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000537SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
538 SelectionDAG &DAG) {
539 DebugLoc dl = Op.getDebugLoc();
540 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
541 SDValue GA = DAG.getTargetGlobalAddress(GV, getPointerTy());
542
543 // FIXME: Verify stuff for constant globals entries
544 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), GA);
545}
546
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000547
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000548SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
549 SelectionDAG &DAG) {
550 DebugLoc dl = Op.getDebugLoc();
551 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
552 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
553
554 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
555}
556
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000557const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
558 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000559 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000560 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000561 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
562 case SystemZISD::CMP: return "SystemZISD::CMP";
563 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000564 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000565 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000566 default: return NULL;
567 }
568}
569
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000570//===----------------------------------------------------------------------===//
571// Other Lowering Code
572//===----------------------------------------------------------------------===//
573
574MachineBasicBlock*
575SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
576 MachineBasicBlock *BB) const {
577 const SystemZInstrInfo &TII = *TM.getInstrInfo();
578 DebugLoc dl = MI->getDebugLoc();
579 assert((MI->getOpcode() == SystemZ::Select32 ||
580 MI->getOpcode() == SystemZ::Select64) &&
581 "Unexpected instr type to insert");
582
583 // To "insert" a SELECT instruction, we actually have to insert the diamond
584 // control-flow pattern. The incoming instruction knows the destination vreg
585 // to set, the condition code register to branch on, the true/false values to
586 // select between, and a branch opcode to use.
587 const BasicBlock *LLVM_BB = BB->getBasicBlock();
588 MachineFunction::iterator I = BB;
589 ++I;
590
591 // thisMBB:
592 // ...
593 // TrueVal = ...
594 // cmpTY ccX, r1, r2
595 // jCC copy1MBB
596 // fallthrough --> copy0MBB
597 MachineBasicBlock *thisMBB = BB;
598 MachineFunction *F = BB->getParent();
599 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
600 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
601 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
602 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
603 F->insert(I, copy0MBB);
604 F->insert(I, copy1MBB);
605 // Update machine-CFG edges by transferring all successors of the current
606 // block to the new block which will contain the Phi node for the select.
607 copy1MBB->transferSuccessors(BB);
608 // Next, add the true and fallthrough blocks as its successors.
609 BB->addSuccessor(copy0MBB);
610 BB->addSuccessor(copy1MBB);
611
612 // copy0MBB:
613 // %FalseValue = ...
614 // # fallthrough to copy1MBB
615 BB = copy0MBB;
616
617 // Update machine-CFG edges
618 BB->addSuccessor(copy1MBB);
619
620 // copy1MBB:
621 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
622 // ...
623 BB = copy1MBB;
624 BuildMI(BB, dl, TII.get(SystemZ::PHI),
625 MI->getOperand(0).getReg())
626 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
627 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
628
629 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
630 return BB;
631}