souffle  2.0.2-371-g6315b36
ValueIndex.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2015, 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 ValueIndex.h
12  *
13  * Defines the ValueIndex class, which indexes the location of variables
14  * and record references within a loop nest during rule conversion.
15  *
16  ***********************************************************************/
17 
18 #include "ast2ram/ValueIndex.h"
19 #include "ast/Aggregator.h"
20 #include "ast/Variable.h"
21 #include "ast2ram/Location.h"
22 #include "ram/Relation.h"
25 #include <cassert>
26 #include <set>
27 
28 namespace souffle::ast2ram {
29 
30 ValueIndex::ValueIndex() = default;
31 ValueIndex::~ValueIndex() = default;
32 
33 void ValueIndex::addVarReference(const ast::Variable& var, const Location& l) {
34  std::set<Location>& locs = var_references[var.getName()];
35  locs.insert(l);
36 }
37 
38 void ValueIndex::addVarReference(const ast::Variable& var, int ident, int pos, std::string rel) {
39  addVarReference(var, Location({ident, pos, rel}));
40 }
41 
42 bool ValueIndex::isDefined(const ast::Variable& var) const {
43  return var_references.find(var.getName()) != var_references.end();
44 }
45 
46 const Location& ValueIndex::getDefinitionPoint(const ast::Variable& var) const {
47  auto pos = var_references.find(var.getName());
48  assert(pos != var_references.end() && "Undefined variable referenced!");
49  return *pos->second.begin();
50 }
51 
52 void ValueIndex::setGeneratorLoc(const ast::Argument& arg, const Location& loc) {
53  arg_generator_locations.push_back(std::make_pair(&arg, loc));
54 }
55 
56 const Location& ValueIndex::getGeneratorLoc(const ast::Argument& arg) const {
57  if (dynamic_cast<const ast::Aggregator*>(&arg) != nullptr) {
58  // aggregators can be used interchangeably if syntactically equal
59  for (const auto& cur : arg_generator_locations) {
60  if (*cur.first == arg) {
61  return cur.second;
62  }
63  }
64  } else {
65  // otherwise, unique for each appearance
66  for (const auto& cur : arg_generator_locations) {
67  if (cur.first == &arg) {
68  return cur.second;
69  }
70  }
71  }
72  fatal("arg `%s` has no generator location", arg);
73 }
74 
75 void ValueIndex::setRecordDefinition(const ast::RecordInit& init, const Location& l) {
76  record_definitions.insert({&init, l});
77 }
78 
79 void ValueIndex::setRecordDefinition(const ast::RecordInit& init, int ident, int pos, std::string rel) {
80  setRecordDefinition(init, Location({ident, pos, rel}));
81 }
82 
84  auto pos = record_definitions.find(&init);
85  if (pos != record_definitions.end()) {
86  return pos->second;
87  }
88 
89  fatal("requested location for undefined record!");
90 }
91 
92 bool ValueIndex::isGenerator(const int level) const {
93  // check for aggregator definitions
95  [&level](const auto& location) { return location.second.identifier == level; });
96 }
97 
98 bool ValueIndex::isSomethingDefinedOn(int level) const {
99  // check for variable definitions
100  for (const auto& cur : var_references) {
101  if (cur.second.begin()->identifier == level) {
102  return true;
103  }
104  }
105  // check for record definitions
106  for (const auto& cur : record_definitions) {
107  if (cur.second.identifier == level) {
108  return true;
109  }
110  }
111  // nothing defined on this level
112  return false;
113 }
114 
115 void ValueIndex::print(std::ostream& out) const {
116  out << "Variables:\n\t";
117  out << join(var_references, "\n\t");
118 }
119 
120 } // namespace souffle::ast2ram
souffle::ast2ram::ValueIndex::getDefinitionPoint
const Location & getDefinitionPoint(const ast::Variable &var) const
Definition: ValueIndex.cpp:53
souffle::ast2ram::ValueIndex::addVarReference
void addVarReference(const ast::Variable &var, const Location &l)
Definition: ValueIndex.cpp:40
souffle::ast2ram::ValueIndex::arg_generator_locations
generator_location_map arg_generator_locations
The level of a nested ram operation that is handling a generator operation.
Definition: ValueIndex.h:106
souffle::ast2ram::ValueIndex::setGeneratorLoc
void setGeneratorLoc(const ast::Argument &arg, const Location &loc)
Definition: ValueIndex.cpp:59
souffle::ast2ram::Location
Definition: Location.h:29
souffle::ast::analysis::Variable
A variable to be utilized within constraints to be handled by the constraint solver.
Definition: ConstraintSystem.h:41
souffle::ast::Argument
An abstract class for arguments.
Definition: Argument.h:33
l
var l
Definition: htmlJsChartistMin.h:15
Relation.h
souffle::join
detail::joined_sequence< Iter, Printer > join(const Iter &a, const Iter &b, const std::string &sep, const Printer &p)
Creates an object to be forwarded to some output stream for printing sequences of elements interspers...
Definition: StreamUtil.h:175
souffle::ast2ram::ValueIndex::setRecordDefinition
void setRecordDefinition(const ast::RecordInit &init, const Location &l)
Definition: ValueIndex.cpp:82
souffle::ast2ram::ValueIndex::isDefined
bool isDefined(const ast::Variable &var) const
Definition: ValueIndex.cpp:49
souffle::any_of
bool any_of(const Container &c, UnaryPredicate p)
A generic test checking whether any elements within a container satisfy a certain predicate.
Definition: FunctionalUtil.h:124
souffle::ast2ram::ValueIndex::getGeneratorLoc
const Location & getGeneratorLoc(const ast::Argument &arg) const
Definition: ValueIndex.cpp:63
Location.h
souffle::ast2ram::ValueIndex::isSomethingDefinedOn
bool isSomethingDefinedOn(int level) const
Definition: ValueIndex.cpp:105
Aggregator.h
souffle::ast2ram::ValueIndex::ValueIndex
ValueIndex()
souffle::ast::Aggregator
Defines the aggregator class.
Definition: Aggregator.h:53
ValueIndex.h
souffle::ast2ram::ValueIndex::isGenerator
bool isGenerator(const int level) const
Definition: ValueIndex.cpp:99
souffle::ast2ram::ValueIndex::~ValueIndex
~ValueIndex()
StreamUtil.h
souffle::fatal
void fatal(const char *format, const Args &... args)
Definition: MiscUtil.h:198
souffle::ast2ram
Definition: AstToRamTranslator.cpp:132
FunctionalUtil.h
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::ast2ram::ValueIndex::record_definitions
record_definition_map record_definitions
The index of record definition points.
Definition: ValueIndex.h:103
souffle::ast::RecordInit
Defines a record initialization class.
Definition: RecordInit.h:42
souffle::ast2ram::ValueIndex::var_references
variable_reference_map var_references
The index of variable accesses.
Definition: ValueIndex.h:100
Variable.h
souffle::ast2ram::ValueIndex::print
void print(std::ostream &out) const
Definition: ValueIndex.cpp:122