1 //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This implements the TargetLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/TargetLowering.h"
15 #include "llvm/Target/TargetMachine.h"
16 #include "llvm/CodeGen/SelectionDAG.h"
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!");
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*));
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,
34 MVT::ValueType *TransformToType,
35 unsigned &ValueTypeActions) {
36 ValueTypeActions |= Action << (VT*2);
37 if (Action == 1 /*promote*/) {
38 MVT::ValueType PromoteTo;
42 unsigned LargerReg = VT+1;
43 while (!TLI.hasNativeSupportFor((MVT::ValueType)LargerReg)) {
45 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
46 "Nothing to promote to??");
48 PromoteTo = (MVT::ValueType)LargerReg;
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);
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!");
71 // Everything defaults to one.
72 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
73 NumElementsForVT[i] = 1;
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!");
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];
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,
92 else if (!hasNativeSupportFor((MVT::ValueType)IntReg))
93 // Otherwise, if we don't have native support, we must promote to a
95 SetValueTypeAction((MVT::ValueType)IntReg, 1, *this, TransformToType,
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);