@@ -1882,6 +1882,42 @@ ir_ref _ir_IF(ir_ctx *ctx, ir_ref condition)
1882
1882
ir_ref if_ref ;
1883
1883
1884
1884
IR_ASSERT (ctx -> control );
1885
+ if (IR_IS_CONST_REF (condition )) {
1886
+ condition = ir_ref_is_true (ctx , condition ) ? IR_TRUE : IR_FALSE ;
1887
+ } else {
1888
+ ir_insn * prev = NULL ;
1889
+ ir_ref ref = ctx -> control ;
1890
+ ir_insn * insn ;
1891
+
1892
+ while (ref > condition ) {
1893
+ insn = & ctx -> ir_base [ref ];
1894
+ if (insn -> op == IR_GUARD_NOT ) {
1895
+ if (insn -> op2 == condition ) {
1896
+ condition = IR_FALSE ;
1897
+ break ;
1898
+ }
1899
+ } else if (insn -> op == IR_GUARD ) {
1900
+ if (insn -> op2 == condition ) {
1901
+ condition = IR_TRUE ;
1902
+ break ;
1903
+ }
1904
+ } else if (insn -> op == IR_IF ) {
1905
+ if (insn -> op2 == condition ) {
1906
+ if (prev -> op == IR_IF_TRUE ) {
1907
+ condition = IR_TRUE ;
1908
+ break ;
1909
+ } else if (prev -> op == IR_IF_FALSE ) {
1910
+ condition = IR_FALSE ;
1911
+ break ;
1912
+ }
1913
+ }
1914
+ } else if (insn -> op == IR_START || insn -> op == IR_MERGE || insn -> op == IR_LOOP_BEGIN ) {
1915
+ break ;
1916
+ }
1917
+ prev = insn ;
1918
+ ref = insn -> op1 ;
1919
+ }
1920
+ }
1885
1921
if_ref = ir_emit2 (ctx , IR_IF , ctx -> control , condition );
1886
1922
ctx -> control = IR_UNUSED ;
1887
1923
return if_ref ;
@@ -2353,9 +2389,13 @@ ir_ref _ir_ADD_OFFSET(ir_ctx *ctx, ir_ref addr, uintptr_t offset)
2353
2389
void _ir_GUARD (ir_ctx * ctx , ir_ref condition , ir_ref addr )
2354
2390
{
2355
2391
IR_ASSERT (ctx -> control );
2356
- if (condition == IR_TRUE ) {
2357
- return ;
2392
+ if (IR_IS_CONST_REF (condition )) {
2393
+ if (ir_ref_is_true (ctx , condition )) {
2394
+ return ;
2395
+ }
2396
+ condition = IR_FALSE ;
2358
2397
} else {
2398
+ ir_insn * prev = NULL ;
2359
2399
ir_ref ref = ctx -> control ;
2360
2400
ir_insn * insn ;
2361
2401
@@ -2370,9 +2410,19 @@ void _ir_GUARD(ir_ctx *ctx, ir_ref condition, ir_ref addr)
2370
2410
condition = IR_FALSE ;
2371
2411
break ;
2372
2412
}
2413
+ } else if (insn -> op == IR_IF ) {
2414
+ if (insn -> op2 == condition ) {
2415
+ if (prev -> op == IR_IF_TRUE ) {
2416
+ return ;
2417
+ } else if (prev -> op == IR_IF_FALSE ) {
2418
+ condition = IR_FALSE ;
2419
+ break ;
2420
+ }
2421
+ }
2373
2422
} else if (insn -> op == IR_START || insn -> op == IR_MERGE || insn -> op == IR_LOOP_BEGIN ) {
2374
2423
break ;
2375
2424
}
2425
+ prev = insn ;
2376
2426
ref = insn -> op1 ;
2377
2427
}
2378
2428
}
@@ -2385,9 +2435,13 @@ void _ir_GUARD(ir_ctx *ctx, ir_ref condition, ir_ref addr)
2385
2435
void _ir_GUARD_NOT (ir_ctx * ctx , ir_ref condition , ir_ref addr )
2386
2436
{
2387
2437
IR_ASSERT (ctx -> control );
2388
- if (condition == IR_FALSE ) {
2389
- return ;
2438
+ if (IR_IS_CONST_REF (condition )) {
2439
+ if (!ir_ref_is_true (ctx , condition )) {
2440
+ return ;
2441
+ }
2442
+ condition = IR_TRUE ;
2390
2443
} else {
2444
+ ir_insn * prev = NULL ;
2391
2445
ir_ref ref = ctx -> control ;
2392
2446
ir_insn * insn ;
2393
2447
@@ -2402,9 +2456,19 @@ void _ir_GUARD_NOT(ir_ctx *ctx, ir_ref condition, ir_ref addr)
2402
2456
condition = IR_TRUE ;
2403
2457
break ;
2404
2458
}
2459
+ } else if (insn -> op == IR_IF ) {
2460
+ if (insn -> op2 == condition ) {
2461
+ if (prev -> op == IR_IF_TRUE ) {
2462
+ condition = IR_TRUE ;
2463
+ break ;
2464
+ } else if (prev -> op == IR_IF_FALSE ) {
2465
+ return ;
2466
+ }
2467
+ }
2405
2468
} else if (insn -> op == IR_START || insn -> op == IR_MERGE || insn -> op == IR_LOOP_BEGIN ) {
2406
2469
break ;
2407
2470
}
2471
+ prev = insn ;
2408
2472
ref = insn -> op1 ;
2409
2473
}
2410
2474
}
0 commit comments