goblint / cil

C Intermediate Language
https://goblint.github.io/cil/
Other
133 stars 20 forks source link

Avoid unnecessary temporary variable for initializers #163

Open sim642 opened 9 months ago

sim642 commented 9 months ago

CIL transforms

int foo() {
  return 42;
}

int main() {
  int x = foo();
  int y;
  y = foo();
  return 0;
}

into

int foo(void) 
{ 
  {
  return (42);
}
}

int main(void) 
{ 
  int x ;
  int tmp ;
  int y ;
  {
  tmp = foo();
  x = tmp;
  y = foo();
  return (0);
}
}

For x a temporary variable tmp is added, which is extra clutter compared to the equivalent code for y. The temporary might be needed in general for initializers, but we should find a way to avoid it in such simple cases because this is counterproductive: writing neater code (e.g. for Goblint tests) causes uglier CIL output (and CFGs). To get more compact Goblint representations, one has to explicitly split initializers into assignments.