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