blob: 38b4c301b7825bbff631d0c3265a11be415dd9e3 [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.
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +000047 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
48 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000049 addRegisterClass(MVT::v2i32,SystemZ::GR64PRegisterClass);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000050 addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000051
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000052 if (!UseSoftFloat) {
53 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
Anton Korobeynikov48e8b3c2009-07-16 14:15:24 +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
Anton Korobeynikovbf022172009-07-16 13:53:35 +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
Anton Korobeynikov85c5c3f2009-07-16 14:22:46 +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
Anton Korobeynikov85c5c3f2009-07-16 14:22:46 +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
85 setOperationAction(ISD::RET, MVT::Other, Custom);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000086
Anton Korobeynikov983d3a12009-07-16 14:07:24 +000087 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000088 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
89 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
90 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +000091 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
92 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
93 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
94 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +000095 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +000096 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Anton Korobeynikovc772c442009-07-16 14:08:15 +000097 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000098
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000099 setOperationAction(ISD::SDIV, MVT::i32, Expand);
100 setOperationAction(ISD::UDIV, MVT::i32, Expand);
101 setOperationAction(ISD::SDIV, MVT::i64, Expand);
102 setOperationAction(ISD::UDIV, MVT::i64, Expand);
103 setOperationAction(ISD::SREM, MVT::i32, Expand);
104 setOperationAction(ISD::UREM, MVT::i32, Expand);
105 setOperationAction(ISD::SREM, MVT::i64, Expand);
106 setOperationAction(ISD::UREM, MVT::i64, Expand);
107
Anton Korobeynikove37a37d2009-07-18 12:20:36 +0000108 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
109
Anton Korobeynikov0cca0692009-07-18 12:26:13 +0000110 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
111 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
112 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
113 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
114 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
115 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
116
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000117 // FIXME: Can we lower these 2 efficiently?
118 setOperationAction(ISD::SETCC, MVT::i32, Expand);
119 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000120 setOperationAction(ISD::SETCC, MVT::f32, Expand);
121 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000122 setOperationAction(ISD::SELECT, MVT::i32, Expand);
123 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000124 setOperationAction(ISD::SELECT, MVT::f32, Expand);
125 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000126 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
127 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000128 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
129 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000130
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +0000131 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
132 // unsigned.
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000133 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000134 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000135
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000136 // Lower some FP stuff
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000137 setOperationAction(ISD::FSIN, MVT::f32, Expand);
138 setOperationAction(ISD::FSIN, MVT::f64, Expand);
139 setOperationAction(ISD::FCOS, MVT::f32, Expand);
140 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Anton Korobeynikov6323a832009-07-18 13:44:25 +0000141 setOperationAction(ISD::FREM, MVT::f32, Expand);
142 setOperationAction(ISD::FREM, MVT::f64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000143
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000144 // We have only 64-bit bitconverts
Anton Korobeynikovbb8a0482009-07-16 14:30:29 +0000145 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
146 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000147
Anton Korobeynikova89430e2009-07-16 14:25:30 +0000148 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
149 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000150 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
151 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000152
153 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000154}
155
156SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
157 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000158 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
159 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000160 case ISD::CALL: return LowerCALL(Op, DAG);
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
172//===----------------------------------------------------------------------===//
173// Calling Convention Implementation
174//===----------------------------------------------------------------------===//
175
176#include "SystemZGenCallingConv.inc"
177
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000178SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
179 SelectionDAG &DAG) {
180 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
181 switch (CC) {
182 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000183 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000184 case CallingConv::C:
185 case CallingConv::Fast:
186 return LowerCCCArguments(Op, DAG);
187 }
188}
189
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000190SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
191 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
192 unsigned CallingConv = TheCall->getCallingConv();
193 switch (CallingConv) {
194 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000195 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000196 case CallingConv::Fast:
197 case CallingConv::C:
198 return LowerCCCCallTo(Op, DAG, CallingConv);
199 }
200}
201
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000202/// LowerCCCArguments - transform physical registers into virtual registers and
203/// generate load operations for arguments places on the stack.
204// FIXME: struct return stuff
205// FIXME: varargs
206SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
207 SelectionDAG &DAG) {
208 MachineFunction &MF = DAG.getMachineFunction();
209 MachineFrameInfo *MFI = MF.getFrameInfo();
210 MachineRegisterInfo &RegInfo = MF.getRegInfo();
211 SDValue Root = Op.getOperand(0);
212 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
213 unsigned CC = MF.getFunction()->getCallingConv();
214 DebugLoc dl = Op.getDebugLoc();
215
216 // Assign locations to all of the incoming arguments.
217 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersone922c022009-07-22 00:24:57 +0000218 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000219 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
220
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000221 if (isVarArg)
222 llvm_report_error("Varargs not supported yet");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000223
224 SmallVector<SDValue, 16> ArgValues;
225 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000226 SDValue ArgValue;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000227 CCValAssign &VA = ArgLocs[i];
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000228 MVT LocVT = VA.getLocVT();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000229 if (VA.isRegLoc()) {
230 // Arguments passed in registers
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000231 TargetRegisterClass *RC;
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000232 switch (LocVT.getSimpleVT()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000233 default:
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000234#ifndef NDEBUG
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000235 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000236 << LocVT.getSimpleVT()
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000237 << "\n";
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000238#endif
239 llvm_unreachable(0);
240 case MVT::i64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000241 RC = SystemZ::GR64RegisterClass;
242 break;
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000243 case MVT::f32:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000244 RC = SystemZ::FP32RegisterClass;
245 break;
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000246 case MVT::f64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000247 RC = SystemZ::FP64RegisterClass;
248 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000249 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000250
251 unsigned VReg = RegInfo.createVirtualRegister(RC);
252 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000253 ArgValue = DAG.getCopyFromReg(Root, dl, VReg, LocVT);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000254 } else {
255 // Sanity check
256 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000257
258 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000259 // Create the frame index object for this incoming parameter...
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000260 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000261 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000262
263 // Create the SelectionDAG nodes corresponding to a load
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000264 // from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000265 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000266 ArgValue = DAG.getLoad(LocVT, dl, Root, FIN,
267 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000268 }
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000269
270 // If this is an 8/16/32-bit value, it is really passed promoted to 64
271 // bits. Insert an assert[sz]ext to capture this, then truncate to the
272 // right size.
273 if (VA.getLocInfo() == CCValAssign::SExt)
274 ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
275 DAG.getValueType(VA.getValVT()));
276 else if (VA.getLocInfo() == CCValAssign::ZExt)
277 ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
278 DAG.getValueType(VA.getValVT()));
279
280 if (VA.getLocInfo() != CCValAssign::Full)
281 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
282
283 ArgValues.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000284 }
285
286 ArgValues.push_back(Root);
287
288 // Return the new list of results.
289 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
290 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
291}
292
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000293/// LowerCCCCallTo - functions arguments are copied from virtual regs to
294/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
295/// TODO: sret.
296SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
297 unsigned CC) {
298 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
299 SDValue Chain = TheCall->getChain();
300 SDValue Callee = TheCall->getCallee();
301 bool isVarArg = TheCall->isVarArg();
302 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000303 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000304
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000305 // Offset to first argument stack slot.
306 const unsigned FirstArgOffset = 160;
307
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000308 // Analyze operands of the call, assigning locations to each operand.
309 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersone922c022009-07-22 00:24:57 +0000310 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000311
312 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
313
314 // Get a count of how many bytes are to be pushed on the stack.
315 unsigned NumBytes = CCInfo.getNextStackOffset();
316
317 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
318 getPointerTy(), true));
319
320 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
321 SmallVector<SDValue, 12> MemOpChains;
322 SDValue StackPtr;
323
324 // Walk the register/memloc assignments, inserting copies/loads.
325 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
326 CCValAssign &VA = ArgLocs[i];
327
328 // Arguments start after the 5 first operands of ISD::CALL
329 SDValue Arg = TheCall->getArg(i);
330
331 // Promote the value if needed.
332 switch (VA.getLocInfo()) {
333 default: assert(0 && "Unknown loc info!");
334 case CCValAssign::Full: break;
335 case CCValAssign::SExt:
336 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
337 break;
338 case CCValAssign::ZExt:
339 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
340 break;
341 case CCValAssign::AExt:
342 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
343 break;
344 }
345
346 // Arguments that can be passed on register must be kept at RegsToPass
347 // vector
348 if (VA.isRegLoc()) {
349 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
350 } else {
351 assert(VA.isMemLoc());
352
353 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000354 StackPtr =
355 DAG.getCopyFromReg(Chain, dl,
356 (RegInfo->hasFP(MF) ?
357 SystemZ::R11D : SystemZ::R15D),
358 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000359
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000360 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
361 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
362 StackPtr,
363 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000364
365 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000366 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000367 }
368 }
369
370 // Transform all store nodes into one single node because all store nodes are
371 // independent of each other.
372 if (!MemOpChains.empty())
373 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
374 &MemOpChains[0], MemOpChains.size());
375
376 // Build a sequence of copy-to-reg nodes chained together with token chain and
377 // flag operands which copy the outgoing args into registers. The InFlag in
378 // necessary since all emited instructions must be stuck together.
379 SDValue InFlag;
380 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
381 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
382 RegsToPass[i].second, InFlag);
383 InFlag = Chain.getValue(1);
384 }
385
386 // If the callee is a GlobalAddress node (quite common, every direct call is)
387 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
388 // Likewise ExternalSymbol -> TargetExternalSymbol.
389 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
390 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
391 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
392 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
393
394 // Returns a chain & a flag for retval copy to use.
395 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
396 SmallVector<SDValue, 8> Ops;
397 Ops.push_back(Chain);
398 Ops.push_back(Callee);
399
400 // Add argument registers to the end of the list so that they are
401 // known live into the call.
402 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
403 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
404 RegsToPass[i].second.getValueType()));
405
406 if (InFlag.getNode())
407 Ops.push_back(InFlag);
408
409 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
410 InFlag = Chain.getValue(1);
411
412 // Create the CALLSEQ_END node.
413 Chain = DAG.getCALLSEQ_END(Chain,
414 DAG.getConstant(NumBytes, getPointerTy(), true),
415 DAG.getConstant(0, getPointerTy(), true),
416 InFlag);
417 InFlag = Chain.getValue(1);
418
419 // Handle result values, copying them out of physregs into vregs that we
420 // return.
421 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
422 Op.getResNo());
423}
424
425/// LowerCallResult - Lower the result values of an ISD::CALL into the
426/// appropriate copies out of appropriate physical registers. This assumes that
427/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
428/// being lowered. Returns a SDNode with the same number of values as the
429/// ISD::CALL.
430SDNode*
431SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
432 CallSDNode *TheCall,
433 unsigned CallingConv,
434 SelectionDAG &DAG) {
435 bool isVarArg = TheCall->isVarArg();
436 DebugLoc dl = TheCall->getDebugLoc();
437
438 // Assign locations to each value returned by this call.
439 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000440 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs,
Owen Andersone922c022009-07-22 00:24:57 +0000441 *DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000442
443 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
444 SmallVector<SDValue, 8> ResultVals;
445
446 // Copy all of the result registers out of their specified physreg.
447 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000448 CCValAssign &VA = RVLocs[i];
449
450 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
451 VA.getLocVT(), InFlag).getValue(1);
452 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000453 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000454
455 // If this is an 8/16/32-bit value, it is really passed promoted to 64
456 // bits. Insert an assert[sz]ext to capture this, then truncate to the
457 // right size.
458 if (VA.getLocInfo() == CCValAssign::SExt)
459 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
460 DAG.getValueType(VA.getValVT()));
461 else if (VA.getLocInfo() == CCValAssign::ZExt)
462 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
463 DAG.getValueType(VA.getValVT()));
464
465 if (VA.getLocInfo() != CCValAssign::Full)
466 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
467
468 ResultVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000469 }
470
471 ResultVals.push_back(Chain);
472
473 // Merge everything together with a MERGE_VALUES node.
474 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
475 &ResultVals[0], ResultVals.size()).getNode();
476}
477
478
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000479SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
480 // CCValAssign - represent the assignment of the return value to a location
481 SmallVector<CCValAssign, 16> RVLocs;
482 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
483 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
484 DebugLoc dl = Op.getDebugLoc();
485
486 // CCState - Info about the registers and stack slot.
Owen Andersone922c022009-07-22 00:24:57 +0000487 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, *DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000488
489 // Analize return values of ISD::RET
490 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
491
492 // If this is the first return lowered for this function, add the regs to the
493 // liveout set for the function.
494 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
495 for (unsigned i = 0; i != RVLocs.size(); ++i)
496 if (RVLocs[i].isRegLoc())
497 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
498 }
499
500 // The chain is always operand #0
501 SDValue Chain = Op.getOperand(0);
502 SDValue Flag;
503
504 // Copy the result values into the output registers.
505 for (unsigned i = 0; i != RVLocs.size(); ++i) {
506 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000507 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000508 assert(VA.isRegLoc() && "Can only return in registers!");
509
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000510 // If this is an 8/16/32-bit value, it is really should be passed promoted
511 // to 64 bits.
512 if (VA.getLocInfo() == CCValAssign::SExt)
513 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
514 else if (VA.getLocInfo() == CCValAssign::ZExt)
515 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
516 else if (VA.getLocInfo() == CCValAssign::AExt)
517 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
518
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000519 // ISD::RET => ret chain, (regnum1,val1), ...
520 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000521 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000522
523 // Guarantee that all emitted copies are stuck together,
524 // avoiding something bad.
525 Flag = Chain.getValue(1);
526 }
527
528 if (Flag.getNode())
529 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
530
531 // Return Void
532 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
533}
534
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000535SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
536 ISD::CondCode CC, SDValue &SystemZCC,
537 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000538 // FIXME: Emit a test if RHS is zero
539
540 bool isUnsigned = false;
541 SystemZCC::CondCodes TCC;
542 switch (CC) {
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000543 default:
544 llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000545 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000546 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000547 TCC = SystemZCC::E;
548 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000549 case ISD::SETUEQ:
550 TCC = SystemZCC::NLH;
551 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000552 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000553 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000554 TCC = SystemZCC::NE;
555 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000556 case ISD::SETUNE:
557 TCC = SystemZCC::LH;
558 break;
559 case ISD::SETO:
560 TCC = SystemZCC::O;
561 break;
562 case ISD::SETUO:
563 TCC = SystemZCC::NO;
564 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000565 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000566 if (LHS.getValueType().isFloatingPoint()) {
567 TCC = SystemZCC::NH;
568 break;
569 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000570 isUnsigned = true; // FALLTHROUGH
571 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000572 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000573 TCC = SystemZCC::LE;
574 break;
575 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000576 if (LHS.getValueType().isFloatingPoint()) {
577 TCC = SystemZCC::NL;
578 break;
579 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000580 isUnsigned = true; // FALLTHROUGH
581 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000582 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000583 TCC = SystemZCC::HE;
584 break;
585 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000586 if (LHS.getValueType().isFloatingPoint()) {
587 TCC = SystemZCC::NLE;
588 break;
589 }
590 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000591 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000592 case ISD::SETOGT:
593 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000594 break;
595 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000596 if (LHS.getValueType().isFloatingPoint()) {
597 TCC = SystemZCC::NHE;
598 break;
599 }
600 isUnsigned = true; // FALLTHROUGH
601 case ISD::SETLT:
602 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000603 TCC = SystemZCC::L;
604 break;
605 }
606
607 SystemZCC = DAG.getConstant(TCC, MVT::i32);
608
609 DebugLoc dl = LHS.getDebugLoc();
610 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
611 dl, MVT::Flag, LHS, RHS);
612}
613
614
615SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
616 SDValue Chain = Op.getOperand(0);
617 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
618 SDValue LHS = Op.getOperand(2);
619 SDValue RHS = Op.getOperand(3);
620 SDValue Dest = Op.getOperand(4);
621 DebugLoc dl = Op.getDebugLoc();
622
623 SDValue SystemZCC;
624 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
625 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
626 Chain, Dest, SystemZCC, Flag);
627}
628
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000629SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
630 SDValue LHS = Op.getOperand(0);
631 SDValue RHS = Op.getOperand(1);
632 SDValue TrueV = Op.getOperand(2);
633 SDValue FalseV = Op.getOperand(3);
634 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
635 DebugLoc dl = Op.getDebugLoc();
636
637 SDValue SystemZCC;
638 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
639
640 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
641 SmallVector<SDValue, 4> Ops;
642 Ops.push_back(TrueV);
643 Ops.push_back(FalseV);
644 Ops.push_back(SystemZCC);
645 Ops.push_back(Flag);
646
647 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
648}
649
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000650SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
651 SelectionDAG &DAG) {
652 DebugLoc dl = Op.getDebugLoc();
653 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000654 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000655
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000656 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
657 bool ExtraLoadRequired =
658 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
659
660 SDValue Result;
661 if (!IsPic && !ExtraLoadRequired) {
662 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
663 Offset = 0;
664 } else {
665 unsigned char OpFlags = 0;
666 if (ExtraLoadRequired)
667 OpFlags = SystemZII::MO_GOTENT;
668
669 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
670 }
671
672 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
673 getPointerTy(), Result);
674
675 if (ExtraLoadRequired)
676 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
677 PseudoSourceValue::getGOT(), 0);
678
679 // If there was a non-zero offset that we didn't fold, create an explicit
680 // addition for it.
681 if (Offset != 0)
682 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
683 DAG.getConstant(Offset, getPointerTy()));
684
685 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000686}
687
Anton Korobeynikovae535672009-07-16 14:19:35 +0000688// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000689SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
690 SelectionDAG &DAG) {
691 DebugLoc dl = Op.getDebugLoc();
692 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
693 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
694
695 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
696}
697
Anton Korobeynikovae535672009-07-16 14:19:35 +0000698
699// FIXME: PIC here
700// FIXME: This is just dirty hack. We need to lower cpool properly
701SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
702 SelectionDAG &DAG) {
703 DebugLoc dl = Op.getDebugLoc();
704 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
705
706 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
707 CP->getAlignment(),
708 CP->getOffset());
709
710 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
711}
712
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000713const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
714 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000715 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000716 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000717 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
718 case SystemZISD::CMP: return "SystemZISD::CMP";
719 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000720 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000721 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000722 default: return NULL;
723 }
724}
725
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000726//===----------------------------------------------------------------------===//
727// Other Lowering Code
728//===----------------------------------------------------------------------===//
729
730MachineBasicBlock*
731SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
732 MachineBasicBlock *BB) const {
733 const SystemZInstrInfo &TII = *TM.getInstrInfo();
734 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000735 assert((MI->getOpcode() == SystemZ::Select32 ||
736 MI->getOpcode() == SystemZ::SelectF32 ||
737 MI->getOpcode() == SystemZ::Select64 ||
738 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000739 "Unexpected instr type to insert");
740
741 // To "insert" a SELECT instruction, we actually have to insert the diamond
742 // control-flow pattern. The incoming instruction knows the destination vreg
743 // to set, the condition code register to branch on, the true/false values to
744 // select between, and a branch opcode to use.
745 const BasicBlock *LLVM_BB = BB->getBasicBlock();
746 MachineFunction::iterator I = BB;
747 ++I;
748
749 // thisMBB:
750 // ...
751 // TrueVal = ...
752 // cmpTY ccX, r1, r2
753 // jCC copy1MBB
754 // fallthrough --> copy0MBB
755 MachineBasicBlock *thisMBB = BB;
756 MachineFunction *F = BB->getParent();
757 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
758 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
759 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
760 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
761 F->insert(I, copy0MBB);
762 F->insert(I, copy1MBB);
763 // Update machine-CFG edges by transferring all successors of the current
764 // block to the new block which will contain the Phi node for the select.
765 copy1MBB->transferSuccessors(BB);
766 // Next, add the true and fallthrough blocks as its successors.
767 BB->addSuccessor(copy0MBB);
768 BB->addSuccessor(copy1MBB);
769
770 // copy0MBB:
771 // %FalseValue = ...
772 // # fallthrough to copy1MBB
773 BB = copy0MBB;
774
775 // Update machine-CFG edges
776 BB->addSuccessor(copy1MBB);
777
778 // copy1MBB:
779 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
780 // ...
781 BB = copy1MBB;
782 BuildMI(BB, dl, TII.get(SystemZ::PHI),
783 MI->getOperand(0).getReg())
784 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
785 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
786
787 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
788 return BB;
789}