diff --git a/ir_ra.c b/ir_ra.c index ef0e23d..1f8fed1 100644 --- a/ir_ra.c +++ b/ir_ra.c @@ -790,6 +790,10 @@ int ir_compute_live_ranges(ir_ctx *ctx) ir_bitset_excl(live, v); /* PHIs inputs must not be processed */ ival = ctx->live_intervals[v]; + if (UNEXPECTED(!ival)) { + /* Dead PHI */ + ival = ir_add_live_range(ctx, v, IR_DEF_LIVE_POS_FROM_REF(ref), IR_USE_LIVE_POS_FROM_REF(ref)); + } ival->type = insn->type; ir_add_use(ctx, ival, 0, IR_DEF_LIVE_POS_FROM_REF(ref), IR_REG_NONE, IR_USE_SHOULD_BE_IN_REG, 0); continue; @@ -1397,6 +1401,10 @@ int ir_compute_live_ranges(ir_ctx *ctx) } else { /* PHIs inputs must not be processed */ ival = ctx->live_intervals[v]; + if (UNEXPECTED(!ival)) { + /* Dead PHI */ + ival = ir_add_live_range(ctx, v, IR_DEF_LIVE_POS_FROM_REF(ref), IR_USE_LIVE_POS_FROM_REF(ref)); + } ival->type = insn->type; ir_add_use(ctx, ival, 0, IR_DEF_LIVE_POS_FROM_REF(ref), IR_REG_NONE, IR_USE_SHOULD_BE_IN_REG, 0); continue; diff --git a/tests/dead_phi_001.irt b/tests/dead_phi_001.irt new file mode 100644 index 0000000..f37e1f5 --- /dev/null +++ b/tests/dead_phi_001.irt @@ -0,0 +1,37 @@ +--TEST-- +001: Dead phi -O0 +--ARGS-- +-O0 --save +--CODE-- +{ + l_1 = START(l_12); + int32_t d_2 = PARAM(l_1, "x", 0); + int32_t d_3 = PARAM(l_1, "y", 1); + l_4 = IF(l_1, d_2); + l_5 = IF_TRUE(l_4); + l_6 = END(l_5); + l_7 = IF_FALSE(l_4); + l_8 = END(l_7); + l_9 = MERGE(l_6, l_8); + int32_t d_10 = PHI(l_9, d_2, d_3); + int32_t d_11 = NOT(d_10); + l_12 = RETURN(l_9); +} +--EXPECT-- +{ + uintptr_t c_1 = 0; + bool c_2 = 0; + bool c_3 = 1; + l_1 = START(l_12); + int32_t d_2 = PARAM(l_1, "x", 0); + int32_t d_3 = PARAM(l_1, "y", 1); + l_4 = IF(l_1, d_2); + l_5 = IF_TRUE(l_4); + l_6 = END(l_5); + l_7 = IF_FALSE(l_4); + l_8 = END(l_7); + l_9 = MERGE(l_6, l_8); + int32_t d_10 = PHI(l_9, d_2, d_3); + int32_t d_11 = NOT(d_10); + l_12 = RETURN(l_9, null); +} diff --git a/tests/dead_phi_002.irt b/tests/dead_phi_002.irt new file mode 100644 index 0000000..bdd19a6 --- /dev/null +++ b/tests/dead_phi_002.irt @@ -0,0 +1,36 @@ +--TEST-- +002: Dead phi -O1 +--ARGS-- +-O1 --save +--CODE-- +{ + l_1 = START(l_12); + int32_t d_2 = PARAM(l_1, "x", 0); + int32_t d_3 = PARAM(l_1, "y", 1); + l_4 = IF(l_1, d_2); + l_5 = IF_TRUE(l_4); + l_6 = END(l_5); + l_7 = IF_FALSE(l_4); + l_8 = END(l_7); + l_9 = MERGE(l_6, l_8); + int32_t d_10 = PHI(l_9, d_2, d_3); + int32_t d_11 = NOT(d_10); + l_12 = RETURN(l_9); +} +--EXPECT-- +{ + uintptr_t c_1 = 0; + bool c_2 = 0; + bool c_3 = 1; + l_1 = START(l_11); + int32_t d_2 = PARAM(l_1, "x", 0); + int32_t d_3 = PARAM(l_1, "y", 1); + l_4 = IF(l_1, d_2); + l_5 = IF_TRUE(l_4); + l_6 = END(l_5); + l_7 = IF_FALSE(l_4); + l_8 = END(l_7); + l_9 = MERGE(l_6, l_8); + int32_t d_10 = PHI(l_9, d_2, d_3); + l_11 = RETURN(l_9, null); +} diff --git a/tests/dead_phi_003.irt b/tests/dead_phi_003.irt new file mode 100644 index 0000000..4139ba3 --- /dev/null +++ b/tests/dead_phi_003.irt @@ -0,0 +1,35 @@ +--TEST-- +003: Dead phi -O2 +--ARGS-- +-O2 --save +--CODE-- +{ + l_1 = START(l_12); + int32_t d_2 = PARAM(l_1, "x", 0); + int32_t d_3 = PARAM(l_1, "y", 1); + l_4 = IF(l_1, d_2); + l_5 = IF_TRUE(l_4); + l_6 = END(l_5); + l_7 = IF_FALSE(l_4); + l_8 = END(l_7); + l_9 = MERGE(l_6, l_8); + int32_t d_10 = PHI(l_9, d_2, d_3); + int32_t d_11 = NOT(d_10); + l_12 = RETURN(l_9); +} +--EXPECT-- +{ + uintptr_t c_1 = 0; + bool c_2 = 0; + bool c_3 = 1; + l_1 = START(l_10); + int32_t d_2 = PARAM(l_1, "x", 0); + int32_t d_3 = PARAM(l_1, "y", 1); + l_4 = IF(l_1, d_2); + l_5 = IF_TRUE(l_4); + l_6 = END(l_5); + l_7 = IF_FALSE(l_4); + l_8 = END(l_7); + l_9 = MERGE(l_6, l_8); + l_10 = RETURN(l_9, null); +}