Skip to content

Commit 56e56e9

Browse files
committed
[SimplifyCFG] Preserve !annotation in FoldBranchToCommonDest.
When folding a branch to a common destination, preserve !annotation on the created instruction, if the terminator of the BB that is going to be removed has !annotation. This should ensure that !annotation is attached to the instructions that 'replace' the original terminator. Reviewed By: jdoerfert, lebedev.ri Differential Revision: https://reviews.llvm.org/D93410
1 parent 54eb1a2 commit 56e56e9

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,11 +2761,15 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, MemorySSAUpdater *MSSAU,
27612761
Changed = true;
27622762

27632763
IRBuilder<> Builder(PBI);
2764+
// The builder is used to create instructions to eliminate the branch in BB.
2765+
// If BB's terminator has !annotation metadata, add it to the new
2766+
// instructions.
2767+
Builder.CollectMetadataToCopy(BB->getTerminator(),
2768+
{LLVMContext::MD_annotation});
27642769

27652770
// If we need to invert the condition in the pred block to match, do so now.
27662771
if (InvertPredCond) {
27672772
Value *NewCond = PBI->getCondition();
2768-
27692773
if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {
27702774
CmpInst *CI = cast<CmpInst>(NewCond);
27712775
CI->setPredicate(CI->getInversePredicate());
@@ -2803,8 +2807,9 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, MemorySSAUpdater *MSSAU,
28032807
// its potential value. The previous information might have been valid
28042808
// only given the branch precondition.
28052809
// For an analogous reason, we must also drop all the metadata whose
2806-
// semantics we don't understand.
2807-
NewBonusInst->dropUnknownNonDebugMetadata();
2810+
// semantics we don't understand. We *can* preserve !annotation, because
2811+
// it is tied to the instruction itself, not the value or position.
2812+
NewBonusInst->dropUnknownNonDebugMetadata(LLVMContext::MD_annotation);
28082813

28092814
PredBlock->getInstList().insert(PBI->getIterator(), NewBonusInst);
28102815
NewBonusInst->takeName(&*BonusInst);

llvm/test/Transforms/SimplifyCFG/annotations.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ define i32 @test_preserve_and(i8* %a, i8* %b, i8* %c, i8* %d) {
66
; CHECK-LABEL: define {{.*}} @test_preserve_and({{.*}}
77
; CHECK-NEXT: entry:
88
; CHECK-NEXT: [[C_1:%.*]] = icmp ult i8* [[A:%.*]], [[B:%.*]], !annotation !0
9-
; CHECK-NEXT: [[C_2:%.*]] = icmp uge i8* [[C:%.*]], [[D:%.*]]
10-
; CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[C_1]], [[C_2]]
9+
; CHECK-NEXT: [[C_2:%.*]] = icmp uge i8* [[C:%.*]], [[D:%.*]], !annotation !0
10+
; CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[C_1]], [[C_2]], !annotation !0
1111
; CHECK-NEXT: br i1 [[OR_COND]], label [[CONT1:%.*]], label [[TRAP:%.*]], !annotation !0
1212
; CHECK: trap: ; preds = %entry
1313
; CHECK-NEXT: call void @fn1()
@@ -39,8 +39,8 @@ define i32 @test_preserve_or(i8* %a, i8* %b, i8* %c, i8* %d) {
3939
; CHECK-LABEL: define {{.*}} @test_preserve_or({{.*}}
4040
; CHECK-NEXT: entry:
4141
; CHECK-NEXT: [[C_1:%.*]] = icmp uge i8* [[A:%.*]], [[B:%.*]], !annotation !0
42-
; CHECK-NEXT: [[C_2:%.*]] = icmp uge i8* [[C:%.*]], [[D:%.*]]
43-
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[C_1]], [[C_2]]
42+
; CHECK-NEXT: [[C_2:%.*]] = icmp uge i8* [[C:%.*]], [[D:%.*]], !annotation !0
43+
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[C_1]], [[C_2]], !annotation !0
4444
; CHECK-NEXT: br i1 [[OR_COND]], label [[TRAP:%.*]], label [[CONT1:%.*]], !annotation !0
4545
; CHECK: trap: ; preds = %entry
4646
; CHECK-NEXT: call void @fn1()
@@ -73,9 +73,9 @@ define i32 @test_preserve_or_not(i8* %a, i8* %b, i8* %c, i8* %d) {
7373
; CHECK-NEXT: entry:
7474
; CHECK-NEXT: [[C_1:%.*]] = icmp ult i8* [[A:%.*]], [[B:%.*]], !annotation !0
7575
; CHECK-NEXT: [[C_2:%.*]] = xor i1 [[C_1]], true
76-
; CHECK-NEXT: [[C_2_NOT:%.*]] = xor i1 [[C_2]], true
77-
; CHECK-NEXT: [[C_3:%.*]] = icmp uge i8* [[C:%.*]], [[D:%.*]]
78-
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[C_2_NOT]], [[C_3]]
76+
; CHECK-NEXT: [[C_2_NOT:%.*]] = xor i1 [[C_2]], true, !annotation !0
77+
; CHECK-NEXT: [[C_3:%.*]] = icmp uge i8* [[C:%.*]], [[D:%.*]], !annotation !0
78+
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[C_2_NOT]], [[C_3]], !annotation !0
7979
; CHECK-NEXT: br i1 [[OR_COND]], label [[TRAP:%.*]], label [[CONT1:%.*]], !annotation !0
8080
; CHECK: trap: ; preds = %entry
8181
; CHECK-NEXT: call void @fn1()
@@ -111,7 +111,7 @@ define i32 @test_or_not_no_annotation(i8* %a, i8* %b, i8* %c, i8* %d) {
111111
; CHECK-NEXT: [[C_1:%.*]] = icmp ult i8* [[A:%.*]], [[B:%.*]], !annotation !0
112112
; CHECK-NEXT: [[C_2:%.*]] = xor i1 [[C_1]], true
113113
; CHECK-NEXT: [[C_2_NOT:%.*]] = xor i1 [[C_2]], true
114-
; CHECK-NEXT: [[C_3:%.*]] = icmp uge i8* [[C:%.*]], [[D:%.*]]
114+
; CHECK-NEXT: [[C_3:%.*]] = icmp uge i8* [[C:%.*]], [[D:%.*]], !annotation !0
115115
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[C_2_NOT]], [[C_3]]
116116
; CHECK-NEXT: br i1 [[OR_COND]], label [[TRAP:%.*]], label [[CONT1:%.*]], !annotation !0
117117
; CHECK: trap: ; preds = %entry

0 commit comments

Comments
 (0)