blob: 2022ec04ce23a739cc63e98911356090625637da [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"
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000034#include "llvm/Target/TargetOptions.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000035#include "llvm/Target/TargetLoweringObjectFile.h"
36#include "llvm/Support/Debug.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000037#include "llvm/ADT/VectorExtras.h"
38using namespace llvm;
39
40SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
Chris Lattnerf0144122009-07-28 03:13:23 +000041 TargetLowering(tm, new TargetLoweringObjectFileELF()),
42 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
Anton Korobeynikov4403b932009-07-16 13:27:25 +000043
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +000044 RegInfo = TM.getRegisterInfo();
45
Anton Korobeynikov4403b932009-07-16 13:27:25 +000046 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +000047 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
48 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
49 addRegisterClass(MVT::v2i32,SystemZ::GR64PRegisterClass);
50 addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000051
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000052 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +000053 addRegisterClass(MVT::f32, SystemZ::FP32RegisterClass);
54 addRegisterClass(MVT::f64, SystemZ::FP64RegisterClass);
Anton Korobeynikov1ada84d2009-07-16 14:24:16 +000055
56 addLegalFPImmediate(APFloat(+0.0)); // lzer
57 addLegalFPImmediate(APFloat(+0.0f)); // lzdr
58 addLegalFPImmediate(APFloat(-0.0)); // lzer + lner
59 addLegalFPImmediate(APFloat(-0.0f)); // lzdr + lndr
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000060 }
61
Anton Korobeynikov4403b932009-07-16 13:27:25 +000062 // Compute derived properties from the register classes
63 computeRegisterProperties();
64
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000065 // Set shifts properties
Owen Anderson825b72b2009-08-11 20:47:22 +000066 setShiftAmountType(MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000067
Anton Korobeynikov4403b932009-07-16 13:27:25 +000068 // Provide all sorts of operation actions
Owen Anderson825b72b2009-08-11 20:47:22 +000069 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
70 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
71 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000072
Owen Anderson825b72b2009-08-11 20:47:22 +000073 setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Expand);
74 setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Expand);
75 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Anton Korobeynikov299dc782009-07-16 14:22:30 +000076
Owen Anderson825b72b2009-08-11 20:47:22 +000077 setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Expand);
78 setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Expand);
79 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
Anton Korobeynikov23eff5c2009-07-16 14:20:08 +000080
Anton Korobeynikove0167c12009-07-16 13:35:30 +000081 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000082 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov159ac632009-07-16 14:28:46 +000083 setBooleanContents(ZeroOrOneBooleanContent);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000084
Owen Anderson825b72b2009-08-11 20:47:22 +000085 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
86 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
87 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
88 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
89 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
90 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
91 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
92 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
93 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
94 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
95 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000096
Owen Anderson825b72b2009-08-11 20:47:22 +000097 setOperationAction(ISD::SDIV, MVT::i32, Expand);
98 setOperationAction(ISD::UDIV, MVT::i32, Expand);
99 setOperationAction(ISD::SDIV, MVT::i64, Expand);
100 setOperationAction(ISD::UDIV, MVT::i64, Expand);
101 setOperationAction(ISD::SREM, MVT::i32, Expand);
102 setOperationAction(ISD::UREM, MVT::i32, Expand);
103 setOperationAction(ISD::SREM, MVT::i64, Expand);
104 setOperationAction(ISD::UREM, MVT::i64, Expand);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000105
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Anton Korobeynikove37a37d2009-07-18 12:20:36 +0000107
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
109 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
110 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
111 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
112 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
113 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
Anton Korobeynikov0cca0692009-07-18 12:26:13 +0000114
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000115 // FIXME: Can we lower these 2 efficiently?
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 setOperationAction(ISD::SETCC, MVT::i32, Expand);
117 setOperationAction(ISD::SETCC, MVT::i64, Expand);
118 setOperationAction(ISD::SETCC, MVT::f32, Expand);
119 setOperationAction(ISD::SETCC, MVT::f64, Expand);
120 setOperationAction(ISD::SELECT, MVT::i32, Expand);
121 setOperationAction(ISD::SELECT, MVT::i64, Expand);
122 setOperationAction(ISD::SELECT, MVT::f32, Expand);
123 setOperationAction(ISD::SELECT, MVT::f64, Expand);
124 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
125 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
126 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
127 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000128
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +0000129 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
130 // unsigned.
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 setOperationAction(ISD::MULHS, MVT::i64, Expand);
132 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000133
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000134 // Lower some FP stuff
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setOperationAction(ISD::FSIN, MVT::f32, Expand);
136 setOperationAction(ISD::FSIN, MVT::f64, Expand);
137 setOperationAction(ISD::FCOS, MVT::f32, Expand);
138 setOperationAction(ISD::FCOS, MVT::f64, Expand);
139 setOperationAction(ISD::FREM, MVT::f32, Expand);
140 setOperationAction(ISD::FREM, MVT::f64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000141
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000142 // We have only 64-bit bitconverts
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
144 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000145
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
147 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
148 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
149 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000150
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000152}
153
154SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
155 switch (Op.getOpcode()) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000156 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000157 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000158 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000159 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000160 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000161 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000162 llvm_unreachable("Should not custom lower this!");
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000163 return SDValue();
164 }
165}
166
167//===----------------------------------------------------------------------===//
Anton Korobeynikov3c2734c2009-08-21 18:15:41 +0000168// SystemZ Inline Assembly Support
169//===----------------------------------------------------------------------===//
170
171/// getConstraintType - Given a constraint letter, return the type of
172/// constraint it is for this target.
173TargetLowering::ConstraintType
174SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
175 if (Constraint.size() == 1) {
176 switch (Constraint[0]) {
177 case 'r':
178 return C_RegisterClass;
179 default:
180 break;
181 }
182 }
183 return TargetLowering::getConstraintType(Constraint);
184}
185
186std::pair<unsigned, const TargetRegisterClass*>
187SystemZTargetLowering::
188getRegForInlineAsmConstraint(const std::string &Constraint,
189 EVT VT) const {
190 if (Constraint.size() == 1) {
191 // GCC Constraint Letters
192 switch (Constraint[0]) {
193 default: break;
194 case 'r': // GENERAL_REGS
195 if (VT == MVT::i32)
196 return std::make_pair(0U, SystemZ::GR32RegisterClass);
197 else if (VT == MVT::i128)
198 return std::make_pair(0U, SystemZ::GR128RegisterClass);
199
200 return std::make_pair(0U, SystemZ::GR64RegisterClass);
201 }
202 }
203
204 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
205}
206
207//===----------------------------------------------------------------------===//
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000208// Calling Convention Implementation
209//===----------------------------------------------------------------------===//
210
211#include "SystemZGenCallingConv.inc"
212
Dan Gohman98ca4f22009-08-05 01:29:28 +0000213SDValue
214SystemZTargetLowering::LowerFormalArguments(SDValue Chain,
215 unsigned CallConv,
216 bool isVarArg,
217 const SmallVectorImpl<ISD::InputArg>
218 &Ins,
219 DebugLoc dl,
220 SelectionDAG &DAG,
221 SmallVectorImpl<SDValue> &InVals) {
222
223 switch (CallConv) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000224 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000225 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000226 case CallingConv::C:
227 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000228 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000229 }
230}
231
Dan Gohman98ca4f22009-08-05 01:29:28 +0000232SDValue
233SystemZTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
234 unsigned CallConv, bool isVarArg,
235 bool isTailCall,
236 const SmallVectorImpl<ISD::OutputArg> &Outs,
237 const SmallVectorImpl<ISD::InputArg> &Ins,
238 DebugLoc dl, SelectionDAG &DAG,
239 SmallVectorImpl<SDValue> &InVals) {
240
241 switch (CallConv) {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000242 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000243 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000244 case CallingConv::Fast:
245 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000246 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
247 Outs, Ins, dl, DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000248 }
249}
250
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000251/// LowerCCCArguments - transform physical registers into virtual registers and
252/// generate load operations for arguments places on the stack.
253// FIXME: struct return stuff
254// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000255SDValue
256SystemZTargetLowering::LowerCCCArguments(SDValue Chain,
257 unsigned CallConv,
258 bool isVarArg,
259 const SmallVectorImpl<ISD::InputArg>
260 &Ins,
261 DebugLoc dl,
262 SelectionDAG &DAG,
263 SmallVectorImpl<SDValue> &InVals) {
264
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000265 MachineFunction &MF = DAG.getMachineFunction();
266 MachineFrameInfo *MFI = MF.getFrameInfo();
267 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000268
269 // Assign locations to all of the incoming arguments.
270 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000271 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
272 ArgLocs, *DAG.getContext());
273 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000274
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000275 if (isVarArg)
276 llvm_report_error("Varargs not supported yet");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000277
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000278 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000279 SDValue ArgValue;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000280 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +0000281 EVT LocVT = VA.getLocVT();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000282 if (VA.isRegLoc()) {
283 // Arguments passed in registers
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000284 TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 switch (LocVT.getSimpleVT().SimpleTy) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000286 default:
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000287#ifndef NDEBUG
Dan Gohman98ca4f22009-08-05 01:29:28 +0000288 cerr << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 << LocVT.getSimpleVT().SimpleTy
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000290 << "\n";
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000291#endif
292 llvm_unreachable(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 case MVT::i64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000294 RC = SystemZ::GR64RegisterClass;
295 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 case MVT::f32:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000297 RC = SystemZ::FP32RegisterClass;
298 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 case MVT::f64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000300 RC = SystemZ::FP64RegisterClass;
301 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000302 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000303
304 unsigned VReg = RegInfo.createVirtualRegister(RC);
305 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000306 ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, LocVT);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000307 } else {
308 // Sanity check
309 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000310
311 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000312 // Create the frame index object for this incoming parameter...
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000313 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000314 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000315
316 // Create the SelectionDAG nodes corresponding to a load
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000317 // from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000318 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000319 ArgValue = DAG.getLoad(LocVT, dl, Chain, FIN,
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000320 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000321 }
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000322
323 // If this is an 8/16/32-bit value, it is really passed promoted to 64
324 // bits. Insert an assert[sz]ext to capture this, then truncate to the
325 // right size.
326 if (VA.getLocInfo() == CCValAssign::SExt)
327 ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
328 DAG.getValueType(VA.getValVT()));
329 else if (VA.getLocInfo() == CCValAssign::ZExt)
330 ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
331 DAG.getValueType(VA.getValVT()));
332
333 if (VA.getLocInfo() != CCValAssign::Full)
334 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
335
Dan Gohman98ca4f22009-08-05 01:29:28 +0000336 InVals.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000337 }
338
Dan Gohman98ca4f22009-08-05 01:29:28 +0000339 return Chain;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000340}
341
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000342/// LowerCCCCallTo - functions arguments are copied from virtual regs to
343/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
344/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000345SDValue
346SystemZTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
347 unsigned CallConv, bool isVarArg,
348 bool isTailCall,
349 const SmallVectorImpl<ISD::OutputArg>
350 &Outs,
351 const SmallVectorImpl<ISD::InputArg> &Ins,
352 DebugLoc dl, SelectionDAG &DAG,
353 SmallVectorImpl<SDValue> &InVals) {
354
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000355 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000356
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000357 // Offset to first argument stack slot.
358 const unsigned FirstArgOffset = 160;
359
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000360 // Analyze operands of the call, assigning locations to each operand.
361 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000362 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
363 ArgLocs, *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000364
Dan Gohman98ca4f22009-08-05 01:29:28 +0000365 CCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000366
367 // Get a count of how many bytes are to be pushed on the stack.
368 unsigned NumBytes = CCInfo.getNextStackOffset();
369
370 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
371 getPointerTy(), true));
372
373 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
374 SmallVector<SDValue, 12> MemOpChains;
375 SDValue StackPtr;
376
377 // Walk the register/memloc assignments, inserting copies/loads.
378 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
379 CCValAssign &VA = ArgLocs[i];
380
Dan Gohman98ca4f22009-08-05 01:29:28 +0000381 SDValue Arg = Outs[i].Val;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000382
383 // Promote the value if needed.
384 switch (VA.getLocInfo()) {
385 default: assert(0 && "Unknown loc info!");
386 case CCValAssign::Full: break;
387 case CCValAssign::SExt:
388 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
389 break;
390 case CCValAssign::ZExt:
391 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
392 break;
393 case CCValAssign::AExt:
394 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
395 break;
396 }
397
398 // Arguments that can be passed on register must be kept at RegsToPass
399 // vector
400 if (VA.isRegLoc()) {
401 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
402 } else {
403 assert(VA.isMemLoc());
404
405 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000406 StackPtr =
407 DAG.getCopyFromReg(Chain, dl,
408 (RegInfo->hasFP(MF) ?
409 SystemZ::R11D : SystemZ::R15D),
410 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000411
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000412 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
413 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
414 StackPtr,
415 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000416
417 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000418 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000419 }
420 }
421
422 // Transform all store nodes into one single node because all store nodes are
423 // independent of each other.
424 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000426 &MemOpChains[0], MemOpChains.size());
427
428 // Build a sequence of copy-to-reg nodes chained together with token chain and
429 // flag operands which copy the outgoing args into registers. The InFlag in
430 // necessary since all emited instructions must be stuck together.
431 SDValue InFlag;
432 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
433 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
434 RegsToPass[i].second, InFlag);
435 InFlag = Chain.getValue(1);
436 }
437
438 // If the callee is a GlobalAddress node (quite common, every direct call is)
439 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
440 // Likewise ExternalSymbol -> TargetExternalSymbol.
441 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
442 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
443 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
444 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
445
446 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000448 SmallVector<SDValue, 8> Ops;
449 Ops.push_back(Chain);
450 Ops.push_back(Callee);
451
452 // Add argument registers to the end of the list so that they are
453 // known live into the call.
454 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
455 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
456 RegsToPass[i].second.getValueType()));
457
458 if (InFlag.getNode())
459 Ops.push_back(InFlag);
460
461 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
462 InFlag = Chain.getValue(1);
463
464 // Create the CALLSEQ_END node.
465 Chain = DAG.getCALLSEQ_END(Chain,
466 DAG.getConstant(NumBytes, getPointerTy(), true),
467 DAG.getConstant(0, getPointerTy(), true),
468 InFlag);
469 InFlag = Chain.getValue(1);
470
471 // Handle result values, copying them out of physregs into vregs that we
472 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000473 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
474 DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000475}
476
Dan Gohman98ca4f22009-08-05 01:29:28 +0000477/// LowerCallResult - Lower the result values of a call into the
478/// appropriate copies out of appropriate physical registers.
479///
480SDValue
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000481SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000482 unsigned CallConv, bool isVarArg,
483 const SmallVectorImpl<ISD::InputArg>
484 &Ins,
485 DebugLoc dl, SelectionDAG &DAG,
486 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000487
488 // Assign locations to each value returned by this call.
489 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000490 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
Owen Andersone922c022009-07-22 00:24:57 +0000491 *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000492
Dan Gohman98ca4f22009-08-05 01:29:28 +0000493 CCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000494
495 // Copy all of the result registers out of their specified physreg.
496 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000497 CCValAssign &VA = RVLocs[i];
498
499 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
500 VA.getLocVT(), InFlag).getValue(1);
501 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000502 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000503
504 // If this is an 8/16/32-bit value, it is really passed promoted to 64
505 // bits. Insert an assert[sz]ext to capture this, then truncate to the
506 // right size.
507 if (VA.getLocInfo() == CCValAssign::SExt)
508 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
509 DAG.getValueType(VA.getValVT()));
510 else if (VA.getLocInfo() == CCValAssign::ZExt)
511 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
512 DAG.getValueType(VA.getValVT()));
513
514 if (VA.getLocInfo() != CCValAssign::Full)
515 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
516
Dan Gohman98ca4f22009-08-05 01:29:28 +0000517 InVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000518 }
519
Dan Gohman98ca4f22009-08-05 01:29:28 +0000520 return Chain;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000521}
522
523
Dan Gohman98ca4f22009-08-05 01:29:28 +0000524SDValue
525SystemZTargetLowering::LowerReturn(SDValue Chain,
526 unsigned CallConv, bool isVarArg,
527 const SmallVectorImpl<ISD::OutputArg> &Outs,
528 DebugLoc dl, SelectionDAG &DAG) {
529
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000530 // CCValAssign - represent the assignment of the return value to a location
531 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000532
533 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000534 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
535 RVLocs, *DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000536
Dan Gohman98ca4f22009-08-05 01:29:28 +0000537 // Analize return values.
538 CCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000539
540 // If this is the first return lowered for this function, add the regs to the
541 // liveout set for the function.
542 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
543 for (unsigned i = 0; i != RVLocs.size(); ++i)
544 if (RVLocs[i].isRegLoc())
545 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
546 }
547
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000548 SDValue Flag;
549
550 // Copy the result values into the output registers.
551 for (unsigned i = 0; i != RVLocs.size(); ++i) {
552 CCValAssign &VA = RVLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000553 SDValue ResValue = Outs[i].Val;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000554 assert(VA.isRegLoc() && "Can only return in registers!");
555
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000556 // If this is an 8/16/32-bit value, it is really should be passed promoted
557 // to 64 bits.
558 if (VA.getLocInfo() == CCValAssign::SExt)
559 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
560 else if (VA.getLocInfo() == CCValAssign::ZExt)
561 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
562 else if (VA.getLocInfo() == CCValAssign::AExt)
563 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
564
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000565 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000566
567 // Guarantee that all emitted copies are stuck together,
568 // avoiding something bad.
569 Flag = Chain.getValue(1);
570 }
571
572 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000574
575 // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000577}
578
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000579SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
580 ISD::CondCode CC, SDValue &SystemZCC,
581 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000582 // FIXME: Emit a test if RHS is zero
583
584 bool isUnsigned = false;
585 SystemZCC::CondCodes TCC;
586 switch (CC) {
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000587 default:
588 llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000589 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000590 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000591 TCC = SystemZCC::E;
592 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000593 case ISD::SETUEQ:
594 TCC = SystemZCC::NLH;
595 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000596 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000597 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000598 TCC = SystemZCC::NE;
599 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000600 case ISD::SETUNE:
601 TCC = SystemZCC::LH;
602 break;
603 case ISD::SETO:
604 TCC = SystemZCC::O;
605 break;
606 case ISD::SETUO:
607 TCC = SystemZCC::NO;
608 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000609 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000610 if (LHS.getValueType().isFloatingPoint()) {
611 TCC = SystemZCC::NH;
612 break;
613 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000614 isUnsigned = true; // FALLTHROUGH
615 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000616 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000617 TCC = SystemZCC::LE;
618 break;
619 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000620 if (LHS.getValueType().isFloatingPoint()) {
621 TCC = SystemZCC::NL;
622 break;
623 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000624 isUnsigned = true; // FALLTHROUGH
625 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000626 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000627 TCC = SystemZCC::HE;
628 break;
629 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000630 if (LHS.getValueType().isFloatingPoint()) {
631 TCC = SystemZCC::NLE;
632 break;
633 }
634 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000635 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000636 case ISD::SETOGT:
637 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000638 break;
639 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000640 if (LHS.getValueType().isFloatingPoint()) {
641 TCC = SystemZCC::NHE;
642 break;
643 }
644 isUnsigned = true; // FALLTHROUGH
645 case ISD::SETLT:
646 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000647 TCC = SystemZCC::L;
648 break;
649 }
650
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 SystemZCC = DAG.getConstant(TCC, MVT::i32);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000652
653 DebugLoc dl = LHS.getDebugLoc();
654 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 dl, MVT::Flag, LHS, RHS);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000656}
657
658
659SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
660 SDValue Chain = Op.getOperand(0);
661 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
662 SDValue LHS = Op.getOperand(2);
663 SDValue RHS = Op.getOperand(3);
664 SDValue Dest = Op.getOperand(4);
665 DebugLoc dl = Op.getDebugLoc();
666
667 SDValue SystemZCC;
668 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
669 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
670 Chain, Dest, SystemZCC, Flag);
671}
672
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000673SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
674 SDValue LHS = Op.getOperand(0);
675 SDValue RHS = Op.getOperand(1);
676 SDValue TrueV = Op.getOperand(2);
677 SDValue FalseV = Op.getOperand(3);
678 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
679 DebugLoc dl = Op.getDebugLoc();
680
681 SDValue SystemZCC;
682 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
683
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000685 SmallVector<SDValue, 4> Ops;
686 Ops.push_back(TrueV);
687 Ops.push_back(FalseV);
688 Ops.push_back(SystemZCC);
689 Ops.push_back(Flag);
690
691 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
692}
693
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000694SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
695 SelectionDAG &DAG) {
696 DebugLoc dl = Op.getDebugLoc();
697 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000698 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000699
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000700 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
701 bool ExtraLoadRequired =
702 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
703
704 SDValue Result;
705 if (!IsPic && !ExtraLoadRequired) {
706 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
707 Offset = 0;
708 } else {
709 unsigned char OpFlags = 0;
710 if (ExtraLoadRequired)
711 OpFlags = SystemZII::MO_GOTENT;
712
713 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
714 }
715
716 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
717 getPointerTy(), Result);
718
719 if (ExtraLoadRequired)
720 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
721 PseudoSourceValue::getGOT(), 0);
722
723 // If there was a non-zero offset that we didn't fold, create an explicit
724 // addition for it.
725 if (Offset != 0)
726 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
727 DAG.getConstant(Offset, getPointerTy()));
728
729 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000730}
731
Anton Korobeynikovae535672009-07-16 14:19:35 +0000732// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000733SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
734 SelectionDAG &DAG) {
735 DebugLoc dl = Op.getDebugLoc();
736 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
737 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
738
739 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
740}
741
Anton Korobeynikovae535672009-07-16 14:19:35 +0000742
743// FIXME: PIC here
744// FIXME: This is just dirty hack. We need to lower cpool properly
745SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
746 SelectionDAG &DAG) {
747 DebugLoc dl = Op.getDebugLoc();
748 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
749
750 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
751 CP->getAlignment(),
752 CP->getOffset());
753
754 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
755}
756
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000757const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
758 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000759 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000760 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000761 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
762 case SystemZISD::CMP: return "SystemZISD::CMP";
763 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000764 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000765 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000766 default: return NULL;
767 }
768}
769
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000770//===----------------------------------------------------------------------===//
771// Other Lowering Code
772//===----------------------------------------------------------------------===//
773
774MachineBasicBlock*
775SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
776 MachineBasicBlock *BB) const {
777 const SystemZInstrInfo &TII = *TM.getInstrInfo();
778 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000779 assert((MI->getOpcode() == SystemZ::Select32 ||
780 MI->getOpcode() == SystemZ::SelectF32 ||
781 MI->getOpcode() == SystemZ::Select64 ||
782 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000783 "Unexpected instr type to insert");
784
785 // To "insert" a SELECT instruction, we actually have to insert the diamond
786 // control-flow pattern. The incoming instruction knows the destination vreg
787 // to set, the condition code register to branch on, the true/false values to
788 // select between, and a branch opcode to use.
789 const BasicBlock *LLVM_BB = BB->getBasicBlock();
790 MachineFunction::iterator I = BB;
791 ++I;
792
793 // thisMBB:
794 // ...
795 // TrueVal = ...
796 // cmpTY ccX, r1, r2
797 // jCC copy1MBB
798 // fallthrough --> copy0MBB
799 MachineBasicBlock *thisMBB = BB;
800 MachineFunction *F = BB->getParent();
801 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
802 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
803 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
804 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
805 F->insert(I, copy0MBB);
806 F->insert(I, copy1MBB);
807 // Update machine-CFG edges by transferring all successors of the current
808 // block to the new block which will contain the Phi node for the select.
809 copy1MBB->transferSuccessors(BB);
810 // Next, add the true and fallthrough blocks as its successors.
811 BB->addSuccessor(copy0MBB);
812 BB->addSuccessor(copy1MBB);
813
814 // copy0MBB:
815 // %FalseValue = ...
816 // # fallthrough to copy1MBB
817 BB = copy0MBB;
818
819 // Update machine-CFG edges
820 BB->addSuccessor(copy1MBB);
821
822 // copy1MBB:
823 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
824 // ...
825 BB = copy1MBB;
826 BuildMI(BB, dl, TII.get(SystemZ::PHI),
827 MI->getOperand(0).getReg())
828 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
829 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
830
831 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
832 return BB;
833}