Skip to contentSkip to conversation

sparse, llvm: improve pointer arithmetic handling

PATCH In review Xi Wang 2 participants 3 messages May 19, 2013 Sparse checker

lore
Share & tools
lore

Xi Wang

author
#0 lore raw

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

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.

---
calc_gep() may be useful for fixing array access as well.

---
 sparse-llvm.c | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)
sparse-llvm.c +19 -9
index 41e0ab7..02f43f7 100644
@@ -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);
author
#1 lore raw

PATCH 2/2 sparse, llvm: set target specification

Set target triple and data layout, which are required by LLVM's backend.

Also export arch_m64 for choosing the target architecture.

Cc Pekka Enberg <[email protected]>
Cc Jonathan Neuschäfer <[email protected]>

---
With the two patches, we will be able to recover struct access.

Consider t.c:

	struct A { int x, y; };

	void foo(struct A *a)
	{
		a->x = 123;
		a->y = 456;
	}
$ ./sparse-llvm t.c | opt -S -O2
...
define void @foo(%A* nocapture) #0 {
L0:
  %1 = getelementptr inbounds %A* %0, i64 0, i32 0
  store i32 123, i32* %1, align 4
  %2 = getelementptr inbounds %A* %0, i64 0, i32 1
  store i32 456, i32* %2, align 4
  ret void
}
---
 lib.h         |  2 ++
 sparse-llvm.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 55 insertions(+), 3 deletions(-)
lib.h +2 -0
index 680ad8f..5ab2bd9 100644
@@ -113,6 +113,8 @@ extern int Wvla;
 extern int dbg_entry;
 extern int dbg_dead;
 
+extern int arch_m64;
+
 extern void declare_builtin_functions(void);
 extern void create_builtin_stream(void);
 extern struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **files);
sparse-llvm.c +53 -3

73 lines not on this page · raw message

Signature · 6 lines
1.8.1.2

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
In reply to Xi Wang
Quoted 7 lines · Xi Wang

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.

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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html