TargetLowering.cpp revision f976c856fcc5055f3fc7d9f070d72c2d027c1d9d
1310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
2f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman//
3310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//                     The LLVM Compiler Infrastructure
4310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//
5310968cbbb564c4141d4bd418a746e8103560222Chris Lattner// This file was developed by the LLVM research group and is distributed under
6310968cbbb564c4141d4bd418a746e8103560222Chris Lattner// the University of Illinois Open Source License. See LICENSE.TXT for details.
7f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman//
8310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//===----------------------------------------------------------------------===//
9310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//
10310968cbbb564c4141d4bd418a746e8103560222Chris Lattner// This implements the TargetLowering class.
11310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//
12310968cbbb564c4141d4bd418a746e8103560222Chris Lattner//===----------------------------------------------------------------------===//
13310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
14310968cbbb564c4141d4bd418a746e8103560222Chris Lattner#include "llvm/Target/TargetLowering.h"
15310968cbbb564c4141d4bd418a746e8103560222Chris Lattner#include "llvm/Target/TargetMachine.h"
16310968cbbb564c4141d4bd418a746e8103560222Chris Lattner#include "llvm/CodeGen/SelectionDAG.h"
17310968cbbb564c4141d4bd418a746e8103560222Chris Lattnerusing namespace llvm;
18310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
19310968cbbb564c4141d4bd418a746e8103560222Chris LattnerTargetLowering::TargetLowering(TargetMachine &tm)
20bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner  : TM(tm), TD(TM.getTargetData()), ValueTypeActions(0) {
21310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  assert(ISD::BUILTIN_OP_END <= 128 &&
22310968cbbb564c4141d4bd418a746e8103560222Chris Lattner         "Fixed size array in TargetLowering is not large enough!");
23cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner  // All operations default to being supported.
24cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner  memset(OpActions, 0, sizeof(OpActions));
25310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
26310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  IsLittleEndian = TD.isLittleEndian();
27714b69d0479e0fab36195e36da4578742ed4b63dChris Lattner  ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD.getIntPtrType());
28d6e496732b9cc2c4b4986d015c7dba9032aaa14cChris Lattner  ShiftAmtHandling = Undefined;
29310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
30310968cbbb564c4141d4bd418a746e8103560222Chris Lattner}
31310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
32cba82f9339566cef76ecb062980e40913e6ccc23Chris LattnerTargetLowering::~TargetLowering() {}
33cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner
34bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner/// setValueTypeAction - Set the action for a particular value type.  This
35bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner/// assumes an action has not already been set for this value type.
36cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattnerstatic void SetValueTypeAction(MVT::ValueType VT,
37cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner                               TargetLowering::LegalizeAction Action,
38bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner                               TargetLowering &TLI,
39bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner                               MVT::ValueType *TransformToType,
40bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner                               unsigned &ValueTypeActions) {
41bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner  ValueTypeActions |= Action << (VT*2);
42cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner  if (Action == TargetLowering::Promote) {
43bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    MVT::ValueType PromoteTo;
44bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    if (VT == MVT::f32)
45bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner      PromoteTo = MVT::f64;
46bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    else {
47bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner      unsigned LargerReg = VT+1;
48bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner      while (!TLI.hasNativeSupportFor((MVT::ValueType)LargerReg)) {
49bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner        ++LargerReg;
50bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner        assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
51bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner               "Nothing to promote to??");
52bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner      }
53bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner      PromoteTo = (MVT::ValueType)LargerReg;
54bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    }
55bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner
56bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
57bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner           MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
58bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner           "Can only promote from int->int or fp->fp!");
59bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    assert(VT < PromoteTo && "Must promote to a larger type!");
60bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    TransformToType[VT] = PromoteTo;
61cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner  } else if (Action == TargetLowering::Expand) {
62bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    assert(MVT::isInteger(VT) && VT > MVT::i8 &&
63bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner           "Cannot expand this type: target must support SOME integer reg!");
64bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    // Expand to the next smaller integer type!
65bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    TransformToType[VT] = (MVT::ValueType)(VT-1);
66bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner  }
67bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner}
68bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner
69bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner
70310968cbbb564c4141d4bd418a746e8103560222Chris Lattner/// computeRegisterProperties - Once all of the register classes are added,
71310968cbbb564c4141d4bd418a746e8103560222Chris Lattner/// this allows us to compute derived properties we expose.
72310968cbbb564c4141d4bd418a746e8103560222Chris Lattnervoid TargetLowering::computeRegisterProperties() {
73bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner  assert(MVT::LAST_VALUETYPE <= 16 &&
74bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner         "Too many value types for ValueTypeActions to hold!");
75bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner
76310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // Everything defaults to one.
77310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
78310968cbbb564c4141d4bd418a746e8103560222Chris Lattner    NumElementsForVT[i] = 1;
79f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman
80310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // Find the largest integer register class.
81310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  unsigned LargestIntReg = MVT::i128;
82310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
83310968cbbb564c4141d4bd418a746e8103560222Chris Lattner    assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
84310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
85310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // Every integer value type larger than this largest register takes twice as
86310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  // many registers to represent as the previous ValueType.
87310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
88310968cbbb564c4141d4bd418a746e8103560222Chris Lattner  for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
89310968cbbb564c4141d4bd418a746e8103560222Chris Lattner    NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
90310968cbbb564c4141d4bd418a746e8103560222Chris Lattner
91bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner  // Inspect all of the ValueType's possible, deciding how to process them.
92bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner  for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
93bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    // If we are expanding this type, expand it!
94bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    if (getNumElements((MVT::ValueType)IntReg) != 1)
95cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner      SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
96bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner                         ValueTypeActions);
97bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner    else if (!hasNativeSupportFor((MVT::ValueType)IntReg))
98bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner      // Otherwise, if we don't have native support, we must promote to a
99bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner      // larger type.
100cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner      SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
101cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner                         TransformToType, ValueTypeActions);
102cfdfe4ce4afba6e6d2ef22bf07068ca6bf5bd75aChris Lattner    else
103cfdfe4ce4afba6e6d2ef22bf07068ca6bf5bd75aChris Lattner      TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
104f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman
105bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner  // If the target does not have native support for F32, promote it to F64.
106bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner  if (!hasNativeSupportFor(MVT::f32))
107cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner    SetValueTypeAction(MVT::f32, Promote, *this,
108cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner                       TransformToType, ValueTypeActions);
109cfdfe4ce4afba6e6d2ef22bf07068ca6bf5bd75aChris Lattner  else
110cfdfe4ce4afba6e6d2ef22bf07068ca6bf5bd75aChris Lattner    TransformToType[MVT::f32] = MVT::f32;
111cfdfe4ce4afba6e6d2ef22bf07068ca6bf5bd75aChris Lattner
112cfdfe4ce4afba6e6d2ef22bf07068ca6bf5bd75aChris Lattner  assert(hasNativeSupportFor(MVT::f64) && "Target does not support FP?");
113cfdfe4ce4afba6e6d2ef22bf07068ca6bf5bd75aChris Lattner  TransformToType[MVT::f64] = MVT::f64;
114bb97d81cc873e2c0914f2ece43832723cc936d24Chris Lattner}
115cba82f9339566cef76ecb062980e40913e6ccc23Chris Lattner
116