[PATCH 1/2] sparse, llvm: improve pointer arithmetic handling

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Converting pointers to integers for pointer arithmetic effectively
disables pointer analysis and future optimizations.  A better way is to
use LLVM's GEP, by converting pointers to `char *' rather than integers.

Cc: Pekka Enberg <penberg@xxxxxxxxxx>
Acked-by: Jonathan Neuschäfer <j.neuschaefer@xxxxxxx>
Signed-off-by: Xi Wang <xi.wang@xxxxxxxxx>
---
calc_gep() may be useful for fixing array access as well.
---
 sparse-llvm.c | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/sparse-llvm.c b/sparse-llvm.c
index 41e0ab7..02f43f7 100644
--- a/sparse-llvm.c
+++ b/sparse-llvm.c
@@ -370,6 +370,22 @@ static LLVMValueRef pseudo_to_value(struct function *fn, struct instruction *ins
 	return result;
 }
 
+static LLVMValueRef calc_gep(LLVMBuilderRef builder, LLVMValueRef base, LLVMValueRef off)
+{
+	LLVMTypeRef type = LLVMTypeOf(base);
+	unsigned int as = LLVMGetPointerAddressSpace(type);
+	LLVMTypeRef bytep = LLVMPointerType(LLVMInt8Type(), as);
+	LLVMValueRef addr;
+
+	/* convert base to char* type */
+	base = LLVMBuildPointerCast(builder, base, bytep, "");
+	/* addr = base + off */
+	addr = LLVMBuildInBoundsGEP(builder, base, &off, 1, "");
+	/* convert back to the actual pointer type */
+	addr = LLVMBuildPointerCast(builder, addr, type, "");
+	return addr;
+}
+
 static LLVMRealPredicate translate_fop(int opcode)
 {
 	static const LLVMRealPredicate trans_tbl[] = {
@@ -544,23 +560,21 @@ static void output_op_ret(struct function *fn, struct instruction *insn)
 static LLVMValueRef calc_memop_addr(struct function *fn, struct instruction *insn)
 {
 	LLVMTypeRef int_type, addr_type;
-	LLVMValueRef src_p, src_i, ofs_i, addr_i, addr;
+	LLVMValueRef src, off, addr;
+	unsigned int as;
 
 	/* int type large enough to hold a pointer */
 	int_type = LLVMIntType(bits_in_pointer);
+	off = LLVMConstInt(int_type, insn->offset, 0);
 
-	/* convert to integer, add src + offset */
-	src_p = pseudo_to_value(fn, insn, insn->src);
-	src_i = LLVMBuildPtrToInt(fn->builder, src_p, int_type, "src_i");
-
-	ofs_i = LLVMConstInt(int_type, insn->offset, 0);
-	addr_i = LLVMBuildAdd(fn->builder, src_i, ofs_i, "addr_i");
-
-	addr_type = LLVMPointerType(insn_symbol_type(fn->module, insn), 0);
-
-	/* convert address back to pointer */
-	addr = LLVMBuildIntToPtr(fn->builder, addr_i, addr_type, "addr");
+	/* convert src to the effective pointer type */
+	src = pseudo_to_value(fn, insn, insn->src);
+	as = LLVMGetPointerAddressSpace(LLVMTypeOf(src));
+	addr_type = LLVMPointerType(insn_symbol_type(fn->module, insn), as);
+	src = LLVMBuildPointerCast(fn->builder, src, addr_type, "");
 
+	/* addr = src + off */
+	addr = calc_gep(fn->builder, src, off);
 	return addr;
 }
 
-- 
1.8.1.2

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux