llvm.org GIT mirror llvm / bb8b58d
Add weights to graph edges. Choose between edges based on their weight. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50757 91177308-0d34-0410-b5e6-96231b3b80d8 Mikhail Glushenkov 11 years ago
3 changed file(s) with 35 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
3232
3333 namespace {
3434
35 // Go through the list C and find the edge that isEnabled(); if
36 // there is no such edge, return the default edge; if there is no
37 // default edge, throw an exception.
35 // Return the edge with the maximum weight.
3836 template
3937 const Edge* ChooseEdge(const C& EdgesContainer,
4038 const std::string& NodeName = "root") {
41 const Edge* DefaultEdge = 0;
39 const Edge* MaxEdge = 0;
40 unsigned MaxWeight = 0;
41 bool SingleMax = true;
4242
4343 for (typename C::const_iterator B = EdgesContainer.begin(),
4444 E = EdgesContainer.end(); B != E; ++B) {
4545 const Edge* E = B->getPtr();
46
47 if (E->isDefault())
48 if (!DefaultEdge)
49 DefaultEdge = E;
50 else
51 throw std::runtime_error("Node " + NodeName
52 + ": multiple default outward edges found!"
53 " Most probably a specification error.");
54 if (E->isEnabled())
55 return E;
56 }
57
58 if (DefaultEdge)
59 return DefaultEdge;
60 else
61 throw std::runtime_error("Node " + NodeName
62 + ": no default outward edge found!"
46 unsigned EW = E->Weight();
47 if (EW > MaxWeight) {
48 MaxEdge = E;
49 MaxWeight = EW;
50 SingleMax = true;
51 }
52 else if (EW == MaxWeight) {
53 SingleMax = false;
54 }
55 }
56
57 if (!SingleMax)
58 throw std::runtime_error("Node " + NodeName +
59 ": multiple maximal outward edges found!"
6360 " Most probably a specification error.");
61 if (!MaxEdge)
62 throw std::runtime_error("Node " + NodeName +
63 ": no maximal outward edge found!"
64 " Most probably a specification error.");
65 return MaxEdge;
6466 }
6567
6668 }
3434 virtual ~Edge() {};
3535
3636 const std::string& ToolName() const { return ToolName_; }
37 virtual bool isEnabled() const = 0;
38 virtual bool isDefault() const = 0;
37 virtual unsigned Weight() const = 0;
3938 private:
4039 std::string ToolName_;
4140 };
4443 class SimpleEdge : public Edge {
4544 public:
4645 SimpleEdge(const std::string& T) : Edge(T) {}
47 bool isEnabled() const { return false;}
48 bool isDefault() const { return true;}
46 unsigned Weight() const { return 1; }
4947 };
5048
5149 // A node of the compilation graph.
973973 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
974974 << "\") {}\n\n"
975975
976 // Function isEnabled().
977 << Indent1 << "bool isEnabled() const {\n"
978 << Indent2 << "bool ret = false;\n";
976 // Function Weight().
977 << Indent1 << "unsigned Weight() const {\n"
978 << Indent2 << "unsigned ret = 0;\n";
979979
980980 for (size_t i = 0, PropsSize = Props->size(); i < PropsSize; ++i) {
981981 const DagInit& Prop = dynamic_cast(*Props->getElement(i));
984984 if (PropName == "default")
985985 IsDefault = true;
986986
987 O << Indent2 << "if (ret || (";
987 O << Indent2 << "if ((";
988988 if (PropName == "and") {
989989 O << '(';
990990 for (unsigned j = 0, NumArgs = Prop.getNumArgs(); j < NumArgs; ++j) {
10011001 else {
10021002 EmitEdgePropertyTest(PropName, Prop, OptDescs, O);
10031003 }
1004 O << "))\n" << Indent3 << "ret = true;\n";
1005 }
1004 O << "))\n" << Indent3 << "ret += 2;\n";
1005 }
1006
1007 if (IsDefault)
1008 O << Indent2 << "ret += 1;\n";
10061009
10071010 O << Indent2 << "return ret;\n"
1008 << Indent1 << "};\n\n"
1009
1010 // Function isDefault().
1011 << Indent1 << "bool isDefault() const { return ";
1012 if (IsDefault)
1013 O << "true";
1014 else
1015 O << "false";
1016 O <<"; }\n};\n\n";
1011 << Indent1 << "};\n\n};\n\n";
10171012 }
10181013
10191014 // Emit Edge* classes that represent graph edges.