blob: 15ef873dfcbfdec87d0660e5987c2d47ecde3f2a [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);
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
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 setOperationAction(ISD::MULHS, MVT::i64, Expand);
130 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000131
Anton Korobeynikovfc9489a2009-08-21 18:52:42 +0000132 // FIXME: Can we support these natively?
133 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
134 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
135 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
136 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
137
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000138 // Lower some FP stuff
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::FSIN, MVT::f32, Expand);
140 setOperationAction(ISD::FSIN, MVT::f64, Expand);
141 setOperationAction(ISD::FCOS, MVT::f32, Expand);
142 setOperationAction(ISD::FCOS, MVT::f64, Expand);
143 setOperationAction(ISD::FREM, MVT::f32, Expand);
144 setOperationAction(ISD::FREM, MVT::f64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000145
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000146 // We have only 64-bit bitconverts
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000147 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
148 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000149
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
151 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
152 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
153 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000154
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000156
157 setMinFunctionAlignment(1);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000158}
159
Dan Gohmand858e902010-04-17 15:26:15 +0000160SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
161 SelectionDAG &DAG) const {
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000162 switch (Op.getOpcode()) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000163 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000164 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000165 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000166 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000167 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000168 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000169 llvm_unreachable("Should not custom lower this!");
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000170 return SDValue();
171 }
172}
173
Evan Chenga1eaa3c2009-10-28 01:43:28 +0000174bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
175 if (UseSoftFloat || (VT != MVT::f32 && VT != MVT::f64))
Evan Chengeb2f9692009-10-27 19:56:55 +0000176 return false;
177
178 // +0.0 lzer
179 // +0.0f lzdr
180 // -0.0 lzer + lner
181 // -0.0f lzdr + lndr
182 return Imm.isZero() || Imm.isNegZero();
183}
184
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000185//===----------------------------------------------------------------------===//
Anton Korobeynikov3c2734c2009-08-21 18:15:41 +0000186// SystemZ Inline Assembly Support
187//===----------------------------------------------------------------------===//
188
189/// getConstraintType - Given a constraint letter, return the type of
190/// constraint it is for this target.
191TargetLowering::ConstraintType
192SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
193 if (Constraint.size() == 1) {
194 switch (Constraint[0]) {
195 case 'r':
196 return C_RegisterClass;
197 default:
198 break;
199 }
200 }
201 return TargetLowering::getConstraintType(Constraint);
202}
203
204std::pair<unsigned, const TargetRegisterClass*>
205SystemZTargetLowering::
206getRegForInlineAsmConstraint(const std::string &Constraint,
207 EVT VT) const {
208 if (Constraint.size() == 1) {
209 // GCC Constraint Letters
210 switch (Constraint[0]) {
211 default: break;
212 case 'r': // GENERAL_REGS
213 if (VT == MVT::i32)
214 return std::make_pair(0U, SystemZ::GR32RegisterClass);
215 else if (VT == MVT::i128)
216 return std::make_pair(0U, SystemZ::GR128RegisterClass);
217
218 return std::make_pair(0U, SystemZ::GR64RegisterClass);
219 }
220 }
221
222 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
223}
224
225//===----------------------------------------------------------------------===//
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000226// Calling Convention Implementation
227//===----------------------------------------------------------------------===//
228
229#include "SystemZGenCallingConv.inc"
230
Dan Gohman98ca4f22009-08-05 01:29:28 +0000231SDValue
232SystemZTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000233 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000234 bool isVarArg,
235 const SmallVectorImpl<ISD::InputArg>
236 &Ins,
237 DebugLoc dl,
238 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000239 SmallVectorImpl<SDValue> &InVals)
240 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000241
242 switch (CallConv) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000243 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000244 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000245 case CallingConv::C:
246 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000247 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000248 }
249}
250
Dan Gohman98ca4f22009-08-05 01:29:28 +0000251SDValue
252SystemZTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000253 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000254 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000255 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000256 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000257 const SmallVectorImpl<ISD::InputArg> &Ins,
258 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000259 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000260 // SystemZ target does not yet support tail call optimization.
261 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000262
263 switch (CallConv) {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000264 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000265 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000266 case CallingConv::Fast:
267 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000268 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanc9403652010-07-07 15:54:55 +0000269 Outs, OutVals, Ins, dl, DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000270 }
271}
272
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000273/// LowerCCCArguments - transform physical registers into virtual registers and
274/// generate load operations for arguments places on the stack.
275// FIXME: struct return stuff
276// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000277SDValue
278SystemZTargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000279 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000280 bool isVarArg,
281 const SmallVectorImpl<ISD::InputArg>
282 &Ins,
283 DebugLoc dl,
284 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000285 SmallVectorImpl<SDValue> &InVals)
286 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000287
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000288 MachineFunction &MF = DAG.getMachineFunction();
289 MachineFrameInfo *MFI = MF.getFrameInfo();
290 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000291
292 // Assign locations to all of the incoming arguments.
293 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000294 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
295 ArgLocs, *DAG.getContext());
296 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000297
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000298 if (isVarArg)
Chris Lattner75361b62010-04-07 22:58:41 +0000299 report_fatal_error("Varargs not supported yet");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000300
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000301 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000302 SDValue ArgValue;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000303 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +0000304 EVT LocVT = VA.getLocVT();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000305 if (VA.isRegLoc()) {
306 // Arguments passed in registers
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000307 TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 switch (LocVT.getSimpleVT().SimpleTy) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000309 default:
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000310#ifndef NDEBUG
Chris Lattner4437ae22009-08-23 07:05:07 +0000311 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 << LocVT.getSimpleVT().SimpleTy
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000313 << "\n";
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000314#endif
315 llvm_unreachable(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 case MVT::i64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000317 RC = SystemZ::GR64RegisterClass;
318 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 case MVT::f32:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000320 RC = SystemZ::FP32RegisterClass;
321 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 case MVT::f64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000323 RC = SystemZ::FP64RegisterClass;
324 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000325 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000326
327 unsigned VReg = RegInfo.createVirtualRegister(RC);
328 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000329 ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, LocVT);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000330 } else {
331 // Sanity check
332 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000333
334 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000335 // Create the frame index object for this incoming parameter...
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000336 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +0000337 VA.getLocMemOffset(), true);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000338
339 // Create the SelectionDAG nodes corresponding to a load
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000340 // from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000341 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000342 ArgValue = DAG.getLoad(LocVT, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000343 MachinePointerInfo::getFixedStack(FI),
David Greene77201552010-02-15 16:57:13 +0000344 false, false, 0);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000345 }
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000346
347 // If this is an 8/16/32-bit value, it is really passed promoted to 64
348 // bits. Insert an assert[sz]ext to capture this, then truncate to the
349 // right size.
350 if (VA.getLocInfo() == CCValAssign::SExt)
351 ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
352 DAG.getValueType(VA.getValVT()));
353 else if (VA.getLocInfo() == CCValAssign::ZExt)
354 ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
355 DAG.getValueType(VA.getValVT()));
356
357 if (VA.getLocInfo() != CCValAssign::Full)
358 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
359
Dan Gohman98ca4f22009-08-05 01:29:28 +0000360 InVals.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000361 }
362
Dan Gohman98ca4f22009-08-05 01:29:28 +0000363 return Chain;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000364}
365
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000366/// LowerCCCCallTo - functions arguments are copied from virtual regs to
367/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
368/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000369SDValue
370SystemZTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000371 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000372 bool isTailCall,
373 const SmallVectorImpl<ISD::OutputArg>
374 &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000375 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000376 const SmallVectorImpl<ISD::InputArg> &Ins,
377 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000378 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000379 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000380 const TargetFrameLowering *TFI = TM.getFrameLowering();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000381
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000382 // Offset to first argument stack slot.
383 const unsigned FirstArgOffset = 160;
384
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000385 // Analyze operands of the call, assigning locations to each operand.
386 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000387 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
388 ArgLocs, *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000389
Dan Gohman98ca4f22009-08-05 01:29:28 +0000390 CCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000391
392 // Get a count of how many bytes are to be pushed on the stack.
393 unsigned NumBytes = CCInfo.getNextStackOffset();
394
395 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
396 getPointerTy(), true));
397
398 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
399 SmallVector<SDValue, 12> MemOpChains;
400 SDValue StackPtr;
401
402 // Walk the register/memloc assignments, inserting copies/loads.
403 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
404 CCValAssign &VA = ArgLocs[i];
405
Dan Gohmanc9403652010-07-07 15:54:55 +0000406 SDValue Arg = OutVals[i];
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000407
408 // Promote the value if needed.
409 switch (VA.getLocInfo()) {
410 default: assert(0 && "Unknown loc info!");
411 case CCValAssign::Full: break;
412 case CCValAssign::SExt:
413 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
414 break;
415 case CCValAssign::ZExt:
416 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
417 break;
418 case CCValAssign::AExt:
419 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
420 break;
421 }
422
423 // Arguments that can be passed on register must be kept at RegsToPass
424 // vector
425 if (VA.isRegLoc()) {
426 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
427 } else {
428 assert(VA.isMemLoc());
429
430 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000431 StackPtr =
432 DAG.getCopyFromReg(Chain, dl,
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000433 (TFI->hasFP(MF) ?
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000434 SystemZ::R11D : SystemZ::R15D),
435 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000436
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000437 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
438 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
439 StackPtr,
440 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000441
442 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000443 MachinePointerInfo(),
David Greene77201552010-02-15 16:57:13 +0000444 false, false, 0));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000445 }
446 }
447
448 // Transform all store nodes into one single node because all store nodes are
449 // independent of each other.
450 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000452 &MemOpChains[0], MemOpChains.size());
453
454 // Build a sequence of copy-to-reg nodes chained together with token chain and
455 // flag operands which copy the outgoing args into registers. The InFlag in
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000456 // necessary since all emitted instructions must be stuck together.
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000457 SDValue InFlag;
458 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
459 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
460 RegsToPass[i].second, InFlag);
461 InFlag = Chain.getValue(1);
462 }
463
464 // If the callee is a GlobalAddress node (quite common, every direct call is)
465 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
466 // Likewise ExternalSymbol -> TargetExternalSymbol.
467 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000468 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000469 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
470 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
471
472 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000473 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000474 SmallVector<SDValue, 8> Ops;
475 Ops.push_back(Chain);
476 Ops.push_back(Callee);
477
478 // Add argument registers to the end of the list so that they are
479 // known live into the call.
480 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
481 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
482 RegsToPass[i].second.getValueType()));
483
484 if (InFlag.getNode())
485 Ops.push_back(InFlag);
486
487 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
488 InFlag = Chain.getValue(1);
489
490 // Create the CALLSEQ_END node.
491 Chain = DAG.getCALLSEQ_END(Chain,
492 DAG.getConstant(NumBytes, getPointerTy(), true),
493 DAG.getConstant(0, getPointerTy(), true),
494 InFlag);
495 InFlag = Chain.getValue(1);
496
497 // Handle result values, copying them out of physregs into vregs that we
498 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000499 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
500 DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000501}
502
Dan Gohman98ca4f22009-08-05 01:29:28 +0000503/// LowerCallResult - Lower the result values of a call into the
504/// appropriate copies out of appropriate physical registers.
505///
506SDValue
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000507SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000508 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000509 const SmallVectorImpl<ISD::InputArg>
510 &Ins,
511 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000512 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000513
514 // Assign locations to each value returned by this call.
515 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000516 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
Owen Andersone922c022009-07-22 00:24:57 +0000517 *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000518
Dan Gohman98ca4f22009-08-05 01:29:28 +0000519 CCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000520
521 // Copy all of the result registers out of their specified physreg.
522 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000523 CCValAssign &VA = RVLocs[i];
524
525 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
526 VA.getLocVT(), InFlag).getValue(1);
527 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000528 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000529
530 // If this is an 8/16/32-bit value, it is really passed promoted to 64
531 // bits. Insert an assert[sz]ext to capture this, then truncate to the
532 // right size.
533 if (VA.getLocInfo() == CCValAssign::SExt)
534 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
535 DAG.getValueType(VA.getValVT()));
536 else if (VA.getLocInfo() == CCValAssign::ZExt)
537 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
538 DAG.getValueType(VA.getValVT()));
539
540 if (VA.getLocInfo() != CCValAssign::Full)
541 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
542
Dan Gohman98ca4f22009-08-05 01:29:28 +0000543 InVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000544 }
545
Dan Gohman98ca4f22009-08-05 01:29:28 +0000546 return Chain;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000547}
548
549
Dan Gohman98ca4f22009-08-05 01:29:28 +0000550SDValue
551SystemZTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000552 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000553 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000554 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +0000555 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000556
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000557 // CCValAssign - represent the assignment of the return value to a location
558 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000559
560 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000561 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
562 RVLocs, *DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000563
Dan Gohman98ca4f22009-08-05 01:29:28 +0000564 // Analize return values.
565 CCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000566
567 // If this is the first return lowered for this function, add the regs to the
568 // liveout set for the function.
569 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
570 for (unsigned i = 0; i != RVLocs.size(); ++i)
571 if (RVLocs[i].isRegLoc())
572 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
573 }
574
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000575 SDValue Flag;
576
577 // Copy the result values into the output registers.
578 for (unsigned i = 0; i != RVLocs.size(); ++i) {
579 CCValAssign &VA = RVLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +0000580 SDValue ResValue = OutVals[i];
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000581 assert(VA.isRegLoc() && "Can only return in registers!");
582
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000583 // If this is an 8/16/32-bit value, it is really should be passed promoted
584 // to 64 bits.
585 if (VA.getLocInfo() == CCValAssign::SExt)
586 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
587 else if (VA.getLocInfo() == CCValAssign::ZExt)
588 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
589 else if (VA.getLocInfo() == CCValAssign::AExt)
590 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
591
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000592 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000593
594 // Guarantee that all emitted copies are stuck together,
595 // avoiding something bad.
596 Flag = Chain.getValue(1);
597 }
598
599 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000601
602 // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000604}
605
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000606SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
607 ISD::CondCode CC, SDValue &SystemZCC,
Dan Gohmand858e902010-04-17 15:26:15 +0000608 SelectionDAG &DAG) const {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000609 // FIXME: Emit a test if RHS is zero
610
611 bool isUnsigned = false;
612 SystemZCC::CondCodes TCC;
613 switch (CC) {
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000614 default:
615 llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000616 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000617 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000618 TCC = SystemZCC::E;
619 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000620 case ISD::SETUEQ:
621 TCC = SystemZCC::NLH;
622 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000623 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000624 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000625 TCC = SystemZCC::NE;
626 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000627 case ISD::SETUNE:
628 TCC = SystemZCC::LH;
629 break;
630 case ISD::SETO:
631 TCC = SystemZCC::O;
632 break;
633 case ISD::SETUO:
634 TCC = SystemZCC::NO;
635 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000636 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000637 if (LHS.getValueType().isFloatingPoint()) {
638 TCC = SystemZCC::NH;
639 break;
640 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000641 isUnsigned = true; // FALLTHROUGH
642 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000643 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000644 TCC = SystemZCC::LE;
645 break;
646 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000647 if (LHS.getValueType().isFloatingPoint()) {
648 TCC = SystemZCC::NL;
649 break;
650 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000651 isUnsigned = true; // FALLTHROUGH
652 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000653 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000654 TCC = SystemZCC::HE;
655 break;
656 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000657 if (LHS.getValueType().isFloatingPoint()) {
658 TCC = SystemZCC::NLE;
659 break;
660 }
661 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000662 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000663 case ISD::SETOGT:
664 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000665 break;
666 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000667 if (LHS.getValueType().isFloatingPoint()) {
668 TCC = SystemZCC::NHE;
669 break;
670 }
671 isUnsigned = true; // FALLTHROUGH
672 case ISD::SETLT:
673 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000674 TCC = SystemZCC::L;
675 break;
676 }
677
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 SystemZCC = DAG.getConstant(TCC, MVT::i32);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000679
680 DebugLoc dl = LHS.getDebugLoc();
681 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
Dan Gohmanaa123222009-10-28 00:55:57 +0000682 dl, MVT::i64, LHS, RHS);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000683}
684
685
Dan Gohmand858e902010-04-17 15:26:15 +0000686SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000687 SDValue Chain = Op.getOperand(0);
688 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
689 SDValue LHS = Op.getOperand(2);
690 SDValue RHS = Op.getOperand(3);
691 SDValue Dest = Op.getOperand(4);
692 DebugLoc dl = Op.getDebugLoc();
693
694 SDValue SystemZCC;
695 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
696 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
697 Chain, Dest, SystemZCC, Flag);
698}
699
Dan Gohmand858e902010-04-17 15:26:15 +0000700SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op,
701 SelectionDAG &DAG) const {
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000702 SDValue LHS = Op.getOperand(0);
703 SDValue RHS = Op.getOperand(1);
704 SDValue TrueV = Op.getOperand(2);
705 SDValue FalseV = Op.getOperand(3);
706 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
707 DebugLoc dl = Op.getDebugLoc();
708
709 SDValue SystemZCC;
710 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
711
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000712 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000713 SmallVector<SDValue, 4> Ops;
714 Ops.push_back(TrueV);
715 Ops.push_back(FalseV);
716 Ops.push_back(SystemZCC);
717 Ops.push_back(Flag);
718
719 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
720}
721
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000722SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000723 SelectionDAG &DAG) const {
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000724 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000725 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000726 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000727
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000728 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
729 bool ExtraLoadRequired =
730 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
731
732 SDValue Result;
733 if (!IsPic && !ExtraLoadRequired) {
Devang Patel0d881da2010-07-06 22:08:15 +0000734 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000735 Offset = 0;
736 } else {
737 unsigned char OpFlags = 0;
738 if (ExtraLoadRequired)
739 OpFlags = SystemZII::MO_GOTENT;
740
Devang Patel0d881da2010-07-06 22:08:15 +0000741 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000742 }
743
744 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
745 getPointerTy(), Result);
746
747 if (ExtraLoadRequired)
748 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000749 MachinePointerInfo::getGOT(), false, false, 0);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000750
751 // If there was a non-zero offset that we didn't fold, create an explicit
752 // addition for it.
753 if (Offset != 0)
754 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
755 DAG.getConstant(Offset, getPointerTy()));
756
757 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000758}
759
Anton Korobeynikovae535672009-07-16 14:19:35 +0000760// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000761SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000762 SelectionDAG &DAG) const {
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000763 DebugLoc dl = Op.getDebugLoc();
764 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
765 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
766
767 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
768}
769
Anton Korobeynikovae535672009-07-16 14:19:35 +0000770
771// FIXME: PIC here
772// FIXME: This is just dirty hack. We need to lower cpool properly
773SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000774 SelectionDAG &DAG) const {
Anton Korobeynikovae535672009-07-16 14:19:35 +0000775 DebugLoc dl = Op.getDebugLoc();
776 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
777
778 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
779 CP->getAlignment(),
780 CP->getOffset());
781
782 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
783}
784
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000785const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
786 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000787 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000788 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000789 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
790 case SystemZISD::CMP: return "SystemZISD::CMP";
791 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000792 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000793 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000794 default: return NULL;
795 }
796}
797
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000798//===----------------------------------------------------------------------===//
799// Other Lowering Code
800//===----------------------------------------------------------------------===//
801
802MachineBasicBlock*
803SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000804 MachineBasicBlock *BB) const {
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000805 const SystemZInstrInfo &TII = *TM.getInstrInfo();
806 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000807 assert((MI->getOpcode() == SystemZ::Select32 ||
808 MI->getOpcode() == SystemZ::SelectF32 ||
809 MI->getOpcode() == SystemZ::Select64 ||
810 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000811 "Unexpected instr type to insert");
812
813 // To "insert" a SELECT instruction, we actually have to insert the diamond
814 // control-flow pattern. The incoming instruction knows the destination vreg
815 // to set, the condition code register to branch on, the true/false values to
816 // select between, and a branch opcode to use.
817 const BasicBlock *LLVM_BB = BB->getBasicBlock();
818 MachineFunction::iterator I = BB;
819 ++I;
820
821 // thisMBB:
822 // ...
823 // TrueVal = ...
824 // cmpTY ccX, r1, r2
825 // jCC copy1MBB
826 // fallthrough --> copy0MBB
827 MachineBasicBlock *thisMBB = BB;
828 MachineFunction *F = BB->getParent();
829 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
830 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
831 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000832 F->insert(I, copy0MBB);
833 F->insert(I, copy1MBB);
834 // Update machine-CFG edges by transferring all successors of the current
835 // block to the new block which will contain the Phi node for the select.
Dan Gohman14152b42010-07-06 20:24:04 +0000836 copy1MBB->splice(copy1MBB->begin(), BB,
837 llvm::next(MachineBasicBlock::iterator(MI)),
838 BB->end());
839 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000840 // Next, add the true and fallthrough blocks as its successors.
841 BB->addSuccessor(copy0MBB);
842 BB->addSuccessor(copy1MBB);
843
Dan Gohman14152b42010-07-06 20:24:04 +0000844 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
845
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000846 // copy0MBB:
847 // %FalseValue = ...
848 // # fallthrough to copy1MBB
849 BB = copy0MBB;
850
851 // Update machine-CFG edges
852 BB->addSuccessor(copy1MBB);
853
854 // copy1MBB:
855 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
856 // ...
857 BB = copy1MBB;
Dan Gohman14152b42010-07-06 20:24:04 +0000858 BuildMI(*BB, BB->begin(), dl, TII.get(SystemZ::PHI),
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000859 MI->getOperand(0).getReg())
860 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
861 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
862
Dan Gohman14152b42010-07-06 20:24:04 +0000863 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000864 return BB;
865}