souffle  2.0.2-371-g6315b36
HoistConditions.h
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 HoistConditions.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
17 #include "ram/Program.h"
18 #include "ram/TranslationUnit.h"
19 #include "ram/analysis/Level.h"
21 #include <string>
22 
23 namespace souffle::ram::transform {
24 
25 /**
26  * @class HoistConditionsTransformer
27  * @brief Hosts conditions in a loop-nest to the most-outer/semantically-correct loop
28  *
29  * Hoists the conditions to the earliest point in the loop nest where their
30  * evaluation is still semantically correct.
31  *
32  * The transformations assumes that filter operations are stored verbose,
33  * i.e. a conjunction is expressed by two consecutive filter operations.
34  * For example ..
35  *
36  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
37  * QUERY
38  * ...
39  * IF C1 /\ C2 then
40  * ...
41  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
42  *
43  * should be rewritten / or produced by the translator as
44  *
45  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
46  * QUERY
47  * ...
48  * IF C1
49  * IF C2
50  * ...
51  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
52  *
53  * otherwise the levelling becomes imprecise, i.e., for both conditions
54  * the most outer-level is sought rather than considered separately.
55  *
56  * If there are transformers prior to hoistConditions() that introduce
57  * conjunction, another transformer is required that splits the
58  * filter operations. However, at the moment this is not necessary
59  * because the translator delivers already the right RAM format.
60  *
61  * TODO: break-up conditions while transforming so that this requirement
62  * is removed.
63  */
64 class HoistConditionsTransformer : public Transformer {
65 public:
66  std::string getName() const override {
67  return "HoistConditionsTransformer";
68  }
69 
70  /**
71  * @brief Hoist filter operations.
72  * @param program that is transformed
73  * @return Flag showing whether the program has been changed by the transformation
74  *
75  * There are two types of conditions in
76  * filter operations. The first type depends on tuples of
77  * TupleOperation operations. The second type are independent of
78  * tuple access. Both types of conditions will be hoisted to
79  * the most out-scope such that the program is still valid.
80  */
81  bool hoistConditions(Program& program);
82 
83 protected:
84  analysis::LevelAnalysis* rla{nullptr};
85 
86  bool transform(TranslationUnit& translationUnit) override {
87  rla = translationUnit.getAnalysis<analysis::LevelAnalysis>();
88  return hoistConditions(translationUnit.getProgram());
89  }
90 };
91 
92 } // namespace souffle::ram::transform
souffle::ram::transform
Definition: ChoiceConversion.cpp:30
souffle::ram::transform::HoistConditionsTransformer::rla
analysis::LevelAnalysis * rla
Definition: HoistConditions.h:88
souffle::ram::Program
RAM program relation declaration and functions.
Definition: Program.h:58
souffle::ram::TranslationUnit::getAnalysis
Analysis * getAnalysis() const
Get an analysis.
Definition: TranslationUnit.h:66
souffle::ram::analysis::LevelAnalysis
A Ram Analysis for determining the level of a expression/condition.
Definition: Level.h:51
souffle::ram::transform::HoistConditionsTransformer::transform
bool transform(TranslationUnit &translationUnit) override
@Brief transform the translation unit / used by apply @Param translationUnit that will be transformed...
Definition: HoistConditions.h:90
Program.h
souffle::ram::TranslationUnit::getProgram
Program & getProgram() const
Get the RAM Program of the translation unit
Definition: TranslationUnit.h:107
souffle::ram::TranslationUnit
Translating a RAM program.
Definition: TranslationUnit.h:55
TranslationUnit.h
Transformer.h
souffle::ram::transform::HoistConditionsTransformer::hoistConditions
bool hoistConditions(Program &program)
Hoist filter operations.
Definition: HoistConditions.cpp:34
Level.h
souffle::ram::transform::HoistConditionsTransformer::getName
std::string getName() const override
@Brief get name of the transformer
Definition: HoistConditions.h:70