http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50217
Bug #: 50217 Summary: combine pass generated wrong code for unsigned char comparison on MIPS Classification: Unclassified Product: gcc Version: 3.4.4 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: rtl-optimization AssignedTo: unassig...@gcc.gnu.org ReportedBy: yaoc...@gmail.com Target: mips Andrew Pinski <pinskia at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords| |wrong-code Target| |mips Component|c |rtl-optimization I found gcc3.4.4 generate wrong MIPS instructions for the codes following when using -O1 and -O2. int test(unsigned char a, int *b) { if(b==NULL) return -1; if(a>=0x01 && a<=0xaf) { *b++; return 0; } else if(a<=0xfe) return 1; else return 3; return 4; } In assemble codes, (a>=0x01 && a<=0xaf) correspond to "add $8,$8,-1; sltui $8,$8,-81 " . According to MIPS instruction manual, "sltui $8,$8,-81 " means the value in $8 will be compared with the constant 0xFFFFFFAF. So when the variable "a" has a value larger than 0xaf, but "sltui $8,$8,-81 " make the condition "(a>=0x01 && a<=0xaf)" hold, and the program flow go into if-body, and get wrong result. It seems to me that when simplify_comparison combining tow rtl insn about the if condition, zero_extend and ltu, the constant was signed-extended. This problem doesn't occur in 2.96 and 4.3, other versions not tested.