blob: 8271bca3d7e8add28e502ede0ff1f4a2a6ccc82e [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"
18#include "llvm/CodeGen/SelectionDAG.h"
19#include "llvm/Function.h"
20
21using namespace llvm;
22
23PPC32TargetLowering::PPC32TargetLowering(TargetMachine &TM)
24 : TargetLowering(TM) {
25
26 // Fold away setcc operations if possible.
27 setSetCCIsExpensive();
28
29 // Set up the register classes.
30 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
31 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
32 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
33
34 // PowerPC has no intrinsics for these particular operations
35 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
36 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
37 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
38
39 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
40 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
41 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
42
43 // PowerPC has no SREM/UREM instructions
44 setOperationAction(ISD::SREM, MVT::i32, Expand);
45 setOperationAction(ISD::UREM, MVT::i32, Expand);
46
47 // We don't support sin/cos/sqrt/fmod
48 setOperationAction(ISD::FSIN , MVT::f64, Expand);
49 setOperationAction(ISD::FCOS , MVT::f64, Expand);
50 setOperationAction(ISD::SREM , MVT::f64, Expand);
51 setOperationAction(ISD::FSIN , MVT::f32, Expand);
52 setOperationAction(ISD::FCOS , MVT::f32, Expand);
53 setOperationAction(ISD::SREM , MVT::f32, Expand);
54
55 // If we're enabling GP optimizations, use hardware square root
56 if (!TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) {
57 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
58 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
59 }
60
61 // PowerPC does not have CTPOP or CTTZ
62 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
63 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
64
65 // PowerPC does not have Select
66 setOperationAction(ISD::SELECT, MVT::i32, Expand);
67 setOperationAction(ISD::SELECT, MVT::f32, Expand);
68 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Nate Begeman7cbd5252005-08-16 19:49:35 +000069
70 // PowerPC does not have BRCOND* which requires SetCC
71 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
72 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000073
74 // PowerPC does not have FP_TO_UINT
75 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
76
Jim Laskeyad23c9d2005-08-17 00:40:22 +000077 // PowerPC does not have [U|S]INT_TO_FP
78 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
79 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
80
Chris Lattner7c5a3d32005-08-16 17:14:42 +000081 setSetCCResultContents(ZeroOrOneSetCCResult);
82 addLegalFPImmediate(+0.0); // Necessary for FSEL
83 addLegalFPImmediate(-0.0); //
84
85 computeRegisterProperties();
86}
87
88std::vector<SDOperand>
89PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
90 //
91 // add beautiful description of PPC stack frame format, or at least some docs
92 //
93 MachineFunction &MF = DAG.getMachineFunction();
94 MachineFrameInfo *MFI = MF.getFrameInfo();
95 MachineBasicBlock& BB = MF.front();
96 std::vector<SDOperand> ArgValues;
97
98 // Due to the rather complicated nature of the PowerPC ABI, rather than a
99 // fixed size array of physical args, for the sake of simplicity let the STL
100 // handle tracking them for us.
101 std::vector<unsigned> argVR, argPR, argOp;
102 unsigned ArgOffset = 24;
103 unsigned GPR_remaining = 8;
104 unsigned FPR_remaining = 13;
105 unsigned GPR_idx = 0, FPR_idx = 0;
106 static const unsigned GPR[] = {
107 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
108 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
109 };
110 static const unsigned FPR[] = {
111 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
112 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
113 };
114
115 // Add DAG nodes to load the arguments... On entry to a function on PPC,
116 // the arguments start at offset 24, although they are likely to be passed
117 // in registers.
118 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
119 SDOperand newroot, argt;
120 unsigned ObjSize;
121 bool needsLoad = false;
122 bool ArgLive = !I->use_empty();
123 MVT::ValueType ObjectVT = getValueType(I->getType());
124
125 switch (ObjectVT) {
126 default: assert(0 && "Unhandled argument type!");
127 case MVT::i1:
128 case MVT::i8:
129 case MVT::i16:
130 case MVT::i32:
131 ObjSize = 4;
132 if (!ArgLive) break;
133 if (GPR_remaining > 0) {
134 MF.addLiveIn(GPR[GPR_idx]);
Chris Lattnera8cd0152005-08-16 21:58:15 +0000135 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
136 GPR[GPR_idx], MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000137 if (ObjectVT != MVT::i32)
138 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
139 } else {
140 needsLoad = true;
141 }
142 break;
143 case MVT::i64: ObjSize = 8;
144 if (!ArgLive) break;
145 if (GPR_remaining > 0) {
146 SDOperand argHi, argLo;
147 MF.addLiveIn(GPR[GPR_idx]);
Chris Lattnera8cd0152005-08-16 21:58:15 +0000148 argHi = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000149 // If we have two or more remaining argument registers, then both halves
150 // of the i64 can be sourced from there. Otherwise, the lower half will
151 // have to come off the stack. This can happen when an i64 is preceded
152 // by 28 bytes of arguments.
153 if (GPR_remaining > 1) {
154 MF.addLiveIn(GPR[GPR_idx+1]);
Chris Lattnera8cd0152005-08-16 21:58:15 +0000155 argLo = DAG.getCopyFromReg(argHi, GPR[GPR_idx+1], MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000156 } else {
157 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
158 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
159 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
160 DAG.getSrcValue(NULL));
161 }
162 // Build the outgoing arg thingy
163 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
164 newroot = argLo;
165 } else {
166 needsLoad = true;
167 }
168 break;
169 case MVT::f32:
170 case MVT::f64:
171 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
172 if (!ArgLive) break;
173 if (FPR_remaining > 0) {
174 MF.addLiveIn(FPR[FPR_idx]);
Chris Lattnera8cd0152005-08-16 21:58:15 +0000175 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
176 FPR[FPR_idx], ObjectVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000177 --FPR_remaining;
178 ++FPR_idx;
179 } else {
180 needsLoad = true;
181 }
182 break;
183 }
184
185 // We need to load the argument to a virtual register if we determined above
186 // that we ran out of physical registers of the appropriate type
187 if (needsLoad) {
188 unsigned SubregOffset = 0;
189 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
190 if (ObjectVT == MVT::i16) SubregOffset = 2;
191 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
192 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
193 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
194 DAG.getConstant(SubregOffset, MVT::i32));
195 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
196 DAG.getSrcValue(NULL));
197 }
198
199 // Every 4 bytes of argument space consumes one of the GPRs available for
200 // argument passing.
201 if (GPR_remaining > 0) {
202 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
203 GPR_remaining -= delta;
204 GPR_idx += delta;
205 }
206 ArgOffset += ObjSize;
207 if (newroot.Val)
208 DAG.setRoot(newroot.getValue(1));
209
210 ArgValues.push_back(argt);
211 }
212
213 // If the function takes variable number of arguments, make a frame index for
214 // the start of the first vararg value... for expansion of llvm.va_start.
215 if (F.isVarArg()) {
216 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
217 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
218 // If this function is vararg, store any remaining integer argument regs
219 // to their spots on the stack so that they may be loaded by deferencing the
220 // result of va_next.
221 std::vector<SDOperand> MemOps;
222 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
223 MF.addLiveIn(GPR[GPR_idx]);
Chris Lattnera8cd0152005-08-16 21:58:15 +0000224 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000225 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
226 Val, FIN, DAG.getSrcValue(NULL));
227 MemOps.push_back(Store);
228 // Increment the address by four for the next argument to store
229 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
230 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
231 }
232 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
233 }
234
235 // Finally, inform the code generator which regs we return values in.
236 switch (getValueType(F.getReturnType())) {
237 default: assert(0 && "Unknown type!");
238 case MVT::isVoid: break;
239 case MVT::i1:
240 case MVT::i8:
241 case MVT::i16:
242 case MVT::i32:
243 MF.addLiveOut(PPC::R3);
244 break;
245 case MVT::i64:
246 MF.addLiveOut(PPC::R3);
247 MF.addLiveOut(PPC::R4);
248 break;
249 case MVT::f32:
250 case MVT::f64:
251 MF.addLiveOut(PPC::F1);
252 break;
253 }
254
255 return ArgValues;
256}
257
258std::pair<SDOperand, SDOperand>
259PPC32TargetLowering::LowerCallTo(SDOperand Chain,
260 const Type *RetTy, bool isVarArg,
261 unsigned CallingConv, bool isTailCall,
262 SDOperand Callee, ArgListTy &Args,
263 SelectionDAG &DAG) {
264 // args_to_use will accumulate outgoing args for the ISD::CALL case in
265 // SelectExpr to use to put the arguments in the appropriate registers.
266 std::vector<SDOperand> args_to_use;
267
268 // Count how many bytes are to be pushed on the stack, including the linkage
269 // area, and parameter passing area.
270 unsigned NumBytes = 24;
271
272 if (Args.empty()) {
273 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
274 DAG.getConstant(NumBytes, getPointerTy()));
275 } else {
276 for (unsigned i = 0, e = Args.size(); i != e; ++i)
277 switch (getValueType(Args[i].second)) {
278 default: assert(0 && "Unknown value type!");
279 case MVT::i1:
280 case MVT::i8:
281 case MVT::i16:
282 case MVT::i32:
283 case MVT::f32:
284 NumBytes += 4;
285 break;
286 case MVT::i64:
287 case MVT::f64:
288 NumBytes += 8;
289 break;
290 }
291
292 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
293 // plus 32 bytes of argument space in case any called code gets funky on us.
294 // (Required by ABI to support var arg)
295 if (NumBytes < 56) NumBytes = 56;
296
297 // Adjust the stack pointer for the new arguments...
298 // These operations are automatically eliminated by the prolog/epilog pass
299 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
300 DAG.getConstant(NumBytes, getPointerTy()));
301
302 // Set up a copy of the stack pointer for use loading and storing any
303 // arguments that may not fit in the registers available for argument
304 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000305 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
306 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000307
308 // Figure out which arguments are going to go in registers, and which in
309 // memory. Also, if this is a vararg function, floating point operations
310 // must be stored to our stack, and loaded into integer regs as well, if
311 // any integer regs are available for argument passing.
312 unsigned ArgOffset = 24;
313 unsigned GPR_remaining = 8;
314 unsigned FPR_remaining = 13;
315
316 std::vector<SDOperand> MemOps;
317 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
318 // PtrOff will be used to store the current argument to the stack if a
319 // register cannot be found for it.
320 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
321 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
322 MVT::ValueType ArgVT = getValueType(Args[i].second);
323
324 switch (ArgVT) {
325 default: assert(0 && "Unexpected ValueType for argument!");
326 case MVT::i1:
327 case MVT::i8:
328 case MVT::i16:
329 // Promote the integer to 32 bits. If the input type is signed use a
330 // sign extend, otherwise use a zero extend.
331 if (Args[i].second->isSigned())
332 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
333 else
334 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
335 // FALL THROUGH
336 case MVT::i32:
337 if (GPR_remaining > 0) {
338 args_to_use.push_back(Args[i].first);
339 --GPR_remaining;
340 } else {
341 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
342 Args[i].first, PtrOff,
343 DAG.getSrcValue(NULL)));
344 }
345 ArgOffset += 4;
346 break;
347 case MVT::i64:
348 // If we have one free GPR left, we can place the upper half of the i64
349 // in it, and store the other half to the stack. If we have two or more
350 // free GPRs, then we can pass both halves of the i64 in registers.
351 if (GPR_remaining > 0) {
352 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
353 Args[i].first, DAG.getConstant(1, MVT::i32));
354 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
355 Args[i].first, DAG.getConstant(0, MVT::i32));
356 args_to_use.push_back(Hi);
357 --GPR_remaining;
358 if (GPR_remaining > 0) {
359 args_to_use.push_back(Lo);
360 --GPR_remaining;
361 } else {
362 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
363 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
364 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
365 Lo, PtrOff, DAG.getSrcValue(NULL)));
366 }
367 } else {
368 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
369 Args[i].first, PtrOff,
370 DAG.getSrcValue(NULL)));
371 }
372 ArgOffset += 8;
373 break;
374 case MVT::f32:
375 case MVT::f64:
376 if (FPR_remaining > 0) {
377 args_to_use.push_back(Args[i].first);
378 --FPR_remaining;
379 if (isVarArg) {
380 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
381 Args[i].first, PtrOff,
382 DAG.getSrcValue(NULL));
383 MemOps.push_back(Store);
384 // Float varargs are always shadowed in available integer registers
385 if (GPR_remaining > 0) {
386 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
387 DAG.getSrcValue(NULL));
388 MemOps.push_back(Load);
389 args_to_use.push_back(Load);
390 --GPR_remaining;
391 }
392 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
393 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
394 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
395 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
396 DAG.getSrcValue(NULL));
397 MemOps.push_back(Load);
398 args_to_use.push_back(Load);
399 --GPR_remaining;
400 }
401 } else {
402 // If we have any FPRs remaining, we may also have GPRs remaining.
403 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
404 // GPRs.
405 if (GPR_remaining > 0) {
406 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
407 --GPR_remaining;
408 }
409 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
410 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
411 --GPR_remaining;
412 }
413 }
414 } else {
415 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
416 Args[i].first, PtrOff,
417 DAG.getSrcValue(NULL)));
418 }
419 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
420 break;
421 }
422 }
423 if (!MemOps.empty())
424 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
425 }
426
427 std::vector<MVT::ValueType> RetVals;
428 MVT::ValueType RetTyVT = getValueType(RetTy);
429 if (RetTyVT != MVT::isVoid)
430 RetVals.push_back(RetTyVT);
431 RetVals.push_back(MVT::Other);
432
433 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
434 Chain, Callee, args_to_use), 0);
435 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
436 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
437 DAG.getConstant(NumBytes, getPointerTy()));
438 return std::make_pair(TheCall, Chain);
439}
440
441SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
442 Value *VAListV, SelectionDAG &DAG) {
443 // vastart just stores the address of the VarArgsFrameIndex slot into the
444 // memory location argument.
445 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
446 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
447 DAG.getSrcValue(VAListV));
448}
449
450std::pair<SDOperand,SDOperand>
451PPC32TargetLowering::LowerVAArg(SDOperand Chain,
452 SDOperand VAListP, Value *VAListV,
453 const Type *ArgTy, SelectionDAG &DAG) {
454 MVT::ValueType ArgVT = getValueType(ArgTy);
455
456 SDOperand VAList =
457 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
458 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
459 unsigned Amt;
460 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
461 Amt = 4;
462 else {
463 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
464 "Other types should have been promoted for varargs!");
465 Amt = 8;
466 }
467 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
468 DAG.getConstant(Amt, VAList.getValueType()));
469 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
470 VAList, VAListP, DAG.getSrcValue(VAListV));
471 return std::make_pair(Result, Chain);
472}
473
474
475std::pair<SDOperand, SDOperand> PPC32TargetLowering::
476LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
477 SelectionDAG &DAG) {
478 assert(0 && "LowerFrameReturnAddress unimplemented");
479 abort();
480}