souffle  2.0.2-371-g6315b36
IfConversion.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 IfConversion.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
17 #include "ram/IndexScan.h"
18 #include "ram/Operation.h"
19 #include "ram/Program.h"
20 #include "ram/TranslationUnit.h"
22 #include <memory>
23 #include <string>
24 
25 namespace souffle::ram::transform {
26 
27 /**
28  * @class IfConversionTransformer
29  * @brief Convert IndexScan operations to Filter/Existence Checks
30 
31  * If there exists IndexScan operations in the RAM, and their tuples
32  * are not further used in subsequent operations, the IndexScan operations
33  * will be rewritten to Filter/Existence Checks.
34  *
35  * For example,
36  *
37  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
38  * QUERY
39  * ...
40  * FOR t1 IN X ON INDEX t1.x = 10 AND t1.y = 20
41  * ... // no occurrence of t1
42  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
43  *
44  * will be rewritten to
45  *
46  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
47  * QUERY
48  * ...
49  * IF (10,20) NOT IN A
50  * ...
51  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
52  *
53  */
54 class IfConversionTransformer : public Transformer {
55 public:
56  std::string getName() const override {
57  return "IfConversionTransformer";
58  }
59 
60  /**
61  * @brief Rewrite IndexScan operations
62  * @param indexScan An index operation
63  * @result The old operation if the if-conversion fails; otherwise the filter/existence check
64  *
65  * Rewrites IndexScan operations to a filter/existence check if the IndexScan's tuple
66  * is not used in a consecutive RAM operation
67  */
68  Own<Operation> rewriteIndexScan(const IndexScan* indexScan);
69 
70  /**
71  * @brief Apply if-conversion to the whole program
72  * @param RAM program
73  * @result A flag indicating whether the RAM program has been changed.
74  *
75  * Search for queries and rewrite their IndexScan operations if possible.
76  */
77  bool convertIndexScans(Program& program);
78 
79 protected:
80  bool transform(TranslationUnit& translationUnit) override {
81  return convertIndexScans(translationUnit.getProgram());
82  }
83 };
84 
85 } // namespace souffle::ram::transform
souffle::ram::transform
Definition: ChoiceConversion.cpp:30
souffle::ram::Program
RAM program relation declaration and functions.
Definition: Program.h:58
souffle::ram::transform::IfConversionTransformer::convertIndexScans
bool convertIndexScans(Program &program)
Apply if-conversion to the whole program.
Definition: IfConversion.cpp:80
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
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
IndexScan.h
souffle::ram::transform::IfConversionTransformer::getName
std::string getName() const override
@Brief get name of the transformer
Definition: IfConversion.h:60
TranslationUnit.h
Transformer.h
souffle::ram::IndexScan
Search for tuples of a relation matching a criteria.
Definition: IndexScan.h:52
souffle::ram::transform::IfConversionTransformer::transform
bool transform(TranslationUnit &translationUnit) override
@Brief transform the translation unit / used by apply @Param translationUnit that will be transformed...
Definition: IfConversion.h:84
Operation.h
souffle::ram::transform::IfConversionTransformer::rewriteIndexScan
Own< Operation > rewriteIndexScan(const IndexScan *indexScan)
Rewrite IndexScan operations.
Definition: IfConversion.cpp:37