diff mbox

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

Message ID 1368965590-6714-1-git-send-email-xi.wang@gmail.com (mailing list archive)
State Mainlined, archived
Headers show

Commit Message

Xi Wang May 19, 2013, 12:13 p.m. UTC
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@kernel.org>
Acked-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
calc_gep() may be useful for fixing array access as well.
---
 sparse-llvm.c | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)

Comments

Pekka Enberg May 19, 2013, 12:49 p.m. UTC | #1
On Sun, May 19, 2013 at 3:13 PM, Xi Wang <xi.wang@gmail.com> wrote:
> 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@kernel.org>
> Acked-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
> Signed-off-by: Xi Wang <xi.wang@gmail.com>

Applied both patches, thanks a lot Xi!
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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;
 }