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