Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/DAGISelEmitter.cpp
35258 views
//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This tablegen backend emits a DAG instruction selector.9//10//===----------------------------------------------------------------------===//1112#include "Common/CodeGenDAGPatterns.h"13#include "Common/CodeGenInstruction.h"14#include "Common/CodeGenTarget.h"15#include "Common/DAGISelMatcher.h"16#include "llvm/Support/Debug.h"17#include "llvm/TableGen/Record.h"18#include "llvm/TableGen/TableGenBackend.h"19using namespace llvm;2021#define DEBUG_TYPE "dag-isel-emitter"2223namespace {24/// DAGISelEmitter - The top-level class which coordinates construction25/// and emission of the instruction selector.26class DAGISelEmitter {27RecordKeeper &Records; // Just so we can get at the timing functions.28CodeGenDAGPatterns CGP;2930public:31explicit DAGISelEmitter(RecordKeeper &R) : Records(R), CGP(R) {}32void run(raw_ostream &OS);33};34} // End anonymous namespace3536//===----------------------------------------------------------------------===//37// DAGISelEmitter Helper methods38//3940/// Compute the number of instructions for this pattern.41/// This is a temporary hack. We should really include the instruction42/// latencies in this calculation.43static unsigned getResultPatternCost(TreePatternNode &P,44const CodeGenDAGPatterns &CGP) {45if (P.isLeaf())46return 0;4748unsigned Cost = 0;49Record *Op = P.getOperator();50if (Op->isSubClassOf("Instruction")) {51Cost++;52CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);53if (II.usesCustomInserter)54Cost += 10;55}56for (unsigned i = 0, e = P.getNumChildren(); i != e; ++i)57Cost += getResultPatternCost(P.getChild(i), CGP);58return Cost;59}6061/// getResultPatternCodeSize - Compute the code size of instructions for this62/// pattern.63static unsigned getResultPatternSize(TreePatternNode &P,64const CodeGenDAGPatterns &CGP) {65if (P.isLeaf())66return 0;6768unsigned Cost = 0;69Record *Op = P.getOperator();70if (Op->isSubClassOf("Instruction")) {71Cost += Op->getValueAsInt("CodeSize");72}73for (unsigned i = 0, e = P.getNumChildren(); i != e; ++i)74Cost += getResultPatternSize(P.getChild(i), CGP);75return Cost;76}7778namespace {79// PatternSortingPredicate - return true if we prefer to match LHS before RHS.80// In particular, we want to match maximal patterns first and lowest cost within81// a particular complexity first.82struct PatternSortingPredicate {83PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}84CodeGenDAGPatterns &CGP;8586bool operator()(const PatternToMatch *LHS, const PatternToMatch *RHS) {87const TreePatternNode < = LHS->getSrcPattern();88const TreePatternNode &RT = RHS->getSrcPattern();8990MVT LHSVT = LT.getNumTypes() != 0 ? LT.getSimpleType(0) : MVT::Other;91MVT RHSVT = RT.getNumTypes() != 0 ? RT.getSimpleType(0) : MVT::Other;92if (LHSVT.isVector() != RHSVT.isVector())93return RHSVT.isVector();9495if (LHSVT.isFloatingPoint() != RHSVT.isFloatingPoint())96return RHSVT.isFloatingPoint();9798// Otherwise, if the patterns might both match, sort based on complexity,99// which means that we prefer to match patterns that cover more nodes in the100// input over nodes that cover fewer.101int LHSSize = LHS->getPatternComplexity(CGP);102int RHSSize = RHS->getPatternComplexity(CGP);103if (LHSSize > RHSSize)104return true; // LHS -> bigger -> less cost105if (LHSSize < RHSSize)106return false;107108// If the patterns have equal complexity, compare generated instruction cost109unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);110unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);111if (LHSCost < RHSCost)112return true;113if (LHSCost > RHSCost)114return false;115116unsigned LHSPatSize = getResultPatternSize(LHS->getDstPattern(), CGP);117unsigned RHSPatSize = getResultPatternSize(RHS->getDstPattern(), CGP);118if (LHSPatSize < RHSPatSize)119return true;120if (LHSPatSize > RHSPatSize)121return false;122123// Sort based on the UID of the pattern, to reflect source order.124// Note that this is not guaranteed to be unique, since a single source125// pattern may have been resolved into multiple match patterns due to126// alternative fragments. To ensure deterministic output, always use127// std::stable_sort with this predicate.128return LHS->getID() < RHS->getID();129}130};131} // End anonymous namespace132133void DAGISelEmitter::run(raw_ostream &OS) {134Records.startTimer("Parse patterns");135emitSourceFileHeader("DAG Instruction Selector for the " +136CGP.getTargetInfo().getName().str() + " target",137OS);138139OS << "// *** NOTE: This file is #included into the middle of the target\n"140<< "// *** instruction selector class. These functions are really "141<< "methods.\n\n";142143OS << "// If GET_DAGISEL_DECL is #defined with any value, only function\n"144"// declarations will be included when this file is included.\n"145"// If GET_DAGISEL_BODY is #defined, its value should be the name of\n"146"// the instruction selector class. Function bodies will be emitted\n"147"// and each function's name will be qualified with the name of the\n"148"// class.\n"149"//\n"150"// When neither of the GET_DAGISEL* macros is defined, the functions\n"151"// are emitted inline.\n\n";152153LLVM_DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n";154for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),155E = CGP.ptm_end();156I != E; ++I) {157errs() << "PATTERN: ";158I->getSrcPattern().dump();159errs() << "\nRESULT: ";160I->getDstPattern().dump();161errs() << "\n";162});163164// Add all the patterns to a temporary list so we can sort them.165Records.startTimer("Sort patterns");166std::vector<const PatternToMatch *> Patterns;167for (const PatternToMatch &PTM : CGP.ptms())168Patterns.push_back(&PTM);169170// We want to process the matches in order of minimal cost. Sort the patterns171// so the least cost one is at the start.172llvm::stable_sort(Patterns, PatternSortingPredicate(CGP));173174// Convert each variant of each pattern into a Matcher.175Records.startTimer("Convert to matchers");176SmallVector<Matcher *, 0> PatternMatchers;177for (const PatternToMatch *PTM : Patterns) {178for (unsigned Variant = 0;; ++Variant) {179if (Matcher *M = ConvertPatternToMatcher(*PTM, Variant, CGP))180PatternMatchers.push_back(M);181else182break;183}184}185186std::unique_ptr<Matcher> TheMatcher =187std::make_unique<ScopeMatcher>(std::move(PatternMatchers));188189Records.startTimer("Optimize matchers");190OptimizeMatcher(TheMatcher, CGP);191192// Matcher->dump();193194Records.startTimer("Emit matcher table");195EmitMatcherTable(TheMatcher.get(), CGP, OS);196}197198static TableGen::Emitter::OptClass<DAGISelEmitter>199X("gen-dag-isel", "Generate a DAG instruction selector");200201202