blob: 7d720f149f783b13414edf8cca33e7f424febc06 [file] [log] [blame]
Chris Lattner310968c2005-01-07 07:44:53 +00001//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattner310968c2005-01-07 07:44:53 +00003// 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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattner310968c2005-01-07 07:44:53 +00008//===----------------------------------------------------------------------===//
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();
Chris Lattner714b69d2005-01-16 23:59:48 +000027 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD.getIntPtrType());
Chris Lattnerd6e49672005-01-19 03:36:14 +000028 ShiftAmtHandling = Undefined;
Chris Lattner310968c2005-01-07 07:44:53 +000029 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
30}
31
Chris Lattnercba82f92005-01-16 07:28:11 +000032TargetLowering::~TargetLowering() {}
33
Chris Lattnerbb97d812005-01-16 01:10:58 +000034/// setValueTypeAction - Set the action for a particular value type. This
35/// assumes an action has not already been set for this value type.
Chris Lattnercba82f92005-01-16 07:28:11 +000036static void SetValueTypeAction(MVT::ValueType VT,
37 TargetLowering::LegalizeAction Action,
Chris Lattnerbb97d812005-01-16 01:10:58 +000038 TargetLowering &TLI,
39 MVT::ValueType *TransformToType,
40 unsigned &ValueTypeActions) {
41 ValueTypeActions |= Action << (VT*2);
Chris Lattnercba82f92005-01-16 07:28:11 +000042 if (Action == TargetLowering::Promote) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000043 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;
Chris Lattnercba82f92005-01-16 07:28:11 +000061 } else if (Action == TargetLowering::Expand) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000062 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
Chris Lattner310968c2005-01-07 07:44:53 +000070/// computeRegisterProperties - Once all of the register classes are added,
71/// this allows us to compute derived properties we expose.
72void TargetLowering::computeRegisterProperties() {
Chris Lattnerbb97d812005-01-16 01:10:58 +000073 assert(MVT::LAST_VALUETYPE <= 16 &&
74 "Too many value types for ValueTypeActions to hold!");
75
Chris Lattner310968c2005-01-07 07:44:53 +000076 // Everything defaults to one.
77 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
78 NumElementsForVT[i] = 1;
Misha Brukmanf976c852005-04-21 22:55:34 +000079
Chris Lattner310968c2005-01-07 07:44:53 +000080 // 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];
Chris Lattner310968c2005-01-07 07:44:53 +000090
Chris Lattnerbb97d812005-01-16 01:10:58 +000091 // 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)
Chris Lattnercba82f92005-01-16 07:28:11 +000095 SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
Chris Lattnerbb97d812005-01-16 01:10:58 +000096 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.
Chris Lattnercba82f92005-01-16 07:28:11 +0000100 SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
101 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000102 else
103 TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
Misha Brukmanf976c852005-04-21 22:55:34 +0000104
Chris Lattnerbb97d812005-01-16 01:10:58 +0000105 // If the target does not have native support for F32, promote it to F64.
106 if (!hasNativeSupportFor(MVT::f32))
Chris Lattnercba82f92005-01-16 07:28:11 +0000107 SetValueTypeAction(MVT::f32, Promote, *this,
108 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000109 else
110 TransformToType[MVT::f32] = MVT::f32;
111
112 assert(hasNativeSupportFor(MVT::f64) && "Target does not support FP?");
113 TransformToType[MVT::f64] = MVT::f64;
Chris Lattnerbb97d812005-01-16 01:10:58 +0000114}
Chris Lattnercba82f92005-01-16 07:28:11 +0000115