souffle  2.0.2-371-g6315b36
Level.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2019, The Souffle Developers. 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 Level.cpp
12  *
13  * Implementation of RAM Level Analysis
14  *
15  ***********************************************************************/
16 
17 #include "ram/analysis/Level.h"
18 #include "ram/Aggregate.h"
19 #include "ram/AutoIncrement.h"
20 #include "ram/Break.h"
21 #include "ram/Choice.h"
22 #include "ram/Condition.h"
23 #include "ram/Conjunction.h"
24 #include "ram/Constant.h"
25 #include "ram/Constraint.h"
26 #include "ram/EmptinessCheck.h"
27 #include "ram/ExistenceCheck.h"
28 #include "ram/Expression.h"
29 #include "ram/False.h"
30 #include "ram/Filter.h"
31 #include "ram/IndexAggregate.h"
32 #include "ram/IndexChoice.h"
33 #include "ram/IndexScan.h"
34 #include "ram/IntrinsicOperator.h"
35 #include "ram/Negation.h"
36 #include "ram/Node.h"
37 #include "ram/Operation.h"
38 #include "ram/PackRecord.h"
39 #include "ram/Project.h"
41 #include "ram/Scan.h"
42 #include "ram/SubroutineArgument.h"
43 #include "ram/SubroutineReturn.h"
44 #include "ram/True.h"
45 #include "ram/TupleElement.h"
46 #include "ram/UndefValue.h"
47 #include "ram/UnpackRecord.h"
49 #include "ram/utility/Visitor.h"
51 #include <algorithm>
52 #include <cassert>
53 #include <utility>
54 #include <vector>
55 
56 namespace souffle::ram::analysis {
57 
58 int LevelAnalysis::getLevel(const Node* node) const {
59  // visitor
60  class ValueLevelVisitor : public Visitor<int> {
61  public:
62  // number
63  int visitConstant(const Constant&) override {
64  return -1;
65  }
66 
67  // true
68  int visitTrue(const True&) override {
69  return -1;
70  }
71 
72  // false
73  int visitFalse(const False&) override {
74  return -1;
75  }
76 
77  // tuple element access
78  int visitTupleElement(const TupleElement& elem) override {
79  return elem.getTupleId();
80  }
81 
82  // scan
83  int visitScan(const Scan&) override {
84  return -1;
85  }
86 
87  // index scan
88  int visitIndexScan(const IndexScan& indexScan) override {
89  int level = -1;
90  for (auto& index : indexScan.getRangePattern().first) {
91  level = std::max(level, visit(index));
92  }
93  for (auto& index : indexScan.getRangePattern().second) {
94  level = std::max(level, visit(index));
95  }
96  return level;
97  }
98 
99  // choice
100  int visitChoice(const Choice& choice) override {
101  return std::max(-1, visit(choice.getCondition()));
102  }
103 
104  // index choice
105  int visitIndexChoice(const IndexChoice& indexChoice) override {
106  int level = -1;
107  for (auto& index : indexChoice.getRangePattern().first) {
108  level = std::max(level, visit(index));
109  }
110  for (auto& index : indexChoice.getRangePattern().second) {
111  level = std::max(level, visit(index));
112  }
113  return std::max(level, visit(indexChoice.getCondition()));
114  }
115 
116  // aggregate
117  int visitAggregate(const Aggregate& aggregate) override {
118  return std::max(visit(aggregate.getExpression()), visit(aggregate.getCondition()));
119  }
120 
121  // index aggregate
122  int visitIndexAggregate(const IndexAggregate& indexAggregate) override {
123  int level = -1;
124  for (auto& index : indexAggregate.getRangePattern().first) {
125  level = std::max(level, visit(index));
126  }
127  for (auto& index : indexAggregate.getRangePattern().second) {
128  level = std::max(level, visit(index));
129  }
130  level = std::max(visit(indexAggregate.getExpression()), level);
131  return std::max(level, visit(indexAggregate.getCondition()));
132  }
133 
134  // unpack record
135  int visitUnpackRecord(const UnpackRecord& unpack) override {
136  return visit(unpack.getExpression());
137  }
138 
139  // filter
140  int visitFilter(const Filter& filter) override {
141  return visit(filter.getCondition());
142  }
143 
144  // break
145  int visitBreak(const Break& b) override {
146  return visit(b.getCondition());
147  }
148 
149  // project
150  int visitProject(const Project& project) override {
151  int level = -1;
152  for (auto& exp : project.getValues()) {
153  level = std::max(level, visit(exp));
154  }
155  return level;
156  }
157 
158  // return
159  int visitSubroutineReturn(const SubroutineReturn& ret) override {
160  int level = -1;
161  for (auto& exp : ret.getValues()) {
162  level = std::max(level, visit(exp));
163  }
164  return level;
165  }
166 
167  // auto increment
168  int visitAutoIncrement(const AutoIncrement&) override {
169  return -1;
170  }
171 
172  // undef value
173  int visitUndefValue(const UndefValue&) override {
174  return -1;
175  }
176 
177  // intrinsic functors
178  int visitIntrinsicOperator(const IntrinsicOperator& op) override {
179  int level = -1;
180  for (const auto& arg : op.getArguments()) {
181  level = std::max(level, visit(arg));
182  }
183  return level;
184  }
185 
186  // pack operator
187  int visitPackRecord(const PackRecord& pack) override {
188  int level = -1;
189  for (const auto& arg : pack.getArguments()) {
190  level = std::max(level, visit(arg));
191  }
192  return level;
193  }
194 
195  // argument
196  int visitSubroutineArgument(const SubroutineArgument&) override {
197  return -1;
198  }
199 
200  // user defined operator
201  int visitUserDefinedOperator(const UserDefinedOperator& op) override {
202  int level = -1;
203  for (const auto& arg : op.getArguments()) {
204  level = std::max(level, visit(arg));
205  }
206  return level;
207  }
208 
209  // conjunction
210  int visitConjunction(const Conjunction& conj) override {
211  return std::max(visit(conj.getLHS()), visit(conj.getRHS()));
212  }
213 
214  // negation
215  int visitNegation(const Negation& neg) override {
216  return visit(neg.getOperand());
217  }
218 
219  // constraint
220  int visitConstraint(const Constraint& binRel) override {
221  return std::max(visit(binRel.getLHS()), visit(binRel.getRHS()));
222  }
223 
224  // existence check
225  int visitExistenceCheck(const ExistenceCheck& exists) override {
226  int level = -1;
227  for (const auto& cur : exists.getValues()) {
228  level = std::max(level, visit(cur));
229  }
230  return level;
231  }
232 
233  // provenance existence check
234  int visitProvenanceExistenceCheck(const ProvenanceExistenceCheck& provExists) override {
235  int level = -1;
236  for (const auto& cur : provExists.getValues()) {
237  level = std::max(level, visit(cur));
238  }
239  return level;
240  }
241 
242  // emptiness check
243  int visitEmptinessCheck(const EmptinessCheck&) override {
244  return -1; // can be in the top level
245  }
246 
247  // default rule
248  int visitNode(const Node&) override {
249  fatal("Node not implemented!");
250  }
251  };
252 
253  assert((isA<Expression>(node) || isA<Condition>(node) || isA<Operation>(node)) &&
254  "not an expression/condition/operation");
255  return ValueLevelVisitor().visit(node);
256 }
257 
258 } // namespace souffle::ram::analysis
Aggregate.h
souffle::ram::analysis::LevelAnalysis::getLevel
int getLevel(const Node *value) const
Get level of a RAM expression/condition.
Definition: Level.cpp:64
UserDefinedOperator.h
Constant.h
PackRecord.h
Constraint.h
Scan.h
Negation.h
Project.h
False.h
souffle::ram::analysis
Definition: Analysis.h:32
MiscUtil.h
Filter.h
SubroutineArgument.h
EmptinessCheck.h
True.h
souffle::ram::False
Definition: False.h:38
IndexAggregate.h
ProvenanceExistenceCheck.h
IndexScan.h
Visitor.h
IndexChoice.h
souffle::filter
std::vector< A > filter(std::vector< A > xs, F &&f)
Filter a vector to include certain elements.
Definition: FunctionalUtil.h:155
Choice.h
Condition.h
ExistenceCheck.h
UnpackRecord.h
AutoIncrement.h
souffle::pack
RamDomain pack(RecordTable &recordTab, Tuple< RamDomain, Arity > const &tuple)
helper to convert tuple to record reference for the synthesiser
Definition: RecordTable.h:153
Break.h
TupleElement.h
Node.h
b
l j a showGridBackground &&c b raw series this eventEmitter b
Definition: htmlJsChartistMin.h:15
Operation.h
souffle::fatal
void fatal(const char *format, const Args &... args)
Definition: MiscUtil.h:198
SubroutineReturn.h
Expression.h
Level.h
UndefValue.h
souffle::ram::True
False value condition.
Definition: True.h:38
IntrinsicOperator.h
Conjunction.h