From patchwork Fri Oct 25 11:54:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 2002323 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=SyTpr+DI; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XZlp70yF3z1xwy for ; Sat, 26 Oct 2024 01:41:03 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0DFA83858280 for ; Fri, 25 Oct 2024 14:41:01 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by sourceware.org (Postfix) with ESMTPS id 5CA243858C98 for ; Fri, 25 Oct 2024 14:39:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5CA243858C98 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5CA243858C98 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::52a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729867152; cv=none; b=CMjQoxqdFxpw4EviJQmxmXyOgB4dtvn1nHk3syWpyjojbEQdKYdB5R60ypPDGGDuewJc2Z+ZITvyZUWyFaDmQ2g8ysCHQ02QvkPQ0V3zeeanPN6yiNHBaBmbTVve+7ykr5IIIombE2ShWbq/rXVxRLQ1h6ibJ1PB5zF+x6TybKg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729867152; c=relaxed/simple; bh=eYTWm/V5fxJc31xy7On5KnH/rP7Jki6+HNAxHw/RJvc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=OyHz1lwYt6ZLDMqg09OD9f+wDJf4AQhBfLKzjyBTyO9GVNoUnJtjE0SEGOOPDHuSlkjdTUtV4h2L0wMrkc6X9rJNMZqcUKC4Y0ecFnnu3sGq4dvsYVp89dV5HAL9ll2Nc4OeN+X6MuipjAwBrYOsNWM029/xP+oGCV+DlUQAz6Q= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-7ea7ad1e01fso1438372a12.0 for ; Fri, 25 Oct 2024 07:39:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1729867146; x=1730471946; darn=gcc.gnu.org; h=mime-version:user-agent:message-id:in-reply-to:date:references :organization:subject:cc:to:from:from:to:cc:subject:date:message-id :reply-to; bh=nbBGZwgzs6IZx8bn6+hmib4uefLaBoyaT+hVpGRYyk0=; b=SyTpr+DIJiOzIPYRGaK281grAOj63fLREIEENUCGYYZW/vI10q0AvuCnSYXIft7Wgw kdLz2TJme/3xfLMN7gQBkad9QPQxi6YchaWpXlZTd7LqTds3IL+XYJ27dWvkKBsJr5RF pqL24OPQ3T6gixotfb4cGdHxk4n2jmHH5+HJc6xPfecEDkepL76rGfndAOyNjTMFN0li jKFGyT6QalPmdyZYXtGvHdUhOoEGMxzsb0OidueACpHGrUMCoWnJPdPaQV3vd68DbHm+ QB6VSynzJAAYxQcR3DaFuZIbSBrQ718p0U28qcOKd1SthPR5S+ObtyJDuk35DbebFO9o iIjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729867146; x=1730471946; h=mime-version:user-agent:message-id:in-reply-to:date:references :organization:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nbBGZwgzs6IZx8bn6+hmib4uefLaBoyaT+hVpGRYyk0=; b=gRT9AQTQftMnKEIs7kS3UTO8Ghq6VIiYuyRuwxgQNFgQrD6hRB0t7blsUEkTh1z2vj TD22klfPwL9IZRvRg7ThG5cLZG++juFMAFMbWFVwLqpu45SzZnHUEFDj3NCG57wfXV5T BnE1D4C9hg+p+bOFPZ1ngWhlf0yNMjn87ENHhxG/3MYboAEwiWZnQ8grzPDDL53U0xpX ThfAnG1El+yCMT0OhvVgQo1/AJC45dEkyX5xOwMnS3IyC8t+hi8GsekmqR5AFjfFfd+E +lOqixN9ktZsuvIAmPrK6iovmIucTlVlzrUtW6oDLC+C5tvn8tSo9KiGvDYjWWPX6UqL ScdQ== X-Gm-Message-State: AOJu0Yze73sQY6pH22CXkUz5Fj6JvGR+okRDzLO5xnD13PQ4W6pNktiT AzDAiHk0UzdoBP8LtX0PJFaWW++TbMvYjp22jeXREfZtux6LgFNn66RbOT4TpF7mrI/n6bANiHA qFw== X-Google-Smtp-Source: AGHT+IHsbNjfkEL1gw+2ncHh/nd2l2/DOwnSyA1X0SplE7sfCdZxxWm0H+54BT3dgauCTsaLFPPztg== X-Received: by 2002:a05:6a21:394a:b0:1d9:3acf:8bdd with SMTP id adf61e73a8af0-1d988997592mr9988071637.22.1729867146133; Fri, 25 Oct 2024 07:39:06 -0700 (PDT) Received: from free.home ([2804:14c:4d1:44a5::1002]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7205791d9basm1123267b3a.43.2024.10.25.07.39.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2024 07:39:05 -0700 (PDT) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 49PBsGcq1633877 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 25 Oct 2024 08:54:16 -0300 From: Alexandre Oliva To: Richard Biener Cc: GCC Patches Subject: [PATCH #4/7] adjust update_profile_after_ifcombine for noncontiguous ifcombine (was: Re: [PATCH] fold fold_truth_andor field merging into ifcombine) Organization: Free thinker, does not speak for AdaCore References: Date: Fri, 25 Oct 2024 08:54:16 -0300 In-Reply-To: (Alexandre Oliva's message of "Fri, 25 Oct 2024 08:07:13 -0300") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, WEIRD_QUOTING autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Prepare for ifcombining noncontiguous blocks, adding (still unused) logic to the ifcombine profile updater to handle such cases. for gcc/ChangeLog * tree-ssa-ifcombine.cc (known_succ_p): New. (update_profile_after_ifcombine): Handle noncontiguous blocks. --- gcc/tree-ssa-ifcombine.cc | 109 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 85 insertions(+), 24 deletions(-) diff --git a/gcc/tree-ssa-ifcombine.cc b/gcc/tree-ssa-ifcombine.cc index 6dcf5e6efe1de..b5b72be29bbf9 100644 --- a/gcc/tree-ssa-ifcombine.cc +++ b/gcc/tree-ssa-ifcombine.cc @@ -49,6 +49,21 @@ along with GCC; see the file COPYING3. If not see false) >= 2) #endif +/* Return FALSE iff the COND_BB ends with a conditional whose result is not a + known constant. */ + +static bool +known_succ_p (basic_block cond_bb) +{ + gcond *cond = safe_dyn_cast (*gsi_last_bb (cond_bb)); + + if (!cond) + return true; + + return (CONSTANT_CLASS_P (gimple_cond_lhs (cond)) + && CONSTANT_CLASS_P (gimple_cond_rhs (cond))); +} + /* This pass combines COND_EXPRs to simplify control flow. It currently recognizes bit tests and comparisons in chains that represent logical and or logical or of two COND_EXPRs. @@ -356,14 +371,28 @@ recognize_bits_test (gcond *cond, tree *name, tree *bits, bool inv) } -/* Update profile after code in outer_cond_bb was adjusted so - outer_cond_bb has no condition. */ +/* Update profile after code in either outer_cond_bb or inner_cond_bb was + adjusted so that it has no condition. */ static void update_profile_after_ifcombine (basic_block inner_cond_bb, basic_block outer_cond_bb) { - edge outer_to_inner = find_edge (outer_cond_bb, inner_cond_bb); + /* In the following we assume that inner_cond_bb has single predecessor. */ + gcc_assert (single_pred_p (inner_cond_bb)); + + basic_block outer_to_inner_bb = inner_cond_bb; + profile_probability prob = profile_probability::always (); + for (;;) + { + basic_block parent = single_pred (outer_to_inner_bb); + prob *= find_edge (parent, outer_to_inner_bb)->probability; + if (parent == outer_cond_bb) + break; + outer_to_inner_bb = parent; + } + + edge outer_to_inner = find_edge (outer_cond_bb, outer_to_inner_bb); edge outer2 = (EDGE_SUCC (outer_cond_bb, 0) == outer_to_inner ? EDGE_SUCC (outer_cond_bb, 1) : EDGE_SUCC (outer_cond_bb, 0)); @@ -374,29 +403,61 @@ update_profile_after_ifcombine (basic_block inner_cond_bb, std::swap (inner_taken, inner_not_taken); gcc_assert (inner_taken->dest == outer2->dest); - /* In the following we assume that inner_cond_bb has single predecessor. */ - gcc_assert (single_pred_p (inner_cond_bb)); - - /* Path outer_cond_bb->(outer2) needs to be merged into path - outer_cond_bb->(outer_to_inner)->inner_cond_bb->(inner_taken) - and probability of inner_not_taken updated. */ - - inner_cond_bb->count = outer_cond_bb->count; + if (outer_to_inner_bb == inner_cond_bb + && known_succ_p (outer_cond_bb)) + { + /* Path outer_cond_bb->(outer2) needs to be merged into path + outer_cond_bb->(outer_to_inner)->inner_cond_bb->(inner_taken) + and probability of inner_not_taken updated. */ + + inner_cond_bb->count = outer_cond_bb->count; + + /* Handle special case where inner_taken probability is always. In this + case we know that the overall outcome will be always as well, but + combining probabilities will be conservative because it does not know + that outer2->probability is inverse of + outer_to_inner->probability. */ + if (inner_taken->probability == profile_probability::always ()) + ; + else + inner_taken->probability = outer2->probability + + outer_to_inner->probability * inner_taken->probability; + inner_not_taken->probability = profile_probability::always () + - inner_taken->probability; - /* Handle special case where inner_taken probability is always. In this case - we know that the overall outcome will be always as well, but combining - probabilities will be conservative because it does not know that - outer2->probability is inverse of outer_to_inner->probability. */ - if (inner_taken->probability == profile_probability::always ()) - ; + outer_to_inner->probability = profile_probability::always (); + outer2->probability = profile_probability::never (); + } + else if (known_succ_p (inner_cond_bb)) + { + /* Path inner_cond_bb->(inner_taken) needs to be merged into path + outer_cond_bb->(outer2). We've accumulated the probabilities from + outer_cond_bb->(outer)->...->inner_cond_bb in prob, so we have to + adjust that by inner_taken, and make inner unconditional. */ + + prob *= inner_taken->probability; + outer2->probability += prob; + outer_to_inner->probability = profile_probability::always () + - outer2->probability; + + inner_taken->probability = profile_probability::never (); + inner_not_taken->probability = profile_probability::always (); + } else - inner_taken->probability = outer2->probability + outer_to_inner->probability - * inner_taken->probability; - inner_not_taken->probability = profile_probability::always () - - inner_taken->probability; - - outer_to_inner->probability = profile_probability::always (); - outer2->probability = profile_probability::never (); + { + /* We've moved part of the inner cond to outer, but we don't know the + probabilities for each part, so estimate the effects by moving half of + the odds of inner_taken to outer. */ + + inner_taken->probability *= profile_probability::even (); + inner_not_taken->probability = profile_probability::always () + - inner_taken->probability; + + prob *= inner_taken->probability; + outer2->probability += prob; + outer_to_inner->probability = profile_probability::always () + - outer2->probability; + } } /* Replace the conditions in INNER_COND with COND.