1 ; Test CFG simplify removal of branch instructions. 2 ; 3 ; RUN: opt < %s -simplifycfg -S | FileCheck %s 4 ; RUN: opt < %s -passes=simplify-cfg -S | FileCheck %s 5 6 define void @test1() { 7 br label %1 8 ret void 9 ; CHECK-LABEL: @test1( 10 ; CHECK-NEXT: ret void 11 } 12 13 define void @test2() { 14 ret void 15 ret void 16 ; CHECK-LABEL: @test2( 17 ; CHECK-NEXT: ret void 18 ; CHECK-NEXT: } 19 } 20 21 define void @test3(i1 %T) { 22 br i1 %T, label %1, label %1 23 ret void 24 ; CHECK-LABEL: @test3( 25 ; CHECK-NEXT: ret void 26 } 27 28 ; Folding branch to a common destination. 29 ; CHECK-LABEL: @test4_fold 30 ; CHECK: %cmp1 = icmp eq i32 %a, %b 31 ; CHECK: %cmp2 = icmp ugt i32 %a, 0 32 ; CHECK: %or.cond = and i1 %cmp1, %cmp2 33 ; CHECK: br i1 %or.cond, label %else, label %untaken 34 ; CHECK-NOT: taken: 35 ; CHECK: ret void 36 define void @test4_fold(i32 %a, i32 %b) { 37 %cmp1 = icmp eq i32 %a, %b 38 br i1 %cmp1, label %taken, label %untaken 39 40 taken: 41 %cmp2 = icmp ugt i32 %a, 0 42 br i1 %cmp2, label %else, label %untaken 43 44 else: 45 call void @foo() 46 ret void 47 48 untaken: 49 ret void 50 } 51 52 ; Prefer a simplification based on a dominating condition rather than folding a 53 ; branch to a common destination. 54 ; CHECK-LABEL: @test4 55 ; CHECK-NOT: br 56 ; CHECK-NOT: br 57 ; CHECK-NOT: call 58 ; CHECK: ret void 59 define void @test4_no_fold(i32 %a, i32 %b) { 60 %cmp1 = icmp eq i32 %a, %b 61 br i1 %cmp1, label %taken, label %untaken 62 63 taken: 64 %cmp2 = icmp ugt i32 %a, %b 65 br i1 %cmp2, label %else, label %untaken 66 67 else: 68 call void @foo() 69 ret void 70 71 untaken: 72 ret void 73 } 74 75 declare void @foo() 76 77 ; PR5795 78 define void @test5(i32 %A) { 79 switch i32 %A, label %return [ 80 i32 2, label %1 81 i32 10, label %2 82 ] 83 84 ret void 85 86 ret void 87 88 return: ; preds = %entry 89 ret void 90 ; CHECK-LABEL: @test5( 91 ; CHECK-NEXT: ret void 92 } 93 94 95 ; PR14893 96 define i8 @test6f() { 97 ; CHECK-LABEL: @test6f 98 ; CHECK: alloca i8, align 1 99 ; CHECK-NEXT: call i8 @test6g 100 ; CHECK-NEXT: icmp eq i8 %tmp, 0 101 ; CHECK-NEXT: load i8, i8* %r, align 1, !dbg !{{[0-9]+$}} 102 103 bb0: 104 %r = alloca i8, align 1 105 %tmp = call i8 @test6g(i8* %r) 106 %tmp1 = icmp eq i8 %tmp, 0 107 br i1 %tmp1, label %bb2, label %bb1 108 bb1: 109 %tmp3 = load i8, i8* %r, align 1, !range !2, !tbaa !1, !dbg !5 110 %tmp4 = icmp eq i8 %tmp3, 1 111 br i1 %tmp4, label %bb2, label %bb3 112 bb2: 113 br label %bb3 114 bb3: 115 %tmp6 = phi i8 [ 0, %bb2 ], [ 1, %bb1 ] 116 ret i8 %tmp6 117 } 118 declare i8 @test6g(i8*) 119 120 !llvm.dbg.cu = !{!3} 121 !llvm.module.flags = !{!8, !9} 122 123 !0 = !{!1, !1, i64 0} 124 !1 = !{!"foo"} 125 !2 = !{i8 0, i8 2} 126 !3 = distinct !DICompileUnit(language: DW_LANG_C99, file: !7, producer: "clang", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !4) 127 !4 = !{} 128 !5 = !DILocation(line: 23, scope: !6) 129 !6 = distinct !DISubprogram(name: "foo", scope: !3, file: !7, line: 1, type: !DISubroutineType(types: !4), isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, unit: !3, variables: !4) 130 !7 = !DIFile(filename: "foo.c", directory: "/") 131 !8 = !{i32 2, !"Dwarf Version", i32 2} 132 !9 = !{i32 2, !"Debug Info Version", i32 3} 133