souffle  2.0.2-371-g6315b36
CollapseFilters.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2018, 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 CollapseFilters.cpp
12  *
13  ***********************************************************************/
14 
16 #include "ram/Condition.h"
17 #include "ram/Node.h"
18 #include "ram/Operation.h"
19 #include "ram/Program.h"
20 #include "ram/Statement.h"
21 #include "ram/utility/Utils.h"
22 #include "ram/utility/Visitor.h"
24 #include <algorithm>
25 #include <functional>
26 #include <memory>
27 #include <vector>
28 
29 namespace souffle::ram::transform {
30 
31 bool CollapseFiltersTransformer::collapseFilters(Program& program) {
32  bool changed = false;
33  visitDepthFirst(program, [&](const Query& query) {
34  std::function<Own<Node>(Own<Node>)> filterRewriter = [&](Own<Node> node) -> Own<Node> {
35  if (const Filter* filter = dynamic_cast<Filter*>(node.get())) {
36  // true if two consecutive filters in loop nest found
37  bool canCollapse = false;
38 
39  // storing conditions for collapsing
40  VecOwn<Condition> conditions;
41 
42  const Filter* prevFilter = filter;
43  conditions.emplace_back(filter->getCondition().clone());
44  while (auto* nextFilter = dynamic_cast<Filter*>(&prevFilter->getOperation())) {
45  canCollapse = true;
46  conditions.emplace_back(nextFilter->getCondition().clone());
47  prevFilter = nextFilter;
48  }
49 
50  if (canCollapse) {
51  changed = true;
52  node = mk<Filter>(toCondition(conditions), souffle::clone(&prevFilter->getOperation()),
53  prevFilter->getProfileText());
54  }
55  }
56  node->apply(makeLambdaRamMapper(filterRewriter));
57  return node;
58  };
59  const_cast<Query*>(&query)->apply(makeLambdaRamMapper(filterRewriter));
60  });
61  return changed;
62 }
63 
64 } // namespace souffle::ram::transform
souffle::ram::transform
Definition: ChoiceConversion.cpp:30
souffle::ram::Query
A relational algebra query.
Definition: Query.h:50
souffle::ram::toCondition
Own< Condition > toCondition(const VecOwn< Condition > &conds)
Convert list of conditions to a conjunction.
Definition: Utils.h:84
souffle::ram::transform::CollapseFiltersTransformer::collapseFilters
bool collapseFilters(Program &program)
Collapse consecutive filter operations.
Definition: CollapseFilters.cpp:35
souffle::ram::transform::Transformer::apply
bool apply(TranslationUnit &translationUnit)
@Brief apply the transformer to a translation unit @Param translationUnit that will be transformed.
Definition: Transformer.cpp:39
souffle::ram::NestedOperation::getOperation
Operation & getOperation() const
Get nested operation.
Definition: NestedOperation.h:62
MiscUtil.h
Program.h
souffle::ram::Filter
Checks whether a given condition holds.
Definition: Filter.h:49
Visitor.h
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
souffle::filter
std::vector< A > filter(std::vector< A > xs, F &&f)
Filter a vector to include certain elements.
Definition: FunctionalUtil.h:155
souffle::ram::NestedOperation::getProfileText
const std::string & getProfileText() const
Get profile text.
Definition: NestedOperation.h:67
Condition.h
Utils.h
CollapseFilters.h
Node.h
Operation.h
Statement.h
souffle::ram::visitDepthFirst
void visitDepthFirst(const Node &root, Visitor< R, Ps... > &visitor, Args &... args)
A utility function visiting all nodes within the RAM fragments rooted by the given node recursively i...
Definition: Visitor.h:357
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
souffle::ram::makeLambdaRamMapper
LambdaNodeMapper< Lambda > makeLambdaRamMapper(const Lambda &lambda)
Creates a node mapper based on a corresponding lambda expression.
Definition: LambdaNodeMapper.h:67