blob: b4d4a170b368d102683f6dff8b7a5d4121309fd7 [file] [log] [blame]
Chris Lattner310968c2005-01-07 07:44:53 +00001//===-- 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)
Chris Lattnerbb97d812005-01-16 01:10:58 +000020 : TM(tm), TD(TM.getTargetData()), ValueTypeActions(0) {
Chris Lattner310968c2005-01-07 07:44:53 +000021 assert(ISD::BUILTIN_OP_END <= 128 &&
22 "Fixed size array in TargetLowering is not large enough!");
Chris Lattnercba82f92005-01-16 07:28:11 +000023 // All operations default to being supported.
24 memset(OpActions, 0, sizeof(OpActions));
Chris Lattner310968c2005-01-07 07:44:53 +000025
26 IsLittleEndian = TD.isLittleEndian();
27 PointerTy = getValueType(TD.getIntPtrType());
Chris Lattner310968c2005-01-07 07:44:53 +000028 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
29}
30
Chris Lattnercba82f92005-01-16 07:28:11 +000031TargetLowering::~TargetLowering() {}
32
Chris Lattnerbb97d812005-01-16 01:10:58 +000033/// setValueTypeAction - Set the action for a particular value type. This
34/// assumes an action has not already been set for this value type.
Chris Lattnercba82f92005-01-16 07:28:11 +000035static void SetValueTypeAction(MVT::ValueType VT,
36 TargetLowering::LegalizeAction Action,
Chris Lattnerbb97d812005-01-16 01:10:58 +000037 TargetLowering &TLI,
38 MVT::ValueType *TransformToType,
39 unsigned &ValueTypeActions) {
40 ValueTypeActions |= Action << (VT*2);
Chris Lattnercba82f92005-01-16 07:28:11 +000041 if (Action == TargetLowering::Promote) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000042 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;
Chris Lattnercba82f92005-01-16 07:28:11 +000060 } else if (Action == TargetLowering::Expand) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000061 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
Chris Lattner310968c2005-01-07 07:44:53 +000069/// computeRegisterProperties - Once all of the register classes are added,
70/// this allows us to compute derived properties we expose.
71void TargetLowering::computeRegisterProperties() {
Chris Lattnerbb97d812005-01-16 01:10:58 +000072 assert(MVT::LAST_VALUETYPE <= 16 &&
73 "Too many value types for ValueTypeActions to hold!");
74
Chris Lattner310968c2005-01-07 07:44:53 +000075 // 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];
Chris Lattner310968c2005-01-07 07:44:53 +000089
Chris Lattnerbb97d812005-01-16 01:10:58 +000090 // 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)
Chris Lattnercba82f92005-01-16 07:28:11 +000094 SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
Chris Lattnerbb97d812005-01-16 01:10:58 +000095 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.
Chris Lattnercba82f92005-01-16 07:28:11 +000099 SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
100 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000101 else
102 TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
Chris Lattnerbb97d812005-01-16 01:10:58 +0000103
104 // If the target does not have native support for F32, promote it to F64.
105 if (!hasNativeSupportFor(MVT::f32))
Chris Lattnercba82f92005-01-16 07:28:11 +0000106 SetValueTypeAction(MVT::f32, Promote, *this,
107 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000108 else
109 TransformToType[MVT::f32] = MVT::f32;
110
111 assert(hasNativeSupportFor(MVT::f64) && "Target does not support FP?");
112 TransformToType[MVT::f64] = MVT::f64;
Chris Lattnerbb97d812005-01-16 01:10:58 +0000113}
Chris Lattnercba82f92005-01-16 07:28:11 +0000114