Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined behaviour warnings in ir_fold.h #99

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions ir_fold.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,13 @@ IR_FOLD(ADD(C_I16, C_I16))
IR_FOLD(ADD(C_I32, C_I32))
{
IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type || (sizeof(void*) == 4 && IR_OPT_TYPE(opt) == IR_ADDR));
IR_FOLD_CONST_I(op1_insn->val.i32 + op2_insn->val.i32);
IR_FOLD_CONST_I(op1_insn->val.u32 + op2_insn->val.u32);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment: e.g. "Here and below we use unsigned math to prevent undefined signed overflow behavior".

}

IR_FOLD(ADD(C_I64, C_I64))
{
IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type || (sizeof(void*) == 8 && IR_OPT_TYPE(opt) == IR_ADDR));
IR_FOLD_CONST_I(op1_insn->val.i64 + op2_insn->val.i64);
IR_FOLD_CONST_I(op1_insn->val.u64 + op2_insn->val.u64);
}

IR_FOLD(ADD(C_DOUBLE, C_DOUBLE))
Expand Down Expand Up @@ -393,13 +393,13 @@ IR_FOLD(SUB(C_I16, C_I16))
IR_FOLD(SUB(C_I32, C_I32))
{
IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);
IR_FOLD_CONST_I(op1_insn->val.i32 - op2_insn->val.i32);
IR_FOLD_CONST_I(op1_insn->val.u32 - op2_insn->val.u32);
}

IR_FOLD(SUB(C_I64, C_I64))
{
IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);
IR_FOLD_CONST_I(op1_insn->val.i64 - op2_insn->val.i64);
IR_FOLD_CONST_I(op1_insn->val.u64 - op2_insn->val.u64);
}

IR_FOLD(SUB(C_DOUBLE, C_DOUBLE))
Expand Down Expand Up @@ -463,13 +463,13 @@ IR_FOLD(MUL(C_I16, C_I16))
IR_FOLD(MUL(C_I32, C_I32))
{
IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);
IR_FOLD_CONST_I(op1_insn->val.i32 * op2_insn->val.i32);
IR_FOLD_CONST_I(op1_insn->val.u32 * op2_insn->val.u32);
}

IR_FOLD(MUL(C_I64, C_I64))
{
IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);
IR_FOLD_CONST_I(op1_insn->val.i64 * op2_insn->val.i64);
IR_FOLD_CONST_I(op1_insn->val.u64 * op2_insn->val.u64);
}

IR_FOLD(MUL(C_DOUBLE, C_DOUBLE))
Expand Down Expand Up @@ -556,7 +556,7 @@ IR_FOLD(NEG(C_I32))
IR_FOLD(NEG(C_I64))
{
IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);
IR_FOLD_CONST_I(-op1_insn->val.i64);
IR_FOLD_CONST_I(-op1_insn->val.u64);
}

IR_FOLD(NEG(C_DOUBLE))
Expand All @@ -580,7 +580,7 @@ IR_FOLD(ABS(C_I64))
if (op1_insn->val.i64 >= 0) {
IR_FOLD_COPY(op1);
} else {
IR_FOLD_CONST_I(-op1_insn->val.i64);
IR_FOLD_CONST_I(-op1_insn->val.u64);
}
}

