Skip to content

Commit e702076

Browse files
committed
[Test] Basic tests for DeadEndBlocks.
1 parent 679552d commit e702076

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,16 +11,17 @@
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/SILArgument.h"
2020
#include "swift/SIL/SILBasicBlock.h"
2121
#include "swift/SIL/SILBuilder.h"
2222
#include "swift/SIL/SILFunction.h"
2323
#include "swift/SIL/TerminatorUtils.h"
24+
#include "swift/SIL/Test.h"
2425
#include "llvm/ADT/STLExtras.h"
2526

2627
using namespace swift;
@@ -413,6 +414,27 @@ bool DeadEndBlocks::triviallyEndsInUnreachable(SILBasicBlock *block) {
413414
return isa<UnreachableInst>(block->getTerminator());
414415
}
415416

417+
namespace swift::test {
418+
// Arguments:
419+
// - none
420+
// Dumps:
421+
// - the function
422+
// - the blocks which are dead-end blocks
423+
static FunctionTest DeadEndBlocksTest("dead_end_blocks", [](auto &function,
424+
auto &arguments,
425+
auto &test) {
426+
std::unique_ptr<DeadEndBlocks> DeadEnds;
427+
DeadEnds.reset(new DeadEndBlocks(&function));
428+
function.print(llvm::outs());
429+
#ifndef NDEBUG
430+
for (auto &block : function) {
431+
if (DeadEnds->isDeadEnd(&block))
432+
block.printID(llvm::outs(), true);
433+
}
434+
#endif
435+
});
436+
} // end namespace swift::test
437+
416438
//===----------------------------------------------------------------------===//
417439
// Post Dominance Set Completion Utilities
418440
//===----------------------------------------------------------------------===//

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)