blob: c9ed2bae1e3a720a07596b9cbd58aaa1675fe5a0 [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();
Nate Begeman405e3ec2005-10-21 00:02:42 +000030 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000031
Chris Lattnerd145a612005-09-27 22:18:25 +000032 // Use _setjmp/_longjmp instead of setjmp/longjmp.
33 setUseUnderscoreSetJmpLongJmp(true);
34
Chris Lattner7c5a3d32005-08-16 17:14:42 +000035 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000036 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
37 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
38 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000039
40 // PowerPC has no intrinsics for these particular operations
41 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
42 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
43 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
44
45 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
46 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
47 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
48
49 // PowerPC has no SREM/UREM instructions
50 setOperationAction(ISD::SREM, MVT::i32, Expand);
51 setOperationAction(ISD::UREM, MVT::i32, Expand);
52
53 // We don't support sin/cos/sqrt/fmod
54 setOperationAction(ISD::FSIN , MVT::f64, Expand);
55 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000056 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000057 setOperationAction(ISD::FSIN , MVT::f32, Expand);
58 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000059 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000060
61 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000062 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000063 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
64 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
65 }
66
67 // PowerPC does not have CTPOP or CTTZ
68 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
69 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
70
71 // PowerPC does not have Select
72 setOperationAction(ISD::SELECT, MVT::i32, Expand);
73 setOperationAction(ISD::SELECT, MVT::f32, Expand);
74 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000075
Chris Lattner0b1e4e52005-08-26 17:36:52 +000076 // PowerPC wants to turn select_cc of FP into fsel when possible.
77 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
78 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000079
Nate Begeman7cbd5252005-08-16 19:49:35 +000080 // PowerPC does not have BRCOND* which requires SetCC
81 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
82 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000083
84 // PowerPC does not have FP_TO_UINT
85 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
86
Chris Lattnerf7605322005-08-31 21:09:52 +000087 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
88 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000089
Jim Laskeyad23c9d2005-08-17 00:40:22 +000090 // PowerPC does not have [U|S]INT_TO_FP
91 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
92 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
93
Chris Lattnere6ec9f22005-09-10 00:21:06 +000094 // PowerPC does not have truncstore for i1.
95 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
96
Nate Begemanc09eeec2005-09-06 22:03:27 +000097 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +000098 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +000099 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
100 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000101 }
102
103 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
104 // 64 bit PowerPC implementations can support i64 types directly
105 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000106 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
107 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000108 } else {
109 // 32 bit PowerPC wants to expand i64 shifts itself.
110 setOperationAction(ISD::SHL, MVT::i64, Custom);
111 setOperationAction(ISD::SRL, MVT::i64, Custom);
112 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000113 }
114
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000115 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000116
117 computeRegisterProperties();
118}
119
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000120/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
121static bool isFloatingPointZero(SDOperand Op) {
122 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
123 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
124 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
125 // Maybe this has already been legalized into the constant pool?
126 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
127 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
128 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
129 }
130 return false;
131}
132
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000133/// LowerOperation - Provide custom lowering hooks for some operations.
134///
Nate Begeman21e463b2005-10-16 05:39:50 +0000135SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000136 switch (Op.getOpcode()) {
137 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000138 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000139 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000140 SDOperand Src = Op.getOperand(0);
141 if (Src.getValueType() == MVT::f32)
142 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
143
Nate Begemanc09eeec2005-09-06 22:03:27 +0000144 switch (Op.getValueType()) {
145 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
146 case MVT::i32:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000147 Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000148 break;
149 case MVT::i64:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000150 Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000151 break;
152 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000153
154 int FrameIdx =
155 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
156 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
157 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
158 Op, FI, DAG.getSrcValue(0));
Nate Begemanc09eeec2005-09-06 22:03:27 +0000159 if (Op.getOpcode() == PPCISD::FCTIDZ) {
160 Op = DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
161 } else {
162 FI = DAG.getNode(ISD::ADD, MVT::i32, FI, DAG.getConstant(4, MVT::i32));
163 Op = DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
164 }
165 return Op;
166 }
167 case ISD::SINT_TO_FP: {
168 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
169 "Unhandled SINT_TO_FP type in custom expander!");
170 int FrameIdx =
171 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
172 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
173 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
174 Op.getOperand(0), FI, DAG.getSrcValue(0));
175 SDOperand LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
176 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, LD);
177 if (MVT::f32 == Op.getValueType())
178 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
179 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000180 }
181 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000182 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000183 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
184 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
185 break;
186
187 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
188
189 // Cannot handle SETEQ/SETNE.
190 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
191
192 MVT::ValueType ResVT = Op.getValueType();
193 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
194 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
195 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000196
Chris Lattnerf7605322005-08-31 21:09:52 +0000197 // If the RHS of the comparison is a 0.0, we don't need to do the
198 // subtraction at all.
199 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000200 switch (CC) {
201 default: assert(0 && "Invalid FSEL condition"); abort();
202 case ISD::SETULT:
203 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000204 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000205 case ISD::SETUGE:
206 case ISD::SETGE:
Chris Lattnerf7605322005-08-31 21:09:52 +0000207 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000208 case ISD::SETUGT:
209 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000210 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000211 case ISD::SETULE:
212 case ISD::SETLE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000213 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattnerf7605322005-08-31 21:09:52 +0000214 DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000215 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000216
217 switch (CC) {
218 default: assert(0 && "Invalid FSEL condition"); abort();
219 case ISD::SETULT:
220 case ISD::SETLT:
221 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner615c2d02005-09-28 22:29:58 +0000222 DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS), FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000223 case ISD::SETUGE:
224 case ISD::SETGE:
225 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner615c2d02005-09-28 22:29:58 +0000226 DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS), TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000227 case ISD::SETUGT:
228 case ISD::SETGT:
229 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner615c2d02005-09-28 22:29:58 +0000230 DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS), FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000231 case ISD::SETULE:
232 case ISD::SETLE:
233 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner615c2d02005-09-28 22:29:58 +0000234 DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000235 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000236 break;
237 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000238 case ISD::SHL: {
239 assert(Op.getValueType() == MVT::i64 &&
240 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
241 // The generic code does a fine job expanding shift by a constant.
242 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
243
244 // Otherwise, expand into a bunch of logical ops. Note that these ops
245 // depend on the PPC behavior for oversized shift amounts.
246 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
247 DAG.getConstant(0, MVT::i32));
248 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
249 DAG.getConstant(1, MVT::i32));
250 SDOperand Amt = Op.getOperand(1);
251
252 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
253 DAG.getConstant(32, MVT::i32), Amt);
254 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
255 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
256 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
257 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
258 DAG.getConstant(-32U, MVT::i32));
259 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
260 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
261 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
262 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
263 }
264 case ISD::SRL: {
265 assert(Op.getValueType() == MVT::i64 &&
266 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
267 // The generic code does a fine job expanding shift by a constant.
268 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
269
270 // Otherwise, expand into a bunch of logical ops. Note that these ops
271 // depend on the PPC behavior for oversized shift amounts.
272 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
273 DAG.getConstant(0, MVT::i32));
274 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
275 DAG.getConstant(1, MVT::i32));
276 SDOperand Amt = Op.getOperand(1);
277
278 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
279 DAG.getConstant(32, MVT::i32), Amt);
280 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
281 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
282 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
283 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
284 DAG.getConstant(-32U, MVT::i32));
285 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
286 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
287 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
288 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
289 }
290 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000291 assert(Op.getValueType() == MVT::i64 &&
292 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
293 // The generic code does a fine job expanding shift by a constant.
294 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
295
296 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
297 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
298 DAG.getConstant(0, MVT::i32));
299 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
300 DAG.getConstant(1, MVT::i32));
301 SDOperand Amt = Op.getOperand(1);
302
303 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
304 DAG.getConstant(32, MVT::i32), Amt);
305 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
306 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
307 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
308 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
309 DAG.getConstant(-32U, MVT::i32));
310 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
311 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
312 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
313 Tmp4, Tmp6, ISD::SETLE);
314 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000315 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000316 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000317 return SDOperand();
318}
319
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000320std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000321PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000322 //
323 // add beautiful description of PPC stack frame format, or at least some docs
324 //
325 MachineFunction &MF = DAG.getMachineFunction();
326 MachineFrameInfo *MFI = MF.getFrameInfo();
327 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000328 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000329 std::vector<SDOperand> ArgValues;
330
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000331 unsigned ArgOffset = 24;
332 unsigned GPR_remaining = 8;
333 unsigned FPR_remaining = 13;
334 unsigned GPR_idx = 0, FPR_idx = 0;
335 static const unsigned GPR[] = {
336 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
337 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
338 };
339 static const unsigned FPR[] = {
340 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
341 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
342 };
343
344 // Add DAG nodes to load the arguments... On entry to a function on PPC,
345 // the arguments start at offset 24, although they are likely to be passed
346 // in registers.
347 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
348 SDOperand newroot, argt;
349 unsigned ObjSize;
350 bool needsLoad = false;
351 bool ArgLive = !I->use_empty();
352 MVT::ValueType ObjectVT = getValueType(I->getType());
353
354 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000355 default: assert(0 && "Unhandled argument type!");
356 case MVT::i1:
357 case MVT::i8:
358 case MVT::i16:
359 case MVT::i32:
360 ObjSize = 4;
361 if (!ArgLive) break;
362 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000363 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000364 MF.addLiveIn(GPR[GPR_idx], VReg);
365 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000366 if (ObjectVT != MVT::i32) {
367 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
368 : ISD::AssertZext;
369 argt = DAG.getNode(AssertOp, MVT::i32, argt,
370 DAG.getValueType(ObjectVT));
371 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
372 }
Chris Lattner915fb302005-08-30 00:19:00 +0000373 } else {
374 needsLoad = true;
375 }
376 break;
377 case MVT::i64: ObjSize = 8;
378 if (!ArgLive) break;
379 if (GPR_remaining > 0) {
380 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000381 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000382 MF.addLiveIn(GPR[GPR_idx], VReg);
383 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000384 // If we have two or more remaining argument registers, then both halves
385 // of the i64 can be sourced from there. Otherwise, the lower half will
386 // have to come off the stack. This can happen when an i64 is preceded
387 // by 28 bytes of arguments.
388 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000389 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000390 MF.addLiveIn(GPR[GPR_idx+1], VReg);
391 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000392 } else {
393 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
394 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
395 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
396 DAG.getSrcValue(NULL));
397 }
398 // Build the outgoing arg thingy
399 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
400 newroot = argLo;
401 } else {
402 needsLoad = true;
403 }
404 break;
405 case MVT::f32:
406 case MVT::f64:
407 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
408 if (!ArgLive) break;
409 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000410 unsigned VReg;
411 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000412 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000413 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000414 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000415 MF.addLiveIn(FPR[FPR_idx], VReg);
416 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000417 --FPR_remaining;
418 ++FPR_idx;
419 } else {
420 needsLoad = true;
421 }
422 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000423 }
424
425 // We need to load the argument to a virtual register if we determined above
426 // that we ran out of physical registers of the appropriate type
427 if (needsLoad) {
428 unsigned SubregOffset = 0;
429 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
430 if (ObjectVT == MVT::i16) SubregOffset = 2;
431 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
432 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
433 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
434 DAG.getConstant(SubregOffset, MVT::i32));
435 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
436 DAG.getSrcValue(NULL));
437 }
438
439 // Every 4 bytes of argument space consumes one of the GPRs available for
440 // argument passing.
441 if (GPR_remaining > 0) {
442 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
443 GPR_remaining -= delta;
444 GPR_idx += delta;
445 }
446 ArgOffset += ObjSize;
447 if (newroot.Val)
448 DAG.setRoot(newroot.getValue(1));
449
450 ArgValues.push_back(argt);
451 }
452
453 // If the function takes variable number of arguments, make a frame index for
454 // the start of the first vararg value... for expansion of llvm.va_start.
455 if (F.isVarArg()) {
456 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
457 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
458 // If this function is vararg, store any remaining integer argument regs
459 // to their spots on the stack so that they may be loaded by deferencing the
460 // result of va_next.
461 std::vector<SDOperand> MemOps;
462 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000463 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000464 MF.addLiveIn(GPR[GPR_idx], VReg);
465 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000466 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
467 Val, FIN, DAG.getSrcValue(NULL));
468 MemOps.push_back(Store);
469 // Increment the address by four for the next argument to store
470 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
471 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
472 }
473 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
474 }
475
476 // Finally, inform the code generator which regs we return values in.
477 switch (getValueType(F.getReturnType())) {
478 default: assert(0 && "Unknown type!");
479 case MVT::isVoid: break;
480 case MVT::i1:
481 case MVT::i8:
482 case MVT::i16:
483 case MVT::i32:
484 MF.addLiveOut(PPC::R3);
485 break;
486 case MVT::i64:
487 MF.addLiveOut(PPC::R3);
488 MF.addLiveOut(PPC::R4);
489 break;
490 case MVT::f32:
491 case MVT::f64:
492 MF.addLiveOut(PPC::F1);
493 break;
494 }
495
496 return ArgValues;
497}
498
499std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000500PPCTargetLowering::LowerCallTo(SDOperand Chain,
501 const Type *RetTy, bool isVarArg,
502 unsigned CallingConv, bool isTailCall,
503 SDOperand Callee, ArgListTy &Args,
504 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000505 // args_to_use will accumulate outgoing args for the ISD::CALL case in
506 // SelectExpr to use to put the arguments in the appropriate registers.
507 std::vector<SDOperand> args_to_use;
508
509 // Count how many bytes are to be pushed on the stack, including the linkage
510 // area, and parameter passing area.
511 unsigned NumBytes = 24;
512
513 if (Args.empty()) {
514 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
515 DAG.getConstant(NumBytes, getPointerTy()));
516 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000517 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000518 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000519 default: assert(0 && "Unknown value type!");
520 case MVT::i1:
521 case MVT::i8:
522 case MVT::i16:
523 case MVT::i32:
524 case MVT::f32:
525 NumBytes += 4;
526 break;
527 case MVT::i64:
528 case MVT::f64:
529 NumBytes += 8;
530 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000531 }
Chris Lattner915fb302005-08-30 00:19:00 +0000532 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000533
Chris Lattner915fb302005-08-30 00:19:00 +0000534 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
535 // plus 32 bytes of argument space in case any called code gets funky on us.
536 // (Required by ABI to support var arg)
537 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000538
539 // Adjust the stack pointer for the new arguments...
540 // These operations are automatically eliminated by the prolog/epilog pass
541 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
542 DAG.getConstant(NumBytes, getPointerTy()));
543
544 // Set up a copy of the stack pointer for use loading and storing any
545 // arguments that may not fit in the registers available for argument
546 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000547 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
548 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000549
550 // Figure out which arguments are going to go in registers, and which in
551 // memory. Also, if this is a vararg function, floating point operations
552 // must be stored to our stack, and loaded into integer regs as well, if
553 // any integer regs are available for argument passing.
554 unsigned ArgOffset = 24;
555 unsigned GPR_remaining = 8;
556 unsigned FPR_remaining = 13;
557
558 std::vector<SDOperand> MemOps;
559 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
560 // PtrOff will be used to store the current argument to the stack if a
561 // register cannot be found for it.
562 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
563 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
564 MVT::ValueType ArgVT = getValueType(Args[i].second);
565
566 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000567 default: assert(0 && "Unexpected ValueType for argument!");
568 case MVT::i1:
569 case MVT::i8:
570 case MVT::i16:
571 // Promote the integer to 32 bits. If the input type is signed use a
572 // sign extend, otherwise use a zero extend.
573 if (Args[i].second->isSigned())
574 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
575 else
576 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
577 // FALL THROUGH
578 case MVT::i32:
579 if (GPR_remaining > 0) {
580 args_to_use.push_back(Args[i].first);
581 --GPR_remaining;
582 } else {
583 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
584 Args[i].first, PtrOff,
585 DAG.getSrcValue(NULL)));
586 }
587 ArgOffset += 4;
588 break;
589 case MVT::i64:
590 // If we have one free GPR left, we can place the upper half of the i64
591 // in it, and store the other half to the stack. If we have two or more
592 // free GPRs, then we can pass both halves of the i64 in registers.
593 if (GPR_remaining > 0) {
594 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
595 Args[i].first, DAG.getConstant(1, MVT::i32));
596 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
597 Args[i].first, DAG.getConstant(0, MVT::i32));
598 args_to_use.push_back(Hi);
599 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000600 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000601 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000602 --GPR_remaining;
603 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000604 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
605 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000606 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000607 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000608 }
Chris Lattner915fb302005-08-30 00:19:00 +0000609 } else {
610 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
611 Args[i].first, PtrOff,
612 DAG.getSrcValue(NULL)));
613 }
614 ArgOffset += 8;
615 break;
616 case MVT::f32:
617 case MVT::f64:
618 if (FPR_remaining > 0) {
619 args_to_use.push_back(Args[i].first);
620 --FPR_remaining;
621 if (isVarArg) {
622 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
623 Args[i].first, PtrOff,
624 DAG.getSrcValue(NULL));
625 MemOps.push_back(Store);
626 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000627 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000628 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
629 DAG.getSrcValue(NULL));
630 MemOps.push_back(Load);
631 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000632 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000633 }
634 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000635 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
636 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000637 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
638 DAG.getSrcValue(NULL));
639 MemOps.push_back(Load);
640 args_to_use.push_back(Load);
641 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000642 }
643 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000644 // If we have any FPRs remaining, we may also have GPRs remaining.
645 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
646 // GPRs.
647 if (GPR_remaining > 0) {
648 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
649 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000650 }
Chris Lattner915fb302005-08-30 00:19:00 +0000651 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
652 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
653 --GPR_remaining;
654 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000655 }
Chris Lattner915fb302005-08-30 00:19:00 +0000656 } else {
657 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
658 Args[i].first, PtrOff,
659 DAG.getSrcValue(NULL)));
660 }
661 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
662 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000663 }
664 }
665 if (!MemOps.empty())
666 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
667 }
668
669 std::vector<MVT::ValueType> RetVals;
670 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000671 MVT::ValueType ActualRetTyVT = RetTyVT;
672 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
673 ActualRetTyVT = MVT::i32; // Promote result to i32.
674
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000675 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000676 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000677 RetVals.push_back(MVT::Other);
678
679 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
680 Chain, Callee, args_to_use), 0);
681 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
682 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
683 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000684 SDOperand RetVal = TheCall;
685
686 // If the result is a small value, add a note so that we keep track of the
687 // information about whether it is sign or zero extended.
688 if (RetTyVT != ActualRetTyVT) {
689 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
690 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
691 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
692 }
693
694 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000695}
696
Nate Begeman4a959452005-10-18 23:23:37 +0000697SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
698 SelectionDAG &DAG) {
699 if (Op.getValueType() == MVT::i64) {
700 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
701 DAG.getConstant(1, MVT::i32));
702 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
703 DAG.getConstant(0, MVT::i32));
704 return DAG.getNode(ISD::RET, MVT::Other, Chain, Lo, Hi);
705 } else {
706 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
707 }
708}
709
Nate Begeman21e463b2005-10-16 05:39:50 +0000710SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
711 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000712 // vastart just stores the address of the VarArgsFrameIndex slot into the
713 // memory location argument.
714 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
715 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
716 DAG.getSrcValue(VAListV));
717}
718
719std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000720PPCTargetLowering::LowerVAArg(SDOperand Chain,
721 SDOperand VAListP, Value *VAListV,
722 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000723 MVT::ValueType ArgVT = getValueType(ArgTy);
724
725 SDOperand VAList =
726 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
727 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
728 unsigned Amt;
729 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
730 Amt = 4;
731 else {
732 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
733 "Other types should have been promoted for varargs!");
734 Amt = 8;
735 }
736 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
737 DAG.getConstant(Amt, VAList.getValueType()));
738 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
739 VAList, VAListP, DAG.getSrcValue(VAListV));
740 return std::make_pair(Result, Chain);
741}
742
743
Nate Begeman21e463b2005-10-16 05:39:50 +0000744std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000745LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
746 SelectionDAG &DAG) {
747 assert(0 && "LowerFrameReturnAddress unimplemented");
748 abort();
749}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000750
751MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000752PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
753 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000754 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000755 MI->getOpcode() == PPC::SELECT_CC_F4 ||
756 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000757 "Unexpected instr type to insert");
758
759 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
760 // control-flow pattern. The incoming instruction knows the destination vreg
761 // to set, the condition code register to branch on, the true/false values to
762 // select between, and a branch opcode to use.
763 const BasicBlock *LLVM_BB = BB->getBasicBlock();
764 ilist<MachineBasicBlock>::iterator It = BB;
765 ++It;
766
767 // thisMBB:
768 // ...
769 // TrueVal = ...
770 // cmpTY ccX, r1, r2
771 // bCC copy1MBB
772 // fallthrough --> copy0MBB
773 MachineBasicBlock *thisMBB = BB;
774 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
775 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
776 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
777 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
778 MachineFunction *F = BB->getParent();
779 F->getBasicBlockList().insert(It, copy0MBB);
780 F->getBasicBlockList().insert(It, sinkMBB);
781 // Update machine-CFG edges
782 BB->addSuccessor(copy0MBB);
783 BB->addSuccessor(sinkMBB);
784
785 // copy0MBB:
786 // %FalseValue = ...
787 // # fallthrough to sinkMBB
788 BB = copy0MBB;
789
790 // Update machine-CFG edges
791 BB->addSuccessor(sinkMBB);
792
793 // sinkMBB:
794 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
795 // ...
796 BB = sinkMBB;
797 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
798 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
799 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
800
801 delete MI; // The pseudo instruction is gone now.
802 return BB;
803}
804