From patchwork Sat Mar 11 09:06:44 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9618465 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D8029604A9 for ; Sat, 11 Mar 2017 09:07:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C5717287AD for ; Sat, 11 Mar 2017 09:07:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BA573287B3; Sat, 11 Mar 2017 09:07:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E2DB6287AD for ; Sat, 11 Mar 2017 09:07:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755348AbdCKJHd (ORCPT ); Sat, 11 Mar 2017 04:07:33 -0500 Received: from mail-wr0-f196.google.com ([209.85.128.196]:32817 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753648AbdCKJH1 (ORCPT ); Sat, 11 Mar 2017 04:07:27 -0500 Received: by mail-wr0-f196.google.com with SMTP id g10so14110516wrg.0 for ; Sat, 11 Mar 2017 01:07:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Y4AwQYN9gR2aLzeEKJ3vlcA8xw+VZIijzC1caVhNUrc=; b=kgugDlARHF5CY8Ua9DMgjGHl7s8l8DBhynX4Qc1sY5sJ7vb1hoNSQhpseenG8TDryN vnJkpJjFCXQm1oOlmWWetXezgqXM+cIPx4yyEZOm2nR+zLRTCPd5RrXAiG+dmBnjtDOS PxxSS0wXAAjB/8/GaAydNzrz/pDOhB+pX1sWaTWxk1KxkCITR2JxcHs9xgT8QclXYvXq Ivwj8BxwyvMx0d0R/KsXvE6pox8ZiFtciEgHlSC+DkS+96GBZav2NMfEpFyVGDwBohVK 9W6hkbq89yfAtvPZgmVtN519Pr979ujGkfT3w+LX8N3PftcXPfzj3Fca7W/9bVOJ+KyA QaLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Y4AwQYN9gR2aLzeEKJ3vlcA8xw+VZIijzC1caVhNUrc=; b=skaIVP+KVIAF82fSKeO1HPezq0Ji0VIEdm2Fi6bErYzr7KNu7mqXcZ//vyX6j/RGPW ya5dCOnA4OSwRKGD5KatyWkh7XrO6p+EPiHp43hCrGFWDBeZQJ/uOX3orfvq4dfYBCQr I17QJolhIup/0I6P+oI4FPZ/UrST578hvIE6R5ZWXf6BlK5IRGMjx6xjXS1RGVmR22w2 MnmdthEJhwdEFBbFDT/WEL6aXOuHE1HcP3pLlJoK9f1DXlSqBf776v5GS/0eTSpSPXQN WDkLAIXyn9hTwYGmtY3KTFIyys88i/awcZPb7aGItiNQJ3RoFiNszm2ygloSgfApBW/H e92Q== X-Gm-Message-State: AMke39n6r+KbK/NpplDaUSdBLmJ8e2/OQZmHAGx0VAWsucxoFnc8Jif4E24G6H2gNLPE2A== X-Received: by 10.223.176.217 with SMTP id j25mr20828863wra.8.1489223245107; Sat, 11 Mar 2017 01:07:25 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:8df:aa00:8824:8320:e6e0:1e9b]) by smtp.gmail.com with ESMTPSA id m80sm2871510wmi.34.2017.03.11.01.07.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 11 Mar 2017 01:07:24 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Dibyendu Majumdar , Pekka Enberg , Jeff Garzik , Luc Van Oostenryck Subject: [PATCH v2 05/27] llvm: fix translation of PSEUDO_VALs into a ValueRefs Date: Sat, 11 Mar 2017 10:06:44 +0100 Message-Id: <20170311090706.17171-6-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170311090706.17171-1-luc.vanoostenryck@gmail.com> References: <20170311090706.17171-1-luc.vanoostenryck@gmail.com> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In sparse-llvm there is the assumption that a PSEUDO_VAL is always of integer type. But this is not always the case: constant pointers, like NULL, are also of the PSEUDO_VAL kind. Fix this by adding a helper 'val_to_value()' and using the instruction's type where this pseudo is used as the type of the value. Note: while this patch improve the situation, like for example for the test cases added here, it's still not correct because now we're making the assumption that 'insn->type' is the type we need for the pseudo. This is often true, but certainly not always. For example this is not true for: - OP_STORE/OP_LOAD's insn->src - OP_SET{EQ,...}'s insn->src[12] - probably some others ones - in general, obviously, for any instructions where the target has a different type than the operands. Reported-by: Dibyendu Majumdar Signed-off-by: Luc Van Oostenryck --- sparse-llvm.c | 26 +++++++++++++++++++++++++- validation/backend/null.c | 24 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 validation/backend/null.c diff --git a/sparse-llvm.c b/sparse-llvm.c index 9f362b3ed..d48b3b20a 100644 --- a/sparse-llvm.c +++ b/sparse-llvm.c @@ -303,6 +303,30 @@ static void pseudo_name(pseudo_t pseudo, char *buf) } } +static LLVMValueRef val_to_value(struct function *fn, unsigned long long val, struct symbol *ctype) +{ + LLVMTypeRef dtype; + LLVMTypeRef itype; + LLVMValueRef result; + + assert(ctype); + dtype = symbol_type(fn->module, ctype); + switch (LLVMGetTypeKind(dtype)) { + case LLVMPointerTypeKind: + itype = LLVMIntType(bits_in_pointer); + result = LLVMConstInt(itype, val, 1); + result = LLVMConstIntToPtr(result, dtype); + break; + case LLVMIntegerTypeKind: + result = LLVMConstInt(dtype, val, 1); + break; + default: + assert(0); + } + + return result; +} + static LLVMValueRef pseudo_to_value(struct function *fn, struct instruction *insn, pseudo_t pseudo) { LLVMValueRef result = NULL; @@ -360,7 +384,7 @@ static LLVMValueRef pseudo_to_value(struct function *fn, struct instruction *ins break; } case PSEUDO_VAL: - result = LLVMConstInt(insn_symbol_type(fn->module, insn), pseudo->value, 1); + result = val_to_value(fn, pseudo->value, insn->type); break; case PSEUDO_ARG: { result = LLVMGetParam(fn->fn, pseudo->nr - 1); diff --git a/validation/backend/null.c b/validation/backend/null.c new file mode 100644 index 000000000..5c595c70b --- /dev/null +++ b/validation/backend/null.c @@ -0,0 +1,24 @@ +extern int *ip[]; + +void foo(void); +void foo(void) +{ + ip[0] = (void *)0L; + ip[1] = (int *)0L; + ip[2] = (void *)0; + ip[3] = (int *)0; + ip[4] = (void *)(long)0; + ip[5] = (int *)(long)0; + ip[6] = (void *)123; + ip[7] = (int *)123; + ip[8] = (void *)123L; + ip[9] = (int *)123L; + ip[10] = (void *)(long)123; + ip[11] = (int *)(long)123; +} + +/* + * check-name: store constants to pointer + * check-command: sparse-llvm $file + * check-output-ignore + */