CBMC
qdimacs_core.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: CM Wintersteiger
6 
7 \*******************************************************************/
8 
9 #include "qdimacs_core.h"
10 
11 #include <util/bitvector_expr.h>
12 #include <util/std_expr.h>
13 
14 #include <set>
15 
17 {
18  if(expr.id()==ID_and)
19  {
20  typedef std::map<exprt, std::set<exprt> > used_bits_mapt;
21  used_bits_mapt used_bits_map;
22 
23  for(const auto &op : expr.operands())
24  {
25  if(op.id() == ID_extractbit)
26  {
27  const auto &extractbit_expr = to_extractbit_expr(op);
28  if(extractbit_expr.op1().is_constant())
29  used_bits_map[extractbit_expr.src()].insert(extractbit_expr.index());
30  }
31  else if(op.id() == ID_not && to_not_expr(op).op().id() == ID_extractbit)
32  {
33  const auto &extractbit_expr = to_extractbit_expr(to_not_expr(op).op());
34  if(extractbit_expr.op1().is_constant())
35  used_bits_map[extractbit_expr.src()].insert(extractbit_expr.index());
36  }
37  }
38 
39  // clang-format off
40  // this is unmaintained code, don't try to reformat it
41  for(used_bits_mapt::const_iterator it=used_bits_map.begin();
42  it!=used_bits_map.end();
43  it++)
44  {
45  #if 0
46  unsigned width;
47  boolbv_get_width(it->first.type(), width);
48 
49  std::string value_string;
50  value_string.resize(width, '0');
51 
52  if(it->second.size()==width) // all bits extracted from this one!
53  {
54  const irep_idt &ident=it->first.get(ID_identifier);
55  const exprt::operandst &old_operands=expr.operands();
56  exprt::operandst new_operands;
57 
58  for(exprt::operandst::const_iterator oit=old_operands.begin();
59  oit!=old_operands.end();
60  oit++)
61  {
62  if(oit->id()==ID_extractbit &&
63  oit->op1().is_constant())
64  {
65  if(oit->op0().get(ID_identifier)==ident)
66  {
67  const exprt &val_expr=oit->op1();
68  const std::size_t value = numeric_cast_v<std::size_t>(val_expr);
69  value_string[value]='1';
70 
71  #if 0
72  std::cout << "[" << value << "]=1\n";
73  #endif
74 
75  continue;
76  }
77  }
78  else if(oit->id()==ID_not &&
79  oit->op0().id()==ID_extractbit &&
80  oit->op0().op1().is_constant())
81  {
82  if(oit->op0().op0().get(ID_identifier)==ident)
83  {
84  // just kick it; the bit in value_string is 0 anyways
85  continue;
86  }
87  }
88 
89  new_operands.push_back(*oit);
90  }
91 
92  const constant_exprt new_value(value_string, it->first.type());
93  new_operands.push_back(equality_exprt(it->first, new_value));
94 
95  #if 0
96  std::cout << "FINAL: " << value_string << '\n';
97  #endif
98 
99  expr.operands()=new_operands;
100  }
101  #endif
102  }
103  // clang-format on
104  }
105 }
API to expression classes for bitvectors.
const extractbit_exprt & to_extractbit_expr(const exprt &expr)
Cast an exprt to an extractbit_exprt.
A constant literal expression.
Definition: std_expr.h:2990
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
std::vector< exprt > operandst
Definition: expr.h:58
exprt & op1()
Definition: expr.h:136
operandst & operands()
Definition: expr.h:94
const irep_idt & id() const
Definition: irep.h:388
void simplify_extractbits(exprt &expr) const
API to expression classes.
const not_exprt & to_not_expr(const exprt &expr)
Cast an exprt to an not_exprt.
Definition: std_expr.h:2352