blob: 7280d707fba698cbea1593ed603d470a3967492a [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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000017#ifndef LLVM_UTILS_TABLEGEN_CODEGENTARGET_H
18#define LLVM_UTILS_TABLEGEN_CODEGENTARGET_H
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000019
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000020#include "CodeGenHwModes.h"
Chris Lattnerc860eca2004-08-01 05:04:00 +000021#include "CodeGenInstruction.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000022#include "CodeGenRegisters.h"
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000023#include "InfoByHwMode.h"
Matt Arsenault303327d2017-12-20 19:36:28 +000024#include "SDNodeProperties.h"
Chris Lattner1802b172010-03-19 01:07:44 +000025#include "llvm/Support/raw_ostream.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000026#include "llvm/TableGen/Record.h"
Dan Gohmane14b8d92009-04-13 15:24:11 +000027#include <algorithm>
Brian Gaeke960707c2003-11-11 22:41:34 +000028
29namespace llvm {
30
Chris Lattner7dfc2d22004-10-27 16:14:51 +000031struct CodeGenRegister;
Andrew Trick87255e32012-07-07 04:00:00 +000032class CodeGenSchedModels;
Chris Lattnerc92f6882006-03-27 22:48:18 +000033class CodeGenTarget;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000034
Owen Anderson9f944592009-08-11 20:47:22 +000035/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands13237ac2008-06-06 12:08:01 +000036/// record corresponds to.
Owen Anderson9f944592009-08-11 20:47:22 +000037MVT::SimpleValueType getValueType(Record *Rec);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000038
Matt Arsenault4fb996e2016-05-25 18:07:40 +000039StringRef getName(MVT::SimpleValueType T);
40StringRef getEnumName(MVT::SimpleValueType T);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000041
Chris Lattner8cab0212008-01-05 22:25:12 +000042/// getQualifiedName - Return the name of the specified record, with a
43/// namespace qualifier if the record contains one.
44std::string getQualifiedName(const Record *R);
Jim Grosbachf910bf22011-03-11 01:27:24 +000045
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000046/// CodeGenTarget - This class corresponds to the Target class in the .td files.
47///
48class CodeGenTarget {
Chris Lattner77d369c2010-12-13 00:23:57 +000049 RecordKeeper &Records;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000050 Record *TargetRec;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000051
Craig Topper48a8e642014-12-10 06:18:57 +000052 mutable DenseMap<const Record*,
53 std::unique_ptr<CodeGenInstruction>> Instructions;
Craig Topper313a96f2014-11-28 20:30:37 +000054 mutable std::unique_ptr<CodeGenRegBank> RegBank;
Owen Andersona84be6c2011-06-27 21:06:21 +000055 mutable std::vector<Record*> RegAltNameIndices;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000056 mutable SmallVector<ValueTypeByHwMode, 8> LegalValueTypes;
57 CodeGenHwModes CGH;
Owen Andersona84be6c2011-06-27 21:06:21 +000058 void ReadRegAltNameIndices() const;
Chris Lattner2a86fab2004-08-21 04:05:00 +000059 void ReadInstructions() const;
Chris Lattnercee994b2005-09-08 21:43:21 +000060 void ReadLegalValueTypes() const;
Jim Grosbachf910bf22011-03-11 01:27:24 +000061
Craig Topper313a96f2014-11-28 20:30:37 +000062 mutable std::unique_ptr<CodeGenSchedModels> SchedModels;
Andrew Trick87255e32012-07-07 04:00:00 +000063
Chris Lattner4763dbe2010-03-19 01:00:55 +000064 mutable std::vector<const CodeGenInstruction*> InstrsByEnum;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000065public:
Chris Lattner77d369c2010-12-13 00:23:57 +000066 CodeGenTarget(RecordKeeper &Records);
Andrew Trick87255e32012-07-07 04:00:00 +000067 ~CodeGenTarget();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000068
69 Record *getTargetRecord() const { return TargetRec; }
Matthias Braun4a86d452016-12-04 05:48:16 +000070 const StringRef getName() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000071
Dan Gohman3e2225d2008-08-20 21:45:57 +000072 /// getInstNamespace - Return the target-specific instruction namespace.
73 ///
Craig Topper86a9aee2017-07-07 06:22:35 +000074 StringRef getInstNamespace() const;
Dan Gohman3e2225d2008-08-20 21:45:57 +000075
Chris Lattner6ffa5012004-08-14 22:50:53 +000076 /// getInstructionSet - Return the InstructionSet object.
Chris Lattnerc860eca2004-08-01 05:04:00 +000077 ///
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000078 Record *getInstructionSet() const;
79
Daniel Dunbar00331992009-07-29 00:02:19 +000080 /// getAsmParser - Return the AssemblyParser definition for this target.
81 ///
82 Record *getAsmParser() const;
83
Devang Patel85d684a2012-01-09 19:13:28 +000084 /// getAsmParserVariant - Return the AssmblyParserVariant definition for
85 /// this target.
86 ///
87 Record *getAsmParserVariant(unsigned i) const;
88
Andrew Trick87255e32012-07-07 04:00:00 +000089 /// getAsmParserVariantCount - Return the AssmblyParserVariant definition
Devang Patel85d684a2012-01-09 19:13:28 +000090 /// available for this target.
91 ///
92 unsigned getAsmParserVariantCount() const;
93
Chris Lattner6ffa5012004-08-14 22:50:53 +000094 /// getAsmWriter - Return the AssemblyWriter definition for this target.
95 ///
96 Record *getAsmWriter() const;
97
Jakob Stoklund Olesen76a5a712011-06-10 18:40:00 +000098 /// getRegBank - Return the register bank description.
99 CodeGenRegBank &getRegBank() const;
100
Chris Lattner77d3ead2010-11-02 18:10:06 +0000101 /// getRegisterByName - If there is a register with the specific AsmName,
102 /// return it.
103 const CodeGenRegister *getRegisterByName(StringRef Name) const;
Chris Lattnerc860eca2004-08-01 05:04:00 +0000104
Owen Andersona84be6c2011-06-27 21:06:21 +0000105 const std::vector<Record*> &getRegAltNameIndices() const {
106 if (RegAltNameIndices.empty()) ReadRegAltNameIndices();
107 return RegAltNameIndices;
108 }
109
Nate Begemancdf2c672005-12-01 00:06:14 +0000110 const CodeGenRegisterClass &getRegisterClass(Record *R) const {
Jakob Stoklund Olesen22ea4242011-06-15 00:20:40 +0000111 return *getRegBank().getRegClass(R);
Chris Lattner3717b4c2005-12-05 02:35:08 +0000112 }
Evan Chengd985d662006-05-16 07:05:30 +0000113
Duncan Sands13237ac2008-06-06 12:08:01 +0000114 /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the
Evan Chengd985d662006-05-16 07:05:30 +0000115 /// specified physical register.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000116 std::vector<ValueTypeByHwMode> getRegisterVTs(Record *R) const;
Jim Grosbachf910bf22011-03-11 01:27:24 +0000117
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000118 ArrayRef<ValueTypeByHwMode> getLegalValueTypes() const {
119 if (LegalValueTypes.empty())
120 ReadLegalValueTypes();
Chris Lattnercee994b2005-09-08 21:43:21 +0000121 return LegalValueTypes;
122 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000123
Andrew Trick87255e32012-07-07 04:00:00 +0000124 CodeGenSchedModels &getSchedModels() const;
125
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000126 const CodeGenHwModes &getHwModes() const { return CGH; }
127
Chris Lattner70eb8972010-03-19 00:18:23 +0000128private:
Craig Topper48a8e642014-12-10 06:18:57 +0000129 DenseMap<const Record*, std::unique_ptr<CodeGenInstruction>> &
130 getInstructions() const {
Chris Lattnerc860eca2004-08-01 05:04:00 +0000131 if (Instructions.empty()) ReadInstructions();
132 return Instructions;
133 }
Chris Lattner9aec14b2010-03-19 00:07:20 +0000134public:
Jim Grosbachf910bf22011-03-11 01:27:24 +0000135
Chris Lattner1802b172010-03-19 01:07:44 +0000136 CodeGenInstruction &getInstruction(const Record *InstRec) const {
137 if (Instructions.empty()) ReadInstructions();
Craig Topper48a8e642014-12-10 06:18:57 +0000138 auto I = Instructions.find(InstRec);
Chris Lattner1802b172010-03-19 01:07:44 +0000139 assert(I != Instructions.end() && "Not an instruction");
140 return *I->second;
141 }
Chris Lattnerfcffc982005-09-14 18:02:53 +0000142
Chris Lattner945e8652005-01-22 18:58:51 +0000143 /// getInstructionsByEnumValue - Return all of the instructions defined by the
144 /// target, ordered by their enum value.
Craig Topperf9265322016-01-17 20:38:14 +0000145 ArrayRef<const CodeGenInstruction *>
Chris Lattner4763dbe2010-03-19 01:00:55 +0000146 getInstructionsByEnumValue() const {
Chris Lattner918be522010-03-19 00:34:35 +0000147 if (InstrsByEnum.empty()) ComputeInstrsByEnum();
148 return InstrsByEnum;
149 }
Chris Lattner945e8652005-01-22 18:58:51 +0000150
Craig Topperf9265322016-01-17 20:38:14 +0000151 typedef ArrayRef<const CodeGenInstruction *>::const_iterator inst_iterator;
Chris Lattner4763dbe2010-03-19 01:00:55 +0000152 inst_iterator inst_begin() const{return getInstructionsByEnumValue().begin();}
153 inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000154
155
Misha Brukman243ded52004-10-14 05:50:43 +0000156 /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]?
157 ///
158 bool isLittleEndianEncoding() const;
Jim Grosbachf910bf22011-03-11 01:27:24 +0000159
Hal Finkel81e6fcc2013-12-17 22:37:50 +0000160 /// reverseBitsForLittleEndianEncoding - For little-endian instruction bit
161 /// encodings, reverse the bit order of all instructions.
162 void reverseBitsForLittleEndianEncoding();
163
Jakob Stoklund Olesen9dc03bb2012-08-23 19:34:41 +0000164 /// guessInstructionProperties - should we just guess unset instruction
165 /// properties?
166 bool guessInstructionProperties() const;
167
Chris Lattner918be522010-03-19 00:34:35 +0000168private:
Chris Lattner4763dbe2010-03-19 01:00:55 +0000169 void ComputeInstrsByEnum() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000170};
171
Evan Cheng9b9567b2005-12-08 02:00:36 +0000172/// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
173/// tablegen class in TargetSelectionDAG.td
174class ComplexPattern {
Owen Anderson9f944592009-08-11 20:47:22 +0000175 MVT::SimpleValueType Ty;
Evan Cheng9b9567b2005-12-08 02:00:36 +0000176 unsigned NumOperands;
177 std::string SelectFunc;
Evan Chengc9a62002005-12-08 02:14:08 +0000178 std::vector<Record*> RootNodes;
Christopher Lamb0592cf72008-01-31 07:27:46 +0000179 unsigned Properties; // Node properties
Peter Collingbourne32ab3a82016-11-09 23:53:43 +0000180 unsigned Complexity;
Evan Cheng9b9567b2005-12-08 02:00:36 +0000181public:
Evan Cheng9b9567b2005-12-08 02:00:36 +0000182 ComplexPattern(Record *R);
183
Owen Anderson9f944592009-08-11 20:47:22 +0000184 MVT::SimpleValueType getValueType() const { return Ty; }
Evan Cheng9b9567b2005-12-08 02:00:36 +0000185 unsigned getNumOperands() const { return NumOperands; }
186 const std::string &getSelectFunc() const { return SelectFunc; }
Evan Chengc9a62002005-12-08 02:14:08 +0000187 const std::vector<Record*> &getRootNodes() const {
188 return RootNodes;
Evan Cheng9b9567b2005-12-08 02:00:36 +0000189 }
Evan Cheng2022c792006-10-11 21:02:01 +0000190 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Peter Collingbourne32ab3a82016-11-09 23:53:43 +0000191 unsigned getComplexity() const { return Complexity; }
Evan Cheng9b9567b2005-12-08 02:00:36 +0000192};
193
Brian Gaeke960707c2003-11-11 22:41:34 +0000194} // End llvm namespace
195
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000196#endif