blob: 87ea5c3ddaa501bf4dcf7ccaa43755ae025cb545 [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
65 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
66 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
67 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +000068 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000069
70 // FIXME: Can we lower these 2 efficiently?
71 setOperationAction(ISD::SETCC, MVT::i32, Expand);
72 setOperationAction(ISD::SETCC, MVT::i64, Expand);
73 setOperationAction(ISD::SELECT, MVT::i32, Expand);
74 setOperationAction(ISD::SELECT, MVT::i64, Expand);
75 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
76 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +000077
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +000078 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
79 // unsigned.
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +000080 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +000081 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000082}
83
84SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
85 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000086 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
87 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +000088 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000089 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000090 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +000091 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000092 default:
93 assert(0 && "unimplemented operand");
94 return SDValue();
95 }
96}
97
98//===----------------------------------------------------------------------===//
99// Calling Convention Implementation
100//===----------------------------------------------------------------------===//
101
102#include "SystemZGenCallingConv.inc"
103
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000104SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
105 SelectionDAG &DAG) {
106 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
107 switch (CC) {
108 default:
109 assert(0 && "Unsupported calling convention");
110 case CallingConv::C:
111 case CallingConv::Fast:
112 return LowerCCCArguments(Op, DAG);
113 }
114}
115
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000116SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
117 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
118 unsigned CallingConv = TheCall->getCallingConv();
119 switch (CallingConv) {
120 default:
121 assert(0 && "Unsupported calling convention");
122 case CallingConv::Fast:
123 case CallingConv::C:
124 return LowerCCCCallTo(Op, DAG, CallingConv);
125 }
126}
127
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000128/// LowerCCCArguments - transform physical registers into virtual registers and
129/// generate load operations for arguments places on the stack.
130// FIXME: struct return stuff
131// FIXME: varargs
132SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
133 SelectionDAG &DAG) {
134 MachineFunction &MF = DAG.getMachineFunction();
135 MachineFrameInfo *MFI = MF.getFrameInfo();
136 MachineRegisterInfo &RegInfo = MF.getRegInfo();
137 SDValue Root = Op.getOperand(0);
138 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
139 unsigned CC = MF.getFunction()->getCallingConv();
140 DebugLoc dl = Op.getDebugLoc();
141
142 // Assign locations to all of the incoming arguments.
143 SmallVector<CCValAssign, 16> ArgLocs;
144 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
145 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
146
147 assert(!isVarArg && "Varargs not supported yet");
148
149 SmallVector<SDValue, 16> ArgValues;
150 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
151 CCValAssign &VA = ArgLocs[i];
152 if (VA.isRegLoc()) {
153 // Arguments passed in registers
154 MVT RegVT = VA.getLocVT();
155 switch (RegVT.getSimpleVT()) {
156 default:
157 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
158 << RegVT.getSimpleVT()
159 << "\n";
160 abort();
161 case MVT::i64:
162 unsigned VReg =
163 RegInfo.createVirtualRegister(SystemZ::GR64RegisterClass);
164 RegInfo.addLiveIn(VA.getLocReg(), VReg);
165 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
166
167 // If this is an 8/16/32-bit value, it is really passed promoted to 64
168 // bits. Insert an assert[sz]ext to capture this, then truncate to the
169 // right size.
170 if (VA.getLocInfo() == CCValAssign::SExt)
171 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
172 DAG.getValueType(VA.getValVT()));
173 else if (VA.getLocInfo() == CCValAssign::ZExt)
174 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
175 DAG.getValueType(VA.getValVT()));
176
177 if (VA.getLocInfo() != CCValAssign::Full)
178 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
179
180 ArgValues.push_back(ArgValue);
181 }
182 } else {
183 // Sanity check
184 assert(VA.isMemLoc());
185 // Load the argument to a virtual register
186 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
187 if (ObjSize > 8) {
188 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
189 << VA.getLocVT().getSimpleVT()
190 << "\n";
191 }
192 // Create the frame index object for this incoming parameter...
193 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
194
195 // Create the SelectionDAG nodes corresponding to a load
196 //from this parameter
197 SDValue FIN = DAG.getFrameIndex(FI, MVT::i64);
198 ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN,
199 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 Korobeynikov4403b932009-07-16 13:27:25 +0000544const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
545 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000546 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000547 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000548 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
549 case SystemZISD::CMP: return "SystemZISD::CMP";
550 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000551 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000552 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000553 default: return NULL;
554 }
555}
556
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000557//===----------------------------------------------------------------------===//
558// Other Lowering Code
559//===----------------------------------------------------------------------===//
560
561MachineBasicBlock*
562SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
563 MachineBasicBlock *BB) const {
564 const SystemZInstrInfo &TII = *TM.getInstrInfo();
565 DebugLoc dl = MI->getDebugLoc();
566 assert((MI->getOpcode() == SystemZ::Select32 ||
567 MI->getOpcode() == SystemZ::Select64) &&
568 "Unexpected instr type to insert");
569
570 // To "insert" a SELECT instruction, we actually have to insert the diamond
571 // control-flow pattern. The incoming instruction knows the destination vreg
572 // to set, the condition code register to branch on, the true/false values to
573 // select between, and a branch opcode to use.
574 const BasicBlock *LLVM_BB = BB->getBasicBlock();
575 MachineFunction::iterator I = BB;
576 ++I;
577
578 // thisMBB:
579 // ...
580 // TrueVal = ...
581 // cmpTY ccX, r1, r2
582 // jCC copy1MBB
583 // fallthrough --> copy0MBB
584 MachineBasicBlock *thisMBB = BB;
585 MachineFunction *F = BB->getParent();
586 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
587 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
588 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
589 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
590 F->insert(I, copy0MBB);
591 F->insert(I, copy1MBB);
592 // Update machine-CFG edges by transferring all successors of the current
593 // block to the new block which will contain the Phi node for the select.
594 copy1MBB->transferSuccessors(BB);
595 // Next, add the true and fallthrough blocks as its successors.
596 BB->addSuccessor(copy0MBB);
597 BB->addSuccessor(copy1MBB);
598
599 // copy0MBB:
600 // %FalseValue = ...
601 // # fallthrough to copy1MBB
602 BB = copy0MBB;
603
604 // Update machine-CFG edges
605 BB->addSuccessor(copy1MBB);
606
607 // copy1MBB:
608 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
609 // ...
610 BB = copy1MBB;
611 BuildMI(BB, dl, TII.get(SystemZ::PHI),
612 MI->getOperand(0).getReg())
613 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
614 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
615
616 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
617 return BB;
618}