blob: f3b7f64f19104b097e02d6fd7b0d8a4644acddd4 [file] [log] [blame]
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001//===-- PPC32ISelLowering.cpp - PPC32 DAG Lowering Implementation ---------===//
2//
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//
10// This file implements the PPC32ISelLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PPC32ISelLowering.h"
15#include "PPC32TargetMachine.h"
16#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
25PPC32TargetLowering::PPC32TargetLowering(TargetMachine &TM)
26 : 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.
35 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
36 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
37 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
38
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);
55 setOperationAction(ISD::SREM , MVT::f64, Expand);
56 setOperationAction(ISD::FSIN , MVT::f32, Expand);
57 setOperationAction(ISD::FCOS , MVT::f32, Expand);
58 setOperationAction(ISD::SREM , MVT::f32, Expand);
59
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
Chris Lattnerbc11c342005-08-31 20:23:54 +000079 // PowerPC wants to expand i64 shifts itself.
80 setOperationAction(ISD::SHL, MVT::i64, Custom);
81 setOperationAction(ISD::SRL, MVT::i64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000082 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begeman7cbd5252005-08-16 19:49:35 +000083
84 // PowerPC does not have BRCOND* which requires SetCC
85 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
86 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000087
88 // PowerPC does not have FP_TO_UINT
89 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
90
Chris Lattnerf7605322005-08-31 21:09:52 +000091 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
92 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000093
Jim Laskeyad23c9d2005-08-17 00:40:22 +000094 // PowerPC does not have [U|S]INT_TO_FP
95 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
96 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
97
Chris Lattnere6ec9f22005-09-10 00:21:06 +000098 // PowerPC does not have truncstore for i1.
99 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
100
Nate Begemanc09eeec2005-09-06 22:03:27 +0000101 // 64 bit PowerPC implementations have instructions to facilitate conversion
102 // between i64 and fp.
103 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
104 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
105 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
106 }
107
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000108 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000109
110 computeRegisterProperties();
111}
112
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000113/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
114static bool isFloatingPointZero(SDOperand Op) {
115 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
116 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
117 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
118 // Maybe this has already been legalized into the constant pool?
119 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
120 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
121 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
122 }
123 return false;
124}
125
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000126/// LowerOperation - Provide custom lowering hooks for some operations.
127///
128SDOperand PPC32TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
129 switch (Op.getOpcode()) {
130 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000131 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000132 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
133 switch (Op.getValueType()) {
134 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
135 case MVT::i32:
136 Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Op.getOperand(0));
137 break;
138 case MVT::i64:
139 Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Op.getOperand(0));
140 break;
141 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000142
143 int FrameIdx =
144 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
145 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
146 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
147 Op, FI, DAG.getSrcValue(0));
Nate Begemanc09eeec2005-09-06 22:03:27 +0000148 if (Op.getOpcode() == PPCISD::FCTIDZ) {
149 Op = DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
150 } else {
151 FI = DAG.getNode(ISD::ADD, MVT::i32, FI, DAG.getConstant(4, MVT::i32));
152 Op = DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
153 }
154 return Op;
155 }
156 case ISD::SINT_TO_FP: {
157 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
158 "Unhandled SINT_TO_FP type in custom expander!");
159 int FrameIdx =
160 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
161 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
162 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
163 Op.getOperand(0), FI, DAG.getSrcValue(0));
164 SDOperand LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
165 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, LD);
166 if (MVT::f32 == Op.getValueType())
167 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
168 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000169 }
170 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000171 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000172 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
173 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
174 break;
175
176 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
177
178 // Cannot handle SETEQ/SETNE.
179 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
180
181 MVT::ValueType ResVT = Op.getValueType();
182 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
183 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
184 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000185
Chris Lattnerf7605322005-08-31 21:09:52 +0000186 // If the RHS of the comparison is a 0.0, we don't need to do the
187 // subtraction at all.
188 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000189 switch (CC) {
190 default: assert(0 && "Invalid FSEL condition"); abort();
191 case ISD::SETULT:
192 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000193 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000194 case ISD::SETUGE:
195 case ISD::SETGE:
Chris Lattnerf7605322005-08-31 21:09:52 +0000196 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000197 case ISD::SETUGT:
198 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000199 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000200 case ISD::SETULE:
201 case ISD::SETLE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000202 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattnerf7605322005-08-31 21:09:52 +0000203 DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000204 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000205
206 switch (CC) {
207 default: assert(0 && "Invalid FSEL condition"); abort();
208 case ISD::SETULT:
209 case ISD::SETLT:
210 return DAG.getNode(PPCISD::FSEL, ResVT,
211 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), FV, TV);
212 case ISD::SETUGE:
213 case ISD::SETGE:
214 return DAG.getNode(PPCISD::FSEL, ResVT,
215 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), TV, FV);
216 case ISD::SETUGT:
217 case ISD::SETGT:
218 return DAG.getNode(PPCISD::FSEL, ResVT,
219 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), FV, TV);
220 case ISD::SETULE:
221 case ISD::SETLE:
222 return DAG.getNode(PPCISD::FSEL, ResVT,
223 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000224 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000225 break;
226 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000227 case ISD::SHL: {
228 assert(Op.getValueType() == MVT::i64 &&
229 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
230 // The generic code does a fine job expanding shift by a constant.
231 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
232
233 // Otherwise, expand into a bunch of logical ops. Note that these ops
234 // depend on the PPC behavior for oversized shift amounts.
235 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
236 DAG.getConstant(0, MVT::i32));
237 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
238 DAG.getConstant(1, MVT::i32));
239 SDOperand Amt = Op.getOperand(1);
240
241 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
242 DAG.getConstant(32, MVT::i32), Amt);
243 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
244 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
245 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
246 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
247 DAG.getConstant(-32U, MVT::i32));
248 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
249 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
250 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
251 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
252 }
253 case ISD::SRL: {
254 assert(Op.getValueType() == MVT::i64 &&
255 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
256 // The generic code does a fine job expanding shift by a constant.
257 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
258
259 // Otherwise, expand into a bunch of logical ops. Note that these ops
260 // depend on the PPC behavior for oversized shift amounts.
261 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
262 DAG.getConstant(0, MVT::i32));
263 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
264 DAG.getConstant(1, MVT::i32));
265 SDOperand Amt = Op.getOperand(1);
266
267 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
268 DAG.getConstant(32, MVT::i32), Amt);
269 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
270 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
271 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
272 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
273 DAG.getConstant(-32U, MVT::i32));
274 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
275 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
276 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
277 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
278 }
279 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000280 assert(Op.getValueType() == MVT::i64 &&
281 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
282 // The generic code does a fine job expanding shift by a constant.
283 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
284
285 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
286 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
287 DAG.getConstant(0, MVT::i32));
288 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
289 DAG.getConstant(1, MVT::i32));
290 SDOperand Amt = Op.getOperand(1);
291
292 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
293 DAG.getConstant(32, MVT::i32), Amt);
294 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
295 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
296 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
297 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
298 DAG.getConstant(-32U, MVT::i32));
299 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
300 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
301 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
302 Tmp4, Tmp6, ISD::SETLE);
303 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000304 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000305 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000306 return SDOperand();
307}
308
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000309std::vector<SDOperand>
310PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
311 //
312 // add beautiful description of PPC stack frame format, or at least some docs
313 //
314 MachineFunction &MF = DAG.getMachineFunction();
315 MachineFrameInfo *MFI = MF.getFrameInfo();
316 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000317 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000318 std::vector<SDOperand> ArgValues;
319
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000320 unsigned ArgOffset = 24;
321 unsigned GPR_remaining = 8;
322 unsigned FPR_remaining = 13;
323 unsigned GPR_idx = 0, FPR_idx = 0;
324 static const unsigned GPR[] = {
325 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
326 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
327 };
328 static const unsigned FPR[] = {
329 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
330 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
331 };
332
333 // Add DAG nodes to load the arguments... On entry to a function on PPC,
334 // the arguments start at offset 24, although they are likely to be passed
335 // in registers.
336 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
337 SDOperand newroot, argt;
338 unsigned ObjSize;
339 bool needsLoad = false;
340 bool ArgLive = !I->use_empty();
341 MVT::ValueType ObjectVT = getValueType(I->getType());
342
343 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000344 default: assert(0 && "Unhandled argument type!");
345 case MVT::i1:
346 case MVT::i8:
347 case MVT::i16:
348 case MVT::i32:
349 ObjSize = 4;
350 if (!ArgLive) break;
351 if (GPR_remaining > 0) {
Chris Lattner7b738342005-09-13 19:33:40 +0000352 unsigned VReg = RegMap->createVirtualRegister(&PPC32::GPRCRegClass);
353 MF.addLiveIn(GPR[GPR_idx], VReg);
354 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000355 if (ObjectVT != MVT::i32) {
356 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
357 : ISD::AssertZext;
358 argt = DAG.getNode(AssertOp, MVT::i32, argt,
359 DAG.getValueType(ObjectVT));
360 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
361 }
Chris Lattner915fb302005-08-30 00:19:00 +0000362 } else {
363 needsLoad = true;
364 }
365 break;
366 case MVT::i64: ObjSize = 8;
367 if (!ArgLive) break;
368 if (GPR_remaining > 0) {
369 SDOperand argHi, argLo;
Chris Lattner7b738342005-09-13 19:33:40 +0000370 unsigned VReg = RegMap->createVirtualRegister(&PPC32::GPRCRegClass);
371 MF.addLiveIn(GPR[GPR_idx], VReg);
372 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000373 // If we have two or more remaining argument registers, then both halves
374 // of the i64 can be sourced from there. Otherwise, the lower half will
375 // have to come off the stack. This can happen when an i64 is preceded
376 // by 28 bytes of arguments.
377 if (GPR_remaining > 1) {
Chris Lattner7b738342005-09-13 19:33:40 +0000378 unsigned VReg = RegMap->createVirtualRegister(&PPC32::GPRCRegClass);
379 MF.addLiveIn(GPR[GPR_idx+1], VReg);
380 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000381 } else {
382 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
383 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
384 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
385 DAG.getSrcValue(NULL));
386 }
387 // Build the outgoing arg thingy
388 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
389 newroot = argLo;
390 } else {
391 needsLoad = true;
392 }
393 break;
394 case MVT::f32:
395 case MVT::f64:
396 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
397 if (!ArgLive) break;
398 if (FPR_remaining > 0) {
Chris Lattner7b738342005-09-13 19:33:40 +0000399 unsigned VReg = RegMap->createVirtualRegister(&PPC32::FPRCRegClass);
400 MF.addLiveIn(FPR[FPR_idx], VReg);
401 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000402 --FPR_remaining;
403 ++FPR_idx;
404 } else {
405 needsLoad = true;
406 }
407 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000408 }
409
410 // We need to load the argument to a virtual register if we determined above
411 // that we ran out of physical registers of the appropriate type
412 if (needsLoad) {
413 unsigned SubregOffset = 0;
414 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
415 if (ObjectVT == MVT::i16) SubregOffset = 2;
416 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
417 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
418 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
419 DAG.getConstant(SubregOffset, MVT::i32));
420 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
421 DAG.getSrcValue(NULL));
422 }
423
424 // Every 4 bytes of argument space consumes one of the GPRs available for
425 // argument passing.
426 if (GPR_remaining > 0) {
427 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
428 GPR_remaining -= delta;
429 GPR_idx += delta;
430 }
431 ArgOffset += ObjSize;
432 if (newroot.Val)
433 DAG.setRoot(newroot.getValue(1));
434
435 ArgValues.push_back(argt);
436 }
437
438 // If the function takes variable number of arguments, make a frame index for
439 // the start of the first vararg value... for expansion of llvm.va_start.
440 if (F.isVarArg()) {
441 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
442 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
443 // If this function is vararg, store any remaining integer argument regs
444 // to their spots on the stack so that they may be loaded by deferencing the
445 // result of va_next.
446 std::vector<SDOperand> MemOps;
447 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Chris Lattner7b738342005-09-13 19:33:40 +0000448 unsigned VReg = RegMap->createVirtualRegister(&PPC32::GPRCRegClass);
449 MF.addLiveIn(GPR[GPR_idx], VReg);
450 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000451 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
452 Val, FIN, DAG.getSrcValue(NULL));
453 MemOps.push_back(Store);
454 // Increment the address by four for the next argument to store
455 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
456 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
457 }
458 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
459 }
460
461 // Finally, inform the code generator which regs we return values in.
462 switch (getValueType(F.getReturnType())) {
463 default: assert(0 && "Unknown type!");
464 case MVT::isVoid: break;
465 case MVT::i1:
466 case MVT::i8:
467 case MVT::i16:
468 case MVT::i32:
469 MF.addLiveOut(PPC::R3);
470 break;
471 case MVT::i64:
472 MF.addLiveOut(PPC::R3);
473 MF.addLiveOut(PPC::R4);
474 break;
475 case MVT::f32:
476 case MVT::f64:
477 MF.addLiveOut(PPC::F1);
478 break;
479 }
480
481 return ArgValues;
482}
483
484std::pair<SDOperand, SDOperand>
485PPC32TargetLowering::LowerCallTo(SDOperand Chain,
486 const Type *RetTy, bool isVarArg,
487 unsigned CallingConv, bool isTailCall,
488 SDOperand Callee, ArgListTy &Args,
489 SelectionDAG &DAG) {
490 // args_to_use will accumulate outgoing args for the ISD::CALL case in
491 // SelectExpr to use to put the arguments in the appropriate registers.
492 std::vector<SDOperand> args_to_use;
493
494 // Count how many bytes are to be pushed on the stack, including the linkage
495 // area, and parameter passing area.
496 unsigned NumBytes = 24;
497
498 if (Args.empty()) {
499 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
500 DAG.getConstant(NumBytes, getPointerTy()));
501 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000502 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000503 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000504 default: assert(0 && "Unknown value type!");
505 case MVT::i1:
506 case MVT::i8:
507 case MVT::i16:
508 case MVT::i32:
509 case MVT::f32:
510 NumBytes += 4;
511 break;
512 case MVT::i64:
513 case MVT::f64:
514 NumBytes += 8;
515 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000516 }
Chris Lattner915fb302005-08-30 00:19:00 +0000517 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000518
Chris Lattner915fb302005-08-30 00:19:00 +0000519 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
520 // plus 32 bytes of argument space in case any called code gets funky on us.
521 // (Required by ABI to support var arg)
522 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000523
524 // Adjust the stack pointer for the new arguments...
525 // These operations are automatically eliminated by the prolog/epilog pass
526 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
527 DAG.getConstant(NumBytes, getPointerTy()));
528
529 // Set up a copy of the stack pointer for use loading and storing any
530 // arguments that may not fit in the registers available for argument
531 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000532 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
533 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000534
535 // Figure out which arguments are going to go in registers, and which in
536 // memory. Also, if this is a vararg function, floating point operations
537 // must be stored to our stack, and loaded into integer regs as well, if
538 // any integer regs are available for argument passing.
539 unsigned ArgOffset = 24;
540 unsigned GPR_remaining = 8;
541 unsigned FPR_remaining = 13;
542
543 std::vector<SDOperand> MemOps;
544 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
545 // PtrOff will be used to store the current argument to the stack if a
546 // register cannot be found for it.
547 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
548 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
549 MVT::ValueType ArgVT = getValueType(Args[i].second);
550
551 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000552 default: assert(0 && "Unexpected ValueType for argument!");
553 case MVT::i1:
554 case MVT::i8:
555 case MVT::i16:
556 // Promote the integer to 32 bits. If the input type is signed use a
557 // sign extend, otherwise use a zero extend.
558 if (Args[i].second->isSigned())
559 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
560 else
561 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
562 // FALL THROUGH
563 case MVT::i32:
564 if (GPR_remaining > 0) {
565 args_to_use.push_back(Args[i].first);
566 --GPR_remaining;
567 } else {
568 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
569 Args[i].first, PtrOff,
570 DAG.getSrcValue(NULL)));
571 }
572 ArgOffset += 4;
573 break;
574 case MVT::i64:
575 // If we have one free GPR left, we can place the upper half of the i64
576 // in it, and store the other half to the stack. If we have two or more
577 // free GPRs, then we can pass both halves of the i64 in registers.
578 if (GPR_remaining > 0) {
579 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
580 Args[i].first, DAG.getConstant(1, MVT::i32));
581 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
582 Args[i].first, DAG.getConstant(0, MVT::i32));
583 args_to_use.push_back(Hi);
584 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000585 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000586 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000587 --GPR_remaining;
588 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000589 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
590 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000591 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000592 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000593 }
Chris Lattner915fb302005-08-30 00:19:00 +0000594 } else {
595 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
596 Args[i].first, PtrOff,
597 DAG.getSrcValue(NULL)));
598 }
599 ArgOffset += 8;
600 break;
601 case MVT::f32:
602 case MVT::f64:
603 if (FPR_remaining > 0) {
604 args_to_use.push_back(Args[i].first);
605 --FPR_remaining;
606 if (isVarArg) {
607 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
608 Args[i].first, PtrOff,
609 DAG.getSrcValue(NULL));
610 MemOps.push_back(Store);
611 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000612 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000613 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
614 DAG.getSrcValue(NULL));
615 MemOps.push_back(Load);
616 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000617 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000618 }
619 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000620 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
621 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000622 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
623 DAG.getSrcValue(NULL));
624 MemOps.push_back(Load);
625 args_to_use.push_back(Load);
626 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000627 }
628 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000629 // If we have any FPRs remaining, we may also have GPRs remaining.
630 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
631 // GPRs.
632 if (GPR_remaining > 0) {
633 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
634 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000635 }
Chris Lattner915fb302005-08-30 00:19:00 +0000636 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
637 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
638 --GPR_remaining;
639 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000640 }
Chris Lattner915fb302005-08-30 00:19:00 +0000641 } else {
642 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
643 Args[i].first, PtrOff,
644 DAG.getSrcValue(NULL)));
645 }
646 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
647 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000648 }
649 }
650 if (!MemOps.empty())
651 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
652 }
653
654 std::vector<MVT::ValueType> RetVals;
655 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000656 MVT::ValueType ActualRetTyVT = RetTyVT;
657 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
658 ActualRetTyVT = MVT::i32; // Promote result to i32.
659
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000660 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000661 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000662 RetVals.push_back(MVT::Other);
663
664 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
665 Chain, Callee, args_to_use), 0);
666 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
667 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
668 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000669 SDOperand RetVal = TheCall;
670
671 // If the result is a small value, add a note so that we keep track of the
672 // information about whether it is sign or zero extended.
673 if (RetTyVT != ActualRetTyVT) {
674 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
675 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
676 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
677 }
678
679 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000680}
681
682SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
683 Value *VAListV, SelectionDAG &DAG) {
684 // vastart just stores the address of the VarArgsFrameIndex slot into the
685 // memory location argument.
686 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
687 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
688 DAG.getSrcValue(VAListV));
689}
690
691std::pair<SDOperand,SDOperand>
692PPC32TargetLowering::LowerVAArg(SDOperand Chain,
693 SDOperand VAListP, Value *VAListV,
694 const Type *ArgTy, SelectionDAG &DAG) {
695 MVT::ValueType ArgVT = getValueType(ArgTy);
696
697 SDOperand VAList =
698 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
699 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
700 unsigned Amt;
701 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
702 Amt = 4;
703 else {
704 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
705 "Other types should have been promoted for varargs!");
706 Amt = 8;
707 }
708 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
709 DAG.getConstant(Amt, VAList.getValueType()));
710 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
711 VAList, VAListP, DAG.getSrcValue(VAListV));
712 return std::make_pair(Result, Chain);
713}
714
715
716std::pair<SDOperand, SDOperand> PPC32TargetLowering::
717LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
718 SelectionDAG &DAG) {
719 assert(0 && "LowerFrameReturnAddress unimplemented");
720 abort();
721}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000722
723MachineBasicBlock *
724PPC32TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
725 MachineBasicBlock *BB) {
726 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
727 MI->getOpcode() == PPC::SELECT_CC_FP) &&
728 "Unexpected instr type to insert");
729
730 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
731 // control-flow pattern. The incoming instruction knows the destination vreg
732 // to set, the condition code register to branch on, the true/false values to
733 // select between, and a branch opcode to use.
734 const BasicBlock *LLVM_BB = BB->getBasicBlock();
735 ilist<MachineBasicBlock>::iterator It = BB;
736 ++It;
737
738 // thisMBB:
739 // ...
740 // TrueVal = ...
741 // cmpTY ccX, r1, r2
742 // bCC copy1MBB
743 // fallthrough --> copy0MBB
744 MachineBasicBlock *thisMBB = BB;
745 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
746 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
747 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
748 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
749 MachineFunction *F = BB->getParent();
750 F->getBasicBlockList().insert(It, copy0MBB);
751 F->getBasicBlockList().insert(It, sinkMBB);
752 // Update machine-CFG edges
753 BB->addSuccessor(copy0MBB);
754 BB->addSuccessor(sinkMBB);
755
756 // copy0MBB:
757 // %FalseValue = ...
758 // # fallthrough to sinkMBB
759 BB = copy0MBB;
760
761 // Update machine-CFG edges
762 BB->addSuccessor(sinkMBB);
763
764 // sinkMBB:
765 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
766 // ...
767 BB = sinkMBB;
768 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
769 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
770 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
771
772 delete MI; // The pseudo instruction is gone now.
773 return BB;
774}
775