blob: d294f08fbaa0f47d9e1389951e19195a17a07330 [file] [log] [blame]
Nate Begeman1d9d7422005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattner7c5a3d32005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner16e71f22005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
15#include "PPCTargetMachine.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000019#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7b738342005-09-13 19:33:40 +000020#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000021#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000022#include "llvm/Function.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000023using namespace llvm;
24
Nate Begeman21e463b2005-10-16 05:39:50 +000025PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000026 : TargetLowering(TM) {
27
28 // Fold away setcc operations if possible.
29 setSetCCIsExpensive();
30
Chris Lattnerd145a612005-09-27 22:18:25 +000031 // Use _setjmp/_longjmp instead of setjmp/longjmp.
32 setUseUnderscoreSetJmpLongJmp(true);
33
Chris Lattner7c5a3d32005-08-16 17:14:42 +000034 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000035 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
36 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
37 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000038
39 // PowerPC has no intrinsics for these particular operations
40 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
41 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
42 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
43
44 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
45 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
46 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
47
48 // PowerPC has no SREM/UREM instructions
49 setOperationAction(ISD::SREM, MVT::i32, Expand);
50 setOperationAction(ISD::UREM, MVT::i32, Expand);
51
52 // We don't support sin/cos/sqrt/fmod
53 setOperationAction(ISD::FSIN , MVT::f64, Expand);
54 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000055 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000056 setOperationAction(ISD::FSIN , MVT::f32, Expand);
57 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000058 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000059
60 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000061 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000062 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
63 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
64 }
65
66 // PowerPC does not have CTPOP or CTTZ
67 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
68 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
69
70 // PowerPC does not have Select
71 setOperationAction(ISD::SELECT, MVT::i32, Expand);
72 setOperationAction(ISD::SELECT, MVT::f32, Expand);
73 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000074
Chris Lattner0b1e4e52005-08-26 17:36:52 +000075 // PowerPC wants to turn select_cc of FP into fsel when possible.
76 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
77 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000078
Nate Begeman7cbd5252005-08-16 19:49:35 +000079 // PowerPC does not have BRCOND* which requires SetCC
80 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
81 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000082
83 // PowerPC does not have FP_TO_UINT
84 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
85
Chris Lattnerf7605322005-08-31 21:09:52 +000086 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
87 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000088
Jim Laskeyad23c9d2005-08-17 00:40:22 +000089 // PowerPC does not have [U|S]INT_TO_FP
90 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
91 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
92
Chris Lattnere6ec9f22005-09-10 00:21:06 +000093 // PowerPC does not have truncstore for i1.
94 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
95
Nate Begemanc09eeec2005-09-06 22:03:27 +000096 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +000097 // 64 bit PowerPC implementations can support i64 types directly
98 // FIXME: enable this once it works.
99 //addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
100 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000101 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
102 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000103 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
104 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
105 // 32 bit PowerPC wants to expand i64 shifts itself.
106 // FIXME: remove these once we natively handle i64 shifts.
107 setOperationAction(ISD::SHL, MVT::i64, Custom);
108 setOperationAction(ISD::SRL, MVT::i64, Custom);
109 setOperationAction(ISD::SRA, MVT::i64, Custom);
110 } else {
111 // 32 bit PowerPC wants to expand i64 shifts itself.
112 setOperationAction(ISD::SHL, MVT::i64, Custom);
113 setOperationAction(ISD::SRL, MVT::i64, Custom);
114 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000115 }
116
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000117 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000118
119 computeRegisterProperties();
120}
121
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000122/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
123static bool isFloatingPointZero(SDOperand Op) {
124 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
125 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
126 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
127 // Maybe this has already been legalized into the constant pool?
128 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
129 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
130 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
131 }
132 return false;
133}
134
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000135/// LowerOperation - Provide custom lowering hooks for some operations.
136///
Nate Begeman21e463b2005-10-16 05:39:50 +0000137SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000138 switch (Op.getOpcode()) {
139 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000140 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000141 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000142 SDOperand Src = Op.getOperand(0);
143 if (Src.getValueType() == MVT::f32)
144 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
145
Nate Begemanc09eeec2005-09-06 22:03:27 +0000146 switch (Op.getValueType()) {
147 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
148 case MVT::i32:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000149 Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000150 break;
151 case MVT::i64:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000152 Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000153 break;
154 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000155
156 int FrameIdx =
157 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
158 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
159 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
160 Op, FI, DAG.getSrcValue(0));
Nate Begemanc09eeec2005-09-06 22:03:27 +0000161 if (Op.getOpcode() == PPCISD::FCTIDZ) {
162 Op = DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
163 } else {
164 FI = DAG.getNode(ISD::ADD, MVT::i32, FI, DAG.getConstant(4, MVT::i32));
165 Op = DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
166 }
167 return Op;
168 }
169 case ISD::SINT_TO_FP: {
170 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
171 "Unhandled SINT_TO_FP type in custom expander!");
172 int FrameIdx =
173 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
174 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
175 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
176 Op.getOperand(0), FI, DAG.getSrcValue(0));
177 SDOperand LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
178 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, LD);
179 if (MVT::f32 == Op.getValueType())
180 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
181 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000182 }
183 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000184 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000185 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
186 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
187 break;
188
189 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
190
191 // Cannot handle SETEQ/SETNE.
192 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
193
194 MVT::ValueType ResVT = Op.getValueType();
195 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
196 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
197 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000198
Chris Lattnerf7605322005-08-31 21:09:52 +0000199 // If the RHS of the comparison is a 0.0, we don't need to do the
200 // subtraction at all.
201 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000202 switch (CC) {
203 default: assert(0 && "Invalid FSEL condition"); abort();
204 case ISD::SETULT:
205 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000206 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000207 case ISD::SETUGE:
208 case ISD::SETGE:
Chris Lattnerf7605322005-08-31 21:09:52 +0000209 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000210 case ISD::SETUGT:
211 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000212 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000213 case ISD::SETULE:
214 case ISD::SETLE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000215 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattnerf7605322005-08-31 21:09:52 +0000216 DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000217 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000218
219 switch (CC) {
220 default: assert(0 && "Invalid FSEL condition"); abort();
221 case ISD::SETULT:
222 case ISD::SETLT:
223 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner615c2d02005-09-28 22:29:58 +0000224 DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS), FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000225 case ISD::SETUGE:
226 case ISD::SETGE:
227 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner615c2d02005-09-28 22:29:58 +0000228 DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS), TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000229 case ISD::SETUGT:
230 case ISD::SETGT:
231 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner615c2d02005-09-28 22:29:58 +0000232 DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS), FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000233 case ISD::SETULE:
234 case ISD::SETLE:
235 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner615c2d02005-09-28 22:29:58 +0000236 DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000237 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000238 break;
239 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000240 case ISD::SHL: {
241 assert(Op.getValueType() == MVT::i64 &&
242 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
243 // The generic code does a fine job expanding shift by a constant.
244 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
245
246 // Otherwise, expand into a bunch of logical ops. Note that these ops
247 // depend on the PPC behavior for oversized shift amounts.
248 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
249 DAG.getConstant(0, MVT::i32));
250 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
251 DAG.getConstant(1, MVT::i32));
252 SDOperand Amt = Op.getOperand(1);
253
254 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
255 DAG.getConstant(32, MVT::i32), Amt);
256 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
257 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
258 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
259 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
260 DAG.getConstant(-32U, MVT::i32));
261 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
262 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
263 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
264 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
265 }
266 case ISD::SRL: {
267 assert(Op.getValueType() == MVT::i64 &&
268 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
269 // The generic code does a fine job expanding shift by a constant.
270 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
271
272 // Otherwise, expand into a bunch of logical ops. Note that these ops
273 // depend on the PPC behavior for oversized shift amounts.
274 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
275 DAG.getConstant(0, MVT::i32));
276 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
277 DAG.getConstant(1, MVT::i32));
278 SDOperand Amt = Op.getOperand(1);
279
280 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
281 DAG.getConstant(32, MVT::i32), Amt);
282 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
283 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
284 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
285 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
286 DAG.getConstant(-32U, MVT::i32));
287 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
288 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
289 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
290 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
291 }
292 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000293 assert(Op.getValueType() == MVT::i64 &&
294 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
295 // The generic code does a fine job expanding shift by a constant.
296 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
297
298 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
299 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
300 DAG.getConstant(0, MVT::i32));
301 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
302 DAG.getConstant(1, MVT::i32));
303 SDOperand Amt = Op.getOperand(1);
304
305 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
306 DAG.getConstant(32, MVT::i32), Amt);
307 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
308 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
309 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
310 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
311 DAG.getConstant(-32U, MVT::i32));
312 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
313 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
314 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
315 Tmp4, Tmp6, ISD::SETLE);
316 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000317 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000318 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000319 return SDOperand();
320}
321
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000322std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000323PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000324 //
325 // add beautiful description of PPC stack frame format, or at least some docs
326 //
327 MachineFunction &MF = DAG.getMachineFunction();
328 MachineFrameInfo *MFI = MF.getFrameInfo();
329 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000330 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000331 std::vector<SDOperand> ArgValues;
332
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000333 unsigned ArgOffset = 24;
334 unsigned GPR_remaining = 8;
335 unsigned FPR_remaining = 13;
336 unsigned GPR_idx = 0, FPR_idx = 0;
337 static const unsigned GPR[] = {
338 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
339 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
340 };
341 static const unsigned FPR[] = {
342 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
343 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
344 };
345
346 // Add DAG nodes to load the arguments... On entry to a function on PPC,
347 // the arguments start at offset 24, although they are likely to be passed
348 // in registers.
349 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
350 SDOperand newroot, argt;
351 unsigned ObjSize;
352 bool needsLoad = false;
353 bool ArgLive = !I->use_empty();
354 MVT::ValueType ObjectVT = getValueType(I->getType());
355
356 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000357 default: assert(0 && "Unhandled argument type!");
358 case MVT::i1:
359 case MVT::i8:
360 case MVT::i16:
361 case MVT::i32:
362 ObjSize = 4;
363 if (!ArgLive) break;
364 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000365 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000366 MF.addLiveIn(GPR[GPR_idx], VReg);
367 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000368 if (ObjectVT != MVT::i32) {
369 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
370 : ISD::AssertZext;
371 argt = DAG.getNode(AssertOp, MVT::i32, argt,
372 DAG.getValueType(ObjectVT));
373 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
374 }
Chris Lattner915fb302005-08-30 00:19:00 +0000375 } else {
376 needsLoad = true;
377 }
378 break;
379 case MVT::i64: ObjSize = 8;
380 if (!ArgLive) break;
381 if (GPR_remaining > 0) {
382 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000383 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000384 MF.addLiveIn(GPR[GPR_idx], VReg);
385 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000386 // If we have two or more remaining argument registers, then both halves
387 // of the i64 can be sourced from there. Otherwise, the lower half will
388 // have to come off the stack. This can happen when an i64 is preceded
389 // by 28 bytes of arguments.
390 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000391 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000392 MF.addLiveIn(GPR[GPR_idx+1], VReg);
393 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000394 } else {
395 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
396 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
397 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
398 DAG.getSrcValue(NULL));
399 }
400 // Build the outgoing arg thingy
401 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
402 newroot = argLo;
403 } else {
404 needsLoad = true;
405 }
406 break;
407 case MVT::f32:
408 case MVT::f64:
409 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
410 if (!ArgLive) break;
411 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000412 unsigned VReg;
413 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000414 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000415 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000416 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000417 MF.addLiveIn(FPR[FPR_idx], VReg);
418 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000419 --FPR_remaining;
420 ++FPR_idx;
421 } else {
422 needsLoad = true;
423 }
424 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000425 }
426
427 // We need to load the argument to a virtual register if we determined above
428 // that we ran out of physical registers of the appropriate type
429 if (needsLoad) {
430 unsigned SubregOffset = 0;
431 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
432 if (ObjectVT == MVT::i16) SubregOffset = 2;
433 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
434 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
435 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
436 DAG.getConstant(SubregOffset, MVT::i32));
437 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
438 DAG.getSrcValue(NULL));
439 }
440
441 // Every 4 bytes of argument space consumes one of the GPRs available for
442 // argument passing.
443 if (GPR_remaining > 0) {
444 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
445 GPR_remaining -= delta;
446 GPR_idx += delta;
447 }
448 ArgOffset += ObjSize;
449 if (newroot.Val)
450 DAG.setRoot(newroot.getValue(1));
451
452 ArgValues.push_back(argt);
453 }
454
455 // If the function takes variable number of arguments, make a frame index for
456 // the start of the first vararg value... for expansion of llvm.va_start.
457 if (F.isVarArg()) {
458 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
459 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
460 // If this function is vararg, store any remaining integer argument regs
461 // to their spots on the stack so that they may be loaded by deferencing the
462 // result of va_next.
463 std::vector<SDOperand> MemOps;
464 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000465 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000466 MF.addLiveIn(GPR[GPR_idx], VReg);
467 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000468 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
469 Val, FIN, DAG.getSrcValue(NULL));
470 MemOps.push_back(Store);
471 // Increment the address by four for the next argument to store
472 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
473 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
474 }
475 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
476 }
477
478 // Finally, inform the code generator which regs we return values in.
479 switch (getValueType(F.getReturnType())) {
480 default: assert(0 && "Unknown type!");
481 case MVT::isVoid: break;
482 case MVT::i1:
483 case MVT::i8:
484 case MVT::i16:
485 case MVT::i32:
486 MF.addLiveOut(PPC::R3);
487 break;
488 case MVT::i64:
489 MF.addLiveOut(PPC::R3);
490 MF.addLiveOut(PPC::R4);
491 break;
492 case MVT::f32:
493 case MVT::f64:
494 MF.addLiveOut(PPC::F1);
495 break;
496 }
497
498 return ArgValues;
499}
500
501std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000502PPCTargetLowering::LowerCallTo(SDOperand Chain,
503 const Type *RetTy, bool isVarArg,
504 unsigned CallingConv, bool isTailCall,
505 SDOperand Callee, ArgListTy &Args,
506 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000507 // args_to_use will accumulate outgoing args for the ISD::CALL case in
508 // SelectExpr to use to put the arguments in the appropriate registers.
509 std::vector<SDOperand> args_to_use;
510
511 // Count how many bytes are to be pushed on the stack, including the linkage
512 // area, and parameter passing area.
513 unsigned NumBytes = 24;
514
515 if (Args.empty()) {
516 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
517 DAG.getConstant(NumBytes, getPointerTy()));
518 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000519 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000520 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000521 default: assert(0 && "Unknown value type!");
522 case MVT::i1:
523 case MVT::i8:
524 case MVT::i16:
525 case MVT::i32:
526 case MVT::f32:
527 NumBytes += 4;
528 break;
529 case MVT::i64:
530 case MVT::f64:
531 NumBytes += 8;
532 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000533 }
Chris Lattner915fb302005-08-30 00:19:00 +0000534 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000535
Chris Lattner915fb302005-08-30 00:19:00 +0000536 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
537 // plus 32 bytes of argument space in case any called code gets funky on us.
538 // (Required by ABI to support var arg)
539 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000540
541 // Adjust the stack pointer for the new arguments...
542 // These operations are automatically eliminated by the prolog/epilog pass
543 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
544 DAG.getConstant(NumBytes, getPointerTy()));
545
546 // Set up a copy of the stack pointer for use loading and storing any
547 // arguments that may not fit in the registers available for argument
548 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000549 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
550 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000551
552 // Figure out which arguments are going to go in registers, and which in
553 // memory. Also, if this is a vararg function, floating point operations
554 // must be stored to our stack, and loaded into integer regs as well, if
555 // any integer regs are available for argument passing.
556 unsigned ArgOffset = 24;
557 unsigned GPR_remaining = 8;
558 unsigned FPR_remaining = 13;
559
560 std::vector<SDOperand> MemOps;
561 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
562 // PtrOff will be used to store the current argument to the stack if a
563 // register cannot be found for it.
564 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
565 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
566 MVT::ValueType ArgVT = getValueType(Args[i].second);
567
568 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000569 default: assert(0 && "Unexpected ValueType for argument!");
570 case MVT::i1:
571 case MVT::i8:
572 case MVT::i16:
573 // Promote the integer to 32 bits. If the input type is signed use a
574 // sign extend, otherwise use a zero extend.
575 if (Args[i].second->isSigned())
576 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
577 else
578 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
579 // FALL THROUGH
580 case MVT::i32:
581 if (GPR_remaining > 0) {
582 args_to_use.push_back(Args[i].first);
583 --GPR_remaining;
584 } else {
585 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
586 Args[i].first, PtrOff,
587 DAG.getSrcValue(NULL)));
588 }
589 ArgOffset += 4;
590 break;
591 case MVT::i64:
592 // If we have one free GPR left, we can place the upper half of the i64
593 // in it, and store the other half to the stack. If we have two or more
594 // free GPRs, then we can pass both halves of the i64 in registers.
595 if (GPR_remaining > 0) {
596 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
597 Args[i].first, DAG.getConstant(1, MVT::i32));
598 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
599 Args[i].first, DAG.getConstant(0, MVT::i32));
600 args_to_use.push_back(Hi);
601 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000602 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000603 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000604 --GPR_remaining;
605 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000606 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
607 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000608 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000609 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000610 }
Chris Lattner915fb302005-08-30 00:19:00 +0000611 } else {
612 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
613 Args[i].first, PtrOff,
614 DAG.getSrcValue(NULL)));
615 }
616 ArgOffset += 8;
617 break;
618 case MVT::f32:
619 case MVT::f64:
620 if (FPR_remaining > 0) {
621 args_to_use.push_back(Args[i].first);
622 --FPR_remaining;
623 if (isVarArg) {
624 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
625 Args[i].first, PtrOff,
626 DAG.getSrcValue(NULL));
627 MemOps.push_back(Store);
628 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000629 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000630 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
631 DAG.getSrcValue(NULL));
632 MemOps.push_back(Load);
633 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000634 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000635 }
636 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000637 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
638 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000639 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
640 DAG.getSrcValue(NULL));
641 MemOps.push_back(Load);
642 args_to_use.push_back(Load);
643 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000644 }
645 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000646 // If we have any FPRs remaining, we may also have GPRs remaining.
647 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
648 // GPRs.
649 if (GPR_remaining > 0) {
650 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
651 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000652 }
Chris Lattner915fb302005-08-30 00:19:00 +0000653 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
654 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
655 --GPR_remaining;
656 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000657 }
Chris Lattner915fb302005-08-30 00:19:00 +0000658 } else {
659 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
660 Args[i].first, PtrOff,
661 DAG.getSrcValue(NULL)));
662 }
663 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
664 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000665 }
666 }
667 if (!MemOps.empty())
668 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
669 }
670
671 std::vector<MVT::ValueType> RetVals;
672 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000673 MVT::ValueType ActualRetTyVT = RetTyVT;
674 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
675 ActualRetTyVT = MVT::i32; // Promote result to i32.
676
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000677 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000678 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000679 RetVals.push_back(MVT::Other);
680
681 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
682 Chain, Callee, args_to_use), 0);
683 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
684 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
685 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000686 SDOperand RetVal = TheCall;
687
688 // If the result is a small value, add a note so that we keep track of the
689 // information about whether it is sign or zero extended.
690 if (RetTyVT != ActualRetTyVT) {
691 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
692 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
693 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
694 }
695
696 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000697}
698
Nate Begeman21e463b2005-10-16 05:39:50 +0000699SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
700 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000701 // vastart just stores the address of the VarArgsFrameIndex slot into the
702 // memory location argument.
703 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
704 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
705 DAG.getSrcValue(VAListV));
706}
707
708std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000709PPCTargetLowering::LowerVAArg(SDOperand Chain,
710 SDOperand VAListP, Value *VAListV,
711 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000712 MVT::ValueType ArgVT = getValueType(ArgTy);
713
714 SDOperand VAList =
715 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
716 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
717 unsigned Amt;
718 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
719 Amt = 4;
720 else {
721 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
722 "Other types should have been promoted for varargs!");
723 Amt = 8;
724 }
725 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
726 DAG.getConstant(Amt, VAList.getValueType()));
727 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
728 VAList, VAListP, DAG.getSrcValue(VAListV));
729 return std::make_pair(Result, Chain);
730}
731
732
Nate Begeman21e463b2005-10-16 05:39:50 +0000733std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000734LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
735 SelectionDAG &DAG) {
736 assert(0 && "LowerFrameReturnAddress unimplemented");
737 abort();
738}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000739
740MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000741PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
742 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000743 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000744 MI->getOpcode() == PPC::SELECT_CC_F4 ||
745 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000746 "Unexpected instr type to insert");
747
748 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
749 // control-flow pattern. The incoming instruction knows the destination vreg
750 // to set, the condition code register to branch on, the true/false values to
751 // select between, and a branch opcode to use.
752 const BasicBlock *LLVM_BB = BB->getBasicBlock();
753 ilist<MachineBasicBlock>::iterator It = BB;
754 ++It;
755
756 // thisMBB:
757 // ...
758 // TrueVal = ...
759 // cmpTY ccX, r1, r2
760 // bCC copy1MBB
761 // fallthrough --> copy0MBB
762 MachineBasicBlock *thisMBB = BB;
763 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
764 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
765 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
766 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
767 MachineFunction *F = BB->getParent();
768 F->getBasicBlockList().insert(It, copy0MBB);
769 F->getBasicBlockList().insert(It, sinkMBB);
770 // Update machine-CFG edges
771 BB->addSuccessor(copy0MBB);
772 BB->addSuccessor(sinkMBB);
773
774 // copy0MBB:
775 // %FalseValue = ...
776 // # fallthrough to sinkMBB
777 BB = copy0MBB;
778
779 // Update machine-CFG edges
780 BB->addSuccessor(sinkMBB);
781
782 // sinkMBB:
783 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
784 // ...
785 BB = sinkMBB;
786 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
787 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
788 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
789
790 delete MI; // The pseudo instruction is gone now.
791 return BB;
792}
793