blob: 6b6ac57770d7a47e4a4435fc9cb86bb76a1765d3 [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"
Chris Lattner4ccb0702006-01-26 20:37:03 +000016#include "llvm/Target/MRegisterInfo.h"
Chris Lattner310968c2005-01-07 07:44:53 +000017#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner4ccb0702006-01-26 20:37:03 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattner310968c2005-01-07 07:44:53 +000019using namespace llvm;
20
21TargetLowering::TargetLowering(TargetMachine &tm)
Chris Lattnerbb97d812005-01-16 01:10:58 +000022 : TM(tm), TD(TM.getTargetData()), ValueTypeActions(0) {
Chris Lattner310968c2005-01-07 07:44:53 +000023 assert(ISD::BUILTIN_OP_END <= 128 &&
24 "Fixed size array in TargetLowering is not large enough!");
Chris Lattnercba82f92005-01-16 07:28:11 +000025 // All operations default to being supported.
26 memset(OpActions, 0, sizeof(OpActions));
Chris Lattner310968c2005-01-07 07:44:53 +000027
28 IsLittleEndian = TD.isLittleEndian();
Chris Lattner714b69d2005-01-16 23:59:48 +000029 ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD.getIntPtrType());
Chris Lattnerd6e49672005-01-19 03:36:14 +000030 ShiftAmtHandling = Undefined;
Chris Lattner310968c2005-01-07 07:44:53 +000031 memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
Reid Spencer0f9beca2005-08-27 19:09:02 +000032 maxStoresPerMemSet = maxStoresPerMemCpy = maxStoresPerMemMove = 8;
33 allowUnalignedMemoryAccesses = false;
Chris Lattner8e6be8b2005-09-27 22:13:56 +000034 UseUnderscoreSetJmpLongJmp = false;
Nate Begeman405e3ec2005-10-21 00:02:42 +000035 IntDivIsCheap = false;
36 Pow2DivIsCheap = false;
Chris Lattneree4a7652006-01-25 18:57:15 +000037 StackPointerRegisterToSaveRestore = 0;
Evan Cheng0577a222006-01-25 18:52:42 +000038 SchedPreferenceInfo = SchedulingForLatency;
Chris Lattner310968c2005-01-07 07:44:53 +000039}
40
Chris Lattnercba82f92005-01-16 07:28:11 +000041TargetLowering::~TargetLowering() {}
42
Chris Lattnerbb97d812005-01-16 01:10:58 +000043/// setValueTypeAction - Set the action for a particular value type. This
44/// assumes an action has not already been set for this value type.
Chris Lattnercba82f92005-01-16 07:28:11 +000045static void SetValueTypeAction(MVT::ValueType VT,
46 TargetLowering::LegalizeAction Action,
Chris Lattnerbb97d812005-01-16 01:10:58 +000047 TargetLowering &TLI,
48 MVT::ValueType *TransformToType,
Nate Begeman6a648612005-11-29 05:45:29 +000049 unsigned long long &ValueTypeActions) {
50 ValueTypeActions |= (unsigned long long)Action << (VT*2);
Chris Lattnercba82f92005-01-16 07:28:11 +000051 if (Action == TargetLowering::Promote) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000052 MVT::ValueType PromoteTo;
53 if (VT == MVT::f32)
54 PromoteTo = MVT::f64;
55 else {
56 unsigned LargerReg = VT+1;
Chris Lattner9ed62c12005-08-24 16:34:12 +000057 while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) {
Chris Lattnerbb97d812005-01-16 01:10:58 +000058 ++LargerReg;
59 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
60 "Nothing to promote to??");
61 }
62 PromoteTo = (MVT::ValueType)LargerReg;
63 }
64
65 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
66 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
67 "Can only promote from int->int or fp->fp!");
68 assert(VT < PromoteTo && "Must promote to a larger type!");
69 TransformToType[VT] = PromoteTo;
Chris Lattnercba82f92005-01-16 07:28:11 +000070 } else if (Action == TargetLowering::Expand) {
Nate Begeman4ef3b812005-11-22 01:29:36 +000071 assert((VT == MVT::Vector || MVT::isInteger(VT)) && VT > MVT::i8 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +000072 "Cannot expand this type: target must support SOME integer reg!");
73 // Expand to the next smaller integer type!
74 TransformToType[VT] = (MVT::ValueType)(VT-1);
75 }
76}
77
78
Chris Lattner310968c2005-01-07 07:44:53 +000079/// computeRegisterProperties - Once all of the register classes are added,
80/// this allows us to compute derived properties we expose.
81void TargetLowering::computeRegisterProperties() {
Nate Begeman6a648612005-11-29 05:45:29 +000082 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerbb97d812005-01-16 01:10:58 +000083 "Too many value types for ValueTypeActions to hold!");
84
Chris Lattner310968c2005-01-07 07:44:53 +000085 // Everything defaults to one.
86 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
87 NumElementsForVT[i] = 1;
Misha Brukmanf976c852005-04-21 22:55:34 +000088
Chris Lattner310968c2005-01-07 07:44:53 +000089 // Find the largest integer register class.
90 unsigned LargestIntReg = MVT::i128;
91 for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
92 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
93
94 // Every integer value type larger than this largest register takes twice as
95 // many registers to represent as the previous ValueType.
96 unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
97 for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
98 NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
Chris Lattner310968c2005-01-07 07:44:53 +000099
Chris Lattnerbb97d812005-01-16 01:10:58 +0000100 // Inspect all of the ValueType's possible, deciding how to process them.
101 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
102 // If we are expanding this type, expand it!
103 if (getNumElements((MVT::ValueType)IntReg) != 1)
Chris Lattnercba82f92005-01-16 07:28:11 +0000104 SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
Chris Lattnerbb97d812005-01-16 01:10:58 +0000105 ValueTypeActions);
Chris Lattner9ed62c12005-08-24 16:34:12 +0000106 else if (!isTypeLegal((MVT::ValueType)IntReg))
Chris Lattnerbb97d812005-01-16 01:10:58 +0000107 // Otherwise, if we don't have native support, we must promote to a
108 // larger type.
Chris Lattnercba82f92005-01-16 07:28:11 +0000109 SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
110 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000111 else
112 TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
Misha Brukmanf976c852005-04-21 22:55:34 +0000113
Chris Lattnerbb97d812005-01-16 01:10:58 +0000114 // If the target does not have native support for F32, promote it to F64.
Chris Lattner9ed62c12005-08-24 16:34:12 +0000115 if (!isTypeLegal(MVT::f32))
Chris Lattnercba82f92005-01-16 07:28:11 +0000116 SetValueTypeAction(MVT::f32, Promote, *this,
117 TransformToType, ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000118 else
119 TransformToType[MVT::f32] = MVT::f32;
Nate Begeman4ef3b812005-11-22 01:29:36 +0000120
121 // Set MVT::Vector to always be Expanded
122 SetValueTypeAction(MVT::Vector, Expand, *this, TransformToType,
123 ValueTypeActions);
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000124
Chris Lattner9ed62c12005-08-24 16:34:12 +0000125 assert(isTypeLegal(MVT::f64) && "Target does not support FP?");
Chris Lattnercfdfe4c2005-01-16 01:20:18 +0000126 TransformToType[MVT::f64] = MVT::f64;
Chris Lattnerbb97d812005-01-16 01:10:58 +0000127}
Chris Lattnercba82f92005-01-16 07:28:11 +0000128
Evan Cheng72261582005-12-20 06:22:03 +0000129const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
130 return NULL;
131}
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000132
Evan Chengff9be112005-12-21 23:14:54 +0000133bool TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
134 uint64_t Mask) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000135 return false;
136}
Chris Lattner4ccb0702006-01-26 20:37:03 +0000137
138std::vector<unsigned> TargetLowering::
139getRegForInlineAsmConstraint(const std::string &Constraint) const {
140 // Scan to see if this constraint is a register name.
141 const MRegisterInfo *RI = TM.getRegisterInfo();
142 for (unsigned i = 1, e = RI->getNumRegs(); i != e; ++i) {
143 if (const char *Name = RI->get(i).Name)
144 if (StringsEqualNoCase(Constraint, Name))
145 return std::vector<unsigned>(1, i);
146 }
147
148 // Not a physreg, must not be a register reference or something.
149 return std::vector<unsigned>();
150}
151