blob: fd9233f229c8b101695ea10dd081829885d11d34 [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);
Nate Begeman7cbd5252005-08-16 19:49:35 +000074
75 // PowerPC does not have BRCOND* which requires SetCC
76 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
77 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000078
79 // PowerPC does not have FP_TO_UINT
80 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
81
Jim Laskeyad23c9d2005-08-17 00:40:22 +000082 // PowerPC does not have [U|S]INT_TO_FP
83 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
84 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
85
Chris Lattner7c5a3d32005-08-16 17:14:42 +000086 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000087
88 computeRegisterProperties();
89}
90
Chris Lattner0b1e4e52005-08-26 17:36:52 +000091/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
92static bool isFloatingPointZero(SDOperand Op) {
93 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
94 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
95 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
96 // Maybe this has already been legalized into the constant pool?
97 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
98 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
99 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
100 }
101 return false;
102}
103
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000104/// LowerOperation - Provide custom lowering hooks for some operations.
105///
106SDOperand PPC32TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
107 switch (Op.getOpcode()) {
108 default: assert(0 && "Wasn't expecting to be able to lower this!");
109 case ISD::SELECT_CC:
110 // Turn FP only select_cc's into fsel instructions.
111 if (MVT::isFloatingPoint(Op.getOperand(0).getValueType()) &&
112 MVT::isFloatingPoint(Op.getOperand(2).getValueType())) {
113 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Chris Lattner6de08f42005-08-30 00:45:18 +0000114
115 // Cannot handle SETEQ/SETNE.
116 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
117
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000118 MVT::ValueType ResVT = Op.getValueType();
119 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
120 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
121 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
122
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000123 // If the RHS of the comparison is a 0.0, we don't need to do the
124 // subtraction at all.
125 if (isFloatingPointZero(RHS))
126 switch (CC) {
127 default: assert(0 && "Invalid FSEL condition"); abort();
128 case ISD::SETULT:
129 case ISD::SETLT:
130 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
131 case ISD::SETUGE:
132 case ISD::SETGE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000133 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000134 case ISD::SETUGT:
135 case ISD::SETGT:
136 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
137 case ISD::SETULE:
138 case ISD::SETLE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000139 return DAG.getNode(PPCISD::FSEL, ResVT,
140 DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000141 }
142
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000143 switch (CC) {
144 default: assert(0 && "Invalid FSEL condition"); abort();
145 case ISD::SETULT:
146 case ISD::SETLT:
Chris Lattner0bbea952005-08-26 20:25:03 +0000147 return DAG.getNode(PPCISD::FSEL, ResVT,
148 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), FV, TV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000149 case ISD::SETUGE:
150 case ISD::SETGE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000151 return DAG.getNode(PPCISD::FSEL, ResVT,
152 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000153 case ISD::SETUGT:
154 case ISD::SETGT:
Chris Lattner0bbea952005-08-26 20:25:03 +0000155 return DAG.getNode(PPCISD::FSEL, ResVT,
156 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), FV, TV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000157 case ISD::SETULE:
158 case ISD::SETLE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000159 return DAG.getNode(PPCISD::FSEL, ResVT,
160 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000161 }
162 }
163 break;
164 }
165 return SDOperand();
166}
167
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000168std::vector<SDOperand>
169PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
170 //
171 // add beautiful description of PPC stack frame format, or at least some docs
172 //
173 MachineFunction &MF = DAG.getMachineFunction();
174 MachineFrameInfo *MFI = MF.getFrameInfo();
175 MachineBasicBlock& BB = MF.front();
176 std::vector<SDOperand> ArgValues;
177
178 // Due to the rather complicated nature of the PowerPC ABI, rather than a
179 // fixed size array of physical args, for the sake of simplicity let the STL
180 // handle tracking them for us.
181 std::vector<unsigned> argVR, argPR, argOp;
182 unsigned ArgOffset = 24;
183 unsigned GPR_remaining = 8;
184 unsigned FPR_remaining = 13;
185 unsigned GPR_idx = 0, FPR_idx = 0;
186 static const unsigned GPR[] = {
187 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
188 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
189 };
190 static const unsigned FPR[] = {
191 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
192 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
193 };
194
195 // Add DAG nodes to load the arguments... On entry to a function on PPC,
196 // the arguments start at offset 24, although they are likely to be passed
197 // in registers.
198 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
199 SDOperand newroot, argt;
200 unsigned ObjSize;
201 bool needsLoad = false;
202 bool ArgLive = !I->use_empty();
203 MVT::ValueType ObjectVT = getValueType(I->getType());
204
205 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000206 default: assert(0 && "Unhandled argument type!");
207 case MVT::i1:
208 case MVT::i8:
209 case MVT::i16:
210 case MVT::i32:
211 ObjSize = 4;
212 if (!ArgLive) break;
213 if (GPR_remaining > 0) {
214 MF.addLiveIn(GPR[GPR_idx]);
215 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
216 GPR[GPR_idx], MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000217 if (ObjectVT != MVT::i32) {
218 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
219 : ISD::AssertZext;
220 argt = DAG.getNode(AssertOp, MVT::i32, argt,
221 DAG.getValueType(ObjectVT));
222 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
223 }
Chris Lattner915fb302005-08-30 00:19:00 +0000224 } else {
225 needsLoad = true;
226 }
227 break;
228 case MVT::i64: ObjSize = 8;
229 if (!ArgLive) break;
230 if (GPR_remaining > 0) {
231 SDOperand argHi, argLo;
232 MF.addLiveIn(GPR[GPR_idx]);
233 argHi = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
234 // If we have two or more remaining argument registers, then both halves
235 // of the i64 can be sourced from there. Otherwise, the lower half will
236 // have to come off the stack. This can happen when an i64 is preceded
237 // by 28 bytes of arguments.
238 if (GPR_remaining > 1) {
239 MF.addLiveIn(GPR[GPR_idx+1]);
240 argLo = DAG.getCopyFromReg(argHi, GPR[GPR_idx+1], MVT::i32);
241 } else {
242 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
243 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
244 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
245 DAG.getSrcValue(NULL));
246 }
247 // Build the outgoing arg thingy
248 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
249 newroot = argLo;
250 } else {
251 needsLoad = true;
252 }
253 break;
254 case MVT::f32:
255 case MVT::f64:
256 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
257 if (!ArgLive) break;
258 if (FPR_remaining > 0) {
259 MF.addLiveIn(FPR[FPR_idx]);
260 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
261 FPR[FPR_idx], ObjectVT);
262 --FPR_remaining;
263 ++FPR_idx;
264 } else {
265 needsLoad = true;
266 }
267 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000268 }
269
270 // We need to load the argument to a virtual register if we determined above
271 // that we ran out of physical registers of the appropriate type
272 if (needsLoad) {
273 unsigned SubregOffset = 0;
274 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
275 if (ObjectVT == MVT::i16) SubregOffset = 2;
276 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
277 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
278 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
279 DAG.getConstant(SubregOffset, MVT::i32));
280 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
281 DAG.getSrcValue(NULL));
282 }
283
284 // Every 4 bytes of argument space consumes one of the GPRs available for
285 // argument passing.
286 if (GPR_remaining > 0) {
287 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
288 GPR_remaining -= delta;
289 GPR_idx += delta;
290 }
291 ArgOffset += ObjSize;
292 if (newroot.Val)
293 DAG.setRoot(newroot.getValue(1));
294
295 ArgValues.push_back(argt);
296 }
297
298 // If the function takes variable number of arguments, make a frame index for
299 // the start of the first vararg value... for expansion of llvm.va_start.
300 if (F.isVarArg()) {
301 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
302 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
303 // If this function is vararg, store any remaining integer argument regs
304 // to their spots on the stack so that they may be loaded by deferencing the
305 // result of va_next.
306 std::vector<SDOperand> MemOps;
307 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
308 MF.addLiveIn(GPR[GPR_idx]);
Chris Lattnera8cd0152005-08-16 21:58:15 +0000309 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000310 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
311 Val, FIN, DAG.getSrcValue(NULL));
312 MemOps.push_back(Store);
313 // Increment the address by four for the next argument to store
314 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
315 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
316 }
317 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
318 }
319
320 // Finally, inform the code generator which regs we return values in.
321 switch (getValueType(F.getReturnType())) {
322 default: assert(0 && "Unknown type!");
323 case MVT::isVoid: break;
324 case MVT::i1:
325 case MVT::i8:
326 case MVT::i16:
327 case MVT::i32:
328 MF.addLiveOut(PPC::R3);
329 break;
330 case MVT::i64:
331 MF.addLiveOut(PPC::R3);
332 MF.addLiveOut(PPC::R4);
333 break;
334 case MVT::f32:
335 case MVT::f64:
336 MF.addLiveOut(PPC::F1);
337 break;
338 }
339
340 return ArgValues;
341}
342
343std::pair<SDOperand, SDOperand>
344PPC32TargetLowering::LowerCallTo(SDOperand Chain,
345 const Type *RetTy, bool isVarArg,
346 unsigned CallingConv, bool isTailCall,
347 SDOperand Callee, ArgListTy &Args,
348 SelectionDAG &DAG) {
349 // args_to_use will accumulate outgoing args for the ISD::CALL case in
350 // SelectExpr to use to put the arguments in the appropriate registers.
351 std::vector<SDOperand> args_to_use;
352
353 // Count how many bytes are to be pushed on the stack, including the linkage
354 // area, and parameter passing area.
355 unsigned NumBytes = 24;
356
357 if (Args.empty()) {
358 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
359 DAG.getConstant(NumBytes, getPointerTy()));
360 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000361 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000362 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000363 default: assert(0 && "Unknown value type!");
364 case MVT::i1:
365 case MVT::i8:
366 case MVT::i16:
367 case MVT::i32:
368 case MVT::f32:
369 NumBytes += 4;
370 break;
371 case MVT::i64:
372 case MVT::f64:
373 NumBytes += 8;
374 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000375 }
Chris Lattner915fb302005-08-30 00:19:00 +0000376 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000377
Chris Lattner915fb302005-08-30 00:19:00 +0000378 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
379 // plus 32 bytes of argument space in case any called code gets funky on us.
380 // (Required by ABI to support var arg)
381 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000382
383 // Adjust the stack pointer for the new arguments...
384 // These operations are automatically eliminated by the prolog/epilog pass
385 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
386 DAG.getConstant(NumBytes, getPointerTy()));
387
388 // Set up a copy of the stack pointer for use loading and storing any
389 // arguments that may not fit in the registers available for argument
390 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000391 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
392 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000393
394 // Figure out which arguments are going to go in registers, and which in
395 // memory. Also, if this is a vararg function, floating point operations
396 // must be stored to our stack, and loaded into integer regs as well, if
397 // any integer regs are available for argument passing.
398 unsigned ArgOffset = 24;
399 unsigned GPR_remaining = 8;
400 unsigned FPR_remaining = 13;
401
402 std::vector<SDOperand> MemOps;
403 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
404 // PtrOff will be used to store the current argument to the stack if a
405 // register cannot be found for it.
406 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
407 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
408 MVT::ValueType ArgVT = getValueType(Args[i].second);
409
410 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000411 default: assert(0 && "Unexpected ValueType for argument!");
412 case MVT::i1:
413 case MVT::i8:
414 case MVT::i16:
415 // Promote the integer to 32 bits. If the input type is signed use a
416 // sign extend, otherwise use a zero extend.
417 if (Args[i].second->isSigned())
418 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
419 else
420 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
421 // FALL THROUGH
422 case MVT::i32:
423 if (GPR_remaining > 0) {
424 args_to_use.push_back(Args[i].first);
425 --GPR_remaining;
426 } else {
427 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
428 Args[i].first, PtrOff,
429 DAG.getSrcValue(NULL)));
430 }
431 ArgOffset += 4;
432 break;
433 case MVT::i64:
434 // If we have one free GPR left, we can place the upper half of the i64
435 // in it, and store the other half to the stack. If we have two or more
436 // free GPRs, then we can pass both halves of the i64 in registers.
437 if (GPR_remaining > 0) {
438 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
439 Args[i].first, DAG.getConstant(1, MVT::i32));
440 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
441 Args[i].first, DAG.getConstant(0, MVT::i32));
442 args_to_use.push_back(Hi);
443 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000444 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000445 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000446 --GPR_remaining;
447 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000448 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
449 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000450 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000451 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000452 }
Chris Lattner915fb302005-08-30 00:19:00 +0000453 } else {
454 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
455 Args[i].first, PtrOff,
456 DAG.getSrcValue(NULL)));
457 }
458 ArgOffset += 8;
459 break;
460 case MVT::f32:
461 case MVT::f64:
462 if (FPR_remaining > 0) {
463 args_to_use.push_back(Args[i].first);
464 --FPR_remaining;
465 if (isVarArg) {
466 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
467 Args[i].first, PtrOff,
468 DAG.getSrcValue(NULL));
469 MemOps.push_back(Store);
470 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000471 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000472 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
473 DAG.getSrcValue(NULL));
474 MemOps.push_back(Load);
475 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000476 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000477 }
478 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000479 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
480 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000481 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
482 DAG.getSrcValue(NULL));
483 MemOps.push_back(Load);
484 args_to_use.push_back(Load);
485 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000486 }
487 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000488 // If we have any FPRs remaining, we may also have GPRs remaining.
489 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
490 // GPRs.
491 if (GPR_remaining > 0) {
492 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
493 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000494 }
Chris Lattner915fb302005-08-30 00:19:00 +0000495 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
496 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
497 --GPR_remaining;
498 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000499 }
Chris Lattner915fb302005-08-30 00:19:00 +0000500 } else {
501 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
502 Args[i].first, PtrOff,
503 DAG.getSrcValue(NULL)));
504 }
505 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
506 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000507 }
508 }
509 if (!MemOps.empty())
510 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
511 }
512
513 std::vector<MVT::ValueType> RetVals;
514 MVT::ValueType RetTyVT = getValueType(RetTy);
515 if (RetTyVT != MVT::isVoid)
516 RetVals.push_back(RetTyVT);
517 RetVals.push_back(MVT::Other);
518
519 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
520 Chain, Callee, args_to_use), 0);
521 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
522 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
523 DAG.getConstant(NumBytes, getPointerTy()));
524 return std::make_pair(TheCall, Chain);
525}
526
527SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
528 Value *VAListV, SelectionDAG &DAG) {
529 // vastart just stores the address of the VarArgsFrameIndex slot into the
530 // memory location argument.
531 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
532 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
533 DAG.getSrcValue(VAListV));
534}
535
536std::pair<SDOperand,SDOperand>
537PPC32TargetLowering::LowerVAArg(SDOperand Chain,
538 SDOperand VAListP, Value *VAListV,
539 const Type *ArgTy, SelectionDAG &DAG) {
540 MVT::ValueType ArgVT = getValueType(ArgTy);
541
542 SDOperand VAList =
543 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
544 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
545 unsigned Amt;
546 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
547 Amt = 4;
548 else {
549 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
550 "Other types should have been promoted for varargs!");
551 Amt = 8;
552 }
553 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
554 DAG.getConstant(Amt, VAList.getValueType()));
555 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
556 VAList, VAListP, DAG.getSrcValue(VAListV));
557 return std::make_pair(Result, Chain);
558}
559
560
561std::pair<SDOperand, SDOperand> PPC32TargetLowering::
562LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
563 SelectionDAG &DAG) {
564 assert(0 && "LowerFrameReturnAddress unimplemented");
565 abort();
566}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000567
568MachineBasicBlock *
569PPC32TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
570 MachineBasicBlock *BB) {
571 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
572 MI->getOpcode() == PPC::SELECT_CC_FP) &&
573 "Unexpected instr type to insert");
574
575 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
576 // control-flow pattern. The incoming instruction knows the destination vreg
577 // to set, the condition code register to branch on, the true/false values to
578 // select between, and a branch opcode to use.
579 const BasicBlock *LLVM_BB = BB->getBasicBlock();
580 ilist<MachineBasicBlock>::iterator It = BB;
581 ++It;
582
583 // thisMBB:
584 // ...
585 // TrueVal = ...
586 // cmpTY ccX, r1, r2
587 // bCC copy1MBB
588 // fallthrough --> copy0MBB
589 MachineBasicBlock *thisMBB = BB;
590 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
591 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
592 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
593 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
594 MachineFunction *F = BB->getParent();
595 F->getBasicBlockList().insert(It, copy0MBB);
596 F->getBasicBlockList().insert(It, sinkMBB);
597 // Update machine-CFG edges
598 BB->addSuccessor(copy0MBB);
599 BB->addSuccessor(sinkMBB);
600
601 // copy0MBB:
602 // %FalseValue = ...
603 // # fallthrough to sinkMBB
604 BB = copy0MBB;
605
606 // Update machine-CFG edges
607 BB->addSuccessor(sinkMBB);
608
609 // sinkMBB:
610 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
611 // ...
612 BB = sinkMBB;
613 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
614 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
615 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
616
617 delete MI; // The pseudo instruction is gone now.
618 return BB;
619}
620