aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-06-22 17:25:39 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-06-29 23:19:40 +0200
commit3ad464ff247aa6c5039c9567960787d578c99462 (patch)
treebb15543e6cd439e3cb7f029943df1b692e6eb1de
parente46af66b3c2b1cb32c6394ac5e6c7ce632d905a9 (diff)
downloadsparse-dev-3ad464ff247aa6c5039c9567960787d578c99462.tar.gz
cast: optimize away casts to/from pointers
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>
-rw-r--r--simplify.c5
-rw-r--r--sparse-llvm.c4
-rw-r--r--validation/optim/cast-kinds.c24
-rw-r--r--validation/optim/cast-nop.c18
4 files changed, 33 insertions, 18 deletions
diff --git a/simplify.c b/simplify.c
index ad32fe08..a9183555 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 937f4490..9560713a 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/optim/cast-kinds.c b/validation/optim/cast-kinds.c
index 5df307bc..b144dc7e 100644
--- a/validation/optim/cast-kinds.c
+++ b/validation/optim/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 00000000..7741b7a7
--- /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\\.
+ */