aboutsummaryrefslogtreecommitdiff
path: root/gcc/gimple-harden-control-flow.cc
blob: 70383e6e26582ad84d6e6b6aa98bbc9dfac3753f (plain)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
/* Control flow redundancy hardening.
   Copyright (C) 2022-2024 Free Software Foundation, Inc.
   Contributed by Alexandre Oliva <oliva@adacore.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"
#define INCLUDE_ALGORITHM /* find */
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "memmodel.h"
#include "tm_p.h"
#include "tree.h"
#include "fold-const.h"
#include "gimple.h"
#include "gimplify.h"
#include "tree-pass.h"
#include "ssa.h"
#include "gimple-iterator.h"
#include "gimple-pretty-print.h"
#include "tree-cfg.h"
#include "tree-cfgcleanup.h"
#include "tree-eh.h"
#include "except.h"
#include "sbitmap.h"
#include "basic-block.h"
#include "cfghooks.h"
#include "cfgloop.h"
#include "cgraph.h"
#include "alias.h"
#include "varasm.h"
#include "output.h"
#include "langhooks.h"
#include "diagnostic.h"
#include "intl.h"

namespace {

/* This pass introduces verification, at function exits, that booleans
   set in each basic block during function execution reflect the
   control flow graph: for each visited block, check that at least one
   predecessor and at least one successor were also visited.  This
   sort of hardening may detect various kinds of attacks.  */

/* Define a pass to harden code through control flow redundancy.  */

const pass_data pass_data_harden_control_flow_redundancy = {
  GIMPLE_PASS,
  "hardcfr",
  OPTGROUP_NONE,
  TV_NONE,
  PROP_cfg | PROP_ssa, // properties_required
  0,	    // properties_provided
  0,	    // properties_destroyed
  TODO_cleanup_cfg, // properties_start
  0,        // properties_finish
};

class pass_harden_control_flow_redundancy : public gimple_opt_pass
{
public:
  pass_harden_control_flow_redundancy (gcc::context *ctxt)
    : gimple_opt_pass (pass_data_harden_control_flow_redundancy, ctxt)
  {}
  opt_pass *clone () { return new pass_harden_control_flow_redundancy (m_ctxt); }
  virtual bool gate (function *fun) {
    /* Return quickly if the pass is disabled, without checking any of
       the conditions that might give rise to warnings that would only
       be appropriate if hardening was requested.  */
    if (!flag_harden_control_flow_redundancy)
      return false;

    /* Functions that return more than once, like setjmp and vfork
       (that also gets this flag set), will start recording a path
       after the first return, and then may take another path when
       they return again.  The unterminated path may then be flagged
       as an error.  ??? We could save the visited array before the
       call and restore it if it returns again.  */
    if (fun->calls_setjmp)
      {
	warning_at (DECL_SOURCE_LOCATION (fun->decl), 0,
		    "%qD calls %<setjmp%> or similar,"
		    " %<-fharden-control-flow-redundancy%> is not supported",
		    fun->decl);
	return false;
      }

    /* Some targets bypass the abnormal dispatcher block in nonlocal
       gotos, and then we'd miss its visited bit.  It might be doable
       to make it work uniformly, but this feature is not used often
       enough to make it worthwhile.  */
    if (fun->has_nonlocal_label)
      {
	warning_at (DECL_SOURCE_LOCATION (fun->decl), 0,
		    "%qD receives nonlocal gotos,"
		    " %<-fharden-control-flow-redundancy%> is not supported",
		    fun->decl);
	return false;
      }

    if (fun->cfg && param_hardcfr_max_blocks > 0
	&& (n_basic_blocks_for_fn (fun) - NUM_FIXED_BLOCKS
	    > param_hardcfr_max_blocks))
      {
	warning_at (DECL_SOURCE_LOCATION (fun->decl), 0,
		    "%qD has more than %u blocks, the requested"
		    " maximum for %<-fharden-control-flow-redundancy%>",
		    fun->decl, param_hardcfr_max_blocks);
	return false;
      }

    return true;
  }
  virtual unsigned int execute (function *);
};

}

/* Return TRUE iff CFR checks should be inserted before returning
   calls.  */

static bool
check_returning_calls_p ()
{
  return
    flag_harden_control_flow_redundancy_check_returning_calls > 0
    || (flag_harden_control_flow_redundancy_check_returning_calls < 0
	/* Gates pass_tail_calls.  */
	&& flag_optimize_sibling_calls
	/* Gates pass_all_optimizations.  */
	&& optimize >= 1 && !optimize_debug);
}

/* Scan BB from the end, updating *RETPTR if given as return stmts and
   copies are found.  Return a call or a stmt that cannot appear after
   a tail call, or NULL if the top of the block is reached without
   finding any.  */

static gimple *
hardcfr_scan_block (basic_block bb, tree **retptr)
{
  gimple_stmt_iterator gsi;
  for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
    {
      gimple *stmt = gsi_stmt (gsi);

      /* Ignore labels, returns, nops, clobbers and debug stmts.  */
      if (gimple_code (stmt) == GIMPLE_LABEL
	  || gimple_code (stmt) == GIMPLE_NOP
	  || gimple_code (stmt) == GIMPLE_PREDICT
	  || gimple_clobber_p (stmt)
	  || is_gimple_debug (stmt))
	continue;

      if (gimple_code (stmt) == GIMPLE_RETURN)
	{
	  greturn *gret = as_a <greturn *> (stmt);
	  if (retptr)
	    {
	      gcc_checking_assert (!*retptr);
	      *retptr = gimple_return_retval_ptr (gret);
	    }
	  continue;
	}

      /* Check for a call.  */
      if (is_gimple_call (stmt))
	return stmt;

      /* Allow simple copies to the return value, updating the return
	 value to be found in earlier assignments.  */
      if (retptr && *retptr && gimple_assign_single_p (stmt)
	  && **retptr == gimple_assign_lhs (stmt))
	{
	  *retptr = gimple_assign_rhs1_ptr (stmt);
	  continue;
	}

      return stmt;
    }

  /* Any other kind of stmt will prevent a tail call.  */
  return NULL;
}

/* Return TRUE iff CALL is to be preceded by a CFR checkpoint, i.e.,
   if it's a returning call (one whose result is ultimately returned
   without intervening non-copy statements) and we're checking
   returning calls, a __builtin_return call (noreturn with a path to
   the exit block), a must-tail call, or a tail call.  */

