Project

General

Profile

Actions

Feature #9985

closed

Fold temporary variables back

Added by Evgeny Novikov about 5 years ago. Updated almost 5 years ago.

Status:
Closed
Priority:
High
Start date:
12/13/2019
Due date:
% Done:

0%

Estimated time:
Published in build:

Description

To simplify complex expressions that contain function calls CIL introduces temporary variables. This is good for internals but not so good for users of outputted code.


Files

hwa-rc.zip (239 KB) hwa-rc.zip Evgeny Novikov, 01/16/2020 08:11 PM
pata_arasan_cf.zip (251 KB) pata_arasan_cf.zip Evgeny Novikov, 01/16/2020 08:18 PM
i7300_idle.i.zip (201 KB) i7300_idle.i.zip Evgeny Novikov, 01/17/2020 04:32 PM

Related issues 2 (0 open2 closed)

Blocks Klever - Feature #9825: Get rid of temporary variables and their removingClosedEvgeny Novikov09/18/2019

Actions
Blocks Klever - Feature #9906: Update CILClosedEvgeny Novikov11/08/2019

Actions
Actions #1

Updated by Evgeny Novikov about 5 years ago

  • Blocks Feature #9825: Get rid of temporary variables and their removing added
Actions #2

Updated by Evgeny Novikov almost 5 years ago

Actions #3

Updated by Evgeny Novikov almost 5 years ago

Is there any progress?

Actions #4

Updated by Mikhail Mandrykin almost 5 years ago

  • Status changed from New to Open
I implemented initial support for temporary variable inlining in framac|f459822c. It inlines definitions of immediately preceding temporary variables into the bodies of the immediately following expressions (without any side-effecting statements in between). However, the inlining is incomplete at least for the following cases:
  • Ternary operator _?_:_ is not supported and is rewritten into an ifthenelse with a temporary variable as before
  • Constant initializers still use temporary variables with the names of the form __const_expr#
  • Expression statements of the form { ...; result } still introduce a temporary variable

Many other constructions, notably, nested function calls, side-effecting expressions, and some type casts are successfully inlined. The inlining is activated with an option -fold-temp-vars. I'd also recommend to use it in conjunction with -no-single-return, -keep-logical-operators and -no-print-cil-as-is (the default) to make the resulting output as close as possible to the original source.

Actions #5

Updated by Evgeny Novikov almost 5 years ago

After running something like:

toplevel.opt "-no-autoload-plugins" "-no-findlib" "-kernel-log" "e:problem desc.txt" "-machdep" "gcc_x86_64" "-c11" "-kernel-warn-key" "CERT:MSC:38=active" "-remove-unused-inline-functions" "-remove-unused-static-functions" "-no-annot" "-no-single-return" "-fold-temp-vars" "-shrink-initializers" "-print-cil-as-is" "-keep-logical-operators" "-aggressive-merging" "-print" "-print-lines" "-no-print-annot" "-ocode" "cil.i" ...

on the code attached, I got invalid statements as a result:
  usb_fill_int_urb(hwarc->neep_urb,usb_dev,08 
tmp_1 = __create_pipe(usb_dev,(unsigned int)epd->bEndpointAddress) | 1073741952U,hwarc->rd_buffer,4096,& hwarc_neep_cb,(void *)hwarc,(int)epd->bInterval);

Actions #6

Updated by Evgeny Novikov almost 5 years ago

Another failure (perhaps due to the same reasons) is for the attached code:

#line 614 
  return  614 
tmp = devm_kmalloc(dev,size,gfp | 32768U);

Actions #7

Updated by Mikhail Mandrykin almost 5 years ago

Fixed in framac|7d70550d. This was due to -print-lines interfering with intermediate printing of nested expressions, the new option was not tested in this combination yet.

Actions #8

Updated by Evgeny Novikov almost 5 years ago

There are still temporary variables in such statements as "if (!func(...)) ...".

Actions #9

Updated by Evgeny Novikov almost 5 years ago

I attached an example where this issue exists.

Actions #10

Updated by Mikhail Mandrykin almost 5 years ago

Added a special case for int casts in conditional expressions in framac|918782e6. (int)cond was treated differently because CIL only supports logical expressions with side effects as conditions in if statements (note that there is no way to use A && B if B has a side-effect and we do not unfold temporary variables, but at the Cabs2cil stage this is not yet known and moreover can fail in general), in other contexts they are rewritten to temporaries as if cond then 1 else 0. With an additional cast the logical expression becomes nested into another expression and is rewritten with a temporary.

Actions #11

Updated by Evgeny Novikov almost 5 years ago

  • Status changed from Open to Closed

Despite some explicitly mentioned and expected cases the new option works perfectly. At last we see no temporary variables in Klever while there is no more an in-house dreadful code that folded them for violation witnesses.

Actions

Also available in: Atom PDF