TargetLowering.cpp revision cba82f9339566cef76ecb062980e40913e6ccc23
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"
17using namespace llvm;
18
19TargetLowering::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  // All operations default to being supported.
24  memset(OpActions, 0, sizeof(OpActions));
25
26  IsLittleEndian = TD.isLittleEndian();
27  PointerTy = getValueType(TD.getIntPtrType());
28  memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
29}
30
31TargetLowering::~TargetLowering() {}
32
33/// setValueTypeAction - Set the action for a particular value type.  This
34/// assumes an action has not already been set for this value type.
35static void SetValueTypeAction(MVT::ValueType VT,
36                               TargetLowering::LegalizeAction Action,
37                               TargetLowering &TLI,
38                               MVT::ValueType *TransformToType,
39                               unsigned &ValueTypeActions) {
40  ValueTypeActions |= Action << (VT*2);
41  if (Action == TargetLowering::Promote) {
42    MVT::ValueType PromoteTo;
43    if (VT == MVT::f32)
44      PromoteTo = MVT::f64;
45    else {
46      unsigned LargerReg = VT+1;
47      while (!TLI.hasNativeSupportFor((MVT::ValueType)LargerReg)) {
48        ++LargerReg;
49        assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
50               "Nothing to promote to??");
51      }
52      PromoteTo = (MVT::ValueType)LargerReg;
53    }
54
55    assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
56           MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
57           "Can only promote from int->int or fp->fp!");
58    assert(VT < PromoteTo && "Must promote to a larger type!");
59    TransformToType[VT] = PromoteTo;
60  } else if (Action == TargetLowering::Expand) {
61    assert(MVT::isInteger(VT) && VT > MVT::i8 &&
62           "Cannot expand this type: target must support SOME integer reg!");
63    // Expand to the next smaller integer type!
64    TransformToType[VT] = (MVT::ValueType)(VT-1);
65  }
66}
67
68
69/// computeRegisterProperties - Once all of the register classes are added,
70/// this allows us to compute derived properties we expose.
71void TargetLowering::computeRegisterProperties() {
72  assert(MVT::LAST_VALUETYPE <= 16 &&
73         "Too many value types for ValueTypeActions to hold!");
74
75  // Everything defaults to one.
76  for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
77    NumElementsForVT[i] = 1;
78
79  // Find the largest integer register class.
80  unsigned LargestIntReg = MVT::i128;
81  for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
82    assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
83
84  // Every integer value type larger than this largest register takes twice as
85  // many registers to represent as the previous ValueType.
86  unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
87  for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
88    NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
89
90  // Inspect all of the ValueType's possible, deciding how to process them.
91  for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
92    // If we are expanding this type, expand it!
93    if (getNumElements((MVT::ValueType)IntReg) != 1)
94      SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
95                         ValueTypeActions);
96    else if (!hasNativeSupportFor((MVT::ValueType)IntReg))
97      // Otherwise, if we don't have native support, we must promote to a
98      // larger type.
99      SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
100                         TransformToType, ValueTypeActions);
101    else
102      TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
103
104  // If the target does not have native support for F32, promote it to F64.
105  if (!hasNativeSupportFor(MVT::f32))
106    SetValueTypeAction(MVT::f32, Promote, *this,
107                       TransformToType, ValueTypeActions);
108  else
109    TransformToType[MVT::f32] = MVT::f32;
110
111  assert(hasNativeSupportFor(MVT::f64) && "Target does not support FP?");
112  TransformToType[MVT::f64] = MVT::f64;
113}
114
115