------- Comment #6 from jakub at gcc dot gnu dot org 2007-05-29 11:42 ------- Seems there were 2 separate bugs that are causing this miscompilation. 1) common_type (in contemporary gcc's common_pointer_type) will for the type of the whole conditional expression use pointer to attribute const function rather than non-const: int fn1 (void); int fn2 (void) __attribute__((const)); ... (cond ? fn1 : fn2) I'd say that's an unfortunate effect from representing __attribute__((const)) as TREE_READONLY on the FUNCTION_TYPE rather than as an attribute. Say for cond expr where one ptr target is volatile and the other is not the result needs to be ptr to volatile and similarly for say int *ptr1; const int *ptr2; cond ? ptr1 : ptr2 where the result should be pointer to const, a conservative choice. But for __attribute__((const)) functions the conservative choise is the exact opposite, if I have (cond ? fn1 : fn2) one of the functions isn't const and therefore nothing should assume the whole thing is pointer to __attribute__((const)) function. gcc trunk handles this the same way.
2) because of 1), but also e.g. on typedef struct rtx_def *rtx; enum machine_mode { VOIDmode = 0, DImode = 13 }; extern rtx gen_rtx_CONST_INT (enum machine_mode, long); extern rtx gen_x86_shld_1 (rtx, rtx, rtx) __attribute__((const)); extern rtx gen_x86_64_shld (rtx, rtx, rtx) __attribute__((const)); extern rtx emit_insn (rtx); void foo (rtx *high, rtx *low, enum machine_mode mode, int count) { emit_insn ((mode == DImode ? gen_x86_shld_1 : gen_x86_64_shld) (high[0], low[0], gen_rtx_CONST_INT (VOIDmode, count))); } where 1) doesn't apply a properly emitted expanded COND_EXPR into rtl is passed through emit_libcall_block which changes: (insn 31 0 32 (set (reg:CCZ 17 flags) (compare:CCZ (mem/f:SI (plus:DI (reg/f:DI 54 virtual-stack-vars) (const_int -20 [0xffffffffffffffec])) [0 mode+0 S4 A32]) (const_int 13 [0xd]))) -1 (nil) (nil)) (jump_insn 32 31 34 (set (pc) (if_then_else (ne (reg:CCZ 17 flags) (const_int 0 [0x0])) (label_ref 37) (pc))) -1 (nil) (nil)) (insn 34 32 35 (set (reg:DI 63) (symbol_ref:DI ("gen_x86_shld_1"))) -1 (nil) (nil)) (jump_insn 35 34 36 (set (pc) (label_ref 40)) -1 (nil) (nil)) (barrier 36 35 37) (code_label 37 36 39 2 "" "" [0 uses]) (insn 39 37 40 (set (reg:DI 63) (symbol_ref:DI ("gen_x86_64_shld"))) -1 (nil) (nil)) (code_label 40 39 42 3 "" "" [0 uses]) (insn 42 40 44 (set (reg:DI 1 rdx) (reg:DI 60)) -1 (nil) (nil)) (insn 44 42 46 (set (reg:DI 4 rsi) (mem:DI (reg/f:DI 61) [0 S8 A64])) -1 (nil) (nil)) (insn 46 44 47 (set (reg:DI 5 rdi) (mem:DI (reg/f:DI 62) [0 S8 A64])) -1 (nil) (nil)) (call_insn/u 47 46 0 (set (reg:DI 0 rax) (call (mem:QI (reg:DI 63) [0 S1 A8]) (const_int 0 [0x0]))) -1 (nil) (nil) (expr_list (use (reg:DI 5 rdi)) (expr_list (use (reg:DI 4 rsi)) (expr_list (use (reg:DI 1 rdx)) (nil))))) into (insn 34 30 39 (set (reg:DI 63) (symbol_ref:DI ("gen_x86_shld_1"))) -1 (nil) (nil)) (insn 39 34 31 (set (reg:DI 63) (symbol_ref:DI ("gen_x86_64_shld"))) -1 (nil) (nil)) (insn 31 39 32 (set (reg:CCZ 17 flags) (compare:CCZ (mem/f:SI (plus:DI (reg/f:DI 54 virtual-stack-vars) (const_int -20 [0xffffffffffffffec])) [0 mode+0 S4 A32]) (const_int 13 [0xd]))) -1 (nil) (nil)) (jump_insn 32 31 35 (set (pc) (if_then_else (ne (reg:CCZ 17 flags) (const_int 0 [0x0])) (label_ref 37) (pc))) -1 (nil) (nil)) (jump_insn 35 32 36 (set (pc) (label_ref 40)) -1 (nil) (nil)) (insn 34 30 39 (set (reg:DI 63) (symbol_ref:DI ("gen_x86_shld_1"))) -1 (nil) (nil)) (insn 39 34 31 (set (reg:DI 63) (symbol_ref:DI ("gen_x86_64_shld"))) -1 (nil) (nil)) (insn 31 39 32 (set (reg:CCZ 17 flags) (compare:CCZ (mem/f:SI (plus:DI (reg/f:DI 54 virtual-stack-vars) (const_int -20 [0xffffffffffffffec])) [0 mode+0 S4 A32]) (const_int 13 [0xd]))) -1 (nil) (nil)) (jump_insn 32 31 35 (set (pc) (if_then_else (ne (reg:CCZ 17 flags) (const_int 0 [0x0])) (label_ref 37) (pc))) -1 (nil) (nil)) (jump_insn 35 32 36 (set (pc) (label_ref 40)) -1 (nil) (nil)) (barrier 36 35 37) (code_label 37 36 40 2 "" "" [0 uses]) (code_label 40 37 42 3 "" "" [0 uses]) (insn 42 40 44 (set (reg:DI 1 rdx) (reg:DI 60)) -1 (nil) (nil)) (insn 44 42 46 (set (reg:DI 4 rsi) (mem:DI (reg/f:DI 61) [0 S8 A64])) -1 (nil) (nil)) (insn 46 44 47 (set (reg:DI 5 rdi) (mem:DI (reg/f:DI 62) [0 S8 A64])) -1 (nil) (nil)) (call_insn/u 47 46 49 (set (reg:DI 0 rax) (call (mem:QI (reg:DI 63) [0 S1 A8]) (const_int 0 [0x0]))) -1 (nil) (expr_list:REG_EH_REGION (const_int -1 [0xffffffffffffffff]) (nil)) (expr_list (use (reg:DI 5 rdi)) (expr_list (use (reg:DI 4 rsi)) (expr_list (use (reg:DI 1 rdx)) (nil))))) Haven't checked yet what exactly fixed this on 3.4 (or already 3.3) branch. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29382