comparison gcc/cp/cp-gimplify.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
1 /* C++-specific tree lowering bits; see also c-gimplify.c and tree-gimple.c. 1 /* C++-specific tree lowering bits; see also c-gimplify.c and tree-gimple.c.
2 2
3 Copyright (C) 2002-2017 Free Software Foundation, Inc. 3 Copyright (C) 2002-2018 Free Software Foundation, Inc.
4 Contributed by Jason Merrill <jason@redhat.com> 4 Contributed by Jason Merrill <jason@redhat.com>
5 5
6 This file is part of GCC. 6 This file is part of GCC.
7 7
8 GCC is free software; you can redistribute it and/or modify it under 8 GCC is free software; you can redistribute it and/or modify it under
29 #include "predict.h" 29 #include "predict.h"
30 #include "stor-layout.h" 30 #include "stor-layout.h"
31 #include "tree-iterator.h" 31 #include "tree-iterator.h"
32 #include "gimplify.h" 32 #include "gimplify.h"
33 #include "c-family/c-ubsan.h" 33 #include "c-family/c-ubsan.h"
34 #include "cilk.h"
35 #include "cp-cilkplus.h"
36 #include "stringpool.h" 34 #include "stringpool.h"
37 #include "attribs.h" 35 #include "attribs.h"
38 #include "asan.h" 36 #include "asan.h"
39 37
40 /* Forward declarations. */ 38 /* Forward declarations. */
217 215
218 if (cond && TREE_CODE (cond) != INTEGER_CST) 216 if (cond && TREE_CODE (cond) != INTEGER_CST)
219 { 217 {
220 /* If COND is constant, don't bother building an exit. If it's false, 218 /* If COND is constant, don't bother building an exit. If it's false,
221 we won't build a loop. If it's true, any exits are in the body. */ 219 we won't build a loop. If it's true, any exits are in the body. */
222 location_t cloc = EXPR_LOC_OR_LOC (cond, start_locus); 220 location_t cloc = cp_expr_loc_or_loc (cond, start_locus);
223 exit = build1_loc (cloc, GOTO_EXPR, void_type_node, 221 exit = build1_loc (cloc, GOTO_EXPR, void_type_node,
224 get_bc_label (bc_break)); 222 get_bc_label (bc_break));
225 exit = fold_build3_loc (cloc, COND_EXPR, void_type_node, cond, 223 exit = fold_build3_loc (cloc, COND_EXPR, void_type_node, cond,
226 build_empty_stmt (cloc), exit); 224 build_empty_stmt (cloc), exit);
227 } 225 }
330 cp_walk_tree (&body, cp_genericize_r, data, NULL); 328 cp_walk_tree (&body, cp_genericize_r, data, NULL);
331 cp_walk_tree (&cond, cp_genericize_r, data, NULL); 329 cp_walk_tree (&cond, cp_genericize_r, data, NULL);
332 cp_walk_tree (&type, cp_genericize_r, data, NULL); 330 cp_walk_tree (&type, cp_genericize_r, data, NULL);
333 *walk_subtrees = 0; 331 *walk_subtrees = 0;
334 332
335 *stmt_p = build3_loc (stmt_locus, SWITCH_EXPR, type, cond, body, NULL_TREE); 333 if (TREE_USED (break_block))
336 finish_bc_block (stmt_p, bc_break, break_block); 334 SWITCH_BREAK_LABEL_P (break_block) = 1;
335 finish_bc_block (&body, bc_break, break_block);
336 *stmt_p = build2_loc (stmt_locus, SWITCH_EXPR, type, cond, body);
337 SWITCH_ALL_CASES_P (*stmt_p) = SWITCH_STMT_ALL_CASES_P (stmt);
338 gcc_checking_assert (!SWITCH_STMT_NO_BREAK_P (stmt)
339 || !TREE_USED (break_block));
337 } 340 }
338 341
339 /* Genericize a CONTINUE_STMT node *STMT_P. */ 342 /* Genericize a CONTINUE_STMT node *STMT_P. */
340 343
341 static void 344 static void
574 577
575 int 578 int
576 cp_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p) 579 cp_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
577 { 580 {
578 int saved_stmts_are_full_exprs_p = 0; 581 int saved_stmts_are_full_exprs_p = 0;
579 location_t loc = EXPR_LOC_OR_LOC (*expr_p, input_location); 582 location_t loc = cp_expr_loc_or_loc (*expr_p, input_location);
580 enum tree_code code = TREE_CODE (*expr_p); 583 enum tree_code code = TREE_CODE (*expr_p);
581 enum gimplify_status ret; 584 enum gimplify_status ret;
582 585
583 if (STATEMENT_CODE_P (code)) 586 if (STATEMENT_CODE_P (code))
584 { 587 {
626 629
627 /* We used to do this for MODIFY_EXPR as well, but that's unsafe; the 630 /* We used to do this for MODIFY_EXPR as well, but that's unsafe; the
628 LHS of an assignment might also be involved in the RHS, as in bug 631 LHS of an assignment might also be involved in the RHS, as in bug
629 25979. */ 632 25979. */
630 case INIT_EXPR: 633 case INIT_EXPR:
631 if (fn_contains_cilk_spawn_p (cfun))
632 {
633 if (cilk_cp_detect_spawn_and_unwrap (expr_p))
634 return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
635 if (seen_error () && contains_cilk_spawn_stmt (*expr_p))
636 return GS_ERROR;
637 }
638
639 cp_gimplify_init_expr (expr_p); 634 cp_gimplify_init_expr (expr_p);
640 if (TREE_CODE (*expr_p) != INIT_EXPR) 635 if (TREE_CODE (*expr_p) != INIT_EXPR)
641 return GS_OK; 636 return GS_OK;
642 /* Fall through. */ 637 /* Fall through. */
643 case MODIFY_EXPR: 638 case MODIFY_EXPR:
644 modify_expr_case: 639 modify_expr_case:
645 { 640 {
646 if (fn_contains_cilk_spawn_p (cfun)
647 && cilk_cp_detect_spawn_and_unwrap (expr_p)
648 && !seen_error ())
649 return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
650 /* If the back end isn't clever enough to know that the lhs and rhs 641 /* If the back end isn't clever enough to know that the lhs and rhs
651 types are the same, add an explicit conversion. */ 642 types are the same, add an explicit conversion. */
652 tree op0 = TREE_OPERAND (*expr_p, 0); 643 tree op0 = TREE_OPERAND (*expr_p, 0);
653 tree op1 = TREE_OPERAND (*expr_p, 1); 644 tree op1 = TREE_OPERAND (*expr_p, 1);
654 645
757 : arg; 748 : arg;
758 ret = GS_OK; 749 ret = GS_OK;
759 } 750 }
760 break; 751 break;
761 752
762 case CILK_SPAWN_STMT:
763 gcc_assert(fn_contains_cilk_spawn_p (cfun)
764 && cilk_cp_detect_spawn_and_unwrap (expr_p));
765
766 if (!seen_error ())
767 return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
768 return GS_ERROR;
769
770 case CALL_EXPR: 753 case CALL_EXPR:
771 if (fn_contains_cilk_spawn_p (cfun)
772 && cilk_cp_detect_spawn_and_unwrap (expr_p)
773 && !seen_error ())
774 return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
775 ret = GS_OK; 754 ret = GS_OK;
776 if (!CALL_EXPR_FN (*expr_p)) 755 if (!CALL_EXPR_FN (*expr_p))
777 /* Internal function call. */; 756 /* Internal function call. */;
778 else if (CALL_EXPR_REVERSE_ARGS (*expr_p)) 757 else if (CALL_EXPR_REVERSE_ARGS (*expr_p))
779 { 758 {
802 else if (flag_strong_eval_order 781 else if (flag_strong_eval_order
803 && !CALL_EXPR_OPERATOR_SYNTAX (*expr_p)) 782 && !CALL_EXPR_OPERATOR_SYNTAX (*expr_p))
804 { 783 {
805 /* If flag_strong_eval_order, evaluate the object argument first. */ 784 /* If flag_strong_eval_order, evaluate the object argument first. */
806 tree fntype = TREE_TYPE (CALL_EXPR_FN (*expr_p)); 785 tree fntype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
807 if (POINTER_TYPE_P (fntype)) 786 if (INDIRECT_TYPE_P (fntype))
808 fntype = TREE_TYPE (fntype); 787 fntype = TREE_TYPE (fntype);
809 if (TREE_CODE (fntype) == METHOD_TYPE) 788 if (TREE_CODE (fntype) == METHOD_TYPE)
810 { 789 {
811 enum gimplify_status t 790 enum gimplify_status t
812 = gimplify_arg (&CALL_EXPR_ARG (*expr_p, 0), pre_p, loc); 791 = gimplify_arg (&CALL_EXPR_ARG (*expr_p, 0), pre_p, loc);
813 if (t == GS_ERROR) 792 if (t == GS_ERROR)
814 ret = GS_ERROR; 793 ret = GS_ERROR;
815 } 794 }
816 } 795 }
796 if (ret != GS_ERROR)
797 {
798 tree decl = cp_get_callee_fndecl_nofold (*expr_p);
799 if (decl
800 && fndecl_built_in_p (decl, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
801 BUILT_IN_FRONTEND))
802 *expr_p = boolean_false_node;
803 }
817 break; 804 break;
818 805
819 case RETURN_EXPR: 806 case RETURN_EXPR:
820 if (TREE_OPERAND (*expr_p, 0) 807 if (TREE_OPERAND (*expr_p, 0)
821 && (TREE_CODE (TREE_OPERAND (*expr_p, 0)) == INIT_EXPR 808 && (TREE_CODE (TREE_OPERAND (*expr_p, 0)) == INIT_EXPR
893 omp_var_to_track (tree decl) 880 omp_var_to_track (tree decl)
894 { 881 {
895 tree type = TREE_TYPE (decl); 882 tree type = TREE_TYPE (decl);
896 if (is_invisiref_parm (decl)) 883 if (is_invisiref_parm (decl))
897 type = TREE_TYPE (type); 884 type = TREE_TYPE (type);
898 else if (TREE_CODE (type) == REFERENCE_TYPE) 885 else if (TYPE_REF_P (type))
899 type = TREE_TYPE (type); 886 type = TREE_TYPE (type);
900 while (TREE_CODE (type) == ARRAY_TYPE) 887 while (TREE_CODE (type) == ARRAY_TYPE)
901 type = TREE_TYPE (type); 888 type = TREE_TYPE (type);
902 if (type == error_mark_node || !CLASS_TYPE_P (type)) 889 if (type == error_mark_node || !CLASS_TYPE_P (type))
903 return false; 890 return false;
947 dtor are instantiated, because during gimplification 934 dtor are instantiated, because during gimplification
948 it will be already too late. */ 935 it will be already too late. */
949 tree type = TREE_TYPE (decl); 936 tree type = TREE_TYPE (decl);
950 if (is_invisiref_parm (decl)) 937 if (is_invisiref_parm (decl))
951 type = TREE_TYPE (type); 938 type = TREE_TYPE (type);
952 else if (TREE_CODE (type) == REFERENCE_TYPE) 939 else if (TYPE_REF_P (type))
953 type = TREE_TYPE (type); 940 type = TREE_TYPE (type);
954 while (TREE_CODE (type) == ARRAY_TYPE) 941 while (TREE_CODE (type) == ARRAY_TYPE)
955 type = TREE_TYPE (type); 942 type = TREE_TYPE (type);
956 get_copy_ctor (type, tf_none); 943 get_copy_ctor (type, tf_none);
957 get_dtor (type, tf_none); 944 get_dtor (type, tf_none);
999 return NULL; 986 return NULL;
1000 } 987 }
1001 988
1002 code = TREE_CODE (stmt); 989 code = TREE_CODE (stmt);
1003 if (code == OMP_FOR || code == OMP_SIMD || code == OMP_DISTRIBUTE 990 if (code == OMP_FOR || code == OMP_SIMD || code == OMP_DISTRIBUTE
1004 || code == OMP_TASKLOOP || code == CILK_FOR || code == CILK_SIMD 991 || code == OMP_TASKLOOP || code == OACC_LOOP)
1005 || code == OACC_LOOP)
1006 { 992 {
1007 tree x; 993 tree x;
1008 int i, n; 994 int i, n;
1009 995
1010 cp_walk_tree (&OMP_FOR_BODY (stmt), cp_fold_r, data, NULL); 996 cp_walk_tree (&OMP_FOR_BODY (stmt), cp_fold_r, data, NULL);
1076 || TREE_CODE (stmt) == PARM_DECL 1062 || TREE_CODE (stmt) == PARM_DECL
1077 || TREE_CODE (stmt) == RESULT_DECL) 1063 || TREE_CODE (stmt) == RESULT_DECL)
1078 && omp_var_to_track (stmt)) 1064 && omp_var_to_track (stmt))
1079 omp_cxx_notice_variable (wtd->omp_ctx, stmt); 1065 omp_cxx_notice_variable (wtd->omp_ctx, stmt);
1080 1066
1067 /* Don't dereference parms in a thunk, pass the references through. */
1068 if ((TREE_CODE (stmt) == CALL_EXPR && CALL_FROM_THUNK_P (stmt))
1069 || (TREE_CODE (stmt) == AGGR_INIT_EXPR && AGGR_INIT_FROM_THUNK_P (stmt)))
1070 {
1071 *walk_subtrees = 0;
1072 return NULL;
1073 }
1074
1081 /* Dereference invisible reference parms. */ 1075 /* Dereference invisible reference parms. */
1082 if (wtd->handle_invisiref_parm_p && is_invisiref_parm (stmt)) 1076 if (wtd->handle_invisiref_parm_p && is_invisiref_parm (stmt))
1083 { 1077 {
1084 *stmt_p = convert_from_reference (stmt); 1078 *stmt_p = convert_from_reference (stmt);
1085 p_set->add (*stmt_p); 1079 p_set->add (*stmt_p);
1097 in.uid = DECL_UID (stmt); 1091 in.uid = DECL_UID (stmt);
1098 h = cp_function_chain->extern_decl_map->find_with_hash (&in, in.uid); 1092 h = cp_function_chain->extern_decl_map->find_with_hash (&in, in.uid);
1099 if (h) 1093 if (h)
1100 { 1094 {
1101 *stmt_p = h->to; 1095 *stmt_p = h->to;
1096 TREE_USED (h->to) |= TREE_USED (stmt);
1102 *walk_subtrees = 0; 1097 *walk_subtrees = 0;
1103 return NULL; 1098 return NULL;
1104 } 1099 }
1105 } 1100 }
1106 1101
1107 if (TREE_CODE (stmt) == INTEGER_CST 1102 if (TREE_CODE (stmt) == INTEGER_CST
1108 && TREE_CODE (TREE_TYPE (stmt)) == REFERENCE_TYPE 1103 && TYPE_REF_P (TREE_TYPE (stmt))
1109 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT)) 1104 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))
1110 && !wtd->no_sanitize_p) 1105 && !wtd->no_sanitize_p)
1111 { 1106 {
1112 ubsan_maybe_instrument_reference (stmt_p); 1107 ubsan_maybe_instrument_reference (stmt_p);
1113 if (*stmt_p != stmt) 1108 if (*stmt_p != stmt)
1306 if ((block = BIND_EXPR_BLOCK (wtd->bind_expr_stack[i]))) 1301 if ((block = BIND_EXPR_BLOCK (wtd->bind_expr_stack[i])))
1307 break; 1302 break;
1308 } 1303 }
1309 if (block) 1304 if (block)
1310 { 1305 {
1311 tree using_directive; 1306 tree decl = TREE_OPERAND (stmt, 0);
1312 gcc_assert (TREE_OPERAND (stmt, 0)); 1307 gcc_assert (decl);
1313 1308
1314 using_directive = make_node (IMPORTED_DECL); 1309 if (undeduced_auto_decl (decl))
1315 TREE_TYPE (using_directive) = void_type_node; 1310 /* Omit from the GENERIC, the back-end can't handle it. */;
1316 1311 else
1317 IMPORTED_DECL_ASSOCIATED_DECL (using_directive) 1312 {
1318 = TREE_OPERAND (stmt, 0); 1313 tree using_directive = make_node (IMPORTED_DECL);
1319 DECL_CHAIN (using_directive) = BLOCK_VARS (block); 1314 TREE_TYPE (using_directive) = void_type_node;
1320 BLOCK_VARS (block) = using_directive; 1315
1316 IMPORTED_DECL_ASSOCIATED_DECL (using_directive) = decl;
1317 DECL_CHAIN (using_directive) = BLOCK_VARS (block);
1318 BLOCK_VARS (block) = using_directive;
1319 }
1321 } 1320 }
1322 /* The USING_STMT won't appear in GENERIC. */ 1321 /* The USING_STMT won't appear in GENERIC. */
1323 *stmt_p = build1 (NOP_EXPR, void_type_node, integer_zero_node); 1322 *stmt_p = build1 (NOP_EXPR, void_type_node, integer_zero_node);
1324 *walk_subtrees = 0; 1323 *walk_subtrees = 0;
1325 } 1324 }
1418 location_t loc = location_of (stmt); 1417 location_t loc = location_of (stmt);
1419 if (TREE_NO_WARNING (stmt)) 1418 if (TREE_NO_WARNING (stmt))
1420 /* Never mind. */; 1419 /* Never mind. */;
1421 else if (wtd->try_block) 1420 else if (wtd->try_block)
1422 { 1421 {
1423 if (TREE_CODE (wtd->try_block) == MUST_NOT_THROW_EXPR 1422 if (TREE_CODE (wtd->try_block) == MUST_NOT_THROW_EXPR)
1424 && warning_at (loc, OPT_Wterminate, 1423 {
1425 "throw will always call terminate()") 1424 auto_diagnostic_group d;
1426 && cxx_dialect >= cxx11 1425 if (warning_at (loc, OPT_Wterminate,
1427 && DECL_DESTRUCTOR_P (current_function_decl)) 1426 "throw will always call terminate()")
1428 inform (loc, "in C++11 destructors default to noexcept"); 1427 && cxx_dialect >= cxx11
1428 && DECL_DESTRUCTOR_P (current_function_decl))
1429 inform (loc, "in C++11 destructors default to noexcept");
1430 }
1429 } 1431 }
1430 else 1432 else
1431 { 1433 {
1432 if (warn_cxx11_compat && cxx_dialect < cxx11 1434 if (warn_cxx11_compat && cxx_dialect < cxx11
1433 && DECL_DESTRUCTOR_P (current_function_decl) 1435 && DECL_DESTRUCTOR_P (current_function_decl)
1471 break; 1473 break;
1472 1474
1473 case OMP_FOR: 1475 case OMP_FOR:
1474 case OMP_SIMD: 1476 case OMP_SIMD:
1475 case OMP_DISTRIBUTE: 1477 case OMP_DISTRIBUTE:
1478 case OACC_LOOP:
1476 genericize_omp_for_stmt (stmt_p, walk_subtrees, data); 1479 genericize_omp_for_stmt (stmt_p, walk_subtrees, data);
1477 break; 1480 break;
1478 1481
1479 case PTRMEM_CST: 1482 case PTRMEM_CST:
1480 /* By the time we get here we're handing off to the back end, so we don't 1483 /* By the time we get here we're handing off to the back end, so we don't
1494 break; 1497 break;
1495 1498
1496 case NOP_EXPR: 1499 case NOP_EXPR:
1497 if (!wtd->no_sanitize_p 1500 if (!wtd->no_sanitize_p
1498 && sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT) 1501 && sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT)
1499 && TREE_CODE (TREE_TYPE (stmt)) == REFERENCE_TYPE) 1502 && TYPE_REF_P (TREE_TYPE (stmt)))
1500 ubsan_maybe_instrument_reference (stmt_p); 1503 ubsan_maybe_instrument_reference (stmt_p);
1501 break; 1504 break;
1502 1505
1503 case CALL_EXPR: 1506 case CALL_EXPR:
1504 if (!wtd->no_sanitize_p 1507 if (!wtd->no_sanitize_p
1506 | SANITIZE_ALIGNMENT | SANITIZE_VPTR))) 1509 | SANITIZE_ALIGNMENT | SANITIZE_VPTR)))
1507 { 1510 {
1508 tree fn = CALL_EXPR_FN (stmt); 1511 tree fn = CALL_EXPR_FN (stmt);
1509 if (fn != NULL_TREE 1512 if (fn != NULL_TREE
1510 && !error_operand_p (fn) 1513 && !error_operand_p (fn)
1511 && POINTER_TYPE_P (TREE_TYPE (fn)) 1514 && INDIRECT_TYPE_P (TREE_TYPE (fn))
1512 && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) == METHOD_TYPE) 1515 && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) == METHOD_TYPE)
1513 { 1516 {
1514 bool is_ctor 1517 bool is_ctor
1515 = TREE_CODE (fn) == ADDR_EXPR 1518 = TREE_CODE (fn) == ADDR_EXPR
1516 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL 1519 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
1518 if (sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT)) 1521 if (sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT))
1519 ubsan_maybe_instrument_member_call (stmt, is_ctor); 1522 ubsan_maybe_instrument_member_call (stmt, is_ctor);
1520 if (sanitize_flags_p (SANITIZE_VPTR) && !is_ctor) 1523 if (sanitize_flags_p (SANITIZE_VPTR) && !is_ctor)
1521 cp_ubsan_maybe_instrument_member_call (stmt); 1524 cp_ubsan_maybe_instrument_member_call (stmt);
1522 } 1525 }
1523 } 1526 else if (fn == NULL_TREE
1527 && CALL_EXPR_IFN (stmt) == IFN_UBSAN_NULL
1528 && TREE_CODE (CALL_EXPR_ARG (stmt, 0)) == INTEGER_CST
1529 && TYPE_REF_P (TREE_TYPE (CALL_EXPR_ARG (stmt, 0))))
1530 *walk_subtrees = 0;
1531 }
1532 /* Fall through. */
1533 case AGGR_INIT_EXPR:
1534 /* For calls to a multi-versioned function, overload resolution
1535 returns the function with the highest target priority, that is,
1536 the version that will checked for dispatching first. If this
1537 version is inlinable, a direct call to this version can be made
1538 otherwise the call should go through the dispatcher. */
1539 {
1540 tree fn = cp_get_callee_fndecl_nofold (stmt);
1541 if (fn && DECL_FUNCTION_VERSIONED (fn)
1542 && (current_function_decl == NULL
1543 || !targetm.target_option.can_inline_p (current_function_decl,
1544 fn)))
1545 if (tree dis = get_function_version_dispatcher (fn))
1546 {
1547 mark_versions_used (dis);
1548 dis = build_address (dis);
1549 if (TREE_CODE (stmt) == CALL_EXPR)
1550 CALL_EXPR_FN (stmt) = dis;
1551 else
1552 AGGR_INIT_EXPR_FN (stmt) = dis;
1553 }
1554 }
1555 break;
1556
1557 case TARGET_EXPR:
1558 if (TARGET_EXPR_INITIAL (stmt)
1559 && TREE_CODE (TARGET_EXPR_INITIAL (stmt)) == CONSTRUCTOR
1560 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (TARGET_EXPR_INITIAL (stmt)))
1561 TARGET_EXPR_NO_ELIDE (stmt) = 1;
1524 break; 1562 break;
1525 1563
1526 default: 1564 default:
1527 if (IS_TYPE_OR_DECL_P (stmt)) 1565 if (IS_TYPE_OR_DECL_P (stmt))
1528 *walk_subtrees = 0; 1566 *walk_subtrees = 0;
1554 cp_ubsan_instrument_member_accesses (t_p); 1592 cp_ubsan_instrument_member_accesses (t_p);
1555 } 1593 }
1556 1594
1557 /* If a function that should end with a return in non-void 1595 /* If a function that should end with a return in non-void
1558 function doesn't obviously end with return, add ubsan 1596 function doesn't obviously end with return, add ubsan
1559 instrumentation code to verify it at runtime. */ 1597 instrumentation code to verify it at runtime. If -fsanitize=return
1598 is not enabled, instrument __builtin_unreachable. */
1560 1599
1561 static void 1600 static void
1562 cp_ubsan_maybe_instrument_return (tree fndecl) 1601 cp_maybe_instrument_return (tree fndecl)
1563 { 1602 {
1564 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl))) 1603 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
1565 || DECL_CONSTRUCTOR_P (fndecl) 1604 || DECL_CONSTRUCTOR_P (fndecl)
1566 || DECL_DESTRUCTOR_P (fndecl) 1605 || DECL_DESTRUCTOR_P (fndecl)
1567 || !targetm.warn_func_return (fndecl)) 1606 || !targetm.warn_func_return (fndecl))
1568 return; 1607 return;
1569 1608
1609 if (!sanitize_flags_p (SANITIZE_RETURN, fndecl)
1610 /* Don't add __builtin_unreachable () if not optimizing, it will not
1611 improve any optimizations in that case, just break UB code.
1612 Don't add it if -fsanitize=unreachable -fno-sanitize=return either,
1613 UBSan covers this with ubsan_instrument_return above where sufficient
1614 information is provided, while the __builtin_unreachable () below
1615 if return sanitization is disabled will just result in hard to
1616 understand runtime error without location. */
1617 && (!optimize
1618 || sanitize_flags_p (SANITIZE_UNREACHABLE, fndecl)))
1619 return;
1620
1570 tree t = DECL_SAVED_TREE (fndecl); 1621 tree t = DECL_SAVED_TREE (fndecl);
1571 while (t) 1622 while (t)
1572 { 1623 {
1573 switch (TREE_CODE (t)) 1624 switch (TREE_CODE (t))
1574 { 1625 {
1575 case BIND_EXPR: 1626 case BIND_EXPR:
1576 t = BIND_EXPR_BODY (t); 1627 t = BIND_EXPR_BODY (t);
1577 continue; 1628 continue;
1578 case TRY_FINALLY_EXPR: 1629 case TRY_FINALLY_EXPR:
1630 case CLEANUP_POINT_EXPR:
1579 t = TREE_OPERAND (t, 0); 1631 t = TREE_OPERAND (t, 0);
1580 continue; 1632 continue;
1581 case STATEMENT_LIST: 1633 case STATEMENT_LIST:
1582 { 1634 {
1583 tree_stmt_iterator i = tsi_last (t); 1635 tree_stmt_iterator i = tsi_last (t);
1636 while (!tsi_end_p (i))
1637 {
1638 tree p = tsi_stmt (i);
1639 if (TREE_CODE (p) != DEBUG_BEGIN_STMT)
1640 break;
1641 tsi_prev (&i);
1642 }
1584 if (!tsi_end_p (i)) 1643 if (!tsi_end_p (i))
1585 { 1644 {
1586 t = tsi_stmt (i); 1645 t = tsi_stmt (i);
1587 continue; 1646 continue;
1588 } 1647 }
1598 if (t == NULL_TREE) 1657 if (t == NULL_TREE)
1599 return; 1658 return;
1600 tree *p = &DECL_SAVED_TREE (fndecl); 1659 tree *p = &DECL_SAVED_TREE (fndecl);
1601 if (TREE_CODE (*p) == BIND_EXPR) 1660 if (TREE_CODE (*p) == BIND_EXPR)
1602 p = &BIND_EXPR_BODY (*p); 1661 p = &BIND_EXPR_BODY (*p);
1603 t = ubsan_instrument_return (DECL_SOURCE_LOCATION (fndecl)); 1662
1663 location_t loc = DECL_SOURCE_LOCATION (fndecl);
1664 if (sanitize_flags_p (SANITIZE_RETURN, fndecl))
1665 t = ubsan_instrument_return (loc);
1666 else
1667 {
1668 tree fndecl = builtin_decl_explicit (BUILT_IN_UNREACHABLE);
1669 t = build_call_expr_loc (BUILTINS_LOCATION, fndecl, 0);
1670 }
1671
1604 append_to_statement_list (t, p); 1672 append_to_statement_list (t, p);
1605 } 1673 }
1606 1674
1607 void 1675 void
1608 cp_genericize (tree fndecl) 1676 cp_genericize (tree fndecl)
1662 save_bc_label[bc_break] = bc_label[bc_break]; 1730 save_bc_label[bc_break] = bc_label[bc_break];
1663 save_bc_label[bc_continue] = bc_label[bc_continue]; 1731 save_bc_label[bc_continue] = bc_label[bc_continue];
1664 bc_label[bc_break] = NULL_TREE; 1732 bc_label[bc_break] = NULL_TREE;
1665 bc_label[bc_continue] = NULL_TREE; 1733 bc_label[bc_continue] = NULL_TREE;
1666 1734
1667 /* Expand all the array notations here. */
1668 if (flag_cilkplus
1669 && contains_array_notation_expr (DECL_SAVED_TREE (fndecl)))
1670 DECL_SAVED_TREE (fndecl)
1671 = expand_array_notation_exprs (DECL_SAVED_TREE (fndecl));
1672
1673 /* We do want to see every occurrence of the parms, so we can't just use 1735 /* We do want to see every occurrence of the parms, so we can't just use
1674 walk_tree's hash functionality. */ 1736 walk_tree's hash functionality. */
1675 cp_genericize_tree (&DECL_SAVED_TREE (fndecl), true); 1737 cp_genericize_tree (&DECL_SAVED_TREE (fndecl), true);
1676 1738
1677 if (sanitize_flags_p (SANITIZE_RETURN) 1739 cp_maybe_instrument_return (fndecl);
1678 && current_function_decl != NULL_TREE)
1679 cp_ubsan_maybe_instrument_return (fndecl);
1680 1740
1681 /* Do everything else. */ 1741 /* Do everything else. */
1682 c_genericize (fndecl); 1742 c_genericize (fndecl);
1683 1743
1684 gcc_assert (bc_label[bc_break] == NULL); 1744 gcc_assert (bc_label[bc_break] == NULL);
1707 1767
1708 defparm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn))); 1768 defparm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
1709 if (arg2) 1769 if (arg2)
1710 defparm = TREE_CHAIN (defparm); 1770 defparm = TREE_CHAIN (defparm);
1711 1771
1772 bool is_method = TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE;
1712 if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE) 1773 if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
1713 { 1774 {
1714 tree inner_type = TREE_TYPE (arg1); 1775 tree inner_type = TREE_TYPE (arg1);
1715 tree start1, end1, p1; 1776 tree start1, end1, p1;
1716 tree start2 = NULL, p2 = NULL; 1777 tree start2 = NULL, p2 = NULL;
1755 argarray[i++] = p2; 1816 argarray[i++] = p2;
1756 /* Handle default arguments. */ 1817 /* Handle default arguments. */
1757 for (parm = defparm; parm && parm != void_list_node; 1818 for (parm = defparm; parm && parm != void_list_node;
1758 parm = TREE_CHAIN (parm), i++) 1819 parm = TREE_CHAIN (parm), i++)
1759 argarray[i] = convert_default_arg (TREE_VALUE (parm), 1820 argarray[i] = convert_default_arg (TREE_VALUE (parm),
1760 TREE_PURPOSE (parm), fn, i, 1821 TREE_PURPOSE (parm), fn,
1761 tf_warning_or_error); 1822 i - is_method, tf_warning_or_error);
1762 t = build_call_a (fn, i, argarray); 1823 t = build_call_a (fn, i, argarray);
1763 t = fold_convert (void_type_node, t); 1824 t = fold_convert (void_type_node, t);
1764 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t); 1825 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
1765 append_to_statement_list (t, &ret); 1826 append_to_statement_list (t, &ret);
1766 1827
1788 argarray[i++] = build_fold_addr_expr_loc (input_location, arg2); 1849 argarray[i++] = build_fold_addr_expr_loc (input_location, arg2);
1789 /* Handle default arguments. */ 1850 /* Handle default arguments. */
1790 for (parm = defparm; parm && parm != void_list_node; 1851 for (parm = defparm; parm && parm != void_list_node;
1791 parm = TREE_CHAIN (parm), i++) 1852 parm = TREE_CHAIN (parm), i++)
1792 argarray[i] = convert_default_arg (TREE_VALUE (parm), 1853 argarray[i] = convert_default_arg (TREE_VALUE (parm),
1793 TREE_PURPOSE (parm), 1854 TREE_PURPOSE (parm), fn,
1794 fn, i, tf_warning_or_error); 1855 i - is_method, tf_warning_or_error);
1795 t = build_call_a (fn, i, argarray); 1856 t = build_call_a (fn, i, argarray);
1796 t = fold_convert (void_type_node, t); 1857 t = fold_convert (void_type_node, t);
1797 return fold_build_cleanup_point_expr (TREE_TYPE (t), t); 1858 return fold_build_cleanup_point_expr (TREE_TYPE (t), t);
1798 } 1859 }
1799 } 1860 }
1863 than the DECL itself. */ 1924 than the DECL itself. */
1864 1925
1865 bool 1926 bool
1866 cxx_omp_privatize_by_reference (const_tree decl) 1927 cxx_omp_privatize_by_reference (const_tree decl)
1867 { 1928 {
1868 return (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE 1929 return (TYPE_REF_P (TREE_TYPE (decl))
1869 || is_invisiref_parm (decl)); 1930 || is_invisiref_parm (decl));
1870 } 1931 }
1871 1932
1872 /* Return true if DECL is const qualified var having no mutable member. */ 1933 /* Return true if DECL is const qualified var having no mutable member. */
1873 bool 1934 bool
1874 cxx_omp_const_qual_no_mutable (tree decl) 1935 cxx_omp_const_qual_no_mutable (tree decl)
1875 { 1936 {
1876 tree type = TREE_TYPE (decl); 1937 tree type = TREE_TYPE (decl);
1877 if (TREE_CODE (type) == REFERENCE_TYPE) 1938 if (TYPE_REF_P (type))
1878 { 1939 {
1879 if (!is_invisiref_parm (decl)) 1940 if (!is_invisiref_parm (decl))
1880 return false; 1941 return false;
1881 type = TREE_TYPE (type); 1942 type = TREE_TYPE (type);
1882 1943
1913 } 1974 }
1914 1975
1915 /* True if OpenMP sharing attribute of DECL is predetermined. */ 1976 /* True if OpenMP sharing attribute of DECL is predetermined. */
1916 1977
1917 enum omp_clause_default_kind 1978 enum omp_clause_default_kind
1918 cxx_omp_predetermined_sharing (tree decl) 1979 cxx_omp_predetermined_sharing_1 (tree decl)
1919 { 1980 {
1920 /* Static data members are predetermined shared. */ 1981 /* Static data members are predetermined shared. */
1921 if (TREE_STATIC (decl)) 1982 if (TREE_STATIC (decl))
1922 { 1983 {
1923 tree ctx = CP_DECL_CONTEXT (decl); 1984 tree ctx = CP_DECL_CONTEXT (decl);
1926 } 1987 }
1927 1988
1928 /* Const qualified vars having no mutable member are predetermined 1989 /* Const qualified vars having no mutable member are predetermined
1929 shared. */ 1990 shared. */
1930 if (cxx_omp_const_qual_no_mutable (decl)) 1991 if (cxx_omp_const_qual_no_mutable (decl))
1992 return OMP_CLAUSE_DEFAULT_SHARED;
1993
1994 return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
1995 }
1996
1997 /* Likewise, but also include the artificial vars. We don't want to
1998 disallow the artificial vars being mentioned in explicit clauses,
1999 as we use artificial vars e.g. for loop constructs with random
2000 access iterators other than pointers, but during gimplification
2001 we want to treat them as predetermined. */
2002
2003 enum omp_clause_default_kind
2004 cxx_omp_predetermined_sharing (tree decl)
2005 {
2006 enum omp_clause_default_kind ret = cxx_omp_predetermined_sharing_1 (decl);
2007 if (ret != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
2008 return ret;
2009
2010 /* Predetermine artificial variables holding integral values, those
2011 are usually result of gimplify_one_sizepos or SAVE_EXPR
2012 gimplification. */
2013 if (VAR_P (decl)
2014 && DECL_ARTIFICIAL (decl)
2015 && INTEGRAL_TYPE_P (TREE_TYPE (decl))
2016 && !(DECL_LANG_SPECIFIC (decl)
2017 && DECL_OMP_PRIVATIZED_MEMBER (decl)))
1931 return OMP_CLAUSE_DEFAULT_SHARED; 2018 return OMP_CLAUSE_DEFAULT_SHARED;
1932 2019
1933 return OMP_CLAUSE_DEFAULT_UNSPECIFIED; 2020 return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
1934 } 2021 }
1935 2022
1947 decl = OMP_CLAUSE_DECL (c); 2034 decl = OMP_CLAUSE_DECL (c);
1948 decl = require_complete_type (decl); 2035 decl = require_complete_type (decl);
1949 inner_type = TREE_TYPE (decl); 2036 inner_type = TREE_TYPE (decl);
1950 if (decl == error_mark_node) 2037 if (decl == error_mark_node)
1951 make_shared = true; 2038 make_shared = true;
1952 else if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE) 2039 else if (TYPE_REF_P (TREE_TYPE (decl)))
1953 inner_type = TREE_TYPE (inner_type); 2040 inner_type = TREE_TYPE (inner_type);
1954 2041
1955 /* We're interested in the base element, not arrays. */ 2042 /* We're interested in the base element, not arrays. */
1956 while (TREE_CODE (inner_type) == ARRAY_TYPE) 2043 while (TREE_CODE (inner_type) == ARRAY_TYPE)
1957 inner_type = TREE_TYPE (inner_type); 2044 inner_type = TREE_TYPE (inner_type);
1995 { 2082 {
1996 while (true) 2083 while (true)
1997 { 2084 {
1998 x = cp_fold (x); 2085 x = cp_fold (x);
1999 if (rval && DECL_P (x) 2086 if (rval && DECL_P (x)
2000 && TREE_CODE (TREE_TYPE (x)) != REFERENCE_TYPE) 2087 && !TYPE_REF_P (TREE_TYPE (x)))
2001 { 2088 {
2002 tree v = decl_constant_value (x); 2089 tree v = decl_constant_value (x);
2003 if (v != x && v != error_mark_node) 2090 if (v != x && v != error_mark_node)
2004 { 2091 {
2005 x = v; 2092 x = v;
2027 if (processing_template_decl) 2114 if (processing_template_decl)
2028 return x; 2115 return x;
2029 /* FIXME cp_fold ought to be a superset of maybe_constant_value so we don't 2116 /* FIXME cp_fold ought to be a superset of maybe_constant_value so we don't
2030 have to call both. */ 2117 have to call both. */
2031 if (cxx_dialect >= cxx11) 2118 if (cxx_dialect >= cxx11)
2032 x = maybe_constant_value (x); 2119 {
2120 x = maybe_constant_value (x);
2121 /* Sometimes we are given a CONSTRUCTOR but the call above wraps it into
2122 a TARGET_EXPR; undo that here. */
2123 if (TREE_CODE (x) == TARGET_EXPR)
2124 x = TARGET_EXPR_INITIAL (x);
2125 else if (TREE_CODE (x) == VIEW_CONVERT_EXPR
2126 && TREE_CODE (TREE_OPERAND (x, 0)) == CONSTRUCTOR
2127 && TREE_TYPE (TREE_OPERAND (x, 0)) == TREE_TYPE (x))
2128 x = TREE_OPERAND (x, 0);
2129 }
2033 return cp_fold_rvalue (x); 2130 return cp_fold_rvalue (x);
2034 } 2131 }
2035 2132
2036 /* c-common interface to cp_fold. If IN_INIT, this is in a static initializer 2133 /* c-common interface to cp_fold. If IN_INIT, this is in a static initializer
2037 and certain changes are made to the folding done. Or should be (FIXME). We 2134 and certain changes are made to the folding done. Or should be (FIXME). We
2038 never touch maybe_const, as it is only used for the C front-end 2135 never touch maybe_const, as it is only used for the C front-end
2039 C_MAYBE_CONST_EXPR. */ 2136 C_MAYBE_CONST_EXPR. */
2040 2137
2041 tree 2138 tree
2042 c_fully_fold (tree x, bool /*in_init*/, bool */*maybe_const*/) 2139 c_fully_fold (tree x, bool /*in_init*/, bool */*maybe_const*/, bool lval)
2043 { 2140 {
2044 /* c_fully_fold is only used on rvalues, and we need to fold CONST_DECL to 2141 return cp_fold_maybe_rvalue (x, !lval);
2045 INTEGER_CST. */
2046 return cp_fold_rvalue (x);
2047 } 2142 }
2048 2143
2049 static GTY((deletable)) hash_map<tree, tree> *fold_cache; 2144 static GTY((deletable)) hash_map<tree, tree> *fold_cache;
2050 2145
2051 /* Dispose of the whole FOLD_CACHE. */ 2146 /* Dispose of the whole FOLD_CACHE. */
2057 fold_cache->empty (); 2152 fold_cache->empty ();
2058 } 2153 }
2059 2154
2060 /* This function tries to fold an expression X. 2155 /* This function tries to fold an expression X.
2061 To avoid combinatorial explosion, folding results are kept in fold_cache. 2156 To avoid combinatorial explosion, folding results are kept in fold_cache.
2062 If we are processing a template or X is invalid, we don't fold at all. 2157 If X is invalid, we don't fold at all.
2063 For performance reasons we don't cache expressions representing a 2158 For performance reasons we don't cache expressions representing a
2064 declaration or constant. 2159 declaration or constant.
2065 Function returns X or its folded variant. */ 2160 Function returns X or its folded variant. */
2066 2161
2067 static tree 2162 static tree
2074 bool rval_ops = true; 2169 bool rval_ops = true;
2075 2170
2076 if (!x || x == error_mark_node) 2171 if (!x || x == error_mark_node)
2077 return x; 2172 return x;
2078 2173
2079 if (processing_template_decl 2174 if (EXPR_P (x) && (!TREE_TYPE (x) || TREE_TYPE (x) == error_mark_node))
2080 || (EXPR_P (x) && (!TREE_TYPE (x) || TREE_TYPE (x) == error_mark_node)))
2081 return x; 2175 return x;
2082 2176
2083 /* Don't bother to cache DECLs or constants. */ 2177 /* Don't bother to cache DECLs or constants. */
2084 if (DECL_P (x) || CONSTANT_CLASS_P (x)) 2178 if (DECL_P (x) || CONSTANT_CLASS_P (x))
2085 return x; 2179 return x;
2111 case CONVERT_EXPR: 2205 case CONVERT_EXPR:
2112 case NOP_EXPR: 2206 case NOP_EXPR:
2113 case NON_LVALUE_EXPR: 2207 case NON_LVALUE_EXPR:
2114 2208
2115 if (VOID_TYPE_P (TREE_TYPE (x))) 2209 if (VOID_TYPE_P (TREE_TYPE (x)))
2116 return x; 2210 {
2211 /* This is just to make sure we don't end up with casts to
2212 void from error_mark_node. If we just return x, then
2213 cp_fold_r might fold the operand into error_mark_node and
2214 leave the conversion in the IR. STRIP_USELESS_TYPE_CONVERSION
2215 during gimplification doesn't like such casts.
2216 Don't create a new tree if op0 != TREE_OPERAND (x, 0), the
2217 folding of the operand should be in the caches and if in cp_fold_r
2218 it will modify it in place. */
2219 op0 = cp_fold (TREE_OPERAND (x, 0));
2220 if (op0 == error_mark_node)
2221 x = error_mark_node;
2222 break;
2223 }
2117 2224
2118 loc = EXPR_LOCATION (x); 2225 loc = EXPR_LOCATION (x);
2119 op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops); 2226 op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops);
2120 2227
2121 if (code == CONVERT_EXPR 2228 if (code == CONVERT_EXPR
2151 return cp_fold (p); 2258 return cp_fold (p);
2152 } 2259 }
2153 goto unary; 2260 goto unary;
2154 2261
2155 case ADDR_EXPR: 2262 case ADDR_EXPR:
2263 loc = EXPR_LOCATION (x);
2264 op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), false);
2265
2266 /* Cope with user tricks that amount to offsetof. */
2267 if (op0 != error_mark_node
2268 && TREE_CODE (TREE_TYPE (op0)) != FUNCTION_TYPE
2269 && TREE_CODE (TREE_TYPE (op0)) != METHOD_TYPE)
2270 {
2271 tree val = get_base_address (op0);
2272 if (val
2273 && INDIRECT_REF_P (val)
2274 && COMPLETE_TYPE_P (TREE_TYPE (val))
2275 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
2276 {
2277 val = TREE_OPERAND (val, 0);
2278 STRIP_NOPS (val);
2279 if (TREE_CODE (val) == INTEGER_CST)
2280 return fold_offsetof (op0, TREE_TYPE (x));
2281 }
2282 }
2283 goto finish_unary;
2284
2156 case REALPART_EXPR: 2285 case REALPART_EXPR:
2157 case IMAGPART_EXPR: 2286 case IMAGPART_EXPR:
2158 rval_ops = false; 2287 rval_ops = false;
2159 /* FALLTHRU */ 2288 /* FALLTHRU */
2160 case CONJ_EXPR: 2289 case CONJ_EXPR:
2161 case FIX_TRUNC_EXPR: 2290 case FIX_TRUNC_EXPR:
2162 case FLOAT_EXPR: 2291 case FLOAT_EXPR:
2163 case NEGATE_EXPR: 2292 case NEGATE_EXPR:
2164 case ABS_EXPR: 2293 case ABS_EXPR:
2294 case ABSU_EXPR:
2165 case BIT_NOT_EXPR: 2295 case BIT_NOT_EXPR:
2166 case TRUTH_NOT_EXPR: 2296 case TRUTH_NOT_EXPR:
2167 case FIXED_CONVERT_EXPR: 2297 case FIXED_CONVERT_EXPR:
2168 unary: 2298 unary:
2169 2299
2170 loc = EXPR_LOCATION (x); 2300 loc = EXPR_LOCATION (x);
2171 op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops); 2301 op0 = cp_fold_maybe_rvalue (TREE_OPERAND (x, 0), rval_ops);
2172 2302
2303 finish_unary:
2173 if (op0 != TREE_OPERAND (x, 0)) 2304 if (op0 != TREE_OPERAND (x, 0))
2174 { 2305 {
2175 if (op0 == error_mark_node) 2306 if (op0 == error_mark_node)
2176 x = error_mark_node; 2307 x = error_mark_node;
2177 else 2308 else
2210 case MODIFY_EXPR: 2341 case MODIFY_EXPR:
2211 rval_ops = false; 2342 rval_ops = false;
2212 /* FALLTHRU */ 2343 /* FALLTHRU */
2213 case POINTER_PLUS_EXPR: 2344 case POINTER_PLUS_EXPR:
2214 case PLUS_EXPR: 2345 case PLUS_EXPR:
2346 case POINTER_DIFF_EXPR:
2215 case MINUS_EXPR: 2347 case MINUS_EXPR:
2216 case MULT_EXPR: 2348 case MULT_EXPR:
2217 case TRUNC_DIV_EXPR: 2349 case TRUNC_DIV_EXPR:
2218 case CEIL_DIV_EXPR: 2350 case CEIL_DIV_EXPR:
2219 case FLOOR_DIV_EXPR: 2351 case FLOOR_DIV_EXPR:
2258 x = fold_build2_loc (loc, code, TREE_TYPE (x), op0, op1); 2390 x = fold_build2_loc (loc, code, TREE_TYPE (x), op0, op1);
2259 } 2391 }
2260 else 2392 else
2261 x = fold (x); 2393 x = fold (x);
2262 2394
2263 if (TREE_NO_WARNING (org_x) 2395 /* This is only needed for -Wnonnull-compare and only if
2264 && warn_nonnull_compare 2396 TREE_NO_WARNING (org_x), but to avoid that option affecting code
2265 && COMPARISON_CLASS_P (org_x)) 2397 generation, we do it always. */
2398 if (COMPARISON_CLASS_P (org_x))
2266 { 2399 {
2267 if (x == error_mark_node || TREE_CODE (x) == INTEGER_CST) 2400 if (x == error_mark_node || TREE_CODE (x) == INTEGER_CST)
2268 ; 2401 ;
2269 else if (COMPARISON_CLASS_P (x)) 2402 else if (COMPARISON_CLASS_P (x))
2270 TREE_NO_WARNING (x) = 1; 2403 {
2404 if (TREE_NO_WARNING (org_x) && warn_nonnull_compare)
2405 TREE_NO_WARNING (x) = 1;
2406 }
2271 /* Otherwise give up on optimizing these, let GIMPLE folders 2407 /* Otherwise give up on optimizing these, let GIMPLE folders
2272 optimize those later on. */ 2408 optimize those later on. */
2273 else if (op0 != TREE_OPERAND (org_x, 0) 2409 else if (op0 != TREE_OPERAND (org_x, 0)
2274 || op1 != TREE_OPERAND (org_x, 1)) 2410 || op1 != TREE_OPERAND (org_x, 1))
2275 { 2411 {
2276 x = build2_loc (loc, code, TREE_TYPE (org_x), op0, op1); 2412 x = build2_loc (loc, code, TREE_TYPE (org_x), op0, op1);
2277 TREE_NO_WARNING (x) = 1; 2413 if (TREE_NO_WARNING (org_x) && warn_nonnull_compare)
2414 TREE_NO_WARNING (x) = 1;
2278 } 2415 }
2279 else 2416 else
2280 x = org_x; 2417 x = org_x;
2281 } 2418 }
2282 break; 2419 break;
2283 2420
2284 case VEC_COND_EXPR: 2421 case VEC_COND_EXPR:
2285 case COND_EXPR: 2422 case COND_EXPR:
2286
2287 /* Don't bother folding a void condition, since it can't produce a
2288 constant value. Also, some statement-level uses of COND_EXPR leave
2289 one of the branches NULL, so folding would crash. */
2290 if (VOID_TYPE_P (TREE_TYPE (x)))
2291 return x;
2292
2293 loc = EXPR_LOCATION (x); 2423 loc = EXPR_LOCATION (x);
2294 op0 = cp_fold_rvalue (TREE_OPERAND (x, 0)); 2424 op0 = cp_fold_rvalue (TREE_OPERAND (x, 0));
2295 op1 = cp_fold (TREE_OPERAND (x, 1)); 2425 op1 = cp_fold (TREE_OPERAND (x, 1));
2296 op2 = cp_fold (TREE_OPERAND (x, 2)); 2426 op2 = cp_fold (TREE_OPERAND (x, 2));
2297 2427
2300 warning_sentinel s (warn_int_in_bool_context); 2430 warning_sentinel s (warn_int_in_bool_context);
2301 if (!VOID_TYPE_P (TREE_TYPE (op1))) 2431 if (!VOID_TYPE_P (TREE_TYPE (op1)))
2302 op1 = cp_truthvalue_conversion (op1); 2432 op1 = cp_truthvalue_conversion (op1);
2303 if (!VOID_TYPE_P (TREE_TYPE (op2))) 2433 if (!VOID_TYPE_P (TREE_TYPE (op2)))
2304 op2 = cp_truthvalue_conversion (op2); 2434 op2 = cp_truthvalue_conversion (op2);
2435 }
2436 else if (VOID_TYPE_P (TREE_TYPE (x)))
2437 {
2438 if (TREE_CODE (op0) == INTEGER_CST)
2439 {
2440 /* If the condition is constant, fold can fold away
2441 the COND_EXPR. If some statement-level uses of COND_EXPR
2442 have one of the branches NULL, avoid folding crash. */
2443 if (!op1)
2444 op1 = build_empty_stmt (loc);
2445 if (!op2)
2446 op2 = build_empty_stmt (loc);
2447 }
2448 else
2449 {
2450 /* Otherwise, don't bother folding a void condition, since
2451 it can't produce a constant value. */
2452 if (op0 != TREE_OPERAND (x, 0)
2453 || op1 != TREE_OPERAND (x, 1)
2454 || op2 != TREE_OPERAND (x, 2))
2455 x = build3_loc (loc, code, TREE_TYPE (x), op0, op1, op2);
2456 break;
2457 }
2305 } 2458 }
2306 2459
2307 if (op0 != TREE_OPERAND (x, 0) 2460 if (op0 != TREE_OPERAND (x, 0)
2308 || op1 != TREE_OPERAND (x, 1) 2461 || op1 != TREE_OPERAND (x, 1)
2309 || op2 != TREE_OPERAND (x, 2)) 2462 || op2 != TREE_OPERAND (x, 2))
2319 x = fold (x); 2472 x = fold (x);
2320 2473
2321 /* A COND_EXPR might have incompatible types in branches if one or both 2474 /* A COND_EXPR might have incompatible types in branches if one or both
2322 arms are bitfields. If folding exposed such a branch, fix it up. */ 2475 arms are bitfields. If folding exposed such a branch, fix it up. */
2323 if (TREE_CODE (x) != code 2476 if (TREE_CODE (x) != code
2477 && x != error_mark_node
2324 && !useless_type_conversion_p (TREE_TYPE (org_x), TREE_TYPE (x))) 2478 && !useless_type_conversion_p (TREE_TYPE (org_x), TREE_TYPE (x)))
2325 x = fold_convert (TREE_TYPE (org_x), x); 2479 x = fold_convert (TREE_TYPE (org_x), x);
2326 2480
2327 break; 2481 break;
2328 2482
2332 tree callee = get_callee_fndecl (x); 2486 tree callee = get_callee_fndecl (x);
2333 2487
2334 /* Some built-in function calls will be evaluated at compile-time in 2488 /* Some built-in function calls will be evaluated at compile-time in
2335 fold (). Set optimize to 1 when folding __builtin_constant_p inside 2489 fold (). Set optimize to 1 when folding __builtin_constant_p inside
2336 a constexpr function so that fold_builtin_1 doesn't fold it to 0. */ 2490 a constexpr function so that fold_builtin_1 doesn't fold it to 0. */
2337 if (callee && DECL_BUILT_IN (callee) && !optimize 2491 if (callee && fndecl_built_in_p (callee) && !optimize
2338 && DECL_IS_BUILTIN_CONSTANT_P (callee) 2492 && DECL_IS_BUILTIN_CONSTANT_P (callee)
2339 && current_function_decl 2493 && current_function_decl
2340 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) 2494 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2341 nw = 1; 2495 nw = 1;
2496
2497 /* Defer folding __builtin_is_constant_evaluated. */
2498 if (callee
2499 && fndecl_built_in_p (callee, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
2500 BUILT_IN_FRONTEND))
2501 break;
2342 2502
2343 x = copy_node (x); 2503 x = copy_node (x);
2344 2504
2345 m = call_expr_nargs (x); 2505 m = call_expr_nargs (x);
2346 for (i = 0; i < m; i++) 2506 for (i = 0; i < m; i++)
2421 nelts = elts->copy (); 2581 nelts = elts->copy ();
2422 (*nelts)[i].value = op; 2582 (*nelts)[i].value = op;
2423 } 2583 }
2424 } 2584 }
2425 if (nelts) 2585 if (nelts)
2426 x = build_constructor (TREE_TYPE (x), nelts); 2586 {
2587 x = build_constructor (TREE_TYPE (x), nelts);
2588 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (x)
2589 = CONSTRUCTOR_PLACEHOLDER_BOUNDARY (org_x);
2590 }
2591 if (VECTOR_TYPE_P (TREE_TYPE (x)))
2592 x = fold (x);
2427 break; 2593 break;
2428 } 2594 }
2429 case TREE_VEC: 2595 case TREE_VEC:
2430 { 2596 {
2431 bool changed = false; 2597 bool changed = false;