blob: 59701a64613ffead158212a6c9f749e6d24376ac [file] [log] [blame]
Anton Korobeynikov4403b932009-07-16 13:27:25 +00001//===-- SystemZISelLowering.cpp - SystemZ DAG Lowering Implementation -----==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SystemZTargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "systemz-lower"
15
16#include "SystemZISelLowering.h"
17#include "SystemZ.h"
18#include "SystemZTargetMachine.h"
19#include "SystemZSubtarget.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/CallingConv.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/GlobalAlias.h"
26#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/CodeGen/PseudoSourceValue.h"
32#include "llvm/CodeGen/SelectionDAGISel.h"
33#include "llvm/CodeGen/ValueTypes.h"
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000034#include "llvm/Target/TargetOptions.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000035#include "llvm/Target/TargetLoweringObjectFile.h"
36#include "llvm/Support/Debug.h"
Chris 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
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000062 // Set shifts properties
Owen Anderson825b72b2009-08-11 20:47:22 +000063 setShiftAmountType(MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000064
Anton Korobeynikov4403b932009-07-16 13:27:25 +000065 // Provide all sorts of operation actions
Owen Anderson825b72b2009-08-11 20:47:22 +000066 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
67 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
68 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000069
Owen Anderson825b72b2009-08-11 20:47:22 +000070 setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Expand);
71 setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Expand);
72 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Anton Korobeynikov299dc782009-07-16 14:22:30 +000073
Owen Anderson825b72b2009-08-11 20:47:22 +000074 setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Expand);
75 setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Expand);
76 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
Anton Korobeynikov23eff5c2009-07-16 14:20:08 +000077
Anton Korobeynikove0167c12009-07-16 13:35:30 +000078 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Dan Gohmanaa123222009-10-28 00:55:57 +000079
80 // TODO: It may be better to default to latency-oriented scheduling, however
81 // LLVM's current latency-oriented scheduler can't handle physreg definitions
82 // such as SystemZ has with PSW, so set this to the register-pressure
83 // scheduler, because it can.
84 setSchedulingPreference(SchedulingForRegPressure);
85
Anton Korobeynikov159ac632009-07-16 14:28:46 +000086 setBooleanContents(ZeroOrOneBooleanContent);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000087
Owen Anderson825b72b2009-08-11 20:47:22 +000088 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
89 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
90 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
91 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
92 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
93 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
94 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
95 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
96 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
97 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
98 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000099
Owen Anderson825b72b2009-08-11 20:47:22 +0000100 setOperationAction(ISD::SDIV, MVT::i32, Expand);
101 setOperationAction(ISD::UDIV, MVT::i32, Expand);
102 setOperationAction(ISD::SDIV, MVT::i64, Expand);
103 setOperationAction(ISD::UDIV, MVT::i64, Expand);
104 setOperationAction(ISD::SREM, MVT::i32, Expand);
105 setOperationAction(ISD::UREM, MVT::i32, Expand);
106 setOperationAction(ISD::SREM, MVT::i64, Expand);
107 setOperationAction(ISD::UREM, MVT::i64, Expand);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000108
Owen Anderson825b72b2009-08-11 20:47:22 +0000109 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Anton Korobeynikove37a37d2009-07-18 12:20:36 +0000110
Owen Anderson825b72b2009-08-11 20:47:22 +0000111 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
112 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
113 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
114 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
115 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
116 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
Anton Korobeynikov0cca0692009-07-18 12:26:13 +0000117
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000118 // FIXME: Can we lower these 2 efficiently?
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 setOperationAction(ISD::SETCC, MVT::i32, Expand);
120 setOperationAction(ISD::SETCC, MVT::i64, Expand);
121 setOperationAction(ISD::SETCC, MVT::f32, Expand);
122 setOperationAction(ISD::SETCC, MVT::f64, Expand);
123 setOperationAction(ISD::SELECT, MVT::i32, Expand);
124 setOperationAction(ISD::SELECT, MVT::i64, Expand);
125 setOperationAction(ISD::SELECT, MVT::f32, Expand);
126 setOperationAction(ISD::SELECT, MVT::f64, Expand);
127 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
128 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
129 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
130 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000131
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setOperationAction(ISD::MULHS, MVT::i64, Expand);
133 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000134
Anton Korobeynikovfc9489a2009-08-21 18:52:42 +0000135 // FIXME: Can we support these natively?
136 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
137 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
138 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
139 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
140
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000141 // Lower some FP stuff
Owen Anderson825b72b2009-08-11 20:47:22 +0000142 setOperationAction(ISD::FSIN, MVT::f32, Expand);
143 setOperationAction(ISD::FSIN, MVT::f64, Expand);
144 setOperationAction(ISD::FCOS, MVT::f32, Expand);
145 setOperationAction(ISD::FCOS, MVT::f64, Expand);
146 setOperationAction(ISD::FREM, MVT::f32, Expand);
147 setOperationAction(ISD::FREM, 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
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
151 setOperationAction(ISD::BIT_CONVERT, 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);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000159}
160
161SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
162 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,
239 SmallVectorImpl<SDValue> &InVals) {
240
241 switch (CallConv) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000242 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000243 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000244 case CallingConv::C:
245 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000246 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000247 }
248}
249
Dan Gohman98ca4f22009-08-05 01:29:28 +0000250SDValue
251SystemZTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000252 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000253 bool isTailCall,
254 const SmallVectorImpl<ISD::OutputArg> &Outs,
255 const SmallVectorImpl<ISD::InputArg> &Ins,
256 DebugLoc dl, SelectionDAG &DAG,
257 SmallVectorImpl<SDValue> &InVals) {
258
259 switch (CallConv) {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000260 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000261 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000262 case CallingConv::Fast:
263 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000264 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
265 Outs, Ins, dl, DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000266 }
267}
268
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000269/// LowerCCCArguments - transform physical registers into virtual registers and
270/// generate load operations for arguments places on the stack.
271// FIXME: struct return stuff
272// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000273SDValue
274SystemZTargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000275 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000276 bool isVarArg,
277 const SmallVectorImpl<ISD::InputArg>
278 &Ins,
279 DebugLoc dl,
280 SelectionDAG &DAG,
281 SmallVectorImpl<SDValue> &InVals) {
282
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000283 MachineFunction &MF = DAG.getMachineFunction();
284 MachineFrameInfo *MFI = MF.getFrameInfo();
285 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000286
287 // Assign locations to all of the incoming arguments.
288 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000289 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
290 ArgLocs, *DAG.getContext());
291 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000292
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000293 if (isVarArg)
294 llvm_report_error("Varargs not supported yet");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000295
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000296 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000297 SDValue ArgValue;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000298 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +0000299 EVT LocVT = VA.getLocVT();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000300 if (VA.isRegLoc()) {
301 // Arguments passed in registers
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000302 TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 switch (LocVT.getSimpleVT().SimpleTy) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000304 default:
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000305#ifndef NDEBUG
Chris Lattner4437ae22009-08-23 07:05:07 +0000306 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 << LocVT.getSimpleVT().SimpleTy
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000308 << "\n";
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000309#endif
310 llvm_unreachable(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 case MVT::i64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000312 RC = SystemZ::GR64RegisterClass;
313 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 case MVT::f32:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000315 RC = SystemZ::FP32RegisterClass;
316 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 case MVT::f64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000318 RC = SystemZ::FP64RegisterClass;
319 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000320 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000321
322 unsigned VReg = RegInfo.createVirtualRegister(RC);
323 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000324 ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, LocVT);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000325 } else {
326 // Sanity check
327 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000328
329 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000330 // Create the frame index object for this incoming parameter...
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000331 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000332 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000333
334 // Create the SelectionDAG nodes corresponding to a load
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000335 // from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000336 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000337 ArgValue = DAG.getLoad(LocVT, dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +0000338 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000339 }
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000340
341 // If this is an 8/16/32-bit value, it is really passed promoted to 64
342 // bits. Insert an assert[sz]ext to capture this, then truncate to the
343 // right size.
344 if (VA.getLocInfo() == CCValAssign::SExt)
345 ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
346 DAG.getValueType(VA.getValVT()));
347 else if (VA.getLocInfo() == CCValAssign::ZExt)
348 ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
349 DAG.getValueType(VA.getValVT()));
350
351 if (VA.getLocInfo() != CCValAssign::Full)
352 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
353
Dan Gohman98ca4f22009-08-05 01:29:28 +0000354 InVals.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000355 }
356
Dan Gohman98ca4f22009-08-05 01:29:28 +0000357 return Chain;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000358}
359
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000360/// LowerCCCCallTo - functions arguments are copied from virtual regs to
361/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
362/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000363SDValue
364SystemZTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000365 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000366 bool isTailCall,
367 const SmallVectorImpl<ISD::OutputArg>
368 &Outs,
369 const SmallVectorImpl<ISD::InputArg> &Ins,
370 DebugLoc dl, SelectionDAG &DAG,
371 SmallVectorImpl<SDValue> &InVals) {
372
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000373 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000374
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000375 // Offset to first argument stack slot.
376 const unsigned FirstArgOffset = 160;
377
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000378 // Analyze operands of the call, assigning locations to each operand.
379 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000380 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
381 ArgLocs, *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000382
Dan Gohman98ca4f22009-08-05 01:29:28 +0000383 CCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000384
385 // Get a count of how many bytes are to be pushed on the stack.
386 unsigned NumBytes = CCInfo.getNextStackOffset();
387
388 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
389 getPointerTy(), true));
390
391 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
392 SmallVector<SDValue, 12> MemOpChains;
393 SDValue StackPtr;
394
395 // Walk the register/memloc assignments, inserting copies/loads.
396 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
397 CCValAssign &VA = ArgLocs[i];
398
Dan Gohman98ca4f22009-08-05 01:29:28 +0000399 SDValue Arg = Outs[i].Val;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000400
401 // Promote the value if needed.
402 switch (VA.getLocInfo()) {
403 default: assert(0 && "Unknown loc info!");
404 case CCValAssign::Full: break;
405 case CCValAssign::SExt:
406 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
407 break;
408 case CCValAssign::ZExt:
409 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
410 break;
411 case CCValAssign::AExt:
412 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
413 break;
414 }
415
416 // Arguments that can be passed on register must be kept at RegsToPass
417 // vector
418 if (VA.isRegLoc()) {
419 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
420 } else {
421 assert(VA.isMemLoc());
422
423 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000424 StackPtr =
425 DAG.getCopyFromReg(Chain, dl,
426 (RegInfo->hasFP(MF) ?
427 SystemZ::R11D : SystemZ::R15D),
428 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000429
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000430 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
431 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
432 StackPtr,
433 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000434
435 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000436 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000437 }
438 }
439
440 // Transform all store nodes into one single node because all store nodes are
441 // independent of each other.
442 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000444 &MemOpChains[0], MemOpChains.size());
445
446 // Build a sequence of copy-to-reg nodes chained together with token chain and
447 // flag operands which copy the outgoing args into registers. The InFlag in
448 // necessary since all emited instructions must be stuck together.
449 SDValue InFlag;
450 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
451 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
452 RegsToPass[i].second, InFlag);
453 InFlag = Chain.getValue(1);
454 }
455
456 // If the callee is a GlobalAddress node (quite common, every direct call is)
457 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
458 // Likewise ExternalSymbol -> TargetExternalSymbol.
459 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
460 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
461 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
462 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
463
464 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000466 SmallVector<SDValue, 8> Ops;
467 Ops.push_back(Chain);
468 Ops.push_back(Callee);
469
470 // Add argument registers to the end of the list so that they are
471 // known live into the call.
472 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
473 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
474 RegsToPass[i].second.getValueType()));
475
476 if (InFlag.getNode())
477 Ops.push_back(InFlag);
478
479 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
480 InFlag = Chain.getValue(1);
481
482 // Create the CALLSEQ_END node.
483 Chain = DAG.getCALLSEQ_END(Chain,
484 DAG.getConstant(NumBytes, getPointerTy(), true),
485 DAG.getConstant(0, getPointerTy(), true),
486 InFlag);
487 InFlag = Chain.getValue(1);
488
489 // Handle result values, copying them out of physregs into vregs that we
490 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000491 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
492 DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000493}
494
Dan Gohman98ca4f22009-08-05 01:29:28 +0000495/// LowerCallResult - Lower the result values of a call into the
496/// appropriate copies out of appropriate physical registers.
497///
498SDValue
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000499SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000500 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000501 const SmallVectorImpl<ISD::InputArg>
502 &Ins,
503 DebugLoc dl, SelectionDAG &DAG,
504 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000505
506 // Assign locations to each value returned by this call.
507 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000508 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
Owen Andersone922c022009-07-22 00:24:57 +0000509 *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000510
Dan Gohman98ca4f22009-08-05 01:29:28 +0000511 CCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000512
513 // Copy all of the result registers out of their specified physreg.
514 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000515 CCValAssign &VA = RVLocs[i];
516
517 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
518 VA.getLocVT(), InFlag).getValue(1);
519 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000520 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000521
522 // If this is an 8/16/32-bit value, it is really passed promoted to 64
523 // bits. Insert an assert[sz]ext to capture this, then truncate to the
524 // right size.
525 if (VA.getLocInfo() == CCValAssign::SExt)
526 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
527 DAG.getValueType(VA.getValVT()));
528 else if (VA.getLocInfo() == CCValAssign::ZExt)
529 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
530 DAG.getValueType(VA.getValVT()));
531
532 if (VA.getLocInfo() != CCValAssign::Full)
533 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
534
Dan Gohman98ca4f22009-08-05 01:29:28 +0000535 InVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000536 }
537
Dan Gohman98ca4f22009-08-05 01:29:28 +0000538 return Chain;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000539}
540
541
Dan Gohman98ca4f22009-08-05 01:29:28 +0000542SDValue
543SystemZTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000544 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000545 const SmallVectorImpl<ISD::OutputArg> &Outs,
546 DebugLoc dl, SelectionDAG &DAG) {
547
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000548 // CCValAssign - represent the assignment of the return value to a location
549 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000550
551 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000552 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
553 RVLocs, *DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000554
Dan Gohman98ca4f22009-08-05 01:29:28 +0000555 // Analize return values.
556 CCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000557
558 // If this is the first return lowered for this function, add the regs to the
559 // liveout set for the function.
560 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
561 for (unsigned i = 0; i != RVLocs.size(); ++i)
562 if (RVLocs[i].isRegLoc())
563 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
564 }
565
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000566 SDValue Flag;
567
568 // Copy the result values into the output registers.
569 for (unsigned i = 0; i != RVLocs.size(); ++i) {
570 CCValAssign &VA = RVLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000571 SDValue ResValue = Outs[i].Val;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000572 assert(VA.isRegLoc() && "Can only return in registers!");
573
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000574 // If this is an 8/16/32-bit value, it is really should be passed promoted
575 // to 64 bits.
576 if (VA.getLocInfo() == CCValAssign::SExt)
577 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
578 else if (VA.getLocInfo() == CCValAssign::ZExt)
579 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
580 else if (VA.getLocInfo() == CCValAssign::AExt)
581 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
582
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000583 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000584
585 // Guarantee that all emitted copies are stuck together,
586 // avoiding something bad.
587 Flag = Chain.getValue(1);
588 }
589
590 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000592
593 // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000595}
596
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000597SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
598 ISD::CondCode CC, SDValue &SystemZCC,
599 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000600 // FIXME: Emit a test if RHS is zero
601
602 bool isUnsigned = false;
603 SystemZCC::CondCodes TCC;
604 switch (CC) {
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000605 default:
606 llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000607 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000608 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000609 TCC = SystemZCC::E;
610 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000611 case ISD::SETUEQ:
612 TCC = SystemZCC::NLH;
613 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000614 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000615 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000616 TCC = SystemZCC::NE;
617 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000618 case ISD::SETUNE:
619 TCC = SystemZCC::LH;
620 break;
621 case ISD::SETO:
622 TCC = SystemZCC::O;
623 break;
624 case ISD::SETUO:
625 TCC = SystemZCC::NO;
626 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000627 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000628 if (LHS.getValueType().isFloatingPoint()) {
629 TCC = SystemZCC::NH;
630 break;
631 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000632 isUnsigned = true; // FALLTHROUGH
633 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000634 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000635 TCC = SystemZCC::LE;
636 break;
637 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000638 if (LHS.getValueType().isFloatingPoint()) {
639 TCC = SystemZCC::NL;
640 break;
641 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000642 isUnsigned = true; // FALLTHROUGH
643 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000644 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000645 TCC = SystemZCC::HE;
646 break;
647 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000648 if (LHS.getValueType().isFloatingPoint()) {
649 TCC = SystemZCC::NLE;
650 break;
651 }
652 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000653 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000654 case ISD::SETOGT:
655 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000656 break;
657 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000658 if (LHS.getValueType().isFloatingPoint()) {
659 TCC = SystemZCC::NHE;
660 break;
661 }
662 isUnsigned = true; // FALLTHROUGH
663 case ISD::SETLT:
664 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000665 TCC = SystemZCC::L;
666 break;
667 }
668
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 SystemZCC = DAG.getConstant(TCC, MVT::i32);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000670
671 DebugLoc dl = LHS.getDebugLoc();
672 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
Dan Gohmanaa123222009-10-28 00:55:57 +0000673 dl, MVT::i64, LHS, RHS);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000674}
675
676
677SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
678 SDValue Chain = Op.getOperand(0);
679 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
680 SDValue LHS = Op.getOperand(2);
681 SDValue RHS = Op.getOperand(3);
682 SDValue Dest = Op.getOperand(4);
683 DebugLoc dl = Op.getDebugLoc();
684
685 SDValue SystemZCC;
686 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
687 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
688 Chain, Dest, SystemZCC, Flag);
689}
690
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000691SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
692 SDValue LHS = Op.getOperand(0);
693 SDValue RHS = Op.getOperand(1);
694 SDValue TrueV = Op.getOperand(2);
695 SDValue FalseV = Op.getOperand(3);
696 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
697 DebugLoc dl = Op.getDebugLoc();
698
699 SDValue SystemZCC;
700 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
701
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000703 SmallVector<SDValue, 4> Ops;
704 Ops.push_back(TrueV);
705 Ops.push_back(FalseV);
706 Ops.push_back(SystemZCC);
707 Ops.push_back(Flag);
708
709 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
710}
711
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000712SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
713 SelectionDAG &DAG) {
714 DebugLoc dl = Op.getDebugLoc();
715 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000716 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000717
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000718 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
719 bool ExtraLoadRequired =
720 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
721
722 SDValue Result;
723 if (!IsPic && !ExtraLoadRequired) {
724 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
725 Offset = 0;
726 } else {
727 unsigned char OpFlags = 0;
728 if (ExtraLoadRequired)
729 OpFlags = SystemZII::MO_GOTENT;
730
731 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
732 }
733
734 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
735 getPointerTy(), Result);
736
737 if (ExtraLoadRequired)
738 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
739 PseudoSourceValue::getGOT(), 0);
740
741 // If there was a non-zero offset that we didn't fold, create an explicit
742 // addition for it.
743 if (Offset != 0)
744 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
745 DAG.getConstant(Offset, getPointerTy()));
746
747 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000748}
749
Anton Korobeynikovae535672009-07-16 14:19:35 +0000750// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000751SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
752 SelectionDAG &DAG) {
753 DebugLoc dl = Op.getDebugLoc();
754 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
755 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
756
757 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
758}
759
Anton Korobeynikovae535672009-07-16 14:19:35 +0000760
761// FIXME: PIC here
762// FIXME: This is just dirty hack. We need to lower cpool properly
763SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
764 SelectionDAG &DAG) {
765 DebugLoc dl = Op.getDebugLoc();
766 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
767
768 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
769 CP->getAlignment(),
770 CP->getOffset());
771
772 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
773}
774
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000775const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
776 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000777 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000778 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000779 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
780 case SystemZISD::CMP: return "SystemZISD::CMP";
781 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000782 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000783 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000784 default: return NULL;
785 }
786}
787
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000788//===----------------------------------------------------------------------===//
789// Other Lowering Code
790//===----------------------------------------------------------------------===//
791
792MachineBasicBlock*
793SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +0000794 MachineBasicBlock *BB,
795 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000796 const SystemZInstrInfo &TII = *TM.getInstrInfo();
797 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000798 assert((MI->getOpcode() == SystemZ::Select32 ||
799 MI->getOpcode() == SystemZ::SelectF32 ||
800 MI->getOpcode() == SystemZ::Select64 ||
801 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000802 "Unexpected instr type to insert");
803
804 // To "insert" a SELECT instruction, we actually have to insert the diamond
805 // control-flow pattern. The incoming instruction knows the destination vreg
806 // to set, the condition code register to branch on, the true/false values to
807 // select between, and a branch opcode to use.
808 const BasicBlock *LLVM_BB = BB->getBasicBlock();
809 MachineFunction::iterator I = BB;
810 ++I;
811
812 // thisMBB:
813 // ...
814 // TrueVal = ...
815 // cmpTY ccX, r1, r2
816 // jCC copy1MBB
817 // fallthrough --> copy0MBB
818 MachineBasicBlock *thisMBB = BB;
819 MachineFunction *F = BB->getParent();
820 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
821 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
822 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
823 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
824 F->insert(I, copy0MBB);
825 F->insert(I, copy1MBB);
Evan Chengce319102009-09-19 09:51:03 +0000826 // Inform sdisel of the edge changes.
827 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
828 SE = BB->succ_end(); SI != SE; ++SI)
829 EM->insert(std::make_pair(*SI, copy1MBB));
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000830 // Update machine-CFG edges by transferring all successors of the current
831 // block to the new block which will contain the Phi node for the select.
832 copy1MBB->transferSuccessors(BB);
833 // Next, add the true and fallthrough blocks as its successors.
834 BB->addSuccessor(copy0MBB);
835 BB->addSuccessor(copy1MBB);
836
837 // copy0MBB:
838 // %FalseValue = ...
839 // # fallthrough to copy1MBB
840 BB = copy0MBB;
841
842 // Update machine-CFG edges
843 BB->addSuccessor(copy1MBB);
844
845 // copy1MBB:
846 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
847 // ...
848 BB = copy1MBB;
849 BuildMI(BB, dl, TII.get(SystemZ::PHI),
850 MI->getOperand(0).getReg())
851 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
852 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
853
854 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
855 return BB;
856}