CBMC
dfcc_check_loop_normal_form.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Dynamic frame condition checking for loop contracts
4 
5 Author: Qinheping Hu, qinhh@amazon.com
6 Author: Remi Delmas, delmasrd@amazon.com
7 
8 \*******************************************************************/
9 
11 
12 #include <analyses/natural_loops.h>
14 
16 {
17  natural_loops_mutablet natural_loops(goto_program);
18 
19  // instruction span for each loop
20  std::vector<std::pair<goto_programt::targett, goto_programt::targett>> spans;
21 
22  std::map<
26  latch_head_pairs;
27 
28  for(auto &loop : natural_loops.loop_map)
29  {
30  auto head = loop.first;
31  auto &loop_instructions = loop.second;
32 
33  if(loop_instructions.size() <= 1)
34  {
35  // Ignore single instruction loops of the form
36  // `LOOP: IF cond GOTO LOOP;`
37  continue;
38  }
39 
40  auto latch = head;
41  bool latch_found = false;
42 
43  // Find latch and check it is unique
44  for(const auto &t : loop_instructions)
45  {
46  if(t->is_goto() && t->get_target() == head)
47  {
48  if(latch_found)
49  {
50  log.error() << "Loop starting at:"
51  << "\n- head: " << head->source_location()
52  << "\nhas more than one latch instruction:"
53  << "\n- latch1: " << latch->source_location()
54  << "\n- latch2: " << t->source_location()
55  << messaget::eom;
56  throw analysis_exceptiont(
57  "Found loop with more than one latch instruction");
58  }
59  latch = t;
60  latch_found = true;
61  }
62  }
63 
64  INVARIANT(latch_found, "Natural loop latch found");
65 
66  // Check that instruction spans are not overlapping
67  for(const auto &span : spans)
68  {
69  bool head_in_span =
70  span.first->location_number <= head->location_number &&
71  head->location_number <= span.second->location_number;
72 
73  bool latch_in_span =
74  span.first->location_number <= latch->location_number &&
75  latch->location_number <= span.second->location_number;
76 
77  if(head_in_span != latch_in_span)
78  {
79  log.error() << "Loop spanning:"
80  << "\n- head: " << head->source_location()
81  << "\n- latch: " << latch->source_location()
82  << "\noverlaps loop spanning:"
83  << "\n- head: " << span.first->source_location()
84  << "\n- latch: " << span.second->source_location()
85  << messaget::eom;
86  throw analysis_exceptiont(
87  "Found loops with overlapping instruction spans");
88  }
89  }
90 
91  spans.push_back({head, latch});
92 
93  // Check that:
94  // 1. all loop instructions are in the range [head, latch]
95  // 2. except for the head instruction, no incoming edge from outside the
96  // loop
97  for(const auto &i : loop_instructions)
98  {
99  if(
100  i->location_number < head->location_number ||
101  i->location_number > latch->location_number)
102  {
103  log.error() << "Loop body instruction at:"
104  << "\n- " << i->source_location() << "\nis outside of"
105  << "\n- head: " << head->source_location()
106  << "\n- latch: " << latch->source_location()
107  << messaget::eom;
108  throw analysis_exceptiont(
109  "Found loop body instruction outside of the [head, latch] "
110  "instruction span");
111  }
112 
113  for(const auto &from : i->incoming_edges)
114  {
115  if(i != head && !loop_instructions.contains(from))
116  {
117  log.error() << "Loop body instruction at:"
118  << "\n- " << i->source_location()
119  << "\n has incoming edge from outside the loop at:"
120  << "\n- " << from->source_location() << messaget::eom;
121 
122  throw analysis_exceptiont(
123  "Found loop body instruction with incoming edge from outside the "
124  "loop");
125  }
126  }
127  }
128 
129  // Check if a loop contains another loop's head (resp. latch) then
130  // it also contains the latch (resp. head)
131  for(auto &other_loop : natural_loops.loop_map)
132  {
133  auto &other_loop_instructions = other_loop.second;
134  bool contains_head = other_loop_instructions.contains(head);
135  bool contains_latch = other_loop_instructions.contains(latch);
136  INVARIANT(
137  contains_head == contains_latch,
138  "nested loop head and latch are in outer loop");
139  }
140 
141  if(!latch_head_pairs.emplace(latch, head).second)
142  UNREACHABLE;
143  }
144 
145  // all checks passed, now we perform some instruction rewriting
146  for(auto &entry_pair : latch_head_pairs)
147  {
148  auto latch = entry_pair.first;
149  auto head = entry_pair.second;
150 
151  // Convert conditional latch into exiting + unconditional latch.
152  // ```
153  // IF g THEN GOTO HEAD
154  // --------------------------
155  // IF !g THEN GOTO EXIT
156  // GOTO HEAD
157  // EXIT: SKIP
158  // ```
159  if(latch->has_condition() && !latch->condition().is_true())
160  {
161  const source_locationt &loc = latch->source_location();
162  const auto &exit =
163  goto_program.insert_after(latch, goto_programt::make_skip(loc));
164 
166  goto_program,
167  latch,
169  exit, not_exprt(latch->condition()), latch->source_location()));
170 
171  // CAUTION: The condition expression needs to be updated in place because
172  // this is where loop contract clauses are stored as extra ireps.
173  // Overwriting this expression with a fresh expression will also lose the
174  // loop contract.
175  exprt latch_condition = latch->condition();
176  latch_condition.set(ID_value, ID_true);
177  *latch = goto_programt::make_goto(head, latch_condition, loc);
178  }
179 
180  // The latch node is now an unconditional jump.
181 
182  // insert a SKIP pre-head node and reroute all incoming edges to that node,
183  // except for edge coming from the latch.
185  goto_program, head, goto_programt::make_skip(head->source_location()));
186  latch->set_target(head);
187  }
188  goto_program.update();
189 }
Thrown when an unexpected error occurs during the analysis (e.g., when the SAT solver returns an erro...
Base class for all expressions.
Definition: expr.h:56
const source_locationt & source_location() const
Definition: expr.h:231
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:73
void update()
Update all indices.
instructionst::iterator targett
Definition: goto_program.h:614
static instructiont make_skip(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:891
targett insert_after(const_targett target)
Insertion after the instruction pointed-to by the given instruction iterator target.
Definition: goto_program.h:708
static instructiont make_goto(targett _target, const source_locationt &l=source_locationt::nil())
void set(const irep_idt &name, const irep_idt &value)
Definition: irep.h:408
loop_mapt loop_map
Definition: loop_analysis.h:88
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
static eomt eom
Definition: message.h:297
Boolean negation.
Definition: std_expr.h:2327
void dfcc_check_loop_normal_form(goto_programt &goto_program, messaget &log)
Checks and enforces normal form properties for natural loops of the given goto_program.
Checks normal form properties of natural loops in a GOTO program.
double log(double x)
Definition: math.c:2776
Compute natural loops in a goto_function.
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:525
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
void exit(int status)
Definition: stdlib.c:102
A total order over targett and const_targett.
Definition: goto_program.h:392
void insert_before_and_update_jumps(goto_programt &destination, goto_programt::targett &target, const goto_programt::instructiont &i)
Insert a goto instruction before a target instruction iterator and update targets of all jumps that p...
Definition: utils.cpp:244