Mixing code and data, Self-modifying code, Mixing code and data -47 – Intel ARCHITECTURE IA-32 User Manual

Page 119: Self-modifying code -47

Advertising
background image

General Optimization Guidelines

2

2-47

Mixing Code and Data

The Pentium 4 processor’s aggressive prefetching and pre-decoding of
instructions has two related effects:

Self-modifying code works correctly, according to the Intel
architecture processor requirements, but incurs a significant
performance penalty. Avoid self-modifying code.

Placing writable data in the code segment might be impossible to
distinguish from self-modifying code. Writable data in the code
segment might suffer the same performance penalty as
self-modifying code.

Assembly/Compiler Coding Rule 26. (M impact, L generality) If (hopefully
read-only) data must occur on the same page as code, avoid placing it
immediately after an indirect jump. For example, follow an indirect jump with
its mostly likely target, and place the data after an unconditional branch.

Tuning Suggestion 1. In rare cases, a performance problem may be noted due
to executing data on a code page as instructions. The condition where this is
very likely to happen is when execution is following an indirect branch that is
not resident in the trace cache. If a performance problem is clearly due to this
cause, try moving the data elsewhere or inserting an illegal opcode or a

pause

instruction immediately following the indirect branch. The latter two
alternatives may degrade performance in some circumstances.

Assembly/Compiler Coding Rule 27. (H impact, L generality) Always put
code and data on separate pages. Avoid self-modifying code wherever possible.
If code is to be modified, try to do it all at once and make sure the code that
performs the modifications and the code being modified are on separate 4 KB
pages or on separate aligned 1 KB subpages.

Self-modifying Code

Self-modifying code (SMC) that ran correctly on Pentium III processors
and prior implementations will run correctly on subsequent
implementations, including Pentium 4 and Intel Xeon processors. SMC

Advertising