blob: 9b9b56ea5e29646629110da0b12f9ab4e3f85e43 [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"
34#include "llvm/Support/Debug.h"
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000035#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000036#include "llvm/ADT/VectorExtras.h"
37using namespace llvm;
38
39SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
40 TargetLowering(tm), Subtarget(*tm.getSubtargetImpl()), TM(tm) {
41
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +000042 RegInfo = TM.getRegisterInfo();
43
Anton Korobeynikov4403b932009-07-16 13:27:25 +000044 // Set up the register classes.
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +000045 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
46 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000047 addRegisterClass(MVT::v2i32,SystemZ::GR64PRegisterClass);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000048 addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000049
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000050 if (!UseSoftFloat) {
51 addRegisterClass(MVT::f32, SystemZ::FP32RegisterClass);
52 addRegisterClass(MVT::f64, SystemZ::FP64RegisterClass);
Anton Korobeynikov1ada84d2009-07-16 14:24:16 +000053
54 addLegalFPImmediate(APFloat(+0.0)); // lzer
55 addLegalFPImmediate(APFloat(+0.0f)); // lzdr
56 addLegalFPImmediate(APFloat(-0.0)); // lzer + lner
57 addLegalFPImmediate(APFloat(-0.0f)); // lzdr + lndr
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000058 }
59
Anton Korobeynikov4403b932009-07-16 13:27:25 +000060 // Compute derived properties from the register classes
61 computeRegisterProperties();
62
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000063 // Set shifts properties
Anton Korobeynikov48e8b3c2009-07-16 14:15:24 +000064 setShiftAmountType(MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000065
Anton Korobeynikov4403b932009-07-16 13:27:25 +000066 // Provide all sorts of operation actions
Anton Korobeynikovbf022172009-07-16 13:53:35 +000067 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
68 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
69 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000070
Anton Korobeynikov85c5c3f2009-07-16 14:22:46 +000071 setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Expand);
72 setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Expand);
73 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Anton Korobeynikov299dc782009-07-16 14:22:30 +000074
Anton Korobeynikov85c5c3f2009-07-16 14:22:46 +000075 setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Expand);
76 setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Expand);
77 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
Anton Korobeynikov23eff5c2009-07-16 14:20:08 +000078
Anton Korobeynikove0167c12009-07-16 13:35:30 +000079 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000080 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov159ac632009-07-16 14:28:46 +000081 setBooleanContents(ZeroOrOneBooleanContent);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000082
83 setOperationAction(ISD::RET, MVT::Other, Custom);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000084
Anton Korobeynikov983d3a12009-07-16 14:07:24 +000085 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000086 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
87 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
88 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +000089 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);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +000093 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +000094 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Anton Korobeynikovc772c442009-07-16 14:08:15 +000095 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000096
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +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);
105
Anton Korobeynikove37a37d2009-07-18 12:20:36 +0000106 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
107
Anton Korobeynikov0cca0692009-07-18 12:26:13 +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);
114
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000115 // FIXME: Can we lower these 2 efficiently?
116 setOperationAction(ISD::SETCC, MVT::i32, Expand);
117 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000118 setOperationAction(ISD::SETCC, MVT::f32, Expand);
119 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000120 setOperationAction(ISD::SELECT, MVT::i32, Expand);
121 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000122 setOperationAction(ISD::SELECT, MVT::f32, Expand);
123 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000124 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
125 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000126 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.
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000131 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000132 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
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +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);
Anton Korobeynikov6323a832009-07-18 13:44:25 +0000139 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
Anton Korobeynikovbb8a0482009-07-16 14:30:29 +0000143 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
144 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000145
Anton Korobeynikova89430e2009-07-16 14:25:30 +0000146 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
147 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000148 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
151 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 Korobeynikov87a24e32009-07-16 13:28:59 +0000156 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
157 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000158 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000159 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000160 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000161 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000162 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000163 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000164 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000165 llvm_unreachable("Should not custom lower this!");
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000166 return SDValue();
167 }
168}
169
170//===----------------------------------------------------------------------===//
171// Calling Convention Implementation
172//===----------------------------------------------------------------------===//
173
174#include "SystemZGenCallingConv.inc"
175
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000176SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
177 SelectionDAG &DAG) {
178 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
179 switch (CC) {
180 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000181 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000182 case CallingConv::C:
183 case CallingConv::Fast:
184 return LowerCCCArguments(Op, DAG);
185 }
186}
187
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000188SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
189 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
190 unsigned CallingConv = TheCall->getCallingConv();
191 switch (CallingConv) {
192 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000193 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000194 case CallingConv::Fast:
195 case CallingConv::C:
196 return LowerCCCCallTo(Op, DAG, CallingConv);
197 }
198}
199
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000200/// LowerCCCArguments - transform physical registers into virtual registers and
201/// generate load operations for arguments places on the stack.
202// FIXME: struct return stuff
203// FIXME: varargs
204SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
205 SelectionDAG &DAG) {
206 MachineFunction &MF = DAG.getMachineFunction();
207 MachineFrameInfo *MFI = MF.getFrameInfo();
208 MachineRegisterInfo &RegInfo = MF.getRegInfo();
209 SDValue Root = Op.getOperand(0);
210 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
211 unsigned CC = MF.getFunction()->getCallingConv();
212 DebugLoc dl = Op.getDebugLoc();
213
214 // Assign locations to all of the incoming arguments.
215 SmallVector<CCValAssign, 16> ArgLocs;
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000216 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000217 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
218
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000219 if (isVarArg)
220 llvm_report_error("Varargs not supported yet");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000221
222 SmallVector<SDValue, 16> ArgValues;
223 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000224 SDValue ArgValue;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000225 CCValAssign &VA = ArgLocs[i];
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000226 MVT LocVT = VA.getLocVT();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000227 if (VA.isRegLoc()) {
228 // Arguments passed in registers
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000229 TargetRegisterClass *RC;
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000230 switch (LocVT.getSimpleVT()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000231 default:
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000232#ifndef NDEBUG
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000233 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000234 << LocVT.getSimpleVT()
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000235 << "\n";
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000236#endif
237 llvm_unreachable(0);
238 case MVT::i64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000239 RC = SystemZ::GR64RegisterClass;
240 break;
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000241 case MVT::f32:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000242 RC = SystemZ::FP32RegisterClass;
243 break;
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000244 case MVT::f64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000245 RC = SystemZ::FP64RegisterClass;
246 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000247 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000248
249 unsigned VReg = RegInfo.createVirtualRegister(RC);
250 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000251 ArgValue = DAG.getCopyFromReg(Root, dl, VReg, LocVT);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000252 } else {
253 // Sanity check
254 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000255
256 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000257 // Create the frame index object for this incoming parameter...
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000258 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000259 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000260
261 // Create the SelectionDAG nodes corresponding to a load
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000262 // from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000263 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000264 ArgValue = DAG.getLoad(LocVT, dl, Root, FIN,
265 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000266 }
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000267
268 // If this is an 8/16/32-bit value, it is really passed promoted to 64
269 // bits. Insert an assert[sz]ext to capture this, then truncate to the
270 // right size.
271 if (VA.getLocInfo() == CCValAssign::SExt)
272 ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
273 DAG.getValueType(VA.getValVT()));
274 else if (VA.getLocInfo() == CCValAssign::ZExt)
275 ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
276 DAG.getValueType(VA.getValVT()));
277
278 if (VA.getLocInfo() != CCValAssign::Full)
279 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
280
281 ArgValues.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000282 }
283
284 ArgValues.push_back(Root);
285
286 // Return the new list of results.
287 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
288 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
289}
290
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000291/// LowerCCCCallTo - functions arguments are copied from virtual regs to
292/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
293/// TODO: sret.
294SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
295 unsigned CC) {
296 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
297 SDValue Chain = TheCall->getChain();
298 SDValue Callee = TheCall->getCallee();
299 bool isVarArg = TheCall->isVarArg();
300 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000301 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000302
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000303 // Offset to first argument stack slot.
304 const unsigned FirstArgOffset = 160;
305
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000306 // Analyze operands of the call, assigning locations to each operand.
307 SmallVector<CCValAssign, 16> ArgLocs;
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000308 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000309
310 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
311
312 // Get a count of how many bytes are to be pushed on the stack.
313 unsigned NumBytes = CCInfo.getNextStackOffset();
314
315 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
316 getPointerTy(), true));
317
318 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
319 SmallVector<SDValue, 12> MemOpChains;
320 SDValue StackPtr;
321
322 // Walk the register/memloc assignments, inserting copies/loads.
323 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
324 CCValAssign &VA = ArgLocs[i];
325
326 // Arguments start after the 5 first operands of ISD::CALL
327 SDValue Arg = TheCall->getArg(i);
328
329 // Promote the value if needed.
330 switch (VA.getLocInfo()) {
331 default: assert(0 && "Unknown loc info!");
332 case CCValAssign::Full: break;
333 case CCValAssign::SExt:
334 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
335 break;
336 case CCValAssign::ZExt:
337 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
338 break;
339 case CCValAssign::AExt:
340 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
341 break;
342 }
343
344 // Arguments that can be passed on register must be kept at RegsToPass
345 // vector
346 if (VA.isRegLoc()) {
347 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
348 } else {
349 assert(VA.isMemLoc());
350
351 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000352 StackPtr =
353 DAG.getCopyFromReg(Chain, dl,
354 (RegInfo->hasFP(MF) ?
355 SystemZ::R11D : SystemZ::R15D),
356 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000357
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000358 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
359 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
360 StackPtr,
361 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000362
363 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000364 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000365 }
366 }
367
368 // Transform all store nodes into one single node because all store nodes are
369 // independent of each other.
370 if (!MemOpChains.empty())
371 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
372 &MemOpChains[0], MemOpChains.size());
373
374 // Build a sequence of copy-to-reg nodes chained together with token chain and
375 // flag operands which copy the outgoing args into registers. The InFlag in
376 // necessary since all emited instructions must be stuck together.
377 SDValue InFlag;
378 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
379 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
380 RegsToPass[i].second, InFlag);
381 InFlag = Chain.getValue(1);
382 }
383
384 // If the callee is a GlobalAddress node (quite common, every direct call is)
385 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
386 // Likewise ExternalSymbol -> TargetExternalSymbol.
387 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
388 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
389 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
390 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
391
392 // Returns a chain & a flag for retval copy to use.
393 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
394 SmallVector<SDValue, 8> Ops;
395 Ops.push_back(Chain);
396 Ops.push_back(Callee);
397
398 // Add argument registers to the end of the list so that they are
399 // known live into the call.
400 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
401 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
402 RegsToPass[i].second.getValueType()));
403
404 if (InFlag.getNode())
405 Ops.push_back(InFlag);
406
407 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
408 InFlag = Chain.getValue(1);
409
410 // Create the CALLSEQ_END node.
411 Chain = DAG.getCALLSEQ_END(Chain,
412 DAG.getConstant(NumBytes, getPointerTy(), true),
413 DAG.getConstant(0, getPointerTy(), true),
414 InFlag);
415 InFlag = Chain.getValue(1);
416
417 // Handle result values, copying them out of physregs into vregs that we
418 // return.
419 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
420 Op.getResNo());
421}
422
423/// LowerCallResult - Lower the result values of an ISD::CALL into the
424/// appropriate copies out of appropriate physical registers. This assumes that
425/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
426/// being lowered. Returns a SDNode with the same number of values as the
427/// ISD::CALL.
428SDNode*
429SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
430 CallSDNode *TheCall,
431 unsigned CallingConv,
432 SelectionDAG &DAG) {
433 bool isVarArg = TheCall->isVarArg();
434 DebugLoc dl = TheCall->getDebugLoc();
435
436 // Assign locations to each value returned by this call.
437 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000438 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs,
439 DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000440
441 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
442 SmallVector<SDValue, 8> ResultVals;
443
444 // Copy all of the result registers out of their specified physreg.
445 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000446 CCValAssign &VA = RVLocs[i];
447
448 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
449 VA.getLocVT(), InFlag).getValue(1);
450 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000451 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000452
453 // If this is an 8/16/32-bit value, it is really passed promoted to 64
454 // bits. Insert an assert[sz]ext to capture this, then truncate to the
455 // right size.
456 if (VA.getLocInfo() == CCValAssign::SExt)
457 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
458 DAG.getValueType(VA.getValVT()));
459 else if (VA.getLocInfo() == CCValAssign::ZExt)
460 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
461 DAG.getValueType(VA.getValVT()));
462
463 if (VA.getLocInfo() != CCValAssign::Full)
464 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
465
466 ResultVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000467 }
468
469 ResultVals.push_back(Chain);
470
471 // Merge everything together with a MERGE_VALUES node.
472 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
473 &ResultVals[0], ResultVals.size()).getNode();
474}
475
476
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000477SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
478 // CCValAssign - represent the assignment of the return value to a location
479 SmallVector<CCValAssign, 16> RVLocs;
480 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
481 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
482 DebugLoc dl = Op.getDebugLoc();
483
484 // CCState - Info about the registers and stack slot.
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000485 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000486
487 // Analize return values of ISD::RET
488 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
489
490 // If this is the first return lowered for this function, add the regs to the
491 // liveout set for the function.
492 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
493 for (unsigned i = 0; i != RVLocs.size(); ++i)
494 if (RVLocs[i].isRegLoc())
495 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
496 }
497
498 // The chain is always operand #0
499 SDValue Chain = Op.getOperand(0);
500 SDValue Flag;
501
502 // Copy the result values into the output registers.
503 for (unsigned i = 0; i != RVLocs.size(); ++i) {
504 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000505 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000506 assert(VA.isRegLoc() && "Can only return in registers!");
507
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000508 // If this is an 8/16/32-bit value, it is really should be passed promoted
509 // to 64 bits.
510 if (VA.getLocInfo() == CCValAssign::SExt)
511 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
512 else if (VA.getLocInfo() == CCValAssign::ZExt)
513 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
514 else if (VA.getLocInfo() == CCValAssign::AExt)
515 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
516
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000517 // ISD::RET => ret chain, (regnum1,val1), ...
518 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000519 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000520
521 // Guarantee that all emitted copies are stuck together,
522 // avoiding something bad.
523 Flag = Chain.getValue(1);
524 }
525
526 if (Flag.getNode())
527 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
528
529 // Return Void
530 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
531}
532
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000533SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
534 ISD::CondCode CC, SDValue &SystemZCC,
535 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000536 // FIXME: Emit a test if RHS is zero
537
538 bool isUnsigned = false;
539 SystemZCC::CondCodes TCC;
540 switch (CC) {
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000541 default:
542 llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000543 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000544 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000545 TCC = SystemZCC::E;
546 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000547 case ISD::SETUEQ:
548 TCC = SystemZCC::NLH;
549 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000550 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000551 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000552 TCC = SystemZCC::NE;
553 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000554 case ISD::SETUNE:
555 TCC = SystemZCC::LH;
556 break;
557 case ISD::SETO:
558 TCC = SystemZCC::O;
559 break;
560 case ISD::SETUO:
561 TCC = SystemZCC::NO;
562 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000563 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000564 if (LHS.getValueType().isFloatingPoint()) {
565 TCC = SystemZCC::NH;
566 break;
567 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000568 isUnsigned = true; // FALLTHROUGH
569 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000570 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000571 TCC = SystemZCC::LE;
572 break;
573 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000574 if (LHS.getValueType().isFloatingPoint()) {
575 TCC = SystemZCC::NL;
576 break;
577 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000578 isUnsigned = true; // FALLTHROUGH
579 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000580 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000581 TCC = SystemZCC::HE;
582 break;
583 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000584 if (LHS.getValueType().isFloatingPoint()) {
585 TCC = SystemZCC::NLE;
586 break;
587 }
588 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000589 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000590 case ISD::SETOGT:
591 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000592 break;
593 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000594 if (LHS.getValueType().isFloatingPoint()) {
595 TCC = SystemZCC::NHE;
596 break;
597 }
598 isUnsigned = true; // FALLTHROUGH
599 case ISD::SETLT:
600 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000601 TCC = SystemZCC::L;
602 break;
603 }
604
605 SystemZCC = DAG.getConstant(TCC, MVT::i32);
606
607 DebugLoc dl = LHS.getDebugLoc();
608 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
609 dl, MVT::Flag, LHS, RHS);
610}
611
612
613SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
614 SDValue Chain = Op.getOperand(0);
615 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
616 SDValue LHS = Op.getOperand(2);
617 SDValue RHS = Op.getOperand(3);
618 SDValue Dest = Op.getOperand(4);
619 DebugLoc dl = Op.getDebugLoc();
620
621 SDValue SystemZCC;
622 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
623 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
624 Chain, Dest, SystemZCC, Flag);
625}
626
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000627SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
628 SDValue LHS = Op.getOperand(0);
629 SDValue RHS = Op.getOperand(1);
630 SDValue TrueV = Op.getOperand(2);
631 SDValue FalseV = Op.getOperand(3);
632 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
633 DebugLoc dl = Op.getDebugLoc();
634
635 SDValue SystemZCC;
636 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
637
638 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
639 SmallVector<SDValue, 4> Ops;
640 Ops.push_back(TrueV);
641 Ops.push_back(FalseV);
642 Ops.push_back(SystemZCC);
643 Ops.push_back(Flag);
644
645 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
646}
647
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000648SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
649 SelectionDAG &DAG) {
650 DebugLoc dl = Op.getDebugLoc();
651 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000652 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000653
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000654 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
655 bool ExtraLoadRequired =
656 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
657
658 SDValue Result;
659 if (!IsPic && !ExtraLoadRequired) {
660 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
661 Offset = 0;
662 } else {
663 unsigned char OpFlags = 0;
664 if (ExtraLoadRequired)
665 OpFlags = SystemZII::MO_GOTENT;
666
667 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
668 }
669
670 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
671 getPointerTy(), Result);
672
673 if (ExtraLoadRequired)
674 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
675 PseudoSourceValue::getGOT(), 0);
676
677 // If there was a non-zero offset that we didn't fold, create an explicit
678 // addition for it.
679 if (Offset != 0)
680 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
681 DAG.getConstant(Offset, getPointerTy()));
682
683 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000684}
685
Anton Korobeynikovae535672009-07-16 14:19:35 +0000686// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000687SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
688 SelectionDAG &DAG) {
689 DebugLoc dl = Op.getDebugLoc();
690 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
691 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
692
693 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
694}
695
Anton Korobeynikovae535672009-07-16 14:19:35 +0000696
697// FIXME: PIC here
698// FIXME: This is just dirty hack. We need to lower cpool properly
699SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
700 SelectionDAG &DAG) {
701 DebugLoc dl = Op.getDebugLoc();
702 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
703
704 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
705 CP->getAlignment(),
706 CP->getOffset());
707
708 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
709}
710
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000711const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
712 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000713 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000714 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000715 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
716 case SystemZISD::CMP: return "SystemZISD::CMP";
717 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000718 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000719 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000720 default: return NULL;
721 }
722}
723
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000724//===----------------------------------------------------------------------===//
725// Other Lowering Code
726//===----------------------------------------------------------------------===//
727
728MachineBasicBlock*
729SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
730 MachineBasicBlock *BB) const {
731 const SystemZInstrInfo &TII = *TM.getInstrInfo();
732 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000733 assert((MI->getOpcode() == SystemZ::Select32 ||
734 MI->getOpcode() == SystemZ::SelectF32 ||
735 MI->getOpcode() == SystemZ::Select64 ||
736 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000737 "Unexpected instr type to insert");
738
739 // To "insert" a SELECT instruction, we actually have to insert the diamond
740 // control-flow pattern. The incoming instruction knows the destination vreg
741 // to set, the condition code register to branch on, the true/false values to
742 // select between, and a branch opcode to use.
743 const BasicBlock *LLVM_BB = BB->getBasicBlock();
744 MachineFunction::iterator I = BB;
745 ++I;
746
747 // thisMBB:
748 // ...
749 // TrueVal = ...
750 // cmpTY ccX, r1, r2
751 // jCC copy1MBB
752 // fallthrough --> copy0MBB
753 MachineBasicBlock *thisMBB = BB;
754 MachineFunction *F = BB->getParent();
755 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
756 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
757 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
758 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
759 F->insert(I, copy0MBB);
760 F->insert(I, copy1MBB);
761 // Update machine-CFG edges by transferring all successors of the current
762 // block to the new block which will contain the Phi node for the select.
763 copy1MBB->transferSuccessors(BB);
764 // Next, add the true and fallthrough blocks as its successors.
765 BB->addSuccessor(copy0MBB);
766 BB->addSuccessor(copy1MBB);
767
768 // copy0MBB:
769 // %FalseValue = ...
770 // # fallthrough to copy1MBB
771 BB = copy0MBB;
772
773 // Update machine-CFG edges
774 BB->addSuccessor(copy1MBB);
775
776 // copy1MBB:
777 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
778 // ...
779 BB = copy1MBB;
780 BuildMI(BB, dl, TII.get(SystemZ::PHI),
781 MI->getOperand(0).getReg())
782 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
783 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
784
785 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
786 return BB;
787}