xref: /llvm-project/llvm/test/Transforms/SimplifyCFG/merge-direct-call-branch-weights-in-hoist.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<no-sink-common-insts;hoist-common-insts>' -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s --check-prefix=HOIST
3
4; Test case based on C++ code with manualy annotated !prof metadata.
5; This is to test that when calls to 'func1' from 'if.then' block
6; and 'if.else' block are hoisted, the branch_weights are merged and
7; attached to merged call rather than dropped.
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;        sum += func1(a, b);
18;        sum -= func2(a, b);
19;    }
20;    return sum;
21; }
22define i32 @_Z4funciib(i32 %a, i32 %b, i1 %c) {
23; HOIST-LABEL: define i32 @_Z4funciib
24; HOIST-SAME: (i32 [[A:%.*]], i32 [[B:%.*]], i1 [[C:%.*]]) {
25; HOIST-NEXT:  entry:
26; HOIST-NEXT:    [[CALL:%.*]] = tail call i32 @_Z5func1ii(i32 [[A]], i32 [[B]]), !prof [[PROF0:![0-9]+]]
27; HOIST-NEXT:    br i1 [[C]], label [[IF_END:%.*]], label [[IF_ELSE:%.*]]
28; HOIST:       if.else:
29; HOIST-NEXT:    [[CALL3:%.*]] = tail call i32 @_Z5func2ii(i32 [[A]], i32 [[B]])
30; HOIST-NEXT:    [[SUB:%.*]] = sub i32 [[CALL]], [[CALL3]]
31; HOIST-NEXT:    br label [[IF_END]]
32; HOIST:       if.end:
33; HOIST-NEXT:    [[SUM_0:%.*]] = phi i32 [ [[SUB]], [[IF_ELSE]] ], [ [[CALL]], [[ENTRY:%.*]] ]
34; HOIST-NEXT:    ret i32 [[SUM_0]]
35;
36entry:
37  br i1 %c, label %if.then, label %if.else
38
39if.then:                                          ; preds = %entry
40  %call = tail call i32 @_Z5func1ii(i32 %a, i32 %b), !prof !0
41  br label %if.end
42
43if.else:                                          ; preds = %entry
44  %call1 = tail call i32 @_Z5func1ii(i32 %a, i32 %b), !prof !1
45  %call3 = tail call i32 @_Z5func2ii(i32 %a, i32 %b)
46  %sub = sub i32 %call1, %call3
47  br label %if.end
48
49if.end:                                           ; preds = %if.else, %if.then
50  %sum.0 = phi i32 [ %call, %if.then ], [ %sub, %if.else ]
51  ret i32 %sum.0
52}
53
54declare i32 @_Z5func1ii(i32, i32)
55
56declare i32 @_Z5func2ii(i32, i32)
57
58!0 = !{!"branch_weights", i32 10}
59!1 = !{!"branch_weights", i32 90}
60;.
61; HOIST: [[PROF0]] = !{!"branch_weights", i64 100}
62;.
63