cprover
accelerate.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Loop Acceleration
4 
5 Author: Matt Lewis
6 
7 \*******************************************************************/
8 
11 
12 #include "accelerate.h"
13 
14 #include <analyses/natural_loops.h>
15 
17 
18 #include <util/std_expr.h>
19 #include <util/arith_tools.h>
20 #include <util/find_symbols.h>
21 
22 #include <ansi-c/expr2c.h>
23 
24 #include <iostream>
25 #include <list>
26 
27 #include "path.h"
28 #include "polynomial_accelerator.h"
31 #include "overflow_instrumenter.h"
32 #include "util.h"
33 
35  goto_programt::targett loop_header)
36 {
38  natural_loops.loop_map[loop_header];
39  goto_programt::targett back_jump=loop_header;
40 
41  for(natural_loops_mutablet::natural_loopt::iterator it=loop.begin();
42  it!=loop.end();
43  ++it)
44  {
46  if(t->is_goto() &&
47  t->guard.is_true() &&
48  t->targets.size()==1 &&
49  t->targets.front()==loop_header &&
50  t->location_number > back_jump->location_number)
51  {
52  back_jump=t;
53  }
54  }
55 
56  return back_jump;
57 }
58 
60 {
62  natural_loops.loop_map[loop_header];
63 
64  for(natural_loops_mutablet::natural_loopt::iterator it=loop.begin();
65  it!=loop.end();
66  ++it)
67  {
68  const goto_programt::targett &t=*it;
69 
70  if(t->is_backwards_goto())
71  {
72  if(t->targets.size()!=1 ||
73  t->get_target()!=loop_header)
74  {
75  return true;
76  }
77  }
78 
79  if(t!=loop_header &&
81  {
82  return true;
83  }
84  }
85 
86  return false;
87 }
88 
90 {
91  pathst loop_paths, exit_paths;
92  goto_programt::targett back_jump=find_back_jump(loop_header);
93  int num_accelerated=0;
94  std::list<path_acceleratort> accelerators;
96  natural_loops.loop_map[loop_header];
97 
98  if(contains_nested_loops(loop_header))
99  {
100  // For now, only accelerate innermost loops.
101 #ifdef DEBUG
102  std::cout << "Not accelerating an outer loop\n";
103 #endif
104  return 0;
105  }
106 
107  goto_programt::targett overflow_loc;
108  make_overflow_loc(loop_header, back_jump, overflow_loc);
109  program.update();
110 
111 #if 1
112  enumerating_loop_accelerationt acceleration(
114  symbol_table,
116  program,
117  loop,
118  loop_header,
120 #else
122  acceleration(symbol_table, goto_functions, program, loop, loop_header);
123 #endif
124 
125  path_acceleratort accelerator;
126 
127  while(acceleration.accelerate(accelerator) &&
128  (accelerate_limit < 0 ||
129  num_accelerated < accelerate_limit))
130  {
131  // set_dirty_vars(accelerator);
132 
133  if(is_underapproximate(accelerator))
134  {
135  // We have some underapproximated variables -- just punt for now.
136 #ifdef DEBUG
137  std::cout << "Not inserting accelerator because of underapproximation\n";
138 #endif
139 
140  continue;
141  }
142 
143  accelerators.push_back(accelerator);
144  num_accelerated++;
145 
146 #ifdef DEBUG
147  std::cout << "Accelerated path:\n";
148  output_path(accelerator.path, program, ns, std::cout);
149 
150  std::cout << "Accelerator has "
151  << accelerator.pure_accelerator.instructions.size()
152  << " instructions\n";
153 #endif
154  }
155 
157  program.insert_before_swap(loop_header, skip);
158 
159  goto_programt::targett new_inst=loop_header;
160  ++new_inst;
161 
162  loop.insert(new_inst);
163 
164 
165  std::cout << "Overflow loc is " << overflow_loc->location_number << '\n';
166  std::cout << "Back jump is " << back_jump->location_number << '\n';
167 
168  for(std::list<path_acceleratort>::iterator it=accelerators.begin();
169  it!=accelerators.end();
170  ++it)
171  {
172  subsumed_patht inserted(it->path);
173 
174  insert_accelerator(loop_header, back_jump, *it, inserted);
175  subsumed.push_back(inserted);
176  num_accelerated++;
177  }
178 
179  return num_accelerated;
180 }
181 
183  goto_programt::targett &loop_header,
184  goto_programt::targett &back_jump,
185  path_acceleratort &accelerator,
186  subsumed_patht &subsumed)
187 {
189  loop_header, back_jump, accelerator.pure_accelerator, subsumed.accelerator);
190 
191  if(!accelerator.overflow_path.instructions.empty())
192  {
194  loop_header, back_jump, accelerator.overflow_path, subsumed.residue);
195  }
196 }
197 
198 /*
199  * Insert a looping path (usually an accelerator) into a goto-program,
200  * beginning at loop_header and jumping back to loop_header via back_jump.
201  * Stores the locations at which the looping path was added in inserted_path.
202  *
203  * THIS DESTROYS looping_path!!
204  */
206  goto_programt::targett &loop_header,
207  goto_programt::targett &back_jump,
208  goto_programt &looping_path,
209  patht &inserted_path)
210 {
211  goto_programt::targett loop_body=loop_header;
212  ++loop_body;
213 
215  jump->make_goto(loop_body, side_effect_expr_nondett(bool_typet()));
216 
217  program.destructive_insert(loop_body, looping_path);
218 
219  jump=program.insert_before(loop_body);
220  jump->make_goto(back_jump, true_exprt());
221 
222  for(goto_programt::targett t=loop_header;
223  t!=loop_body;
224  ++t)
225  {
226  inserted_path.push_back(path_nodet(t));
227  }
228 
229  inserted_path.push_back(path_nodet(back_jump));
230 }
231 
233  goto_programt::targett loop_header,
234  goto_programt::targett &loop_end,
235  goto_programt::targett &overflow_loc)
236 {
237  symbolt overflow_sym=utils.fresh_symbol("accelerate::overflow", bool_typet());
238  const exprt &overflow_var=overflow_sym.symbol_expr();
240  natural_loops.loop_map[loop_header];
241  overflow_instrumentert instrumenter(program, overflow_var, symbol_table);
242 
243  for(natural_loops_mutablet::natural_loopt::iterator it=loop.begin();
244  it!=loop.end();
245  ++it)
246  {
249 
250  instrumenter.add_overflow_checks(*it, added);
251  loop.insert(added.begin(), added.end());
252  }
253 
255  t->make_assignment();
256  t->code=code_assignt(overflow_var, false_exprt());
257  t->swap(*loop_header);
258  loop.insert(t);
259  overflow_locs[loop_header].push_back(t);
260 
262  overflow_loc=program.insert_after(loop_end);
263  *overflow_loc=s;
264  overflow_loc->swap(*loop_end);
265  loop.insert(overflow_loc);
266 
268  g.guard=not_exprt(overflow_var);
269  g.targets.push_back(overflow_loc);
271  *t2=g;
272  t2->swap(*loop_end);
273  overflow_locs[overflow_loc].push_back(t2);
274  loop.insert(t2);
275 
276  goto_programt::targett tmp=overflow_loc;
277  overflow_loc=loop_end;
278  loop_end=tmp;
279 }
280 
282 {
283  trace_automatont automaton(program);
284 
285  for(subsumed_pathst::iterator it=subsumed.begin();
286  it!=subsumed.end();
287  ++it)
288  {
289  if(!it->subsumed.empty())
290  {
291 #ifdef DEBUG
293  std::cout << "Restricting path:\n";
294  output_path(it->subsumed, program, ns, std::cout);
295 #endif
296 
297  automaton.add_path(it->subsumed);
298  }
299 
300  patht double_accelerator;
301  patht::iterator jt=double_accelerator.begin();
302  double_accelerator.insert(
303  jt, it->accelerator.begin(), it->accelerator.end());
304  double_accelerator.insert(
305  jt, it->accelerator.begin(), it->accelerator.end());
306 
307 #ifdef DEBUG
309  std::cout << "Restricting path:\n";
310  output_path(double_accelerator, program, ns, std::cout);
311 #endif
312  automaton.add_path(double_accelerator);
313  }
314 
315  std::cout << "Building trace automaton...\n";
316 
317  automaton.build();
318  insert_automaton(automaton);
319 }
320 
322 {
323  for(std::set<exprt>::iterator it=accelerator.dirty_vars.begin();
324  it!=accelerator.dirty_vars.end();
325  ++it)
326  {
327  expr_mapt::iterator jt=dirty_vars_map.find(*it);
328  exprt dirty_var;
329 
330  if(jt==dirty_vars_map.end())
331  {
333  symbolt new_sym=utils.fresh_symbol("accelerate::dirty", bool_typet());
334  dirty_var=new_sym.symbol_expr();
335  dirty_vars_map[*it]=dirty_var;
336  }
337  else
338  {
339  dirty_var=jt->second;
340  }
341 
342 #ifdef DEBUG
343  std::cout << "Setting dirty flag " << expr2c(dirty_var, ns)
344  << " for " << expr2c(*it, ns) << '\n';
345 #endif
346 
347  accelerator.pure_accelerator.add_instruction(ASSIGN)->code =
348  code_assignt(dirty_var, true_exprt());
349  }
350 }
351 
353 {
354  for(expr_mapt::iterator it=dirty_vars_map.begin();
355  it!=dirty_vars_map.end();
356  ++it)
357  {
359  assign.code=code_assignt(it->second, false_exprt());
361  }
362 
364 
366  it!=program.instructions.end();
367  it=next)
368  {
369  next=it;
370  ++next;
371 
372  // If this is an assign to a tracked variable, clear the dirty flag.
373  // Note: this order of insertions means that we assume each of the read
374  // variables is clean _before_ clearing any dirty flags.
375  if(it->is_assign())
376  {
377  exprt &lhs=it->code.op0();
378  expr_mapt::iterator dirty_var=dirty_vars_map.find(lhs);
379 
380  if(dirty_var!=dirty_vars_map.end())
381  {
383  clear_flag.code=code_assignt(dirty_var->second, false_exprt());
384  program.insert_before_swap(it, clear_flag);
385  }
386  }
387 
388  // Find which symbols are read, i.e. those appearing in a guard or on
389  // the right hand side of an assignment. Assume each is not dirty.
390  find_symbols_sett read;
391 
392  find_symbols(it->guard, read);
393 
394  if(it->is_assign())
395  {
396  find_symbols(it->code.op1(), read);
397  }
398 
399  for(find_symbols_sett::iterator jt=read.begin();
400  jt!=read.end();
401  ++jt)
402  {
403  const exprt &var=ns.lookup(*jt).symbol_expr();
404  expr_mapt::iterator dirty_var=dirty_vars_map.find(var);
405 
406  if(dirty_var==dirty_vars_map.end())
407  {
408  continue;
409  }
410 
412  not_dirty.guard=not_exprt(dirty_var->second);
413  program.insert_before_swap(it, not_dirty);
414  }
415  }
416 }
417 
419 {
420  for(std::set<exprt>::iterator it=accelerator.dirty_vars.begin();
421  it!=accelerator.dirty_vars.end();
422  ++it)
423  {
424  if(it->id()==ID_symbol && it->type() == bool_typet())
425  {
426  const irep_idt &id=to_symbol_expr(*it).get_identifier();
427  const symbolt &sym=*symbol_table.lookup(id);
428 
429  if(sym.module=="scratch")
430  {
431  continue;
432  }
433  }
434 
435 #ifdef DEBUG
436  std::cout << "Underapproximate variable: " << expr2c(*it, ns) << '\n';
437 #endif
438  return true;
439  }
440 
441  return false;
442 }
443 
444 symbolt acceleratet::make_symbol(std::string name, typet type)
445 {
446  symbolt ret;
447  ret.module="accelerate";
448  ret.name=name;
449  ret.base_name=name;
450  ret.pretty_name=name;
451  ret.type=type;
452 
453  symbol_table.add(ret);
454 
455  return ret;
456 }
457 
459 {
460 #if 0
462  code_declt code(sym);
463 
464  decl->make_decl();
465  decl->code=code;
466 #endif
467 }
468 
470 {
471  decl(sym, t);
472 
474  code_assignt code(sym, init);
475 
476  assign->make_assignment();
477  assign->code=code;
478 }
479 
481 {
482  symbolt state_sym=make_symbol("trace_automaton::state",
484  symbolt next_state_sym=make_symbol("trace_automaton::next_state",
486  symbol_exprt state=state_sym.symbol_expr();
487  symbol_exprt next_state=next_state_sym.symbol_expr();
488 
489  trace_automatont::sym_mapt transitions;
490  state_sett accept_states;
491 
492  automaton.get_transitions(transitions);
493  automaton.accept_states(accept_states);
494 
495  std::cout
496  << "Inserting trace automaton with "
497  << automaton.num_states() << " states, "
498  << accept_states.size() << " accepting states and "
499  << transitions.size() << " transitions\n";
500 
501  // Declare the variables we'll use to encode the state machine.
503  decl(state, t, from_integer(automaton.init_state(), state.type()));
504  decl(next_state, t);
505 
506  // Now for each program location that appears as a symbol in the
507  // trace automaton, add the appropriate code to drive the state
508  // machine.
509  for(const auto &sym : automaton.alphabet)
510  {
512  trace_automatont::sym_range_pairt p=transitions.equal_range(sym);
513 
514  build_state_machine(p.first, p.second, accept_states, state, next_state,
515  state_machine);
516 
517  program.insert_before_swap(sym, state_machine);
518  }
519 }
520 
522  trace_automatont::sym_mapt::iterator begin,
523  trace_automatont::sym_mapt::iterator end,
524  state_sett &accept_states,
525  symbol_exprt state,
526  symbol_exprt next_state,
527  scratch_programt &state_machine)
528 {
529  std::map<unsigned int, unsigned int> successor_counts;
530  unsigned int max_count=0;
531  unsigned int likely_next=0;
532 
533  // Optimisation: find the most common successor state and initialise
534  // next_state to that value. This reduces the size of the state machine
535  // driver substantially.
536  for(trace_automatont::sym_mapt::iterator p=begin; p!=end; ++p)
537  {
538  trace_automatont::state_pairt state_pair=p->second;
539  unsigned int to=state_pair.second;
540  unsigned int count=0;
541 
542  if(successor_counts.find(to)==successor_counts.end())
543  {
544  count=1;
545  }
546  else
547  {
548  count=successor_counts[to] + 1;
549  }
550 
551  successor_counts[to]=count;
552 
553  if(count > max_count)
554  {
555  max_count=count;
556  likely_next=to;
557  }
558  }
559 
560  // Optimisation: if there is only one possible successor state, just
561  // jump straight to it instead of driving the whole machine.
562  if(successor_counts.size()==1)
563  {
564  if(accept_states.find(likely_next)!=accept_states.end())
565  {
566  // It's an accept state. Just assume(false).
567  state_machine.assume(false_exprt());
568  }
569  else
570  {
571  state_machine.assign(state,
572  from_integer(likely_next, next_state.type()));
573  }
574 
575  return;
576  }
577 
578  state_machine.assign(next_state,
579  from_integer(likely_next, next_state.type()));
580 
581  for(trace_automatont::sym_mapt::iterator p=begin; p!=end; ++p)
582  {
583  trace_automatont::state_pairt state_pair=p->second;
584  unsigned int from=state_pair.first;
585  unsigned int to=state_pair.second;
586 
587  if(to==likely_next)
588  {
589  continue;
590  }
591 
592  // We're encoding the transition
593  //
594  // from -loc-> to
595  //
596  // which we encode by inserting:
597  //
598  // next_state=(state==from) ? to : next_state;
599  //
600  // just before loc.
601  equal_exprt guard(state, from_integer(from, state.type()));
602  if_exprt rhs(guard, from_integer(to, next_state.type()), next_state);
603  state_machine.assign(next_state, rhs);
604  }
605 
606  // Update the state and assume(false) if we've hit an accept state.
607  state_machine.assign(state, next_state);
608 
609  for(state_sett::iterator it=accept_states.begin();
610  it!=accept_states.end();
611  ++it)
612  {
613  state_machine.assume(
614  not_exprt(equal_exprt(state, from_integer(*it, state.type()))));
615  }
616 }
617 
619 {
620  int num_accelerated=0;
621 
622  for(natural_loops_mutablet::loop_mapt::iterator it =
623  natural_loops.loop_map.begin();
624  it!=natural_loops.loop_map.end();
625  ++it)
626  {
627  goto_programt::targett t=it->first;
628  num_accelerated += accelerate_loop(t);
629  }
630 
631  program.update();
632 
633  if(num_accelerated > 0)
634  {
635  std::cout << "Engaging crush mode...\n";
636 
637  restrict_traces();
638  // add_dirty_checks();
639  program.update();
640 
641  std::cout << "Crush mode engaged.\n";
642  }
643 
644  return num_accelerated;
645 }
646 
648  goto_modelt &goto_model,
650  bool use_z3)
651 {
652  Forall_goto_functions(it, goto_model.goto_functions)
653  {
654  std::cout << "Accelerating function " << it->first << '\n';
655  acceleratet accelerate(
656  it->second.body, goto_model, message_handler, use_z3);
657 
658  int num_accelerated=accelerate.accelerate_loops();
659 
660  if(num_accelerated > 0)
661  {
662  std::cout << "Added " << num_accelerated
663  << " accelerator(s)\n";
664  }
665  }
666 }
exprt guard
Guard for gotos, assume, assert.
Definition: goto_program.h:188
void get_transitions(sym_mapt &transitions)
expr_mapt dirty_vars_map
Definition: accelerate.h:125
The type of an expression.
Definition: type.h:22
irep_idt name
The unique identifier.
Definition: symbol.h:43
void insert_automaton(trace_automatont &automaton)
Definition: accelerate.cpp:480
Loop Acceleration.
void update()
Update all indices.
Boolean negation.
Definition: std_expr.h:3230
void make_overflow_loc(goto_programt::targett loop_header, goto_programt::targett &loop_end, goto_programt::targett &overflow_loc)
Definition: accelerate.cpp:232
void insert_before_swap(targett target)
Insertion that preserves jumps to "target".
Definition: goto_program.h:441
std::list< targett > targetst
Definition: goto_program.h:399
targett assign(const exprt &lhs, const exprt &rhs)
natural_loops_mutablet natural_loops
Definition: accelerate.h:117
static const int accelerate_limit
Definition: accelerate.h:56
unsigned num_states()
targett insert_before(const_targett target)
Insertion before the given target.
Definition: goto_program.h:473
exprt & op0()
Definition: expr.h:72
void insert_looping_path(goto_programt::targett &loop_header, goto_programt::targett &back_jump, goto_programt &looping_path, patht &inserted_path)
Definition: accelerate.cpp:205
goto_functionst & goto_functions
Definition: accelerate.h:114
bool contains_nested_loops(goto_programt::targett &loop_header)
Definition: accelerate.cpp:59
const irep_idt & get_identifier() const
Definition: std_expr.h:128
Goto Programs with Functions.
message_handlert & message_handler
Definition: accelerate.h:59
int accelerate_loop(goto_programt::targett &loop_header)
Definition: accelerate.cpp:89
The trinary if-then-else operator.
Definition: std_expr.h:3361
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:46
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:55
typet & type()
Definition: expr.h:56
void accept_states(state_sett &states)
void set_dirty_vars(path_acceleratort &accelerator)
Definition: accelerate.cpp:321
The proper Booleans.
Definition: std_types.h:34
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:30
subsumed_pathst subsumed
Definition: accelerate.h:118
std::pair< statet, statet > state_pairt
symbol_tablet & symbol_table
Definition: accelerate.h:115
targetst targets
The list of successor instructions.
Definition: goto_program.h:198
goto_programt overflow_path
Definition: accelerator.h:64
equality
Definition: std_expr.h:1354
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:173
void add_dirty_checks()
Definition: accelerate.cpp:352
void accelerate_functions(goto_modelt &goto_model, message_handlert &message_handler, bool use_z3)
Definition: accelerate.cpp:647
virtual bool accelerate(path_acceleratort &accelerator)
std::pair< sym_mapt::iterator, sym_mapt::iterator > sym_range_pairt
class symbol_exprt symbol_expr() const
produces a symbol_exprt for a symbol
Definition: symbol.cpp:111
The boolean constant true.
Definition: std_expr.h:4488
unsignedbv_typet unsigned_poly_type()
Definition: util.cpp:25
instructionst::iterator targett
Definition: goto_program.h:397
A declaration of a local variable.
Definition: std_code.h:253
targett assume(const exprt &guard)
symbolt fresh_symbol(std::string base, typet type)
goto_programt & program
Definition: accelerate.h:113
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:403
Loop Acceleration.
API to expression classes.
std::list< path_nodet > patht
Definition: path.h:45
TO_BE_DOCUMENTED.
Definition: namespace.h:74
bool is_underapproximate(path_acceleratort &accelerator)
Definition: accelerate.cpp:418
targett insert_after(const_targett target)
Insertion after the given target.
Definition: goto_program.h:480
A side effect that returns a non-deterministically chosen value.
Definition: std_code.h:1301
void destructive_insert(const_targett target, goto_programt &p)
Inserts the given program at the given location.
Definition: goto_program.h:495
std::list< patht > pathst
Definition: path.h:46
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast a generic exprt to a symbol_exprt.
Definition: std_expr.h:210
void add_path(patht &path)
void insert_accelerator(goto_programt::targett &loop_header, goto_programt::targett &back_jump, path_acceleratort &accelerator, subsumed_patht &subsumed)
Definition: accelerate.cpp:182
Loop Acceleration.
std::set< statet > state_sett
The boolean constant false.
Definition: std_expr.h:4499
symbolt make_symbol(std::string name, typet type)
Definition: accelerate.cpp:444
goto_programt::targett find_back_jump(goto_programt::targett loop_header)
Definition: accelerate.cpp:34
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:70
Loop Acceleration.
void decl(symbol_exprt &sym, goto_programt::targett t)
Definition: accelerate.cpp:458
void output_path(const patht &path, const goto_programt &program, const namespacet &ns, std::ostream &str)
Definition: path.cpp:18
typet type
Type of symbol.
Definition: symbol.h:34
std::multimap< goto_programt::targett, state_pairt > sym_mapt
std::set< exprt > dirty_vars
Definition: accelerator.h:66
targett add_instruction()
Adds an instruction at the end.
Definition: goto_program.h:505
Base class for all expressions.
Definition: expr.h:42
overflow_mapt overflow_locs
Definition: accelerate.h:123
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:49
#define Forall_goto_functions(it, functions)
goto_programt pure_accelerator
Definition: accelerator.h:63
std::string expr2c(const exprt &expr, const namespacet &ns)
Definition: expr2c.cpp:3967
void swap(instructiont &instruction)
Swap two instructions.
Definition: goto_program.h:339
void build_state_machine(trace_automatont::sym_mapt::iterator p, trace_automatont::sym_mapt::iterator end, state_sett &accept_states, symbol_exprt state, symbol_exprt next_state, scratch_programt &state_machine)
Definition: accelerate.cpp:521
Expression to hold a symbol (variable)
Definition: std_expr.h:90
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
void restrict_traces()
Definition: accelerate.cpp:281
Compute natural loops in a goto_function.
goto_programt coverage_criteriont message_handlert & message_handler
Definition: cover.cpp:66
std::unordered_set< irep_idt > find_symbols_sett
Definition: find_symbols.h:20
int accelerate_loops()
Definition: accelerate.cpp:618
acceleration_utilst utils
Definition: accelerate.h:119
namespacet ns
Definition: accelerate.h:116
Loop Acceleration.
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
void find_symbols(const exprt &src, find_symbols_sett &dest)
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:32
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See namespace_baset::lookup().
Definition: namespace.cpp:130
Assignment.
Definition: std_code.h:195