blob: 415ab724dce84685413503f26a93032e303ab6cb [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) {
364 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
365 RVLocs[i].getValVT(), InFlag).getValue(1);
366 InFlag = Chain.getValue(2);
367 ResultVals.push_back(Chain.getValue(0));
368 }
369
370 ResultVals.push_back(Chain);
371
372 // Merge everything together with a MERGE_VALUES node.
373 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
374 &ResultVals[0], ResultVals.size()).getNode();
375}
376
377
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000378SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
379 // CCValAssign - represent the assignment of the return value to a location
380 SmallVector<CCValAssign, 16> RVLocs;
381 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
382 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
383 DebugLoc dl = Op.getDebugLoc();
384
385 // CCState - Info about the registers and stack slot.
386 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
387
388 // Analize return values of ISD::RET
389 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
390
391 // If this is the first return lowered for this function, add the regs to the
392 // liveout set for the function.
393 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
394 for (unsigned i = 0; i != RVLocs.size(); ++i)
395 if (RVLocs[i].isRegLoc())
396 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
397 }
398
399 // The chain is always operand #0
400 SDValue Chain = Op.getOperand(0);
401 SDValue Flag;
402
403 // Copy the result values into the output registers.
404 for (unsigned i = 0; i != RVLocs.size(); ++i) {
405 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000406 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000407 assert(VA.isRegLoc() && "Can only return in registers!");
408
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000409 // If this is an 8/16/32-bit value, it is really should be passed promoted
410 // to 64 bits.
411 if (VA.getLocInfo() == CCValAssign::SExt)
412 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
413 else if (VA.getLocInfo() == CCValAssign::ZExt)
414 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
415 else if (VA.getLocInfo() == CCValAssign::AExt)
416 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
417
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000418 // ISD::RET => ret chain, (regnum1,val1), ...
419 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000420 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000421
422 // Guarantee that all emitted copies are stuck together,
423 // avoiding something bad.
424 Flag = Chain.getValue(1);
425 }
426
427 if (Flag.getNode())
428 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
429
430 // Return Void
431 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
432}
433
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000434SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
435 ISD::CondCode CC, SDValue &SystemZCC,
436 SelectionDAG &DAG) {
437 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
438
439 // FIXME: Emit a test if RHS is zero
440
441 bool isUnsigned = false;
442 SystemZCC::CondCodes TCC;
443 switch (CC) {
444 default: assert(0 && "Invalid integer condition!");
445 case ISD::SETEQ:
446 TCC = SystemZCC::E;
447 break;
448 case ISD::SETNE:
449 TCC = SystemZCC::NE;
450 break;
451 case ISD::SETULE:
452 isUnsigned = true; // FALLTHROUGH
453 case ISD::SETLE:
454 TCC = SystemZCC::LE;
455 break;
456 case ISD::SETUGE:
457 isUnsigned = true; // FALLTHROUGH
458 case ISD::SETGE:
459 TCC = SystemZCC::HE;
460 break;
461 case ISD::SETUGT:
462 isUnsigned = true;
463 case ISD::SETGT:
464 TCC = SystemZCC::H; // FALLTHROUGH
465 break;
466 case ISD::SETULT:
467 isUnsigned = true;
468 case ISD::SETLT: // FALLTHROUGH
469 TCC = SystemZCC::L;
470 break;
471 }
472
473 SystemZCC = DAG.getConstant(TCC, MVT::i32);
474
475 DebugLoc dl = LHS.getDebugLoc();
476 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
477 dl, MVT::Flag, LHS, RHS);
478}
479
480
481SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
482 SDValue Chain = Op.getOperand(0);
483 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
484 SDValue LHS = Op.getOperand(2);
485 SDValue RHS = Op.getOperand(3);
486 SDValue Dest = Op.getOperand(4);
487 DebugLoc dl = Op.getDebugLoc();
488
489 SDValue SystemZCC;
490 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
491 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
492 Chain, Dest, SystemZCC, Flag);
493}
494
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000495SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
496 SDValue LHS = Op.getOperand(0);
497 SDValue RHS = Op.getOperand(1);
498 SDValue TrueV = Op.getOperand(2);
499 SDValue FalseV = Op.getOperand(3);
500 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
501 DebugLoc dl = Op.getDebugLoc();
502
503 SDValue SystemZCC;
504 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
505
506 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
507 SmallVector<SDValue, 4> Ops;
508 Ops.push_back(TrueV);
509 Ops.push_back(FalseV);
510 Ops.push_back(SystemZCC);
511 Ops.push_back(Flag);
512
513 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
514}
515
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000516SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
517 SelectionDAG &DAG) {
518 DebugLoc dl = Op.getDebugLoc();
519 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
520 SDValue GA = DAG.getTargetGlobalAddress(GV, getPointerTy());
521
522 // FIXME: Verify stuff for constant globals entries
523 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), GA);
524}
525
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000526
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000527const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
528 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000529 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000530 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000531 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
532 case SystemZISD::CMP: return "SystemZISD::CMP";
533 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000534 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000535 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000536 default: return NULL;
537 }
538}
539
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000540//===----------------------------------------------------------------------===//
541// Other Lowering Code
542//===----------------------------------------------------------------------===//
543
544MachineBasicBlock*
545SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
546 MachineBasicBlock *BB) const {
547 const SystemZInstrInfo &TII = *TM.getInstrInfo();
548 DebugLoc dl = MI->getDebugLoc();
549 assert((MI->getOpcode() == SystemZ::Select32 ||
550 MI->getOpcode() == SystemZ::Select64) &&
551 "Unexpected instr type to insert");
552
553 // To "insert" a SELECT instruction, we actually have to insert the diamond
554 // control-flow pattern. The incoming instruction knows the destination vreg
555 // to set, the condition code register to branch on, the true/false values to
556 // select between, and a branch opcode to use.
557 const BasicBlock *LLVM_BB = BB->getBasicBlock();
558 MachineFunction::iterator I = BB;
559 ++I;
560
561 // thisMBB:
562 // ...
563 // TrueVal = ...
564 // cmpTY ccX, r1, r2
565 // jCC copy1MBB
566 // fallthrough --> copy0MBB
567 MachineBasicBlock *thisMBB = BB;
568 MachineFunction *F = BB->getParent();
569 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
570 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
571 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
572 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
573 F->insert(I, copy0MBB);
574 F->insert(I, copy1MBB);
575 // Update machine-CFG edges by transferring all successors of the current
576 // block to the new block which will contain the Phi node for the select.
577 copy1MBB->transferSuccessors(BB);
578 // Next, add the true and fallthrough blocks as its successors.
579 BB->addSuccessor(copy0MBB);
580 BB->addSuccessor(copy1MBB);
581
582 // copy0MBB:
583 // %FalseValue = ...
584 // # fallthrough to copy1MBB
585 BB = copy0MBB;
586
587 // Update machine-CFG edges
588 BB->addSuccessor(copy1MBB);
589
590 // copy1MBB:
591 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
592 // ...
593 BB = copy1MBB;
594 BuildMI(BB, dl, TII.get(SystemZ::PHI),
595 MI->getOperand(0).getReg())
596 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
597 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
598
599 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
600 return BB;
601}