mirror of
https://github.com/freebsd/freebsd-src.git
synced 2026-10-31 12:56:10 +01:00
MFC r309126 (by emaste):
Correct lld llvm-tblgen dependency file name
MFC r309169:
Get rid of separate Subversion mergeinfo properties for llvm-dwarfdump
and llvm-lto. The mergeinfo confuses Subversion enormously, and these
directories will just use the mergeinfo for llvm itself.
MFC r312765:
Pull in r276136 from upstream llvm trunk (by Wei Mi):
Use ValueOffsetPair to enhance value reuse during SCEV expansion.
In D12090, the ExprValueMap was added to reuse existing value during
SCEV expansion. However, const folding and sext/zext distribution can
make the reuse still difficult.
A simplified case is: suppose we know S1 expands to V1 in
ExprValueMap, and
S1 = S2 + C_a
S3 = S2 + C_b
where C_a and C_b are different SCEVConstants. Then we'd like to
expand S3 as V1 - C_a + C_b instead of expanding S2 literally. It is
helpful when S2 is a complex SCEV expr and S2 has no entry in
ExprValueMap, which is usually caused by the fact that S3 is
generated from S1 after const folding.
In order to do that, we represent ExprValueMap as a mapping from SCEV
to ValueOffsetPair. We will save both S1->{V1, 0} and S2->{V1, C_a}
into the ExprValueMap when we create SCEV for V1. When S3 is
expanded, it will first expand S2 to V1 - C_a because of S2->{V1,
C_a} in the map, then expand S3 to V1 - C_a + C_b.
Differential Revision: https://reviews.llvm.org/D21313
This should fix assertion failures when building OpenCV >= 3.1.
PR: 215649
MFC r312831:
Revert r312765 for now, since it causes assertions when building
lang/spidermonkey24.
Reported by: antoine
PR: 215649
MFC r316511 (by jhb):
Add an implementation of __ffssi2() derived from __ffsdi2().
Newer versions of GCC include an __ffssi2() symbol in libgcc and the
compiler can emit calls to it in generated code. This is true for at
least GCC 6.2 when compiling world for mips and mips64.
Reviewed by: jmallett, dim
Sponsored by: DARPA / AFRL
Differential Revision: https://reviews.freebsd.org/D10086
MFC r318601 (by adrian):
[libcompiler-rt] add bswapdi2/bswapsi2
This is required for mips gcc 6.3 userland to build/run.
Reviewed by: emaste, dim
Approved by: emaste
Differential Revision: https://reviews.freebsd.org/D10838
MFC r318884 (by emaste):
lldb: map TRAP_CAP to a trace trap
In the absense of a more specific handler for TRAP_CAP (generated by
ENOTCAPABLE or ECAPMODE while in capability mode) treat it as a trace
trap.
Example usage (testing the bug in PR219173):
% proccontrol -m trapcap lldb usr.bin/hexdump/obj/hexdump -- -Cv -s 1 /bin/ls
...
(lldb) run
Process 12980 launching
Process 12980 launched: '.../usr.bin/hexdump/obj/hexdump' (x86_64)
Process 12980 stopped
* thread #1, stop reason = trace
frame #0: 0x0000004b80c65f1a libc.so.7`__sys_lseek + 10
...
In the future we should have LLDB control the trapcap procctl itself
(as it does with ASLR), as well as report a specific stop reason.
This change eliminates an assertion failure from LLDB for now.
MFC r319796:
Remove a few unneeded files from libllvm, libclang and liblldb.
MFC r319885 (by emaste):
lld: ELF: Fix ICF crash on absolute symbol relocations.
If two sections contained relocations to absolute symbols with the same
value we would crash when trying to access their sections. Add a check that
both symbols point to sections before accessing their sections, and treat
absolute symbols as equal if their values are equal.
Obtained from: LLD commit r292578
MFC r319918:
Revert r319796 for now, it can cause undefined references when linking
in some circumstances.
Reported by: Shawn Webb <[email protected]>
MFC r319957 (by emaste):
lld: Add armelf emulation mode
Obtained from: LLD r305375
MFC r321369:
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
5.0.0 (trunk r308421). Upstream has branched for the 5.0.0 release,
which should be in about a month. Please report bugs and regressions,
so we can get them into the release.
Please note that from 3.5.0 onwards, clang, llvm and lldb require C++11
support to build; see UPDATING for more information.
MFC r321420:
Add a few more object files to liblldb, which should solve errors when
linking the lldb executable in some cases. In particular, when the
-ffunction-sections -fdata-sections options are turned off, or
ineffective.
Reported by: Shawn Webb, Mark Millard
MFC r321433:
Cleanup stale Options.inc files from the previous libllvm build for
clang 4.0.0. Otherwise, these can get included before the two newly
generated ones (which are different) for clang 5.0.0.
Reported by: Mark Millard
MFC r321439 (by bdrewery):
Move llvm Options.inc hack from r321433 for NO_CLEAN to lib/clang/libllvm.
The files are only ever generated to .OBJDIR, not to WORLDTMP (as a
sysroot) and are only ever included from a compilation. So using
a beforebuild target here removes the file before the compilation
tries to include it.
MFC r321664:
Pull in r308891 from upstream llvm trunk (by Benjamin Kramer):
[CodeGenPrepare] Cut off FindAllMemoryUses if there are too many uses.
This avoids excessive compile time. The case I'm looking at is
Function.cpp from an old version of LLVM that still had the giant
memcmp string matcher in it. Before r308322 this compiled in about 2
minutes, after it, clang takes infinite* time to compile it. With
this patch we're at 5 min, which is still bad but this is a
pathological case.
The cut off at 20 uses was chosen by looking at other cut-offs in LLVM
for user scanning. It's probably too high, but does the job and is
very unlikely to regress anything.
Fixes PR33900.
* I'm impatient and aborted after 15 minutes, on the bug report it was
killed after 2h.
Pull in r308986 from upstream llvm trunk (by Simon Pilgrim):
[X86][CGP] Reduce memcmp() expansion to 2 load pairs (PR33914)
D35067/rL308322 attempted to support up to 4 load pairs for memcmp
inlining which resulted in regressions for some optimized libc memcmp
implementations (PR33914).
Until we can match these more optimal cases, this patch reduces the
memcmp expansion to a maximum of 2 load pairs (which matches what we
do for -Os).
This patch should be considered for the 5.0.0 release branch as well
Differential Revision: https://reviews.llvm.org/D35830
These fix a hang (or extremely long compile time) when building older
LLVM ports.
Reported by: antoine
PR: 219139
MFC r321719:
Pull in r309503 from upstream clang trunk (by Richard Smith):
PR33902: Invalidate line number cache when adding more text to
existing buffer.
This led to crashes as the line number cache would report a bogus
line number for a line of code, and we'd try to find a nonexistent
column within the line when printing diagnostics.
This fixes an assertion when building the graphics/champlain port.
Reported by: antoine, kwm
PR: 219139
MFC r321723:
Upgrade our copies of clang, llvm, lld and lldb to r309439 from the
upstream release_50 branch. This is just after upstream's 5.0.0-rc1.
MFC r322320:
Upgrade our copies of clang, llvm and libc++ to r310316 from the
upstream release_50 branch.
MFC r322326 (by emaste):
lldb: Make i386-*-freebsd expression work on JIT path
* Enable i386 ABI creation for freebsd
* Added an extra argument in ABISysV_i386::PrepareTrivialCall for mmap
syscall
* Unlike linux, the last argument of mmap is actually 64-bit(off_t).
This requires us to push an additional word for the higher order bits.
* Prior to this change, ktrace dump will show mmap failures due to
invalid argument coming from the 6th mmap argument.
Submitted by: Karnajit Wangkhem
Differential Revision: https://reviews.llvm.org/D34776
MFC r322360 (by emaste):
lldb: Report inferior signals as signals, not exceptions, on FreeBSD
This is the FreeBSD equivalent of LLVM r238549.
This serves 2 purposes:
* LLDB should handle inferior process signals SIGSEGV/SIGILL/SIGBUS/
SIGFPE the way it is suppose to be handled. Prior to this fix these
signals will neither create a coredump, nor exit from the debugger
or work for signal handling scenario.
* eInvalidCrashReason need not report "unknown crash reason" if we have
a valid si_signo
llvm.org/pr23699
Patch by Karnajit Wangkhem
Differential Revision: https://reviews.llvm.org/D35223
Submitted by: Karnajit Wangkhem
Obtained from: LLVM r310591
MFC r322474 (by emaste):
lld: Add `-z muldefs` option.
Obtained from: LLVM r310757
MFC r322740:
Upgrade our copies of clang, llvm, lld and libc++ to r311219 from the
upstream release_50 branch.
MFC r322855:
Upgrade our copies of clang, llvm, lldb and compiler-rt to r311606 from
the upstream release_50 branch.
As of this version, lib/msun's trig test should also work correctly
again (see bug 220989 for more information).
PR: 220989
MFC r323112:
Upgrade our copies of clang, llvm, lldb and compiler-rt to r312293 from
the upstream release_50 branch. This corresponds to 5.0.0 rc4.
As of this version, the cad/stepcode port should now compile in a more
reasonable time on i386 (see bug 221836 for more information).
PR: 221836
MFC r323245:
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
5.0.0 release (upstream r312559).
Release notes for llvm, clang and lld will be available here soon:
<http://releases.llvm.org/5.0.0/docs/ReleaseNotes.html>
<http://releases.llvm.org/5.0.0/tools/clang/docs/ReleaseNotes.html>
<http://releases.llvm.org/5.0.0/tools/lld/docs/ReleaseNotes.html>
Relnotes: yes
609 lines
23 KiB
C++
609 lines
23 KiB
C++
//===- CodeGenMapTable.cpp - Instruction Mapping Table Generator ----------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
// CodeGenMapTable provides functionality for the TabelGen to create
|
|
// relation mapping between instructions. Relation models are defined using
|
|
// InstrMapping as a base class. This file implements the functionality which
|
|
// parses these definitions and generates relation maps using the information
|
|
// specified there. These maps are emitted as tables in the XXXGenInstrInfo.inc
|
|
// file along with the functions to query them.
|
|
//
|
|
// A relationship model to relate non-predicate instructions with their
|
|
// predicated true/false forms can be defined as follows:
|
|
//
|
|
// def getPredOpcode : InstrMapping {
|
|
// let FilterClass = "PredRel";
|
|
// let RowFields = ["BaseOpcode"];
|
|
// let ColFields = ["PredSense"];
|
|
// let KeyCol = ["none"];
|
|
// let ValueCols = [["true"], ["false"]]; }
|
|
//
|
|
// CodeGenMapTable parses this map and generates a table in XXXGenInstrInfo.inc
|
|
// file that contains the instructions modeling this relationship. This table
|
|
// is defined in the function
|
|
// "int getPredOpcode(uint16_t Opcode, enum PredSense inPredSense)"
|
|
// that can be used to retrieve the predicated form of the instruction by
|
|
// passing its opcode value and the predicate sense (true/false) of the desired
|
|
// instruction as arguments.
|
|
//
|
|
// Short description of the algorithm:
|
|
//
|
|
// 1) Iterate through all the records that derive from "InstrMapping" class.
|
|
// 2) For each record, filter out instructions based on the FilterClass value.
|
|
// 3) Iterate through this set of instructions and insert them into
|
|
// RowInstrMap map based on their RowFields values. RowInstrMap is keyed by the
|
|
// vector of RowFields values and contains vectors of Records (instructions) as
|
|
// values. RowFields is a list of fields that are required to have the same
|
|
// values for all the instructions appearing in the same row of the relation
|
|
// table. All the instructions in a given row of the relation table have some
|
|
// sort of relationship with the key instruction defined by the corresponding
|
|
// relationship model.
|
|
//
|
|
// Ex: RowInstrMap(RowVal1, RowVal2, ...) -> [Instr1, Instr2, Instr3, ... ]
|
|
// Here Instr1, Instr2, Instr3 have same values (RowVal1, RowVal2) for
|
|
// RowFields. These groups of instructions are later matched against ValueCols
|
|
// to determine the column they belong to, if any.
|
|
//
|
|
// While building the RowInstrMap map, collect all the key instructions in
|
|
// KeyInstrVec. These are the instructions having the same values as KeyCol
|
|
// for all the fields listed in ColFields.
|
|
//
|
|
// For Example:
|
|
//
|
|
// Relate non-predicate instructions with their predicated true/false forms.
|
|
//
|
|
// def getPredOpcode : InstrMapping {
|
|
// let FilterClass = "PredRel";
|
|
// let RowFields = ["BaseOpcode"];
|
|
// let ColFields = ["PredSense"];
|
|
// let KeyCol = ["none"];
|
|
// let ValueCols = [["true"], ["false"]]; }
|
|
//
|
|
// Here, only instructions that have "none" as PredSense will be selected as key
|
|
// instructions.
|
|
//
|
|
// 4) For each key instruction, get the group of instructions that share the
|
|
// same key-value as the key instruction from RowInstrMap. Iterate over the list
|
|
// of columns in ValueCols (it is defined as a list<list<string> >. Therefore,
|
|
// it can specify multi-column relationships). For each column, find the
|
|
// instruction from the group that matches all the values for the column.
|
|
// Multiple matches are not allowed.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "CodeGenTarget.h"
|
|
#include "llvm/Support/Format.h"
|
|
#include "llvm/TableGen/Error.h"
|
|
using namespace llvm;
|
|
typedef std::map<std::string, std::vector<Record*> > InstrRelMapTy;
|
|
|
|
typedef std::map<std::vector<Init*>, std::vector<Record*> > RowInstrMapTy;
|
|
|
|
namespace {
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// This class is used to represent InstrMapping class defined in Target.td file.
|
|
class InstrMap {
|
|
private:
|
|
std::string Name;
|
|
std::string FilterClass;
|
|
ListInit *RowFields;
|
|
ListInit *ColFields;
|
|
ListInit *KeyCol;
|
|
std::vector<ListInit*> ValueCols;
|
|
|
|
public:
|
|
InstrMap(Record* MapRec) {
|
|
Name = MapRec->getName();
|
|
|
|
// FilterClass - It's used to reduce the search space only to the
|
|
// instructions that define the kind of relationship modeled by
|
|
// this InstrMapping object/record.
|
|
const RecordVal *Filter = MapRec->getValue("FilterClass");
|
|
FilterClass = Filter->getValue()->getAsUnquotedString();
|
|
|
|
// List of fields/attributes that need to be same across all the
|
|
// instructions in a row of the relation table.
|
|
RowFields = MapRec->getValueAsListInit("RowFields");
|
|
|
|
// List of fields/attributes that are constant across all the instruction
|
|
// in a column of the relation table. Ex: ColFields = 'predSense'
|
|
ColFields = MapRec->getValueAsListInit("ColFields");
|
|
|
|
// Values for the fields/attributes listed in 'ColFields'.
|
|
// Ex: KeyCol = 'noPred' -- key instruction is non-predicated
|
|
KeyCol = MapRec->getValueAsListInit("KeyCol");
|
|
|
|
// List of values for the fields/attributes listed in 'ColFields', one for
|
|
// each column in the relation table.
|
|
//
|
|
// Ex: ValueCols = [['true'],['false']] -- it results two columns in the
|
|
// table. First column requires all the instructions to have predSense
|
|
// set to 'true' and second column requires it to be 'false'.
|
|
ListInit *ColValList = MapRec->getValueAsListInit("ValueCols");
|
|
|
|
// Each instruction map must specify at least one column for it to be valid.
|
|
if (ColValList->empty())
|
|
PrintFatalError(MapRec->getLoc(), "InstrMapping record `" +
|
|
MapRec->getName() + "' has empty " + "`ValueCols' field!");
|
|
|
|
for (Init *I : ColValList->getValues()) {
|
|
ListInit *ColI = dyn_cast<ListInit>(I);
|
|
|
|
// Make sure that all the sub-lists in 'ValueCols' have same number of
|
|
// elements as the fields in 'ColFields'.
|
|
if (ColI->size() != ColFields->size())
|
|
PrintFatalError(MapRec->getLoc(), "Record `" + MapRec->getName() +
|
|
"', field `ValueCols' entries don't match with " +
|
|
" the entries in 'ColFields'!");
|
|
ValueCols.push_back(ColI);
|
|
}
|
|
}
|
|
|
|
std::string getName() const {
|
|
return Name;
|
|
}
|
|
|
|
std::string getFilterClass() {
|
|
return FilterClass;
|
|
}
|
|
|
|
ListInit *getRowFields() const {
|
|
return RowFields;
|
|
}
|
|
|
|
ListInit *getColFields() const {
|
|
return ColFields;
|
|
}
|
|
|
|
ListInit *getKeyCol() const {
|
|
return KeyCol;
|
|
}
|
|
|
|
const std::vector<ListInit*> &getValueCols() const {
|
|
return ValueCols;
|
|
}
|
|
};
|
|
} // End anonymous namespace.
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// class MapTableEmitter : It builds the instruction relation maps using
|
|
// the information provided in InstrMapping records. It outputs these
|
|
// relationship maps as tables into XXXGenInstrInfo.inc file along with the
|
|
// functions to query them.
|
|
|
|
namespace {
|
|
class MapTableEmitter {
|
|
private:
|
|
// std::string TargetName;
|
|
const CodeGenTarget &Target;
|
|
// InstrMapDesc - InstrMapping record to be processed.
|
|
InstrMap InstrMapDesc;
|
|
|
|
// InstrDefs - list of instructions filtered using FilterClass defined
|
|
// in InstrMapDesc.
|
|
std::vector<Record*> InstrDefs;
|
|
|
|
// RowInstrMap - maps RowFields values to the instructions. It's keyed by the
|
|
// values of the row fields and contains vector of records as values.
|
|
RowInstrMapTy RowInstrMap;
|
|
|
|
// KeyInstrVec - list of key instructions.
|
|
std::vector<Record*> KeyInstrVec;
|
|
DenseMap<Record*, std::vector<Record*> > MapTable;
|
|
|
|
public:
|
|
MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec):
|
|
Target(Target), InstrMapDesc(IMRec) {
|
|
const std::string FilterClass = InstrMapDesc.getFilterClass();
|
|
InstrDefs = Records.getAllDerivedDefinitions(FilterClass);
|
|
}
|
|
|
|
void buildRowInstrMap();
|
|
|
|
// Returns true if an instruction is a key instruction, i.e., its ColFields
|
|
// have same values as KeyCol.
|
|
bool isKeyColInstr(Record* CurInstr);
|
|
|
|
// Find column instruction corresponding to a key instruction based on the
|
|
// constraints for that column.
|
|
Record *getInstrForColumn(Record *KeyInstr, ListInit *CurValueCol);
|
|
|
|
// Find column instructions for each key instruction based
|
|
// on ValueCols and store them into MapTable.
|
|
void buildMapTable();
|
|
|
|
void emitBinSearch(raw_ostream &OS, unsigned TableSize);
|
|
void emitTablesWithFunc(raw_ostream &OS);
|
|
unsigned emitBinSearchTable(raw_ostream &OS);
|
|
|
|
// Lookup functions to query binary search tables.
|
|
void emitMapFuncBody(raw_ostream &OS, unsigned TableSize);
|
|
|
|
};
|
|
} // End anonymous namespace.
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Process all the instructions that model this relation (alreday present in
|
|
// InstrDefs) and insert them into RowInstrMap which is keyed by the values of
|
|
// the fields listed as RowFields. It stores vectors of records as values.
|
|
// All the related instructions have the same values for the RowFields thus are
|
|
// part of the same key-value pair.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
void MapTableEmitter::buildRowInstrMap() {
|
|
for (Record *CurInstr : InstrDefs) {
|
|
std::vector<Init*> KeyValue;
|
|
ListInit *RowFields = InstrMapDesc.getRowFields();
|
|
for (Init *RowField : RowFields->getValues()) {
|
|
Init *CurInstrVal = CurInstr->getValue(RowField)->getValue();
|
|
KeyValue.push_back(CurInstrVal);
|
|
}
|
|
|
|
// Collect key instructions into KeyInstrVec. Later, these instructions are
|
|
// processed to assign column position to the instructions sharing
|
|
// their KeyValue in RowInstrMap.
|
|
if (isKeyColInstr(CurInstr))
|
|
KeyInstrVec.push_back(CurInstr);
|
|
|
|
RowInstrMap[KeyValue].push_back(CurInstr);
|
|
}
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Return true if an instruction is a KeyCol instruction.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
bool MapTableEmitter::isKeyColInstr(Record* CurInstr) {
|
|
ListInit *ColFields = InstrMapDesc.getColFields();
|
|
ListInit *KeyCol = InstrMapDesc.getKeyCol();
|
|
|
|
// Check if the instruction is a KeyCol instruction.
|
|
bool MatchFound = true;
|
|
for (unsigned j = 0, endCF = ColFields->size();
|
|
(j < endCF) && MatchFound; j++) {
|
|
RecordVal *ColFieldName = CurInstr->getValue(ColFields->getElement(j));
|
|
std::string CurInstrVal = ColFieldName->getValue()->getAsUnquotedString();
|
|
std::string KeyColValue = KeyCol->getElement(j)->getAsUnquotedString();
|
|
MatchFound = (CurInstrVal == KeyColValue);
|
|
}
|
|
return MatchFound;
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Build a map to link key instructions with the column instructions arranged
|
|
// according to their column positions.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
void MapTableEmitter::buildMapTable() {
|
|
// Find column instructions for a given key based on the ColField
|
|
// constraints.
|
|
const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
|
|
unsigned NumOfCols = ValueCols.size();
|
|
for (Record *CurKeyInstr : KeyInstrVec) {
|
|
std::vector<Record*> ColInstrVec(NumOfCols);
|
|
|
|
// Find the column instruction based on the constraints for the column.
|
|
for (unsigned ColIdx = 0; ColIdx < NumOfCols; ColIdx++) {
|
|
ListInit *CurValueCol = ValueCols[ColIdx];
|
|
Record *ColInstr = getInstrForColumn(CurKeyInstr, CurValueCol);
|
|
ColInstrVec[ColIdx] = ColInstr;
|
|
}
|
|
MapTable[CurKeyInstr] = ColInstrVec;
|
|
}
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Find column instruction based on the constraints for that column.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr,
|
|
ListInit *CurValueCol) {
|
|
ListInit *RowFields = InstrMapDesc.getRowFields();
|
|
std::vector<Init*> KeyValue;
|
|
|
|
// Construct KeyValue using KeyInstr's values for RowFields.
|
|
for (Init *RowField : RowFields->getValues()) {
|
|
Init *KeyInstrVal = KeyInstr->getValue(RowField)->getValue();
|
|
KeyValue.push_back(KeyInstrVal);
|
|
}
|
|
|
|
// Get all the instructions that share the same KeyValue as the KeyInstr
|
|
// in RowInstrMap. We search through these instructions to find a match
|
|
// for the current column, i.e., the instruction which has the same values
|
|
// as CurValueCol for all the fields in ColFields.
|
|
const std::vector<Record*> &RelatedInstrVec = RowInstrMap[KeyValue];
|
|
|
|
ListInit *ColFields = InstrMapDesc.getColFields();
|
|
Record *MatchInstr = nullptr;
|
|
|
|
for (unsigned i = 0, e = RelatedInstrVec.size(); i < e; i++) {
|
|
bool MatchFound = true;
|
|
Record *CurInstr = RelatedInstrVec[i];
|
|
for (unsigned j = 0, endCF = ColFields->size();
|
|
(j < endCF) && MatchFound; j++) {
|
|
Init *ColFieldJ = ColFields->getElement(j);
|
|
Init *CurInstrInit = CurInstr->getValue(ColFieldJ)->getValue();
|
|
std::string CurInstrVal = CurInstrInit->getAsUnquotedString();
|
|
Init *ColFieldJVallue = CurValueCol->getElement(j);
|
|
MatchFound = (CurInstrVal == ColFieldJVallue->getAsUnquotedString());
|
|
}
|
|
|
|
if (MatchFound) {
|
|
if (MatchInstr) {
|
|
// Already had a match
|
|
// Error if multiple matches are found for a column.
|
|
std::string KeyValueStr;
|
|
for (Init *Value : KeyValue) {
|
|
if (!KeyValueStr.empty())
|
|
KeyValueStr += ", ";
|
|
KeyValueStr += Value->getAsString();
|
|
}
|
|
|
|
PrintFatalError("Multiple matches found for `" + KeyInstr->getName() +
|
|
"', for the relation `" + InstrMapDesc.getName() + "', row fields [" +
|
|
KeyValueStr + "], column `" + CurValueCol->getAsString() + "'");
|
|
}
|
|
MatchInstr = CurInstr;
|
|
}
|
|
}
|
|
return MatchInstr;
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Emit one table per relation. Only instructions with a valid relation of a
|
|
// given type are included in the table sorted by their enum values (opcodes).
|
|
// Binary search is used for locating instructions in the table.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
|
|
|
|
ArrayRef<const CodeGenInstruction*> NumberedInstructions =
|
|
Target.getInstructionsByEnumValue();
|
|
StringRef Namespace = Target.getInstNamespace();
|
|
const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
|
|
unsigned NumCol = ValueCols.size();
|
|
unsigned TotalNumInstr = NumberedInstructions.size();
|
|
unsigned TableSize = 0;
|
|
|
|
OS << "static const uint16_t "<<InstrMapDesc.getName();
|
|
// Number of columns in the table are NumCol+1 because key instructions are
|
|
// emitted as first column.
|
|
OS << "Table[]["<< NumCol+1 << "] = {\n";
|
|
for (unsigned i = 0; i < TotalNumInstr; i++) {
|
|
Record *CurInstr = NumberedInstructions[i]->TheDef;
|
|
std::vector<Record*> ColInstrs = MapTable[CurInstr];
|
|
std::string OutStr("");
|
|
unsigned RelExists = 0;
|
|
if (!ColInstrs.empty()) {
|
|
for (unsigned j = 0; j < NumCol; j++) {
|
|
if (ColInstrs[j] != nullptr) {
|
|
RelExists = 1;
|
|
OutStr += ", ";
|
|
OutStr += Namespace;
|
|
OutStr += "::";
|
|
OutStr += ColInstrs[j]->getName();
|
|
} else { OutStr += ", (uint16_t)-1U";}
|
|
}
|
|
|
|
if (RelExists) {
|
|
OS << " { " << Namespace << "::" << CurInstr->getName();
|
|
OS << OutStr <<" },\n";
|
|
TableSize++;
|
|
}
|
|
}
|
|
}
|
|
if (!TableSize) {
|
|
OS << " { " << Namespace << "::" << "INSTRUCTION_LIST_END, ";
|
|
OS << Namespace << "::" << "INSTRUCTION_LIST_END }";
|
|
}
|
|
OS << "}; // End of " << InstrMapDesc.getName() << "Table\n\n";
|
|
return TableSize;
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Emit binary search algorithm as part of the functions used to query
|
|
// relation tables.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) {
|
|
OS << " unsigned mid;\n";
|
|
OS << " unsigned start = 0;\n";
|
|
OS << " unsigned end = " << TableSize << ";\n";
|
|
OS << " while (start < end) {\n";
|
|
OS << " mid = start + (end - start)/2;\n";
|
|
OS << " if (Opcode == " << InstrMapDesc.getName() << "Table[mid][0]) {\n";
|
|
OS << " break;\n";
|
|
OS << " }\n";
|
|
OS << " if (Opcode < " << InstrMapDesc.getName() << "Table[mid][0])\n";
|
|
OS << " end = mid;\n";
|
|
OS << " else\n";
|
|
OS << " start = mid + 1;\n";
|
|
OS << " }\n";
|
|
OS << " if (start == end)\n";
|
|
OS << " return -1; // Instruction doesn't exist in this table.\n\n";
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Emit functions to query relation tables.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
void MapTableEmitter::emitMapFuncBody(raw_ostream &OS,
|
|
unsigned TableSize) {
|
|
|
|
ListInit *ColFields = InstrMapDesc.getColFields();
|
|
const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
|
|
|
|
// Emit binary search algorithm to locate instructions in the
|
|
// relation table. If found, return opcode value from the appropriate column
|
|
// of the table.
|
|
emitBinSearch(OS, TableSize);
|
|
|
|
if (ValueCols.size() > 1) {
|
|
for (unsigned i = 0, e = ValueCols.size(); i < e; i++) {
|
|
ListInit *ColumnI = ValueCols[i];
|
|
for (unsigned j = 0, ColSize = ColumnI->size(); j < ColSize; ++j) {
|
|
std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
|
|
OS << " if (in" << ColName;
|
|
OS << " == ";
|
|
OS << ColName << "_" << ColumnI->getElement(j)->getAsUnquotedString();
|
|
if (j < ColumnI->size() - 1) OS << " && ";
|
|
else OS << ")\n";
|
|
}
|
|
OS << " return " << InstrMapDesc.getName();
|
|
OS << "Table[mid]["<<i+1<<"];\n";
|
|
}
|
|
OS << " return -1;";
|
|
}
|
|
else
|
|
OS << " return " << InstrMapDesc.getName() << "Table[mid][1];\n";
|
|
|
|
OS <<"}\n\n";
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Emit relation tables and the functions to query them.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
|
|
|
|
// Emit function name and the input parameters : mostly opcode value of the
|
|
// current instruction. However, if a table has multiple columns (more than 2
|
|
// since first column is used for the key instructions), then we also need
|
|
// to pass another input to indicate the column to be selected.
|
|
|
|
ListInit *ColFields = InstrMapDesc.getColFields();
|
|
const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
|
|
OS << "// "<< InstrMapDesc.getName() << "\nLLVM_READONLY\n";
|
|
OS << "int "<< InstrMapDesc.getName() << "(uint16_t Opcode";
|
|
if (ValueCols.size() > 1) {
|
|
for (Init *CF : ColFields->getValues()) {
|
|
std::string ColName = CF->getAsUnquotedString();
|
|
OS << ", enum " << ColName << " in" << ColName << ") {\n";
|
|
}
|
|
} else { OS << ") {\n"; }
|
|
|
|
// Emit map table.
|
|
unsigned TableSize = emitBinSearchTable(OS);
|
|
|
|
// Emit rest of the function body.
|
|
emitMapFuncBody(OS, TableSize);
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Emit enums for the column fields across all the instruction maps.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
static void emitEnums(raw_ostream &OS, RecordKeeper &Records) {
|
|
|
|
std::vector<Record*> InstrMapVec;
|
|
InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
|
|
std::map<std::string, std::vector<Init*> > ColFieldValueMap;
|
|
|
|
// Iterate over all InstrMapping records and create a map between column
|
|
// fields and their possible values across all records.
|
|
for (Record *CurMap : InstrMapVec) {
|
|
ListInit *ColFields;
|
|
ColFields = CurMap->getValueAsListInit("ColFields");
|
|
ListInit *List = CurMap->getValueAsListInit("ValueCols");
|
|
std::vector<ListInit*> ValueCols;
|
|
unsigned ListSize = List->size();
|
|
|
|
for (unsigned j = 0; j < ListSize; j++) {
|
|
ListInit *ListJ = dyn_cast<ListInit>(List->getElement(j));
|
|
|
|
if (ListJ->size() != ColFields->size())
|
|
PrintFatalError("Record `" + CurMap->getName() + "', field "
|
|
"`ValueCols' entries don't match with the entries in 'ColFields' !");
|
|
ValueCols.push_back(ListJ);
|
|
}
|
|
|
|
for (unsigned j = 0, endCF = ColFields->size(); j < endCF; j++) {
|
|
for (unsigned k = 0; k < ListSize; k++){
|
|
std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
|
|
ColFieldValueMap[ColName].push_back((ValueCols[k])->getElement(j));
|
|
}
|
|
}
|
|
}
|
|
|
|
for (auto &Entry : ColFieldValueMap) {
|
|
std::vector<Init*> FieldValues = Entry.second;
|
|
|
|
// Delete duplicate entries from ColFieldValueMap
|
|
for (unsigned i = 0; i < FieldValues.size() - 1; i++) {
|
|
Init *CurVal = FieldValues[i];
|
|
for (unsigned j = i+1; j < FieldValues.size(); j++) {
|
|
if (CurVal == FieldValues[j]) {
|
|
FieldValues.erase(FieldValues.begin()+j);
|
|
--j;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Emit enumerated values for the column fields.
|
|
OS << "enum " << Entry.first << " {\n";
|
|
for (unsigned i = 0, endFV = FieldValues.size(); i < endFV; i++) {
|
|
OS << "\t" << Entry.first << "_" << FieldValues[i]->getAsUnquotedString();
|
|
if (i != endFV - 1)
|
|
OS << ",\n";
|
|
else
|
|
OS << "\n};\n\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace llvm {
|
|
//===----------------------------------------------------------------------===//
|
|
// Parse 'InstrMapping' records and use the information to form relationship
|
|
// between instructions. These relations are emitted as a tables along with the
|
|
// functions to query them.
|
|
//===----------------------------------------------------------------------===//
|
|
void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
|
|
CodeGenTarget Target(Records);
|
|
StringRef NameSpace = Target.getInstNamespace();
|
|
std::vector<Record*> InstrMapVec;
|
|
InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
|
|
|
|
if (InstrMapVec.empty())
|
|
return;
|
|
|
|
OS << "#ifdef GET_INSTRMAP_INFO\n";
|
|
OS << "#undef GET_INSTRMAP_INFO\n";
|
|
OS << "namespace llvm {\n\n";
|
|
OS << "namespace " << NameSpace << " {\n\n";
|
|
|
|
// Emit coulumn field names and their values as enums.
|
|
emitEnums(OS, Records);
|
|
|
|
// Iterate over all instruction mapping records and construct relationship
|
|
// maps based on the information specified there.
|
|
//
|
|
for (Record *CurMap : InstrMapVec) {
|
|
MapTableEmitter IMap(Target, Records, CurMap);
|
|
|
|
// Build RowInstrMap to group instructions based on their values for
|
|
// RowFields. In the process, also collect key instructions into
|
|
// KeyInstrVec.
|
|
IMap.buildRowInstrMap();
|
|
|
|
// Build MapTable to map key instructions with the corresponding column
|
|
// instructions.
|
|
IMap.buildMapTable();
|
|
|
|
// Emit map tables and the functions to query them.
|
|
IMap.emitTablesWithFunc(OS);
|
|
}
|
|
OS << "} // End " << NameSpace << " namespace\n";
|
|
OS << "} // End llvm namespace\n";
|
|
OS << "#endif // GET_INSTRMAP_INFO\n\n";
|
|
}
|
|
|
|
} // End llvm namespace
|