linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Xi Wang <xi.wang@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: "Xi Wang" <xi.wang@gmail.com>,
	"Pekka Enberg" <penberg@kernel.org>,
	"Jonathan Neuschäfer" <j.neuschaefer@gmx.net>
Subject: [PATCH 2/2] sparse, llvm: set target specification
Date: Sun, 19 May 2013 08:13:10 -0400	[thread overview]
Message-ID: <1368965590-6714-2-git-send-email-xi.wang@gmail.com> (raw)
In-Reply-To: <1368965590-6714-1-git-send-email-xi.wang@gmail.com>

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

  reply	other threads:[~2013-05-19 12:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-19 12:13 [PATCH 1/2] sparse, llvm: improve pointer arithmetic handling Xi Wang
2013-05-19 12:13 ` Xi Wang [this message]
2013-05-19 12:49 ` Pekka Enberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1368965590-6714-2-git-send-email-xi.wang@gmail.com \
    --to=xi.wang@gmail.com \
    --cc=j.neuschaefer@gmx.net \
    --cc=linux-sparse@vger.kernel.org \
    --cc=penberg@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).