blob: d331614400e4f7e50c4b72992ed3c34dac7de2ba [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);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000156}
157
Dan Gohmand858e902010-04-17 15:26:15 +0000158SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
159 SelectionDAG &DAG) const {
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000160 switch (Op.getOpcode()) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000161 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000162 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000163 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000164 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000165 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000166 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000167 llvm_unreachable("Should not custom lower this!");
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000168 return SDValue();
169 }
170}
171
Evan Chenga1eaa3c2009-10-28 01:43:28 +0000172bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
173 if (UseSoftFloat || (VT != MVT::f32 && VT != MVT::f64))
Evan Chengeb2f9692009-10-27 19:56:55 +0000174 return false;
175
176 // +0.0 lzer
177 // +0.0f lzdr
178 // -0.0 lzer + lner
179 // -0.0f lzdr + lndr
180 return Imm.isZero() || Imm.isNegZero();
181}
182
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000183//===----------------------------------------------------------------------===//
Anton Korobeynikov3c2734c2009-08-21 18:15:41 +0000184// SystemZ Inline Assembly Support
185//===----------------------------------------------------------------------===//
186
187/// getConstraintType - Given a constraint letter, return the type of
188/// constraint it is for this target.
189TargetLowering::ConstraintType
190SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
191 if (Constraint.size() == 1) {
192 switch (Constraint[0]) {
193 case 'r':
194 return C_RegisterClass;
195 default:
196 break;
197 }
198 }
199 return TargetLowering::getConstraintType(Constraint);
200}
201
202std::pair<unsigned, const TargetRegisterClass*>
203SystemZTargetLowering::
204getRegForInlineAsmConstraint(const std::string &Constraint,
205 EVT VT) const {
206 if (Constraint.size() == 1) {
207 // GCC Constraint Letters
208 switch (Constraint[0]) {
209 default: break;
210 case 'r': // GENERAL_REGS
211 if (VT == MVT::i32)
212 return std::make_pair(0U, SystemZ::GR32RegisterClass);
213 else if (VT == MVT::i128)
214 return std::make_pair(0U, SystemZ::GR128RegisterClass);
215
216 return std::make_pair(0U, SystemZ::GR64RegisterClass);
217 }
218 }
219
220 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
221}
222
223//===----------------------------------------------------------------------===//
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000224// Calling Convention Implementation
225//===----------------------------------------------------------------------===//
226
227#include "SystemZGenCallingConv.inc"
228
Dan Gohman98ca4f22009-08-05 01:29:28 +0000229SDValue
230SystemZTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000231 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000232 bool isVarArg,
233 const SmallVectorImpl<ISD::InputArg>
234 &Ins,
235 DebugLoc dl,
236 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000237 SmallVectorImpl<SDValue> &InVals)
238 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000239
240 switch (CallConv) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000241 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000242 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000243 case CallingConv::C:
244 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000245 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000246 }
247}
248
Dan Gohman98ca4f22009-08-05 01:29:28 +0000249SDValue
250SystemZTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000251 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000252 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000253 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000254 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000255 const SmallVectorImpl<ISD::InputArg> &Ins,
256 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000257 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000258 // SystemZ target does not yet support tail call optimization.
259 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000260
261 switch (CallConv) {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000262 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000263 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000264 case CallingConv::Fast:
265 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000266 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohmanc9403652010-07-07 15:54:55 +0000267 Outs, OutVals, Ins, dl, DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000268 }
269}
270
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000271/// LowerCCCArguments - transform physical registers into virtual registers and
272/// generate load operations for arguments places on the stack.
273// FIXME: struct return stuff
274// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000275SDValue
276SystemZTargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000277 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000278 bool isVarArg,
279 const SmallVectorImpl<ISD::InputArg>
280 &Ins,
281 DebugLoc dl,
282 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000283 SmallVectorImpl<SDValue> &InVals)
284 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000285
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000286 MachineFunction &MF = DAG.getMachineFunction();
287 MachineFrameInfo *MFI = MF.getFrameInfo();
288 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000289
290 // Assign locations to all of the incoming arguments.
291 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000292 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
293 ArgLocs, *DAG.getContext());
294 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000295
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000296 if (isVarArg)
Chris Lattner75361b62010-04-07 22:58:41 +0000297 report_fatal_error("Varargs not supported yet");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000298
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000299 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000300 SDValue ArgValue;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000301 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +0000302 EVT LocVT = VA.getLocVT();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000303 if (VA.isRegLoc()) {
304 // Arguments passed in registers
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000305 TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 switch (LocVT.getSimpleVT().SimpleTy) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000307 default:
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000308#ifndef NDEBUG
Chris Lattner4437ae22009-08-23 07:05:07 +0000309 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 << LocVT.getSimpleVT().SimpleTy
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000311 << "\n";
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000312#endif
313 llvm_unreachable(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 case MVT::i64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000315 RC = SystemZ::GR64RegisterClass;
316 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 case MVT::f32:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000318 RC = SystemZ::FP32RegisterClass;
319 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 case MVT::f64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000321 RC = SystemZ::FP64RegisterClass;
322 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000323 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000324
325 unsigned VReg = RegInfo.createVirtualRegister(RC);
326 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000327 ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, LocVT);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000328 } else {
329 // Sanity check
330 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000331
332 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000333 // Create the frame index object for this incoming parameter...
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000334 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +0000335 VA.getLocMemOffset(), true);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000336
337 // Create the SelectionDAG nodes corresponding to a load
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000338 // from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000339 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000340 ArgValue = DAG.getLoad(LocVT, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000341 MachinePointerInfo::getFixedStack(FI),
David Greene77201552010-02-15 16:57:13 +0000342 false, false, 0);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000343 }
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000344
345 // If this is an 8/16/32-bit value, it is really passed promoted to 64
346 // bits. Insert an assert[sz]ext to capture this, then truncate to the
347 // right size.
348 if (VA.getLocInfo() == CCValAssign::SExt)
349 ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
350 DAG.getValueType(VA.getValVT()));
351 else if (VA.getLocInfo() == CCValAssign::ZExt)
352 ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
353 DAG.getValueType(VA.getValVT()));
354
355 if (VA.getLocInfo() != CCValAssign::Full)
356 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
357
Dan Gohman98ca4f22009-08-05 01:29:28 +0000358 InVals.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000359 }
360
Dan Gohman98ca4f22009-08-05 01:29:28 +0000361 return Chain;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000362}
363
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000364/// LowerCCCCallTo - functions arguments are copied from virtual regs to
365/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
366/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000367SDValue
368SystemZTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000369 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000370 bool isTailCall,
371 const SmallVectorImpl<ISD::OutputArg>
372 &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000373 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000374 const SmallVectorImpl<ISD::InputArg> &Ins,
375 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000376 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000377 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000378 const TargetFrameLowering *TFI = TM.getFrameLowering();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000379
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000380 // Offset to first argument stack slot.
381 const unsigned FirstArgOffset = 160;
382
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000383 // Analyze operands of the call, assigning locations to each operand.
384 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000385 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
386 ArgLocs, *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000387
Dan Gohman98ca4f22009-08-05 01:29:28 +0000388 CCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000389
390 // Get a count of how many bytes are to be pushed on the stack.
391 unsigned NumBytes = CCInfo.getNextStackOffset();
392
393 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
394 getPointerTy(), true));
395
396 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
397 SmallVector<SDValue, 12> MemOpChains;
398 SDValue StackPtr;
399
400 // Walk the register/memloc assignments, inserting copies/loads.
401 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
402 CCValAssign &VA = ArgLocs[i];
403
Dan Gohmanc9403652010-07-07 15:54:55 +0000404 SDValue Arg = OutVals[i];
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000405
406 // Promote the value if needed.
407 switch (VA.getLocInfo()) {
408 default: assert(0 && "Unknown loc info!");
409 case CCValAssign::Full: break;
410 case CCValAssign::SExt:
411 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
412 break;
413 case CCValAssign::ZExt:
414 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
415 break;
416 case CCValAssign::AExt:
417 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
418 break;
419 }
420
421 // Arguments that can be passed on register must be kept at RegsToPass
422 // vector
423 if (VA.isRegLoc()) {
424 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
425 } else {
426 assert(VA.isMemLoc());
427
428 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000429 StackPtr =
430 DAG.getCopyFromReg(Chain, dl,
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000431 (TFI->hasFP(MF) ?
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000432 SystemZ::R11D : SystemZ::R15D),
433 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000434
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000435 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
436 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
437 StackPtr,
438 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000439
440 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000441 MachinePointerInfo(),
David Greene77201552010-02-15 16:57:13 +0000442 false, false, 0));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000443 }
444 }
445
446 // Transform all store nodes into one single node because all store nodes are
447 // independent of each other.
448 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000450 &MemOpChains[0], MemOpChains.size());
451
452 // Build a sequence of copy-to-reg nodes chained together with token chain and
453 // flag operands which copy the outgoing args into registers. The InFlag in
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000454 // necessary since all emitted instructions must be stuck together.
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000455 SDValue InFlag;
456 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
457 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
458 RegsToPass[i].second, InFlag);
459 InFlag = Chain.getValue(1);
460 }
461
462 // If the callee is a GlobalAddress node (quite common, every direct call is)
463 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
464 // Likewise ExternalSymbol -> TargetExternalSymbol.
465 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000466 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000467 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
468 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
469
470 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000471 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000472 SmallVector<SDValue, 8> Ops;
473 Ops.push_back(Chain);
474 Ops.push_back(Callee);
475
476 // Add argument registers to the end of the list so that they are
477 // known live into the call.
478 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
479 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
480 RegsToPass[i].second.getValueType()));
481
482 if (InFlag.getNode())
483 Ops.push_back(InFlag);
484
485 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
486 InFlag = Chain.getValue(1);
487
488 // Create the CALLSEQ_END node.
489 Chain = DAG.getCALLSEQ_END(Chain,
490 DAG.getConstant(NumBytes, getPointerTy(), true),
491 DAG.getConstant(0, getPointerTy(), true),
492 InFlag);
493 InFlag = Chain.getValue(1);
494
495 // Handle result values, copying them out of physregs into vregs that we
496 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000497 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
498 DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000499}
500
Dan Gohman98ca4f22009-08-05 01:29:28 +0000501/// LowerCallResult - Lower the result values of a call into the
502/// appropriate copies out of appropriate physical registers.
503///
504SDValue
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000505SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000506 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000507 const SmallVectorImpl<ISD::InputArg>
508 &Ins,
509 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000510 SmallVectorImpl<SDValue> &InVals) const {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000511
512 // Assign locations to each value returned by this call.
513 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000514 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
Owen Andersone922c022009-07-22 00:24:57 +0000515 *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000516
Dan Gohman98ca4f22009-08-05 01:29:28 +0000517 CCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000518
519 // Copy all of the result registers out of their specified physreg.
520 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000521 CCValAssign &VA = RVLocs[i];
522
523 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
524 VA.getLocVT(), InFlag).getValue(1);
525 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000526 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000527
528 // If this is an 8/16/32-bit value, it is really passed promoted to 64
529 // bits. Insert an assert[sz]ext to capture this, then truncate to the
530 // right size.
531 if (VA.getLocInfo() == CCValAssign::SExt)
532 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
533 DAG.getValueType(VA.getValVT()));
534 else if (VA.getLocInfo() == CCValAssign::ZExt)
535 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
536 DAG.getValueType(VA.getValVT()));
537
538 if (VA.getLocInfo() != CCValAssign::Full)
539 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
540
Dan Gohman98ca4f22009-08-05 01:29:28 +0000541 InVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000542 }
543
Dan Gohman98ca4f22009-08-05 01:29:28 +0000544 return Chain;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000545}
546
547
Dan Gohman98ca4f22009-08-05 01:29:28 +0000548SDValue
549SystemZTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000550 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000551 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000552 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +0000553 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000554
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000555 // CCValAssign - represent the assignment of the return value to a location
556 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000557
558 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000559 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
560 RVLocs, *DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000561
Dan Gohman98ca4f22009-08-05 01:29:28 +0000562 // Analize return values.
563 CCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000564
565 // If this is the first return lowered for this function, add the regs to the
566 // liveout set for the function.
567 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
568 for (unsigned i = 0; i != RVLocs.size(); ++i)
569 if (RVLocs[i].isRegLoc())
570 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
571 }
572
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000573 SDValue Flag;
574
575 // Copy the result values into the output registers.
576 for (unsigned i = 0; i != RVLocs.size(); ++i) {
577 CCValAssign &VA = RVLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +0000578 SDValue ResValue = OutVals[i];
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000579 assert(VA.isRegLoc() && "Can only return in registers!");
580
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000581 // If this is an 8/16/32-bit value, it is really should be passed promoted
582 // to 64 bits.
583 if (VA.getLocInfo() == CCValAssign::SExt)
584 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
585 else if (VA.getLocInfo() == CCValAssign::ZExt)
586 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
587 else if (VA.getLocInfo() == CCValAssign::AExt)
588 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
589
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000590 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000591
592 // Guarantee that all emitted copies are stuck together,
593 // avoiding something bad.
594 Flag = Chain.getValue(1);
595 }
596
597 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000599
600 // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000602}
603
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000604SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
605 ISD::CondCode CC, SDValue &SystemZCC,
Dan Gohmand858e902010-04-17 15:26:15 +0000606 SelectionDAG &DAG) const {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000607 // FIXME: Emit a test if RHS is zero
608
609 bool isUnsigned = false;
610 SystemZCC::CondCodes TCC;
611 switch (CC) {
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000612 default:
613 llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000614 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000615 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000616 TCC = SystemZCC::E;
617 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000618 case ISD::SETUEQ:
619 TCC = SystemZCC::NLH;
620 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000621 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000622 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000623 TCC = SystemZCC::NE;
624 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000625 case ISD::SETUNE:
626 TCC = SystemZCC::LH;
627 break;
628 case ISD::SETO:
629 TCC = SystemZCC::O;
630 break;
631 case ISD::SETUO:
632 TCC = SystemZCC::NO;
633 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000634 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000635 if (LHS.getValueType().isFloatingPoint()) {
636 TCC = SystemZCC::NH;
637 break;
638 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000639 isUnsigned = true; // FALLTHROUGH
640 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000641 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000642 TCC = SystemZCC::LE;
643 break;
644 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000645 if (LHS.getValueType().isFloatingPoint()) {
646 TCC = SystemZCC::NL;
647 break;
648 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000649 isUnsigned = true; // FALLTHROUGH
650 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000651 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000652 TCC = SystemZCC::HE;
653 break;
654 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000655 if (LHS.getValueType().isFloatingPoint()) {
656 TCC = SystemZCC::NLE;
657 break;
658 }
659 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000660 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000661 case ISD::SETOGT:
662 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000663 break;
664 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000665 if (LHS.getValueType().isFloatingPoint()) {
666 TCC = SystemZCC::NHE;
667 break;
668 }
669 isUnsigned = true; // FALLTHROUGH
670 case ISD::SETLT:
671 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000672 TCC = SystemZCC::L;
673 break;
674 }
675
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 SystemZCC = DAG.getConstant(TCC, MVT::i32);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000677
678 DebugLoc dl = LHS.getDebugLoc();
679 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
Dan Gohmanaa123222009-10-28 00:55:57 +0000680 dl, MVT::i64, LHS, RHS);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000681}
682
683
Dan Gohmand858e902010-04-17 15:26:15 +0000684SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000685 SDValue Chain = Op.getOperand(0);
686 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
687 SDValue LHS = Op.getOperand(2);
688 SDValue RHS = Op.getOperand(3);
689 SDValue Dest = Op.getOperand(4);
690 DebugLoc dl = Op.getDebugLoc();
691
692 SDValue SystemZCC;
693 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
694 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
695 Chain, Dest, SystemZCC, Flag);
696}
697
Dan Gohmand858e902010-04-17 15:26:15 +0000698SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op,
699 SelectionDAG &DAG) const {
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000700 SDValue LHS = Op.getOperand(0);
701 SDValue RHS = Op.getOperand(1);
702 SDValue TrueV = Op.getOperand(2);
703 SDValue FalseV = Op.getOperand(3);
704 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
705 DebugLoc dl = Op.getDebugLoc();
706
707 SDValue SystemZCC;
708 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
709
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000710 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000711 SmallVector<SDValue, 4> Ops;
712 Ops.push_back(TrueV);
713 Ops.push_back(FalseV);
714 Ops.push_back(SystemZCC);
715 Ops.push_back(Flag);
716
717 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
718}
719
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000720SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000721 SelectionDAG &DAG) const {
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000722 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000723 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000724 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000725
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000726 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
727 bool ExtraLoadRequired =
728 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
729
730 SDValue Result;
731 if (!IsPic && !ExtraLoadRequired) {
Devang Patel0d881da2010-07-06 22:08:15 +0000732 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000733 Offset = 0;
734 } else {
735 unsigned char OpFlags = 0;
736 if (ExtraLoadRequired)
737 OpFlags = SystemZII::MO_GOTENT;
738
Devang Patel0d881da2010-07-06 22:08:15 +0000739 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000740 }
741
742 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
743 getPointerTy(), Result);
744
745 if (ExtraLoadRequired)
746 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000747 MachinePointerInfo::getGOT(), false, false, 0);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000748
749 // If there was a non-zero offset that we didn't fold, create an explicit
750 // addition for it.
751 if (Offset != 0)
752 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
753 DAG.getConstant(Offset, getPointerTy()));
754
755 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000756}
757
Anton Korobeynikovae535672009-07-16 14:19:35 +0000758// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000759SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000760 SelectionDAG &DAG) const {
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000761 DebugLoc dl = Op.getDebugLoc();
762 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
763 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
764
765 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
766}
767
Anton Korobeynikovae535672009-07-16 14:19:35 +0000768
769// FIXME: PIC here
770// FIXME: This is just dirty hack. We need to lower cpool properly
771SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000772 SelectionDAG &DAG) const {
Anton Korobeynikovae535672009-07-16 14:19:35 +0000773 DebugLoc dl = Op.getDebugLoc();
774 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
775
776 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
777 CP->getAlignment(),
778 CP->getOffset());
779
780 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
781}
782
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000783const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
784 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000785 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000786 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000787 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
788 case SystemZISD::CMP: return "SystemZISD::CMP";
789 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000790 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000791 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000792 default: return NULL;
793 }
794}
795
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000796//===----------------------------------------------------------------------===//
797// Other Lowering Code
798//===----------------------------------------------------------------------===//
799
800MachineBasicBlock*
801SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000802 MachineBasicBlock *BB) const {
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000803 const SystemZInstrInfo &TII = *TM.getInstrInfo();
804 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000805 assert((MI->getOpcode() == SystemZ::Select32 ||
806 MI->getOpcode() == SystemZ::SelectF32 ||
807 MI->getOpcode() == SystemZ::Select64 ||
808 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000809 "Unexpected instr type to insert");
810
811 // To "insert" a SELECT instruction, we actually have to insert the diamond
812 // control-flow pattern. The incoming instruction knows the destination vreg
813 // to set, the condition code register to branch on, the true/false values to
814 // select between, and a branch opcode to use.
815 const BasicBlock *LLVM_BB = BB->getBasicBlock();
816 MachineFunction::iterator I = BB;
817 ++I;
818
819 // thisMBB:
820 // ...
821 // TrueVal = ...
822 // cmpTY ccX, r1, r2
823 // jCC copy1MBB
824 // fallthrough --> copy0MBB
825 MachineBasicBlock *thisMBB = BB;
826 MachineFunction *F = BB->getParent();
827 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
828 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
829 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000830 F->insert(I, copy0MBB);
831 F->insert(I, copy1MBB);
832 // Update machine-CFG edges by transferring all successors of the current
833 // block to the new block which will contain the Phi node for the select.
Dan Gohman14152b42010-07-06 20:24:04 +0000834 copy1MBB->splice(copy1MBB->begin(), BB,
835 llvm::next(MachineBasicBlock::iterator(MI)),
836 BB->end());
837 copy1MBB->transferSuccessorsAndUpdatePHIs(BB);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000838 // Next, add the true and fallthrough blocks as its successors.
839 BB->addSuccessor(copy0MBB);
840 BB->addSuccessor(copy1MBB);
841
Dan Gohman14152b42010-07-06 20:24:04 +0000842 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
843
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000844 // copy0MBB:
845 // %FalseValue = ...
846 // # fallthrough to copy1MBB
847 BB = copy0MBB;
848
849 // Update machine-CFG edges
850 BB->addSuccessor(copy1MBB);
851
852 // copy1MBB:
853 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
854 // ...
855 BB = copy1MBB;
Dan Gohman14152b42010-07-06 20:24:04 +0000856 BuildMI(*BB, BB->begin(), dl, TII.get(SystemZ::PHI),
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000857 MI->getOperand(0).getReg())
858 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
859 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
860
Dan Gohman14152b42010-07-06 20:24:04 +0000861 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000862 return BB;
863}