linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] sparse, llvm: improve pointer arithmetic handling
@ 2013-05-19 12:13 Xi Wang
  2013-05-19 12:13 ` [PATCH 2/2] sparse, llvm: set target specification Xi Wang
  2013-05-19 12:49 ` [PATCH 1/2] sparse, llvm: improve pointer arithmetic handling Pekka Enberg
  0 siblings, 2 replies; 3+ messages in thread
From: Xi Wang @ 2013-05-19 12:13 UTC (permalink / raw)
  To: linux-sparse; +Cc: Xi Wang, Pekka Enberg

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(-)

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

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-05-19 12:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-19 12:13 [PATCH 1/2] sparse, llvm: improve pointer arithmetic handling Xi Wang
2013-05-19 12:13 ` [PATCH 2/2] sparse, llvm: set target specification Xi Wang
2013-05-19 12:49 ` [PATCH 1/2] sparse, llvm: improve pointer arithmetic handling Pekka Enberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).