souffle  2.0.2-371-g6315b36
RedundantRelations.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file RedundantRelations.cpp
12  *
13  * Implements method of precedence graph to build the precedence graph,
14  * compute strongly connected components of the precedence graph, and
15  * build the strongly connected component graph.
16  *
17  ***********************************************************************/
18 
20 #include "GraphUtils.h"
21 #include "ast/Node.h"
22 #include "ast/Program.h"
23 #include "ast/Relation.h"
24 #include "ast/TranslationUnit.h"
25 #include "ast/analysis/IOType.h"
28 #include <set>
29 #include <vector>
30 
31 namespace souffle::ast::analysis {
32 
33 void RedundantRelationsAnalysis::run(const TranslationUnit& translationUnit) {
34  precedenceGraph = translationUnit.getAnalysis<PrecedenceGraphAnalysis>();
35 
36  std::set<const Relation*> work;
37  std::set<const Relation*> notRedundant;
38  auto* ioType = translationUnit.getAnalysis<IOTypeAnalysis>();
39  Program& program = translationUnit.getProgram();
40 
41  const std::vector<Relation*>& relations = program.getRelations();
42  /* Add all output relations to the work set */
43  for (const Relation* r : relations) {
44  if (ioType->isOutput(r)) {
45  work.insert(r);
46  }
47  }
48 
49  /* Find all relations which are not redundant for the computations of the
50  output relations. */
51  while (!work.empty()) {
52  /* Chose one element in the work set and add it to notRedundant */
53  const Relation* u = *(work.begin());
54  work.erase(work.begin());
55  notRedundant.insert(u);
56 
57  /* Find all predecessors of u and add them to the worklist
58  if they are not in the set notRedundant */
59  for (const Relation* predecessor : precedenceGraph->graph().predecessors(u)) {
60  if (notRedundant.count(predecessor) == 0u) {
61  work.insert(predecessor);
62  }
63  }
64  }
65 
66  /* All remaining relations are redundant. */
67  redundantRelations.clear();
68  for (const Relation* r : relations) {
69  if (notRedundant.count(r) == 0u) {
70  redundantRelations.insert(r);
71  }
72  }
73 }
74 
75 void RedundantRelationsAnalysis::print(std::ostream& os) const {
76  os << redundantRelations << std::endl;
77 }
78 
79 } // namespace souffle::ast::analysis
TranslationUnit.h
IOType.h
souffle::ast::analysis::RedundantRelationsAnalysis::precedenceGraph
PrecedenceGraphAnalysis * precedenceGraph
Definition: RedundantRelations.h:59
souffle::ast::analysis::RedundantRelationsAnalysis::run
void run(const TranslationUnit &translationUnit) override
run analysis for a Ast translation unit
Definition: RedundantRelations.cpp:41
souffle::ast::Relation
Defines a relation with a name, attributes, qualifiers, and internal representation.
Definition: Relation.h:49
souffle::ast::analysis::PrecedenceGraphAnalysis::graph
const Graph< const Relation *, NameComparison > & graph() const
Definition: PrecedenceGraph.h:54
souffle::ast::analysis::RedundantRelationsAnalysis::redundantRelations
std::set< const Relation * > redundantRelations
Definition: RedundantRelations.h:61
relations
std::vector< Own< Relation > > relations
Definition: ComponentInstantiation.cpp:65
Relation.h
RedundantRelations.h
GraphUtils.h
Node.h
Program.h
StreamUtil.h
souffle::ast::analysis
Definition: Aggregate.cpp:39
PrecedenceGraph.h
souffle::ast::analysis::RedundantRelationsAnalysis::print
void print(std::ostream &os) const override
print the analysis result in HTML format
Definition: RedundantRelations.cpp:83