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