CBMC
interval_analysis.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Interval Analysis
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
13 
14 #include "interval_analysis.h"
15 
16 #include <util/find_symbols.h>
17 
18 #include "ai.h"
19 #include "interval_domain.h"
20 
32  goto_functionst::goto_functiont &goto_function)
33 {
34  std::set<symbol_exprt> symbols;
35 
36  for(const auto &i : goto_function.body.instructions)
37  i.apply([&symbols](const exprt &e) { find_symbols(e, symbols); });
38 
39  Forall_goto_program_instructions(i_it, goto_function.body)
40  {
41  if(i_it==goto_function.body.instructions.begin())
42  {
43  // first instruction, we instrument
44  }
45  else
46  {
47  goto_programt::const_targett previous = std::prev(i_it);
48 
49  if(previous->is_goto() && !previous->condition().is_true())
50  {
51  // we follow a branch, instrument
52  }
53  else if(previous->is_function_call())
54  {
55  // we follow a function call, instrument
56  }
57  else if(i_it->is_target() || i_it->is_function_call())
58  {
59  // we are a target or a function call, instrument
60  }
61  else
62  continue; // don't instrument
63  }
64 
65  const interval_domaint &d=interval_analysis[i_it];
66 
67  exprt::operandst assertion;
68 
69  for(const auto &symbol_expr : symbols)
70  {
71  exprt tmp=d.make_expression(symbol_expr);
72  if(!tmp.is_true())
73  assertion.push_back(tmp);
74  }
75 
76  if(!assertion.empty())
77  {
79  goto_function.body.insert_before_swap(i_it);
81  i_it++; // goes to original instruction
82  t->source_location_nonconst() = i_it->source_location();
83  }
84  }
85 }
86 
90 void interval_analysis(goto_modelt &goto_model)
91 {
93 
94  const namespacet ns(goto_model.symbol_table);
95  interval_analysis(goto_model.goto_functions, ns);
96 
97  for(auto &gf_entry : goto_model.goto_functions.function_map)
98  instrument_intervals(interval_analysis, gf_entry.second);
99 }
Forall_goto_program_instructions
#define Forall_goto_program_instructions(it, program)
Definition: goto_program.h:1234
find_symbols
static bool find_symbols(symbol_kindt, const typet &, std::function< bool(const symbol_exprt &)>, std::unordered_set< irep_idt > &bindings)
Definition: find_symbols.cpp:122
conjunction
exprt conjunction(const exprt::operandst &op)
1) generates a conjunction for two or more operands 2) for one operand, returns the operand 3) return...
Definition: std_expr.cpp:62
exprt
Base class for all expressions.
Definition: expr.h:55
ait
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition: ai.h:563
goto_modelt
Definition: goto_model.h:25
exprt::is_true
bool is_true() const
Return whether the expression is a constant representing true.
Definition: expr.cpp:34
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:29
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
find_symbols.h
instrument_intervals
void instrument_intervals(const ait< interval_domaint > &interval_analysis, goto_functionst::goto_functiont &goto_function)
Instruments all "post-branch" instructions with assumptions about variable intervals.
Definition: interval_analysis.cpp:30
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:58
ai.h
goto_functionst::goto_functiont
::goto_functiont goto_functiont
Definition: goto_functions.h:27
interval_domain.h
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
interval_domaint
Definition: interval_domain.h:23
interval_domaint::make_expression
exprt make_expression(const symbol_exprt &) const
Definition: interval_domain.cpp:404
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:587
goto_programt::make_assumption
static instructiont make_assumption(const exprt &g, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:936
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:586
interval_analysis.h
interval_analysis
void interval_analysis(goto_modelt &goto_model)
Initialises the abstract interpretation over interval domain and instruments instructions using inter...
Definition: interval_analysis.cpp:90