1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/* Rich optional information on why an optimization wasn't possible.
Copyright (C) 2018-2022 Free Software Foundation, Inc.
Contributed by David Malcolm <dmalcolm@redhat.com>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple.h"
#include "pretty-print.h"
#include "opt-problem.h"
#include "dump-context.h"
#include "tree-pass.h"
#include "selftest.h"
/* opt_problem's ctor.
Use FMT and AP to emit a message to the "immediate" dump destinations
as if via:
dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, ...)
The optinfo_item instances are not emitted yet. Instead, they
are retained internally so that the message can be replayed and
emitted when this problem is handled, higher up the call stack. */
opt_problem::opt_problem (const dump_location_t &loc,
const char *fmt, va_list *ap)
: m_optinfo (loc, OPTINFO_KIND_FAILURE, current_pass)
{
/* We shouldn't be bothering to construct these objects if
dumping isn't enabled. */
gcc_assert (dump_enabled_p ());
/* Update the singleton. */
delete s_the_problem;
s_the_problem = this;
/* Print the location to the "immediate" dump destinations. */
dump_context &dc = dump_context::get ();
dc.dump_loc (MSG_MISSED_OPTIMIZATION, loc.get_user_location ());
/* Print the formatted string to this opt_problem's optinfo, dumping
the items to the "immediate" dump destinations, and storing items
for later retrieval. */
{
dump_pretty_printer pp (&dump_context::get (), MSG_MISSED_OPTIMIZATION);
text_info text;
text.err_no = errno;
text.args_ptr = ap;
text.format_spec = fmt; /* No i18n is performed. */
/* Phases 1 and 2, using pp_format. */
pp_format (&pp, &text);
/* Phase 3: dump the items to the "immediate" dump destinations,
and storing them into m_optinfo for later retrieval. */
pp.emit_items (&m_optinfo);
}
}
/* Emit this problem and delete it, clearing the current opt_problem. */
void
opt_problem::emit_and_clear ()
{
gcc_assert (this == s_the_problem);
m_optinfo.emit_for_opt_problem ();
delete this;
s_the_problem = NULL;
}
/* The singleton opt_problem *. */
opt_problem *opt_problem::s_the_problem;
#if CHECKING_P
namespace selftest {
static opt_result
function_that_succeeds ()
{
return opt_result::success ();
}
/* Verify that opt_result::success works. */
static void
test_opt_result_success ()
{
/* Run all tests twice, with and then without dumping enabled. */
for (int i = 0 ; i < 2; i++)
{
bool with_dumping = (i == 0);
temp_dump_context tmp (with_dumping, with_dumping,
MSG_ALL_KINDS | MSG_ALL_PRIORITIES);
if (with_dumping)
gcc_assert (dump_enabled_p ());
else
gcc_assert (!dump_enabled_p ());
opt_result res = function_that_succeeds ();
/* Verify that "success" can be used as a "true" boolean. */
ASSERT_TRUE (res);
/* Verify the underlying opt_wrapper<bool>. */
ASSERT_TRUE (res.get_result ());
ASSERT_EQ (res.get_problem (), NULL);
/* Nothing should have been dumped. */
ASSERT_DUMPED_TEXT_EQ (tmp, "");
optinfo *info = tmp.get_pending_optinfo ();
ASSERT_EQ (info, NULL);
}
}
/* Example of a function that fails, with a non-trivial
pre-canned error message. */
static opt_result
function_that_fails (const greturn *stmt)
{
gcc_assert (stmt);
gcc_assert (gimple_return_retval (stmt));
AUTO_DUMP_SCOPE ("function_that_fails", stmt);
return opt_result::failure_at (stmt,
"can't handle return type: %T for stmt: %G",
TREE_TYPE (gimple_return_retval (stmt)),
static_cast <const gimple *> (stmt));
}
/* Example of a function that indirectly fails. */
static opt_result
function_that_indirectly_fails (const greturn *stmt)
{
AUTO_DUMP_SCOPE ("function_that_indirectly_fails", stmt);
opt_result res = function_that_fails (stmt);
if (!res)
return res;
return opt_result::success ();
}
/* Verify that opt_result::failure_at works.
Simulate a failure handling a stmt at one location whilst considering
an optimization that's notionally at another location (as a microcosm
of e.g. a problematic statement within a loop that prevents loop
vectorization). */
static void
test_opt_result_failure_at (const line_table_case &case_)
{
/* Generate a location_t for testing. */
line_table_test ltt (case_);
const line_map_ordinary *ord_map
= linemap_check_ordinary (linemap_add (line_table, LC_ENTER, false,
"test.c", 0));
linemap_line_start (line_table, 5, 100);
/* A test location: "test.c:5:10". */
const location_t line_5 = linemap_position_for_column (line_table, 10);
/* Another test location: "test.c:6:12". */
const location_t line_6
= linemap_position_for_line_and_column (line_table, ord_map, 6, 12);
if (line_6 > LINE_MAP_MAX_LOCATION_WITH_COLS)
return;
/* Generate statements using "line_5" and "line_6" for testing. */
greturn *stmt_at_5 = gimple_build_return (integer_one_node);
gimple_set_location (stmt_at_5, line_5);
greturn *stmt_at_6 = gimple_build_return (integer_zero_node);
gimple_set_location (stmt_at_6, line_6);
/* Run with and then without dumping enabled. */
for (int i = 0; i < 2; i++)
{
bool with_dumping = (i == 0);
/* Run with all 4 combinations of
with and without MSG_PRIORITY_INTERNALS and
with and without MSG_PRIORITY_REEMITTED. */
for (int j = 0; j < 4; j++)
{
dump_flags_t filter = MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING;
if (j / 2)
filter |= MSG_PRIORITY_INTERNALS;
if (j % 2)
filter |= MSG_PRIORITY_REEMITTED;
temp_dump_context tmp (with_dumping, with_dumping, filter);
if (with_dumping)
gcc_assert (dump_enabled_p ());
else
gcc_assert (!dump_enabled_p ());
/* Simulate attempting to optimize "stmt_at_6". */
opt_result res = function_that_indirectly_fails (stmt_at_6);
/* Verify that "failure" can be used as a "false" boolean. */
ASSERT_FALSE (res);
/* Verify the underlying opt_wrapper<bool>. */
ASSERT_FALSE (res.get_result ());
opt_problem *problem = res.get_problem ();
if (with_dumping)
{
ASSERT_NE (problem, NULL);
ASSERT_EQ (problem->get_dump_location ().get_location_t (),
line_6);
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
/* Verify that the problem captures the implementation location
it was emitted from. */
const dump_impl_location_t &impl_location
= problem->get_dump_location ().get_impl_location ();
ASSERT_STR_CONTAINS (impl_location.m_function,
"function_that_fails");
#endif
/* Verify that the underlying dump items are retained in the
opt_problem. */
const optinfo &info = problem->get_optinfo ();
ASSERT_EQ (info.get_dump_location ().get_location_t (), line_6);
ASSERT_EQ (info.num_items (), 4);
ASSERT_IS_TEXT (info.get_item (0), "can't handle return type: ");
ASSERT_IS_TREE (info.get_item (1), UNKNOWN_LOCATION, "int");
ASSERT_IS_TEXT (info.get_item (2), " for stmt: ");
ASSERT_IS_GIMPLE (info.get_item (3), line_6, "return 0;\n");
/* ...but not in the dump_context's pending_optinfo. */
ASSERT_EQ (tmp.get_pending_optinfo (), NULL);
/* Simulate emitting a high-level summary message, followed
by the problem. */
dump_printf_loc (MSG_MISSED_OPTIMIZATION, stmt_at_5,
"can't optimize loop\n");
problem->emit_and_clear ();
ASSERT_EQ (res.get_problem (), NULL);
/* Verify that the error message was dumped (when the failure
occurred). We can't use a switch here as not all of the
values are const expressions (using C++98). */
dump_flags_t effective_filter
= filter & (MSG_PRIORITY_INTERNALS | MSG_PRIORITY_REEMITTED);
if (effective_filter
== (MSG_PRIORITY_INTERNALS | MSG_PRIORITY_REEMITTED))
/* The -fopt-info-internals case. */
ASSERT_DUMPED_TEXT_EQ
(tmp,
"test.c:6:12: note: === function_that_indirectly_fails"
" ===\n"
"test.c:6:12: note: === function_that_fails ===\n"
"test.c:6:12: missed: can't handle return type: int"
" for stmt: return 0;\n"
"test.c:5:10: missed: can't optimize loop\n"
"test.c:6:12: missed: can't handle return type: int"
" for stmt: return 0;\n");
else if (effective_filter == MSG_PRIORITY_INTERNALS)
/* The default for dump files. */
ASSERT_DUMPED_TEXT_EQ
(tmp,
"test.c:6:12: note: === function_that_indirectly_fails"
" ===\n"
"test.c:6:12: note: === function_that_fails ===\n"
"test.c:6:12: missed: can't handle return type: int"
" for stmt: return 0;\n"
"test.c:5:10: missed: can't optimize loop\n");
else if (effective_filter == MSG_PRIORITY_REEMITTED)
/* The default when -fopt-info is enabled. */
ASSERT_DUMPED_TEXT_EQ
(tmp,
"test.c:5:10: missed: can't optimize loop\n"
"test.c:6:12: missed: can't handle return type: int"
" for stmt: return 0;\n");
else
{
gcc_assert (effective_filter == 0);
ASSERT_DUMPED_TEXT_EQ
(tmp,
"test.c:5:10: missed: can't optimize loop\n");
}
}
else
{
/* If dumping was disabled, then no problem should have been
created, and nothing should have been dumped. */
ASSERT_EQ (problem, NULL);
ASSERT_DUMPED_TEXT_EQ (tmp, "");
}
}
}
}
/* Run all of the selftests within this file. */
void
c_opt_problem_cc_tests ()
{
test_opt_result_success ();
for_each_line_table_case (test_opt_result_failure_at);
}
} // namespace selftest
#endif /* CHECKING_P */
|