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