summaryrefslogtreecommitdiff
path: root/src/omt/omt_optimizer.cpp
blob: 2145492dbf0ed74c521cecfe68d2b8cada04374c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/******************************************************************************
 * Top contributors (to current version):
 *   Yancheng Ou, Aina Niemetz
 *
 * This file is part of the cvc5 project.
 *
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
 * in the top-level source directory and their institutional affiliations.
 * All rights reserved.  See the file COPYING in the top-level source
 * directory for licensing information.
 * ****************************************************************************
 *
 * The base class for optimizers of individual cvc5 type.
 */

#include "omt/omt_optimizer.h"

#include "omt/bitvector_optimizer.h"
#include "omt/integer_optimizer.h"

using namespace cvc5::theory;
using namespace cvc5::smt;
namespace cvc5::omt {

bool OMTOptimizer::nodeSupportsOptimization(TNode node)
{
  TypeNode type = node.getType();
  // only supports Integer and BitVectors as of now
  return (type.isInteger() || type.isBitVector());
}

std::unique_ptr<OMTOptimizer> OMTOptimizer::getOptimizerForObjective(
    const OptimizationObjective& objective)
{
  // the datatype of the target node
  TypeNode objectiveType = objective.getTarget().getType(true);
  if (objectiveType.isInteger())
  {
    // integer type: use OMTOptimizerInteger
    return std::unique_ptr<OMTOptimizer>(new OMTOptimizerInteger());
  }
  else if (objectiveType.isBitVector())
  {
    // bitvector type: use OMTOptimizerBitVector
    return std::unique_ptr<OMTOptimizer>(
        new OMTOptimizerBitVector(objective.bvIsSigned()));
  }
  else
  {
    Unimplemented() << "Target type " << objectiveType
                    << " does not support optimization";
  }
}

Node OMTOptimizer::mkStrongIncrementalExpression(
    NodeManager* nm,
    TNode lhs,
    TNode rhs,
    const OptimizationObjective& objective)
{
  constexpr const char lhsTypeError[] =
      "lhs type does not match or is not implicitly convertable to the target "
      "type";
  constexpr const char rhsTypeError[] =
      "rhs type does not match or is not implicitly convertable to the target "
      "type";
  TypeNode targetType = objective.getTarget().getType();
  switch (objective.getType())
  {
    case OptimizationObjective::MINIMIZE:
    {
      if (targetType.isInteger())
      {
        Assert(lhs.getType().isInteger()) << lhsTypeError;
        Assert(rhs.getType().isInteger()) << rhsTypeError;
        return nm->mkNode(Kind::LT, lhs, rhs);
      }
      else if (targetType.isBitVector())
      {
        Assert(lhs.getType() == targetType) << lhsTypeError;
        Assert(rhs.getType() == targetType) << rhsTypeError;
        return (objective.bvIsSigned())
                   ? (nm->mkNode(Kind::BITVECTOR_SLT, lhs, rhs))
                   : (nm->mkNode(Kind::BITVECTOR_ULT, lhs, rhs));
      }
      else
      {
        Unimplemented() << "Target type " << targetType
                        << " does not support optimization";
      }
    }
    case OptimizationObjective::MAXIMIZE:
    {
      if (targetType.isInteger())
      {
        Assert(lhs.getType().isInteger()) << lhsTypeError;
        Assert(rhs.getType().isInteger()) << rhsTypeError;
        return nm->mkNode(Kind::GT, lhs, rhs);
      }
      else if (targetType.isBitVector())
      {
        Assert(lhs.getType() == targetType) << lhsTypeError;
        Assert(rhs.getType() == targetType) << rhsTypeError;
        return (objective.bvIsSigned())
                   ? (nm->mkNode(Kind::BITVECTOR_SGT, lhs, rhs))
                   : (nm->mkNode(Kind::BITVECTOR_UGT, lhs, rhs));
      }
      else
      {
        Unimplemented() << "Target type " << targetType
                        << " does not support optimization";
      }
    }
    default:
      CVC5_FATAL() << "Optimization objective is neither MAXIMIZE nor MINIMIZE";
  }
  Unreachable();
}

Node OMTOptimizer::mkWeakIncrementalExpression(
    NodeManager* nm,
    TNode lhs,
    TNode rhs,
    const OptimizationObjective& objective)
{
  constexpr const char lhsTypeError[] =
      "lhs type does not match or is not implicitly convertable to the target "
      "type";
  constexpr const char rhsTypeError[] =
      "rhs type does not match or is not implicitly convertable to the target "
      "type";
  TypeNode targetType = objective.getTarget().getType();
  switch (objective.getType())
  {
    case OptimizationObjective::MINIMIZE:
    {
      if (targetType.isInteger())
      {
        Assert(lhs.getType().isInteger()) << lhsTypeError;
        Assert(rhs.getType().isInteger()) << rhsTypeError;
        return nm->mkNode(Kind::LEQ, lhs, rhs);
      }
      else if (targetType.isBitVector())
      {
        Assert(lhs.getType() == targetType) << lhsTypeError;
        Assert(rhs.getType() == targetType) << rhsTypeError;
        return (objective.bvIsSigned())
                   ? (nm->mkNode(Kind::BITVECTOR_SLE, lhs, rhs))
                   : (nm->mkNode(Kind::BITVECTOR_ULE, lhs, rhs));
      }
      else
      {
        Unimplemented() << "Target type " << targetType
                        << " does not support optimization";
      }
    }
    case OptimizationObjective::MAXIMIZE:
    {
      if (targetType.isInteger())
      {
        Assert(lhs.getType().isInteger()) << lhsTypeError;
        Assert(rhs.getType().isInteger()) << rhsTypeError;
        return nm->mkNode(Kind::GEQ, lhs, rhs);
      }
      else if (targetType.isBitVector())
      {
        Assert(lhs.getType() == targetType) << lhsTypeError;
        Assert(rhs.getType() == targetType) << rhsTypeError;
        return (objective.bvIsSigned())
                   ? (nm->mkNode(Kind::BITVECTOR_SGE, lhs, rhs))
                   : (nm->mkNode(Kind::BITVECTOR_UGE, lhs, rhs));
      }
      else
      {
        Unimplemented() << "Target type " << targetType
                        << " does not support optimization";
      }
    }
    default:
      CVC5_FATAL() << "Optimization objective is neither MAXIMIZE nor MINIMIZE";
  }
  Unreachable();
}

}  // namespace cvc5::omt
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback