From patchwork Sun May 19 12:13:09 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Xi Wang X-Patchwork-Id: 2590151 Return-Path: X-Original-To: patchwork-linux-sparse@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 37B9DDF24C for ; Sun, 19 May 2013 12:14:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751177Ab3ESMOl (ORCPT ); Sun, 19 May 2013 08:14:41 -0400 Received: from mail-gh0-f181.google.com ([209.85.160.181]:46317 "EHLO mail-gh0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751380Ab3ESMOk (ORCPT ); Sun, 19 May 2013 08:14:40 -0400 Received: by mail-gh0-f181.google.com with SMTP id z12so1277096ghb.26 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:mime-version :content-type:content-transfer-encoding; bh=HtKXWaBr9gaz9tYzHnL9Ixb7Nj1N5EWAiiqsUsPg3UA=; b=Ti3rcjai8WE03oikpn3XuyfAI3k3iRWUXVR2zeD1lRvlwB3p/jyzjYkOg6LCT7Pn7s lWFb8WJIOa6a+JKA18u16p3ybPwxKNimFQ5ob0eD2YuDHI8J4CP4vEDhO4ELmkZK88Qi xqqxdWRRionbo8syiSznnDXI3ao1ztKxuCkrbq7IPY73ZhdjzXnXDnttkYDKKweiv9qF +llqf3L7OMUOPOEwisfKZZyT9Z0sj16WjmWwTSJtAnPyQe0EC/hX173VZtqY+75wkcPF ZOkt7BJkkbhomanFQUWmv70kd50SkbLdqmCN3jN70VQGSov3iZkE/uVZv5nnnpjZ6ibg I9sw== X-Received: by 10.236.227.234 with SMTP id d100mr22886827yhq.134.1368965679950; Sun, 19 May 2013 05:14:39 -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.38 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 19 May 2013 05:14:39 -0700 (PDT) From: Xi Wang To: linux-sparse@vger.kernel.org Cc: Xi Wang , Pekka Enberg Subject: [PATCH 1/2] sparse, llvm: improve pointer arithmetic handling Date: Sun, 19 May 2013 08:13:09 -0400 Message-Id: <1368965590-6714-1-git-send-email-xi.wang@gmail.com> X-Mailer: git-send-email 1.8.1.2 MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org 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 Acked-by: Jonathan Neuschäfer Signed-off-by: Xi Wang --- 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); - - /* convert address back to pointer */ - addr = LLVMBuildIntToPtr(fn->builder, addr_i, addr_type, "addr"); + /* convert src to the effective pointer type */ + src = pseudo_to_value(fn, insn, insn->src); + as = LLVMGetPointerAddressSpace(LLVMTypeOf(src)); + addr_type = LLVMPointerType(insn_symbol_type(fn->module, insn), as); + src = LLVMBuildPointerCast(fn->builder, src, addr_type, ""); + /* addr = src + off */ + addr = calc_gep(fn->builder, src, off); return addr; }