blob: 987ffe1612b869080a7d7b046138a2ede638374b [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
57 if (!TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) {
58 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
Jim Laskeyad23c9d2005-08-17 00:40:22 +000087 // PowerPC does not have [U|S]INT_TO_FP
88 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
89 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
90
Chris Lattner7c5a3d32005-08-16 17:14:42 +000091 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000092
93 computeRegisterProperties();
94}
95
Chris Lattner0b1e4e52005-08-26 17:36:52 +000096/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
97static bool isFloatingPointZero(SDOperand Op) {
98 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
99 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
100 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
101 // Maybe this has already been legalized into the constant pool?
102 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
103 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
104 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
105 }
106 return false;
107}
108
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000109/// LowerOperation - Provide custom lowering hooks for some operations.
110///
111SDOperand PPC32TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
112 switch (Op.getOpcode()) {
113 default: assert(0 && "Wasn't expecting to be able to lower this!");
114 case ISD::SELECT_CC:
115 // Turn FP only select_cc's into fsel instructions.
116 if (MVT::isFloatingPoint(Op.getOperand(0).getValueType()) &&
117 MVT::isFloatingPoint(Op.getOperand(2).getValueType())) {
118 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Chris Lattner6de08f42005-08-30 00:45:18 +0000119
120 // Cannot handle SETEQ/SETNE.
121 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
122
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000123 MVT::ValueType ResVT = Op.getValueType();
124 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
125 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
126 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
127
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000128 // If the RHS of the comparison is a 0.0, we don't need to do the
129 // subtraction at all.
130 if (isFloatingPointZero(RHS))
131 switch (CC) {
132 default: assert(0 && "Invalid FSEL condition"); abort();
133 case ISD::SETULT:
134 case ISD::SETLT:
135 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
136 case ISD::SETUGE:
137 case ISD::SETGE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000138 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000139 case ISD::SETUGT:
140 case ISD::SETGT:
141 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
142 case ISD::SETULE:
143 case ISD::SETLE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000144 return DAG.getNode(PPCISD::FSEL, ResVT,
145 DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000146 }
147
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000148 switch (CC) {
149 default: assert(0 && "Invalid FSEL condition"); abort();
150 case ISD::SETULT:
151 case ISD::SETLT:
Chris Lattner0bbea952005-08-26 20:25:03 +0000152 return DAG.getNode(PPCISD::FSEL, ResVT,
153 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), FV, TV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000154 case ISD::SETUGE:
155 case ISD::SETGE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000156 return DAG.getNode(PPCISD::FSEL, ResVT,
157 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000158 case ISD::SETUGT:
159 case ISD::SETGT:
Chris Lattner0bbea952005-08-26 20:25:03 +0000160 return DAG.getNode(PPCISD::FSEL, ResVT,
161 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), FV, TV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000162 case ISD::SETULE:
163 case ISD::SETLE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000164 return DAG.getNode(PPCISD::FSEL, ResVT,
165 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000166 }
167 }
168 break;
Chris Lattnerbc11c342005-08-31 20:23:54 +0000169 case ISD::SHL: {
170 assert(Op.getValueType() == MVT::i64 &&
171 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
172 // The generic code does a fine job expanding shift by a constant.
173 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
174
175 // Otherwise, expand into a bunch of logical ops. Note that these ops
176 // depend on the PPC behavior for oversized shift amounts.
177 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
178 DAG.getConstant(0, MVT::i32));
179 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
180 DAG.getConstant(1, MVT::i32));
181 SDOperand Amt = Op.getOperand(1);
182
183 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
184 DAG.getConstant(32, MVT::i32), Amt);
185 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
186 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
187 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
188 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
189 DAG.getConstant(-32U, MVT::i32));
190 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
191 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
192 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
193 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
194 }
195 case ISD::SRL: {
196 assert(Op.getValueType() == MVT::i64 &&
197 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
198 // The generic code does a fine job expanding shift by a constant.
199 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
200
201 // Otherwise, expand into a bunch of logical ops. Note that these ops
202 // depend on the PPC behavior for oversized shift amounts.
203 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
204 DAG.getConstant(0, MVT::i32));
205 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
206 DAG.getConstant(1, MVT::i32));
207 SDOperand Amt = Op.getOperand(1);
208
209 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
210 DAG.getConstant(32, MVT::i32), Amt);
211 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
212 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
213 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
214 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
215 DAG.getConstant(-32U, MVT::i32));
216 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
217 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
218 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
219 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
220 }
221 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000222 assert(Op.getValueType() == MVT::i64 &&
223 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
224 // The generic code does a fine job expanding shift by a constant.
225 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
226
227 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
228 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
229 DAG.getConstant(0, MVT::i32));
230 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
231 DAG.getConstant(1, MVT::i32));
232 SDOperand Amt = Op.getOperand(1);
233
234 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
235 DAG.getConstant(32, MVT::i32), Amt);
236 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
237 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
238 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
239 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
240 DAG.getConstant(-32U, MVT::i32));
241 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
242 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
243 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
244 Tmp4, Tmp6, ISD::SETLE);
245 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000246 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000247 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000248 return SDOperand();
249}
250
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000251std::vector<SDOperand>
252PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
253 //
254 // add beautiful description of PPC stack frame format, or at least some docs
255 //
256 MachineFunction &MF = DAG.getMachineFunction();
257 MachineFrameInfo *MFI = MF.getFrameInfo();
258 MachineBasicBlock& BB = MF.front();
259 std::vector<SDOperand> ArgValues;
260
261 // Due to the rather complicated nature of the PowerPC ABI, rather than a
262 // fixed size array of physical args, for the sake of simplicity let the STL
263 // handle tracking them for us.
264 std::vector<unsigned> argVR, argPR, argOp;
265 unsigned ArgOffset = 24;
266 unsigned GPR_remaining = 8;
267 unsigned FPR_remaining = 13;
268 unsigned GPR_idx = 0, FPR_idx = 0;
269 static const unsigned GPR[] = {
270 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
271 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
272 };
273 static const unsigned FPR[] = {
274 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
275 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
276 };
277
278 // Add DAG nodes to load the arguments... On entry to a function on PPC,
279 // the arguments start at offset 24, although they are likely to be passed
280 // in registers.
281 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
282 SDOperand newroot, argt;
283 unsigned ObjSize;
284 bool needsLoad = false;
285 bool ArgLive = !I->use_empty();
286 MVT::ValueType ObjectVT = getValueType(I->getType());
287
288 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000289 default: assert(0 && "Unhandled argument type!");
290 case MVT::i1:
291 case MVT::i8:
292 case MVT::i16:
293 case MVT::i32:
294 ObjSize = 4;
295 if (!ArgLive) break;
296 if (GPR_remaining > 0) {
297 MF.addLiveIn(GPR[GPR_idx]);
298 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
299 GPR[GPR_idx], MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000300 if (ObjectVT != MVT::i32) {
301 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
302 : ISD::AssertZext;
303 argt = DAG.getNode(AssertOp, MVT::i32, argt,
304 DAG.getValueType(ObjectVT));
305 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
306 }
Chris Lattner915fb302005-08-30 00:19:00 +0000307 } else {
308 needsLoad = true;
309 }
310 break;
311 case MVT::i64: ObjSize = 8;
312 if (!ArgLive) break;
313 if (GPR_remaining > 0) {
314 SDOperand argHi, argLo;
315 MF.addLiveIn(GPR[GPR_idx]);
316 argHi = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
317 // If we have two or more remaining argument registers, then both halves
318 // of the i64 can be sourced from there. Otherwise, the lower half will
319 // have to come off the stack. This can happen when an i64 is preceded
320 // by 28 bytes of arguments.
321 if (GPR_remaining > 1) {
322 MF.addLiveIn(GPR[GPR_idx+1]);
323 argLo = DAG.getCopyFromReg(argHi, GPR[GPR_idx+1], MVT::i32);
324 } else {
325 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
326 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
327 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
328 DAG.getSrcValue(NULL));
329 }
330 // Build the outgoing arg thingy
331 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
332 newroot = argLo;
333 } else {
334 needsLoad = true;
335 }
336 break;
337 case MVT::f32:
338 case MVT::f64:
339 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
340 if (!ArgLive) break;
341 if (FPR_remaining > 0) {
342 MF.addLiveIn(FPR[FPR_idx]);
343 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
344 FPR[FPR_idx], ObjectVT);
345 --FPR_remaining;
346 ++FPR_idx;
347 } else {
348 needsLoad = true;
349 }
350 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000351 }
352
353 // We need to load the argument to a virtual register if we determined above
354 // that we ran out of physical registers of the appropriate type
355 if (needsLoad) {
356 unsigned SubregOffset = 0;
357 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
358 if (ObjectVT == MVT::i16) SubregOffset = 2;
359 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
360 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
361 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
362 DAG.getConstant(SubregOffset, MVT::i32));
363 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
364 DAG.getSrcValue(NULL));
365 }
366
367 // Every 4 bytes of argument space consumes one of the GPRs available for
368 // argument passing.
369 if (GPR_remaining > 0) {
370 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
371 GPR_remaining -= delta;
372 GPR_idx += delta;
373 }
374 ArgOffset += ObjSize;
375 if (newroot.Val)
376 DAG.setRoot(newroot.getValue(1));
377
378 ArgValues.push_back(argt);
379 }
380
381 // If the function takes variable number of arguments, make a frame index for
382 // the start of the first vararg value... for expansion of llvm.va_start.
383 if (F.isVarArg()) {
384 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
385 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
386 // If this function is vararg, store any remaining integer argument regs
387 // to their spots on the stack so that they may be loaded by deferencing the
388 // result of va_next.
389 std::vector<SDOperand> MemOps;
390 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
391 MF.addLiveIn(GPR[GPR_idx]);
Chris Lattnera8cd0152005-08-16 21:58:15 +0000392 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000393 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
394 Val, FIN, DAG.getSrcValue(NULL));
395 MemOps.push_back(Store);
396 // Increment the address by four for the next argument to store
397 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
398 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
399 }
400 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
401 }
402
403 // Finally, inform the code generator which regs we return values in.
404 switch (getValueType(F.getReturnType())) {
405 default: assert(0 && "Unknown type!");
406 case MVT::isVoid: break;
407 case MVT::i1:
408 case MVT::i8:
409 case MVT::i16:
410 case MVT::i32:
411 MF.addLiveOut(PPC::R3);
412 break;
413 case MVT::i64:
414 MF.addLiveOut(PPC::R3);
415 MF.addLiveOut(PPC::R4);
416 break;
417 case MVT::f32:
418 case MVT::f64:
419 MF.addLiveOut(PPC::F1);
420 break;
421 }
422
423 return ArgValues;
424}
425
426std::pair<SDOperand, SDOperand>
427PPC32TargetLowering::LowerCallTo(SDOperand Chain,
428 const Type *RetTy, bool isVarArg,
429 unsigned CallingConv, bool isTailCall,
430 SDOperand Callee, ArgListTy &Args,
431 SelectionDAG &DAG) {
432 // args_to_use will accumulate outgoing args for the ISD::CALL case in
433 // SelectExpr to use to put the arguments in the appropriate registers.
434 std::vector<SDOperand> args_to_use;
435
436 // Count how many bytes are to be pushed on the stack, including the linkage
437 // area, and parameter passing area.
438 unsigned NumBytes = 24;
439
440 if (Args.empty()) {
441 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
442 DAG.getConstant(NumBytes, getPointerTy()));
443 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000444 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000445 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000446 default: assert(0 && "Unknown value type!");
447 case MVT::i1:
448 case MVT::i8:
449 case MVT::i16:
450 case MVT::i32:
451 case MVT::f32:
452 NumBytes += 4;
453 break;
454 case MVT::i64:
455 case MVT::f64:
456 NumBytes += 8;
457 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000458 }
Chris Lattner915fb302005-08-30 00:19:00 +0000459 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000460
Chris Lattner915fb302005-08-30 00:19:00 +0000461 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
462 // plus 32 bytes of argument space in case any called code gets funky on us.
463 // (Required by ABI to support var arg)
464 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000465
466 // Adjust the stack pointer for the new arguments...
467 // These operations are automatically eliminated by the prolog/epilog pass
468 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
469 DAG.getConstant(NumBytes, getPointerTy()));
470
471 // Set up a copy of the stack pointer for use loading and storing any
472 // arguments that may not fit in the registers available for argument
473 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000474 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
475 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000476
477 // Figure out which arguments are going to go in registers, and which in
478 // memory. Also, if this is a vararg function, floating point operations
479 // must be stored to our stack, and loaded into integer regs as well, if
480 // any integer regs are available for argument passing.
481 unsigned ArgOffset = 24;
482 unsigned GPR_remaining = 8;
483 unsigned FPR_remaining = 13;
484
485 std::vector<SDOperand> MemOps;
486 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
487 // PtrOff will be used to store the current argument to the stack if a
488 // register cannot be found for it.
489 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
490 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
491 MVT::ValueType ArgVT = getValueType(Args[i].second);
492
493 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000494 default: assert(0 && "Unexpected ValueType for argument!");
495 case MVT::i1:
496 case MVT::i8:
497 case MVT::i16:
498 // Promote the integer to 32 bits. If the input type is signed use a
499 // sign extend, otherwise use a zero extend.
500 if (Args[i].second->isSigned())
501 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
502 else
503 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
504 // FALL THROUGH
505 case MVT::i32:
506 if (GPR_remaining > 0) {
507 args_to_use.push_back(Args[i].first);
508 --GPR_remaining;
509 } else {
510 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
511 Args[i].first, PtrOff,
512 DAG.getSrcValue(NULL)));
513 }
514 ArgOffset += 4;
515 break;
516 case MVT::i64:
517 // If we have one free GPR left, we can place the upper half of the i64
518 // in it, and store the other half to the stack. If we have two or more
519 // free GPRs, then we can pass both halves of the i64 in registers.
520 if (GPR_remaining > 0) {
521 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
522 Args[i].first, DAG.getConstant(1, MVT::i32));
523 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
524 Args[i].first, DAG.getConstant(0, MVT::i32));
525 args_to_use.push_back(Hi);
526 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000527 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000528 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000529 --GPR_remaining;
530 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000531 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
532 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000533 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000534 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000535 }
Chris Lattner915fb302005-08-30 00:19:00 +0000536 } else {
537 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
538 Args[i].first, PtrOff,
539 DAG.getSrcValue(NULL)));
540 }
541 ArgOffset += 8;
542 break;
543 case MVT::f32:
544 case MVT::f64:
545 if (FPR_remaining > 0) {
546 args_to_use.push_back(Args[i].first);
547 --FPR_remaining;
548 if (isVarArg) {
549 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
550 Args[i].first, PtrOff,
551 DAG.getSrcValue(NULL));
552 MemOps.push_back(Store);
553 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000554 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000555 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
556 DAG.getSrcValue(NULL));
557 MemOps.push_back(Load);
558 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000559 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000560 }
561 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000562 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
563 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000564 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
565 DAG.getSrcValue(NULL));
566 MemOps.push_back(Load);
567 args_to_use.push_back(Load);
568 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000569 }
570 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000571 // If we have any FPRs remaining, we may also have GPRs remaining.
572 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
573 // GPRs.
574 if (GPR_remaining > 0) {
575 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
576 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000577 }
Chris Lattner915fb302005-08-30 00:19:00 +0000578 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
579 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
580 --GPR_remaining;
581 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000582 }
Chris Lattner915fb302005-08-30 00:19:00 +0000583 } else {
584 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
585 Args[i].first, PtrOff,
586 DAG.getSrcValue(NULL)));
587 }
588 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
589 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000590 }
591 }
592 if (!MemOps.empty())
593 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
594 }
595
596 std::vector<MVT::ValueType> RetVals;
597 MVT::ValueType RetTyVT = getValueType(RetTy);
598 if (RetTyVT != MVT::isVoid)
599 RetVals.push_back(RetTyVT);
600 RetVals.push_back(MVT::Other);
601
602 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
603 Chain, Callee, args_to_use), 0);
604 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
605 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
606 DAG.getConstant(NumBytes, getPointerTy()));
607 return std::make_pair(TheCall, Chain);
608}
609
610SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
611 Value *VAListV, SelectionDAG &DAG) {
612 // vastart just stores the address of the VarArgsFrameIndex slot into the
613 // memory location argument.
614 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
615 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
616 DAG.getSrcValue(VAListV));
617}
618
619std::pair<SDOperand,SDOperand>
620PPC32TargetLowering::LowerVAArg(SDOperand Chain,
621 SDOperand VAListP, Value *VAListV,
622 const Type *ArgTy, SelectionDAG &DAG) {
623 MVT::ValueType ArgVT = getValueType(ArgTy);
624
625 SDOperand VAList =
626 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
627 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
628 unsigned Amt;
629 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
630 Amt = 4;
631 else {
632 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
633 "Other types should have been promoted for varargs!");
634 Amt = 8;
635 }
636 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
637 DAG.getConstant(Amt, VAList.getValueType()));
638 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
639 VAList, VAListP, DAG.getSrcValue(VAListV));
640 return std::make_pair(Result, Chain);
641}
642
643
644std::pair<SDOperand, SDOperand> PPC32TargetLowering::
645LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
646 SelectionDAG &DAG) {
647 assert(0 && "LowerFrameReturnAddress unimplemented");
648 abort();
649}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000650
651MachineBasicBlock *
652PPC32TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
653 MachineBasicBlock *BB) {
654 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
655 MI->getOpcode() == PPC::SELECT_CC_FP) &&
656 "Unexpected instr type to insert");
657
658 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
659 // control-flow pattern. The incoming instruction knows the destination vreg
660 // to set, the condition code register to branch on, the true/false values to
661 // select between, and a branch opcode to use.
662 const BasicBlock *LLVM_BB = BB->getBasicBlock();
663 ilist<MachineBasicBlock>::iterator It = BB;
664 ++It;
665
666 // thisMBB:
667 // ...
668 // TrueVal = ...
669 // cmpTY ccX, r1, r2
670 // bCC copy1MBB
671 // fallthrough --> copy0MBB
672 MachineBasicBlock *thisMBB = BB;
673 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
674 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
675 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
676 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
677 MachineFunction *F = BB->getParent();
678 F->getBasicBlockList().insert(It, copy0MBB);
679 F->getBasicBlockList().insert(It, sinkMBB);
680 // Update machine-CFG edges
681 BB->addSuccessor(copy0MBB);
682 BB->addSuccessor(sinkMBB);
683
684 // copy0MBB:
685 // %FalseValue = ...
686 // # fallthrough to sinkMBB
687 BB = copy0MBB;
688
689 // Update machine-CFG edges
690 BB->addSuccessor(sinkMBB);
691
692 // sinkMBB:
693 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
694 // ...
695 BB = sinkMBB;
696 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
697 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
698 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
699
700 delete MI; // The pseudo instruction is gone now.
701 return BB;
702}
703