From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x34.google.com (mail-oa1-x34.google.com [IPv6:2001:4860:4864:20::34]) by sourceware.org (Postfix) with ESMTPS id C339E3858D33 for ; Thu, 27 Apr 2023 11:48:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C339E3858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-oa1-x34.google.com with SMTP id 586e51a60fabf-18ef8e9aa00so1386114fac.1 for ; Thu, 27 Apr 2023 04:48:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1682596092; x=1685188092; h=mime-version:user-agent:message-id:date:errors-to:organization :subject:to:from:from:to:cc:subject:date:message-id:reply-to; bh=yboyFPoPjsTNLkGW9qbBtzZzYTuldWXuhvWLZHtMhF8=; b=NwiGZfuSkLZ0nk3fHJ8nTRLk/59WxxU+QwSgMC4U3cLcUge9WVG9B1FX4kMAdqRHAv 7lRTO5TbA8qwbYF+lzUdCzwfZVK0zKYZ3xpoe4wxdQZv6DAfcxdBAQvu77FyYD8kV85P WBUZISvIVobMfsjE/qOmOywQywFR6zRA4lAqhvDzsyZco313r2UsbpRD5UuYM0z+GJoK t3KOzxKzvOQd/Pv+d1+c4oiXjRbCM2K83vBvwgouPP0pXsXqkRg3fAdbLszc4Fckjtgc rZcIVdT+XZRWFnE5fO4VQDPZJtNAAa9aJdmY6Ajjz4JRj2dxVVhh/836g6/CSXroWBOH jeoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682596092; x=1685188092; h=mime-version:user-agent:message-id:date:errors-to:organization :subject:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=yboyFPoPjsTNLkGW9qbBtzZzYTuldWXuhvWLZHtMhF8=; b=YyK1syc04Wx42j57LodEIO8S32AZe4PxYu//tVmuioGG4w3tKVC1Lc24aJ1D9Sj/Rq TwuxsHxriPOk+EKDbqZ3HOGo5ZaTsTo/65FirDUMkiVRjO/0Wo0H76QGW/l8Vw19GPO5 3eS9E9bWBk4+45/KXH1gOSZjXBO38ZMdedTBfwvGn6ZlENJn1gnlK+iagpQIgxM+Pf5O Df6oj5kZQJssFR3w4fyUXHgiMeHaCdmuXMAKKCrYp57fTi42pCZ2/JSSqacchvNs9wzp vW3OdU8/ygNhbRLX0a1xeZFrswQWt+sXmxbfIHUTlzcI/ALL0AOSlYaBHmqf83porTqH 7IbA== X-Gm-Message-State: AC+VfDx+tF8sHoKzC4Q1uSxoMWL/lfLMoRBYBHkDjXbHPD/kJ+LqrVcz NCJ13AM5AaaphIPbqgZr/kQ78Vx2mke5XccO1Es= X-Google-Smtp-Source: ACHHUZ4+mk9/3VERTk2DvOsULU9zCD8tvuM9FVXzLKBBT2gdNlroSv4csvebiu4pnjJF9UOEYroWAQ== X-Received: by 2002:a05:6870:715:b0:187:c4ee:f527 with SMTP id ea21-20020a056870071500b00187c4eef527mr733183oab.0.1682596091923; Thu, 27 Apr 2023 04:48:11 -0700 (PDT) Received: from free.home ([2804:7f1:2080:112e:6b9f:4a49:b083:4727]) by smtp.gmail.com with ESMTPSA id g3-20020a056870c38300b0018456166c7asm7626087oao.39.2023.04.27.04.48.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Apr 2023 04:48:11 -0700 (PDT) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 33RBm1a52459074 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 27 Apr 2023 08:48:01 -0300 From: Alexandre Oliva To: gcc-patches@gcc.gnu.org Subject: harden-conditionals: detach values before compares Organization: Free thinker, does not speak for AdaCore Errors-To: aoliva@lxoliva.fsfla.org Date: Thu, 27 Apr 2023 08:48:01 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-11.0 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,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: The optimization barriers inserted after compares enable GCC to derive information about the values from e.g. the taken paths, or the absence of exceptions. Move them before the original compares, so that the reversed compares test copies of the original operands, without further optimizations. Regstrapped on x86_64-linux-gnu, and also bootstrapped with both passes enabled. Further tested on multiple other targets with gcc-12. Ok to install? for gcc/ChangeLog * gimple-harden-conditionals.cc (insert_edge_check_and_trap): Move detach value calls... (pass_harden_conditional_branches::execute): ... here. (pass_harden_compares::execute): Detach values before compares. for gcc/testsuite/ChangeLog * c-c++-common/torture/harden-cond-comp.c: New. --- gcc/gimple-harden-conditionals.cc | 25 ++++++++++++-------- .../c-c++-common/torture/harden-cond-comp.c | 24 +++++++++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/torture/harden-cond-comp.c diff --git a/gcc/gimple-harden-conditionals.cc b/gcc/gimple-harden-conditionals.cc index 78b8d5692d76f..2e5a42e9e71b1 100644 --- a/gcc/gimple-harden-conditionals.cc +++ b/gcc/gimple-harden-conditionals.cc @@ -276,8 +276,8 @@ insert_check_and_trap (location_t loc, gimple_stmt_iterator *gsip, } /* Split edge E, and insert_check_and_trap (see above) in the - newly-created block, using detached copies of LHS's and RHS's - values (see detach_value above) for the COP compare. */ + newly-created block, using already-detached copies of LHS's and + RHS's values (see detach_value above) for the COP compare. */ static inline void insert_edge_check_and_trap (location_t loc, edge e, @@ -301,10 +301,6 @@ insert_edge_check_and_trap (location_t loc, edge e, gimple_stmt_iterator gsik = gsi_after_labels (chk); - bool same_p = (lhs == rhs); - lhs = detach_value (loc, &gsik, lhs); - rhs = same_p ? lhs : detach_value (loc, &gsik, rhs); - insert_check_and_trap (loc, &gsik, flags, cop, lhs, rhs); } @@ -366,6 +362,12 @@ pass_harden_conditional_branches::execute (function *fun) /* ??? Can we do better? */ continue; + /* Detach the values before the compares. If we do so later, + the compiler may use values inferred from the compares. */ + bool same_p = (lhs == rhs); + lhs = detach_value (loc, &gsi, lhs); + rhs = same_p ? lhs : detach_value (loc, &gsi, rhs); + insert_edge_check_and_trap (loc, EDGE_SUCC (bb, 0), cop, lhs, rhs); insert_edge_check_and_trap (loc, EDGE_SUCC (bb, 1), cop, lhs, rhs); } @@ -508,6 +510,13 @@ pass_harden_compares::execute (function *fun) tree rhs = copy_ssa_name (lhs); + /* Detach the values before the compares, so that the + compiler infers nothing from them, not even from a + throwing compare that didn't throw. */ + bool same_p = (op1 == op2); + op1 = detach_value (loc, &gsi, op1); + op2 = same_p ? op1 : detach_value (loc, &gsi, op2); + gimple_stmt_iterator gsi_split = gsi; /* Don't separate the original assignment from debug stmts that might be associated with it, and arrange to split the @@ -529,10 +538,6 @@ pass_harden_compares::execute (function *fun) gimple_bb (asgn)->index, nbb->index); } - bool same_p = (op1 == op2); - op1 = detach_value (loc, &gsi_split, op1); - op2 = same_p ? op1 : detach_value (loc, &gsi_split, op2); - gassign *asgnck = gimple_build_assign (rhs, cop, op1, op2); gimple_set_location (asgnck, loc); gsi_insert_before (&gsi_split, asgnck, GSI_SAME_STMT); diff --git a/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c b/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c new file mode 100644 index 0000000000000..5aad890a1d3b6 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/harden-cond-comp.c @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-options "-fharden-conditional-branches -fharden-compares -fdump-tree-hardcbr -fdump-tree-hardcmp -ffat-lto-objects" } */ + +int f(int i, int j) { + if (i == 0) + return j != 0; + else + return i * j != 0; +} + +/* { dg-final { scan-tree-dump-times "Splitting edge" 2 "hardcbr" } } */ +/* { dg-final { scan-tree-dump-times "Adding reversed compare" 2 "hardcbr" } } */ +/* { dg-final { scan-tree-dump-times "__builtin_trap" 2 "hardcbr" } } */ + +/* { dg-final { scan-tree-dump-times "Splitting block" 2 "hardcmp" } } */ +/* { dg-final { scan-tree-dump-times "Adding reversed compare" 2 "hardcmp" } } */ +/* { dg-final { scan-tree-dump-times "__builtin_trap" 4 "hardcmp" } } */ + +/* Check that the optimization barrier is placed before the original compare. */ +/* { dg-final { scan-tree-dump-times {__asm__[(]"" : "=g" _[0-9]* : "0" i_[0-9]*[(]D[)][)][;][\n][ ]*if [(]i_[0-9]*[(]D[)] == 0[)]} 1 "hardcbr" } } */ +/* { dg-final { scan-tree-dump-times {if [(]_[0-9]* != 0[)]} 2 "hardcbr" } } */ + +/* { dg-final { scan-tree-dump-times {__asm__[(]"" : "=g" _[0-9]* : "0" j_[0-9]*[(]D[)][)][;][\n][ ]*_[0-9]* = j_[0-9]*[(]D[)] != 0;[\n] *_[0-9]* = _[0-9]* == 0} 1 "hardcmp" } } */ +/* { dg-final { scan-tree-dump-times {__asm__[(]"" : "=g" _[0-9]* : "0" _[0-9]*[)][;][\n][ ]*_[0-9]* = _[0-9]* != 0;[\n] *_[0-9]* = _[0-9]* == 0} 1 "hardcmp" } } */ -- Alexandre Oliva, happy hacker https://FSFLA.org/blogs/lxo/ Free Software Activist GNU Toolchain Engineer Disinformation flourishes because many people care deeply about injustice but very few check the facts. Ask me about