blob: 806233ee42277e87895242c59f575d1ccb65800e [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
64 setShiftAmountFlavor(Extend);
Anton Korobeynikov48e8b3c2009-07-16 14:15:24 +000065 setShiftAmountType(MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000066
Anton Korobeynikov4403b932009-07-16 13:27:25 +000067 // Provide all sorts of operation actions
Anton Korobeynikovbf022172009-07-16 13:53:35 +000068 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
69 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
70 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000071
Anton Korobeynikov85c5c3f2009-07-16 14:22:46 +000072 setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Expand);
73 setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Expand);
74 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Anton Korobeynikov299dc782009-07-16 14:22:30 +000075
Anton Korobeynikov85c5c3f2009-07-16 14:22:46 +000076 setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Expand);
77 setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Expand);
78 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
Anton Korobeynikov23eff5c2009-07-16 14:20:08 +000079
Anton Korobeynikove0167c12009-07-16 13:35:30 +000080 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000081 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov159ac632009-07-16 14:28:46 +000082 setBooleanContents(ZeroOrOneBooleanContent);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000083
84 setOperationAction(ISD::RET, MVT::Other, Custom);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000085
Anton Korobeynikov983d3a12009-07-16 14:07:24 +000086 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000087 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
88 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
89 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +000090 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
91 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
92 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
93 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +000094 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +000095 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Anton Korobeynikovc772c442009-07-16 14:08:15 +000096 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000097
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000098 setOperationAction(ISD::SDIV, MVT::i32, Expand);
99 setOperationAction(ISD::UDIV, MVT::i32, Expand);
100 setOperationAction(ISD::SDIV, MVT::i64, Expand);
101 setOperationAction(ISD::UDIV, MVT::i64, Expand);
102 setOperationAction(ISD::SREM, MVT::i32, Expand);
103 setOperationAction(ISD::UREM, MVT::i32, Expand);
104 setOperationAction(ISD::SREM, MVT::i64, Expand);
105 setOperationAction(ISD::UREM, MVT::i64, Expand);
106
Anton Korobeynikove37a37d2009-07-18 12:20:36 +0000107 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
108
Anton Korobeynikov0cca0692009-07-18 12:26:13 +0000109 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
110 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
111 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
112 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
113 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
114 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
115
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000116 // FIXME: Can we lower these 2 efficiently?
117 setOperationAction(ISD::SETCC, MVT::i32, Expand);
118 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000119 setOperationAction(ISD::SETCC, MVT::f32, Expand);
120 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000121 setOperationAction(ISD::SELECT, MVT::i32, Expand);
122 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000123 setOperationAction(ISD::SELECT, MVT::f32, Expand);
124 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000125 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
126 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000127 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
128 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000129
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +0000130 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
131 // unsigned.
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000132 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000133 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000134
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000135 // Lower some FP stuff
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000136 setOperationAction(ISD::FSIN, MVT::f32, Expand);
137 setOperationAction(ISD::FSIN, MVT::f64, Expand);
138 setOperationAction(ISD::FCOS, MVT::f32, Expand);
139 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Anton Korobeynikov6323a832009-07-18 13:44:25 +0000140 setOperationAction(ISD::FREM, MVT::f32, Expand);
141 setOperationAction(ISD::FREM, MVT::f64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000142
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000143 // We have only 64-bit bitconverts
Anton Korobeynikovbb8a0482009-07-16 14:30:29 +0000144 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
145 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000146
Anton Korobeynikova89430e2009-07-16 14:25:30 +0000147 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
148 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000149 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
150 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000151
152 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000153}
154
155SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
156 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000157 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
158 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000159 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000160 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000161 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000162 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000163 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000164 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000165 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000166 llvm_unreachable("Should not custom lower this!");
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000167 return SDValue();
168 }
169}
170
171//===----------------------------------------------------------------------===//
172// Calling Convention Implementation
173//===----------------------------------------------------------------------===//
174
175#include "SystemZGenCallingConv.inc"
176
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000177SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
178 SelectionDAG &DAG) {
179 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
180 switch (CC) {
181 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000182 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000183 case CallingConv::C:
184 case CallingConv::Fast:
185 return LowerCCCArguments(Op, DAG);
186 }
187}
188
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000189SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
190 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
191 unsigned CallingConv = TheCall->getCallingConv();
192 switch (CallingConv) {
193 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000194 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000195 case CallingConv::Fast:
196 case CallingConv::C:
197 return LowerCCCCallTo(Op, DAG, CallingConv);
198 }
199}
200
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000201/// LowerCCCArguments - transform physical registers into virtual registers and
202/// generate load operations for arguments places on the stack.
203// FIXME: struct return stuff
204// FIXME: varargs
205SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
206 SelectionDAG &DAG) {
207 MachineFunction &MF = DAG.getMachineFunction();
208 MachineFrameInfo *MFI = MF.getFrameInfo();
209 MachineRegisterInfo &RegInfo = MF.getRegInfo();
210 SDValue Root = Op.getOperand(0);
211 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
212 unsigned CC = MF.getFunction()->getCallingConv();
213 DebugLoc dl = Op.getDebugLoc();
214
215 // Assign locations to all of the incoming arguments.
216 SmallVector<CCValAssign, 16> ArgLocs;
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000217 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000218 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
219
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000220 if (isVarArg)
221 llvm_report_error("Varargs not supported yet");
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000222
223 SmallVector<SDValue, 16> ArgValues;
224 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000225 SDValue ArgValue;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000226 CCValAssign &VA = ArgLocs[i];
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000227 MVT LocVT = VA.getLocVT();
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000228 if (VA.isRegLoc()) {
229 // Arguments passed in registers
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000230 TargetRegisterClass *RC;
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000231 switch (LocVT.getSimpleVT()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000232 default:
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000233#ifndef NDEBUG
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000234 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000235 << LocVT.getSimpleVT()
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000236 << "\n";
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000237#endif
238 llvm_unreachable(0);
239 case MVT::i64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000240 RC = SystemZ::GR64RegisterClass;
241 break;
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000242 case MVT::f32:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000243 RC = SystemZ::FP32RegisterClass;
244 break;
Anton Korobeynikov6d94eff2009-07-18 13:34:59 +0000245 case MVT::f64:
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000246 RC = SystemZ::FP64RegisterClass;
247 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000248 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000249
250 unsigned VReg = RegInfo.createVirtualRegister(RC);
251 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000252 ArgValue = DAG.getCopyFromReg(Root, dl, VReg, LocVT);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000253 } else {
254 // Sanity check
255 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000256
257 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000258 // Create the frame index object for this incoming parameter...
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000259 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000260 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000261
262 // Create the SelectionDAG nodes corresponding to a load
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000263 // from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000264 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000265 ArgValue = DAG.getLoad(LocVT, dl, Root, FIN,
266 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000267 }
Anton Korobeynikovc1a1e4a2009-07-16 14:29:05 +0000268
269 // If this is an 8/16/32-bit value, it is really passed promoted to 64
270 // bits. Insert an assert[sz]ext to capture this, then truncate to the
271 // right size.
272 if (VA.getLocInfo() == CCValAssign::SExt)
273 ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
274 DAG.getValueType(VA.getValVT()));
275 else if (VA.getLocInfo() == CCValAssign::ZExt)
276 ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
277 DAG.getValueType(VA.getValVT()));
278
279 if (VA.getLocInfo() != CCValAssign::Full)
280 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
281
282 ArgValues.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000283 }
284
285 ArgValues.push_back(Root);
286
287 // Return the new list of results.
288 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
289 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
290}
291
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000292/// LowerCCCCallTo - functions arguments are copied from virtual regs to
293/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
294/// TODO: sret.
295SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
296 unsigned CC) {
297 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
298 SDValue Chain = TheCall->getChain();
299 SDValue Callee = TheCall->getCallee();
300 bool isVarArg = TheCall->isVarArg();
301 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000302 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000303
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000304 // Offset to first argument stack slot.
305 const unsigned FirstArgOffset = 160;
306
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000307 // Analyze operands of the call, assigning locations to each operand.
308 SmallVector<CCValAssign, 16> ArgLocs;
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000309 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000310
311 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
312
313 // Get a count of how many bytes are to be pushed on the stack.
314 unsigned NumBytes = CCInfo.getNextStackOffset();
315
316 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
317 getPointerTy(), true));
318
319 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
320 SmallVector<SDValue, 12> MemOpChains;
321 SDValue StackPtr;
322
323 // Walk the register/memloc assignments, inserting copies/loads.
324 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
325 CCValAssign &VA = ArgLocs[i];
326
327 // Arguments start after the 5 first operands of ISD::CALL
328 SDValue Arg = TheCall->getArg(i);
329
330 // Promote the value if needed.
331 switch (VA.getLocInfo()) {
332 default: assert(0 && "Unknown loc info!");
333 case CCValAssign::Full: break;
334 case CCValAssign::SExt:
335 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
336 break;
337 case CCValAssign::ZExt:
338 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
339 break;
340 case CCValAssign::AExt:
341 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
342 break;
343 }
344
345 // Arguments that can be passed on register must be kept at RegsToPass
346 // vector
347 if (VA.isRegLoc()) {
348 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
349 } else {
350 assert(VA.isMemLoc());
351
352 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000353 StackPtr =
354 DAG.getCopyFromReg(Chain, dl,
355 (RegInfo->hasFP(MF) ?
356 SystemZ::R11D : SystemZ::R15D),
357 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000358
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000359 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
360 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
361 StackPtr,
362 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000363
364 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000365 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000366 }
367 }
368
369 // Transform all store nodes into one single node because all store nodes are
370 // independent of each other.
371 if (!MemOpChains.empty())
372 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
373 &MemOpChains[0], MemOpChains.size());
374
375 // Build a sequence of copy-to-reg nodes chained together with token chain and
376 // flag operands which copy the outgoing args into registers. The InFlag in
377 // necessary since all emited instructions must be stuck together.
378 SDValue InFlag;
379 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
380 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
381 RegsToPass[i].second, InFlag);
382 InFlag = Chain.getValue(1);
383 }
384
385 // If the callee is a GlobalAddress node (quite common, every direct call is)
386 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
387 // Likewise ExternalSymbol -> TargetExternalSymbol.
388 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
389 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
390 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
391 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
392
393 // Returns a chain & a flag for retval copy to use.
394 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
395 SmallVector<SDValue, 8> Ops;
396 Ops.push_back(Chain);
397 Ops.push_back(Callee);
398
399 // Add argument registers to the end of the list so that they are
400 // known live into the call.
401 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
402 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
403 RegsToPass[i].second.getValueType()));
404
405 if (InFlag.getNode())
406 Ops.push_back(InFlag);
407
408 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
409 InFlag = Chain.getValue(1);
410
411 // Create the CALLSEQ_END node.
412 Chain = DAG.getCALLSEQ_END(Chain,
413 DAG.getConstant(NumBytes, getPointerTy(), true),
414 DAG.getConstant(0, getPointerTy(), true),
415 InFlag);
416 InFlag = Chain.getValue(1);
417
418 // Handle result values, copying them out of physregs into vregs that we
419 // return.
420 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
421 Op.getResNo());
422}
423
424/// LowerCallResult - Lower the result values of an ISD::CALL into the
425/// appropriate copies out of appropriate physical registers. This assumes that
426/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
427/// being lowered. Returns a SDNode with the same number of values as the
428/// ISD::CALL.
429SDNode*
430SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
431 CallSDNode *TheCall,
432 unsigned CallingConv,
433 SelectionDAG &DAG) {
434 bool isVarArg = TheCall->isVarArg();
435 DebugLoc dl = TheCall->getDebugLoc();
436
437 // Assign locations to each value returned by this call.
438 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000439 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs,
440 DAG.getContext());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000441
442 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
443 SmallVector<SDValue, 8> ResultVals;
444
445 // Copy all of the result registers out of their specified physreg.
446 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000447 CCValAssign &VA = RVLocs[i];
448
449 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
450 VA.getLocVT(), InFlag).getValue(1);
451 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000452 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000453
454 // If this is an 8/16/32-bit value, it is really passed promoted to 64
455 // bits. Insert an assert[sz]ext to capture this, then truncate to the
456 // right size.
457 if (VA.getLocInfo() == CCValAssign::SExt)
458 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
459 DAG.getValueType(VA.getValVT()));
460 else if (VA.getLocInfo() == CCValAssign::ZExt)
461 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
462 DAG.getValueType(VA.getValVT()));
463
464 if (VA.getLocInfo() != CCValAssign::Full)
465 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
466
467 ResultVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000468 }
469
470 ResultVals.push_back(Chain);
471
472 // Merge everything together with a MERGE_VALUES node.
473 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
474 &ResultVals[0], ResultVals.size()).getNode();
475}
476
477
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000478SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
479 // CCValAssign - represent the assignment of the return value to a location
480 SmallVector<CCValAssign, 16> RVLocs;
481 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
482 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
483 DebugLoc dl = Op.getDebugLoc();
484
485 // CCState - Info about the registers and stack slot.
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000486 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, DAG.getContext());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000487
488 // Analize return values of ISD::RET
489 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
490
491 // If this is the first return lowered for this function, add the regs to the
492 // liveout set for the function.
493 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
494 for (unsigned i = 0; i != RVLocs.size(); ++i)
495 if (RVLocs[i].isRegLoc())
496 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
497 }
498
499 // The chain is always operand #0
500 SDValue Chain = Op.getOperand(0);
501 SDValue Flag;
502
503 // Copy the result values into the output registers.
504 for (unsigned i = 0; i != RVLocs.size(); ++i) {
505 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000506 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000507 assert(VA.isRegLoc() && "Can only return in registers!");
508
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000509 // If this is an 8/16/32-bit value, it is really should be passed promoted
510 // to 64 bits.
511 if (VA.getLocInfo() == CCValAssign::SExt)
512 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
513 else if (VA.getLocInfo() == CCValAssign::ZExt)
514 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
515 else if (VA.getLocInfo() == CCValAssign::AExt)
516 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
517
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000518 // ISD::RET => ret chain, (regnum1,val1), ...
519 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000520 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000521
522 // Guarantee that all emitted copies are stuck together,
523 // avoiding something bad.
524 Flag = Chain.getValue(1);
525 }
526
527 if (Flag.getNode())
528 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
529
530 // Return Void
531 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
532}
533
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000534SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
535 ISD::CondCode CC, SDValue &SystemZCC,
536 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000537 // FIXME: Emit a test if RHS is zero
538
539 bool isUnsigned = false;
540 SystemZCC::CondCodes TCC;
541 switch (CC) {
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000542 default:
543 llvm_unreachable("Invalid integer condition!");
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000544 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000545 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000546 TCC = SystemZCC::E;
547 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000548 case ISD::SETUEQ:
549 TCC = SystemZCC::NLH;
550 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000551 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000552 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000553 TCC = SystemZCC::NE;
554 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000555 case ISD::SETUNE:
556 TCC = SystemZCC::LH;
557 break;
558 case ISD::SETO:
559 TCC = SystemZCC::O;
560 break;
561 case ISD::SETUO:
562 TCC = SystemZCC::NO;
563 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000564 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000565 if (LHS.getValueType().isFloatingPoint()) {
566 TCC = SystemZCC::NH;
567 break;
568 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000569 isUnsigned = true; // FALLTHROUGH
570 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000571 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000572 TCC = SystemZCC::LE;
573 break;
574 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000575 if (LHS.getValueType().isFloatingPoint()) {
576 TCC = SystemZCC::NL;
577 break;
578 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000579 isUnsigned = true; // FALLTHROUGH
580 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000581 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000582 TCC = SystemZCC::HE;
583 break;
584 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000585 if (LHS.getValueType().isFloatingPoint()) {
586 TCC = SystemZCC::NLE;
587 break;
588 }
589 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000590 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000591 case ISD::SETOGT:
592 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000593 break;
594 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000595 if (LHS.getValueType().isFloatingPoint()) {
596 TCC = SystemZCC::NHE;
597 break;
598 }
599 isUnsigned = true; // FALLTHROUGH
600 case ISD::SETLT:
601 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000602 TCC = SystemZCC::L;
603 break;
604 }
605
606 SystemZCC = DAG.getConstant(TCC, MVT::i32);
607
608 DebugLoc dl = LHS.getDebugLoc();
609 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
610 dl, MVT::Flag, LHS, RHS);
611}
612
613
614SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
615 SDValue Chain = Op.getOperand(0);
616 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
617 SDValue LHS = Op.getOperand(2);
618 SDValue RHS = Op.getOperand(3);
619 SDValue Dest = Op.getOperand(4);
620 DebugLoc dl = Op.getDebugLoc();
621
622 SDValue SystemZCC;
623 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
624 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
625 Chain, Dest, SystemZCC, Flag);
626}
627
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000628SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
629 SDValue LHS = Op.getOperand(0);
630 SDValue RHS = Op.getOperand(1);
631 SDValue TrueV = Op.getOperand(2);
632 SDValue FalseV = Op.getOperand(3);
633 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
634 DebugLoc dl = Op.getDebugLoc();
635
636 SDValue SystemZCC;
637 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
638
639 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
640 SmallVector<SDValue, 4> Ops;
641 Ops.push_back(TrueV);
642 Ops.push_back(FalseV);
643 Ops.push_back(SystemZCC);
644 Ops.push_back(Flag);
645
646 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
647}
648
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000649SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
650 SelectionDAG &DAG) {
651 DebugLoc dl = Op.getDebugLoc();
652 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000653 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000654
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000655 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
656 bool ExtraLoadRequired =
657 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
658
659 SDValue Result;
660 if (!IsPic && !ExtraLoadRequired) {
661 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
662 Offset = 0;
663 } else {
664 unsigned char OpFlags = 0;
665 if (ExtraLoadRequired)
666 OpFlags = SystemZII::MO_GOTENT;
667
668 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
669 }
670
671 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
672 getPointerTy(), Result);
673
674 if (ExtraLoadRequired)
675 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
676 PseudoSourceValue::getGOT(), 0);
677
678 // If there was a non-zero offset that we didn't fold, create an explicit
679 // addition for it.
680 if (Offset != 0)
681 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
682 DAG.getConstant(Offset, getPointerTy()));
683
684 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000685}
686
Anton Korobeynikovae535672009-07-16 14:19:35 +0000687// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000688SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
689 SelectionDAG &DAG) {
690 DebugLoc dl = Op.getDebugLoc();
691 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
692 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
693
694 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
695}
696
Anton Korobeynikovae535672009-07-16 14:19:35 +0000697
698// FIXME: PIC here
699// FIXME: This is just dirty hack. We need to lower cpool properly
700SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
701 SelectionDAG &DAG) {
702 DebugLoc dl = Op.getDebugLoc();
703 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
704
705 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
706 CP->getAlignment(),
707 CP->getOffset());
708
709 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
710}
711
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000712const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
713 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000714 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000715 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000716 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
717 case SystemZISD::CMP: return "SystemZISD::CMP";
718 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000719 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000720 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000721 default: return NULL;
722 }
723}
724
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000725//===----------------------------------------------------------------------===//
726// Other Lowering Code
727//===----------------------------------------------------------------------===//
728
729MachineBasicBlock*
730SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
731 MachineBasicBlock *BB) const {
732 const SystemZInstrInfo &TII = *TM.getInstrInfo();
733 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000734 assert((MI->getOpcode() == SystemZ::Select32 ||
735 MI->getOpcode() == SystemZ::SelectF32 ||
736 MI->getOpcode() == SystemZ::Select64 ||
737 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000738 "Unexpected instr type to insert");
739
740 // To "insert" a SELECT instruction, we actually have to insert the diamond
741 // control-flow pattern. The incoming instruction knows the destination vreg
742 // to set, the condition code register to branch on, the true/false values to
743 // select between, and a branch opcode to use.
744 const BasicBlock *LLVM_BB = BB->getBasicBlock();
745 MachineFunction::iterator I = BB;
746 ++I;
747
748 // thisMBB:
749 // ...
750 // TrueVal = ...
751 // cmpTY ccX, r1, r2
752 // jCC copy1MBB
753 // fallthrough --> copy0MBB
754 MachineBasicBlock *thisMBB = BB;
755 MachineFunction *F = BB->getParent();
756 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
757 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
758 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
759 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
760 F->insert(I, copy0MBB);
761 F->insert(I, copy1MBB);
762 // Update machine-CFG edges by transferring all successors of the current
763 // block to the new block which will contain the Phi node for the select.
764 copy1MBB->transferSuccessors(BB);
765 // Next, add the true and fallthrough blocks as its successors.
766 BB->addSuccessor(copy0MBB);
767 BB->addSuccessor(copy1MBB);
768
769 // copy0MBB:
770 // %FalseValue = ...
771 // # fallthrough to copy1MBB
772 BB = copy0MBB;
773
774 // Update machine-CFG edges
775 BB->addSuccessor(copy1MBB);
776
777 // copy1MBB:
778 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
779 // ...
780 BB = copy1MBB;
781 BuildMI(BB, dl, TII.get(SystemZ::PHI),
782 MI->getOperand(0).getReg())
783 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
784 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
785
786 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
787 return BB;
788}