Skip to content

Commit aa89c01

Browse files
committed
[Test] Basic tests for DeadEndBlocks.
1 parent 353e495 commit aa89c01

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

lib/SIL/Utils/BasicBlockUtils.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/SIL/BasicBlockUtils.h"
14-
#include "swift/SIL/BasicBlockDatastructures.h"
1514
#include "swift/Basic/Defer.h"
1615
#include "swift/Basic/STLExtras.h"
16+
#include "swift/SIL/BasicBlockDatastructures.h"
1717
#include "swift/SIL/Dominance.h"
1818
#include "swift/SIL/LoopInfo.h"
1919
#include "swift/SIL/OwnershipUtils.h"
@@ -22,6 +22,7 @@
2222
#include "swift/SIL/SILBuilder.h"
2323
#include "swift/SIL/SILFunction.h"
2424
#include "swift/SIL/TerminatorUtils.h"
25+
#include "swift/SIL/Test.h"
2526
#include "llvm/ADT/STLExtras.h"
2627

2728
using namespace swift;
@@ -420,6 +421,27 @@ bool DeadEndBlocks::triviallyEndsInUnreachable(SILBasicBlock *block) {
420421
return isa<UnreachableInst>(block->getTerminator());
421422
}
422423

424+
namespace swift::test {
425+
// Arguments:
426+
// - none
427+
// Dumps:
428+
// - the function
429+
// - the blocks which are dead-end blocks
430+
static FunctionTest DeadEndBlocksTest("dead_end_blocks", [](auto &function,
431+
auto &arguments,
432+
auto &test) {
433+
std::unique_ptr<DeadEndBlocks> DeadEnds;
434+
DeadEnds.reset(new DeadEndBlocks(&function));
435+
function.print(llvm::outs());
436+
#ifndef NDEBUG
437+
for (auto &block : function) {
438+
if (DeadEnds->isDeadEnd(&block))
439+
block.printID(llvm::outs(), true);
440+
}
441+
#endif
442+
});
443+
} // end namespace swift::test
444+
423445
//===----------------------------------------------------------------------===//
424446
// Post Dominance Set Completion Utilities
425447
//===----------------------------------------------------------------------===//

test/SILOptimizer/dead_end_blocks.sil

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// RUN: %target-sil-opt -test-runner %s -o /dev/null 2>&1 | %FileCheck %s
2+
3+
// REQUIRES: asserts
4+
5+
// CHECK-LABEL: begin running test {{.*}} on with_trap: dead_end_blocks
6+
// CHECK-LABEL: sil @with_trap : {{.*}} {
7+
// CHECK: cond_br undef, [[DIE:bb[0-9]+]], [[EXIT:bb[0-9]+]]
8+
// CHECK: [[DIE]]:
9+
// CHECK: unreachable
10+
// CHECK-LABEL: } // end sil function 'with_trap'
11+
// CHECK: [[DIE]]
12+
// CHECK-LABEL: end running test {{.*}} on with_trap: dead_end_blocks
13+
sil @with_trap : $@convention(thin) () -> () {
14+
entry:
15+
specify_test "dead_end_blocks"
16+
cond_br undef, die, exit
17+
18+
die:
19+
unreachable
20+
21+
exit:
22+
%retval = tuple ()
23+
return %retval : $()
24+
}
25+
26+
// CHECK-LABEL: begin running test {{.*}} on with_infinite_loop: dead_end_blocks
27+
// CHECK-LABEL: sil @with_infinite_loop : $@convention(thin) () -> () {
28+
// CHECK: cond_br undef, [[EXIT:bb[0-9]+]], [[HEADER:bb[0-9]+]]
29+
// CHECK: [[HEADER]]:
30+
// CHECK: br [[LOOP:bb[0-9]+]]
31+
// CHECK: [[LOOP]]:
32+
// CHECK: br [[LOOP]]
33+
// CHECK-LABEL: } // end sil function 'with_infinite_loop'
34+
// CHECK: [[HEADER]]
35+
// CHECK: [[LOOP]]
36+
// CHECK-LABEL: end running test {{.*}} on with_infinite_loop: dead_end_blocks
37+
sil @with_infinite_loop : $@convention(thin) () -> () {
38+
entry:
39+
specify_test "dead_end_blocks"
40+
cond_br undef, exit, header
41+
header:
42+
br loop
43+
loop:
44+
br loop
45+
exit:
46+
%retval = tuple ()
47+
return %retval : $()
48+
}
49+
50+

0 commit comments

Comments
 (0)