blob: 48ca99ff9ea27d9822c5a15188e5d85f8f64922a [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"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000033#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000034#include "llvm/CodeGen/ValueTypes.h"
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000036#include "llvm/Support/Debug.h"
Chris Lattner8f9b0f62009-11-07 09:20:54 +000037#include "llvm/Support/ErrorHandling.h"
Chris Lattner4437ae22009-08-23 07:05:07 +000038#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000039#include "llvm/ADT/VectorExtras.h"
40using namespace llvm;
41
42SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
Chris Lattnerf0144122009-07-28 03:13:23 +000043 TargetLowering(tm, new TargetLoweringObjectFileELF()),
44 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
Anton Korobeynikov4403b932009-07-16 13:27:25 +000045
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +000046 RegInfo = TM.getRegisterInfo();
47
Anton Korobeynikov4403b932009-07-16 13:27:25 +000048 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +000049 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
50 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
51 addRegisterClass(MVT::v2i32,SystemZ::GR64PRegisterClass);
52 addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000053
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000054 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +000055 addRegisterClass(MVT::f32, SystemZ::FP32RegisterClass);
56 addRegisterClass(MVT::f64, SystemZ::FP64RegisterClass);
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000057 }
58
Anton Korobeynikov4403b932009-07-16 13:27:25 +000059 // Compute derived properties from the register classes
60 computeRegisterProperties();
61
62 // Provide all sorts of operation actions
Owen Anderson825b72b2009-08-11 20:47:22 +000063 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
64 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
65 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000066
Owen Anderson825b72b2009-08-11 20:47:22 +000067 setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Expand);
68 setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Expand);
69 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Anton Korobeynikov299dc782009-07-16 14:22:30 +000070
Owen Anderson825b72b2009-08-11 20:47:22 +000071 setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Expand);
72 setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Expand);
73 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
Anton Korobeynikov23eff5c2009-07-16 14:20:08 +000074
Anton Korobeynikove0167c12009-07-16 13:35:30 +000075 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Dan Gohmanaa123222009-10-28 00:55:57 +000076
77 // TODO: It may be better to default to latency-oriented scheduling, however
78 // LLVM's current latency-oriented scheduler can't handle physreg definitions
79 // such as SystemZ has with PSW, so set this to the register-pressure
80 // scheduler, because it can.
Evan Cheng211ffa12010-05-19 20:19:50 +000081 setSchedulingPreference(Sched::RegPressure);
Dan Gohmanaa123222009-10-28 00:55:57 +000082
Anton Korobeynikov159ac632009-07-16 14:28:46 +000083 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000084 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000085
Owen Anderson825b72b2009-08-11 20:47:22 +000086 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
87 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
88 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
89 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
90 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
91 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
92 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
93 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
94 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
95 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
96 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000097
Owen Anderson825b72b2009-08-11 20:47:22 +000098 setOperationAction(ISD::SDIV, MVT::i32, Expand);
99 setOperationAction(ISD::UDIV, MVT::i32, Expand);
100 setOperationAction(ISD::SDIV, MVT::i64, Expand);
101 setOperationAction(ISD::UDIV, MVT::i64, Expand);
102 setOperationAction(ISD::SREM, MVT::i32, Expand);
103 setOperationAction(ISD::UREM, MVT::i32, Expand);
104 setOperationAction(ISD::SREM, MVT::i64, Expand);
105 setOperationAction(ISD::UREM, MVT::i64, Expand);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000106
Owen Anderson825b72b2009-08-11 20:47:22 +0000107 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Anton Korobeynikove37a37d2009-07-18 12:20:36 +0000108
Owen Anderson825b72b2009-08-11 20:47:22 +0000109 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
110 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
111 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
112 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
113 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
114 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
Anton Korobeynikov0cca0692009-07-18 12:26:13 +0000115
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000116 // FIXME: Can we lower these 2 efficiently?
Owen Anderson825b72b2009-08-11 20:47:22 +0000117 setOperationAction(ISD::SETCC, MVT::i32, Expand);
118 setOperationAction(ISD::SETCC, MVT::i64, Expand);
119 setOperationAction(ISD::SETCC, MVT::f32, Expand);
120 setOperationAction(ISD::SETCC, MVT::f64, Expand);
121 setOperationAction(ISD::SELECT, MVT::i32, Expand);
122 setOperationAction(ISD::SELECT, MVT::i64, Expand);
123 setOperationAction(ISD::SELECT, MVT::f32, Expand);
124 setOperationAction(ISD::SELECT, MVT::f64, Expand);
125 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
126 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
127 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
128 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000129
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 setOperationAction(ISD::MULHS, MVT::i64, Expand);
131 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000132
Anton Korobeynikovfc9489a2009-08-21 18:52:42 +0000133 // FIXME: Can we support these natively?
134 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
135 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
136 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
137 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
138
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000139 // Lower some FP stuff
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 setOperationAction(ISD::FSIN, MVT::f32, Expand);
141 setOperationAction(ISD::FSIN, MVT::f64, Expand);
142 setOperationAction(ISD::FCOS, MVT::f32, Expand);
143 setOperationAction(ISD::FCOS, MVT::f64, Expand);
144 setOperationAction(ISD::FREM, MVT::f32, Expand);
145 setOperationAction(ISD::FREM, MVT::f64, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000146 setOperationAction(ISD::FMA, MVT::f32, Expand);
147 setOperationAction(ISD::FMA, MVT::f64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000148
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000149 // We have only 64-bit bitconverts
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000150 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
151 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000152
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
154 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
155 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
156 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000157
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000159
160 setMinFunctionAlignment(1);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000161}
162
Dan Gohmand858e902010-04-17 15:26:15 +0000163SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
164 SelectionDAG &DAG) const {
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000165 switch (Op.getOpcode()) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000166 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000167 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000168 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000169 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000170 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000171 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000172 llvm_unreachable("Should not custom lower this!");
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000173 return SDValue();
174 }
175}
176
Evan Chenga1eaa3c2009-10-28 01:43:28 +0000177bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
178 if (UseSoftFloat || (VT != MVT::f32 && VT != MVT::f64))
Evan Chengeb2f9692009-10-27 19:56:55 +0000179 return false;
180
181 // +0.0 lzer
182 // +0.0f lzdr
183 // -0.0 lzer + lner
184 // -0.0f lzdr + lndr
185 return Imm.isZero() || Imm.isNegZero();
186}
187
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000188//===----------------------------------------------------------------------===//
Anton Korobeynikov3c2734c2009-08-21 18:15:41 +0000189// SystemZ Inline Assembly Support
190//===----------------------------------------------------------------------===//
191
192/// getConstraintType - Given a constraint letter, return the type of
193/// constraint it is for this target.
194TargetLowering::ConstraintType
195SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
196 if (Constraint.size() == 1) {
197 switch (Constraint[0]) {
198 case 'r':
199 return C_RegisterClass;
200 default:
201 break;
202 }
203 }
204 return TargetLowering::getConstraintType(Constraint);
205}
206
207std::pair<unsigned, const TargetRegisterClass*>
208SystemZTargetLowering::
209getRegForInlineAsmConstraint(const std::string &Constraint,
210 EVT VT) const {
211 if (Constraint.size() == 1) {
212 // GCC Constraint Letters
213 switch (Constraint[0]) {
214 default: break;
215 case 'r': // GENERAL_REGS
216 if (VT == MVT::i32)
217 return std::make_pair(0U, SystemZ::GR32RegisterClass);
218 else if (VT == MVT::i128)
219 return std::make_pair(0U, SystemZ::GR128RegisterClass);
220
221 return std::make_pair(0U, SystemZ::GR64RegisterClass);
222 }
223 }
224
225 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
226}
227
228//===----------------------------------------------------------------------===//
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000229// Calling Convention Implementation
230//===----------------------------------------------------------------------===//
231
232#include "SystemZGenCallingConv.inc"
233
Dan Gohman98ca4f22009-08-05 01:29:28 +0000234SDValue
235SystemZTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000236 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000237 bool isVarArg,
238 const SmallVectorImpl<ISD::InputArg>
239 &Ins,
240 DebugLoc dl,
241 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000242 SmallVectorImpl<SDValue> &InVals)
243 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000244
245 switch (CallConv) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000246 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000247 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000248 case CallingConv::C:
249 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000250 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000251 }
252}
253
Dan Gohman98ca4f22009-08-05 01:29:28 +0000254SDValue
255SystemZTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000256 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000257 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000258 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000259 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000260 const SmallVectorImpl<ISD::InputArg> &Ins,
261 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000262 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000263 // SystemZ target does not yet support tail call optimization.
264 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000265
266 switch (CallConv) {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000267 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000268 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000269 case CallingConv::Fast:
270 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000271 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanc9403652010-07-07 15:54:55 +0000272 Outs, OutVals, Ins, dl, DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000273 }
274}
275
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000276/// LowerCCCArguments - transform physical registers into virtual registers and
277/// generate load operations for arguments places on the stack.
278// FIXME: struct return stuff
279// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000280SDValue
281SystemZTargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000282 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000283 bool isVarArg,
284 const SmallVectorImpl<ISD::InputArg>
285 &Ins,
286 DebugLoc dl,
287 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000288 SmallVectorImpl<SDValue> &InVals)
289 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000290
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000291 MachineFunction &MF = DAG.getMachineFunction();
292 MachineFrameInfo *MFI = MF.getFrameInfo();
293 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000294
295 // Assign locations to all of the incoming arguments.
296 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000297 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
298 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000299 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000300
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000301 if (isVarArg)
Chris Lattner75361b62010-04-07 22:58:41 +0000302 report_fatal_error("Varargs not supported yet");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000303
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000304 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000305 SDValue ArgValue;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000306 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +0000307 EVT LocVT = VA.getLocVT();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000308 if (VA.isRegLoc()) {
309 // Arguments passed in registers
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000310 TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 switch (LocVT.getSimpleVT().SimpleTy) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000312 default:
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000313#ifndef NDEBUG
Chris Lattner4437ae22009-08-23 07:05:07 +0000314 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 << LocVT.getSimpleVT().SimpleTy
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000316 << "\n";
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000317#endif
318 llvm_unreachable(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 case MVT::i64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000320 RC = SystemZ::GR64RegisterClass;
321 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 case MVT::f32:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000323 RC = SystemZ::FP32RegisterClass;
324 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 case MVT::f64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000326 RC = SystemZ::FP64RegisterClass;
327 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000328 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000329
330 unsigned VReg = RegInfo.createVirtualRegister(RC);
331 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000332 ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, LocVT);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000333 } else {
334 // Sanity check
335 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000336
337 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000338 // Create the frame index object for this incoming parameter...
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000339 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +0000340 VA.getLocMemOffset(), true);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000341
342 // Create the SelectionDAG nodes corresponding to a load
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000343 // from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000344 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000345 ArgValue = DAG.getLoad(LocVT, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000346 MachinePointerInfo::getFixedStack(FI),
David Greene77201552010-02-15 16:57:13 +0000347 false, false, 0);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000348 }
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000349
350 // If this is an 8/16/32-bit value, it is really passed promoted to 64
351 // bits. Insert an assert[sz]ext to capture this, then truncate to the
352 // right size.
353 if (VA.getLocInfo() == CCValAssign::SExt)
354 ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
355 DAG.getValueType(VA.getValVT()));
356 else if (VA.getLocInfo() == CCValAssign::ZExt)
357 ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
358 DAG.getValueType(VA.getValVT()));
359
360 if (VA.getLocInfo() != CCValAssign::Full)
361 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
362
Dan Gohman98ca4f22009-08-05 01:29:28 +0000363 InVals.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000364 }
365
Dan Gohman98ca4f22009-08-05 01:29:28 +0000366 return Chain;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000367}
368
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000369/// LowerCCCCallTo - functions arguments are copied from virtual regs to
370/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
371/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000372SDValue
373SystemZTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000374 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000375 bool isTailCall,
376 const SmallVectorImpl<ISD::OutputArg>
377 &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000378 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000379 const SmallVectorImpl<ISD::InputArg> &Ins,
380 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000381 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000382 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000383 const TargetFrameLowering *TFI = TM.getFrameLowering();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000384
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000385 // Offset to first argument stack slot.
386 const unsigned FirstArgOffset = 160;
387
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000388 // Analyze operands of the call, assigning locations to each operand.
389 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000390 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
391 getTargetMachine(), ArgLocs, *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000392
Dan Gohman98ca4f22009-08-05 01:29:28 +0000393 CCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000394
395 // Get a count of how many bytes are to be pushed on the stack.
396 unsigned NumBytes = CCInfo.getNextStackOffset();
397
398 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
399 getPointerTy(), true));
400
401 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
402 SmallVector<SDValue, 12> MemOpChains;
403 SDValue StackPtr;
404
405 // Walk the register/memloc assignments, inserting copies/loads.
406 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
407 CCValAssign &VA = ArgLocs[i];
408
Dan Gohmanc9403652010-07-07 15:54:55 +0000409 SDValue Arg = OutVals[i];
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000410
411 // Promote the value if needed.
412 switch (VA.getLocInfo()) {
413 default: assert(0 && "Unknown loc info!");
414 case CCValAssign::Full: break;
415 case CCValAssign::SExt:
416 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
417 break;
418 case CCValAssign::ZExt:
419 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
420 break;
421 case CCValAssign::AExt:
422 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
423 break;
424 }
425
426 // Arguments that can be passed on register must be kept at RegsToPass
427 // vector
428 if (VA.isRegLoc()) {
429 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
430 } else {
431 assert(VA.isMemLoc());
432
433 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000434 StackPtr =
435 DAG.getCopyFromReg(Chain, dl,
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000436 (TFI->hasFP(MF) ?
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000437 SystemZ::R11D : SystemZ::R15D),
438 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000439
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000440 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
441 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
442 StackPtr,
443 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000444
445 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000446 MachinePointerInfo(),
David Greene77201552010-02-15 16:57:13 +0000447 false, false, 0));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000448 }
449 }
450
451 // Transform all store nodes into one single node because all store nodes are
452 // independent of each other.
453 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000455 &MemOpChains[0], MemOpChains.size());
456
457 // Build a sequence of copy-to-reg nodes chained together with token chain and
458 // flag operands which copy the outgoing args into registers. The InFlag in
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000459 // necessary since all emitted instructions must be stuck together.
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000460 SDValue InFlag;
461 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
462 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
463 RegsToPass[i].second, InFlag);
464 InFlag = Chain.getValue(1);
465 }
466
467 // If the callee is a GlobalAddress node (quite common, every direct call is)
468 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
469 // Likewise ExternalSymbol -> TargetExternalSymbol.
470 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000471 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000472 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
473 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
474
475 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000476 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000477 SmallVector<SDValue, 8> Ops;
478 Ops.push_back(Chain);
479 Ops.push_back(Callee);
480
481 // Add argument registers to the end of the list so that they are
482 // known live into the call.
483 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
484 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
485 RegsToPass[i].second.getValueType()));
486
487 if (InFlag.getNode())
488 Ops.push_back(InFlag);
489
490 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
491 InFlag = Chain.getValue(1);
492
493 // Create the CALLSEQ_END node.
494 Chain = DAG.getCALLSEQ_END(Chain,
495 DAG.getConstant(NumBytes, getPointerTy(), true),
496 DAG.getConstant(0, getPointerTy(), true),
497 InFlag);
498 InFlag = Chain.getValue(1);
499
500 // Handle result values, copying them out of physregs into vregs that we
501 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000502 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
503 DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000504}
505
Dan Gohman98ca4f22009-08-05 01:29:28 +0000506/// LowerCallResult - Lower the result values of a call into the
507/// appropriate copies out of appropriate physical registers.
508///
509SDValue
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000510SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000511 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000512 const SmallVectorImpl<ISD::InputArg>
513 &Ins,
514 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000515 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000516
517 // Assign locations to each value returned by this call.
518 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000519 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
520 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000521
Dan Gohman98ca4f22009-08-05 01:29:28 +0000522 CCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000523
524 // Copy all of the result registers out of their specified physreg.
525 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000526 CCValAssign &VA = RVLocs[i];
527
528 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
529 VA.getLocVT(), InFlag).getValue(1);
530 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000531 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000532
533 // If this is an 8/16/32-bit value, it is really passed promoted to 64
534 // bits. Insert an assert[sz]ext to capture this, then truncate to the
535 // right size.
536 if (VA.getLocInfo() == CCValAssign::SExt)
537 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
538 DAG.getValueType(VA.getValVT()));
539 else if (VA.getLocInfo() == CCValAssign::ZExt)
540 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
541 DAG.getValueType(VA.getValVT()));
542
543 if (VA.getLocInfo() != CCValAssign::Full)
544 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
545
Dan Gohman98ca4f22009-08-05 01:29:28 +0000546 InVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000547 }
548
Dan Gohman98ca4f22009-08-05 01:29:28 +0000549 return Chain;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000550}
551
552
Dan Gohman98ca4f22009-08-05 01:29:28 +0000553SDValue
554SystemZTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000555 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000556 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000557 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +0000558 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000559
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000560 // CCValAssign - represent the assignment of the return value to a location
561 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000562
563 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +0000564 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
565 getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000566
Dan Gohman98ca4f22009-08-05 01:29:28 +0000567 // Analize return values.
568 CCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000569
570 // If this is the first return lowered for this function, add the regs to the
571 // liveout set for the function.
572 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
573 for (unsigned i = 0; i != RVLocs.size(); ++i)
574 if (RVLocs[i].isRegLoc())
575 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
576 }
577
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000578 SDValue Flag;
579
580 // Copy the result values into the output registers.
581 for (unsigned i = 0; i != RVLocs.size(); ++i) {
582 CCValAssign &VA = RVLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +0000583 SDValue ResValue = OutVals[i];
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000584 assert(VA.isRegLoc() && "Can only return in registers!");
585
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000586 // If this is an 8/16/32-bit value, it is really should be passed promoted
587 // to 64 bits.
588 if (VA.getLocInfo() == CCValAssign::SExt)
589 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
590 else if (VA.getLocInfo() == CCValAssign::ZExt)
591 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
592 else if (VA.getLocInfo() == CCValAssign::AExt)
593 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
594
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000595 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000596
597 // Guarantee that all emitted copies are stuck together,
598 // avoiding something bad.
599 Flag = Chain.getValue(1);
600 }
601
602 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000604
605 // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000607}
608
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000609SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
610 ISD::CondCode CC, SDValue &SystemZCC,
Dan Gohmand858e902010-04-17 15:26:15 +0000611 SelectionDAG &DAG) const {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000612 // FIXME: Emit a test if RHS is zero
613
614 bool isUnsigned = false;
615 SystemZCC::CondCodes TCC;
616 switch (CC) {
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000617 default:
618 llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000619 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000620 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000621 TCC = SystemZCC::E;
622 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000623 case ISD::SETUEQ:
624 TCC = SystemZCC::NLH;
625 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000626 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000627 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000628 TCC = SystemZCC::NE;
629 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000630 case ISD::SETUNE:
631 TCC = SystemZCC::LH;
632 break;
633 case ISD::SETO:
634 TCC = SystemZCC::O;
635 break;
636 case ISD::SETUO:
637 TCC = SystemZCC::NO;
638 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000639 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000640 if (LHS.getValueType().isFloatingPoint()) {
641 TCC = SystemZCC::NH;
642 break;
643 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000644 isUnsigned = true; // FALLTHROUGH
645 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000646 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000647 TCC = SystemZCC::LE;
648 break;
649 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000650 if (LHS.getValueType().isFloatingPoint()) {
651 TCC = SystemZCC::NL;
652 break;
653 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000654 isUnsigned = true; // FALLTHROUGH
655 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000656 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000657 TCC = SystemZCC::HE;
658 break;
659 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000660 if (LHS.getValueType().isFloatingPoint()) {
661 TCC = SystemZCC::NLE;
662 break;
663 }
664 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000665 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000666 case ISD::SETOGT:
667 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000668 break;
669 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000670 if (LHS.getValueType().isFloatingPoint()) {
671 TCC = SystemZCC::NHE;
672 break;
673 }
674 isUnsigned = true; // FALLTHROUGH
675 case ISD::SETLT:
676 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000677 TCC = SystemZCC::L;
678 break;
679 }
680
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 SystemZCC = DAG.getConstant(TCC, MVT::i32);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000682
683 DebugLoc dl = LHS.getDebugLoc();
684 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
Dan Gohmanaa123222009-10-28 00:55:57 +0000685 dl, MVT::i64, LHS, RHS);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000686}
687
688
Dan Gohmand858e902010-04-17 15:26:15 +0000689SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000690 SDValue Chain = Op.getOperand(0);
691 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
692 SDValue LHS = Op.getOperand(2);
693 SDValue RHS = Op.getOperand(3);
694 SDValue Dest = Op.getOperand(4);
695 DebugLoc dl = Op.getDebugLoc();
696
697 SDValue SystemZCC;
698 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
699 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
700 Chain, Dest, SystemZCC, Flag);
701}
702
Dan Gohmand858e902010-04-17 15:26:15 +0000703SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op,
704 SelectionDAG &DAG) const {
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000705 SDValue LHS = Op.getOperand(0);
706 SDValue RHS = Op.getOperand(1);
707 SDValue TrueV = Op.getOperand(2);
708 SDValue FalseV = Op.getOperand(3);
709 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
710 DebugLoc dl = Op.getDebugLoc();
711
712 SDValue SystemZCC;
713 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
714
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000715 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000716 SmallVector<SDValue, 4> Ops;
717 Ops.push_back(TrueV);
718 Ops.push_back(FalseV);
719 Ops.push_back(SystemZCC);
720 Ops.push_back(Flag);
721
722 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
723}
724
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000725SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000726 SelectionDAG &DAG) const {
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000727 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000728 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000729 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000730
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000731 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
732 bool ExtraLoadRequired =
733 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
734
735 SDValue Result;
736 if (!IsPic && !ExtraLoadRequired) {
Devang Patel0d881da2010-07-06 22:08:15 +0000737 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000738 Offset = 0;
739 } else {
740 unsigned char OpFlags = 0;
741 if (ExtraLoadRequired)
742 OpFlags = SystemZII::MO_GOTENT;
743
Devang Patel0d881da2010-07-06 22:08:15 +0000744 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000745 }
746
747 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
748 getPointerTy(), Result);
749
750 if (ExtraLoadRequired)
751 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000752 MachinePointerInfo::getGOT(), false, false, 0);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000753
754 // If there was a non-zero offset that we didn't fold, create an explicit
755 // addition for it.
756 if (Offset != 0)
757 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
758 DAG.getConstant(Offset, getPointerTy()));
759
760 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000761}
762
Anton Korobeynikovae535672009-07-16 14:19:35 +0000763// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000764SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000765 SelectionDAG &DAG) const {
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000766 DebugLoc dl = Op.getDebugLoc();
767 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
768 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
769
770 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
771}
772
Anton Korobeynikovae535672009-07-16 14:19:35 +0000773
774// FIXME: PIC here
775// FIXME: This is just dirty hack. We need to lower cpool properly
776SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000777 SelectionDAG &DAG) const {
Anton Korobeynikovae535672009-07-16 14:19:35 +0000778 DebugLoc dl = Op.getDebugLoc();
779 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
780
781 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
782 CP->getAlignment(),
783 CP->getOffset());
784
785 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
786}
787
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000788const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
789 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000790 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000791 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000792 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
793 case SystemZISD::CMP: return "SystemZISD::CMP";
794 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000795 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000796 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000797 default: return NULL;
798 }
799}
800
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000801//===----------------------------------------------------------------------===//
802// Other Lowering Code
803//===----------------------------------------------------------------------===//
804
805MachineBasicBlock*
806SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000807 MachineBasicBlock *BB) const {
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000808 const SystemZInstrInfo &TII = *TM.getInstrInfo();
809 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000810 assert((MI->getOpcode() == SystemZ::Select32 ||
811 MI->getOpcode() == SystemZ::SelectF32 ||
812 MI->getOpcode() == SystemZ::Select64 ||
813 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000814 "Unexpected instr type to insert");
815
816 // To "insert" a SELECT instruction, we actually have to insert the diamond
817 // control-flow pattern. The incoming instruction knows the destination vreg
818 // to set, the condition code register to branch on, the true/false values to
819 // select between, and a branch opcode to use.
820 const BasicBlock *LLVM_BB = BB->getBasicBlock();
821 MachineFunction::iterator I = BB;
822 ++I;
823
824 // thisMBB:
825 // ...
826 // TrueVal = ...
827 // cmpTY ccX, r1, r2
828 // jCC copy1MBB
829 // fallthrough --> copy0MBB
830 MachineBasicBlock *thisMBB = BB;
831 MachineFunction *F = BB->getParent();
832 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
833 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
834 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000835 F->insert(I, copy0MBB);
836 F->insert(I, copy1MBB);
837 // Update machine-CFG edges by transferring all successors of the current
838 // block to the new block which will contain the Phi node for the select.
Dan Gohman14152b42010-07-06 20:24:04 +0000839 copy1MBB->splice(copy1MBB->begin(), BB,
840 llvm::next(MachineBasicBlock::iterator(MI)),
841 BB->end());
842 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000843 // Next, add the true and fallthrough blocks as its successors.
844 BB->addSuccessor(copy0MBB);
845 BB->addSuccessor(copy1MBB);
846
Dan Gohman14152b42010-07-06 20:24:04 +0000847 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
848
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000849 // copy0MBB:
850 // %FalseValue = ...
851 // # fallthrough to copy1MBB
852 BB = copy0MBB;
853
854 // Update machine-CFG edges
855 BB->addSuccessor(copy1MBB);
856
857 // copy1MBB:
858 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
859 // ...
860 BB = copy1MBB;
Dan Gohman14152b42010-07-06 20:24:04 +0000861 BuildMI(*BB, BB->begin(), dl, TII.get(SystemZ::PHI),
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000862 MI->getOperand(0).getReg())
863 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
864 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
865
Dan Gohman14152b42010-07-06 20:24:04 +0000866 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000867 return BB;
868}