From patchwork Sat May 18 22:45:18 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Xi Wang X-Patchwork-Id: 2589471 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 80F37DF2E5 for ; Sat, 18 May 2013 22:45:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753384Ab3ERWpW (ORCPT ); Sat, 18 May 2013 18:45:22 -0400 Received: from mail-gh0-f178.google.com ([209.85.160.178]:32916 "EHLO mail-gh0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753347Ab3ERWpV (ORCPT ); Sat, 18 May 2013 18:45:21 -0400 Received: by mail-gh0-f178.google.com with SMTP id g15so311588ghb.37 for ; Sat, 18 May 2013 15:45:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:message-id:date:from:user-agent:mime-version:to:cc :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=3IiLvApBEqaf4UOGiPj/IqHgx40RtcC52Fi3mEBKWmw=; b=UfytQeasEyY5XaFbpuRttcECPoAEw/qcZSwBE+94A6i+eRC0ZvxVCxwCk3BI3CDw9j OnZYwsOvQqlGfrl3V0BxEcSP4YZ5lvVCXz12lXOQPvQHG38EIX1iMgkCb7f48VFDJatP eFzmfw4idCzg68cSB03WLPC/QARDVOXExm99wKVFvT6F1C/8u7469482z4iiekwmW3E3 KYHhSxoyg8YDYox0Ib1YvO5P+rW/FvKOyn2VtmMOg9zOC5G9Fr0wKVddX0W3a1WP6+Z0 89/KnCgsuwnQEBLUK6fbluLYDZVdAQ/5yRhg1fdlttWnwvE7K346Iu2kTB6Fn/9gM9hk BIiA== X-Received: by 10.236.132.106 with SMTP id n70mr31511646yhi.150.1368917120653; Sat, 18 May 2013 15:45:20 -0700 (PDT) Received: from [18.101.16.116] (VPN-18-101-16-116.MIT.EDU. [18.101.16.116]) by mx.google.com with ESMTPSA id a24sm28218940yhj.23.2013.05.18.15.45.19 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 18 May 2013 15:45:19 -0700 (PDT) Message-ID: <5198047E.5000806@gmail.com> Date: Sat, 18 May 2013 18:45:18 -0400 From: Xi Wang User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 To: =?UTF-8?B?Sm9uYXRoYW4gTmV1c2Now6RmZXI=?= CC: linux-sparse@vger.kernel.org, Pekka Enberg , Christopher Li , Jeff Garzik , Linus Torvalds Subject: Re: [PATCH 3/4] sparse, llvm: base load/store address type on insn_symbol_type() References: <1368899527-2350-1-git-send-email-j.neuschaefer@gmx.net> <1368899527-2350-3-git-send-email-j.neuschaefer@gmx.net> In-Reply-To: <1368899527-2350-3-git-send-email-j.neuschaefer@gmx.net> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org On 05/18/2013 01:52 PM, Jonathan Neuschäfer wrote: > /* convert address back to pointer */ > - addr = LLVMBuildIntToPtr(fn->builder, addr_i, > - LLVMTypeOf(src_p), "addr"); > + addr = LLVMBuildIntToPtr(fn->builder, addr_i, addr_type, "addr"); Actually, we shouldn't convert pointers to integers in the first place. This effectively disables pointer analysis and future optimizations. A better way is to use LLVM's GEP for pointer arithmetic, by converting pointers to `char *', rather than integers. See more examples here: http://www.spinics.net/lists/linux-sparse/msg02768.html Jonathan, how about this version using `char *' based on your patchset? --- 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 diff --git a/sparse-llvm.c b/sparse-llvm.c index 00ace6e..a01c4b7 100644 --- a/sparse-llvm.c +++ b/sparse-llvm.c @@ -541,24 +541,47 @@ static void output_op_ret(struct function *fn, struct instruction *insn) LLVMBuildRetVoid(fn->builder); } -static void output_op_load(struct function *fn, struct instruction *insn) +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 base back to the actual pointer type */ + addr = LLVMBuildPointerCast(builder, addr, type, ""); + return addr; +} + +static LLVMValueRef calc_memop_addr(struct function *fn, struct instruction *insn) { - LLVMTypeRef int_type; - LLVMValueRef src_p, src_i, ofs_i, addr_i, addr, target; + LLVMTypeRef addr_type, int_type; + LLVMValueRef src, base, off, addr; + unsigned int as; + + src = pseudo_to_value(fn, insn, insn->src); + as = LLVMGetPointerAddressSpace(LLVMTypeOf(src)); + addr_type = LLVMPointerType(insn_symbol_type(fn->module, insn), as); + base = LLVMBuildPointerCast(fn->builder, src, addr_type, ""); /* int type large enough to hold a pointer */ int_type = LLVMIntType(bits_in_pointer); + off = LLVMConstInt(int_type, insn->offset, 0); + + addr = calc_gep(fn->builder, base, off); + return addr; +} - /* 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"); +static void output_op_load(struct function *fn, struct instruction *insn) +{ + LLVMValueRef addr, target; - /* convert address back to pointer */ - addr = LLVMBuildIntToPtr(fn->builder, addr_i, - LLVMTypeOf(src_p), "addr"); + addr = calc_memop_addr(fn, insn); /* perform load */ target = LLVMBuildLoad(fn->builder, addr, "load_target"); @@ -568,22 +591,9 @@ static void output_op_load(struct function *fn, struct instruction *insn) static void output_op_store(struct function *fn, struct instruction *insn) { - LLVMTypeRef int_type; - LLVMValueRef src_p, src_i, ofs_i, addr_i, addr, target, target_in; - - /* int type large enough to hold a pointer */ - int_type = LLVMIntType(bits_in_pointer); - - /* 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"); + LLVMValueRef addr, target, target_in; - /* convert address back to pointer */ - addr = LLVMBuildIntToPtr(fn->builder, addr_i, - LLVMPointerType(int_type, 0), "addr"); + addr = calc_memop_addr(fn, insn); target_in = pseudo_to_value(fn, insn, insn->target); diff --git a/validation/backend/store-type.c b/validation/backend/store-type.c new file mode 100644 index 0000000..9e2ce73 --- /dev/null +++ b/validation/backend/store-type.c @@ -0,0 +1,12 @@ +struct foo; +static struct foo *var; + +static void set(struct foo *f) +{ + var = f; +} + +/* + * check-name: Type of stored objects + * check-command: ./sparsec -c $file -o tmp.o + */ diff --git a/validation/backend/struct-access.c b/validation/backend/struct-access.c new file mode 100644 index 0000000..884b470 --- /dev/null +++ b/validation/backend/struct-access.c @@ -0,0 +1,28 @@ +struct st { + int i, *d; +}; + +static int load_i(struct st *st) +{ + return st->i; +} + +static void store_i(struct st *st, int i) +{ + st->i = i; +} + +static int *load_d(struct st *st) +{ + return st->d; +} + +static void store_d(struct st *st, int *d) +{ + st->d = d; +} + +/* + * check-name: struct access code generation + * check-command: ./sparsec -c $file -o tmp.o + */