-//===----------------------------------------------------------------------===//
-// Predicate emitter implementation.
-//
-
-void DAGISelEmitter::EmitPredicateFunctions(raw_ostream &OS) {
- OS << "\n// Predicate functions.\n";
-
- // Walk the pattern fragments, adding them to a map, which sorts them by
- // name.
- typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
- PFsByNameTy PFsByName;
-
- for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
- I != E; ++I)
- PFsByName.insert(std::make_pair(I->first->getName(), *I));
-
-
- for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
- I != E; ++I) {
- Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
- TreePattern *P = I->second.second;
-
- // If there is a code init for this fragment, emit the predicate code.
- std::string Code = PatFragRecord->getValueAsCode("Predicate");
- if (Code.empty()) continue;
-
- if (P->getOnlyTree()->isLeaf())
- OS << "inline bool Predicate_" << PatFragRecord->getName()
- << "(SDNode *N) const {\n";
- else {
- std::string ClassName =
- CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
- const char *C2 = ClassName == "SDNode" ? "N" : "inN";
-
- OS << "inline bool Predicate_" << PatFragRecord->getName()
- << "(SDNode *" << C2 << ") const {\n";
- if (ClassName != "SDNode")
- OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
- }
- OS << Code << "\n}\n";
- }
-
- OS << "\n\n";
-}
-
-/// CouldMatchSameInput - Return true if it is possible for these two patterns
-/// to match the same input. For example, (add reg, reg) and
-/// (add reg, (mul ...)) could both match the same input. Where this is
-/// conservative, it falls back to returning true.
-static bool CouldMatchSameInput(const TreePatternNode *N1,
- const TreePatternNode *N2) {
- // If the types of the two nodes differ, they can't match the same thing.
- if (N1->getNumTypes() != N2->getNumTypes()) return false;
- for (unsigned i = 0, e = N1->getNumTypes(); i != e; ++i)
- if (N1->getType(i) != N2->getType(i))
- return false;
-
- // Handle the case when at least one is a leaf.
- if (N1->isLeaf()) {
- if (N2->isLeaf()) {
- // Handle leaf/leaf cases. Register operands can match just about
- // anything, so we can only disambiguate a few things here.
-
- // If both operands are leaf integer nodes with different values, they
- // can't match the same thing.
- if (IntInit *II1 = dynamic_cast<IntInit*>(N1->getLeafValue()))
- if (IntInit *II2 = dynamic_cast<IntInit*>(N2->getLeafValue()))
- return II1->getValue() == II2->getValue();
-
- DefInit *DI1 = dynamic_cast<DefInit*>(N1->getLeafValue());
- DefInit *DI2 = dynamic_cast<DefInit*>(N2->getLeafValue());
- if (DI1 != 0 && DI2 != 0) {
- if (DI1->getDef()->isSubClassOf("ValueType") &&
- DI2->getDef()->isSubClassOf("ValueType"))
- return DI1 == DI2;
- if (DI1->getDef()->isSubClassOf("CondCode") &&
- DI2->getDef()->isSubClassOf("CondCode"))
- return DI1 == DI2;
- }
-
- // TODO: Regclass cannot match a condcode etc.
-
- // Otherwise, complex pattern could match anything, so just return a
- // conservative response.
- return true;
- }
-
- // Conservatively return true. (imm) could match "7" for example, and GPR
- // can match anything.
- // TODO: could handle (add ...) != "1" if we cared.
- return true;
- }
-
- // If N2 is a leaf and N1 isn't, check the other way.
- if (N2->isLeaf())
- return CouldMatchSameInput(N2, N1);
-
- // Now we know neither node is a leaf. If the two patterns have different
- // number of children or different operators, they can't both match.
- Record *Op1 = N1->getOperator(), *Op2 = N1->getOperator();
-
- if (Op1 != Op2 || N1->getNumChildren() != N2->getNumChildren())
- return false;
-
- // If a child prevents the two patterns from matching, use that.
- for (unsigned i = 0, e = N1->getNumChildren(); i != e; ++i)
- if (!CouldMatchSameInput(N1->getChild(i), N2->getChild(i)))
- return false;
-
- // Otherwise, it looks like they could both match the same thing.
- return true;
-}
-
-/// GetSourceMatchPreferenceOrdering - The two input patterns are guaranteed to
-/// not match the same input. Decide which pattern we'd prefer to match first
-/// in order to reduce compile time. This sorting predicate is used to improve
-/// compile time so that we try to match scalar operations before vector
-/// operations since scalar operations are much more common in practice.
-///
-/// This returns -1 if we prefer to match N1 before N2, 1 if we prefer to match
-/// N2 before N1 or 0 if no preference.
-///
-static int GetSourceMatchPreferenceOrdering(const TreePatternNode *N1,
- const TreePatternNode *N2) {
- // The primary thing we sort on here is to get ints before floats and scalars
- // before vectors.
- for (unsigned i = 0, e = std::min(N1->getNumTypes(), N2->getNumTypes());
- i != e; ++i)
- if (N1->getType(i) != N2->getType(i)) {
- MVT::SimpleValueType V1 = N1->getType(i), V2 = N2->getType(i);
- if (MVT(V1).isVector() != MVT(V2).isVector())
- return MVT(V1).isVector() ? 1 : -1;
-
- if (MVT(V1).isFloatingPoint() != MVT(V2).isFloatingPoint())
- return MVT(V1).isFloatingPoint() ? 1 : -1;
- }
-
- for (unsigned i = 0, e = std::min(N1->getNumChildren(), N2->getNumChildren());
- i != e; ++i)
- if (int Res = GetSourceMatchPreferenceOrdering(N1->getChild(i),
- N2->getChild(i)))
- return Res;
- return 0;
-}
-
-