static bool
returning_call_p (gcall *call)
{
  if (!(gimple_call_noreturn_p (call)
	|| gimple_call_must_tail_p (call)
	|| gimple_call_tail_p (call)
	|| check_returning_calls_p ()))
    return false;

  /* Quickly check that there's a path to exit compatible with a
     returning call.  Detect infinite loops by limiting the path
     length to the basic block count, and by looking for duplicate
     blocks before allocating more memory for the path, for amortized
     O(n).  */
  auto_vec<basic_block, 10> path;
  for (basic_block bb = gimple_bb (call);
       bb != EXIT_BLOCK_PTR_FOR_FN (cfun);
       bb = single_succ (bb))
    if (!single_succ_p (bb)
	|| (single_succ_edge (bb)->flags & EDGE_EH) != 0
	|| n_basic_blocks_for_fn (cfun) - path.length () <= NUM_FIXED_BLOCKS
	|| (path.length () == path.allocated ()
	    && std::find (path.begin (), path.end (), bb) != path.end ()))
      return false;
    else
      path.safe_push (bb);

  /* Check the stmts in the blocks and trace the return value.  */
  tree *retptr = NULL;
  for (;;)
    {
      gcc_checking_assert (!path.is_empty ());
      basic_block bb = path.pop ();
      gimple *stop = hardcfr_scan_block (bb, &retptr);
      if (stop)
	{
	  if (stop != call)
	    return false;
	  gcc_checking_assert (path.is_empty ());
	  break;
	}

      gphi *retphi = NULL;
      if (retptr && *retptr && TREE_CODE (*retptr) == SSA_NAME
	  && !SSA_NAME_IS_DEFAULT_DEF (*retptr)
	  && SSA_NAME_DEF_STMT (*retptr)
	  && is_a <gphi *> (SSA_NAME_DEF_STMT (*retptr))
	  && gimple_bb (SSA_NAME_DEF_STMT (*retptr)) == bb)
	{
	  retphi = as_a <gphi *> (SSA_NAME_DEF_STMT (*retptr));
	  gcc_checking_assert (gimple_phi_result (retphi) == *retptr);
	}
      else
	continue;

      gcc_checking_assert (!path.is_empty ());
      edge e = single_succ_edge (path.last ());
      int i = EDGE_COUNT (bb->preds);
      while (i--)
	if (EDGE_PRED (bb, i) == e)
	  break;
      gcc_checking_assert (i >= 0);
      retptr = gimple_phi_arg_def_ptr (retphi, i);
    }

  return (gimple_call_noreturn_p (call)
	  || gimple_call_must_tail_p (call)
	  || gimple_call_tail_p (call)
	  || (gimple_call_lhs (call) == (retptr ? *retptr : NULL)
	      && check_returning_calls_p ()));
}

typedef auto_vec<edge, 10> chk_edges_t;

/* Declare for mutual recursion.  */
static bool hardcfr_sibcall_search_preds (basic_block bb,
					  chk_edges_t &chk_edges,
					  int &count_chkcall,
					  auto_sbitmap &chkcall_blocks,
					  int &count_postchk,
					  auto_sbitmap &postchk_blocks,
					  tree *retptr);

/* Search backwards from the end of BB for a mandatory or potential
   sibcall.  Schedule the block to be handled sort-of like noreturn if
   so.  Recurse to preds, with updated RETPTR, if the block only
   contains stmts that may follow such a call, scheduling checking at
   edges and marking blocks as post-check as needed.  Return true iff,
   at the end of the block, a check will have already been
   performed.  */

static bool
hardcfr_sibcall_search_block (basic_block bb,
			      chk_edges_t &chk_edges,
			      int &count_chkcall,
			      auto_sbitmap &chkcall_blocks,
			      int &count_postchk,
			      auto_sbitmap &postchk_blocks,
			      tree *retptr)
{
  /* Conditionals and internal exceptions rule out tail calls.  */
  if (!single_succ_p (bb)
      || (single_succ_edge (bb)->flags & EDGE_EH) != 0)
    return false;

  gimple *stmt = hardcfr_scan_block (bb, &retptr);
  if (!stmt)
    return hardcfr_sibcall_search_preds (bb, chk_edges,
					 count_chkcall, chkcall_blocks,
					 count_postchk, postchk_blocks,
					 retptr);

  if (!is_a <gcall *> (stmt))
    return false;

  /* Avoid disrupting mandatory or early-marked tail calls,
     inserting the check before them.  This works for
     must-tail calls, but tail calling as an optimization is
     detected too late for us.

     Also check for noreturn calls here.  Noreturn calls won't
     normally have edges to exit, so they won't be found here,
     but __builtin_return does, and we must check before
     it, so handle it like a tail call.  */
  gcall *call = as_a <gcall *> (stmt);
  if (!(gimple_call_noreturn_p (call)
	|| gimple_call_must_tail_p (call)
	|| gimple_call_tail_p (call)
	|| (gimple_call_lhs (call) == (retptr ? *retptr : NULL)
	    && check_returning_calls_p ())))
    return false;

  gcc_checking_assert (returning_call_p (call));

  /* We found a call that is to be preceded by checking.  */
  if (bitmap_set_bit (chkcall_blocks, bb->index))
    ++count_chkcall;
  else
    gcc_unreachable ();
  return true;
}


/* Search preds of BB for a mandatory or potential sibcall or
   returning call, and arrange for the blocks containing them to have
   a check inserted before the call, like noreturn calls.  If any
   preds are found to perform checking, schedule checks at the edges
   of those that don't, and mark BB as postcheck..  */

static bool
hardcfr_sibcall_search_preds (basic_block bb,
			      chk_edges_t &chk_edges,
			      int &count_chkcall,
			      auto_sbitmap &chkcall_blocks,
			      int &count_postchk,
			      auto_sbitmap &postchk_blocks,
			      tree *retptr)
{
  /* For the exit block, we wish to force a check at every
     predecessor, so pretend we've already found a pred that had
     checking, so that we schedule checking at every one of its pred
     edges.  */
  bool first = bb->index >= NUM_FIXED_BLOCKS;
  bool postchecked = true;

  gphi *retphi = NULL;
  if (retptr && *retptr && TREE_CODE (*retptr) == SSA_NAME
      && !SSA_NAME_IS_DEFAULT_DEF (*retptr)
      && SSA_NAME_DEF_STMT (*retptr)
      && is_a <gphi *> (SSA_NAME_DEF_STMT (*retptr))
      && gimple_bb (SSA_NAME_DEF_STMT (*retptr)) == bb)
    {
      retphi = as_a <gphi *> (SSA_NAME_DEF_STMT (*retptr));
      gcc_checking_assert (gimple_phi_result (retphi) == *retptr);
    }

  for (int i = EDGE_COUNT (bb->preds); i--; first = false)
    {
      edge e = EDGE_PRED (bb, i);

      bool checked
	= hardcfr_sibcall_search_block (e->src, chk_edges,
					count_chkcall, chkcall_blocks,
					count_postchk, postchk_blocks,
					!retphi ? retptr
					: gimple_phi_arg_def_ptr (retphi, i));

      if (first)
	{
	  postchecked = checked;
	  continue;
	}

      /* When we first find a checked block, force a check at every
	 other incoming edge we've already visited, and those we
	 visit afterwards that don't have their own check, so that
	 when we reach BB, the check has already been performed.  */
      if (!postchecked && checked)
	{
	  for (int j = EDGE_COUNT (bb->preds); --j > i; )
	    chk_edges.safe_push (EDGE_PRED (bb, j));
	  postchecked = true;
	}
      if (postchecked && !checked)
	chk_edges.safe_push (EDGE_PRED (bb, i));
    }

  if (postchecked && bb->index >= NUM_FIXED_BLOCKS)
    {
      if (bitmap_set_bit (postchk_blocks, bb->index))
	count_postchk++;
      else
	gcc_unreachable ();
    }

  return postchecked;
}


class rt_bb_visited
{
  /* Use a sufficiently wide unsigned type to hold basic block numbers.  */
  typedef size_t blknum;

  /* Record the original block count of the function.  */
  blknum nblocks;
  /* Record the number of bits per VWORD (short for VISITED WORD), an
     efficient mode to set and test bits for blocks we visited, and to
     encode the CFG in case out-of-line verification is used.  */
  unsigned vword_bits;

  /* Hold the unsigned integral VWORD type.  */
  tree vword_type;
  /* Hold a pointer-to-VWORD type.  */
  tree vword_ptr;

