souffle  2.0.2-371-g6315b36
Public Member Functions | Private Member Functions
souffle::ast::transform::RemoveRedundantSumsTransformer Class Reference

Transformation pass to remove expressions of the form sum k : { ... More...

#include <RemoveRedundantSums.h>

Inheritance diagram for souffle::ast::transform::RemoveRedundantSumsTransformer:
Inheritance graph
Collaboration diagram for souffle::ast::transform::RemoveRedundantSumsTransformer:
Collaboration graph

Public Member Functions

RemoveRedundantSumsTransformerclone () const override
 
std::string getName () const override
 
- Public Member Functions inherited from souffle::ast::transform::Transformer
bool apply (TranslationUnit &translationUnit)
 
virtual ~Transformer ()=default
 

Private Member Functions

bool transform (TranslationUnit &translationUnit) override
 

Detailed Description

Transformation pass to remove expressions of the form sum k : { ...

} and replace them with k * count : { ... } where k is a constant.

Definition at line 33 of file RemoveRedundantSums.h.

Member Function Documentation

◆ clone()

RemoveRedundantSumsTransformer* souffle::ast::transform::RemoveRedundantSumsTransformer::clone ( ) const
inlineoverridevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 43 of file RemoveRedundantSums.h.

◆ getName()

std::string souffle::ast::transform::RemoveRedundantSumsTransformer::getName ( ) const
inlineoverridevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 39 of file RemoveRedundantSums.h.

39  :
40  bool transform(TranslationUnit& translationUnit) override;
41 };

◆ transform()

bool souffle::ast::transform::RemoveRedundantSumsTransformer::transform ( TranslationUnit translationUnit)
overrideprivatevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 38 of file RemoveRedundantSums.cpp.

38  {
39  // Apply to all aggregates of the form
40  // sum k : { .. } where k is a constant
41  if (auto* agg = dynamic_cast<Aggregator*>(node.get())) {
42  if (agg->getBaseOperator() == AggregateOp::SUM) {
43  if (const auto* constant =
44  dynamic_cast<const NumericConstant*>(agg->getTargetExpression())) {
45  changed = true;
46  // Then construct the new thing to replace it with
47  auto count = mk<Aggregator>(AggregateOp::COUNT);
48  // Duplicate the body of the aggregate
49  VecOwn<Literal> newBody;
50  for (const auto& lit : agg->getBodyLiterals()) {
51  newBody.push_back(souffle::clone(lit));
52  }
53  count->setBody(std::move(newBody));
54  auto number = souffle::clone(constant);
55  // Now it's constant * count : { ... }
56  auto result = mk<IntrinsicFunctor>("*", std::move(number), std::move(count));
57 
58  return result;
59  }
60  }
61  }
62  node->apply(*this);
63  return node;
64  }
65 
66  // variables
67  mutable bool changed = false;
68  };
69 
70  ReplaceSumWithCount update;
71  Program& program = translationUnit.getProgram();
72  program.apply(update);
73  return update.changed;
74 }
75 
76 } // namespace souffle::ast::transform

References souffle::clone(), souffle::test::count(), souffle::COUNT, and souffle::SUM.

Here is the call graph for this function:

The documentation for this class was generated from the following files:
souffle::ast::transform::RemoveRedundantSumsTransformer::transform
bool transform(TranslationUnit &translationUnit) override
Definition: RemoveRedundantSums.cpp:38
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
souffle::test::count
int count(const C &c)
Definition: table_test.cpp:40
souffle::AggregateOp::COUNT
@ COUNT
souffle::AggregateOp::SUM
@ SUM