blob: c38f92bb979a135323bd5e161627592286c36ccb [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
Geoff Berryf8bf2ec2018-02-23 18:25:08 +000080 /// getAllowRegisterRenaming - Return the AllowRegisterRenaming flag value for
81 /// this target.
82 ///
83 bool getAllowRegisterRenaming() const;
84
Daniel Dunbar00331992009-07-29 00:02:19 +000085 /// getAsmParser - Return the AssemblyParser definition for this target.
86 ///
87 Record *getAsmParser() const;
88
Devang Patel85d684a2012-01-09 19:13:28 +000089 /// getAsmParserVariant - Return the AssmblyParserVariant definition for
90 /// this target.
91 ///
92 Record *getAsmParserVariant(unsigned i) const;
93
Andrew Trick87255e32012-07-07 04:00:00 +000094 /// getAsmParserVariantCount - Return the AssmblyParserVariant definition
Devang Patel85d684a2012-01-09 19:13:28 +000095 /// available for this target.
96 ///
97 unsigned getAsmParserVariantCount() const;
98
Chris Lattner6ffa5012004-08-14 22:50:53 +000099 /// getAsmWriter - Return the AssemblyWriter definition for this target.
100 ///
101 Record *getAsmWriter() const;
102
Jakob Stoklund Olesen76a5a712011-06-10 18:40:00 +0000103 /// getRegBank - Return the register bank description.
104 CodeGenRegBank &getRegBank() const;
105
Chris Lattner77d3ead2010-11-02 18:10:06 +0000106 /// getRegisterByName - If there is a register with the specific AsmName,
107 /// return it.
108 const CodeGenRegister *getRegisterByName(StringRef Name) const;
Chris Lattnerc860eca2004-08-01 05:04:00 +0000109
Owen Andersona84be6c2011-06-27 21:06:21 +0000110 const std::vector<Record*> &getRegAltNameIndices() const {
111 if (RegAltNameIndices.empty()) ReadRegAltNameIndices();
112 return RegAltNameIndices;
113 }
114
Nate Begemancdf2c672005-12-01 00:06:14 +0000115 const CodeGenRegisterClass &getRegisterClass(Record *R) const {
Jakob Stoklund Olesen22ea4242011-06-15 00:20:40 +0000116 return *getRegBank().getRegClass(R);
Chris Lattner3717b4c2005-12-05 02:35:08 +0000117 }
Evan Chengd985d662006-05-16 07:05:30 +0000118
Duncan Sands13237ac2008-06-06 12:08:01 +0000119 /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the
Evan Chengd985d662006-05-16 07:05:30 +0000120 /// specified physical register.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000121 std::vector<ValueTypeByHwMode> getRegisterVTs(Record *R) const;
Jim Grosbachf910bf22011-03-11 01:27:24 +0000122
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000123 ArrayRef<ValueTypeByHwMode> getLegalValueTypes() const {
124 if (LegalValueTypes.empty())
125 ReadLegalValueTypes();
Chris Lattnercee994b2005-09-08 21:43:21 +0000126 return LegalValueTypes;
127 }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000128
Andrew Trick87255e32012-07-07 04:00:00 +0000129 CodeGenSchedModels &getSchedModels() const;
130
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000131 const CodeGenHwModes &getHwModes() const { return CGH; }
132
Chris Lattner70eb8972010-03-19 00:18:23 +0000133private:
Craig Topper48a8e642014-12-10 06:18:57 +0000134 DenseMap<const Record*, std::unique_ptr<CodeGenInstruction>> &
135 getInstructions() const {
Chris Lattnerc860eca2004-08-01 05:04:00 +0000136 if (Instructions.empty()) ReadInstructions();
137 return Instructions;
138 }
Chris Lattner9aec14b2010-03-19 00:07:20 +0000139public:
Jim Grosbachf910bf22011-03-11 01:27:24 +0000140
Chris Lattner1802b172010-03-19 01:07:44 +0000141 CodeGenInstruction &getInstruction(const Record *InstRec) const {
142 if (Instructions.empty()) ReadInstructions();
Craig Topper48a8e642014-12-10 06:18:57 +0000143 auto I = Instructions.find(InstRec);
Chris Lattner1802b172010-03-19 01:07:44 +0000144 assert(I != Instructions.end() && "Not an instruction");
145 return *I->second;
146 }
Chris Lattnerfcffc982005-09-14 18:02:53 +0000147
Benjamin Kramer4890a712018-01-24 22:35:11 +0000148 /// Returns the number of predefined instructions.
149 static unsigned getNumFixedInstructions();
150
Chris Lattner945e8652005-01-22 18:58:51 +0000151 /// getInstructionsByEnumValue - Return all of the instructions defined by the
152 /// target, ordered by their enum value.
Craig Topperf9265322016-01-17 20:38:14 +0000153 ArrayRef<const CodeGenInstruction *>
Chris Lattner4763dbe2010-03-19 01:00:55 +0000154 getInstructionsByEnumValue() const {
Chris Lattner918be522010-03-19 00:34:35 +0000155 if (InstrsByEnum.empty()) ComputeInstrsByEnum();
156 return InstrsByEnum;
157 }
Chris Lattner945e8652005-01-22 18:58:51 +0000158
Craig Topperf9265322016-01-17 20:38:14 +0000159 typedef ArrayRef<const CodeGenInstruction *>::const_iterator inst_iterator;
Chris Lattner4763dbe2010-03-19 01:00:55 +0000160 inst_iterator inst_begin() const{return getInstructionsByEnumValue().begin();}
161 inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); }
Jim Grosbachf910bf22011-03-11 01:27:24 +0000162
163
Misha Brukman243ded52004-10-14 05:50:43 +0000164 /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]?
165 ///
166 bool isLittleEndianEncoding() const;
Jim Grosbachf910bf22011-03-11 01:27:24 +0000167
Hal Finkel81e6fcc2013-12-17 22:37:50 +0000168 /// reverseBitsForLittleEndianEncoding - For little-endian instruction bit
169 /// encodings, reverse the bit order of all instructions.
170 void reverseBitsForLittleEndianEncoding();
171
Jakob Stoklund Olesen9dc03bb2012-08-23 19:34:41 +0000172 /// guessInstructionProperties - should we just guess unset instruction
173 /// properties?
174 bool guessInstructionProperties() const;
175
Chris Lattner918be522010-03-19 00:34:35 +0000176private:
Chris Lattner4763dbe2010-03-19 01:00:55 +0000177 void ComputeInstrsByEnum() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000178};
179
Evan Cheng9b9567b2005-12-08 02:00:36 +0000180/// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
181/// tablegen class in TargetSelectionDAG.td
182class ComplexPattern {
Owen Anderson9f944592009-08-11 20:47:22 +0000183 MVT::SimpleValueType Ty;
Evan Cheng9b9567b2005-12-08 02:00:36 +0000184 unsigned NumOperands;
185 std::string SelectFunc;
Evan Chengc9a62002005-12-08 02:14:08 +0000186 std::vector<Record*> RootNodes;
Christopher Lamb0592cf72008-01-31 07:27:46 +0000187 unsigned Properties; // Node properties
Peter Collingbourne32ab3a82016-11-09 23:53:43 +0000188 unsigned Complexity;
Evan Cheng9b9567b2005-12-08 02:00:36 +0000189public:
Evan Cheng9b9567b2005-12-08 02:00:36 +0000190 ComplexPattern(Record *R);
191
Owen Anderson9f944592009-08-11 20:47:22 +0000192 MVT::SimpleValueType getValueType() const { return Ty; }
Evan Cheng9b9567b2005-12-08 02:00:36 +0000193 unsigned getNumOperands() const { return NumOperands; }
194 const std::string &getSelectFunc() const { return SelectFunc; }
Evan Chengc9a62002005-12-08 02:14:08 +0000195 const std::vector<Record*> &getRootNodes() const {
196 return RootNodes;
Evan Cheng9b9567b2005-12-08 02:00:36 +0000197 }
Evan Cheng2022c792006-10-11 21:02:01 +0000198 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Peter Collingbourne32ab3a82016-11-09 23:53:43 +0000199 unsigned getComplexity() const { return Complexity; }
Evan Cheng9b9567b2005-12-08 02:00:36 +0000200};
201
Brian Gaeke960707c2003-11-11 22:41:34 +0000202} // End llvm namespace
203
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000204#endif