  /* Hold a growing sequence used to check, inline or out-of-line,
     that VISITED encodes an expected execution path.  */
  gimple_seq ckseq;
  /* If nonNULL, hold a growing representation of the CFG for
     out-of-line testing.  */
  tree rtcfg;

  /* Hold the declaration of an array of VWORDs, used as an array of
     NBLOCKS-2 bits.  */
  tree visited;

  /* If performing inline checking, hold a declarations of boolean
     variables used for inline checking.  CKBLK holds the result of
     testing whether the VISITED bit corresponding to a predecessor or
     successor is set, CKINV inverts that bit, CKPART gets cleared if
     a block was not visited or if CKINV for any of its predecessors
     or successors is set, and CKFAIL gets set if CKPART remains set
     at the end of a block's predecessors or successors list.  */
  tree ckfail, ckpart, ckinv, ckblk;

  /* If we need to deal with abnormal edges, we insert SSA_NAMEs for
     boolean true and false.  */
  tree vfalse, vtrue;

  /* Convert a block index N to a block vindex, the index used to
     identify it in the VISITED array.  Check that it's in range:
     neither ENTRY nor EXIT, but maybe one-past-the-end, to compute
     the visited array length.  */
  blknum num2idx (blknum n) {
    gcc_checking_assert (n >= NUM_FIXED_BLOCKS && n <= nblocks);
    return (n - NUM_FIXED_BLOCKS);
  }
  /* Return the block vindex for BB, that must not be ENTRY or
     EXIT.  */
  blknum bb2idx (basic_block bb) {
    gcc_checking_assert (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
			 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
    gcc_checking_assert (blknum (bb->index) < nblocks);
    return num2idx (bb->index);
  }

  /* Compute the type to be used for the VISITED array.  */
  tree vtype ()
  {
    blknum n = num2idx (nblocks);
    return build_array_type_nelts (vword_type,
				   (n + vword_bits - 1) / vword_bits);
  }

  /* Compute and return the index into VISITED for block BB.  If BITP
     is non-NULL, also compute and store the bit mask corresponding to
     block BB in *BITP, so that (visited[index] & mask) tells whether
     BB was visited.  */
  tree vwordidx (basic_block bb, tree *bitp = NULL)
  {
    blknum idx = bb2idx (bb);
    if (bitp)
      {
	unsigned bit = idx % vword_bits;
	/* We don't need to adjust shifts to follow native bit
	   endianness here, all of our uses of the CFG and visited
	   bitmaps, whether at compile or runtime, are shifted bits on
	   full words.  This adjustment here would require a
	   corresponding adjustment at runtime, which would be nothing
	   but undesirable overhead for us.  */
	if (0 /* && BITS_BIG_ENDIAN */)
	  bit = vword_bits - bit - 1;
	wide_int wbit = wi::set_bit_in_zero (bit, vword_bits);
	*bitp = wide_int_to_tree (vword_type, wbit);
      }
    return build_int_cst (vword_ptr, idx / vword_bits);
  }

  /* Return an expr to accesses the visited element that holds
     information about BB.  If BITP is non-NULL, set it to the mask to
     tell which bit in that expr refers to BB.  */
  tree vword (basic_block bb, tree *bitp = NULL)
  {
    return build2 (MEM_REF, vword_type,
		   build1 (ADDR_EXPR, vword_ptr, visited),
		   int_const_binop (MULT_EXPR, vwordidx (bb, bitp),
				    fold_convert (vword_ptr,
						  TYPE_SIZE_UNIT
						  (vword_type))));
  }

  /* Return an expr that evaluates to true iff BB was marked as
     VISITED.  Add any gimple stmts to SEQP.  */
  tree vindex (basic_block bb, gimple_seq *seqp)
  {
    if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
	|| bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
      return boolean_true_node;

    tree bit, setme = vword (bb, &bit);
    tree temp = create_tmp_var (vword_type, ".cfrtemp");

    gassign *vload = gimple_build_assign (temp, setme);
    gimple_seq_add_stmt (seqp, vload);

    gassign *vmask = gimple_build_assign (temp, BIT_AND_EXPR, temp, bit);
    gimple_seq_add_stmt (seqp, vmask);

    return build2 (NE_EXPR, boolean_type_node,
		   temp, build_int_cst (vword_type, 0));
  }

  /* Set the bit corresponding to BB in VISITED.  Add to SEQ any
     required gimple stmts, and return SEQ, possibly modified.  */
  gimple_seq vset (basic_block bb, gimple_seq seq = NULL)
  {
    tree bit, setme = vword (bb, &bit);
    tree temp = create_tmp_var (vword_type, ".cfrtemp");

    gassign *vload = gimple_build_assign (temp, setme);
    gimple_seq_add_stmt (&seq, vload);

    gassign *vbitset = gimple_build_assign (temp, BIT_IOR_EXPR, temp, bit);
    gimple_seq_add_stmt (&seq, vbitset);

    gassign *vstore = gimple_build_assign (unshare_expr (setme), temp);
    gimple_seq_add_stmt (&seq, vstore);

    /* Prevent stores into visited from being deferred, forcing
       subsequent bitsets to reload the word rather than reusing
       values already in register.  The purpose is threefold: make the
       bitset get to memory in this block, so that control flow
       attacks in functions called in this block don't easily bypass
       the bitset; prevent the bitset word from being retained in a
       register across blocks, which could, in an attack scenario,
       make a later block set more than one bit; and prevent hoisting
       or sinking loads or stores of bitset words out of loops or even
       throughout functions, which could significantly weaken the
       verification.  This is equivalent to making the bitsetting
       volatile within the function body, but without changing its
       type; making the bitset volatile would make inline checking far
       less optimizable for no reason.  */
    vec<tree, va_gc> *inputs = NULL;
    vec<tree, va_gc> *outputs = NULL;
    vec_safe_push (outputs,
		   build_tree_list
		   (build_tree_list
		    (NULL_TREE, build_string (2, "=m")),
		    visited));
    vec_safe_push (inputs,
		   build_tree_list
		   (build_tree_list
		    (NULL_TREE, build_string (1, "m")),
		    visited));
    gasm *stabilize = gimple_build_asm_vec ("", inputs, outputs,
					    NULL, NULL);
    gimple_seq_add_stmt (&seq, stabilize);

    return seq;
  }

public:
  /* Prepare to add control flow redundancy testing to CFUN.  */
  rt_bb_visited (int checkpoints)
    : nblocks (n_basic_blocks_for_fn (cfun)),
      vword_type (NULL), ckseq (NULL), rtcfg (NULL),
      vfalse (NULL), vtrue (NULL)
  {
    /* If we've already added a declaration for the builtin checker,
       extract vword_type and vword_bits from its declaration.  */
    if (tree checkfn = builtin_decl_explicit (BUILT_IN___HARDCFR_CHECK))
      {
	tree check_arg_list = TYPE_ARG_TYPES (TREE_TYPE (checkfn));
	tree vword_const_ptr_type = TREE_VALUE (TREE_CHAIN (check_arg_list));
	vword_type = TYPE_MAIN_VARIANT (TREE_TYPE (vword_const_ptr_type));
	vword_bits = tree_to_shwi (TYPE_SIZE (vword_type));
      }
    /* Otherwise, select vword_bits, vword_type et al, and use it to
       declare the builtin checker.  */
    else
      {
	/* This setting needs to be kept in sync with libgcc/hardcfr.c.
	   We aim for at least 28 bits, which enables us to refer to as
	   many as 28 << 28 blocks in a function's CFG.  That's way over
	   4G blocks.  */
	machine_mode VWORDmode;
	if (BITS_PER_UNIT >= 28)
	  {
	    VWORDmode = QImode;
	    vword_bits = BITS_PER_UNIT;
	  }
	else if (BITS_PER_UNIT >= 14)
	  {
	    VWORDmode = HImode;
	    vword_bits = 2 * BITS_PER_UNIT;
	  }
	else
	  {
	    VWORDmode = SImode;
	    vword_bits = 4 * BITS_PER_UNIT;
	  }

	vword_type = lang_hooks.types.type_for_mode (VWORDmode, 1);
	gcc_checking_assert (vword_bits == tree_to_shwi (TYPE_SIZE
							 (vword_type)));

	vword_type = build_variant_type_copy (vword_type);
	TYPE_ALIAS_SET (vword_type) = new_alias_set ();

	tree vword_const = build_qualified_type (vword_type, TYPE_QUAL_CONST);
	tree vword_const_ptr = build_pointer_type (vword_const);
	tree type = build_function_type_list (void_type_node, sizetype,
					      vword_const_ptr, vword_const_ptr,
					      NULL_TREE);
	tree decl = add_builtin_function_ext_scope
	  ("__builtin___hardcfr_check",
	   type, BUILT_IN___HARDCFR_CHECK, BUILT_IN_NORMAL,
	   "__hardcfr_check", NULL_TREE);
	TREE_NOTHROW (decl) = true;
	set_builtin_decl (BUILT_IN___HARDCFR_CHECK, decl, true);
      }

    /* The checker uses a qualified pointer, so we can't reuse it,
       so build a new one.  */
    vword_ptr = build_pointer_type (vword_type);

    tree visited_type = vtype ();
    visited = create_tmp_var (visited_type, ".cfrvisited");

    if (nblocks - NUM_FIXED_BLOCKS > blknum (param_hardcfr_max_inline_blocks)
	|| checkpoints > 1)
      {
	/* Make sure vword_bits is wide enough for the representation
	   of nblocks in rtcfg.  Compare with vword_bits << vword_bits,
	   but avoiding overflows, shifting nblocks right instead.  If
	   vword_bits is wider than HOST_WIDE_INT, assume it fits, so
	   as to avoid undefined shifts.  */
	gcc_assert (HOST_BITS_PER_WIDE_INT <= vword_bits
		    || (((unsigned HOST_WIDE_INT)(num2idx (nblocks))
			 >> vword_bits) < vword_bits));

	/* Build a terminator for the constructor list.  */
	rtcfg = build_tree_list (NULL_TREE, NULL_TREE);
	return;
      }

    ckfail = create_tmp_var (boolean_type_node, ".cfrfail");
    ckpart = create_tmp_var (boolean_type_node, ".cfrpart");
    ckinv = create_tmp_var (boolean_type_node, ".cfrinv");
    ckblk = create_tmp_var (boolean_type_node, ".cfrblk");

    gassign *ckfail_init = gimple_build_assign (ckfail, boolean_false_node);
    gimple_seq_add_stmt (&ckseq, ckfail_init);
  }

  /* Insert SEQ before a resx or a call in INSBB.  */
  void insert_exit_check_in_block (gimple_seq seq, basic_block insbb)
  {
    gimple_stmt_iterator gsi = gsi_last_bb (insbb);

    while (!gsi_end_p (gsi))
      if (is_a <gresx *> (gsi_stmt (gsi))
	  || is_a <gcall *> (gsi_stmt (gsi)))
	break;
      else
	gsi_prev (&gsi);

    gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
  }

  /* Insert SEQ on E.  */
  void insert_exit_check_on_edge (gimple_seq seq, edge e)
  {
    if (!(e->flags & EDGE_ABNORMAL))
      {
	gsi_insert_seq_on_edge_immediate (e, seq);
	return;
      }

    /* Initialize SSA boolean constants for use in abnormal PHIs.  */
    if (!vfalse)
      {
	vfalse = make_ssa_name (boolean_type_node);
	vtrue = make_ssa_name (boolean_type_node);

	gimple_seq vft_seq = NULL;
	gassign *vfalse_init = gimple_build_assign (vfalse, boolean_false_node);
	gimple_seq_add_stmt (&vft_seq, vfalse_init);
	gassign *vtrue_init = gimple_build_assign (vtrue, boolean_true_node);
	gimple_seq_add_stmt (&vft_seq, vtrue_init);

	gsi_insert_seq_on_edge_immediate (single_succ_edge
					  (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
					  vft_seq);
      }

    /* We can't insert on abnormal edges, but we can arrange for SEQ
       to execute conditionally at dest.  Add a PHI boolean with TRUE
       from E and FALSE from other preds, split the whole block, add a
       test for the PHI to run a new block with SEQ or skip straight
       to the original block.  If there are multiple incoming abnormal
       edges, we'll do this multiple times.  ??? Unless there are
       multiple abnormal edges with different postcheck status, we
       could split the block and redirect other edges, rearranging the
       PHI nodes.  Optimizers already know how to do this, so we can
       keep things simple here.  */
    basic_block bb = e->dest;
    basic_block bb_postcheck = split_block_after_labels (bb)->dest;

    basic_block bb_check = create_empty_bb (e->dest);
    bb_check->count = e->count ();
    if (dom_info_available_p (CDI_DOMINATORS))
      set_immediate_dominator (CDI_DOMINATORS, bb_check, bb);
    if (current_loops)
      add_bb_to_loop (bb_check, current_loops->tree_root);

    gimple_stmt_iterator chkpt = gsi_after_labels (bb_check);
    gsi_insert_seq_before_without_update (&chkpt, seq, GSI_SAME_STMT);
    edge edge_postcheck = make_edge (bb_check, bb_postcheck, EDGE_FALLTHRU);
    edge_postcheck->probability = profile_probability::always ();

    tree cond_var = make_ssa_name (boolean_type_node);
    gcond *cond = gimple_build_cond (NE_EXPR, cond_var, boolean_false_node,
				     NULL, NULL);
    gimple_stmt_iterator condpt = gsi_after_labels (bb);
    gsi_insert_before (&condpt, cond, GSI_SAME_STMT);
    edge edge_nocheck = single_succ_edge (bb);
    edge_nocheck->flags &= ~EDGE_FALLTHRU;
    edge_nocheck->flags |= EDGE_FALSE_VALUE;
    edge edge_check = make_edge (bb, bb_check, EDGE_TRUE_VALUE);
    edge_check->probability = e->count ().probability_in (bb->count);
    edge_nocheck->probability = edge_check->probability.invert ();

    gphi *cond_phi = create_phi_node (cond_var, bb);
    for (int i = 0, ei = EDGE_COUNT (bb->preds); i < ei; i++)
      {
	edge pred = EDGE_PRED (bb, i);
	bool check_edge = pred == e;
	tree val = check_edge ? vtrue : vfalse;
	add_phi_arg (cond_phi, val, pred, UNKNOWN_LOCATION);
      }
  }

  /* Add checking code to CHK_EDGES and CHKCALL_BLOCKS, and
     initialization code on the entry edge.  Before this point, the
     CFG has been undisturbed, and all the needed data has been
     collected and safely stowed.  */
  void check (chk_edges_t &chk_edges,
	      int count_chkcall, auto_sbitmap const &chkcall_blocks)
  {
    /* If we're using out-of-line checking, create and statically
       initialize the CFG checking representation, generate the
       checker call for the checking sequence, and insert it in all
       exit edges, if there's more than one.  If there's only one, we
       use the same logic as the inline case to insert the check
       sequence.  */
    if (rtcfg)
      {
	/* Unreverse the list, and drop the tail node turned into head.  */
	rtcfg = TREE_CHAIN (nreverse (rtcfg));

	/* Turn the indices stored in TREE_PURPOSE into separate
	   nodes.  It was useful to keep them together to enable
	   combination of masks and for clear separation of
	   terminators while constructing it, but now we have to turn
	   it into a sequence of words.  */
	for (tree node = rtcfg; node; node = TREE_CHAIN (node))
	  {
	    tree wordidx = TREE_PURPOSE (node);
	    if (!wordidx)
	      continue;

	    TREE_PURPOSE (node) = NULL_TREE;
	    TREE_CHAIN (node) = tree_cons (NULL_TREE,
					   fold_convert (vword_type, wordidx),
					   TREE_CHAIN (node));
	  }

	/* Build the static initializer for the array with the CFG
	   representation for out-of-line checking.  */
	tree init = build_constructor_from_list (NULL_TREE, rtcfg);
	TREE_TYPE (init) = build_array_type_nelts (vword_type,
						   CONSTRUCTOR_NELTS (init));
	char buf[32];
	ASM_GENERATE_INTERNAL_LABEL (buf, "Lhardcfg",
				     current_function_funcdef_no);
	rtcfg = build_decl (UNKNOWN_LOCATION, VAR_DECL,
			    get_identifier (buf),
			    TREE_TYPE (init));
	TREE_READONLY (rtcfg) = 1;
	TREE_STATIC (rtcfg) = 1;
	TREE_ADDRESSABLE (rtcfg) = 1;
	TREE_USED (rtcfg) = 1;
	DECL_ARTIFICIAL (rtcfg) = 1;
	DECL_IGNORED_P (rtcfg) = 1;
	DECL_INITIAL (rtcfg) = init;
	make_decl_rtl (rtcfg);
	varpool_node::finalize_decl (rtcfg);

	/* Add the checker call to ckseq.  */
	gcall *call_chk = gimple_build_call (builtin_decl_explicit
					     (BUILT_IN___HARDCFR_CHECK), 3,
					     build_int_cst (sizetype,
							    num2idx (nblocks)),
					     build1 (ADDR_EXPR, vword_ptr,
						     visited),
					     build1 (ADDR_EXPR, vword_ptr,
						     rtcfg));
	gimple_seq_add_stmt (&ckseq, call_chk);

	gimple *clobber = gimple_build_assign (visited,
					       build_clobber
					       (TREE_TYPE (visited)));
	gimple_seq_add_stmt (&ckseq, clobber);

	/* If we have multiple exit edges, insert (copies of)
	   ckseq in all of them.  */
	for (int i = chk_edges.length (); i--; )
	  {
	    gimple_seq seq = ckseq;
	    /* Copy the sequence, unless we're dealing with the
	       last edge (we're counting down to zero).  */
	    if (i || count_chkcall)
	      seq = gimple_seq_copy (seq);

	    edge e = chk_edges[i];

	    if (dump_file)
	      {
		if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
		  fprintf (dump_file,
			   "Inserting out-of-line check in"
			   " block %i's edge to exit.\n",
			   e->src->index);
		else
		  fprintf (dump_file,
			   "Inserting out-of-line check in"
			   " block %i's edge to postcheck block %i.\n",
			   e->src->index, e->dest->index);
	      }

	    insert_exit_check_on_edge (seq, e);

	    gcc_checking_assert (!bitmap_bit_p (chkcall_blocks, e->src->index));
	  }

	sbitmap_iterator it;
	unsigned i;
	EXECUTE_IF_SET_IN_BITMAP (chkcall_blocks, 0, i, it)
	  {
	    basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);

	    gimple_seq seq = ckseq;
	    gcc_checking_assert (count_chkcall > 0);
	    if (--count_chkcall)
	      seq = gimple_seq_copy (seq);

	    if (dump_file)
	      fprintf (dump_file,
		       "Inserting out-of-line check before stmt in block %i.\n",
		       bb->index);

	    insert_exit_check_in_block (seq, bb);
	  }

	gcc_checking_assert (count_chkcall == 0);
      }
    else
      {
	/* Inline checking requires a single exit edge.  */
	gimple *last = gimple_build_assign (visited,
					    build_clobber
					    (TREE_TYPE (visited)));
	gimple_seq_add_stmt (&ckseq, last);

	if (!count_chkcall)
	  {
	    edge e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));

	    if (dump_file)
	      {
		if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
		  fprintf (dump_file,
			   "Inserting out-of-line check in"
			   " block %i's edge to postcheck block %i.\n",
			   e->src->index, e->dest->index);
		else
		  fprintf (dump_file,
			   "Inserting inline check in"
			   " block %i's edge to exit.\n",
			   e->src->index);
	      }

	    insert_exit_check_on_edge (ckseq, e);
	  }
	else
	  {
	    gcc_checking_assert (count_chkcall == 1);

	    sbitmap_iterator it;
	    unsigned i;
	    EXECUTE_IF_SET_IN_BITMAP (chkcall_blocks, 0, i, it)
	      {
		basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);

		gimple_seq seq = ckseq;
		gcc_checking_assert (count_chkcall > 0);
		if (--count_chkcall)
		  seq = gimple_seq_copy (seq);

		if (dump_file)
		  fprintf (dump_file,
			   "Inserting inline check before stmt in block %i.\n",
			   bb->index);

		insert_exit_check_in_block (seq, bb);
	      }

	    gcc_checking_assert (count_chkcall == 0);
	  }

