* [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
* [PATCH 2/2] sparse, llvm: set target specification
2013-05-19 12:13 [PATCH 1/2] sparse, llvm: improve pointer arithmetic handling Xi Wang
@ 2013-05-19 12:13 ` Xi Wang
2013-05-19 12:49 ` [PATCH 1/2] sparse, llvm: improve pointer arithmetic handling Pekka Enberg
1 sibling, 0 replies; 3+ messages in thread
From: Xi Wang @ 2013-05-19 12:13 UTC (permalink / raw)
To: linux-sparse; +Cc: Xi Wang, Pekka Enberg, Jonathan Neuschäfer
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 <penberg@kernel.org>
Cc: Jonathan Neuschäfer <j.neuschaefer@gmx.net>
Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
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(-)
diff --git a/lib.h b/lib.h
index 680ad8f..5ab2bd9 100644
--- a/lib.h
+++ b/lib.h
@@ -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);
diff --git a/sparse-llvm.c b/sparse-llvm.c
index 02f43f7..09eedb8 100644
--- a/sparse-llvm.c
+++ b/sparse-llvm.c
@@ -6,6 +6,7 @@
#include <llvm-c/Core.h>
#include <llvm-c/BitWriter.h>
#include <llvm-c/Analysis.h>
+#include <llvm-c/Target.h>
#include <stdbool.h>
#include <stdio.h>
@@ -1066,14 +1067,63 @@ static int compile(LLVMModuleRef module, struct symbol_list *list)
return 0;
}
+#define X86_LINUX_LAYOUT \
+ "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" \
+ "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-" \
+ "a0:0:64-f80:32:32-n8:16:32-S128"
+
+#define X86_64_LINUX_LAYOUT \
+ "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" \
+ "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-" \
+ "a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+
+static void set_target(LLVMModuleRef module)
+{
+ char target[] = LLVM_DEFAULT_TARGET_TRIPLE;
+ const char *arch, *vendor, *os, *env, *layout = NULL;
+ char triple[256];
+
+ arch = strtok(target, "-");
+ vendor = strtok(NULL, "-");
+ os = strtok(NULL, "-");
+ env = strtok(NULL, "-");
+
+ if (!os)
+ return;
+ if (!env)
+ env = "unknown";
+
+ if (!strcmp(arch, "x86_64") && !strcmp(os, "linux")) {
+ if (arch_m64) {
+ layout = X86_64_LINUX_LAYOUT;
+ } else {
+ arch = "i386";
+ layout = X86_LINUX_LAYOUT;
+ }
+ }
+
+ /* unsupported target */
+ if (!layout)
+ return;
+
+ snprintf(triple, sizeof(triple), "%s-%s-%s-%s", arch, vendor, os, env);
+ LLVMSetTarget(module, triple);
+ LLVMSetDataLayout(module, layout);
+}
+
int main(int argc, char **argv)
{
- struct string_list * filelist = NULL;
+ struct string_list *filelist = NULL;
+ struct symbol_list *symlist;
+ LLVMModuleRef module;
char *file;
- LLVMModuleRef module = LLVMModuleCreateWithName("sparse");
+ symlist = sparse_initialize(argc, argv, &filelist);
+
+ module = LLVMModuleCreateWithName("sparse");
+ set_target(module);
- compile(module, sparse_initialize(argc, argv, &filelist));
+ compile(module, symlist);
/* need ->phi_users */
dbg_dead = 1;
--
1.8.1.2
--
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] sparse, llvm: improve pointer arithmetic handling
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 ` Pekka Enberg
1 sibling, 0 replies; 3+ messages in thread
From: Pekka Enberg @ 2013-05-19 12:49 UTC (permalink / raw)
To: Xi Wang; +Cc: Sparse Mailing-list
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
^ permalink raw reply [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).