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>
Subject: [PATCH 1/2] sparse, llvm: improve pointer arithmetic handling
Date: Sun, 19 May 2013 08:13:09 -0400 [thread overview]
Message-ID: <1368965590-6714-1-git-send-email-xi.wang@gmail.com> (raw)
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);
next 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 Xi Wang [this message]
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
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-1-git-send-email-xi.wang@gmail.com \
--to=xi.wang@gmail.com \
--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).