Skip to content

Commit

Permalink
Swap operans according to folding rules
Browse files Browse the repository at this point in the history
(recompilatrion of the saved IR file should give the same result)
same
  • Loading branch information
dstogov committed Apr 24, 2024
1 parent 3983e78 commit 44d9908
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 10 deletions.
32 changes: 32 additions & 0 deletions ir_gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion tests/014.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/023.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/debug.Windows-x86_64/dce_001.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/debug.Windows-x86_64/loop_001.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/debug.aarch64/dce_001.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/debug.aarch64/loop_001.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/debug.x86/dce_001.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/debug.x86/loop_001.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/debug/dce_001.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/debug/loop_001.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 44d9908

Please sign in to comment.