blob: 279e22b653310f6d4e3500b86472cf95fda0d4f0 [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"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000037#include "llvm/ADT/VectorExtras.h"
38using namespace llvm;
39
40SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
Chris Lattnerf0144122009-07-28 03:13:23 +000041 TargetLowering(tm, new TargetLoweringObjectFileELF()),
42 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
Anton Korobeynikov4403b932009-07-16 13:27:25 +000043
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +000044 RegInfo = TM.getRegisterInfo();
45
Anton Korobeynikov4403b932009-07-16 13:27:25 +000046 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +000047 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
48 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
49 addRegisterClass(MVT::v2i32,SystemZ::GR64PRegisterClass);
50 addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000051
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000052 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +000053 addRegisterClass(MVT::f32, SystemZ::FP32RegisterClass);
54 addRegisterClass(MVT::f64, SystemZ::FP64RegisterClass);
Anton Korobeynikov1ada84d2009-07-16 14:24:16 +000055
56 addLegalFPImmediate(APFloat(+0.0)); // lzer
57 addLegalFPImmediate(APFloat(+0.0f)); // lzdr
58 addLegalFPImmediate(APFloat(-0.0)); // lzer + lner
59 addLegalFPImmediate(APFloat(-0.0f)); // lzdr + lndr
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000060 }
61
Anton Korobeynikov4403b932009-07-16 13:27:25 +000062 // Compute derived properties from the register classes
63 computeRegisterProperties();
64
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000065 // Set shifts properties
Owen Anderson825b72b2009-08-11 20:47:22 +000066 setShiftAmountType(MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000067
Anton Korobeynikov4403b932009-07-16 13:27:25 +000068 // Provide all sorts of operation actions
Owen Anderson825b72b2009-08-11 20:47:22 +000069 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
70 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
71 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000072
Owen Anderson825b72b2009-08-11 20:47:22 +000073 setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Expand);
74 setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Expand);
75 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Anton Korobeynikov299dc782009-07-16 14:22:30 +000076
Owen Anderson825b72b2009-08-11 20:47:22 +000077 setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Expand);
78 setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Expand);
79 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
Anton Korobeynikov23eff5c2009-07-16 14:20:08 +000080
Anton Korobeynikove0167c12009-07-16 13:35:30 +000081 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000082 setSchedulingPreference(SchedulingForLatency);
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
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +0000129 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
130 // unsigned.
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 setOperationAction(ISD::MULHS, MVT::i64, Expand);
132 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000133
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000134 // Lower some FP stuff
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setOperationAction(ISD::FSIN, MVT::f32, Expand);
136 setOperationAction(ISD::FSIN, MVT::f64, Expand);
137 setOperationAction(ISD::FCOS, MVT::f32, Expand);
138 setOperationAction(ISD::FCOS, MVT::f64, Expand);
139 setOperationAction(ISD::FREM, MVT::f32, Expand);
140 setOperationAction(ISD::FREM, MVT::f64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000141
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000142 // We have only 64-bit bitconverts
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
144 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000145
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
147 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
148 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
149 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000150
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000152}
153
154SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
155 switch (Op.getOpcode()) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000156 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000157 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000158 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000159 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000160 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000161 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000162 llvm_unreachable("Should not custom lower this!");
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000163 return SDValue();
164 }
165}
166
167//===----------------------------------------------------------------------===//
168// Calling Convention Implementation
169//===----------------------------------------------------------------------===//
170
171#include "SystemZGenCallingConv.inc"
172
Dan Gohman98ca4f22009-08-05 01:29:28 +0000173SDValue
174SystemZTargetLowering::LowerFormalArguments(SDValue Chain,
175 unsigned CallConv,
176 bool isVarArg,
177 const SmallVectorImpl<ISD::InputArg>
178 &Ins,
179 DebugLoc dl,
180 SelectionDAG &DAG,
181 SmallVectorImpl<SDValue> &InVals) {
182
183 switch (CallConv) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000184 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000185 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000186 case CallingConv::C:
187 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000188 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000189 }
190}
191
Dan Gohman98ca4f22009-08-05 01:29:28 +0000192SDValue
193SystemZTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
194 unsigned CallConv, bool isVarArg,
195 bool isTailCall,
196 const SmallVectorImpl<ISD::OutputArg> &Outs,
197 const SmallVectorImpl<ISD::InputArg> &Ins,
198 DebugLoc dl, SelectionDAG &DAG,
199 SmallVectorImpl<SDValue> &InVals) {
200
201 switch (CallConv) {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000202 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000203 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000204 case CallingConv::Fast:
205 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000206 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
207 Outs, Ins, dl, DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000208 }
209}
210
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000211/// LowerCCCArguments - transform physical registers into virtual registers and
212/// generate load operations for arguments places on the stack.
213// FIXME: struct return stuff
214// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000215SDValue
216SystemZTargetLowering::LowerCCCArguments(SDValue Chain,
217 unsigned CallConv,
218 bool isVarArg,
219 const SmallVectorImpl<ISD::InputArg>
220 &Ins,
221 DebugLoc dl,
222 SelectionDAG &DAG,
223 SmallVectorImpl<SDValue> &InVals) {
224
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000225 MachineFunction &MF = DAG.getMachineFunction();
226 MachineFrameInfo *MFI = MF.getFrameInfo();
227 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000228
229 // Assign locations to all of the incoming arguments.
230 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000231 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
232 ArgLocs, *DAG.getContext());
233 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000234
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000235 if (isVarArg)
236 llvm_report_error("Varargs not supported yet");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000237
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000238 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000239 SDValue ArgValue;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000240 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +0000241 EVT LocVT = VA.getLocVT();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000242 if (VA.isRegLoc()) {
243 // Arguments passed in registers
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000244 TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 switch (LocVT.getSimpleVT().SimpleTy) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000246 default:
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000247#ifndef NDEBUG
Dan Gohman98ca4f22009-08-05 01:29:28 +0000248 cerr << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 << LocVT.getSimpleVT().SimpleTy
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000250 << "\n";
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000251#endif
252 llvm_unreachable(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 case MVT::i64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000254 RC = SystemZ::GR64RegisterClass;
255 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 case MVT::f32:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000257 RC = SystemZ::FP32RegisterClass;
258 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 case MVT::f64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000260 RC = SystemZ::FP64RegisterClass;
261 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000262 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000263
264 unsigned VReg = RegInfo.createVirtualRegister(RC);
265 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000266 ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, LocVT);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000267 } else {
268 // Sanity check
269 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000270
271 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000272 // Create the frame index object for this incoming parameter...
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000273 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000274 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000275
276 // Create the SelectionDAG nodes corresponding to a load
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000277 // from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000278 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000279 ArgValue = DAG.getLoad(LocVT, dl, Chain, FIN,
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000280 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000281 }
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000282
283 // If this is an 8/16/32-bit value, it is really passed promoted to 64
284 // bits. Insert an assert[sz]ext to capture this, then truncate to the
285 // right size.
286 if (VA.getLocInfo() == CCValAssign::SExt)
287 ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
288 DAG.getValueType(VA.getValVT()));
289 else if (VA.getLocInfo() == CCValAssign::ZExt)
290 ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
291 DAG.getValueType(VA.getValVT()));
292
293 if (VA.getLocInfo() != CCValAssign::Full)
294 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
295
Dan Gohman98ca4f22009-08-05 01:29:28 +0000296 InVals.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000297 }
298
Dan Gohman98ca4f22009-08-05 01:29:28 +0000299 return Chain;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000300}
301
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000302/// LowerCCCCallTo - functions arguments are copied from virtual regs to
303/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
304/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000305SDValue
306SystemZTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
307 unsigned CallConv, bool isVarArg,
308 bool isTailCall,
309 const SmallVectorImpl<ISD::OutputArg>
310 &Outs,
311 const SmallVectorImpl<ISD::InputArg> &Ins,
312 DebugLoc dl, SelectionDAG &DAG,
313 SmallVectorImpl<SDValue> &InVals) {
314
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000315 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000316
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000317 // Offset to first argument stack slot.
318 const unsigned FirstArgOffset = 160;
319
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000320 // Analyze operands of the call, assigning locations to each operand.
321 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000322 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
323 ArgLocs, *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000324
Dan Gohman98ca4f22009-08-05 01:29:28 +0000325 CCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000326
327 // Get a count of how many bytes are to be pushed on the stack.
328 unsigned NumBytes = CCInfo.getNextStackOffset();
329
330 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
331 getPointerTy(), true));
332
333 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
334 SmallVector<SDValue, 12> MemOpChains;
335 SDValue StackPtr;
336
337 // Walk the register/memloc assignments, inserting copies/loads.
338 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
339 CCValAssign &VA = ArgLocs[i];
340
Dan Gohman98ca4f22009-08-05 01:29:28 +0000341 SDValue Arg = Outs[i].Val;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000342
343 // Promote the value if needed.
344 switch (VA.getLocInfo()) {
345 default: assert(0 && "Unknown loc info!");
346 case CCValAssign::Full: break;
347 case CCValAssign::SExt:
348 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
349 break;
350 case CCValAssign::ZExt:
351 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
352 break;
353 case CCValAssign::AExt:
354 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
355 break;
356 }
357
358 // Arguments that can be passed on register must be kept at RegsToPass
359 // vector
360 if (VA.isRegLoc()) {
361 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
362 } else {
363 assert(VA.isMemLoc());
364
365 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000366 StackPtr =
367 DAG.getCopyFromReg(Chain, dl,
368 (RegInfo->hasFP(MF) ?
369 SystemZ::R11D : SystemZ::R15D),
370 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000371
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000372 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
373 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
374 StackPtr,
375 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000376
377 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000378 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000379 }
380 }
381
382 // Transform all store nodes into one single node because all store nodes are
383 // independent of each other.
384 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000386 &MemOpChains[0], MemOpChains.size());
387
388 // Build a sequence of copy-to-reg nodes chained together with token chain and
389 // flag operands which copy the outgoing args into registers. The InFlag in
390 // necessary since all emited instructions must be stuck together.
391 SDValue InFlag;
392 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
393 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
394 RegsToPass[i].second, InFlag);
395 InFlag = Chain.getValue(1);
396 }
397
398 // If the callee is a GlobalAddress node (quite common, every direct call is)
399 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
400 // Likewise ExternalSymbol -> TargetExternalSymbol.
401 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
402 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
403 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
404 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
405
406 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000408 SmallVector<SDValue, 8> Ops;
409 Ops.push_back(Chain);
410 Ops.push_back(Callee);
411
412 // Add argument registers to the end of the list so that they are
413 // known live into the call.
414 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
415 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
416 RegsToPass[i].second.getValueType()));
417
418 if (InFlag.getNode())
419 Ops.push_back(InFlag);
420
421 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
422 InFlag = Chain.getValue(1);
423
424 // Create the CALLSEQ_END node.
425 Chain = DAG.getCALLSEQ_END(Chain,
426 DAG.getConstant(NumBytes, getPointerTy(), true),
427 DAG.getConstant(0, getPointerTy(), true),
428 InFlag);
429 InFlag = Chain.getValue(1);
430
431 // Handle result values, copying them out of physregs into vregs that we
432 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000433 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
434 DAG, InVals);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000435}
436
Dan Gohman98ca4f22009-08-05 01:29:28 +0000437/// LowerCallResult - Lower the result values of a call into the
438/// appropriate copies out of appropriate physical registers.
439///
440SDValue
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000441SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000442 unsigned CallConv, bool isVarArg,
443 const SmallVectorImpl<ISD::InputArg>
444 &Ins,
445 DebugLoc dl, SelectionDAG &DAG,
446 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000447
448 // Assign locations to each value returned by this call.
449 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000450 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
Owen Andersone922c022009-07-22 00:24:57 +0000451 *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000452
Dan Gohman98ca4f22009-08-05 01:29:28 +0000453 CCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000454
455 // Copy all of the result registers out of their specified physreg.
456 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000457 CCValAssign &VA = RVLocs[i];
458
459 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
460 VA.getLocVT(), InFlag).getValue(1);
461 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000462 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000463
464 // If this is an 8/16/32-bit value, it is really passed promoted to 64
465 // bits. Insert an assert[sz]ext to capture this, then truncate to the
466 // right size.
467 if (VA.getLocInfo() == CCValAssign::SExt)
468 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
469 DAG.getValueType(VA.getValVT()));
470 else if (VA.getLocInfo() == CCValAssign::ZExt)
471 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
472 DAG.getValueType(VA.getValVT()));
473
474 if (VA.getLocInfo() != CCValAssign::Full)
475 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
476
Dan Gohman98ca4f22009-08-05 01:29:28 +0000477 InVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000478 }
479
Dan Gohman98ca4f22009-08-05 01:29:28 +0000480 return Chain;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000481}
482
483
Dan Gohman98ca4f22009-08-05 01:29:28 +0000484SDValue
485SystemZTargetLowering::LowerReturn(SDValue Chain,
486 unsigned CallConv, bool isVarArg,
487 const SmallVectorImpl<ISD::OutputArg> &Outs,
488 DebugLoc dl, SelectionDAG &DAG) {
489
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000490 // CCValAssign - represent the assignment of the return value to a location
491 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000492
493 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000494 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
495 RVLocs, *DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000496
Dan Gohman98ca4f22009-08-05 01:29:28 +0000497 // Analize return values.
498 CCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000499
500 // If this is the first return lowered for this function, add the regs to the
501 // liveout set for the function.
502 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
503 for (unsigned i = 0; i != RVLocs.size(); ++i)
504 if (RVLocs[i].isRegLoc())
505 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
506 }
507
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000508 SDValue Flag;
509
510 // Copy the result values into the output registers.
511 for (unsigned i = 0; i != RVLocs.size(); ++i) {
512 CCValAssign &VA = RVLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000513 SDValue ResValue = Outs[i].Val;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000514 assert(VA.isRegLoc() && "Can only return in registers!");
515
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000516 // If this is an 8/16/32-bit value, it is really should be passed promoted
517 // to 64 bits.
518 if (VA.getLocInfo() == CCValAssign::SExt)
519 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
520 else if (VA.getLocInfo() == CCValAssign::ZExt)
521 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
522 else if (VA.getLocInfo() == CCValAssign::AExt)
523 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
524
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000525 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000526
527 // Guarantee that all emitted copies are stuck together,
528 // avoiding something bad.
529 Flag = Chain.getValue(1);
530 }
531
532 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000534
535 // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000537}
538
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000539SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
540 ISD::CondCode CC, SDValue &SystemZCC,
541 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000542 // FIXME: Emit a test if RHS is zero
543
544 bool isUnsigned = false;
545 SystemZCC::CondCodes TCC;
546 switch (CC) {
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000547 default:
548 llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000549 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000550 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000551 TCC = SystemZCC::E;
552 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000553 case ISD::SETUEQ:
554 TCC = SystemZCC::NLH;
555 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000556 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000557 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000558 TCC = SystemZCC::NE;
559 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000560 case ISD::SETUNE:
561 TCC = SystemZCC::LH;
562 break;
563 case ISD::SETO:
564 TCC = SystemZCC::O;
565 break;
566 case ISD::SETUO:
567 TCC = SystemZCC::NO;
568 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000569 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000570 if (LHS.getValueType().isFloatingPoint()) {
571 TCC = SystemZCC::NH;
572 break;
573 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000574 isUnsigned = true; // FALLTHROUGH
575 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000576 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000577 TCC = SystemZCC::LE;
578 break;
579 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000580 if (LHS.getValueType().isFloatingPoint()) {
581 TCC = SystemZCC::NL;
582 break;
583 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000584 isUnsigned = true; // FALLTHROUGH
585 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000586 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000587 TCC = SystemZCC::HE;
588 break;
589 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000590 if (LHS.getValueType().isFloatingPoint()) {
591 TCC = SystemZCC::NLE;
592 break;
593 }
594 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000595 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000596 case ISD::SETOGT:
597 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000598 break;
599 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000600 if (LHS.getValueType().isFloatingPoint()) {
601 TCC = SystemZCC::NHE;
602 break;
603 }
604 isUnsigned = true; // FALLTHROUGH
605 case ISD::SETLT:
606 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000607 TCC = SystemZCC::L;
608 break;
609 }
610
Owen Anderson825b72b2009-08-11 20:47:22 +0000611 SystemZCC = DAG.getConstant(TCC, MVT::i32);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000612
613 DebugLoc dl = LHS.getDebugLoc();
614 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 dl, MVT::Flag, LHS, RHS);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000616}
617
618
619SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
620 SDValue Chain = Op.getOperand(0);
621 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
622 SDValue LHS = Op.getOperand(2);
623 SDValue RHS = Op.getOperand(3);
624 SDValue Dest = Op.getOperand(4);
625 DebugLoc dl = Op.getDebugLoc();
626
627 SDValue SystemZCC;
628 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
629 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
630 Chain, Dest, SystemZCC, Flag);
631}
632
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000633SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
634 SDValue LHS = Op.getOperand(0);
635 SDValue RHS = Op.getOperand(1);
636 SDValue TrueV = Op.getOperand(2);
637 SDValue FalseV = Op.getOperand(3);
638 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
639 DebugLoc dl = Op.getDebugLoc();
640
641 SDValue SystemZCC;
642 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
643
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000645 SmallVector<SDValue, 4> Ops;
646 Ops.push_back(TrueV);
647 Ops.push_back(FalseV);
648 Ops.push_back(SystemZCC);
649 Ops.push_back(Flag);
650
651 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
652}
653
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000654SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
655 SelectionDAG &DAG) {
656 DebugLoc dl = Op.getDebugLoc();
657 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000658 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000659
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000660 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
661 bool ExtraLoadRequired =
662 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
663
664 SDValue Result;
665 if (!IsPic && !ExtraLoadRequired) {
666 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
667 Offset = 0;
668 } else {
669 unsigned char OpFlags = 0;
670 if (ExtraLoadRequired)
671 OpFlags = SystemZII::MO_GOTENT;
672
673 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
674 }
675
676 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
677 getPointerTy(), Result);
678
679 if (ExtraLoadRequired)
680 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
681 PseudoSourceValue::getGOT(), 0);
682
683 // If there was a non-zero offset that we didn't fold, create an explicit
684 // addition for it.
685 if (Offset != 0)
686 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
687 DAG.getConstant(Offset, getPointerTy()));
688
689 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000690}
691
Anton Korobeynikovae535672009-07-16 14:19:35 +0000692// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000693SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
694 SelectionDAG &DAG) {
695 DebugLoc dl = Op.getDebugLoc();
696 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
697 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
698
699 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
700}
701
Anton Korobeynikovae535672009-07-16 14:19:35 +0000702
703// FIXME: PIC here
704// FIXME: This is just dirty hack. We need to lower cpool properly
705SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
706 SelectionDAG &DAG) {
707 DebugLoc dl = Op.getDebugLoc();
708 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
709
710 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
711 CP->getAlignment(),
712 CP->getOffset());
713
714 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
715}
716
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000717const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
718 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000719 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000720 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000721 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
722 case SystemZISD::CMP: return "SystemZISD::CMP";
723 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000724 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000725 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000726 default: return NULL;
727 }
728}
729
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000730//===----------------------------------------------------------------------===//
731// Other Lowering Code
732//===----------------------------------------------------------------------===//
733
734MachineBasicBlock*
735SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
736 MachineBasicBlock *BB) const {
737 const SystemZInstrInfo &TII = *TM.getInstrInfo();
738 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000739 assert((MI->getOpcode() == SystemZ::Select32 ||
740 MI->getOpcode() == SystemZ::SelectF32 ||
741 MI->getOpcode() == SystemZ::Select64 ||
742 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000743 "Unexpected instr type to insert");
744
745 // To "insert" a SELECT instruction, we actually have to insert the diamond
746 // control-flow pattern. The incoming instruction knows the destination vreg
747 // to set, the condition code register to branch on, the true/false values to
748 // select between, and a branch opcode to use.
749 const BasicBlock *LLVM_BB = BB->getBasicBlock();
750 MachineFunction::iterator I = BB;
751 ++I;
752
753 // thisMBB:
754 // ...
755 // TrueVal = ...
756 // cmpTY ccX, r1, r2
757 // jCC copy1MBB
758 // fallthrough --> copy0MBB
759 MachineBasicBlock *thisMBB = BB;
760 MachineFunction *F = BB->getParent();
761 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
762 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
763 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
764 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
765 F->insert(I, copy0MBB);
766 F->insert(I, copy1MBB);
767 // Update machine-CFG edges by transferring all successors of the current
768 // block to the new block which will contain the Phi node for the select.
769 copy1MBB->transferSuccessors(BB);
770 // Next, add the true and fallthrough blocks as its successors.
771 BB->addSuccessor(copy0MBB);
772 BB->addSuccessor(copy1MBB);
773
774 // copy0MBB:
775 // %FalseValue = ...
776 // # fallthrough to copy1MBB
777 BB = copy0MBB;
778
779 // Update machine-CFG edges
780 BB->addSuccessor(copy1MBB);
781
782 // copy1MBB:
783 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
784 // ...
785 BB = copy1MBB;
786 BuildMI(BB, dl, TII.get(SystemZ::PHI),
787 MI->getOperand(0).getReg())
788 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
789 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
790
791 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
792 return BB;
793}