blob: d2833d5b6a920251d42fcdaf7a37d8345e9b9c62 [file] [log] [blame]
Chris Lattner803a5f62004-08-01 04:04:35 +00001//===- CodeGenTarget.h - Target Class Wrapper -------------------*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-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 Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattner45872072003-08-07 05:38:11 +00009//
Chris Lattner803a5f62004-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 Sonnenberger61131ab2012-10-25 20:33:17 +000012// place that needs to check it for validity. All of these classes abort
13// on error conditions.
Chris Lattner45872072003-08-07 05:38:11 +000014//
15//===----------------------------------------------------------------------===//
16
Benjamin Kramer00e08fc2014-08-13 16:26:38 +000017#ifndef LLVM_UTILS_TABLEGEN_CODEGENTARGET_H
18#define LLVM_UTILS_TABLEGEN_CODEGENTARGET_H
Chris Lattner45872072003-08-07 05:38:11 +000019
Krzysztof Parzyszekdb815642017-09-14 16:56:21 +000020#include "CodeGenHwModes.h"
Chris Lattnerec352402004-08-01 05:04:00 +000021#include "CodeGenInstruction.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000022#include "CodeGenRegisters.h"
Krzysztof Parzyszekdb815642017-09-14 16:56:21 +000023#include "InfoByHwMode.h"
Matt Arsenault082879a2017-12-20 19:36:28 +000024#include "SDNodeProperties.h"
Chris Lattnere14d2e22010-03-19 01:07:44 +000025#include "llvm/Support/raw_ostream.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000026#include "llvm/TableGen/Record.h"
Dan Gohman8a178702009-04-13 15:24:11 +000027#include <algorithm>
Brian Gaeked0fde302003-11-11 22:41:34 +000028
29namespace llvm {
30
Chris Lattner1fca5ff2004-10-27 16:14:51 +000031struct CodeGenRegister;
Andrew Trick2661b412012-07-07 04:00:00 +000032class CodeGenSchedModels;
Chris Lattner8850a1b2006-03-27 22:48:18 +000033class CodeGenTarget;
Chris Lattner45872072003-08-07 05:38:11 +000034
Owen Anderson825b72b2009-08-11 20:47:22 +000035/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands83ec4b62008-06-06 12:08:01 +000036/// record corresponds to.
Owen Anderson825b72b2009-08-11 20:47:22 +000037MVT::SimpleValueType getValueType(Record *Rec);
Chris Lattner45872072003-08-07 05:38:11 +000038
Matt Arsenault2b7ac012016-05-25 18:07:40 +000039StringRef getName(MVT::SimpleValueType T);
40StringRef getEnumName(MVT::SimpleValueType T);
Chris Lattner45872072003-08-07 05:38:11 +000041
Chris Lattner6cefb772008-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 Grosbach510207c2011-03-11 01:27:24 +000045
Chris Lattner45872072003-08-07 05:38:11 +000046/// CodeGenTarget - This class corresponds to the Target class in the .td files.
47///
48class CodeGenTarget {
Chris Lattner67db8832010-12-13 00:23:57 +000049 RecordKeeper &Records;
Chris Lattner45872072003-08-07 05:38:11 +000050 Record *TargetRec;
Chris Lattner45872072003-08-07 05:38:11 +000051
Craig Topperf492c9e2014-12-10 06:18:57 +000052 mutable DenseMap<const Record*,
53 std::unique_ptr<CodeGenInstruction>> Instructions;
Craig Topper0c839522014-11-28 20:30:37 +000054 mutable std::unique_ptr<CodeGenRegBank> RegBank;
Owen Andersonbea6f612011-06-27 21:06:21 +000055 mutable std::vector<Record*> RegAltNameIndices;
Krzysztof Parzyszekdb815642017-09-14 16:56:21 +000056 mutable SmallVector<ValueTypeByHwMode, 8> LegalValueTypes;
57 CodeGenHwModes CGH;
Owen Andersonbea6f612011-06-27 21:06:21 +000058 void ReadRegAltNameIndices() const;
Chris Lattner056afef2004-08-21 04:05:00 +000059 void ReadInstructions() const;
Chris Lattnere9f4ba82005-09-08 21:43:21 +000060 void ReadLegalValueTypes() const;
Jim Grosbach510207c2011-03-11 01:27:24 +000061
Craig Topper0c839522014-11-28 20:30:37 +000062 mutable std::unique_ptr<CodeGenSchedModels> SchedModels;
Andrew Trick2661b412012-07-07 04:00:00 +000063
Chris Lattner6a91b182010-03-19 01:00:55 +000064 mutable std::vector<const CodeGenInstruction*> InstrsByEnum;
Roman Tereshin673840e2018-05-23 22:10:21 +000065 mutable unsigned NumPseudoInstructions = 0;
Chris Lattner45872072003-08-07 05:38:11 +000066public:
Chris Lattner67db8832010-12-13 00:23:57 +000067 CodeGenTarget(RecordKeeper &Records);
Andrew Trick2661b412012-07-07 04:00:00 +000068 ~CodeGenTarget();
Chris Lattner45872072003-08-07 05:38:11 +000069
70 Record *getTargetRecord() const { return TargetRec; }
Matthias Braun0c517c82016-12-04 05:48:16 +000071 const StringRef getName() const;
Chris Lattner45872072003-08-07 05:38:11 +000072
Dan Gohman1e0ee4b2008-08-20 21:45:57 +000073 /// getInstNamespace - Return the target-specific instruction namespace.
74 ///
Craig Topper77eddb72017-07-07 06:22:35 +000075 StringRef getInstNamespace() const;
Dan Gohman1e0ee4b2008-08-20 21:45:57 +000076
Chris Lattner175580c2004-08-14 22:50:53 +000077 /// getInstructionSet - Return the InstructionSet object.
Chris Lattnerec352402004-08-01 05:04:00 +000078 ///
Chris Lattner45872072003-08-07 05:38:11 +000079 Record *getInstructionSet() const;
80
Geoff Berry13357c92018-02-23 18:25:08 +000081 /// getAllowRegisterRenaming - Return the AllowRegisterRenaming flag value for
82 /// this target.
83 ///
84 bool getAllowRegisterRenaming() const;
85
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000086 /// getAsmParser - Return the AssemblyParser definition for this target.
87 ///
88 Record *getAsmParser() const;
89
Devang Patel0dbcada2012-01-09 19:13:28 +000090 /// getAsmParserVariant - Return the AssmblyParserVariant definition for
91 /// this target.
92 ///
93 Record *getAsmParserVariant(unsigned i) const;
94
Andrew Trick2661b412012-07-07 04:00:00 +000095 /// getAsmParserVariantCount - Return the AssmblyParserVariant definition
Devang Patel0dbcada2012-01-09 19:13:28 +000096 /// available for this target.
97 ///
98 unsigned getAsmParserVariantCount() const;
99
Chris Lattner175580c2004-08-14 22:50:53 +0000100 /// getAsmWriter - Return the AssemblyWriter definition for this target.
101 ///
102 Record *getAsmWriter() const;
103
Jakob Stoklund Olesendc29c442011-06-10 18:40:00 +0000104 /// getRegBank - Return the register bank description.
105 CodeGenRegBank &getRegBank() const;
106
Chris Lattnerec6f0962010-11-02 18:10:06 +0000107 /// getRegisterByName - If there is a register with the specific AsmName,
108 /// return it.
109 const CodeGenRegister *getRegisterByName(StringRef Name) const;
Chris Lattnerec352402004-08-01 05:04:00 +0000110
Owen Andersonbea6f612011-06-27 21:06:21 +0000111 const std::vector<Record*> &getRegAltNameIndices() const {
112 if (RegAltNameIndices.empty()) ReadRegAltNameIndices();
113 return RegAltNameIndices;
114 }
115
Nate Begemanddb39542005-12-01 00:06:14 +0000116 const CodeGenRegisterClass &getRegisterClass(Record *R) const {
Jakob Stoklund Olesen7b9cafd2011-06-15 00:20:40 +0000117 return *getRegBank().getRegClass(R);
Chris Lattner5c4736a2005-12-05 02:35:08 +0000118 }
Evan Cheng44a65fa2006-05-16 07:05:30 +0000119
Duncan Sands83ec4b62008-06-06 12:08:01 +0000120 /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the
Evan Cheng44a65fa2006-05-16 07:05:30 +0000121 /// specified physical register.
Krzysztof Parzyszekdb815642017-09-14 16:56:21 +0000122 std::vector<ValueTypeByHwMode> getRegisterVTs(Record *R) const;
Jim Grosbach510207c2011-03-11 01:27:24 +0000123
Krzysztof Parzyszekdb815642017-09-14 16:56:21 +0000124 ArrayRef<ValueTypeByHwMode> getLegalValueTypes() const {
125 if (LegalValueTypes.empty())
126 ReadLegalValueTypes();
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000127 return LegalValueTypes;
128 }
Jim Grosbach510207c2011-03-11 01:27:24 +0000129
Andrew Trick2661b412012-07-07 04:00:00 +0000130 CodeGenSchedModels &getSchedModels() const;
131
Krzysztof Parzyszekdb815642017-09-14 16:56:21 +0000132 const CodeGenHwModes &getHwModes() const { return CGH; }
133
Chris Lattnerb61e09d2010-03-19 00:18:23 +0000134private:
Craig Topperf492c9e2014-12-10 06:18:57 +0000135 DenseMap<const Record*, std::unique_ptr<CodeGenInstruction>> &
136 getInstructions() const {
Chris Lattnerec352402004-08-01 05:04:00 +0000137 if (Instructions.empty()) ReadInstructions();
138 return Instructions;
139 }
Chris Lattnerf30187a2010-03-19 00:07:20 +0000140public:
Jim Grosbach510207c2011-03-11 01:27:24 +0000141
Chris Lattnere14d2e22010-03-19 01:07:44 +0000142 CodeGenInstruction &getInstruction(const Record *InstRec) const {
143 if (Instructions.empty()) ReadInstructions();
Craig Topperf492c9e2014-12-10 06:18:57 +0000144 auto I = Instructions.find(InstRec);
Chris Lattnere14d2e22010-03-19 01:07:44 +0000145 assert(I != Instructions.end() && "Not an instruction");
146 return *I->second;
147 }
Chris Lattnera974b202005-09-14 18:02:53 +0000148
Benjamin Kramer8001e1b2018-01-24 22:35:11 +0000149 /// Returns the number of predefined instructions.
150 static unsigned getNumFixedInstructions();
151
Roman Tereshin673840e2018-05-23 22:10:21 +0000152 /// Returns the number of pseudo instructions.
153 unsigned getNumPseudoInstructions() const {
154 if (InstrsByEnum.empty())
155 ComputeInstrsByEnum();
156 return NumPseudoInstructions;
157 }
158
Roman Tereshinc349f812018-05-23 20:45:43 +0000159 /// Return all of the instructions defined by the target, ordered by their
160 /// enum value.
Roman Tereshin673840e2018-05-23 22:10:21 +0000161 /// The following order of instructions is also guaranteed:
162 /// - fixed / generic instructions as declared in TargetOpcodes.def, in order;
163 /// - pseudo instructions in lexicographical order sorted by name;
164 /// - other instructions in lexicographical order sorted by name.
Roman Tereshinc349f812018-05-23 20:45:43 +0000165 ArrayRef<const CodeGenInstruction *> getInstructionsByEnumValue() const {
166 if (InstrsByEnum.empty())
167 ComputeInstrsByEnum();
Chris Lattnerf6502782010-03-19 00:34:35 +0000168 return InstrsByEnum;
169 }
Chris Lattnerd6488672005-01-22 18:58:51 +0000170
Craig Topper3356fb62016-01-17 20:38:14 +0000171 typedef ArrayRef<const CodeGenInstruction *>::const_iterator inst_iterator;
Chris Lattner6a91b182010-03-19 01:00:55 +0000172 inst_iterator inst_begin() const{return getInstructionsByEnumValue().begin();}
173 inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); }
Jim Grosbach510207c2011-03-11 01:27:24 +0000174
175
Misha Brukman35e83cc2004-10-14 05:50:43 +0000176 /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]?
177 ///
178 bool isLittleEndianEncoding() const;
Jim Grosbach510207c2011-03-11 01:27:24 +0000179
Hal Finkelaf73dfe2013-12-17 22:37:50 +0000180 /// reverseBitsForLittleEndianEncoding - For little-endian instruction bit
181 /// encodings, reverse the bit order of all instructions.
182 void reverseBitsForLittleEndianEncoding();
183
Jakob Stoklund Olesenf104bf62012-08-23 19:34:41 +0000184 /// guessInstructionProperties - should we just guess unset instruction
185 /// properties?
186 bool guessInstructionProperties() const;
187
Chris Lattnerf6502782010-03-19 00:34:35 +0000188private:
Chris Lattner6a91b182010-03-19 01:00:55 +0000189 void ComputeInstrsByEnum() const;
Chris Lattner45872072003-08-07 05:38:11 +0000190};
191
Evan Cheng0fc71982005-12-08 02:00:36 +0000192/// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern
193/// tablegen class in TargetSelectionDAG.td
194class ComplexPattern {
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 MVT::SimpleValueType Ty;
Evan Cheng0fc71982005-12-08 02:00:36 +0000196 unsigned NumOperands;
197 std::string SelectFunc;
Evan Cheng3aa39f42005-12-08 02:14:08 +0000198 std::vector<Record*> RootNodes;
Christopher Lamb85356242008-01-31 07:27:46 +0000199 unsigned Properties; // Node properties
Peter Collingbourne027f4d02016-11-09 23:53:43 +0000200 unsigned Complexity;
Evan Cheng0fc71982005-12-08 02:00:36 +0000201public:
Evan Cheng0fc71982005-12-08 02:00:36 +0000202 ComplexPattern(Record *R);
203
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 MVT::SimpleValueType getValueType() const { return Ty; }
Evan Cheng0fc71982005-12-08 02:00:36 +0000205 unsigned getNumOperands() const { return NumOperands; }
206 const std::string &getSelectFunc() const { return SelectFunc; }
Evan Cheng3aa39f42005-12-08 02:14:08 +0000207 const std::vector<Record*> &getRootNodes() const {
208 return RootNodes;
Evan Cheng0fc71982005-12-08 02:00:36 +0000209 }
Evan Cheng94b30402006-10-11 21:02:01 +0000210 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Peter Collingbourne027f4d02016-11-09 23:53:43 +0000211 unsigned getComplexity() const { return Complexity; }
Evan Cheng0fc71982005-12-08 02:00:36 +0000212};
213
Brian Gaeked0fde302003-11-11 22:41:34 +0000214} // End llvm namespace
215
Chris Lattner45872072003-08-07 05:38:11 +0000216#endif