blob: a5999feaa3e2a83147545dda22dd0cedfef8fbc9 [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 Korobeynikova51752c2009-07-16 13:42:31 +000044 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000045 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
46
47 // Compute derived properties from the register classes
48 computeRegisterProperties();
49
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000050 // Set shifts properties
51 setShiftAmountFlavor(Extend);
52 setShiftAmountType(MVT::i32);
53
Anton Korobeynikov4403b932009-07-16 13:27:25 +000054 // Provide all sorts of operation actions
Anton Korobeynikovbf022172009-07-16 13:53:35 +000055 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
56 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
57 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000058
Anton Korobeynikove0167c12009-07-16 13:35:30 +000059 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000060 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000061
62 setOperationAction(ISD::RET, MVT::Other, Custom);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000063
64 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
65 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
66 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000067
68 // FIXME: Can we lower these 2 efficiently?
69 setOperationAction(ISD::SETCC, MVT::i32, Expand);
70 setOperationAction(ISD::SETCC, MVT::i64, Expand);
71 setOperationAction(ISD::SELECT, MVT::i32, Expand);
72 setOperationAction(ISD::SELECT, MVT::i64, Expand);
73 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
74 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +000075
76 // FIXME: We can lower this better
77 setOperationAction(ISD::MULHS, MVT::i32, Expand);
78 setOperationAction(ISD::MULHS, MVT::i64, Expand);
79 setOperationAction(ISD::MULHU, MVT::i32, Expand);
80 setOperationAction(ISD::MULHU, MVT::i64, Expand);
81
82 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
83 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
84 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
85 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000086}
87
88SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
89 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000090 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
91 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +000092 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000093 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000094 case ISD::SELECT_CC: return LowerSELECT_CC(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) {
367 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
368 RVLocs[i].getValVT(), InFlag).getValue(1);
369 InFlag = Chain.getValue(2);
370 ResultVals.push_back(Chain.getValue(0));
371 }
372
373 ResultVals.push_back(Chain);
374
375 // Merge everything together with a MERGE_VALUES node.
376 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
377 &ResultVals[0], ResultVals.size()).getNode();
378}
379
380
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000381SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
382 // CCValAssign - represent the assignment of the return value to a location
383 SmallVector<CCValAssign, 16> RVLocs;
384 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
385 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
386 DebugLoc dl = Op.getDebugLoc();
387
388 // CCState - Info about the registers and stack slot.
389 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
390
391 // Analize return values of ISD::RET
392 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
393
394 // If this is the first return lowered for this function, add the regs to the
395 // liveout set for the function.
396 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
397 for (unsigned i = 0; i != RVLocs.size(); ++i)
398 if (RVLocs[i].isRegLoc())
399 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
400 }
401
402 // The chain is always operand #0
403 SDValue Chain = Op.getOperand(0);
404 SDValue Flag;
405
406 // Copy the result values into the output registers.
407 for (unsigned i = 0; i != RVLocs.size(); ++i) {
408 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000409 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000410 assert(VA.isRegLoc() && "Can only return in registers!");
411
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000412 // If this is an 8/16/32-bit value, it is really should be passed promoted
413 // to 64 bits.
414 if (VA.getLocInfo() == CCValAssign::SExt)
415 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
416 else if (VA.getLocInfo() == CCValAssign::ZExt)
417 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
418 else if (VA.getLocInfo() == CCValAssign::AExt)
419 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
420
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000421 // ISD::RET => ret chain, (regnum1,val1), ...
422 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000423 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000424
425 // Guarantee that all emitted copies are stuck together,
426 // avoiding something bad.
427 Flag = Chain.getValue(1);
428 }
429
430 if (Flag.getNode())
431 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
432
433 // Return Void
434 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
435}
436
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000437SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
438 ISD::CondCode CC, SDValue &SystemZCC,
439 SelectionDAG &DAG) {
440 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
441
442 // FIXME: Emit a test if RHS is zero
443
444 bool isUnsigned = false;
445 SystemZCC::CondCodes TCC;
446 switch (CC) {
447 default: assert(0 && "Invalid integer condition!");
448 case ISD::SETEQ:
449 TCC = SystemZCC::E;
450 break;
451 case ISD::SETNE:
452 TCC = SystemZCC::NE;
453 break;
454 case ISD::SETULE:
455 isUnsigned = true; // FALLTHROUGH
456 case ISD::SETLE:
457 TCC = SystemZCC::LE;
458 break;
459 case ISD::SETUGE:
460 isUnsigned = true; // FALLTHROUGH
461 case ISD::SETGE:
462 TCC = SystemZCC::HE;
463 break;
464 case ISD::SETUGT:
465 isUnsigned = true;
466 case ISD::SETGT:
467 TCC = SystemZCC::H; // FALLTHROUGH
468 break;
469 case ISD::SETULT:
470 isUnsigned = true;
471 case ISD::SETLT: // FALLTHROUGH
472 TCC = SystemZCC::L;
473 break;
474 }
475
476 SystemZCC = DAG.getConstant(TCC, MVT::i32);
477
478 DebugLoc dl = LHS.getDebugLoc();
479 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
480 dl, MVT::Flag, LHS, RHS);
481}
482
483
484SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
485 SDValue Chain = Op.getOperand(0);
486 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
487 SDValue LHS = Op.getOperand(2);
488 SDValue RHS = Op.getOperand(3);
489 SDValue Dest = Op.getOperand(4);
490 DebugLoc dl = Op.getDebugLoc();
491
492 SDValue SystemZCC;
493 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
494 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
495 Chain, Dest, SystemZCC, Flag);
496}
497
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000498SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
499 SDValue LHS = Op.getOperand(0);
500 SDValue RHS = Op.getOperand(1);
501 SDValue TrueV = Op.getOperand(2);
502 SDValue FalseV = Op.getOperand(3);
503 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
504 DebugLoc dl = Op.getDebugLoc();
505
506 SDValue SystemZCC;
507 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
508
509 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
510 SmallVector<SDValue, 4> Ops;
511 Ops.push_back(TrueV);
512 Ops.push_back(FalseV);
513 Ops.push_back(SystemZCC);
514 Ops.push_back(Flag);
515
516 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
517}
518
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000519
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000520const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
521 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000522 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000523 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000524 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
525 case SystemZISD::CMP: return "SystemZISD::CMP";
526 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000527 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000528 default: return NULL;
529 }
530}
531
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000532//===----------------------------------------------------------------------===//
533// Other Lowering Code
534//===----------------------------------------------------------------------===//
535
536MachineBasicBlock*
537SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
538 MachineBasicBlock *BB) const {
539 const SystemZInstrInfo &TII = *TM.getInstrInfo();
540 DebugLoc dl = MI->getDebugLoc();
541 assert((MI->getOpcode() == SystemZ::Select32 ||
542 MI->getOpcode() == SystemZ::Select64) &&
543 "Unexpected instr type to insert");
544
545 // To "insert" a SELECT instruction, we actually have to insert the diamond
546 // control-flow pattern. The incoming instruction knows the destination vreg
547 // to set, the condition code register to branch on, the true/false values to
548 // select between, and a branch opcode to use.
549 const BasicBlock *LLVM_BB = BB->getBasicBlock();
550 MachineFunction::iterator I = BB;
551 ++I;
552
553 // thisMBB:
554 // ...
555 // TrueVal = ...
556 // cmpTY ccX, r1, r2
557 // jCC copy1MBB
558 // fallthrough --> copy0MBB
559 MachineBasicBlock *thisMBB = BB;
560 MachineFunction *F = BB->getParent();
561 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
562 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
563 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
564 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
565 F->insert(I, copy0MBB);
566 F->insert(I, copy1MBB);
567 // Update machine-CFG edges by transferring all successors of the current
568 // block to the new block which will contain the Phi node for the select.
569 copy1MBB->transferSuccessors(BB);
570 // Next, add the true and fallthrough blocks as its successors.
571 BB->addSuccessor(copy0MBB);
572 BB->addSuccessor(copy1MBB);
573
574 // copy0MBB:
575 // %FalseValue = ...
576 // # fallthrough to copy1MBB
577 BB = copy0MBB;
578
579 // Update machine-CFG edges
580 BB->addSuccessor(copy1MBB);
581
582 // copy1MBB:
583 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
584 // ...
585 BB = copy1MBB;
586 BuildMI(BB, dl, TII.get(SystemZ::PHI),
587 MI->getOperand(0).getReg())
588 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
589 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
590
591 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
592 return BB;
593}