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