From d290efa0319b0327a6dc804a4b9ecad0e8b5a5bb Mon Sep 17 00:00:00 2001 From: xiezhiheng Date: Thu, 23 Nov 2023 11:01:45 +0800 Subject: [PATCH 4/4] [Backport] Learn GIMPLE pretty printer to produce nicer dump output. Reference: https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=5edb1c48f8e526a2b8f7f9d03fce9a7fdcb14b88 But we only port part of changes about attr-hotcold-2.c becasue the rest is irrelevant. [Backport] Recover GOTO predictor. Reference: https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=7fef86d3486c9f4208a111a41a2cc66b7328b6d9 --- gcc/c/c-typeck.c | 1 + gcc/cp/constexpr.c | 1 + gcc/cp/pt.c | 2 ++ gcc/cp/semantics.c | 2 ++ gcc/gimplify.c | 4 +++- gcc/predict.def | 5 ++--- gcc/testsuite/gcc.dg/predict-15.c | 17 +++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/attr-hotcold-2.c | 13 ++++++------- gcc/testsuite/gcc.dg/tree-ssa/vrp24.c | 10 ++++------ 9 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/predict-15.c diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index ee365313c..cf8463da7 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -9816,6 +9816,7 @@ c_finish_goto_label (location_t loc, tree label) return NULL_TREE; TREE_USED (decl) = 1; { + add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN)); tree t = build1 (GOTO_EXPR, void_type_node, decl); SET_EXPR_LOCATION (t, loc); return add_stmt (t); diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 9082230b9..adae14b4f 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -5827,6 +5827,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, case CLEANUP_STMT: case EMPTY_CLASS_EXPR: + case PREDICT_EXPR: return false; case GOTO_EXPR: diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 5687bb212..b536a54af 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -15161,6 +15161,8 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) return tsubst_binary_left_fold (t, args, complain, in_decl); case BINARY_RIGHT_FOLD_EXPR: return tsubst_binary_right_fold (t, args, complain, in_decl); + case PREDICT_EXPR: + return t; default: /* We shouldn't get here, but keep going if !flag_checking. */ diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index e06637646..840d193b9 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -41,6 +41,7 @@ along with GCC; see the file COPYING3. If not see #include "omp-general.h" #include "convert.h" #include "gomp-constants.h" +#include "predict.h" /* There routines provide a modular interface to perform many parsing operations. They may therefore be used during actual parsing, or @@ -628,6 +629,7 @@ finish_goto_stmt (tree destination) check_goto (destination); + add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN)); return add_stmt (build_stmt (input_location, GOTO_EXPR, destination)); } diff --git a/gcc/gimplify.c b/gcc/gimplify.c index c6a06d014..dfc2fddd8 100644 --- a/gcc/gimplify.c +++ b/gcc/gimplify.c @@ -2023,7 +2023,9 @@ should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label) gsi = *gsi_p; /* Skip all immediately following labels. */ - while (!gsi_end_p (gsi) && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL) + while (!gsi_end_p (gsi) + && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL + || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT)) gsi_next (&gsi); /* { ... something; default:; } */ diff --git a/gcc/predict.def b/gcc/predict.def index e96be12be..d7048e433 100644 --- a/gcc/predict.def +++ b/gcc/predict.def @@ -141,9 +141,8 @@ DEF_PREDICTOR (PRED_RECURSIVE_CALL, "recursive call", HITRATE (75), 0) this from FE or retire the predictor. */ DEF_PREDICTOR (PRED_TREE_EARLY_RETURN, "early return (on trees)", HITRATE (54), 0) -/* Branch containing goto is probably not taken. - FIXME: Currently not used. */ -DEF_PREDICTOR (PRED_GOTO, "goto", HITRATE (70), 0) +/* Branch containing goto is probably not taken. */ +DEF_PREDICTOR (PRED_GOTO, "goto", HITRATE (66), 0) /* Branch ending with return constant is probably not taken. */ DEF_PREDICTOR (PRED_CONST_RETURN, "const return", HITRATE (69), 0) diff --git a/gcc/testsuite/gcc.dg/predict-15.c b/gcc/testsuite/gcc.dg/predict-15.c new file mode 100644 index 000000000..2a8c3ea85 --- /dev/null +++ b/gcc/testsuite/gcc.dg/predict-15.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-profile_estimate" } */ + +int main(int argc, char **argv) +{ + if (argc == 123) + goto exit; + else + { + return 0; + } + +exit: + return 1; +} + +/* { dg-final { scan-tree-dump "goto heuristics of edge" "profile_estimate"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/attr-hotcold-2.c b/gcc/testsuite/gcc.dg/tree-ssa/attr-hotcold-2.c index 184dd10dd..17526113d 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/attr-hotcold-2.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/attr-hotcold-2.c @@ -1,8 +1,7 @@ /* { dg-do compile } */ -/* { dg-options "-O2 -fdump-tree-profile_estimate-blocks-details" } */ +/* { dg-options "-O2 -fdump-tree-profile_estimate" } */ -void g(void); -void h(void); +int v1, v2; void f(int x, int y) { if (x) goto A; @@ -10,19 +9,19 @@ void f(int x, int y) return; A: __attribute__((cold)) - g(); + v1 = x; return; B: __attribute__((hot)) - h(); + v2 = y; return; } /* { dg-final { scan-tree-dump-times "hot label heuristics" 1 "profile_estimate" } } */ /* { dg-final { scan-tree-dump-times "cold label heuristics" 1 "profile_estimate" } } */ -/* { dg-final { scan-tree-dump "A \\\[0\\\..*\\\]" "profile_estimate" } } */ +/* { dg-final { scan-tree-dump-times "combined heuristics: 0\\\..*" 1 "profile_estimate" } } */ /* Note: we're attempting to match some number > 6000, i.e. > 60%. The exact number ought to be tweekable without having to juggle the testcase around too much. */ -/* { dg-final { scan-tree-dump "B \\\[\[6-9\]\[0-9\]\\\..*\\\]" "profile_estimate" } } */ +/* { dg-final { scan-tree-dump-times "combined heuristics: \[6-9\]\[0-9\]\\\..*" 1 "profile_estimate" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp24.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp24.c index 853ee21bb..ed49e25f8 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/vrp24.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp24.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-O2 -fno-tree-forwprop -fdump-tree-vrp1-details" } */ +/* { dg-options "-O2 -fno-tree-forwprop -fdump-tree-vrp1-details -fdump-tree-optimized" } */ struct rtx_def; @@ -86,10 +86,8 @@ L7: /* The first n_sets > 0 test can be simplfiied into n_sets == 1 since n_sets can only have the values [0, 1] as it's the result of a - boolean operation. + boolean operation. */ - The second n_sets > 0 test can also be simplified into n_sets == 1 - as the only way to reach the tests is when n_sets <= 1 and the only - value which satisfies both conditions is n_sets == 1. */ -/* { dg-final { scan-tree-dump-times "Simplified relational" 2 "vrp1" } } */ +/* { dg-final { scan-tree-dump-times "Simplified relational" 1 "vrp1" } } */ +/* { dg-final { scan-tree-dump-times "if " 4 "optimized" } } */ -- 2.19.1