@@ -948,6 +948,7 @@ static int simplify_cast(struct instruction *insn)
{
struct instruction *def;
pseudo_t src;
+ int osize;
int size;
if (dead_insn(insn, &insn->src, NULL, NULL))
@@ -973,6 +974,14 @@ static int simplify_cast(struct instruction *insn)
}
}
break;
+ case OP_TRUNC:
+ osize = def->orig_type->bit_size;
+ if (insn->opcode == OP_ZEXT && size == osize) {
+ insn->opcode = OP_AND;
+ insn->src2 = value_pseudo((1ULL << def->size) - 1);
+ return replace_pseudo(insn, &insn->src1, def->src);
+ }
+ break;
}
return 0;
similarity index 82%
rename from validation/linear/bitfield-init-zero.c
rename to validation/optim/bitfield-init-zero.c
@@ -57,8 +57,7 @@ int bfs_get0(void)
bfuu_init:
.L0:
<entry-point>
- trunc.9 %r2 <- (32) %arg1
- zext.32 %r4 <- (9) %r2
+ and.32 %r4 <- %arg1, $511
shl.32 %r5 <- %r4, $11
ret.32 %r5
@@ -66,8 +65,7 @@ bfuu_init:
bfus_init:
.L2:
<entry-point>
- trunc.9 %r11 <- (32) %arg1
- zext.32 %r13 <- (9) %r11
+ and.32 %r13 <- %arg1, $511
shl.32 %r14 <- %r13, $11
ret.32 %r14
@@ -81,8 +79,7 @@ bfu_get0:
bfsu_init:
.L6:
<entry-point>
- trunc.9 %r25 <- (32) %arg1
- zext.32 %r27 <- (9) %r25
+ and.32 %r27 <- %arg1, $511
shl.32 %r28 <- %r27, $11
ret.32 %r28
@@ -90,8 +87,7 @@ bfsu_init:
bfss_init:
.L8:
<entry-point>
- trunc.9 %r34 <- (32) %arg1
- zext.32 %r36 <- (9) %r34
+ and.32 %r36 <- %arg1, $511
shl.32 %r37 <- %r36, $11
ret.32 %r37
similarity index 93%
rename from validation/bitfield-size.c
rename to validation/optim/bitfield-size.c
@@ -35,8 +35,8 @@ unsigned int get_pbfi_b(struct bfi *bf) { return bf->b; }
* check-command: test-linearize -Wno-decl $file
* check-output-ignore
*
- * check-output-pattern(8): zext\\.
+ * check-output-pattern(8): and\\.
* check-output-pattern(4): sext\\.
- * check-output-pattern(12): trunc\\.4
+ * check-output-pattern(4): trunc\\.4
* check-output-pattern(6): lsr\\..*\\$6
*/
A truncation followed by a zero-extension to the original size, which is produced when loading a storing bitfields, is equivalent to a simple AND masking. Often, this AND can then trigger even more optimizations. So, replace TRUNC + ZEXT instructions by the equivalent AND. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> --- simplify.c | 9 +++++++++ validation/{linear => optim}/bitfield-init-zero.c | 12 ++++-------- validation/{ => optim}/bitfield-size.c | 4 ++-- 3 files changed, 15 insertions(+), 10 deletions(-) rename validation/{linear => optim}/bitfield-init-zero.c (82%) rename validation/{ => optim}/bitfield-size.c (93%)