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(
216  loop_body,
218 
219  program.destructive_insert(loop_body, looping_path);
220 
221  jump=program.insert_before(loop_body);
222  jump->make_goto(back_jump, true_exprt());
223 
224  for(goto_programt::targett t=loop_header;
225  t!=loop_body;
226  ++t)
227  {
228  inserted_path.push_back(path_nodet(t));
229  }
230 
231  inserted_path.push_back(path_nodet(back_jump));
232 }
233 
235  goto_programt::targett loop_header,
236  goto_programt::targett &loop_end,
237  goto_programt::targett &overflow_loc)
238 {
239  symbolt overflow_sym=utils.fresh_symbol("accelerate::overflow", bool_typet());
240  const exprt &overflow_var=overflow_sym.symbol_expr();
242  natural_loops.loop_map[loop_header];
243  overflow_instrumentert instrumenter(program, overflow_var, symbol_table);
244 
245  for(natural_loops_mutablet::natural_loopt::iterator it=loop.begin();
246  it!=loop.end();
247  ++it)
248  {
251 
252  instrumenter.add_overflow_checks(*it, added);
253  loop.insert(added.begin(), added.end());
254  }
255 
257  t->make_assignment();
258  t->code=code_assignt(overflow_var, false_exprt());
259  t->swap(*loop_header);
260  loop.insert(t);
261  overflow_locs[loop_header].push_back(t);
262 
264  overflow_loc=program.insert_after(loop_end);
265  *overflow_loc=s;
266  overflow_loc->swap(*loop_end);
267  loop.insert(overflow_loc);
268 
270  g.guard=not_exprt(overflow_var);
271  g.targets.push_back(overflow_loc);
273  *t2=g;
274  t2->swap(*loop_end);
275  overflow_locs[overflow_loc].push_back(t2);
276  loop.insert(t2);
277 
278  goto_programt::targett tmp=overflow_loc;
279  overflow_loc=loop_end;
280  loop_end=tmp;
281 }
282 
284 {
285  trace_automatont automaton(program);
286 
287  for(subsumed_pathst::iterator it=subsumed.begin();
288  it!=subsumed.end();
289  ++it)
290  {
291  if(!it->subsumed.empty())
292  {
293 #ifdef DEBUG
295  std::cout << "Restricting path:\n";
296  output_path(it->subsumed, program, ns, std::cout);
297 #endif
298 
299  automaton.add_path(it->subsumed);
300  }
301 
302  patht double_accelerator;
303  patht::iterator jt=double_accelerator.begin();
304  double_accelerator.insert(
305  jt, it->accelerator.begin(), it->accelerator.end());
306  double_accelerator.insert(
307  jt, it->accelerator.begin(), it->accelerator.end());
308 
309 #ifdef DEBUG
311  std::cout << "Restricting path:\n";
312  output_path(double_accelerator, program, ns, std::cout);
313 #endif
314  automaton.add_path(double_accelerator);
315  }
316 
317  std::cout << "Building trace automaton...\n";
318 
319  automaton.build();
320  insert_automaton(automaton);
321 }
322 
324 {
325  for(std::set<exprt>::iterator it=accelerator.dirty_vars.begin();
326  it!=accelerator.dirty_vars.end();
327  ++it)
328  {
329  expr_mapt::iterator jt=dirty_vars_map.find(*it);
330  exprt dirty_var;
331 
332  if(jt==dirty_vars_map.end())
333  {
335  symbolt new_sym=utils.fresh_symbol("accelerate::dirty", bool_typet());
336  dirty_var=new_sym.symbol_expr();
337  dirty_vars_map[*it]=dirty_var;
338  }
339  else
340  {
341  dirty_var=jt->second;
342  }
343 
344 #ifdef DEBUG
345  std::cout << "Setting dirty flag " << expr2c(dirty_var, ns)
346  << " for " << expr2c(*it, ns) << '\n';
347 #endif
348 
349  accelerator.pure_accelerator.add_instruction(ASSIGN)->code =
350  code_assignt(dirty_var, true_exprt());
351  }
352 }
353 
355 {
356  for(expr_mapt::iterator it=dirty_vars_map.begin();
357  it!=dirty_vars_map.end();
358  ++it)
359  {
361  assign.code=code_assignt(it->second, false_exprt());
363  }
364 
366 
368  it!=program.instructions.end();
369  it=next)
370  {
371  next=it;
372  ++next;
373 
374  // If this is an assign to a tracked variable, clear the dirty flag.
375  // Note: this order of insertions means that we assume each of the read
376  // variables is clean _before_ clearing any dirty flags.
377  if(it->is_assign())
378  {
379  exprt &lhs=it->code.op0();
380  expr_mapt::iterator dirty_var=dirty_vars_map.find(lhs);
381 
382  if(dirty_var!=dirty_vars_map.end())
383  {
385  clear_flag.code=code_assignt(dirty_var->second, false_exprt());
386  program.insert_before_swap(it, clear_flag);
387  }
388  }
389 
390  // Find which symbols are read, i.e. those appearing in a guard or on
391  // the right hand side of an assignment. Assume each is not dirty.
392  find_symbols_sett read;
393 
394  find_symbols(it->guard, read);
395 
396  if(it->is_assign())
397  {
398  find_symbols(it->code.op1(), read);
399  }
400 
401  for(find_symbols_sett::iterator jt=read.begin();
402  jt!=read.end();
403  ++jt)
404  {
405  const exprt &var=ns.lookup(*jt).symbol_expr();
406  expr_mapt::iterator dirty_var=dirty_vars_map.find(var);
407 
408  if(dirty_var==dirty_vars_map.end())
409  {
410  continue;
411  }
412 
414  not_dirty.guard=not_exprt(dirty_var->second);
415  program.insert_before_swap(it, not_dirty);
416  }
417  }
418 }
419 
421 {
422  for(std::set<exprt>::iterator it=accelerator.dirty_vars.begin();
423  it!=accelerator.dirty_vars.end();
424  ++it)
425  {
426  if(it->id()==ID_symbol && it->type() == bool_typet())
427  {
428  const irep_idt &id=to_symbol_expr(*it).get_identifier();
429  const symbolt &sym=*symbol_table.lookup(id);
430 
431  if(sym.module=="scratch")
432  {
433  continue;
434  }
435  }
436 
437 #ifdef DEBUG
438  std::cout << "Underapproximate variable: " << expr2c(*it, ns) << '\n';
439 #endif
440  return true;
441  }
442 
443  return false;
444 }
445 
446 symbolt acceleratet::make_symbol(std::string name, typet type)
447 {
448  symbolt ret;
449  ret.module="accelerate";
450  ret.name=name;
451  ret.base_name=name;
452  ret.pretty_name=name;
453  ret.type=type;
454 
455  symbol_table.add(ret);
456 
457  return ret;
458 }
459 
461 {
462 #if 0
464  code_declt code(sym);
465 
466  decl->make_decl();
467  decl->code=code;
468 #endif
469 }
470 
472 {
473  decl(sym, t);
474 
476  code_assignt code(sym, init);
477 
478  assign->make_assignment();
479  assign->code=code;
480 }
481 
483 {
484  symbolt state_sym=make_symbol("trace_automaton::state",
486  symbolt next_state_sym=make_symbol("trace_automaton::next_state",
488  symbol_exprt state=state_sym.symbol_expr();
489  symbol_exprt next_state=next_state_sym.symbol_expr();
490 
491  trace_automatont::sym_mapt transitions;
492  state_sett accept_states;
493 
494  automaton.get_transitions(transitions);
495  automaton.accept_states(accept_states);
496 
497  std::cout
498  << "Inserting trace automaton with "
499  << automaton.num_states() << " states, "
500  << accept_states.size() << " accepting states and "
501  << transitions.size() << " transitions\n";
502 
503  // Declare the variables we'll use to encode the state machine.
505  decl(state, t, from_integer(automaton.init_state(), state.type()));
506  decl(next_state, t);
507 
508  // Now for each program location that appears as a symbol in the
509  // trace automaton, add the appropriate code to drive the state
510  // machine.
511  for(const auto &sym : automaton.alphabet)
512  {
514  trace_automatont::sym_range_pairt p=transitions.equal_range(sym);
515 
516  build_state_machine(p.first, p.second, accept_states, state, next_state,
517  state_machine);
518 
519  program.insert_before_swap(sym, state_machine);
520  }
521 }
522 
524  trace_automatont::sym_mapt::iterator begin,
525  trace_automatont::sym_mapt::iterator end,
526  state_sett &accept_states,
527  symbol_exprt state,
528  symbol_exprt next_state,
529  scratch_programt &state_machine)
530 {
531  std::map<unsigned int, unsigned int> successor_counts;
532  unsigned int max_count=0;
533  unsigned int likely_next=0;
534 
535  // Optimisation: find the most common successor state and initialise
536  // next_state to that value. This reduces the size of the state machine
537  // driver substantially.
538  for(trace_automatont::sym_mapt::iterator p=begin; p!=end; ++p)
539  {
540  trace_automatont::state_pairt state_pair=p->second;
541  unsigned int to=state_pair.second;
542  unsigned int count=0;
543 
544  if(successor_counts.find(to)==successor_counts.end())
545  {
546  count=1;
547  }
548  else
549  {
550  count=successor_counts[to] + 1;
551  }
552 
553  successor_counts[to]=count;
554 
555  if(count > max_count)
556  {
557  max_count=count;
558  likely_next=to;
559  }
560  }
561 
562  // Optimisation: if there is only one possible successor state, just
563  // jump straight to it instead of driving the whole machine.
564  if(successor_counts.size()==1)
565  {
566  if(accept_states.find(likely_next)!=accept_states.end())
567  {
568  // It's an accept state. Just assume(false).
569  state_machine.assume(false_exprt());
570  }
571  else
572  {
573  state_machine.assign(state,
574  from_integer(likely_next, next_state.type()));
575  }
576 
577  return;
578  }
579 
580  state_machine.assign(next_state,
581  from_integer(likely_next, next_state.type()));
582 
583  for(trace_automatont::sym_mapt::iterator p=begin; p!=end; ++p)
584  {
585  trace_automatont::state_pairt state_pair=p->second;
586  unsigned int from=state_pair.first;
587  unsigned int to=state_pair.second;
588 
589  if(to==likely_next)
590  {
591  continue;
592  }
593 
594  // We're encoding the transition
595  //
596  // from -loc-> to
597  //
598  // which we encode by inserting:
599  //
600  // next_state=(state==from) ? to : next_state;
601  //
602  // just before loc.
603  equal_exprt guard(state, from_integer(from, state.type()));
604  if_exprt rhs(guard, from_integer(to, next_state.type()), next_state);
605  state_machine.assign(next_state, rhs);
606  }
607 
608  // Update the state and assume(false) if we've hit an accept state.
609  state_machine.assign(state, next_state);
610 
611  for(state_sett::iterator it=accept_states.begin();
612  it!=accept_states.end();
613  ++it)
614  {
615  state_machine.assume(
616  not_exprt(equal_exprt(state, from_integer(*it, state.type()))));
617  }
618 }
619 
621 {
622  int num_accelerated=0;
623 
624  for(natural_loops_mutablet::loop_mapt::iterator it =
625  natural_loops.loop_map.begin();
626  it!=natural_loops.loop_map.end();
627  ++it)
628  {
629  goto_programt::targett t=it->first;
630  num_accelerated += accelerate_loop(t);
631  }
632 
633  program.update();
634 
635  if(num_accelerated > 0)
636  {
637  std::cout << "Engaging crush mode...\n";
638 
639  restrict_traces();
640  // add_dirty_checks();
641  program.update();
642 
643  std::cout << "Crush mode engaged.\n";
644  }
645 
646  return num_accelerated;
647 }
648 
650  goto_modelt &goto_model,
652  bool use_z3)
653 {
654  Forall_goto_functions(it, goto_model.goto_functions)
655  {
656  std::cout << "Accelerating function " << it->first << '\n';
657  acceleratet accelerate(
658  it->second.body, goto_model, message_handler, use_z3);
659 
660  int num_accelerated=accelerate.accelerate_loops();
661 
662  if(num_accelerated > 0)
663  {
664  std::cout << "Added " << num_accelerated
665  << " accelerator(s)\n";
666  }
667  }
668 }
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:482
Loop Acceleration.
void update()
Update all indices.
Boolean negation.
Definition: std_expr.h:3228
void make_overflow_loc(goto_programt::targett loop_header, goto_programt::targett &loop_end, goto_programt::targett &overflow_loc)
Definition: accelerate.cpp:234
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:3359
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:323
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:354
void accelerate_functions(goto_modelt &goto_model, message_handlert &message_handler, bool use_z3)
Definition: accelerate.cpp:649
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:4486
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:254
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:420
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:1340
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.
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:33
std::set< statet > state_sett
The boolean constant false.
Definition: std_expr.h:4497
symbolt make_symbol(std::string name, typet type)
Definition: accelerate.cpp:446
goto_programt::targett find_back_jump(goto_programt::targett loop_header)
Definition: accelerate.cpp:34
const source_locationt & source_location() const
Definition: type.h:97
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:460
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:3950
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:523
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:283
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:620
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:136
Assignment.
Definition: std_code.h:196