Expand Down Expand Up @@ -680,7 +680,7 @@ IR_FOLD(MUL_OV(C_I64, C_I64))
int64_t min = - max - 1;
int64_t res;
IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type);
res = op1_insn->val.i64 * op2_insn->val.i64;
res = op1_insn->val.u64 * op2_insn->val.u64;
if (op1_insn->val.i64 != 0 && res / op1_insn->val.i64 != op2_insn->val.i64 && res >= min && res <= max) {
IR_FOLD_NEXT;
}
Expand Down Expand Up @@ -2527,7 +2527,7 @@ IR_FOLD(ADD(ADD, C_I64))
{
if (IR_IS_CONST_REF(op1_insn->op2) && !IR_IS_SYM_CONST(ctx->ir_base[op1_insn->op2].op)) {
/* (x + c1) + c2 => x + (c1 + c2) */
val.i64 = ctx->ir_base[op1_insn->op2].val.i64 + op2_insn->val.i64;
val.i64 = ctx->ir_base[op1_insn->op2].val.u64 + op2_insn->val.u64;
op1 = op1_insn->op1;
op2 = ir_const(ctx, val, IR_OPT_TYPE(opt));
IR_FOLD_RESTART;
Expand Down Expand Up @@ -2565,8 +2565,8 @@ IR_FOLD(ADD(SUB, C_I64))
{
if (IR_IS_CONST_REF(op1_insn->op2) && !IR_IS_SYM_CONST(ctx->ir_base[op1_insn->op2].op)) {
/* (x - c1) + c2 => x + (c2 - c1) */
val.i64 = op2_insn->val.i64 - ctx->ir_base[op1_insn->op2].val.i64;
if (val.i64 < 0 && val.i64 - 1 < 0) {
val.i64 = op2_insn->val.u64 - ctx->ir_base[op1_insn->op2].val.u64;
if (val.i64 < 0 && val.i64 != INT64_MIN) {
val.i64 = -val.i64;
opt++; /* ADD -> SUB */
}
Expand All @@ -2575,7 +2575,7 @@ IR_FOLD(ADD(SUB, C_I64))
IR_FOLD_RESTART;
} else if (IR_IS_CONST_REF(op1_insn->op1) && !IR_IS_SYM_CONST(ctx->ir_base[op1_insn->op1].op)) {
/* (c1 - x) + c2 => (c1 + c2) - x */
val.i64 = ctx->ir_base[op1_insn->op1].val.i64 + op2_insn->val.i64;
val.i64 = ctx->ir_base[op1_insn->op1].val.u64 + op2_insn->val.u64;
opt++; /* ADD -> SUB */
op2 = op1_insn->op2;
op1 = ir_const(ctx, val, IR_OPT_TYPE(opt));
Expand Down Expand Up @@ -2608,8 +2608,8 @@ IR_FOLD(SUB(ADD, C_I64))
{
if (IR_IS_CONST_REF(op1_insn->op2) && !IR_IS_SYM_CONST(ctx->ir_base[op1_insn->op2].op)) {
/* (x + c1) - c2 => x + (c1 - c2) */
val.i64 = ctx->ir_base[op1_insn->op2].val.i64 - op2_insn->val.i64;
if (val.i64 < 0 && val.i64 - 1 < 0) {
val.i64 = ctx->ir_base[op1_insn->op2].val.u64 - op2_insn->val.u64;
if (val.i64 < 0 && val.i64 != INT64_MIN) {
val.i64 = -val.i64;
} else {
opt--; /* SUB -> ADD */
Expand Down Expand Up @@ -2644,7 +2644,7 @@ IR_FOLD(SUB(C_I64, ADD))
{
if (IR_IS_CONST_REF(op2_insn->op2) && !IR_IS_SYM_CONST(ctx->ir_base[op2_insn->op2].op)) {
/* c1 - (x + c2) => (c1 - c2) - x */
val.i64 = op1_insn->val.i64 - ctx->ir_base[op2_insn->op2].val.i64;
val.i64 = op1_insn->val.u64 - ctx->ir_base[op2_insn->op2].val.u64;
op2 = op2_insn->op1;
op1 = ir_const(ctx, val, IR_OPT_TYPE(opt));
IR_FOLD_RESTART;
Expand All @@ -2661,7 +2661,7 @@ IR_FOLD(SUB(SUB, C_ADDR))
if (IR_IS_CONST_REF(op1_insn->op2) && !IR_IS_SYM_CONST(ctx->ir_base[op1_insn->op2].op)) {
/* (x - c1) - c2 => x - (c1 + c2) */
val.u64 = ctx->ir_base[op1_insn->op2].val.u64 + op2_insn->val.u64;
if (val.i64 < 0 && val.i64 - 1 < 0) {
if (val.i64 < 0 && val.i64 != INT64_MIN) {
val.i64 = -val.i64;
opt--; /* SUB -> ADD */
}
Expand All @@ -2685,8 +2685,8 @@ IR_FOLD(SUB(SUB, C_I64))
{
if (IR_IS_CONST_REF(op1_insn->op2) && !IR_IS_SYM_CONST(ctx->ir_base[op1_insn->op2].op)) {
/* (x - c1) - c2 => x - (c1 + c2) */
val.i64 = ctx->ir_base[op1_insn->op2].val.i64 + op2_insn->val.i64;
if (val.i64 < 0 && val.i64 - 1 < 0) {
val.i64 = ctx->ir_base[op1_insn->op2].val.u64 + op2_insn->val.u64;
if (val.i64 < 0 && val.i64 != INT64_MIN) {
val.i64 = -val.i64;
opt--; /* SUB -> ADD */
}
Expand All @@ -2695,7 +2695,7 @@ IR_FOLD(SUB(SUB, C_I64))
IR_FOLD_RESTART;
} else if (IR_IS_CONST_REF(op1_insn->op1) && !IR_IS_SYM_CONST(ctx->ir_base[op1_insn->op1].op)) {
/* (c1 - x) - c2 => (c1 - c2) - x */
val.i64 = ctx->ir_base[op1_insn->op1].val.i64 - op2_insn->val.i64;
val.i64 = ctx->ir_base[op1_insn->op1].val.u64 - op2_insn->val.u64;
op2 = op1_insn->op2;
op1 = ir_const(ctx, val, IR_OPT_TYPE(opt));
IR_FOLD_RESTART;
Expand All @@ -2718,7 +2718,7 @@ IR_FOLD(SUB(C_ADDR, SUB))
} else if (IR_IS_CONST_REF(op2_insn->op1) && !IR_IS_SYM_CONST(ctx->ir_base[op2_insn->op1].op)) {
/* c1 - (c2 - x) => x + (c1 - c2) */
val.u64 = op1_insn->val.u64 - ctx->ir_base[op2_insn->op1].val.u64;
if (val.i64 < 0 && val.i64 - 1 < 0) {
if (val.i64 < 0 && val.i64 != INT64_MIN) {
val.i64 = -val.i64;
opt++; /* ADD -> SUB */
}
Expand All @@ -2736,14 +2736,14 @@ IR_FOLD(SUB(C_I64, SUB))
{
if (IR_IS_CONST_REF(op2_insn->op2) && !IR_IS_SYM_CONST(ctx->ir_base[op2_insn->op2].op)) {
/* c1 - (x - c2) => (c1 + c2) - x */
val.i64 = op1_insn->val.i64 + ctx->ir_base[op2_insn->op2].val.i64;
val.i64 = op1_insn->val.u64 + ctx->ir_base[op2_insn->op2].val.u64;
op2 = op2_insn->op1;
op1 = ir_const(ctx, val, IR_OPT_TYPE(opt));
IR_FOLD_RESTART;
} else if (IR_IS_CONST_REF(op2_insn->op1) && !IR_IS_SYM_CONST(ctx->ir_base[op2_insn->op1].op)) {
/* c1 - (c2 - x) => x + (c1 - c2) */
val.i64 = op1_insn->val.i64 - ctx->ir_base[op2_insn->op1].val.i64;
if (val.i64 < 0 && val.i64 - 1 < 0) {
val.i64 = op1_insn->val.u64 - ctx->ir_base[op2_insn->op1].val.u64;
if (val.i64 < 0 && val.i64 != INT64_MIN) {
val.i64 = -val.i64;
opt++; /* ADD -> SUB */
}
Expand Down Expand Up @@ -2777,7 +2777,7 @@ IR_FOLD(MUL(MUL, C_I64))
{
if (IR_IS_CONST_REF(op1_insn->op2) && !IR_IS_SYM_CONST(ctx->ir_base[op1_insn->op2].op)) {
/* (x * c1) * c2 => x * (c1 * c2) */
val.i64 = ctx->ir_base[op1_insn->op2].val.i64 * op2_insn->val.i64;
val.i64 = ctx->ir_base[op1_insn->op2].val.u64 * op2_insn->val.u64;
op1 = op1_insn->op1;
op2 = ir_const(ctx, val, IR_OPT_TYPE(opt));
IR_FOLD_RESTART;
Expand Down