diff mbox

cast: optimize away casts to/from pointers

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

Commit Message

Luc Van Oostenryck June 26, 2018, 10:55 p.m. UTC
Now that all casts to or from a pointer are between a pointer
and a pointer-sized unsigned integer, from an optimization
PoV, they are all no-ops.

So, optimize them away at simplification time.

Note: casts between pointers (OP_PTRCAST) should also be
      optimized away but the original type is used for a
      number a things (for example in check_access()) and
      can't be optimized away so simply (yet).

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                     |  5 +++--
 sparse-llvm.c                  |  4 ++++
 validation/cast-weirds.c       |  6 ++----
 validation/linear/cast-kinds.c | 24 ++++++++----------------
 validation/optim/cast-nop.c    | 18 ++++++++++++++++++
 5 files changed, 35 insertions(+), 22 deletions(-)
 create mode 100644 validation/optim/cast-nop.c
diff mbox

Patch

diff --git a/simplify.c b/simplify.c
index ad32fe08a..a91835552 100644
--- a/simplify.c
+++ b/simplify.c
@@ -1210,12 +1210,13 @@  int simplify_instruction(struct instruction *insn)
 	case OP_FCVTU: case OP_FCVTS:
 	case OP_UCVTF: case OP_SCVTF:
 	case OP_FCVTF:
-	case OP_UTPTR:
-	case OP_PTRTU:
 	case OP_PTRCAST:
 		if (dead_insn(insn, &insn->src, NULL, NULL))
 			return REPEAT_CSE;
 		break;
+	case OP_UTPTR:
+	case OP_PTRTU:
+		return replace_with_pseudo(insn, insn->src);
 	case OP_PHI:
 		if (dead_insn(insn, NULL, NULL, NULL)) {
 			kill_use_list(insn->phi_list);
diff --git a/sparse-llvm.c b/sparse-llvm.c
index 937f4490c..9560713a3 100644
--- a/sparse-llvm.c
+++ b/sparse-llvm.c
@@ -660,6 +660,10 @@  static void output_op_compare(struct function *fn, struct instruction *insn)
 	case LLVMIntegerTypeKind: {
 		LLVMIntPredicate op = translate_op(insn->opcode);
 
+		if (LLVMGetTypeKind(LLVMTypeOf(rhs)) == LLVMPointerTypeKind) {
+			LLVMTypeRef ltype = LLVMTypeOf(lhs);
+			rhs = LLVMBuildPtrToInt(fn->builder, rhs, ltype, "");
+		}
 		target = LLVMBuildICmp(fn->builder, op, lhs, rhs, target_name);
 		break;
 	}
diff --git a/validation/cast-weirds.c b/validation/cast-weirds.c
index a99c65d25..df62aafd8 100644
--- a/validation/cast-weirds.c
+++ b/validation/cast-weirds.c
@@ -21,16 +21,14 @@  int_2_iptr:
 .L0:
 	<entry-point>
 	sext.64     %r2 <- (32) %arg1
-	utptr.64    %r3 <- (64) %r2
-	ret.64      %r3
+	ret.64      %r2
 
 
 uint_2_iptr:
 .L2:
 	<entry-point>
 	zext.64     %r6 <- (32) %arg1
-	utptr.64    %r7 <- (64) %r6
-	ret.64      %r7
+	ret.64      %r6
 
 
 int_2_vptr:
diff --git a/validation/linear/cast-kinds.c b/validation/linear/cast-kinds.c
index 5df307bc6..b144dc7ef 100644
--- a/validation/linear/cast-kinds.c
+++ b/validation/linear/cast-kinds.c
@@ -88,8 +88,7 @@  vptr_2_int:
 iptr_2_int:
 .L8:
 	<entry-point>
-	ptrtu.64    %r13 <- (64) %arg1
-	trunc.32    %r14 <- (64) %r13
+	trunc.32    %r14 <- (64) %arg1
 	ret.32      %r14
 
 
@@ -137,8 +136,7 @@  vptr_2_uint:
 iptr_2_uint:
 .L22:
 	<entry-point>
-	ptrtu.64    %r34 <- (64) %arg1
-	trunc.32    %r35 <- (64) %r34
+	trunc.32    %r35 <- (64) %arg1
 	ret.32      %r35
 
 
@@ -185,8 +183,7 @@  vptr_2_long:
 iptr_2_long:
 .L36:
 	<entry-point>
-	ptrtu.64    %r54 <- (64) %arg1
-	ret.64      %r54
+	ret.64      %arg1
 
 
 float_2_long:
@@ -232,8 +229,7 @@  vptr_2_ulong:
 iptr_2_ulong:
 .L50:
 	<entry-point>
-	ptrtu.64    %r73 <- (64) %arg1
-	ret.64      %r73
+	ret.64      %arg1
 
 
 float_2_ulong:
@@ -286,30 +282,26 @@  int_2_iptr:
 .L66:
 	<entry-point>
 	sext.64     %r94 <- (32) %arg1
-	utptr.64    %r95 <- (64) %r94
-	ret.64      %r95
+	ret.64      %r94
 
 
 uint_2_iptr:
 .L68:
 	<entry-point>
 	zext.64     %r98 <- (32) %arg1
-	utptr.64    %r99 <- (64) %r98
-	ret.64      %r99
+	ret.64      %r98
 
 
 long_2_iptr:
 .L70:
 	<entry-point>
-	utptr.64    %r102 <- (64) %arg1
-	ret.64      %r102
+	ret.64      %arg1
 
 
 ulong_2_iptr:
 .L72:
 	<entry-point>
-	utptr.64    %r105 <- (64) %arg1
-	ret.64      %r105
+	ret.64      %arg1
 
 
 vptr_2_iptr:
diff --git a/validation/optim/cast-nop.c b/validation/optim/cast-nop.c
new file mode 100644
index 000000000..7741b7a72
--- /dev/null
+++ b/validation/optim/cast-nop.c
@@ -0,0 +1,18 @@ 
+static long p2l(long *p)
+{
+	return (long) p;
+}
+
+static long *l2p(long l)
+{
+	return (long*)l;
+}
+
+/*
+ * check-name: cast-nop
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-excludes: utptr\\.
+ * check-output-excludes: ptrtu\\.
+ */