Skip to content

Commit fa9caa7

Browse files
committed
Remove floating-point value comparison flag
1 parent 79dd115 commit fa9caa7

File tree

7 files changed

+29
-31
lines changed

7 files changed

+29
-31
lines changed

src/_debug/dump_ir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static void dump_ir(FILE *fp, IR *ir, RegAlloc *ra) {
106106
fprintf(fp, "%s%s\t", kOps[ir->kind], us);
107107
break;
108108
case IR_JMP:
109-
fprintf(fp, "J%s%s\t", kCond[ir->jmp.cond & (COND_MASK | COND_UNSIGNED)], us);
109+
fprintf(fp, "J%s%s\t", kCond[ir->jmp.cond], us);
110110
break;
111111
default:
112112
assert(0 <= ir->kind && ir->kind <= IR_ASM);
@@ -132,7 +132,7 @@ static void dump_ir(FILE *fp, IR *ir, RegAlloc *ra) {
132132
case IR_BITXOR: dump_vreg(fp, ir->dst, ra); fprintf(fp, " = "); dump_vreg(fp, ir->opr1, ra); fprintf(fp, " ^ "); dump_vreg(fp, ir->opr2, ra); fprintf(fp, "\n"); break;
133133
case IR_LSHIFT: dump_vreg(fp, ir->dst, ra); fprintf(fp, " = "); dump_vreg(fp, ir->opr1, ra); fprintf(fp, " << "); dump_vreg(fp, ir->opr2, ra); fprintf(fp, "\n"); break;
134134
case IR_RSHIFT: dump_vreg(fp, ir->dst, ra); fprintf(fp, " = "); dump_vreg(fp, ir->opr1, ra); fprintf(fp, " >> "); dump_vreg(fp, ir->opr2, ra); fprintf(fp, "\n"); break;
135-
case IR_COND: dump_vreg(fp, ir->dst, ra); fprintf(fp, " = "); if (ir->cond.kind != COND_ANY && ir->cond.kind != COND_NONE) {dump_vreg(fp, ir->opr1, ra); fprintf(fp, " %s ", kCond2[ir->cond.kind & (COND_MASK | COND_UNSIGNED)]); dump_vreg(fp, ir->opr2, ra);} fprintf(fp, "\n"); break;
135+
case IR_COND: dump_vreg(fp, ir->dst, ra); fprintf(fp, " = "); if (ir->cond.kind != COND_ANY && ir->cond.kind != COND_NONE) {dump_vreg(fp, ir->opr1, ra); fprintf(fp, " %s ", kCond2[ir->cond.kind]); dump_vreg(fp, ir->opr2, ra);} fprintf(fp, "\n"); break;
136136
case IR_NEG: dump_vreg(fp, ir->dst, ra); fprintf(fp, " = -"); dump_vreg(fp, ir->opr1, ra); fprintf(fp, "\n"); break;
137137
case IR_BITNOT: dump_vreg(fp, ir->dst, ra); fprintf(fp, " = ~"); dump_vreg(fp, ir->opr1, ra); fprintf(fp, "\n"); break;
138138
case IR_CAST: dump_vreg(fp, ir->dst, ra); fprintf(fp, " = "); dump_vreg(fp, ir->opr1, ra); fprintf(fp, "\n"); break;

src/cc/arch/aarch64/ir_aarch64.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,8 @@ static void ei_cond(IR *ir) {
797797
const char *dst = kReg32s[ir->dst->phys]; // Assume bool is 4 byte.
798798
int cond = ir->cond.kind;
799799
// On aarch64, flag for comparing flonum is signed.
800-
if (cond & COND_FLONUM) {
801-
cond &= COND_MASK;
800+
if (ir->opr1->flag & VRF_FLONUM) {
801+
assert((cond & ~COND_MASK) == 0);
802802
switch (cond) {
803803
case COND_LT: CSET(dst, CMI); return;
804804
case COND_LE: CSET(dst, CLS); return;
@@ -849,8 +849,8 @@ static void ei_jmp(IR *ir) {
849849

850850
cmp_vregs(ir->opr1, ir->opr2);
851851

852-
if (cond & COND_FLONUM) {
853-
cond &= COND_MASK;
852+
if (ir->opr1->flag & VRF_FLONUM) {
853+
assert((cond & ~COND_MASK) == 0);
854854
switch (cond) {
855855
case COND_LT: Bcc(CMI, label); return;
856856
case COND_LE: Bcc(CLS, label); return;

src/cc/arch/riscv64/ir_riscv64.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ static void ei_cond(IR *ir) {
698698
assert(ir->opr2 != NULL);
699699
const char *dst = kReg64s[ir->dst->phys];
700700
assert(!(ir->opr1->flag & VRF_CONST));
701-
int cond = ir->cond.kind & (COND_MASK | COND_UNSIGNED);
701+
int cond = ir->cond.kind;
702702

703703
if (ir->opr1->flag & VRF_FLONUM) {
704704
assert(ir->opr2->flag & VRF_FLONUM);
@@ -835,7 +835,7 @@ static void ei_jmp(IR *ir) {
835835
const char *opr2 = !(ir->opr2->flag & VRF_CONST) ? kReg64s[ir->opr2->phys] : ZERO;
836836

837837
// On aarch64, flag for comparing flonum is signed.
838-
switch (cond & (COND_MASK | COND_UNSIGNED)) {
838+
switch (cond) {
839839
case COND_EQ | COND_UNSIGNED: // Fallthrough
840840
case COND_EQ: Bcc(CEQ, opr1, opr2, label); break;
841841

src/cc/arch/x64/ir_x64.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,8 @@ static void ei_cond(IR *ir) {
809809
const char *dst = kReg8s[ir->dst->phys];
810810
int cond = ir->cond.kind;
811811
// On x64, flag for comparing flonum is same as unsigned.
812-
if (cond & COND_FLONUM) {
813-
cond &= COND_MASK;
812+
if (ir->opr1->flag & VRF_FLONUM) {
813+
assert((cond & ~COND_MASK) == 0);
814814
if (cond == COND_LT || cond == COND_LE) {
815815
VReg *tmp = opr1;
816816
opr1 = opr2;
@@ -865,27 +865,33 @@ static void ei_cond(IR *ir) {
865865
}
866866

867867
static void ei_jmp(IR *ir) {
868+
const char *label = fmt_name(ir->jmp.bb->label);
868869
int cond = ir->jmp.cond;
869870
assert(cond != COND_NONE);
871+
if (cond == COND_ANY) {
872+
JMP(label);
873+
return;
874+
}
870875

871-
const char *label = fmt_name(ir->jmp.bb->label);
872876
VReg *opr1 = ir->opr1, *opr2 = ir->opr2;
877+
assert(opr1 != NULL && opr2 != NULL);
878+
873879
// On x64, flag for comparing flonum is same as unsigned.
874-
if (cond & COND_FLONUM) {
875-
cond &= COND_MASK;
880+
if (opr1->flag & VRF_FLONUM) {
881+
assert((cond & ~COND_MASK) == 0);
876882
// Special handling required for `==` and `!=`.
877883
switch (cond) {
878884
case COND_EQ:
879885
{
880886
const Name *skip_label = alloc_label();
881-
cmp_vregs(ir->opr1, ir->opr2, cond);
887+
cmp_vregs(opr1, opr2, cond);
882888
JP(fmt_name(skip_label));
883889
JE(label);
884890
EMIT_LABEL(fmt_name(skip_label));
885891
}
886892
return;
887893
case COND_NE:
888-
cmp_vregs(ir->opr1, ir->opr2, cond);
894+
cmp_vregs(opr1, opr2, cond);
889895
JP(label);
890896
JNE(label);
891897
return;
@@ -905,11 +911,6 @@ static void ei_jmp(IR *ir) {
905911
cond |= COND_UNSIGNED; // Turn on unsigned
906912
}
907913

908-
if (cond == COND_ANY) {
909-
JMP(label);
910-
return;
911-
}
912-
913914
cmp_vregs(opr1, opr2, cond);
914915

915916
switch (cond) {

src/cc/backend/codegen_expr.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ static struct CompareExpr gen_compare_expr(enum ExprKind kind, Expr *lhs, Expr *
7272
// unsigned
7373
flag = COND_UNSIGNED;
7474
}
75-
if (is_flonum(lhs->type))
76-
flag |= COND_FLONUM;
7775

7876
VReg *lhs_reg = gen_expr(lhs);
7977
VReg *rhs_reg = gen_expr(rhs);
@@ -82,7 +80,7 @@ static struct CompareExpr gen_compare_expr(enum ExprKind kind, Expr *lhs, Expr *
8280
static struct CompareExpr kNone = {.cond = COND_NONE};
8381
static struct CompareExpr kAny = {.cond = COND_ANY};
8482
#ifndef __NO_FLONUM
85-
if (flag & COND_FLONUM) {
83+
if (is_flonum(lhs->type)) {
8684
assert(lhs_reg->flag & VRF_FLONUM);
8785
assert(rhs_reg->flag & VRF_FLONUM);
8886
switch (cond) {

src/cc/backend/ir.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ enum ConditionKind {
121121
enum {
122122
COND_MASK = 0x07,
123123
COND_UNSIGNED = 1 << 3,
124-
COND_FLONUM = 1 << 4,
125124
};
126125

127126
enum ConditionKind swap_cond(enum ConditionKind cond);

src/cc/backend/optimize.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static void remove_unnecessary_bb(BBContainer *bbcon) {
8484
IR *ir0 = is_last_jmp(pbb);
8585
if (ir0 != NULL && ir0->jmp.cond != COND_ANY && // Fallthrough pass exists.
8686
ir0->jmp.bb == bb->next && // Skip jmp: Fix bb connection.
87-
!(ir0->jmp.cond & COND_FLONUM)) {
87+
!(ir0->opr1->flag & VRF_FLONUM)) {
8888
// Invert prev jmp condition and change jmp destination.
8989
ir0->jmp.cond = invert_cond(ir0->jmp.cond);
9090
ir0->jmp.bb = ir_jmp->jmp.bb;
@@ -280,12 +280,12 @@ static bool calc_const_cond(enum ConditionKind cond, VReg *opr1, VReg *opr2) {
280280
double f1 = opr1->flonum.value;
281281
double f2 = opr2->flonum.value;
282282
switch ((int)cond) {
283-
case COND_EQ | COND_FLONUM: return f1 == f2;
284-
case COND_NE | COND_FLONUM: return f1 != f2;
285-
case COND_LT | COND_FLONUM: return f1 < f2;
286-
case COND_GT | COND_FLONUM: return f1 > f2;
287-
case COND_LE | COND_FLONUM: return f1 <= f2;
288-
case COND_GE | COND_FLONUM: return f1 >= f2;
283+
case COND_EQ: return f1 == f2;
284+
case COND_NE: return f1 != f2;
285+
case COND_LT: return f1 < f2;
286+
case COND_GT: return f1 > f2;
287+
case COND_LE: return f1 <= f2;
288+
case COND_GE: return f1 >= f2;
289289
default: assert(false); return false;
290290
}
291291
} else

0 commit comments

Comments
 (0)