xref: /llvm-project/llvm/test/Transforms/SimplifyCFG/merge-direct-call-branch-weights-in-sink.ll (revision b3cb950cf3d162459a31a9ab533af0f403f344eb)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals --version 2
2; RUN: opt < %s -passes='simplifycfg<sink-common-insts;no-hoist-common-insts>' -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s --check-prefix=SINK
3
4
5; Test case based on the following C++ code with manualy annotated !prof metadata.
6; This is to test that when calls to 'func1' from 'if.then' and 'if.else' are
7; sinked, the branch weights are merged and attached to sinked call.
8;
9; int func1(int a, int b) ;
10; int func2(int a, int b) ;
11
12; int func(int a, int b, bool c) {
13;    int sum = 0;
14;    if (c) {
15;        sum += func1(a,b);
16;    } else {
17;        b -= func2(a,b);
18;        sum += func1(a,b);
19;    }
20;    return sum;
21; }
22
23define i32 @_Z4funciib(i32 %a, i32 %b, i1 %c) {
24; SINK-LABEL: define i32 @_Z4funciib
25; SINK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]], i1 [[C:%.*]]) {
26; SINK-NEXT:  entry:
27; SINK-NEXT:    br i1 [[C]], label [[IF_END:%.*]], label [[IF_ELSE:%.*]]
28; SINK:       if.else:
29; SINK-NEXT:    [[CALL1:%.*]] = tail call i32 @_Z5func2ii(i32 [[A]], i32 [[B]])
30; SINK-NEXT:    [[SUB:%.*]] = sub i32 [[B]], [[CALL1]]
31; SINK-NEXT:    br label [[IF_END]]
32; SINK:       if.end:
33; SINK-NEXT:    [[SUB_SINK:%.*]] = phi i32 [ [[SUB]], [[IF_ELSE]] ], [ [[B]], [[ENTRY:%.*]] ]
34; SINK-NEXT:    [[CALL2:%.*]] = tail call i32 @_Z5func1ii(i32 [[A]], i32 [[SUB_SINK]]), !prof [[PROF0:![0-9]+]]
35; SINK-NEXT:    ret i32 [[CALL2]]
36;
37entry:
38  br i1 %c, label %if.then, label %if.else
39
40if.then:                                          ; preds = %entry
41  %call = tail call i32 @_Z5func1ii(i32 %a, i32 %b), !prof !0
42  br label %if.end
43
44if.else:                                          ; preds = %entry
45  %call1 = tail call i32 @_Z5func2ii(i32 %a, i32 %b)
46  %sub = sub i32 %b, %call1
47  %call2 = tail call i32 @_Z5func1ii(i32 %a, i32 %sub), !prof !1
48  br label %if.end
49
50if.end:                                           ; preds = %if.else, %if.then
51  %sum.0 = phi i32 [ %call, %if.then ], [ %call2, %if.else ]
52  ret i32 %sum.0
53}
54
55declare i32 @_Z5func1ii(i32, i32)
56
57declare i32 @_Z5func2ii(i32, i32)
58
59!0 = !{!"branch_weights", i32 10}
60!1 = !{!"branch_weights", i32 90}
61;.
62; SINK: [[PROF0]] = !{!"branch_weights", i64 100}
63;.
64