From patchwork Thu Nov 8 11:52:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 994811 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-489343-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="pOn8klc5"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42rM9370X5z9sBN for ; Thu, 8 Nov 2018 22:52:39 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to:cc :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=XJO5/bkDYaUt2mLS8pReKIWOcqEQ7ZpZkEEfBrv5oWOBvaJWOK UKD1zO+uNZFv3R0aznsxw/BpoHPY16cjurAsWQshiAZm1K/qKKNNA3E7EibZdYvt 8HwqknplrysHRzXZRJEUHHnljoaSr0GkUDYdFgKVvk0UPXI9O0VV2xmc4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to:cc :from:subject:message-id:date:mime-version:content-type; s= default; bh=fPMR1TF5VcVpNG633sAncd3ESbM=; b=pOn8klc5vks/68MHTNNB L0lLg4Y6B84jrdzfLuhA2lKWlU2LHU7QqtA2NZWp7K2I0CgH9PbLUOh/eQ5rqKgc fI6j8QwTyDxo8soU0ty+90+Pdkt8Q8KW0j+36BBV6QaOWnXH792WMi11Jga3qvY0 cxjn0npOnVeUiBygZlNA3cE= Received: (qmail 111080 invoked by alias); 8 Nov 2018 11:52:32 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 110382 invoked by uid 89); 8 Nov 2018 11:52:31 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 spammy=SIZE, gimplefoldc, gimple-fold.c, 6912 X-HELO: mail-wr1-f43.google.com Received: from mail-wr1-f43.google.com (HELO mail-wr1-f43.google.com) (209.85.221.43) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 08 Nov 2018 11:52:30 +0000 Received: by mail-wr1-f43.google.com with SMTP id y15-v6so20918238wru.9 for ; Thu, 08 Nov 2018 03:52:29 -0800 (PST) Received: from abulafia.quesejoda.com (128.red-88-6-116.staticip.rima-tde.net. [88.6.116.128]) by smtp.gmail.com with ESMTPSA id i10-v6sm3438926wrr.69.2018.11.08.03.52.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Nov 2018 03:52:27 -0800 (PST) To: gcc-patches Cc: Martin Sebor , Andrew MacLeod From: Aldy Hernandez Subject: Implement {get, set}_range_info() variants that work with value_range's Message-ID: Date: Thu, 8 Nov 2018 06:52:26 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 X-IsSubscribed: yes get/set_range_info() currently returns the extremes of a range. I have implemented overloaded variants that return a proper range. In the future we should use actual ranges throughout, and not depend on range extremes, as depending on this behavior could causes us to lose precision. I am also including changes to size_must_be_zero_p() to show how we should be using the range API, as opposed to performing error prone ad-hoc calculations on ranges and anti-ranges. Martin, I'm not saying your code is wrong. There are numerous other places in the compiler where we manipulate ranges/anti-ranges directly, all of which should be adapted in the future. Everywhere there is a mention of VR_RANGE/VR_ANTI_RANGE in the compiler is suspect. We should ideally be using intersect/union/may_contain_p/null_p, etc. OK pending another round of tests? (As I had tested this patch along with a whole slew of other upcoming changes ;-)). Aldy gcc/ * gimple-fold.c (size_must_be_zero_p): Use value_range API instead of performing ad-hoc calculations. * tree-ssanames.c (set_range_info): New overloaded function accepting value_range &. (get_range_info): Same. * tree-ssanames.h (set_range_info_raw): Remove. (set_range_info): New prototype. (get_range_info): Same. diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c index 5468b604dec..4d9bdcd097f 100644 --- a/gcc/gimple-fold.c +++ b/gcc/gimple-fold.c @@ -635,9 +635,8 @@ var_decl_component_p (tree var) && TREE_CODE (TREE_OPERAND (inner, 0)) == ADDR_EXPR)); } -/* If the SIZE argument representing the size of an object is in a range - of values of which exactly one is valid (and that is zero), return - true, otherwise false. */ +/* Return TRUE if the SIZE argument, representing the size of an + object, is in a range of values of which exactly zero is valid. */ static bool size_must_be_zero_p (tree size) @@ -648,21 +647,19 @@ size_must_be_zero_p (tree size) if (TREE_CODE (size) != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (size))) return false; - wide_int min, max; - enum value_range_kind rtype = get_range_info (size, &min, &max); - if (rtype != VR_ANTI_RANGE) - return false; - tree type = TREE_TYPE (size); int prec = TYPE_PRECISION (type); - wide_int wone = wi::one (prec); - /* Compute the value of SSIZE_MAX, the largest positive value that can be stored in ssize_t, the signed counterpart of size_t. */ wide_int ssize_max = wi::lshift (wi::one (prec), prec - 1) - 1; - - return wi::eq_p (min, wone) && wi::geu_p (max, ssize_max); + value_range valid_range (VR_RANGE, + build_int_cst (type, 0), + wide_int_to_tree (type, ssize_max)); + value_range vr; + get_range_info (size, vr); + vr.intersect (&valid_range); + return vr.null_p (); } /* Fold function call to builtin mem{{,p}cpy,move}. Try to detect and diff --git a/gcc/tree-ssanames.c b/gcc/tree-ssanames.c index ff906e831e5..a2c2efb634a 100644 --- a/gcc/tree-ssanames.c +++ b/gcc/tree-ssanames.c @@ -398,6 +398,15 @@ set_range_info (tree name, enum value_range_kind range_type, set_range_info_raw (name, range_type, min, max); } +/* Store range information for NAME from a value_range. */ + +void +set_range_info (tree name, const value_range &vr) +{ + wide_int min = wi::to_wide (vr.min ()); + wide_int max = wi::to_wide (vr.max ()); + set_range_info (name, vr.kind (), min, max); +} /* Gets range information MIN, MAX and returns enum value_range_kind corresponding to tree ssa_name NAME. enum value_range_kind returned @@ -421,6 +430,27 @@ get_range_info (const_tree name, wide_int *min, wide_int *max) return SSA_NAME_RANGE_TYPE (name); } +/* Gets range information corresponding to ssa_name NAME and stores it + in a value_range VR. Returns the value_range_kind. */ + +enum value_range_kind +get_range_info (const_tree name, value_range &vr) +{ + tree min, max; + wide_int wmin, wmax; + enum value_range_kind kind = get_range_info (name, &wmin, &wmax); + + if (kind == VR_VARYING || kind == VR_UNDEFINED) + min = max = NULL; + else + { + min = wide_int_to_tree (TREE_TYPE (name), wmin); + max = wide_int_to_tree (TREE_TYPE (name), wmax); + } + vr = value_range (kind, min, max); + return kind; +} + /* Set nonnull attribute to pointer NAME. */ void diff --git a/gcc/tree-ssanames.h b/gcc/tree-ssanames.h index 18a001a5461..a5ff14e524f 100644 --- a/gcc/tree-ssanames.h +++ b/gcc/tree-ssanames.h @@ -69,12 +69,11 @@ struct GTY ((variable_size)) range_info_def { /* Sets the value range to SSA. */ extern void set_range_info (tree, enum value_range_kind, const wide_int_ref &, const wide_int_ref &); -extern void set_range_info_raw (tree, enum value_range_kind, - const wide_int_ref &, - const wide_int_ref &); +extern void set_range_info (tree, const value_range &); /* Gets the value range from SSA. */ extern enum value_range_kind get_range_info (const_tree, wide_int *, wide_int *); +extern enum value_range_kind get_range_info (const_tree, value_range &); extern void set_nonzero_bits (tree, const wide_int_ref &); extern wide_int get_nonzero_bits (const_tree); extern bool ssa_name_has_boolean_range (tree);