Definition of problem
CODESYS is a hardware-independent programming system for programmable logic controllers. The IEC 61131-3-compliant programming languages used are translated into processor-specific code. There are currently code generator backends for more than 10 CPUs for CODESYS.
Today's CPUs have deep pipelines to load and process commands long before they are actually executed. In conditional jumps, these CPUs use simple heuristics to determine which path is the most likely to be processed.
Incorrect heuristics can lead to very poor performance because the pipeline may have to be completely purged and refilled.
An efficient compiler can exploit these properties of a CPU to generate faster code when the probability of a condition being met is known. It would be very helpful to implement a directive that allows the programmer to set the probability of a condition. For example, this kind of directive could look something like this:
IF (__UNLIKELY(x > 100000)) THEN
LOG_Message(‘Variable too big’);
ELSIF (__LIKELY(x < 100)) THEN
// do this
ELSE
// do that
END_IF
In addition to IF-ELSIF statements, there are also conditional calls, conditional jumps, and conditional return statements in the graphical languages of IEC 61131-3. These should also be optimized with the __LIKELY directive.