#include "DAGISelMatcher.h"
#include "CodeGenDAGPatterns.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
#define DEBUG_TYPE "isel-opt"
static void ContractNodes(std::unique_ptr<Matcher> &MatcherPtr,
const CodeGenDAGPatterns &CGP) {
Matcher *N = MatcherPtr.get();
if (!N) return;
if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
std::unique_ptr<Matcher> Child(Scope->takeChild(i));
ContractNodes(Child, CGP);
Scope->resetChild(i, Child.release());
}
return;
}
if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) {
Matcher *New = nullptr;
if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext()))
if (MC->getChildNo() < 8) New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor(),
RM->getResultNo());
if (CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(MC->getNext()))
if (MC->getChildNo() < 8 && CT->getResNo() == 0) New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType());
if (CheckSameMatcher *CS = dyn_cast<CheckSameMatcher>(MC->getNext()))
if (MC->getChildNo() < 4) New = new CheckChildSameMatcher(MC->getChildNo(), CS->getMatchNumber());
if (CheckIntegerMatcher *CI = dyn_cast<CheckIntegerMatcher>(MC->getNext()))
if (MC->getChildNo() < 5) New = new CheckChildIntegerMatcher(MC->getChildNo(), CI->getValue());
if (auto *CCC = dyn_cast<CheckCondCodeMatcher>(MC->getNext()))
if (MC->getChildNo() == 2) New = new CheckChild2CondCodeMatcher(CCC->getCondCodeName());
if (New) {
New->setNext(MatcherPtr.release());
MatcherPtr.reset(New);
MC->setNext(MC->getNext()->takeNext());
return ContractNodes(MatcherPtr, CGP);
}
}
if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N))
if (MoveParentMatcher *MP =
dyn_cast<MoveParentMatcher>(MC->getNext())) {
MatcherPtr.reset(MP->takeNext());
return ContractNodes(MatcherPtr, CGP);
}
if (EmitNodeMatcher *EN = dyn_cast<EmitNodeMatcher>(N))
if (CompleteMatchMatcher *CM =
dyn_cast<CompleteMatchMatcher>(EN->getNext())) {
unsigned RootResultFirst = EN->getFirstResultSlot();
bool ResultsMatch = true;
for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
if (CM->getResult(i) != RootResultFirst+i)
ResultsMatch = false;
const PatternToMatch &Pattern = CM->getPattern();
if (!EN->hasChain() &&
Pattern.getSrcPattern()->NodeHasProperty(SDNPHasChain, CGP))
ResultsMatch = false;
if (!EN->hasOutFlag() &&
Pattern.getSrcPattern()->NodeHasProperty(SDNPOutGlue, CGP))
ResultsMatch = false;
#if 0#endif
if (ResultsMatch) {
const SmallVectorImpl<MVT::SimpleValueType> &VTs = EN->getVTList();
const SmallVectorImpl<unsigned> &Operands = EN->getOperandList();
MatcherPtr.reset(new MorphNodeToMatcher(EN->getOpcodeName(),
VTs, Operands,
EN->hasChain(), EN->hasInFlag(),
EN->hasOutFlag(),
EN->hasMemRefs(),
EN->getNumFixedArityOperands(),
Pattern));
return;
}
}
ContractNodes(N->getNextPtr(), CGP);
if ((isa<CheckTypeMatcher>(N) || isa<CheckChildTypeMatcher>(N) ||
isa<RecordMatcher>(N)) &&
isa<CheckOpcodeMatcher>(N->getNext())) {
Matcher *CheckType = MatcherPtr.release();
Matcher *CheckOpcode = CheckType->takeNext();
Matcher *Tail = CheckOpcode->takeNext();
MatcherPtr.reset(CheckOpcode);
CheckOpcode->setNext(CheckType);
CheckType->setNext(Tail);
return ContractNodes(MatcherPtr, CGP);
}
}
static Matcher *FindNodeWithKind(Matcher *M, Matcher::KindTy Kind) {
for (; M; M = M->getNext())
if (M->getKind() == Kind)
return M;
return nullptr;
}
static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
ScopeMatcher *Scope = nullptr;
std::unique_ptr<Matcher> *RebindableMatcherPtr = &InputMatcherPtr;
while (!Scope) {
Matcher *N = RebindableMatcherPtr->get();
if (!N) return;
Scope = dyn_cast<ScopeMatcher>(N);
if (!Scope)
RebindableMatcherPtr = &(N->getNextPtr());
}
std::unique_ptr<Matcher> &MatcherPtr = *RebindableMatcherPtr;
SmallVector<Matcher*, 32> OptionsToMatch;
for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
std::unique_ptr<Matcher> Child(Scope->takeChild(i));
FactorNodes(Child);
if (Child) {
if (auto *SM = dyn_cast<ScopeMatcher>(Child.get())) {
for (unsigned j = 0, e = SM->getNumChildren(); j != e; ++j)
OptionsToMatch.push_back(SM->takeChild(j));
} else {
OptionsToMatch.push_back(Child.release());
}
}
}
SmallVector<Matcher*, 32> NewOptionsToMatch;
for (unsigned OptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) {
Matcher *Optn = OptionsToMatch[OptionIdx++];
if (OptionIdx == e) {
NewOptionsToMatch.push_back(Optn);
continue;
}
SmallVector<Matcher*, 8> EqualMatchers;
EqualMatchers.push_back(Optn);
while (OptionIdx != e && OptionsToMatch[OptionIdx]->isEqual(Optn))
EqualMatchers.push_back(OptionsToMatch[OptionIdx++]);
unsigned Scan = OptionIdx;
while (true) {
if (Scan == e) break;
Matcher *ScanMatcher = OptionsToMatch[Scan];
if (Optn->isEqual(ScanMatcher)) {
EqualMatchers.push_back(ScanMatcher);
OptionsToMatch.erase(OptionsToMatch.begin()+Scan);
--e;
continue;
}
if (Optn->isContradictory(ScanMatcher)) {
++Scan;
continue;
}
if (Optn->isSimplePredicateOrRecordNode()) {
Matcher *M2 = FindNodeWithKind(ScanMatcher, Optn->getKind());
if (M2 && M2 != ScanMatcher &&
M2->canMoveBefore(ScanMatcher) &&
(M2->isEqual(Optn) || M2->isContradictory(Optn))) {
Matcher *MatcherWithoutM2 = ScanMatcher->unlinkNode(M2);
M2->setNext(MatcherWithoutM2);
OptionsToMatch[Scan] = M2;
continue;
}
}
break;
}
if (Scan != e &&
Scan+1 != e) {
LLVM_DEBUG(errs() << "Couldn't merge this:\n"; Optn->print(errs(), 4);
errs() << "into this:\n";
OptionsToMatch[Scan]->print(errs(), 4);
if (Scan + 1 != e) OptionsToMatch[Scan + 1]->printOne(errs());
if (Scan + 2 < e) OptionsToMatch[Scan + 2]->printOne(errs());
errs() << "\n");
}
if (EqualMatchers.size() == 1) {
NewOptionsToMatch.push_back(EqualMatchers[0]);
continue;
}
Matcher *Shared = Optn;
Optn = Optn->takeNext();
EqualMatchers[0] = Optn;
for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) {
Matcher *Tmp = EqualMatchers[i]->takeNext();
delete EqualMatchers[i];
EqualMatchers[i] = Tmp;
}
Shared->setNext(new ScopeMatcher(EqualMatchers));
FactorNodes(Shared->getNextPtr());
NewOptionsToMatch.push_back(Shared);
}
if (NewOptionsToMatch.size() == 1) {
MatcherPtr.reset(NewOptionsToMatch[0]);
return;
}
if (NewOptionsToMatch.empty()) {
MatcherPtr.reset();
return;
}
bool AllOpcodeChecks = true, AllTypeChecks = true;
for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
if (AllOpcodeChecks &&
!isa<CheckOpcodeMatcher>(NewOptionsToMatch[i])) {
#if 0#endif
AllOpcodeChecks = false;
}
if (AllTypeChecks) {
CheckTypeMatcher *CTM =
cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],
Matcher::CheckType));
if (!CTM ||
CTM->getType() == MVT::iPTR ||
CTM->getResNo() != 0 ||
!CTM->canMoveBefore(NewOptionsToMatch[i])) {
#if 0#endif
AllTypeChecks = false;
}
}
}
if (AllOpcodeChecks) {
StringSet<> Opcodes;
SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
CheckOpcodeMatcher *COM = cast<CheckOpcodeMatcher>(NewOptionsToMatch[i]);
assert(Opcodes.insert(COM->getOpcode().getEnumName()).second &&
"Duplicate opcodes not factored?");
Cases.push_back(std::make_pair(&COM->getOpcode(), COM->takeNext()));
delete COM;
}
MatcherPtr.reset(new SwitchOpcodeMatcher(Cases));
return;
}
if (AllTypeChecks) {
DenseMap<unsigned, unsigned> TypeEntry;
SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
Matcher* M = FindNodeWithKind(NewOptionsToMatch[i], Matcher::CheckType);
assert(M && isa<CheckTypeMatcher>(M) && "Unknown Matcher type");
auto *CTM = cast<CheckTypeMatcher>(M);
Matcher *MatcherWithoutCTM = NewOptionsToMatch[i]->unlinkNode(CTM);
MVT::SimpleValueType CTMTy = CTM->getType();
delete CTM;
unsigned &Entry = TypeEntry[CTMTy];
if (Entry != 0) {
Matcher *PrevMatcher = Cases[Entry-1].second;
if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(PrevMatcher)) {
SM->setNumChildren(SM->getNumChildren()+1);
SM->resetChild(SM->getNumChildren()-1, MatcherWithoutCTM);
continue;
}
Matcher *Entries[2] = { PrevMatcher, MatcherWithoutCTM };
Cases[Entry-1].second = new ScopeMatcher(Entries);
continue;
}
Entry = Cases.size()+1;
Cases.push_back(std::make_pair(CTMTy, MatcherWithoutCTM));
}
for (auto &M : Cases) {
if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(M.second)) {
std::unique_ptr<Matcher> Scope(SM);
FactorNodes(Scope);
M.second = Scope.release();
assert(M.second && "null matcher");
}
}
if (Cases.size() != 1) {
MatcherPtr.reset(new SwitchTypeMatcher(Cases));
} else {
MatcherPtr.reset(new CheckTypeMatcher(Cases[0].first, 0));
MatcherPtr->setNext(Cases[0].second);
}
return;
}
Scope->setNumChildren(NewOptionsToMatch.size());
for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i)
Scope->resetChild(i, NewOptionsToMatch[i]);
}
void
llvm::OptimizeMatcher(std::unique_ptr<Matcher> &MatcherPtr,
const CodeGenDAGPatterns &CGP) {
ContractNodes(MatcherPtr, CGP);
FactorNodes(MatcherPtr);
}