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
113  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 
156  goto_programt::instructiont skip(SKIP);
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,
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  jump->guard=side_effect_expr_nondett(bool_typet());
217  jump->targets.push_back(loop_body);
218 
219  program.destructive_insert(loop_body, looping_path);
220 
221  jump=program.insert_before(loop_body);
222  jump->make_goto();
223  jump->guard=true_exprt();
224  jump->targets.push_back(back_jump);
225 
226  for(goto_programt::targett t=loop_header;
227  t!=loop_body;
228  ++t)
229  {
230  inserted_path.push_back(path_nodet(t));
231  }
232 
233  inserted_path.push_back(path_nodet(back_jump));
234 }
235 
237  goto_programt::targett loop_header,
238  goto_programt::targett &loop_end,
239  goto_programt::targett &overflow_loc)
240 {
241  symbolt overflow_sym=utils.fresh_symbol("accelerate::overflow", bool_typet());
242  const exprt &overflow_var=overflow_sym.symbol_expr();
244  natural_loops.loop_map[loop_header];
245  overflow_instrumentert instrumenter(program, overflow_var, symbol_table);
246 
247  for(natural_loops_mutablet::natural_loopt::iterator it=loop.begin();
248  it!=loop.end();
249  ++it)
250  {
253 
254  instrumenter.add_overflow_checks(*it, added);
255  loop.insert(added.begin(), added.end());
256  }
257 
259  t->make_assignment();
260  t->code=code_assignt(overflow_var, false_exprt());
261  t->swap(*loop_header);
262  loop.insert(t);
263  overflow_locs[loop_header].push_back(t);
264 
265  goto_programt::instructiont s(SKIP);
266  overflow_loc=program.insert_after(loop_end);
267  *overflow_loc=s;
268  overflow_loc->swap(*loop_end);
269  loop.insert(overflow_loc);
270 
271  goto_programt::instructiont g(GOTO);
272  g.guard=not_exprt(overflow_var);
273  g.targets.push_back(overflow_loc);
275  *t2=g;
276  t2->swap(*loop_end);
277  overflow_locs[overflow_loc].push_back(t2);
278  loop.insert(t2);
279 
280  goto_programt::targett tmp=overflow_loc;
281  overflow_loc=loop_end;
282  loop_end=tmp;
283 }
284 
286 {
287  trace_automatont automaton(program);
288 
289  for(subsumed_pathst::iterator it=subsumed.begin();
290  it!=subsumed.end();
291  ++it)
292  {
293  if(!it->subsumed.empty())
294  {
295 #ifdef DEBUG
297  std::cout << "Restricting path:\n";
298  output_path(it->subsumed, program, ns, std::cout);
299 #endif
300 
301  automaton.add_path(it->subsumed);
302  }
303 
304  patht double_accelerator;
305  patht::iterator jt=double_accelerator.begin();
306  double_accelerator.insert(
307  jt, it->accelerator.begin(), it->accelerator.end());
308  double_accelerator.insert(
309  jt, it->accelerator.begin(), it->accelerator.end());
310 
311 #ifdef DEBUG
313  std::cout << "Restricting path:\n";
314  output_path(double_accelerator, program, ns, std::cout);
315 #endif
316  automaton.add_path(double_accelerator);
317  }
318 
319  std::cout << "Building trace automaton...\n";
320 
321  automaton.build();
322  insert_automaton(automaton);
323 }
324 
326 {
327  for(std::set<exprt>::iterator it=accelerator.dirty_vars.begin();
328  it!=accelerator.dirty_vars.end();
329  ++it)
330  {
331  expr_mapt::iterator jt=dirty_vars_map.find(*it);
332  exprt dirty_var;
333 
334  if(jt==dirty_vars_map.end())
335  {
337  symbolt new_sym=utils.fresh_symbol("accelerate::dirty", bool_typet());
338  dirty_var=new_sym.symbol_expr();
339  dirty_vars_map[*it]=dirty_var;
340  }
341  else
342  {
343  dirty_var=jt->second;
344  }
345 
346 #ifdef DEBUG
347  std::cout << "Setting dirty flag " << expr2c(dirty_var, ns)
348  << " for " << expr2c(*it, ns) << '\n';
349 #endif
350 
351  accelerator.pure_accelerator.add_instruction(ASSIGN)->code =
352  code_assignt(dirty_var, true_exprt());
353  }
354 }
355 
357 {
358  for(expr_mapt::iterator it=dirty_vars_map.begin();
359  it!=dirty_vars_map.end();
360  ++it)
361  {
362  goto_programt::instructiont assign(ASSIGN);
363  assign.code=code_assignt(it->second, false_exprt());
365  }
366 
368 
370  it!=program.instructions.end();
371  it=next)
372  {
373  next=it;
374  ++next;
375 
376  // If this is an assign to a tracked variable, clear the dirty flag.
377  // Note: this order of insertions means that we assume each of the read
378  // variables is clean _before_ clearing any dirty flags.
379  if(it->is_assign())
380  {
381  exprt &lhs=it->code.op0();
382  expr_mapt::iterator dirty_var=dirty_vars_map.find(lhs);
383 
384  if(dirty_var!=dirty_vars_map.end())
385  {
386  goto_programt::instructiont clear_flag(ASSIGN);
387  clear_flag.code=code_assignt(dirty_var->second, false_exprt());
388  program.insert_before_swap(it, clear_flag);
389  }
390  }
391 
392  // Find which symbols are read, i.e. those appearing in a guard or on
393  // the right hand side of an assignment. Assume each is not dirty.
394  find_symbols_sett read;
395 
396  find_symbols(it->guard, read);
397 
398  if(it->is_assign())
399  {
400  find_symbols(it->code.op1(), read);
401  }
402 
403  for(find_symbols_sett::iterator jt=read.begin();
404  jt!=read.end();
405  ++jt)
406  {
407  const exprt &var=ns.lookup(*jt).symbol_expr();
408  expr_mapt::iterator dirty_var=dirty_vars_map.find(var);
409 
410  if(dirty_var==dirty_vars_map.end())
411  {
412  continue;
413  }
414 
415  goto_programt::instructiont not_dirty(ASSUME);
416  not_dirty.guard=not_exprt(dirty_var->second);
417  program.insert_before_swap(it, not_dirty);
418  }
419  }
420 }
421 
423 {
424  for(std::set<exprt>::iterator it=accelerator.dirty_vars.begin();
425  it!=accelerator.dirty_vars.end();
426  ++it)
427  {
428  if(it->id()==ID_symbol && it->type() == bool_typet())
429  {
430  const irep_idt &id=to_symbol_expr(*it).get_identifier();
431  const symbolt &sym=symbol_table.lookup(id);
432 
433  if(sym.module=="scratch")
434  {
435  continue;
436  }
437  }
438 
439 #ifdef DEBUG
440  std::cout << "Underapproximate variable: " << expr2c(*it, ns) << '\n';
441 #endif
442  return true;
443  }
444 
445  return false;
446 }
447 
448 symbolt acceleratet::make_symbol(std::string name, typet type)
449 {
450  symbolt ret;
451  ret.module="accelerate";
452  ret.name=name;
453  ret.base_name=name;
454  ret.pretty_name=name;
455  ret.type=type;
456 
457  symbol_table.add(ret);
458 
459  return ret;
460 }
461 
463 {
464  return;
465 
467  code_declt code(sym);
468 
469  decl->make_decl();
470  decl->code=code;
471 }
472 
474 {
475  decl(sym, t);
476 
478  code_assignt code(sym, init);
479 
480  assign->make_assignment();
481  assign->code=code;
482 }
483 
485 {
486  symbolt state_sym=make_symbol("trace_automaton::state",
488  symbolt next_state_sym=make_symbol("trace_automaton::next_state",
490  symbol_exprt state=state_sym.symbol_expr();
491  symbol_exprt next_state=next_state_sym.symbol_expr();
492 
493  trace_automatont::sym_mapt transitions;
494  state_sett accept_states;
495 
496  automaton.get_transitions(transitions);
497  automaton.accept_states(accept_states);
498 
499  std::cout
500  << "Inserting trace automaton with "
501  << automaton.num_states() << " states, "
502  << accept_states.size() << " accepting states and "
503  << transitions.size() << " transitions\n";
504 
505  // Declare the variables we'll use to encode the state machine.
507  decl(state, t, from_integer(automaton.init_state(), state.type()));
508  decl(next_state, t);
509 
510  // Now for each program location that appears as a symbol in the
511  // trace automaton, add the appropriate code to drive the state
512  // machine.
513  for(const auto &sym : automaton.alphabet)
514  {
515  scratch_programt state_machine(symbol_table);
516  trace_automatont::sym_range_pairt p=transitions.equal_range(sym);
517 
518  build_state_machine(p.first, p.second, accept_states, state, next_state,
519  state_machine);
520 
521  program.insert_before_swap(sym, state_machine);
522  }
523 }
524 
526  trace_automatont::sym_mapt::iterator begin,
527  trace_automatont::sym_mapt::iterator end,
528  state_sett &accept_states,
529  symbol_exprt state,
530  symbol_exprt next_state,
531  scratch_programt &state_machine)
532 {
533  std::map<unsigned int, unsigned int> successor_counts;
534  unsigned int max_count=0;
535  unsigned int likely_next=0;
536 
537  // Optimisation: find the most common successor state and initialise
538  // next_state to that value. This reduces the size of the state machine
539  // driver substantially.
540  for(trace_automatont::sym_mapt::iterator p=begin; p!=end; ++p)
541  {
542  trace_automatont::state_pairt state_pair=p->second;
543  unsigned int to=state_pair.second;
544  unsigned int count=0;
545 
546  if(successor_counts.find(to)==successor_counts.end())
547  {
548  count=1;
549  }
550  else
551  {
552  count=successor_counts[to] + 1;
553  }
554 
555  successor_counts[to]=count;
556 
557  if(count > max_count)
558  {
559  max_count=count;
560  likely_next=to;
561  }
562  }
563 
564  // Optimisation: if there is only one possible successor state, just
565  // jump straight to it instead of driving the whole machine.
566  if(successor_counts.size()==1)
567  {
568  if(accept_states.find(likely_next)!=accept_states.end())
569  {
570  // It's an accept state. Just assume(false).
571  state_machine.assume(false_exprt());
572  }
573  else
574  {
575  state_machine.assign(state,
576  from_integer(likely_next, next_state.type()));
577  }
578 
579  return;
580  }
581 
582  state_machine.assign(next_state,
583  from_integer(likely_next, next_state.type()));
584 
585  for(trace_automatont::sym_mapt::iterator p=begin; p!=end; ++p)
586  {
587  trace_automatont::state_pairt state_pair=p->second;
588  unsigned int from=state_pair.first;
589  unsigned int to=state_pair.second;
590 
591  if(to==likely_next)
592  {
593  continue;
594  }
595 
596  // We're encoding the transition
597  //
598  // from -loc-> to
599  //
600  // which we encode by inserting:
601  //
602  // next_state=(state==from) ? to : next_state;
603  //
604  // just before loc.
605  equal_exprt guard(state, from_integer(from, state.type()));
606  if_exprt rhs(guard, from_integer(to, next_state.type()), next_state);
607  state_machine.assign(next_state, rhs);
608  }
609 
610  // Update the state and assume(false) if we've hit an accept state.
611  state_machine.assign(state, next_state);
612 
613  for(state_sett::iterator it=accept_states.begin();
614  it!=accept_states.end();
615  ++it)
616  {
617  state_machine.assume(
618  not_exprt(equal_exprt(state, from_integer(*it, state.type()))));
619  }
620 }
621 
623 {
624  int num_accelerated=0;
625 
626  for(natural_loops_mutablet::loop_mapt::iterator it =
627  natural_loops.loop_map.begin();
628  it!=natural_loops.loop_map.end();
629  ++it)
630  {
631  goto_programt::targett t=it->first;
632  num_accelerated += accelerate_loop(t);
633  }
634 
635  program.update();
636 
637  if(num_accelerated > 0)
638  {
639  std::cout << "Engaging crush mode...\n";
640 
641  restrict_traces();
642  // add_dirty_checks();
643  program.update();
644 
645  std::cout << "Crush mode engaged.\n";
646  }
647 
648  return num_accelerated;
649 }
650 
651 
653  goto_functionst &functions,
655  bool use_z3)
656 {
657  Forall_goto_functions(it, functions)
658  {
659  std::cout << "Accelerating function " << it->first << '\n';
660  acceleratet accelerate(it->second.body, functions, symbol_table, use_z3);
661 
662  int num_accelerated=accelerate.accelerate_loops();
663 
664  if(num_accelerated > 0)
665  {
666  std::cout << "Added " << num_accelerated
667  << " accelerator(s)\n";
668  }
669  }
670 }
void get_transitions(sym_mapt &transitions)
expr_mapt dirty_vars_map
Definition: accelerate.h:114
The type of an expression.
Definition: type.h:20
irep_idt name
The unique identifier.
Definition: symbol.h:46
void insert_automaton(trace_automatont &automaton)
Definition: accelerate.cpp:484
Loop Acceleration.
Boolean negation.
Definition: std_expr.h:2648
void make_overflow_loc(goto_programt::targett loop_header, goto_programt::targett &loop_end, goto_programt::targett &overflow_loc)
Definition: accelerate.cpp:236
void update()
Update all indices.
virtual bool lookup(const irep_idt &name, const symbolt *&symbol) const
Definition: namespace.cpp:139
symbolt & lookup(const irep_idt &identifier)
Find a symbol in the symbol table.
targett add_instruction()
Adds an instruction at the end.
targett assign(const exprt &lhs, const exprt &rhs)
natural_loops_mutablet natural_loops
Definition: accelerate.h:106
static const int accelerate_limit
Definition: accelerate.h:53
exprt & op0()
Definition: expr.h:84
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:103
patht residue
Definition: subsumed.h:30
bool contains_nested_loops(goto_programt::targett &loop_header)
Definition: accelerate.cpp:59
instructionst instructions
The list of instructions in the goto program.
const irep_idt & get_identifier() const
Definition: std_expr.h:120
Goto Programs with Functions.
int accelerate_loop(goto_programt::targett &loop_header)
Definition: accelerate.cpp:89
The trinary if-then-else operator.
Definition: std_expr.h:2771
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:49
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:58
typet & type()
Definition: expr.h:60
void accept_states(state_sett &states)
void set_dirty_vars(path_acceleratort &accelerator)
Definition: accelerate.cpp:325
The proper Booleans.
Definition: std_types.h:33
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:33
subsumed_pathst subsumed
Definition: accelerate.h:107
std::pair< statet, statet > state_pairt
symbol_tablet & symbol_table
Definition: accelerate.h:104
std::unordered_set< irep_idt, irep_id_hash > find_symbols_sett
Definition: find_symbols.h:20
goto_programt overflow_path
Definition: accelerator.h:64
equality
Definition: std_expr.h:1082
void add_dirty_checks()
Definition: accelerate.cpp:356
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:191
The boolean constant true.
Definition: std_expr.h:3742
unsignedbv_typet unsigned_poly_type()
Definition: util.cpp:25
patht accelerator
Definition: subsumed.h:29
A declaration of a local variable.
Definition: std_code.h:192
targett assume(const exprt &guard)
symbolt fresh_symbol(std::string base, typet type)
goto_programt & program
Definition: accelerate.h:102
Loop Acceleration.
API to expression classes.
std::list< path_nodet > patht
Definition: path.h:45
The symbol table.
Definition: symbol_table.h:52
TO_BE_DOCUMENTED.
Definition: namespace.h:62
bool is_underapproximate(path_acceleratort &accelerator)
Definition: accelerate.cpp:422
A side effect that returns a non-deterministically chosen value.
Definition: std_code.h:1037
std::list< patht > pathst
Definition: path.h:46
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.
targett insert_after(const_targett target)
Insertion after the given target.
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
std::set< statet > state_sett
The boolean constant false.
Definition: std_expr.h:3753
symbolt make_symbol(std::string name, typet type)
Definition: accelerate.cpp:448
targett insert_before(const_targett target)
Insertion before the given target.
goto_programt::targett find_back_jump(goto_programt::targett loop_header)
Definition: accelerate.cpp:34
A specialization of goto_program_templatet over goto programs in which instructions have codet type...
Definition: goto_program.h:24
Loop Acceleration.
void decl(symbol_exprt &sym, goto_programt::targett t)
Definition: accelerate.cpp:462
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:37
std::multimap< goto_programt::targett, state_pairt > sym_mapt
std::set< exprt > dirty_vars
Definition: accelerator.h:66
Base class for all expressions.
Definition: expr.h:46
overflow_mapt overflow_locs
Definition: accelerate.h:112
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:52
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast a generic exprt to a symbol_exprt.
Definition: std_expr.h:202
#define Forall_goto_functions(it, functions)
goto_programt pure_accelerator
Definition: accelerator.h:63
void insert_before_swap(targett target)
Insertion that preserves jumps to "target".
std::string expr2c(const exprt &expr, const namespacet &ns)
Definition: expr2c.cpp:3874
void accelerate_functions(goto_functionst &functions, symbol_tablet &symbol_table, bool use_z3)
Definition: accelerate.cpp:652
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:525
void destructive_insert(const_targett target, goto_program_templatet< codeT, guardT > &p)
Inserts the given program at the given location.
Expression to hold a symbol (variable)
Definition: std_expr.h:82
void restrict_traces()
Definition: accelerate.cpp:285
Compute natural loops in a goto_function.
int accelerate_loops()
Definition: accelerate.cpp:622
acceleration_utilst utils
Definition: accelerate.h:108
namespacet ns
Definition: accelerate.h:105
Loop Acceleration.
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
void find_symbols(const exprt &src, find_symbols_sett &dest)
Assignment.
Definition: std_code.h:144