	/* The inserted ckseq computes CKFAIL at LAST.  Now we have to
	   conditionally trap on it.  */
	basic_block insbb = gimple_bb (last);

	/* Create a block with the unconditional trap.  */
	basic_block trp = create_empty_bb (insbb);
	gimple_stmt_iterator gsit = gsi_after_labels (trp);

	gcall *trap = gimple_build_call (builtin_decl_explicit
					 (BUILT_IN_TRAP), 0);
	gsi_insert_before (&gsit, trap, GSI_SAME_STMT);

	if (BB_PARTITION (insbb))
	  BB_SET_PARTITION (trp, BB_COLD_PARTITION);

	if (current_loops)
	  add_bb_to_loop (trp, current_loops->tree_root);

	/* Insert a conditional branch to the trap block.  If the
	   conditional wouldn't be the last stmt, split the block.  */
	gimple_stmt_iterator gsi = gsi_for_stmt (last);
	if (!gsi_one_before_end_p (gsi))
	  split_block (gsi_bb (gsi), gsi_stmt (gsi));

	gcond *cond = gimple_build_cond (NE_EXPR, ckfail,
					 fold_convert (TREE_TYPE (ckfail),
						       boolean_false_node),
					 NULL, NULL);
	gsi_insert_after (&gsi, cond, GSI_SAME_STMT);

