diff --git a/ir_gcm.c b/ir_gcm.c index 8a48fed..adbe77a 100644 --- a/ir_gcm.c +++ b/ir_gcm.c @@ -15,6 +15,7 @@ #define IR_GCM_EARLY_BLOCK(b) ((uint32_t)-((int32_t)(b))) #define IR_GCM_SPLIT 1 +#define IR_SCHEDULE_SWAP_OPS 1 static uint32_t ir_gcm_schedule_early(ir_ctx *ctx, ir_ref ref, ir_list *queue_late) { @@ -1131,6 +1132,37 @@ int ir_schedule(ir_ctx *ctx) new_insn->op1 = _xlat[insn->op1]; new_insn->op2 = _xlat[insn->op2]; new_insn->op3 = insn->op3; +#if IR_SCHEDULE_SWAP_OPS + /* Swap operands according to folding rules */ + if (new_insn->op1 < new_insn->op2) { + switch (new_insn->op) { + case IR_EQ: + case IR_NE: + case IR_ADD: + case IR_MUL: + case IR_ADD_OV: + case IR_MUL_OV: + case IR_OR: + case IR_AND: + case IR_XOR: + case IR_MIN: + case IR_MAX: + SWAP_REFS(new_insn->op1, new_insn->op2); + break; + case IR_LT: + case IR_GE: + case IR_LE: + case IR_GT: + case IR_ULT: + case IR_UGE: + case IR_ULE: + case IR_UGT: + SWAP_REFS(new_insn->op1, new_insn->op2); + new_insn->op ^= 3; /* [U]LT <-> [U]GT, [U]LE <-> [U]GE */ + break; + } + } +#endif break; case 3: new_insn->op1 = _xlat[insn->op1]; diff --git a/tests/014.irt b/tests/014.irt index 85281e9..96fa8f8 100644 --- a/tests/014.irt +++ b/tests/014.irt @@ -38,7 +38,7 @@ l_4 = END(l_1); l_5 = LOOP_BEGIN(l_4, l_11); int32_t d_6 = PHI(l_5, c_6, d_7); - int32_t d_7 = ADD(d_3, d_6); + int32_t d_7 = ADD(d_6, d_3); bool d_8 = LT(d_7, c_4); l_9 = IF(l_5, d_8); l_10 = IF_TRUE(l_9); diff --git a/tests/023.irt b/tests/023.irt index 0af490a..60b213b 100644 --- a/tests/023.irt +++ b/tests/023.irt @@ -39,7 +39,7 @@ l_4 = END(l_1); l_5 = LOOP_BEGIN(l_4, l_11); int32_t d_6 = PHI(l_5, c_6, d_7); - int32_t d_7 = ADD(d_3, d_6); + int32_t d_7 = ADD(d_6, d_3); bool d_8 = LT(d_7, c_4); l_9 = IF(l_5, d_8); l_10 = IF_TRUE(l_9); diff --git a/tests/debug.Windows-x86_64/dce_001.irt b/tests/debug.Windows-x86_64/dce_001.irt index 0587be1..6684a95 100755 --- a/tests/debug.Windows-x86_64/dce_001.irt +++ b/tests/debug.Windows-x86_64/dce_001.irt @@ -33,7 +33,7 @@ Windows-x86_64 test: xorl %eax, %eax .L1: - leal 1(%rcx, %rax), %eax + leal 1(%rax, %rcx), %eax cmpl $0xa, %eax jl .L1 addl %eax, %eax diff --git a/tests/debug.Windows-x86_64/loop_001.irt b/tests/debug.Windows-x86_64/loop_001.irt index aae3b3b..003bb32 100755 --- a/tests/debug.Windows-x86_64/loop_001.irt +++ b/tests/debug.Windows-x86_64/loop_001.irt @@ -32,7 +32,7 @@ Windows-x86_64 test: xorl %eax, %eax .L1: - leal 1(%rcx, %rax), %eax + leal 1(%rax, %rcx), %eax cmpl $0xa, %eax jl .L1 addl %eax, %eax diff --git a/tests/debug.aarch64/dce_001.irt b/tests/debug.aarch64/dce_001.irt index 9608c18..01cabce 100644 --- a/tests/debug.aarch64/dce_001.irt +++ b/tests/debug.aarch64/dce_001.irt @@ -34,7 +34,7 @@ test: add w0, w0, #1 mov w1, wzr .L1: - add w1, w0, w1 + add w1, w1, w0 cmp w1, #0xa b.lt .L1 add w0, w1, w1 diff --git a/tests/debug.aarch64/loop_001.irt b/tests/debug.aarch64/loop_001.irt index 547639e..39aaf0a 100644 --- a/tests/debug.aarch64/loop_001.irt +++ b/tests/debug.aarch64/loop_001.irt @@ -33,7 +33,7 @@ test: add w0, w0, #1 mov w1, wzr .L1: - add w1, w0, w1 + add w1, w1, w0 cmp w1, #0xa b.lt .L1 add w0, w1, w1 diff --git a/tests/debug.x86/dce_001.irt b/tests/debug.x86/dce_001.irt index a0dbf91..5ab06f7 100644 --- a/tests/debug.x86/dce_001.irt +++ b/tests/debug.x86/dce_001.irt @@ -34,7 +34,7 @@ test: movl 4(%esp), %eax xorl %ecx, %ecx .L1: - leal 1(%eax, %ecx), %ecx + leal 1(%ecx, %eax), %ecx cmpl $0xa, %ecx jl .L1 leal (%ecx, %ecx), %eax diff --git a/tests/debug.x86/loop_001.irt b/tests/debug.x86/loop_001.irt index 5ef8b2f..c13944c 100644 --- a/tests/debug.x86/loop_001.irt +++ b/tests/debug.x86/loop_001.irt @@ -33,7 +33,7 @@ test: movl 4(%esp), %eax xorl %ecx, %ecx .L1: - leal 1(%eax, %ecx), %ecx + leal 1(%ecx, %eax), %ecx cmpl $0xa, %ecx jl .L1 leal (%ecx, %ecx), %eax diff --git a/tests/debug/dce_001.irt b/tests/debug/dce_001.irt index 84f856a..8c6b1a7 100644 --- a/tests/debug/dce_001.irt +++ b/tests/debug/dce_001.irt @@ -33,7 +33,7 @@ x86_64 test: xorl %eax, %eax .L1: - leal 1(%rdi, %rax), %eax + leal 1(%rax, %rdi), %eax cmpl $0xa, %eax jl .L1 addl %eax, %eax diff --git a/tests/debug/loop_001.irt b/tests/debug/loop_001.irt index bd95603..32156e6 100644 --- a/tests/debug/loop_001.irt +++ b/tests/debug/loop_001.irt @@ -32,7 +32,7 @@ x86_64 test: xorl %eax, %eax .L1: - leal 1(%rdi, %rax), %eax + leal 1(%rax, %rdi), %eax cmpl $0xa, %eax jl .L1 addl %eax, %eax