Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an inline assembly (__asm) block prevent function optimisation?

When using Microsoft Visual C++ (not CLI, just standard native C++) does inline assembly cause optimisation to be disabled for the function?

When I checked using IDA, some of the function code outside the block does seem to change, but I'm not sure what the cause is. The function is (relatively) simple, containing bit manipulation and some math, but no external calls.

Other possible causes I could think of are:

  • Extra provisioning to enable debugging in inline assembly.
  • Alterations to exception handling (doesn't really look like this, though)
  • Partial inlining of something else due to some unknown compiler logic.

Any ideas?

like image 877
Polynomial Avatar asked Apr 27 '12 20:04

Polynomial


People also ask

What is the use of inline assembly?

The uses of inline assembly include: Writing functions in assembly language. Spot-optimizing speed-critical sections of code. Making direct hardware access for device drivers.

Does GCC support inline assembly?

GCC provides two forms of inline asm statements. A basic asm statement is one with no operands (see Basic Asm), while an extended asm statement (see Extended Asm) includes one or more operands.


1 Answers

Yes. See the MSDN articles: Optimizing Inline Assembly as well as Advantages of Inline Assembly

From the article:

The presence of an __asm block in a function affects optimization in several ways. First, the compiler doesn't try to optimize the __asm block itself. What you write in assembly language is exactly what you get. Second, the presence of an __asm block affects register variable storage. The compiler avoids enregistering variables across an __asm block if the register's contents would be changed by the __asm block. Finally, some other function-wide optimizations will be affected by the inclusion of assembly language in a function.

like image 114
dirkgently Avatar answered Oct 03 '22 15:10

dirkgently