	/* Adjust the edges.  */
	single_succ_edge (gsi_bb (gsi))->flags &= ~EDGE_FALLTHRU;
	single_succ_edge (gsi_bb (gsi))->flags |= EDGE_FALSE_VALUE;
	single_succ_edge (gsi_bb (gsi))->probability
	  = profile_probability::always ();
	edge e = make_edge (gsi_bb (gsi), trp, EDGE_TRUE_VALUE);
	e->probability = profile_probability::never ();
	gcc_checking_assert (e->dest == trp);
	gcc_checking_assert (!e->dest->count.initialized_p ());
	e->dest->count = e->count ();

	/* Set the trap's dominator after splitting.  */
	if (dom_info_available_p (CDI_DOMINATORS))
	  set_immediate_dominator (CDI_DOMINATORS, trp, gimple_bb (last));
      }

    /* Insert initializers for visited at the entry.  Do this after
       other insertions, to avoid messing with block numbers.  */
    gimple_seq iseq = NULL;

    gcall *vinit = gimple_build_call (builtin_decl_explicit
				      (BUILT_IN_MEMSET), 3,
				      build1 (ADDR_EXPR,
					      build_pointer_type
					      (TREE_TYPE (visited)),
					      visited),
				      integer_zero_node,
				      TYPE_SIZE_UNIT (TREE_TYPE (visited)));
    gimple_seq_add_stmt (&iseq, vinit);

    gsi_insert_seq_on_edge_immediate (single_succ_edge
				      (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
				      iseq);
  }

  /* Push onto RTCFG a (mask, index) pair to test for IBB when BB is
     visited. XSELF is to be the ENTRY or EXIT block (depending on
     whether we're looking at preds or succs), to be remapped to BB
     because we can't represent them, and there's no point in testing
     them anyway.  Return true if no further blocks need to be visited
     in the list, because we've already encountered a
     self-reference.  */
  bool
  push_rtcfg_pair (basic_block ibb, basic_block bb,
		   basic_block xself)
  {
    /* We don't have a bit to test for the entry and exit
       blocks, but it is always visited, so we test for the
       block itself, which gets us the right result and
       enables the self-test optimization below.  */
    if (ibb == xself)
      ibb = bb;

    tree mask, idx = vwordidx (ibb, &mask);
    /* Combine masks with the same idx, but not if we're going
       to optimize for self-test.  */
    if (ibb != bb && TREE_PURPOSE (rtcfg)
	&& tree_int_cst_equal (idx, TREE_PURPOSE (rtcfg)))
      TREE_VALUE (rtcfg) = int_const_binop (BIT_IOR_EXPR, mask,
					    TREE_VALUE (rtcfg));
    else
      rtcfg = tree_cons (idx, mask, rtcfg);

    /* For self-tests (i.e., tests that the block itself was
       also visited), testing anything else is pointless,
       because it's a tautology, so just drop other edges.  */
    if (ibb == bb)
      {
	while (TREE_PURPOSE (TREE_CHAIN (rtcfg)))
	  TREE_CHAIN (rtcfg) = TREE_CHAIN (TREE_CHAIN (rtcfg));
	return true;
      }

    return false;
  }

  /* Add to CKSEQ stmts to clear CKPART if OBB is visited.  */
  void
  build_block_check (basic_block obb)
  {
    tree vobb = fold_convert (TREE_TYPE (ckblk),
			      vindex (obb, &ckseq));
    gassign *blkrunp = gimple_build_assign (ckblk, vobb);
    gimple_seq_add_stmt (&ckseq, blkrunp);

    gassign *blknotrunp = gimple_build_assign (ckinv,
					       EQ_EXPR,
					       ckblk,
					       fold_convert
					       (TREE_TYPE (ckblk),
						boolean_false_node));
    gimple_seq_add_stmt (&ckseq, blknotrunp);

    gassign *andblk = gimple_build_assign (ckpart,
					   BIT_AND_EXPR,
					   ckpart, ckinv);
    gimple_seq_add_stmt (&ckseq, andblk);
  }

  /* Add to BB code to set its bit in VISITED, and add to RTCFG or
     CKSEQ the data or code needed to check BB's predecessors and
     successors.  If CHECKPOINT, assume the block is a checkpoint,
     whether or not it has an edge to EXIT.  If POSTCHECK, assume the
     block post-dominates checkpoints and therefore no bitmap setting
     or checks are to be performed in or for it.  Do NOT change the
     CFG.  */
  void visit (basic_block bb, bool checkpoint, bool postcheck)
  {
    /* Set the bit in VISITED when entering the block.  */
    gimple_stmt_iterator gsi = gsi_after_labels (bb);
    if (!postcheck)
      gsi_insert_seq_before (&gsi, vset (bb), GSI_SAME_STMT);

    if (rtcfg)
      {
	if (!postcheck)
	  {
	    /* Build a list of (index, mask) terminated by (NULL, 0).
	       Consolidate masks with the same index when they're
	       adjacent.  First, predecessors.  Count backwards, because
	       we're going to reverse the list.  The order shouldn't
	       matter, but let's not make it surprising.  */
	    for (int i = EDGE_COUNT (bb->preds); i--; )
	      if (push_rtcfg_pair (EDGE_PRED (bb, i)->src, bb,
				   ENTRY_BLOCK_PTR_FOR_FN (cfun)))
		break;
	  }
	rtcfg = tree_cons (NULL_TREE, build_int_cst (vword_type, 0), rtcfg);

	if (!postcheck)
	  {
	    /* Then, successors.  */
	    if (!checkpoint
		|| !push_rtcfg_pair (EXIT_BLOCK_PTR_FOR_FN (cfun),
				     bb, EXIT_BLOCK_PTR_FOR_FN (cfun)))
	      for (int i = EDGE_COUNT (bb->succs); i--; )
		if (push_rtcfg_pair (EDGE_SUCC (bb, i)->dest, bb,
				     EXIT_BLOCK_PTR_FOR_FN (cfun)))
		  break;
	  }
	rtcfg = tree_cons (NULL_TREE, build_int_cst (vword_type, 0), rtcfg);
      }
    else if (!postcheck)
      {
	/* Schedule test to fail if the block was reached but somehow none
	   of its predecessors were.  */
	tree bit = fold_convert (TREE_TYPE (ckpart), vindex (bb, &ckseq));
	gassign *blkrunp = gimple_build_assign (ckpart, bit);
	gimple_seq_add_stmt (&ckseq, blkrunp);

	for (int i = 0, e = EDGE_COUNT (bb->preds); i < e; i++)
	  build_block_check (EDGE_PRED (bb, i)->src);
	gimple *orfailp = gimple_build_assign (ckfail, BIT_IOR_EXPR,
					       ckfail, ckpart);
	gimple_seq_add_stmt (&ckseq, orfailp);

	/* Likewise for successors.  */
	gassign *blkruns = gimple_build_assign (ckpart, unshare_expr (bit));
	gimple_seq_add_stmt (&ckseq, blkruns);

	if (checkpoint)
	  build_block_check (EXIT_BLOCK_PTR_FOR_FN (cfun));
	for (int i = 0, e = EDGE_COUNT (bb->succs); i < e; i++)
	  build_block_check (EDGE_SUCC (bb, i)->dest);

	gimple *orfails = gimple_build_assign (ckfail, BIT_IOR_EXPR,
					       ckfail, ckpart);
	gimple_seq_add_stmt (&ckseq, orfails);
      }
  }
};

