blob: 340c0a478ad9f492adf465fccd92d21fbc5d644a [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 Lattner0b1e4e52005-08-26 17:36:52 +000020#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000021#include "llvm/Function.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000022using namespace llvm;
23
24PPC32TargetLowering::PPC32TargetLowering(TargetMachine &TM)
25 : TargetLowering(TM) {
26
27 // Fold away setcc operations if possible.
28 setSetCCIsExpensive();
29
30 // Set up the register classes.
31 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
32 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
33 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
34
35 // PowerPC has no intrinsics for these particular operations
36 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
37 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
38 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
39
40 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
41 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
42 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
43
44 // PowerPC has no SREM/UREM instructions
45 setOperationAction(ISD::SREM, MVT::i32, Expand);
46 setOperationAction(ISD::UREM, MVT::i32, Expand);
47
48 // We don't support sin/cos/sqrt/fmod
49 setOperationAction(ISD::FSIN , MVT::f64, Expand);
50 setOperationAction(ISD::FCOS , MVT::f64, Expand);
51 setOperationAction(ISD::SREM , MVT::f64, Expand);
52 setOperationAction(ISD::FSIN , MVT::f32, Expand);
53 setOperationAction(ISD::FCOS , MVT::f32, Expand);
54 setOperationAction(ISD::SREM , MVT::f32, Expand);
55
56 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000057 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000058 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
59 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
60 }
61
62 // PowerPC does not have CTPOP or CTTZ
63 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
64 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
65
66 // PowerPC does not have Select
67 setOperationAction(ISD::SELECT, MVT::i32, Expand);
68 setOperationAction(ISD::SELECT, MVT::f32, Expand);
69 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000070
Chris Lattner0b1e4e52005-08-26 17:36:52 +000071 // PowerPC wants to turn select_cc of FP into fsel when possible.
72 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
73 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000074
Chris Lattnerbc11c342005-08-31 20:23:54 +000075 // PowerPC wants to expand i64 shifts itself.
76 setOperationAction(ISD::SHL, MVT::i64, Custom);
77 setOperationAction(ISD::SRL, MVT::i64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000078 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begeman7cbd5252005-08-16 19:49:35 +000079
80 // 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 // 64 bit PowerPC implementations have instructions to facilitate conversion
98 // between i64 and fp.
99 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
100 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
101 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
102 }
103
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000104 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000105
106 computeRegisterProperties();
107}
108
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000109/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
110static bool isFloatingPointZero(SDOperand Op) {
111 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
112 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
113 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
114 // Maybe this has already been legalized into the constant pool?
115 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
116 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
117 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
118 }
119 return false;
120}
121
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000122/// LowerOperation - Provide custom lowering hooks for some operations.
123///
124SDOperand PPC32TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
125 switch (Op.getOpcode()) {
126 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000127 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000128 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
129 switch (Op.getValueType()) {
130 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
131 case MVT::i32:
132 Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Op.getOperand(0));
133 break;
134 case MVT::i64:
135 Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Op.getOperand(0));
136 break;
137 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000138
139 int FrameIdx =
140 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
141 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
142 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
143 Op, FI, DAG.getSrcValue(0));
Nate Begemanc09eeec2005-09-06 22:03:27 +0000144 if (Op.getOpcode() == PPCISD::FCTIDZ) {
145 Op = DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
146 } else {
147 FI = DAG.getNode(ISD::ADD, MVT::i32, FI, DAG.getConstant(4, MVT::i32));
148 Op = DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
149 }
150 return Op;
151 }
152 case ISD::SINT_TO_FP: {
153 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
154 "Unhandled SINT_TO_FP type in custom expander!");
155 int FrameIdx =
156 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
157 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
158 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
159 Op.getOperand(0), FI, DAG.getSrcValue(0));
160 SDOperand LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
161 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, LD);
162 if (MVT::f32 == Op.getValueType())
163 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
164 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000165 }
166 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000167 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000168 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
169 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
170 break;
171
172 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
173
174 // Cannot handle SETEQ/SETNE.
175 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
176
177 MVT::ValueType ResVT = Op.getValueType();
178 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
179 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
180 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000181
Chris Lattnerf7605322005-08-31 21:09:52 +0000182 // If the RHS of the comparison is a 0.0, we don't need to do the
183 // subtraction at all.
184 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000185 switch (CC) {
186 default: assert(0 && "Invalid FSEL condition"); abort();
187 case ISD::SETULT:
188 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000189 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000190 case ISD::SETUGE:
191 case ISD::SETGE:
Chris Lattnerf7605322005-08-31 21:09:52 +0000192 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000193 case ISD::SETUGT:
194 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000195 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000196 case ISD::SETULE:
197 case ISD::SETLE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000198 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattnerf7605322005-08-31 21:09:52 +0000199 DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000200 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000201
202 switch (CC) {
203 default: assert(0 && "Invalid FSEL condition"); abort();
204 case ISD::SETULT:
205 case ISD::SETLT:
206 return DAG.getNode(PPCISD::FSEL, ResVT,
207 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), FV, TV);
208 case ISD::SETUGE:
209 case ISD::SETGE:
210 return DAG.getNode(PPCISD::FSEL, ResVT,
211 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), TV, FV);
212 case ISD::SETUGT:
213 case ISD::SETGT:
214 return DAG.getNode(PPCISD::FSEL, ResVT,
215 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), FV, TV);
216 case ISD::SETULE:
217 case ISD::SETLE:
218 return DAG.getNode(PPCISD::FSEL, ResVT,
219 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000220 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000221 break;
222 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000223 case ISD::SHL: {
224 assert(Op.getValueType() == MVT::i64 &&
225 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
226 // The generic code does a fine job expanding shift by a constant.
227 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
228
229 // Otherwise, expand into a bunch of logical ops. Note that these ops
230 // depend on the PPC behavior for oversized shift amounts.
231 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
232 DAG.getConstant(0, MVT::i32));
233 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
234 DAG.getConstant(1, MVT::i32));
235 SDOperand Amt = Op.getOperand(1);
236
237 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
238 DAG.getConstant(32, MVT::i32), Amt);
239 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
240 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
241 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
242 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
243 DAG.getConstant(-32U, MVT::i32));
244 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
245 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
246 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
247 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
248 }
249 case ISD::SRL: {
250 assert(Op.getValueType() == MVT::i64 &&
251 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
252 // The generic code does a fine job expanding shift by a constant.
253 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
254
255 // Otherwise, expand into a bunch of logical ops. Note that these ops
256 // depend on the PPC behavior for oversized shift amounts.
257 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
258 DAG.getConstant(0, MVT::i32));
259 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
260 DAG.getConstant(1, MVT::i32));
261 SDOperand Amt = Op.getOperand(1);
262
263 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
264 DAG.getConstant(32, MVT::i32), Amt);
265 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
266 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
267 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
268 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
269 DAG.getConstant(-32U, MVT::i32));
270 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
271 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
272 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
273 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
274 }
275 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000276 assert(Op.getValueType() == MVT::i64 &&
277 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
278 // The generic code does a fine job expanding shift by a constant.
279 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
280
281 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
282 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
283 DAG.getConstant(0, MVT::i32));
284 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
285 DAG.getConstant(1, MVT::i32));
286 SDOperand Amt = Op.getOperand(1);
287
288 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
289 DAG.getConstant(32, MVT::i32), Amt);
290 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
291 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
292 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
293 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
294 DAG.getConstant(-32U, MVT::i32));
295 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
296 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
297 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
298 Tmp4, Tmp6, ISD::SETLE);
299 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000300 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000301 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000302 return SDOperand();
303}
304
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000305std::vector<SDOperand>
306PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
307 //
308 // add beautiful description of PPC stack frame format, or at least some docs
309 //
310 MachineFunction &MF = DAG.getMachineFunction();
311 MachineFrameInfo *MFI = MF.getFrameInfo();
312 MachineBasicBlock& BB = MF.front();
313 std::vector<SDOperand> ArgValues;
314
315 // Due to the rather complicated nature of the PowerPC ABI, rather than a
316 // fixed size array of physical args, for the sake of simplicity let the STL
317 // handle tracking them for us.
318 std::vector<unsigned> argVR, argPR, argOp;
319 unsigned ArgOffset = 24;
320 unsigned GPR_remaining = 8;
321 unsigned FPR_remaining = 13;
322 unsigned GPR_idx = 0, FPR_idx = 0;
323 static const unsigned GPR[] = {
324 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
325 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
326 };
327 static const unsigned FPR[] = {
328 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
329 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
330 };
331
332 // Add DAG nodes to load the arguments... On entry to a function on PPC,
333 // the arguments start at offset 24, although they are likely to be passed
334 // in registers.
335 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
336 SDOperand newroot, argt;
337 unsigned ObjSize;
338 bool needsLoad = false;
339 bool ArgLive = !I->use_empty();
340 MVT::ValueType ObjectVT = getValueType(I->getType());
341
342 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000343 default: assert(0 && "Unhandled argument type!");
344 case MVT::i1:
345 case MVT::i8:
346 case MVT::i16:
347 case MVT::i32:
348 ObjSize = 4;
349 if (!ArgLive) break;
350 if (GPR_remaining > 0) {
351 MF.addLiveIn(GPR[GPR_idx]);
352 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
353 GPR[GPR_idx], MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000354 if (ObjectVT != MVT::i32) {
355 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
356 : ISD::AssertZext;
357 argt = DAG.getNode(AssertOp, MVT::i32, argt,
358 DAG.getValueType(ObjectVT));
359 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
360 }
Chris Lattner915fb302005-08-30 00:19:00 +0000361 } else {
362 needsLoad = true;
363 }
364 break;
365 case MVT::i64: ObjSize = 8;
366 if (!ArgLive) break;
367 if (GPR_remaining > 0) {
368 SDOperand argHi, argLo;
369 MF.addLiveIn(GPR[GPR_idx]);
370 argHi = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
371 // If we have two or more remaining argument registers, then both halves
372 // of the i64 can be sourced from there. Otherwise, the lower half will
373 // have to come off the stack. This can happen when an i64 is preceded
374 // by 28 bytes of arguments.
375 if (GPR_remaining > 1) {
376 MF.addLiveIn(GPR[GPR_idx+1]);
377 argLo = DAG.getCopyFromReg(argHi, GPR[GPR_idx+1], MVT::i32);
378 } else {
379 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
380 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
381 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
382 DAG.getSrcValue(NULL));
383 }
384 // Build the outgoing arg thingy
385 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
386 newroot = argLo;
387 } else {
388 needsLoad = true;
389 }
390 break;
391 case MVT::f32:
392 case MVT::f64:
393 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
394 if (!ArgLive) break;
395 if (FPR_remaining > 0) {
396 MF.addLiveIn(FPR[FPR_idx]);
397 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
398 FPR[FPR_idx], ObjectVT);
399 --FPR_remaining;
400 ++FPR_idx;
401 } else {
402 needsLoad = true;
403 }
404 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000405 }
406
407 // We need to load the argument to a virtual register if we determined above
408 // that we ran out of physical registers of the appropriate type
409 if (needsLoad) {
410 unsigned SubregOffset = 0;
411 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
412 if (ObjectVT == MVT::i16) SubregOffset = 2;
413 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
414 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
415 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
416 DAG.getConstant(SubregOffset, MVT::i32));
417 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
418 DAG.getSrcValue(NULL));
419 }
420
421 // Every 4 bytes of argument space consumes one of the GPRs available for
422 // argument passing.
423 if (GPR_remaining > 0) {
424 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
425 GPR_remaining -= delta;
426 GPR_idx += delta;
427 }
428 ArgOffset += ObjSize;
429 if (newroot.Val)
430 DAG.setRoot(newroot.getValue(1));
431
432 ArgValues.push_back(argt);
433 }
434
435 // If the function takes variable number of arguments, make a frame index for
436 // the start of the first vararg value... for expansion of llvm.va_start.
437 if (F.isVarArg()) {
438 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
439 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
440 // If this function is vararg, store any remaining integer argument regs
441 // to their spots on the stack so that they may be loaded by deferencing the
442 // result of va_next.
443 std::vector<SDOperand> MemOps;
444 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
445 MF.addLiveIn(GPR[GPR_idx]);
Chris Lattnera8cd0152005-08-16 21:58:15 +0000446 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000447 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
448 Val, FIN, DAG.getSrcValue(NULL));
449 MemOps.push_back(Store);
450 // Increment the address by four for the next argument to store
451 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
452 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
453 }
454 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
455 }
456
457 // Finally, inform the code generator which regs we return values in.
458 switch (getValueType(F.getReturnType())) {
459 default: assert(0 && "Unknown type!");
460 case MVT::isVoid: break;
461 case MVT::i1:
462 case MVT::i8:
463 case MVT::i16:
464 case MVT::i32:
465 MF.addLiveOut(PPC::R3);
466 break;
467 case MVT::i64:
468 MF.addLiveOut(PPC::R3);
469 MF.addLiveOut(PPC::R4);
470 break;
471 case MVT::f32:
472 case MVT::f64:
473 MF.addLiveOut(PPC::F1);
474 break;
475 }
476
477 return ArgValues;
478}
479
480std::pair<SDOperand, SDOperand>
481PPC32TargetLowering::LowerCallTo(SDOperand Chain,
482 const Type *RetTy, bool isVarArg,
483 unsigned CallingConv, bool isTailCall,
484 SDOperand Callee, ArgListTy &Args,
485 SelectionDAG &DAG) {
486 // args_to_use will accumulate outgoing args for the ISD::CALL case in
487 // SelectExpr to use to put the arguments in the appropriate registers.
488 std::vector<SDOperand> args_to_use;
489
490 // Count how many bytes are to be pushed on the stack, including the linkage
491 // area, and parameter passing area.
492 unsigned NumBytes = 24;
493
494 if (Args.empty()) {
495 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
496 DAG.getConstant(NumBytes, getPointerTy()));
497 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000498 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000499 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000500 default: assert(0 && "Unknown value type!");
501 case MVT::i1:
502 case MVT::i8:
503 case MVT::i16:
504 case MVT::i32:
505 case MVT::f32:
506 NumBytes += 4;
507 break;
508 case MVT::i64:
509 case MVT::f64:
510 NumBytes += 8;
511 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000512 }
Chris Lattner915fb302005-08-30 00:19:00 +0000513 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000514
Chris Lattner915fb302005-08-30 00:19:00 +0000515 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
516 // plus 32 bytes of argument space in case any called code gets funky on us.
517 // (Required by ABI to support var arg)
518 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000519
520 // Adjust the stack pointer for the new arguments...
521 // These operations are automatically eliminated by the prolog/epilog pass
522 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
523 DAG.getConstant(NumBytes, getPointerTy()));
524
525 // Set up a copy of the stack pointer for use loading and storing any
526 // arguments that may not fit in the registers available for argument
527 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000528 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
529 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000530
531 // Figure out which arguments are going to go in registers, and which in
532 // memory. Also, if this is a vararg function, floating point operations
533 // must be stored to our stack, and loaded into integer regs as well, if
534 // any integer regs are available for argument passing.
535 unsigned ArgOffset = 24;
536 unsigned GPR_remaining = 8;
537 unsigned FPR_remaining = 13;
538
539 std::vector<SDOperand> MemOps;
540 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
541 // PtrOff will be used to store the current argument to the stack if a
542 // register cannot be found for it.
543 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
544 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
545 MVT::ValueType ArgVT = getValueType(Args[i].second);
546
547 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000548 default: assert(0 && "Unexpected ValueType for argument!");
549 case MVT::i1:
550 case MVT::i8:
551 case MVT::i16:
552 // Promote the integer to 32 bits. If the input type is signed use a
553 // sign extend, otherwise use a zero extend.
554 if (Args[i].second->isSigned())
555 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
556 else
557 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
558 // FALL THROUGH
559 case MVT::i32:
560 if (GPR_remaining > 0) {
561 args_to_use.push_back(Args[i].first);
562 --GPR_remaining;
563 } else {
564 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
565 Args[i].first, PtrOff,
566 DAG.getSrcValue(NULL)));
567 }
568 ArgOffset += 4;
569 break;
570 case MVT::i64:
571 // If we have one free GPR left, we can place the upper half of the i64
572 // in it, and store the other half to the stack. If we have two or more
573 // free GPRs, then we can pass both halves of the i64 in registers.
574 if (GPR_remaining > 0) {
575 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
576 Args[i].first, DAG.getConstant(1, MVT::i32));
577 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
578 Args[i].first, DAG.getConstant(0, MVT::i32));
579 args_to_use.push_back(Hi);
580 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000581 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000582 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000583 --GPR_remaining;
584 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000585 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
586 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000587 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000588 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000589 }
Chris Lattner915fb302005-08-30 00:19:00 +0000590 } else {
591 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
592 Args[i].first, PtrOff,
593 DAG.getSrcValue(NULL)));
594 }
595 ArgOffset += 8;
596 break;
597 case MVT::f32:
598 case MVT::f64:
599 if (FPR_remaining > 0) {
600 args_to_use.push_back(Args[i].first);
601 --FPR_remaining;
602 if (isVarArg) {
603 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
604 Args[i].first, PtrOff,
605 DAG.getSrcValue(NULL));
606 MemOps.push_back(Store);
607 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000608 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000609 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
610 DAG.getSrcValue(NULL));
611 MemOps.push_back(Load);
612 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000613 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000614 }
615 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000616 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
617 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000618 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
619 DAG.getSrcValue(NULL));
620 MemOps.push_back(Load);
621 args_to_use.push_back(Load);
622 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000623 }
624 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000625 // If we have any FPRs remaining, we may also have GPRs remaining.
626 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
627 // GPRs.
628 if (GPR_remaining > 0) {
629 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
630 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000631 }
Chris Lattner915fb302005-08-30 00:19:00 +0000632 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
633 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
634 --GPR_remaining;
635 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000636 }
Chris Lattner915fb302005-08-30 00:19:00 +0000637 } else {
638 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
639 Args[i].first, PtrOff,
640 DAG.getSrcValue(NULL)));
641 }
642 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
643 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000644 }
645 }
646 if (!MemOps.empty())
647 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
648 }
649
650 std::vector<MVT::ValueType> RetVals;
651 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000652 MVT::ValueType ActualRetTyVT = RetTyVT;
653 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
654 ActualRetTyVT = MVT::i32; // Promote result to i32.
655
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000656 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000657 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000658 RetVals.push_back(MVT::Other);
659
660 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
661 Chain, Callee, args_to_use), 0);
662 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
663 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
664 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000665 SDOperand RetVal = TheCall;
666
667 // If the result is a small value, add a note so that we keep track of the
668 // information about whether it is sign or zero extended.
669 if (RetTyVT != ActualRetTyVT) {
670 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
671 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
672 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
673 }
674
675 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000676}
677
678SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
679 Value *VAListV, SelectionDAG &DAG) {
680 // vastart just stores the address of the VarArgsFrameIndex slot into the
681 // memory location argument.
682 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
683 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
684 DAG.getSrcValue(VAListV));
685}
686
687std::pair<SDOperand,SDOperand>
688PPC32TargetLowering::LowerVAArg(SDOperand Chain,
689 SDOperand VAListP, Value *VAListV,
690 const Type *ArgTy, SelectionDAG &DAG) {
691 MVT::ValueType ArgVT = getValueType(ArgTy);
692
693 SDOperand VAList =
694 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
695 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
696 unsigned Amt;
697 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
698 Amt = 4;
699 else {
700 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
701 "Other types should have been promoted for varargs!");
702 Amt = 8;
703 }
704 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
705 DAG.getConstant(Amt, VAList.getValueType()));
706 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
707 VAList, VAListP, DAG.getSrcValue(VAListV));
708 return std::make_pair(Result, Chain);
709}
710
711
712std::pair<SDOperand, SDOperand> PPC32TargetLowering::
713LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
714 SelectionDAG &DAG) {
715 assert(0 && "LowerFrameReturnAddress unimplemented");
716 abort();
717}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000718
719MachineBasicBlock *
720PPC32TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
721 MachineBasicBlock *BB) {
722 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
723 MI->getOpcode() == PPC::SELECT_CC_FP) &&
724 "Unexpected instr type to insert");
725
726 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
727 // control-flow pattern. The incoming instruction knows the destination vreg
728 // to set, the condition code register to branch on, the true/false values to
729 // select between, and a branch opcode to use.
730 const BasicBlock *LLVM_BB = BB->getBasicBlock();
731 ilist<MachineBasicBlock>::iterator It = BB;
732 ++It;
733
734 // thisMBB:
735 // ...
736 // TrueVal = ...
737 // cmpTY ccX, r1, r2
738 // bCC copy1MBB
739 // fallthrough --> copy0MBB
740 MachineBasicBlock *thisMBB = BB;
741 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
742 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
743 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
744 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
745 MachineFunction *F = BB->getParent();
746 F->getBasicBlockList().insert(It, copy0MBB);
747 F->getBasicBlockList().insert(It, sinkMBB);
748 // Update machine-CFG edges
749 BB->addSuccessor(copy0MBB);
750 BB->addSuccessor(sinkMBB);
751
752 // copy0MBB:
753 // %FalseValue = ...
754 // # fallthrough to sinkMBB
755 BB = copy0MBB;
756
757 // Update machine-CFG edges
758 BB->addSuccessor(sinkMBB);
759
760 // sinkMBB:
761 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
762 // ...
763 BB = sinkMBB;
764 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
765 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
766 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
767
768 delete MI; // The pseudo instruction is gone now.
769 return BB;
770}
771