[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Lightning] addi_i with non-constant immediate operand

From: Frank Bi
Subject: [Lightning] addi_i with non-constant immediate operand
Date: Sun, 24 Dec 2006 23:51:19 +0800
User-agent: Mutt/

Hello all,

I found that GNU lightning causes gcc to generate a lot of extra code on the
x86 for the addi_i instruction -- and possibly others -- when the immediate
operand is not a constant -- the huge amount of code is meant to decide
whether to emit the shorter machine code sequence (e.g. 83 c0 xx) or the
longer one (e.g. 05 xx xx xx xx). Here is a program exhibiting this problem:

        #include <stdio.h>
        #include <stdlib.h>
        #include <time.h>
        #include "lightning.h"

        static jit_insn codeBuffer[1024];
        typedef int (*pifi)(int);

        int main(void)
                pifi curry = (pifi) (jit_set_ip(codeBuffer).iptr);
                int in, rand_val;
                rand_val = rand();
                in = jit_arg_i();
                jit_getarg_i(JIT_R0, in);
                jit_addi_i(JIT_RET, JIT_R0, rand_val);
                jit_flush_code(codeBuffer, jit_get_ip().ptr);
                printf("%d + %d = %d\n", 5, rand_val, curry(5));
                return 0;

To solve this, I propose to add the following lines in lightning/i386/asm.h,
after the definitions of _Os, _sW, and _sL:

        #ifdef __GNUC__
        #   undef _Os
        #   define _Os(op, b) \
                        (__builtin_constant_p(b) && _s8P(b) ? \
                                _jit_B((op) | _b10) : \
        #   undef _sW
        #   define _sW(w) \
                        (__builtin_constant_p(w) && _s8P(w) ? \
                                _jit_B(w) : \
        #   undef _sL
        #   define _sL(l) \
                        (__builtin_constant_p(l) && _s8P(l) ? \
                                _jit_B(l) : \

This will cause GNU lightning to always emit the long form of the x86 add
instruction when the immediate operand cannot be optimized into a constant.
How does this look?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]