/* Avoid checking before noreturn calls that are known (expected,
   really) to finish by throwing an exception, rather than by ending
   the program or looping forever.  Such functions have to be
   annotated, with an attribute (expected_throw) or flag (ECF_XTHROW),
   so that exception-raising functions, such as C++'s __cxa_throw,
   __cxa_rethrow, and Ada's gnat_rcheck_*, gnat_reraise*,
   ada.exception.raise_exception*, and the language-independent
   unwinders could be detected here and handled differently from other
   noreturn functions.  */
static bool
always_throwing_noreturn_call_p (gimple *stmt)
{
  if (!is_a <gcall *> (stmt))
    return is_a <gresx *> (stmt);

  gcall *call = as_a <gcall *> (stmt);
  return (gimple_call_noreturn_p (call)
	  && gimple_call_expected_throw_p (call));
}

/* Control flow redundancy hardening: record the execution path, and
   verify at exit that an expect path was taken.  */

unsigned int
pass_harden_control_flow_redundancy::execute (function *fun)
{
  bool const check_at_escaping_exceptions
    = (flag_exceptions
       && flag_harden_control_flow_redundancy_check_exceptions);
  bool const check_before_noreturn_calls
    = flag_harden_control_flow_redundancy_check_noreturn > HCFRNR_NEVER;
  bool const check_before_nothrow_noreturn_calls
    = (check_before_noreturn_calls
       && flag_harden_control_flow_redundancy_check_noreturn >= HCFRNR_NOTHROW);
  bool const check_before_throwing_noreturn_calls
    = (flag_exceptions
       && check_before_noreturn_calls
       && flag_harden_control_flow_redundancy_check_noreturn > HCFRNR_NOTHROW);
  bool const check_before_always_throwing_noreturn_calls
    = (flag_exceptions
       && check_before_noreturn_calls
       && flag_harden_control_flow_redundancy_check_noreturn >= HCFRNR_ALWAYS);
  basic_block bb;
  basic_block bb_eh_cleanup = NULL;

  if (flag_harden_control_flow_redundancy_skip_leaf)
    {
      bool found_calls_p = false;

      FOR_EACH_BB_FN (bb, fun)
	{
	  for (gimple_stmt_iterator gsi = gsi_last_bb (bb);
	       !gsi_end_p (gsi); gsi_prev (&gsi))
	    if (is_a <gcall *> (gsi_stmt (gsi)))
	      {
		found_calls_p = true;
		break;
	      }
	  if (found_calls_p)
	    break;
	}

      if (!found_calls_p)
	{
	  if (dump_file)
	    fprintf (dump_file,
		     "Disabling CFR for leaf function, as requested\n");

	  return 0;
	}
    }

  if (check_at_escaping_exceptions)
    {
      int lp_eh_cleanup = -1;

      /* Record the preexisting blocks, to avoid visiting newly-created
	 blocks.  */
      auto_sbitmap to_visit (last_basic_block_for_fn (fun));
      bitmap_clear (to_visit);

      FOR_EACH_BB_FN (bb, fun)
	bitmap_set_bit (to_visit, bb->index);

      /* Scan the blocks for stmts with escaping exceptions, that
	 wouldn't be denoted in the CFG, and associate them with an
	 empty cleanup handler around the whole function.  Walk
	 backwards, so that even when we split the block, */
      sbitmap_iterator it;
      unsigned i;
      EXECUTE_IF_SET_IN_BITMAP (to_visit, 0, i, it)
	{
	  bb = BASIC_BLOCK_FOR_FN (fun, i);

	  for (gimple_stmt_iterator gsi = gsi_last_bb (bb);
	       !gsi_end_p (gsi); gsi_prev (&gsi))
	    {
	      gimple *stmt = gsi_stmt (gsi);
	      if (!stmt_could_throw_p (fun, stmt))
		continue;

	      /* If it must not throw, or if it already has a handler,
		 we need not worry about it.  */
	      if (lookup_stmt_eh_lp (stmt) != 0)
		continue;

	      /* Don't split blocks at, nor add EH edges to, tail
		 calls, we will add verification before the call
		 anyway.  */
	      if (is_a <gcall *> (stmt)
		  && (gimple_call_must_tail_p (as_a <gcall *> (stmt))
		      || gimple_call_tail_p (as_a <gcall *> (stmt))
		      || returning_call_p (as_a <gcall *> (stmt))))
		continue;

	      if (!gsi_one_before_end_p (gsi))
		split_block (bb, stmt);
	      /* A resx or noreturn call needs not be associated with
		 the cleanup handler if we're going to add checking
		 before it.  We only test cases that didn't require
		 block splitting because noreturn calls would always
		 be at the end of blocks, and we test for zero
		 successors because if there is an edge, it's not
		 noreturn, as any EH edges would have already been
		 caught by the lookup_stmt_eh_lp test above.  */
	      else if (check_before_noreturn_calls
		       && EDGE_COUNT (bb->succs) == 0
		       && (is_a <gresx *> (stmt)
			   ? check_before_always_throwing_noreturn_calls
			   : (!is_a <gcall *> (stmt)
			      || !gimple_call_noreturn_p (stmt))
			   ? (gcc_unreachable (), false)
			   : (!flag_exceptions
			      || gimple_call_nothrow_p (as_a <gcall *> (stmt)))
			   ? check_before_nothrow_noreturn_calls
			   : always_throwing_noreturn_call_p (stmt)
			   ? check_before_always_throwing_noreturn_calls
			   : check_before_throwing_noreturn_calls))
		{
		  if (dump_file)
		    {
		      fprintf (dump_file,
			       "Bypassing cleanup for noreturn stmt"
			       " in block %i:\n",
			       bb->index);
		      print_gimple_stmt (dump_file, stmt, 0);
		    }
		  continue;
		}

	      if (!bb_eh_cleanup)
		{
		  bb_eh_cleanup = create_empty_bb (bb);
		  if (dom_info_available_p (CDI_DOMINATORS))
		    set_immediate_dominator (CDI_DOMINATORS, bb_eh_cleanup, bb);
		  if (current_loops)
		    add_bb_to_loop (bb_eh_cleanup, current_loops->tree_root);

		  /* Make the new block an EH cleanup for the call.  */
		  eh_region new_r = gen_eh_region_cleanup (NULL);
		  eh_landing_pad lp = gen_eh_landing_pad (new_r);
		  tree label = gimple_block_label (bb_eh_cleanup);
		  lp->post_landing_pad = label;
		  EH_LANDING_PAD_NR (label) = lp_eh_cleanup = lp->index;

		  /* Just propagate the exception.
		     We will later insert the verifier call.  */
		  gimple_stmt_iterator ehgsi;
		  ehgsi = gsi_after_labels (bb_eh_cleanup);
		  gresx *resx = gimple_build_resx (new_r->index);
		  gsi_insert_before (&ehgsi, resx, GSI_SAME_STMT);

		  if (dump_file)
		    fprintf (dump_file,
			     "Created cleanup block %i:\n",
			     bb_eh_cleanup->index);
		}
	      else if (dom_info_available_p (CDI_DOMINATORS))
		{
		  basic_block immdom;
		  immdom = get_immediate_dominator (CDI_DOMINATORS,
						    bb_eh_cleanup);
		  if (!dominated_by_p (CDI_DOMINATORS, bb, immdom))
		    {
		      immdom = nearest_common_dominator (CDI_DOMINATORS,
							 immdom, bb);
		      set_immediate_dominator (CDI_DOMINATORS,
					       bb_eh_cleanup, immdom);
		    }
		}

	      if (dump_file)
		{
		  fprintf (dump_file,
			   "Associated cleanup block with stmt in block %i:\n",
			   bb->index);
		  print_gimple_stmt (dump_file, stmt, 0);
		}

	      add_stmt_to_eh_lp (stmt, lp_eh_cleanup);
	      /* Finally, wire the EH cleanup block into the CFG.  */
	      edge neeh = make_eh_edge (stmt);
	      neeh->probability = profile_probability::never ();
	      gcc_checking_assert (neeh->dest == bb_eh_cleanup);
	      if (neeh->dest->count.initialized_p ())
		neeh->dest->count += neeh->count ();
	      else
		neeh->dest->count = neeh->count ();
	    }
	}

      if (bb_eh_cleanup)
	{
	  /* A cfg_cleanup after bb_eh_cleanup makes for a more compact
	     rtcfg, and it avoids bb numbering differences when we split
	     blocks because of trailing debug insns only.  */
	  cleanup_tree_cfg ();
	  gcc_checking_assert (EDGE_COUNT (bb_eh_cleanup->succs) == 0);
	}
    }

  /* These record blocks with calls that are to be preceded by
     checkpoints, such as noreturn calls (if so chosen), must-tail
     calls, potential early-marked tail calls, and returning calls (if
     so chosen).  */
  int count_chkcall = 0;
  auto_sbitmap chkcall_blocks (last_basic_block_for_fn (fun));
  bitmap_clear (chkcall_blocks);

  /* We wish to add verification at blocks without successors, such as
     noreturn calls (raising or not) and the reraise at the cleanup
     block, but not other reraises: they will go through the cleanup
     block.  */
  if (check_before_noreturn_calls)
    FOR_EACH_BB_FN (bb, fun)
      {
	gimple_stmt_iterator gsi = gsi_last_bb (bb);
	if (gsi_end_p (gsi))
	  continue;
	gimple *stmt = gsi_stmt (gsi);

	if (EDGE_COUNT (bb->succs) == 0)
	  {
	    /* A stmt at the end of a block without any successors is
	       either a resx or a noreturn call without a local
	       handler.  Check that it's one of the desired
	       checkpoints.  */
	    if (flag_exceptions && is_a <gresx *> (stmt)
		? (check_before_always_throwing_noreturn_calls
		   || bb == bb_eh_cleanup)
		: (!is_a <gcall *> (stmt)
		   || !gimple_call_noreturn_p (stmt))
		? (stmt_can_make_abnormal_goto (stmt)
		   /* ??? Check before indirect nonlocal goto, or
		      calls thereof?  */
		   ? false
		   /* Catch cases in which successors would be
		      expected.  */
		   : (gcc_unreachable (), false))
		: (!flag_exceptions
		   || gimple_call_nothrow_p (as_a <gcall *> (stmt)))
		? check_before_nothrow_noreturn_calls
		: always_throwing_noreturn_call_p (stmt)
		? check_before_always_throwing_noreturn_calls
		: check_before_throwing_noreturn_calls)
	      {
		if (dump_file)
		  {
		    fprintf (dump_file,
			     "Scheduling check before stmt"
			     " in succ-less block %i:\n",
			     bb->index);
		    print_gimple_stmt (dump_file, stmt, 0);
		  }

		if (bitmap_set_bit (chkcall_blocks, bb->index))
		  count_chkcall++;
		else
		  gcc_unreachable ();
	      }
	    continue;
	  }

	/* If there are no exceptions, it would seem like any noreturn
	   call must have zero successor edges, but __builtin_return
	   gets successor edges.  We don't want to handle it here, it
	   will be dealt with in sibcall_search_preds.  Otherwise,
	   check for blocks without non-EH successors, but skip those
	   with resx stmts and edges (i.e., those other than that in
	   bb_eh_cleanup), since those will go through bb_eh_cleanup,
	   that will have been counted as noreturn above because it
	   has no successors.  */
	gcc_checking_assert (bb != bb_eh_cleanup
			     || !check_at_escaping_exceptions);
	if (flag_exceptions && is_a <gresx *> (stmt)
	    ? check_before_always_throwing_noreturn_calls
	    : (!is_a <gcall *> (stmt)
	       || !gimple_call_noreturn_p (stmt))
	    ? false
	    : (!flag_exceptions
	       || gimple_call_nothrow_p (as_a <gcall *> (stmt)))
	    ? false /* rather than check_before_nothrow_noreturn_calls */
	    : always_throwing_noreturn_call_p (stmt)
	    ? check_before_always_throwing_noreturn_calls
	    : check_before_throwing_noreturn_calls)
	  {
	    gcc_checking_assert (single_succ_p (bb)
				 && (single_succ_edge (bb)->flags & EDGE_EH));

	    if (dump_file)
	      {
		fprintf (dump_file,
			 "Scheduling check before stmt"
			 " in EH-succ block %i:\n",
			 bb->index);
		print_gimple_stmt (dump_file, stmt, 0);
	      }

	    if (bitmap_set_bit (chkcall_blocks, bb->index))
	      count_chkcall++;
	    else
	      gcc_unreachable ();
	  }
      }
  else if (bb_eh_cleanup)
    {
      if (bitmap_set_bit (chkcall_blocks, bb_eh_cleanup->index))
	count_chkcall++;
      else
	gcc_unreachable ();
    }

  gcc_checking_assert (!bb_eh_cleanup
		       || bitmap_bit_p (chkcall_blocks, bb_eh_cleanup->index));

  /* If we don't have edges to exit nor noreturn calls (including the
     cleanup reraise), then we may skip instrumentation: that would
     amount to a function that ends with an infinite loop.  */
  if (!count_chkcall
      && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
    {
      if (dump_file)
	fprintf (dump_file,
		 "Disabling CFR, no exit paths to check\n");

      return 0;
    }

  /* Search for must-tail calls, early-marked potential tail calls,
     and, if requested, returning calls.  As we introduce early
     checks, */
  int count_postchk = 0;
  auto_sbitmap postchk_blocks (last_basic_block_for_fn (fun));
  bitmap_clear (postchk_blocks);
  chk_edges_t chk_edges;
  hardcfr_sibcall_search_preds (EXIT_BLOCK_PTR_FOR_FN (fun), chk_edges,
				count_chkcall, chkcall_blocks,
				count_postchk, postchk_blocks,
				NULL);

  rt_bb_visited vstd (chk_edges.length () + count_chkcall);

  auto_sbitmap combined_blocks (last_basic_block_for_fn (fun));
  bitmap_copy (combined_blocks, chkcall_blocks);
  int i;
  edge *e;
  FOR_EACH_VEC_ELT (chk_edges, i, e)
    if (!bitmap_set_bit (combined_blocks, (*e)->src->index))
      /* There may be multiple chk_edges with the same src block;
	 guard againt overlaps with chkcall_blocks only.  */
      gcc_assert (!bitmap_bit_p (chkcall_blocks, (*e)->src->index));

  /* Visit blocks in index order, because building rtcfg depends on
     that.  Blocks must be compact, which the cleanup_cfg requirement
     ensures.  This would also enable FOR_EACH_BB_FN to be used to
     iterate in index order, but bb_eh_cleanup block splits and
     insertions changes that.  */
  gcc_checking_assert (n_basic_blocks_for_fn (fun)
		       == last_basic_block_for_fn (fun));
  for (int i = NUM_FIXED_BLOCKS; i < n_basic_blocks_for_fn (fun); i++)
    {
      bb = BASIC_BLOCK_FOR_FN (fun, i);
      gcc_checking_assert (bb->index == i);
      vstd.visit (bb, bitmap_bit_p (combined_blocks, i),
		  bitmap_bit_p (postchk_blocks, i));
    }

  vstd.check (chk_edges, count_chkcall, chkcall_blocks);

  return
    TODO_update_ssa
    | TODO_cleanup_cfg
    | TODO_verify_il;
}

/* Instantiate a hardcfr pass.  */

gimple_opt_pass *
make_pass_harden_control_flow_redundancy (gcc::context *ctxt)
{
  return new pass_harden_control_flow_redundancy (ctxt);
}