blob: 54143103dc1849278280143083be7390a83704d6 [file] [log] [blame]
Chris Lattnerfce96032004-08-01 04:04:35 +00001//===- CodeGenTarget.h - Target Class Wrapper -------------------*- C++ -*-===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswelld3032032003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswelld3032032003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00009//
Chris Lattnerfce96032004-08-01 04:04:35 +000010// This file defines wrappers for the Target class and related global
11// functionality. This makes it easier to access the data and provides a single
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000012// place that needs to check it for validity. All of these classes abort
13// on error conditions.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattnerfce96032004-08-01 04:04:35 +000017#ifndef CODEGEN_TARGET_H
18#define CODEGEN_TARGET_H
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000019
Chris Lattnerc860eca2004-08-01 05:04:00 +000020#include "CodeGenInstruction.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000021#include "CodeGenRegisters.h"
Chris Lattner1802b172010-03-19 01:07:44 +000022#include "llvm/Support/raw_ostream.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000023#include "llvm/TableGen/Record.h"
Dan Gohmane14b8d92009-04-13 15:24:11 +000024#include <algorithm>
Brian Gaeke960707c2003-11-11 22:41:34 +000025
26namespace llvm {
27
Chris Lattner7dfc2d22004-10-27 16:14:51 +000028struct CodeGenRegister;
Andrew Trick87255e32012-07-07 04:00:00 +000029class CodeGenSchedModels;
Chris Lattnerc92f6882006-03-27 22:48:18 +000030class CodeGenTarget;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000031
Evan Cheng2022c792006-10-11 21:02:01 +000032// SelectionDAG node properties.
Mon P Wang6a490372008-06-25 08:15:39 +000033// SDNPMemOperand: indicates that a node touches memory and therefore must
34// have an associated memory operand that describes the access.
Chris Lattnera348f552008-01-06 06:44:58 +000035enum SDNP {
Jim Grosbachf910bf22011-03-11 01:27:24 +000036 SDNPCommutative,
37 SDNPAssociative,
Chris Lattnera348f552008-01-06 06:44:58 +000038 SDNPHasChain,
Chris Lattner2a0a3b42010-12-23 18:28:41 +000039 SDNPOutGlue,
40 SDNPInGlue,
41 SDNPOptInGlue,
Chris Lattner1ca20682008-01-10 04:38:57 +000042 SDNPMayLoad,
Chris Lattner42c63ef2008-01-10 05:39:30 +000043 SDNPMayStore,
Mon P Wang6a490372008-06-25 08:15:39 +000044 SDNPSideEffect,
Chris Lattner83aeaab2010-03-19 05:07:09 +000045 SDNPMemOperand,
Chris Lattner0e023ea2010-09-21 20:31:19 +000046 SDNPVariadic,
47 SDNPWantRoot,
48 SDNPWantParent
Chris Lattnera348f552008-01-06 06:44:58 +000049};
Evan Cheng2022c792006-10-11 21:02:01 +000050
Owen Anderson9f944592009-08-11 20:47:22 +000051/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands13237ac2008-06-06 12:08:01 +000052/// record corresponds to.
Owen Anderson9f944592009-08-11 20:47:22 +000053MVT::SimpleValueType getValueType(Record *Rec);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000054
Owen Anderson9f944592009-08-11 20:47:22 +000055std::string getName(MVT::SimpleValueType T);
56std::string getEnumName(MVT::SimpleValueType T);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000057
Chris Lattner8cab0212008-01-05 22:25:12 +000058/// getQualifiedName - Return the name of the specified record, with a
59/// namespace qualifier if the record contains one.
60std::string getQualifiedName(const Record *R);
Jim Grosbachf910bf22011-03-11 01:27:24 +000061
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000062/// CodeGenTarget - This class corresponds to the Target class in the .td files.
63///
64class CodeGenTarget {
Chris Lattner77d369c2010-12-13 00:23:57 +000065 RecordKeeper &Records;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000066 Record *TargetRec;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000067
Chris Lattner1802b172010-03-19 01:07:44 +000068 mutable DenseMap<const Record*, CodeGenInstruction*> Instructions;
Jakob Stoklund Olesen76a5a712011-06-10 18:40:00 +000069 mutable CodeGenRegBank *RegBank;
Owen Andersona84be6c2011-06-27 21:06:21 +000070 mutable std::vector<Record*> RegAltNameIndices;
Jakob Stoklund Olesen13d4a072013-03-17 17:26:09 +000071 mutable SmallVector<MVT::SimpleValueType, 8> LegalValueTypes;
Owen Andersona84be6c2011-06-27 21:06:21 +000072 void ReadRegAltNameIndices() const;
Chris Lattner2a86fab2004-08-21 04:05:00 +000073 void ReadInstructions() const;
Chris Lattnercee994b2005-09-08 21:43:21 +000074 void ReadLegalValueTypes() const;
Jim Grosbachf910bf22011-03-11 01:27:24 +000075
Andrew Trick87255e32012-07-07 04:00:00 +000076 mutable CodeGenSchedModels *SchedModels;
77
Chris Lattner4763dbe2010-03-19 01:00:55 +000078 mutable std::vector<const CodeGenInstruction*> InstrsByEnum;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000079public:
Chris Lattner77d369c2010-12-13 00:23:57 +000080 CodeGenTarget(RecordKeeper &Records);
Andrew Trick87255e32012-07-07 04:00:00 +000081 ~CodeGenTarget();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000082
83 Record *getTargetRecord() const { return TargetRec; }
84 const std::string &getName() const;
85
Dan Gohman3e2225d2008-08-20 21:45:57 +000086 /// getInstNamespace - Return the target-specific instruction namespace.
87 ///
88 std::string getInstNamespace() const;
89
Chris Lattner6ffa5012004-08-14 22:50:53 +000090 /// getInstructionSet - Return the InstructionSet object.
Chris Lattnerc860eca2004-08-01 05:04:00 +000091 ///
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000092 Record *getInstructionSet() const;
93
Daniel Dunbar00331992009-07-29 00:02:19 +000094 /// getAsmParser - Return the AssemblyParser definition for this target.
95 ///
96 Record *getAsmParser() const;
97
Devang Patel85d684a2012-01-09 19:13:28 +000098 /// getAsmParserVariant - Return the AssmblyParserVariant definition for
99 /// this target.
100 ///
101 Record *getAsmParserVariant(unsigned i) const;
102
Andrew Trick87255e32012-07-07 04:00:00 +0000103 /// getAsmParserVariantCount - Return the AssmblyParserVariant definition
Devang Patel85d684a2012-01-09 19:13:28 +0000104 /// available for this target.
105 ///
106 unsigned getAsmParserVariantCount() const;
107
Chris Lattner6ffa5012004-08-14 22:50:53 +0000108 /// getAsmWriter - Return the AssemblyWriter definition for this target.
109 ///
110 Record *getAsmWriter() const;
111
Jakob Stoklund Olesen76a5a712011-06-10 18:40:00 +0000112 /// getRegBank - Return the register bank description.
113 CodeGenRegBank &getRegBank() const;
114
Chris Lattner77d3ead2010-11-02 18:10:06 +0000115 /// getRegisterByName - If there is a register with the specific AsmName,
116 /// return it.
117 const CodeGenRegister *getRegisterByName(StringRef Name) const;
Chris Lattnerc860eca2004-08-01 05:04:00 +0000118
Owen Andersona84be6c2011-06-27 21:06:21 +0000119 const std::vector<Record*> &getRegAltNameIndices() const {
120 if (RegAltNameIndices.empty()) ReadRegAltNameIndices();
121 return RegAltNameIndices;
122 }
123
Nate Begemancdf2c672005-12-01 00:06:14 +0000124 const CodeGenRegisterClass &getRegisterClass(Record *R) const {
Jakob Stoklund Olesen22ea4242011-06-15 00:20:40 +0000125 return *getRegBank().getRegClass(R);
Chris Lattner3717b4c2005-12-05 02:35:08 +0000126 }
Evan Chengd985d662006-05-16 07:05:30 +0000127
Duncan Sands13237ac2008-06-06 12:08:01 +0000128 /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the
Evan Chengd985d662006-05-16 07:05:30 +0000129 /// specified physical register.
Chris Lattnercabe0372010-03-15 06:00:16 +0000130 std::vector<MVT::SimpleValueType> getRegisterVTs(Record *R) const;
Jim Grosbachf910bf22011-03-11 01:27:24 +0000131
Jakob Stoklund Olesen13d4a072013-03-17 17:26:09 +0000132 ArrayRef<MVT::SimpleValueType> getLegalValueTypes() const {
Chris Lattnercee994b2005-09-08 21:43:21 +0000133 if (LegalValueTypes.empty()) ReadLegalValueTypes();
134 return LegalValueTypes;
135 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000136
Chris Lattnercee994b2005-09-08 21:43:21 +0000137 /// isLegalValueType - Return true if the specified value type is natively
138 /// supported by the target (i.e. there are registers that directly hold it).
Owen Anderson9f944592009-08-11 20:47:22 +0000139 bool isLegalValueType(MVT::SimpleValueType VT) const {
Jakob Stoklund Olesen13d4a072013-03-17 17:26:09 +0000140 ArrayRef<MVT::SimpleValueType> LegalVTs = getLegalValueTypes();
Chris Lattnercee994b2005-09-08 21:43:21 +0000141 for (unsigned i = 0, e = LegalVTs.size(); i != e; ++i)
142 if (LegalVTs[i] == VT) return true;
Jim Grosbachf910bf22011-03-11 01:27:24 +0000143 return false;
Chris Lattnercee994b2005-09-08 21:43:21 +0000144 }
Chris Lattner2a86fab2004-08-21 04:05:00 +0000145
Andrew Trick87255e32012-07-07 04:00:00 +0000146 CodeGenSchedModels &getSchedModels() const;
147
Chris Lattner70eb8972010-03-19 00:18:23 +0000148private:
Chris Lattner1802b172010-03-19 01:07:44 +0000149 DenseMap<const Record*, CodeGenInstruction*> &getInstructions() const {
Chris Lattnerc860eca2004-08-01 05:04:00 +0000150 if (Instructions.empty()) ReadInstructions();
151 return Instructions;
152 }
Chris Lattner9aec14b2010-03-19 00:07:20 +0000153public:
Jim Grosbachf910bf22011-03-11 01:27:24 +0000154
Chris Lattner1802b172010-03-19 01:07:44 +0000155 CodeGenInstruction &getInstruction(const Record *InstRec) const {
156 if (Instructions.empty()) ReadInstructions();
157 DenseMap<const Record*, CodeGenInstruction*>::iterator I =
158 Instructions.find(InstRec);
159 assert(I != Instructions.end() && "Not an instruction");
160 return *I->second;
161 }
Chris Lattnerfcffc982005-09-14 18:02:53 +0000162
Chris Lattner945e8652005-01-22 18:58:51 +0000163 /// getInstructionsByEnumValue - Return all of the instructions defined by the
164 /// target, ordered by their enum value.
Chris Lattner4763dbe2010-03-19 01:00:55 +0000165 const std::vector<const CodeGenInstruction*> &
166 getInstructionsByEnumValue() const {
Chris Lattner918be522010-03-19 00:34:35 +0000167 if (InstrsByEnum.empty()) ComputeInstrsByEnum();
168 return InstrsByEnum;
169 }
Chris Lattner945e8652005-01-22 18:58:51 +0000170
Chris Lattner4763dbe2010-03-19 01:00:55 +0000171 typedef std::vector<const CodeGenInstruction*>::const_iterator inst_iterator;
172 inst_iterator inst_begin() const{return getInstructionsByEnumValue().begin();}
173 inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); }
Jim Grosbach56fd8882014-04-18 02:09:02 +0000174 iterator_range<inst_iterator> instructions() const {
175 return iterator_range<inst_iterator>(inst_begin(), inst_end());
176 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000177
178
Misha Brukman243ded52004-10-14 05:50:43 +0000179 /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]?
180 ///
181 bool isLittleEndianEncoding() const;
Jim Grosbachf910bf22011-03-11 01:27:24 +0000182
Hal Finkel81e6fcc2013-12-17 22:37:50 +0000183 /// reverseBitsForLittleEndianEncoding - For little-endian instruction bit
184 /// encodings, reverse the bit order of all instructions.
185 void reverseBitsForLittleEndianEncoding();
186
Jakob Stoklund Olesen9dc03bb2012-08-23 19:34:41 +0000187 /// guessInstructionProperties - should we just guess unset instruction
188 /// properties?
189 bool guessInstructionProperties() const;
190
Chris Lattner918be522010-03-19 00:34:35 +0000191private:
Chris Lattner4763dbe2010-03-19 01:00:55 +0000192 void ComputeInstrsByEnum() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000193};
194
Evan Cheng9b9567b2005-12-08 02:00:36 +0000195/// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
196/// tablegen class in TargetSelectionDAG.td
197class ComplexPattern {
Owen Anderson9f944592009-08-11 20:47:22 +0000198 MVT::SimpleValueType Ty;
Evan Cheng9b9567b2005-12-08 02:00:36 +0000199 unsigned NumOperands;
200 std::string SelectFunc;
Evan Chengc9a62002005-12-08 02:14:08 +0000201 std::vector<Record*> RootNodes;
Christopher Lamb0592cf72008-01-31 07:27:46 +0000202 unsigned Properties; // Node properties
Evan Cheng9b9567b2005-12-08 02:00:36 +0000203public:
Chris Lattner81f1b312009-11-06 06:33:01 +0000204 ComplexPattern() : NumOperands(0) {}
Evan Cheng9b9567b2005-12-08 02:00:36 +0000205 ComplexPattern(Record *R);
206
Owen Anderson9f944592009-08-11 20:47:22 +0000207 MVT::SimpleValueType getValueType() const { return Ty; }
Evan Cheng9b9567b2005-12-08 02:00:36 +0000208 unsigned getNumOperands() const { return NumOperands; }
209 const std::string &getSelectFunc() const { return SelectFunc; }
Evan Chengc9a62002005-12-08 02:14:08 +0000210 const std::vector<Record*> &getRootNodes() const {
211 return RootNodes;
Evan Cheng9b9567b2005-12-08 02:00:36 +0000212 }
Evan Cheng2022c792006-10-11 21:02:01 +0000213 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Evan Cheng9b9567b2005-12-08 02:00:36 +0000214};
215
Brian Gaeke960707c2003-11-11 22:41:34 +0000216} // End llvm namespace
217
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000218#endif