diff mbox

[6/6] cast: simplify TRUNC + ZEXT to AND

Message ID 20180623150937.69155-7-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show

Commit Message

Luc Van Oostenryck June 23, 2018, 3:09 p.m. UTC
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%)
diff mbox

Patch

diff --git a/simplify.c b/simplify.c
index 29ec6e6f3..ad32fe08a 100644
--- a/simplify.c
+++ b/simplify.c
@@ -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;
diff --git a/validation/linear/bitfield-init-zero.c b/validation/optim/bitfield-init-zero.c
similarity index 82%
rename from validation/linear/bitfield-init-zero.c
rename to validation/optim/bitfield-init-zero.c
index 16422c19c..e619d1d25 100644
--- a/validation/linear/bitfield-init-zero.c
+++ b/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
 
diff --git a/validation/bitfield-size.c b/validation/optim/bitfield-size.c
similarity index 93%
rename from validation/bitfield-size.c
rename to validation/optim/bitfield-size.c
index a39c39660..a82586796 100644
--- a/validation/bitfield-size.c
+++ b/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
  */