blob: 3c2a09cfbd7092be7a238ab458dcae927f93b2bf [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*));
Reid Spencer0f9beca2005-08-27 19:09:02 +000030 maxStoresPerMemSet = maxStoresPerMemCpy = maxStoresPerMemMove = 8;
31 allowUnalignedMemoryAccesses = false;
Chris Lattner310968c2005-01-07 07:44:53 +000032}
33
Chris Lattnercba82f92005-01-16 07:28:11 +000034TargetLowering::~TargetLowering() {}
35
Chris Lattnerbb97d812005-01-16 01:10:58 +000036/// setValueTypeAction - Set the action for a particular value type. This
37/// assumes an action has not already been set for this value type.
Chris Lattnercba82f92005-01-16 07:28:11 +000038static void SetValueTypeAction(MVT::ValueType VT,
39 TargetLowering::LegalizeAction Action,
Chris Lattnerbb97d812005-01-16 01:10:58 +000040 TargetLowering &TLI,
41 MVT::ValueType *TransformToType,
42 unsigned &ValueTypeActions) {
43 ValueTypeActions |= Action << (VT*2);
Chris Lattnercba82f92005-01-16 07:28:11 +000044 if (Action == TargetLowering::Promote) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000045 MVT::ValueType PromoteTo;
46 if (VT == MVT::f32)
47 PromoteTo = MVT::f64;
48 else {
49 unsigned LargerReg = VT+1;
Chris Lattner9ed62c12005-08-24 16:34:12 +000050 while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000051 ++LargerReg;
52 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
53 "Nothing to promote to??");
54 }
55 PromoteTo = (MVT::ValueType)LargerReg;
56 }
57
58 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
59 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
60 "Can only promote from int->int or fp->fp!");
61 assert(VT < PromoteTo && "Must promote to a larger type!");
62 TransformToType[VT] = PromoteTo;
Chris Lattnercba82f92005-01-16 07:28:11 +000063 } else if (Action == TargetLowering::Expand) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000064 assert(MVT::isInteger(VT) && VT > MVT::i8 &&
65 "Cannot expand this type: target must support SOME integer reg!");
66 // Expand to the next smaller integer type!
67 TransformToType[VT] = (MVT::ValueType)(VT-1);
68 }
69}
70
71
Chris Lattner310968c2005-01-07 07:44:53 +000072/// computeRegisterProperties - Once all of the register classes are added,
73/// this allows us to compute derived properties we expose.
74void TargetLowering::computeRegisterProperties() {
Chris Lattnerbb97d812005-01-16 01:10:58 +000075 assert(MVT::LAST_VALUETYPE <= 16 &&
76 "Too many value types for ValueTypeActions to hold!");
77
Chris Lattner310968c2005-01-07 07:44:53 +000078 // Everything defaults to one.
79 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
80 NumElementsForVT[i] = 1;
Misha Brukmanf976c852005-04-21 22:55:34 +000081
Chris Lattner310968c2005-01-07 07:44:53 +000082 // Find the largest integer register class.
83 unsigned LargestIntReg = MVT::i128;
84 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
85 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
86
87 // Every integer value type larger than this largest register takes twice as
88 // many registers to represent as the previous ValueType.
89 unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
90 for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
91 NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
Chris Lattner310968c2005-01-07 07:44:53 +000092
Chris Lattnerbb97d812005-01-16 01:10:58 +000093 // Inspect all of the ValueType's possible, deciding how to process them.
94 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
95 // If we are expanding this type, expand it!
96 if (getNumElements((MVT::ValueType)IntReg) != 1)
Chris Lattnercba82f92005-01-16 07:28:11 +000097 SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
Chris Lattnerbb97d812005-01-16 01:10:58 +000098 ValueTypeActions);
Chris Lattner9ed62c12005-08-24 16:34:12 +000099 else if (!isTypeLegal((MVT::ValueType)IntReg))
Chris Lattnerbb97d812005-01-16 01:10:58 +0000100 // Otherwise, if we don't have native support, we must promote to a
101 // larger type.
Chris Lattnercba82f92005-01-16 07:28:11 +0000102 SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
103 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000104 else
105 TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
Misha Brukmanf976c852005-04-21 22:55:34 +0000106
Chris Lattnerbb97d812005-01-16 01:10:58 +0000107 // If the target does not have native support for F32, promote it to F64.
Chris Lattner9ed62c12005-08-24 16:34:12 +0000108 if (!isTypeLegal(MVT::f32))
Chris Lattnercba82f92005-01-16 07:28:11 +0000109 SetValueTypeAction(MVT::f32, Promote, *this,
110 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000111 else
112 TransformToType[MVT::f32] = MVT::f32;
113
Chris Lattner9ed62c12005-08-24 16:34:12 +0000114 assert(isTypeLegal(MVT::f64) && "Target does not support FP?");
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000115 TransformToType[MVT::f64] = MVT::f64;
Chris Lattnerbb97d812005-01-16 01:10:58 +0000116}
Chris Lattnercba82f92005-01-16 07:28:11 +0000117