From patchwork Sun May 19 12:13:10 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Xi Wang X-Patchwork-Id: 2590161 Return-Path: X-Original-To: patchwork-linux-sparse@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id C5C113FDBC for ; Sun, 19 May 2013 12:14:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751447Ab3ESMOn (ORCPT ); Sun, 19 May 2013 08:14:43 -0400 Received: from mail-gg0-f177.google.com ([209.85.161.177]:61891 "EHLO mail-gg0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751380Ab3ESMOl (ORCPT ); Sun, 19 May 2013 08:14:41 -0400 Received: by mail-gg0-f177.google.com with SMTP id r4so1104111ggn.22 for ; Sun, 19 May 2013 05:14:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:mime-version:content-type:content-transfer-encoding; bh=FyxaAlil2iXsT5Q7rg2+gXJLw51+VXlqZZpomM+i0ac=; b=Ep1P9hlA1Hxlwf5a+O80Po7dQw38f8N0d/Z1x+5Y6f584E9LaSJBcjFAcYGNmUkmae Luu3EiCTSXx6TlN5mccEwYDQ6Gq2q+KUVGttwtX6cuY0MpdGe5bAtfRub//UHzwXZEBn ryhUUNqBKok/XVyO7GjY9s7jeKTZlYOtXstr46c1zIF67D4tAHRbNm3lXBM2lshpW3z0 ivcPHBmClwirP3ghxCGo0JIV4RrrIMIiEVuvNOvd46vy1KxlhSzMUI9/h2lr1k487HuN z9Fo8WrVe9skobfXMJxAOHWwGoaJ6FtE6IRiv2gM6am1SLlfsKRdfYoxP5WEX6rbLdTB SM9A== X-Received: by 10.236.97.4 with SMTP id s4mr31904495yhf.151.1368965680917; Sun, 19 May 2013 05:14:40 -0700 (PDT) Received: from hchen.csail.mit.edu (hchen.csail.mit.edu. [18.26.5.5]) by mx.google.com with ESMTPSA id i21sm32172236yhl.15.2013.05.19.05.14.39 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 19 May 2013 05:14:40 -0700 (PDT) From: Xi Wang To: linux-sparse@vger.kernel.org Cc: Xi Wang , Pekka Enberg , =?UTF-8?q?Jonathan=20Neusch=C3=A4fer?= Subject: [PATCH 2/2] sparse, llvm: set target specification Date: Sun, 19 May 2013 08:13:10 -0400 Message-Id: <1368965590-6714-2-git-send-email-xi.wang@gmail.com> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1368965590-6714-1-git-send-email-xi.wang@gmail.com> References: <1368965590-6714-1-git-send-email-xi.wang@gmail.com> MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org 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 Cc: Jonathan Neuschäfer Signed-off-by: Xi Wang --- 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 #include #include +#include #include #include @@ -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;