Move some information out of LegalizeDAG into the generic Target interface.
[oota-llvm.git] / lib / CodeGen / SelectionDAG / TargetLowering.cpp
1 //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements the TargetLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Target/TargetLowering.h"
15 #include "llvm/Target/TargetMachine.h"
16 #include "llvm/CodeGen/SelectionDAG.h"
17 using namespace llvm;
18
19 TargetLowering::TargetLowering(TargetMachine &tm)
20   : TM(tm), TD(TM.getTargetData()), ValueTypeActions(0) {
21   assert(ISD::BUILTIN_OP_END <= 128 &&
22          "Fixed size array in TargetLowering is not large enough!");
23
24   IsLittleEndian = TD.isLittleEndian();
25   PointerTy = getValueType(TD.getIntPtrType());
26   memset(UnsupportedOps, 0, 128*sizeof(short));
27   memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
28 }
29
30 /// setValueTypeAction - Set the action for a particular value type.  This
31 /// assumes an action has not already been set for this value type.
32 static void SetValueTypeAction(MVT::ValueType VT, unsigned Action,
33                                TargetLowering &TLI,
34                                MVT::ValueType *TransformToType,
35                                unsigned &ValueTypeActions) {
36   ValueTypeActions |= Action << (VT*2);
37   if (Action == 1 /*promote*/) {
38     MVT::ValueType PromoteTo;
39     if (VT == MVT::f32)
40       PromoteTo = MVT::f64;
41     else {
42       unsigned LargerReg = VT+1;
43       while (!TLI.hasNativeSupportFor((MVT::ValueType)LargerReg)) {
44         ++LargerReg;
45         assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
46                "Nothing to promote to??");
47       }
48       PromoteTo = (MVT::ValueType)LargerReg;
49     }
50
51     assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
52            MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
53            "Can only promote from int->int or fp->fp!");
54     assert(VT < PromoteTo && "Must promote to a larger type!");
55     TransformToType[VT] = PromoteTo;
56   } else if (Action == 2) {
57     assert(MVT::isInteger(VT) && VT > MVT::i8 &&
58            "Cannot expand this type: target must support SOME integer reg!");
59     // Expand to the next smaller integer type!
60     TransformToType[VT] = (MVT::ValueType)(VT-1);
61   }
62 }
63
64
65 /// computeRegisterProperties - Once all of the register classes are added,
66 /// this allows us to compute derived properties we expose.
67 void TargetLowering::computeRegisterProperties() {
68   assert(MVT::LAST_VALUETYPE <= 16 &&
69          "Too many value types for ValueTypeActions to hold!");
70
71   // Everything defaults to one.
72   for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
73     NumElementsForVT[i] = 1;
74   
75   // Find the largest integer register class.
76   unsigned LargestIntReg = MVT::i128;
77   for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
78     assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
79
80   // Every integer value type larger than this largest register takes twice as
81   // many registers to represent as the previous ValueType.
82   unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
83   for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
84     NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
85
86   // Inspect all of the ValueType's possible, deciding how to process them.
87   for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
88     // If we are expanding this type, expand it!
89     if (getNumElements((MVT::ValueType)IntReg) != 1)
90       SetValueTypeAction((MVT::ValueType)IntReg, 2, *this, TransformToType,
91                          ValueTypeActions);
92     else if (!hasNativeSupportFor((MVT::ValueType)IntReg))
93       // Otherwise, if we don't have native support, we must promote to a
94       // larger type.
95       SetValueTypeAction((MVT::ValueType)IntReg, 1, *this, TransformToType,
96                          ValueTypeActions);
97   
98   // If the target does not have native support for F32, promote it to F64.
99   if (!hasNativeSupportFor(MVT::f32))
100     SetValueTypeAction(MVT::f32, 1, *this, TransformToType, ValueTypeActions);
101 }