blob: eb4d250504d9bed01159c1e5fad6e382b7c17d82 [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"
Chris Lattner4437ae22009-08-23 07:05:07 +000037#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000038#include "llvm/ADT/VectorExtras.h"
39using namespace llvm;
40
41SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
Chris Lattnerf0144122009-07-28 03:13:23 +000042 TargetLowering(tm, new TargetLoweringObjectFileELF()),
43 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
Anton Korobeynikov4403b932009-07-16 13:27:25 +000044
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +000045 RegInfo = TM.getRegisterInfo();
46
Anton Korobeynikov4403b932009-07-16 13:27:25 +000047 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +000048 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
49 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
50 addRegisterClass(MVT::v2i32,SystemZ::GR64PRegisterClass);
51 addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000052
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000053 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +000054 addRegisterClass(MVT::f32, SystemZ::FP32RegisterClass);
55 addRegisterClass(MVT::f64, SystemZ::FP64RegisterClass);
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000056 }
57
Anton Korobeynikov4403b932009-07-16 13:27:25 +000058 // Compute derived properties from the register classes
59 computeRegisterProperties();
60
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000061 // Set shifts properties
Owen Anderson825b72b2009-08-11 20:47:22 +000062 setShiftAmountType(MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000063
Anton Korobeynikov4403b932009-07-16 13:27:25 +000064 // Provide all sorts of operation actions
Owen Anderson825b72b2009-08-11 20:47:22 +000065 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
66 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
67 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000068
Owen Anderson825b72b2009-08-11 20:47:22 +000069 setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Expand);
70 setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Expand);
71 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Anton Korobeynikov299dc782009-07-16 14:22:30 +000072
Owen Anderson825b72b2009-08-11 20:47:22 +000073 setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Expand);
74 setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Expand);
75 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
Anton Korobeynikov23eff5c2009-07-16 14:20:08 +000076
Anton Korobeynikove0167c12009-07-16 13:35:30 +000077 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000078 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov159ac632009-07-16 14:28:46 +000079 setBooleanContents(ZeroOrOneBooleanContent);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000080
Owen Anderson825b72b2009-08-11 20:47:22 +000081 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
82 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
83 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
84 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
85 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
86 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
87 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
88 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
89 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
90 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
91 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000092
Owen Anderson825b72b2009-08-11 20:47:22 +000093 setOperationAction(ISD::SDIV, MVT::i32, Expand);
94 setOperationAction(ISD::UDIV, MVT::i32, Expand);
95 setOperationAction(ISD::SDIV, MVT::i64, Expand);
96 setOperationAction(ISD::UDIV, MVT::i64, Expand);
97 setOperationAction(ISD::SREM, MVT::i32, Expand);
98 setOperationAction(ISD::UREM, MVT::i32, Expand);
99 setOperationAction(ISD::SREM, MVT::i64, Expand);
100 setOperationAction(ISD::UREM, MVT::i64, Expand);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000101
Owen Anderson825b72b2009-08-11 20:47:22 +0000102 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Anton Korobeynikove37a37d2009-07-18 12:20:36 +0000103
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
105 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
106 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
107 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
108 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
109 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
Anton Korobeynikov0cca0692009-07-18 12:26:13 +0000110
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000111 // FIXME: Can we lower these 2 efficiently?
Owen Anderson825b72b2009-08-11 20:47:22 +0000112 setOperationAction(ISD::SETCC, MVT::i32, Expand);
113 setOperationAction(ISD::SETCC, MVT::i64, Expand);
114 setOperationAction(ISD::SETCC, MVT::f32, Expand);
115 setOperationAction(ISD::SETCC, MVT::f64, Expand);
116 setOperationAction(ISD::SELECT, MVT::i32, Expand);
117 setOperationAction(ISD::SELECT, MVT::i64, Expand);
118 setOperationAction(ISD::SELECT, MVT::f32, Expand);
119 setOperationAction(ISD::SELECT, MVT::f64, Expand);
120 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
121 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
122 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
123 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000124
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setOperationAction(ISD::MULHS, MVT::i64, Expand);
126 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000127
Anton Korobeynikovfc9489a2009-08-21 18:52:42 +0000128 // FIXME: Can we support these natively?
129 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
130 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
131 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
132 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
133
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
Evan Chengeb2f9692009-10-27 19:56:55 +0000167bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm) const {
168 if (UseSoftFloat)
169 return false;
170
171 // +0.0 lzer
172 // +0.0f lzdr
173 // -0.0 lzer + lner
174 // -0.0f lzdr + lndr
175 return Imm.isZero() || Imm.isNegZero();
176}
177
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000178//===----------------------------------------------------------------------===//
Anton Korobeynikov3c2734c2009-08-21 18:15:41 +0000179// SystemZ Inline Assembly Support
180//===----------------------------------------------------------------------===//
181
182/// getConstraintType - Given a constraint letter, return the type of
183/// constraint it is for this target.
184TargetLowering::ConstraintType
185SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
186 if (Constraint.size() == 1) {
187 switch (Constraint[0]) {
188 case 'r':
189 return C_RegisterClass;
190 default:
191 break;
192 }
193 }
194 return TargetLowering::getConstraintType(Constraint);
195}
196
197std::pair<unsigned, const TargetRegisterClass*>
198SystemZTargetLowering::
199getRegForInlineAsmConstraint(const std::string &Constraint,
200 EVT VT) const {
201 if (Constraint.size() == 1) {
202 // GCC Constraint Letters
203 switch (Constraint[0]) {
204 default: break;
205 case 'r': // GENERAL_REGS
206 if (VT == MVT::i32)
207 return std::make_pair(0U, SystemZ::GR32RegisterClass);
208 else if (VT == MVT::i128)
209 return std::make_pair(0U, SystemZ::GR128RegisterClass);
210
211 return std::make_pair(0U, SystemZ::GR64RegisterClass);
212 }
213 }
214
215 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
216}
217
218//===----------------------------------------------------------------------===//
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000219// Calling Convention Implementation
220//===----------------------------------------------------------------------===//
221
222#include "SystemZGenCallingConv.inc"
223
Dan Gohman98ca4f22009-08-05 01:29:28 +0000224SDValue
225SystemZTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000226 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000227 bool isVarArg,
228 const SmallVectorImpl<ISD::InputArg>
229 &Ins,
230 DebugLoc dl,
231 SelectionDAG &DAG,
232 SmallVectorImpl<SDValue> &InVals) {
233
234 switch (CallConv) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000235 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000236 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000237 case CallingConv::C:
238 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000239 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000240 }
241}
242
Dan Gohman98ca4f22009-08-05 01:29:28 +0000243SDValue
244SystemZTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000245 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000246 bool isTailCall,
247 const SmallVectorImpl<ISD::OutputArg> &Outs,
248 const SmallVectorImpl<ISD::InputArg> &Ins,
249 DebugLoc dl, SelectionDAG &DAG,
250 SmallVectorImpl<SDValue> &InVals) {
251
252 switch (CallConv) {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000253 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000254 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000255 case CallingConv::Fast:
256 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000257 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
258 Outs, Ins, dl, DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000259 }
260}
261
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000262/// LowerCCCArguments - transform physical registers into virtual registers and
263/// generate load operations for arguments places on the stack.
264// FIXME: struct return stuff
265// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000266SDValue
267SystemZTargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000268 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000269 bool isVarArg,
270 const SmallVectorImpl<ISD::InputArg>
271 &Ins,
272 DebugLoc dl,
273 SelectionDAG &DAG,
274 SmallVectorImpl<SDValue> &InVals) {
275
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000276 MachineFunction &MF = DAG.getMachineFunction();
277 MachineFrameInfo *MFI = MF.getFrameInfo();
278 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000279
280 // Assign locations to all of the incoming arguments.
281 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000282 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
283 ArgLocs, *DAG.getContext());
284 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000285
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000286 if (isVarArg)
287 llvm_report_error("Varargs not supported yet");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000288
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000289 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000290 SDValue ArgValue;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000291 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +0000292 EVT LocVT = VA.getLocVT();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000293 if (VA.isRegLoc()) {
294 // Arguments passed in registers
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000295 TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 switch (LocVT.getSimpleVT().SimpleTy) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000297 default:
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000298#ifndef NDEBUG
Chris Lattner4437ae22009-08-23 07:05:07 +0000299 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 << LocVT.getSimpleVT().SimpleTy
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000301 << "\n";
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000302#endif
303 llvm_unreachable(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 case MVT::i64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000305 RC = SystemZ::GR64RegisterClass;
306 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 case MVT::f32:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000308 RC = SystemZ::FP32RegisterClass;
309 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 case MVT::f64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000311 RC = SystemZ::FP64RegisterClass;
312 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000313 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000314
315 unsigned VReg = RegInfo.createVirtualRegister(RC);
316 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000317 ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, LocVT);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000318 } else {
319 // Sanity check
320 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000321
322 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000323 // Create the frame index object for this incoming parameter...
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000324 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000325 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000326
327 // Create the SelectionDAG nodes corresponding to a load
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000328 // from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000329 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000330 ArgValue = DAG.getLoad(LocVT, dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +0000331 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000332 }
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000333
334 // If this is an 8/16/32-bit value, it is really passed promoted to 64
335 // bits. Insert an assert[sz]ext to capture this, then truncate to the
336 // right size.
337 if (VA.getLocInfo() == CCValAssign::SExt)
338 ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
339 DAG.getValueType(VA.getValVT()));
340 else if (VA.getLocInfo() == CCValAssign::ZExt)
341 ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
342 DAG.getValueType(VA.getValVT()));
343
344 if (VA.getLocInfo() != CCValAssign::Full)
345 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
346
Dan Gohman98ca4f22009-08-05 01:29:28 +0000347 InVals.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000348 }
349
Dan Gohman98ca4f22009-08-05 01:29:28 +0000350 return Chain;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000351}
352
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000353/// LowerCCCCallTo - functions arguments are copied from virtual regs to
354/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
355/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000356SDValue
357SystemZTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000358 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000359 bool isTailCall,
360 const SmallVectorImpl<ISD::OutputArg>
361 &Outs,
362 const SmallVectorImpl<ISD::InputArg> &Ins,
363 DebugLoc dl, SelectionDAG &DAG,
364 SmallVectorImpl<SDValue> &InVals) {
365
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000366 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000367
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000368 // Offset to first argument stack slot.
369 const unsigned FirstArgOffset = 160;
370
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000371 // Analyze operands of the call, assigning locations to each operand.
372 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000373 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
374 ArgLocs, *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000375
Dan Gohman98ca4f22009-08-05 01:29:28 +0000376 CCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000377
378 // Get a count of how many bytes are to be pushed on the stack.
379 unsigned NumBytes = CCInfo.getNextStackOffset();
380
381 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
382 getPointerTy(), true));
383
384 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
385 SmallVector<SDValue, 12> MemOpChains;
386 SDValue StackPtr;
387
388 // Walk the register/memloc assignments, inserting copies/loads.
389 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
390 CCValAssign &VA = ArgLocs[i];
391
Dan Gohman98ca4f22009-08-05 01:29:28 +0000392 SDValue Arg = Outs[i].Val;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000393
394 // Promote the value if needed.
395 switch (VA.getLocInfo()) {
396 default: assert(0 && "Unknown loc info!");
397 case CCValAssign::Full: break;
398 case CCValAssign::SExt:
399 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
400 break;
401 case CCValAssign::ZExt:
402 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
403 break;
404 case CCValAssign::AExt:
405 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
406 break;
407 }
408
409 // Arguments that can be passed on register must be kept at RegsToPass
410 // vector
411 if (VA.isRegLoc()) {
412 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
413 } else {
414 assert(VA.isMemLoc());
415
416 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000417 StackPtr =
418 DAG.getCopyFromReg(Chain, dl,
419 (RegInfo->hasFP(MF) ?
420 SystemZ::R11D : SystemZ::R15D),
421 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000422
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000423 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
424 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
425 StackPtr,
426 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000427
428 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000429 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000430 }
431 }
432
433 // Transform all store nodes into one single node because all store nodes are
434 // independent of each other.
435 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000437 &MemOpChains[0], MemOpChains.size());
438
439 // Build a sequence of copy-to-reg nodes chained together with token chain and
440 // flag operands which copy the outgoing args into registers. The InFlag in
441 // necessary since all emited instructions must be stuck together.
442 SDValue InFlag;
443 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
444 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
445 RegsToPass[i].second, InFlag);
446 InFlag = Chain.getValue(1);
447 }
448
449 // If the callee is a GlobalAddress node (quite common, every direct call is)
450 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
451 // Likewise ExternalSymbol -> TargetExternalSymbol.
452 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
453 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
454 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
455 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
456
457 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000459 SmallVector<SDValue, 8> Ops;
460 Ops.push_back(Chain);
461 Ops.push_back(Callee);
462
463 // Add argument registers to the end of the list so that they are
464 // known live into the call.
465 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
466 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
467 RegsToPass[i].second.getValueType()));
468
469 if (InFlag.getNode())
470 Ops.push_back(InFlag);
471
472 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
473 InFlag = Chain.getValue(1);
474
475 // Create the CALLSEQ_END node.
476 Chain = DAG.getCALLSEQ_END(Chain,
477 DAG.getConstant(NumBytes, getPointerTy(), true),
478 DAG.getConstant(0, getPointerTy(), true),
479 InFlag);
480 InFlag = Chain.getValue(1);
481
482 // Handle result values, copying them out of physregs into vregs that we
483 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000484 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
485 DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000486}
487
Dan Gohman98ca4f22009-08-05 01:29:28 +0000488/// LowerCallResult - Lower the result values of a call into the
489/// appropriate copies out of appropriate physical registers.
490///
491SDValue
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000492SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000493 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000494 const SmallVectorImpl<ISD::InputArg>
495 &Ins,
496 DebugLoc dl, SelectionDAG &DAG,
497 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000498
499 // Assign locations to each value returned by this call.
500 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000501 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
Owen Andersone922c022009-07-22 00:24:57 +0000502 *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000503
Dan Gohman98ca4f22009-08-05 01:29:28 +0000504 CCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000505
506 // Copy all of the result registers out of their specified physreg.
507 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000508 CCValAssign &VA = RVLocs[i];
509
510 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
511 VA.getLocVT(), InFlag).getValue(1);
512 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000513 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000514
515 // If this is an 8/16/32-bit value, it is really passed promoted to 64
516 // bits. Insert an assert[sz]ext to capture this, then truncate to the
517 // right size.
518 if (VA.getLocInfo() == CCValAssign::SExt)
519 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
520 DAG.getValueType(VA.getValVT()));
521 else if (VA.getLocInfo() == CCValAssign::ZExt)
522 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
523 DAG.getValueType(VA.getValVT()));
524
525 if (VA.getLocInfo() != CCValAssign::Full)
526 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
527
Dan Gohman98ca4f22009-08-05 01:29:28 +0000528 InVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000529 }
530
Dan Gohman98ca4f22009-08-05 01:29:28 +0000531 return Chain;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000532}
533
534
Dan Gohman98ca4f22009-08-05 01:29:28 +0000535SDValue
536SystemZTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000537 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000538 const SmallVectorImpl<ISD::OutputArg> &Outs,
539 DebugLoc dl, SelectionDAG &DAG) {
540
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000541 // CCValAssign - represent the assignment of the return value to a location
542 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000543
544 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000545 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
546 RVLocs, *DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000547
Dan Gohman98ca4f22009-08-05 01:29:28 +0000548 // Analize return values.
549 CCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000550
551 // If this is the first return lowered for this function, add the regs to the
552 // liveout set for the function.
553 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
554 for (unsigned i = 0; i != RVLocs.size(); ++i)
555 if (RVLocs[i].isRegLoc())
556 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
557 }
558
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000559 SDValue Flag;
560
561 // Copy the result values into the output registers.
562 for (unsigned i = 0; i != RVLocs.size(); ++i) {
563 CCValAssign &VA = RVLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000564 SDValue ResValue = Outs[i].Val;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000565 assert(VA.isRegLoc() && "Can only return in registers!");
566
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000567 // If this is an 8/16/32-bit value, it is really should be passed promoted
568 // to 64 bits.
569 if (VA.getLocInfo() == CCValAssign::SExt)
570 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
571 else if (VA.getLocInfo() == CCValAssign::ZExt)
572 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
573 else if (VA.getLocInfo() == CCValAssign::AExt)
574 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
575
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000576 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000577
578 // Guarantee that all emitted copies are stuck together,
579 // avoiding something bad.
580 Flag = Chain.getValue(1);
581 }
582
583 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000585
586 // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000588}
589
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000590SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
591 ISD::CondCode CC, SDValue &SystemZCC,
592 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000593 // FIXME: Emit a test if RHS is zero
594
595 bool isUnsigned = false;
596 SystemZCC::CondCodes TCC;
597 switch (CC) {
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000598 default:
599 llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000600 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000601 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000602 TCC = SystemZCC::E;
603 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000604 case ISD::SETUEQ:
605 TCC = SystemZCC::NLH;
606 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000607 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000608 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000609 TCC = SystemZCC::NE;
610 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000611 case ISD::SETUNE:
612 TCC = SystemZCC::LH;
613 break;
614 case ISD::SETO:
615 TCC = SystemZCC::O;
616 break;
617 case ISD::SETUO:
618 TCC = SystemZCC::NO;
619 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000620 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000621 if (LHS.getValueType().isFloatingPoint()) {
622 TCC = SystemZCC::NH;
623 break;
624 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000625 isUnsigned = true; // FALLTHROUGH
626 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000627 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000628 TCC = SystemZCC::LE;
629 break;
630 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000631 if (LHS.getValueType().isFloatingPoint()) {
632 TCC = SystemZCC::NL;
633 break;
634 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000635 isUnsigned = true; // FALLTHROUGH
636 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000637 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000638 TCC = SystemZCC::HE;
639 break;
640 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000641 if (LHS.getValueType().isFloatingPoint()) {
642 TCC = SystemZCC::NLE;
643 break;
644 }
645 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000646 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000647 case ISD::SETOGT:
648 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000649 break;
650 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000651 if (LHS.getValueType().isFloatingPoint()) {
652 TCC = SystemZCC::NHE;
653 break;
654 }
655 isUnsigned = true; // FALLTHROUGH
656 case ISD::SETLT:
657 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000658 TCC = SystemZCC::L;
659 break;
660 }
661
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 SystemZCC = DAG.getConstant(TCC, MVT::i32);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000663
664 DebugLoc dl = LHS.getDebugLoc();
665 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 dl, MVT::Flag, LHS, RHS);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000667}
668
669
670SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
671 SDValue Chain = Op.getOperand(0);
672 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
673 SDValue LHS = Op.getOperand(2);
674 SDValue RHS = Op.getOperand(3);
675 SDValue Dest = Op.getOperand(4);
676 DebugLoc dl = Op.getDebugLoc();
677
678 SDValue SystemZCC;
679 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
680 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
681 Chain, Dest, SystemZCC, Flag);
682}
683
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000684SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
685 SDValue LHS = Op.getOperand(0);
686 SDValue RHS = Op.getOperand(1);
687 SDValue TrueV = Op.getOperand(2);
688 SDValue FalseV = Op.getOperand(3);
689 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
690 DebugLoc dl = Op.getDebugLoc();
691
692 SDValue SystemZCC;
693 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
694
Owen Anderson825b72b2009-08-11 20:47:22 +0000695 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000696 SmallVector<SDValue, 4> Ops;
697 Ops.push_back(TrueV);
698 Ops.push_back(FalseV);
699 Ops.push_back(SystemZCC);
700 Ops.push_back(Flag);
701
702 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
703}
704
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000705SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
706 SelectionDAG &DAG) {
707 DebugLoc dl = Op.getDebugLoc();
708 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000709 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000710
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000711 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
712 bool ExtraLoadRequired =
713 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
714
715 SDValue Result;
716 if (!IsPic && !ExtraLoadRequired) {
717 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
718 Offset = 0;
719 } else {
720 unsigned char OpFlags = 0;
721 if (ExtraLoadRequired)
722 OpFlags = SystemZII::MO_GOTENT;
723
724 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
725 }
726
727 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
728 getPointerTy(), Result);
729
730 if (ExtraLoadRequired)
731 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
732 PseudoSourceValue::getGOT(), 0);
733
734 // If there was a non-zero offset that we didn't fold, create an explicit
735 // addition for it.
736 if (Offset != 0)
737 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
738 DAG.getConstant(Offset, getPointerTy()));
739
740 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000741}
742
Anton Korobeynikovae535672009-07-16 14:19:35 +0000743// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000744SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
745 SelectionDAG &DAG) {
746 DebugLoc dl = Op.getDebugLoc();
747 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
748 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
749
750 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
751}
752
Anton Korobeynikovae535672009-07-16 14:19:35 +0000753
754// FIXME: PIC here
755// FIXME: This is just dirty hack. We need to lower cpool properly
756SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
757 SelectionDAG &DAG) {
758 DebugLoc dl = Op.getDebugLoc();
759 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
760
761 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
762 CP->getAlignment(),
763 CP->getOffset());
764
765 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
766}
767
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000768const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
769 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000770 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000771 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000772 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
773 case SystemZISD::CMP: return "SystemZISD::CMP";
774 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000775 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000776 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000777 default: return NULL;
778 }
779}
780
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000781//===----------------------------------------------------------------------===//
782// Other Lowering Code
783//===----------------------------------------------------------------------===//
784
785MachineBasicBlock*
786SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +0000787 MachineBasicBlock *BB,
788 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000789 const SystemZInstrInfo &TII = *TM.getInstrInfo();
790 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000791 assert((MI->getOpcode() == SystemZ::Select32 ||
792 MI->getOpcode() == SystemZ::SelectF32 ||
793 MI->getOpcode() == SystemZ::Select64 ||
794 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000795 "Unexpected instr type to insert");
796
797 // To "insert" a SELECT instruction, we actually have to insert the diamond
798 // control-flow pattern. The incoming instruction knows the destination vreg
799 // to set, the condition code register to branch on, the true/false values to
800 // select between, and a branch opcode to use.
801 const BasicBlock *LLVM_BB = BB->getBasicBlock();
802 MachineFunction::iterator I = BB;
803 ++I;
804
805 // thisMBB:
806 // ...
807 // TrueVal = ...
808 // cmpTY ccX, r1, r2
809 // jCC copy1MBB
810 // fallthrough --> copy0MBB
811 MachineBasicBlock *thisMBB = BB;
812 MachineFunction *F = BB->getParent();
813 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
814 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
815 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
816 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
817 F->insert(I, copy0MBB);
818 F->insert(I, copy1MBB);
Evan Chengce319102009-09-19 09:51:03 +0000819 // Inform sdisel of the edge changes.
820 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
821 SE = BB->succ_end(); SI != SE; ++SI)
822 EM->insert(std::make_pair(*SI, copy1MBB));
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000823 // Update machine-CFG edges by transferring all successors of the current
824 // block to the new block which will contain the Phi node for the select.
825 copy1MBB->transferSuccessors(BB);
826 // Next, add the true and fallthrough blocks as its successors.
827 BB->addSuccessor(copy0MBB);
828 BB->addSuccessor(copy1MBB);
829
830 // copy0MBB:
831 // %FalseValue = ...
832 // # fallthrough to copy1MBB
833 BB = copy0MBB;
834
835 // Update machine-CFG edges
836 BB->addSuccessor(copy1MBB);
837
838 // copy1MBB:
839 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
840 // ...
841 BB = copy1MBB;
842 BuildMI(BB, dl, TII.get(SystemZ::PHI),
843 MI->getOperand(0).getReg())
844 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
845 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
846
847 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
848 return BB;
849}