Skip to content

Commit 95f7f7c

Browse files
committed
Revert "[SimplifyCFG] use profile metadata to refine merging branch conditions"
This reverts commit 27ae17a. There are bot failures that end with: rust-lang#4 0x00007fff7ae3c9b8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0 rust-lang#5 0x00007fff84e504d8 (linux-vdso64.so.1+0x4d8) rust-lang#6 0x00007fff7c419a5c llvm::TargetTransformInfo::getPredictableBranchThreshold() const (/home/buildbots/ppc64le-clang-multistage-test/clang-ppc64le-multistage/stage1.install/bin/../lib/libLLVMAnalysis.so.13git+0x479a5c) ...but not sure how to trigger that yet.
1 parent bca0cf7 commit 95f7f7c

File tree

3 files changed

+44
-89
lines changed

3 files changed

+44
-89
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

+18-41
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
#include "llvm/IR/User.h"
6464
#include "llvm/IR/Value.h"
6565
#include "llvm/IR/ValueHandle.h"
66-
#include "llvm/Support/BranchProbability.h"
6766
#include "llvm/Support/Casting.h"
6867
#include "llvm/Support/CommandLine.h"
6968
#include "llvm/Support/Debug.h"
@@ -2841,60 +2840,38 @@ static bool extractPredSuccWeights(BranchInst *PBI, BranchInst *BI,
28412840
}
28422841
}
28432842

2844-
/// Determine if the two branches share a common destination and deduce a glue
2845-
/// that joins branch's conditions to arrive at the common destination if that
2846-
/// would be profitable.
2843+
// Determine if the two branches share a common destination,
2844+
// and deduce a glue that we need to use to join branch's conditions
2845+
// to arrive at the common destination.
28472846
static Optional<std::pair<Instruction::BinaryOps, bool>>
2848-
shouldFoldCondBranchesToCommonDestination(BranchInst *BI, BranchInst *PBI,
2849-
const TargetTransformInfo *TTI) {
2847+
CheckIfCondBranchesShareCommonDestination(BranchInst *BI, BranchInst *PBI) {
28502848
assert(BI && PBI && BI->isConditional() && PBI->isConditional() &&
28512849
"Both blocks must end with a conditional branches.");
28522850
assert(is_contained(predecessors(BI->getParent()), PBI->getParent()) &&
28532851
"PredBB must be a predecessor of BB.");
28542852

2855-
// We have the potential to fold the conditions together, but if the
2856-
// predecessor branch is predictable, we may not want to merge them.
2857-
uint64_t PTWeight, PFWeight;
2858-
BranchProbability PBITrueProb, Likely;
2859-
if (PBI->extractProfMetadata(PTWeight, PFWeight) &&
2860-
(PTWeight + PFWeight) != 0) {
2861-
PBITrueProb =
2862-
BranchProbability::getBranchProbability(PTWeight, PTWeight + PFWeight);
2863-
Likely = TTI->getPredictableBranchThreshold();
2864-
}
2865-
2866-
if (PBI->getSuccessor(0) == BI->getSuccessor(0)) {
2867-
// Speculate the 2nd condition unless the 1st is probably true.
2868-
if (PBITrueProb.isUnknown() || PBITrueProb < Likely)
2869-
return {{Instruction::Or, false}};
2870-
} else if (PBI->getSuccessor(1) == BI->getSuccessor(1)) {
2871-
// Speculate the 2nd condition unless the 1st is probably false.
2872-
if (PBITrueProb.isUnknown() || PBITrueProb.getCompl() < Likely)
2873-
return {{Instruction::And, false}};
2874-
} else if (PBI->getSuccessor(0) == BI->getSuccessor(1)) {
2875-
// Speculate the 2nd condition unless the 1st is probably true.
2876-
if (PBITrueProb.isUnknown() || PBITrueProb < Likely)
2877-
return {{Instruction::And, true}};
2878-
} else if (PBI->getSuccessor(1) == BI->getSuccessor(0)) {
2879-
// Speculate the 2nd condition unless the 1st is probably false.
2880-
if (PBITrueProb.isUnknown() || PBITrueProb.getCompl() < Likely)
2881-
return {{Instruction::Or, true}};
2882-
}
2853+
if (PBI->getSuccessor(0) == BI->getSuccessor(0))
2854+
return {{Instruction::Or, false}};
2855+
else if (PBI->getSuccessor(1) == BI->getSuccessor(1))
2856+
return {{Instruction::And, false}};
2857+
else if (PBI->getSuccessor(0) == BI->getSuccessor(1))
2858+
return {{Instruction::And, true}};
2859+
else if (PBI->getSuccessor(1) == BI->getSuccessor(0))
2860+
return {{Instruction::Or, true}};
28832861
return None;
28842862
}
28852863

2886-
static bool performBranchToCommonDestFolding(BranchInst *BI, BranchInst *PBI,
2864+
static bool PerformBranchToCommonDestFolding(BranchInst *BI, BranchInst *PBI,
28872865
DomTreeUpdater *DTU,
2888-
MemorySSAUpdater *MSSAU,
2889-
const TargetTransformInfo *TTI) {
2866+
MemorySSAUpdater *MSSAU) {
28902867
BasicBlock *BB = BI->getParent();
28912868
BasicBlock *PredBlock = PBI->getParent();
28922869

28932870
// Determine if the two branches share a common destination.
28942871
Instruction::BinaryOps Opc;
28952872
bool InvertPredCond;
28962873
std::tie(Opc, InvertPredCond) =
2897-
*shouldFoldCondBranchesToCommonDestination(BI, PBI, TTI);
2874+
*CheckIfCondBranchesShareCommonDestination(BI, PBI);
28982875

28992876
LLVM_DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
29002877

@@ -3082,8 +3059,8 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
30823059
// Determine if the two branches share a common destination.
30833060
Instruction::BinaryOps Opc;
30843061
bool InvertPredCond;
3085-
if (auto Recipe = shouldFoldCondBranchesToCommonDestination(BI, PBI, TTI))
3086-
std::tie(Opc, InvertPredCond) = *Recipe;
3062+
if (auto Recepie = CheckIfCondBranchesShareCommonDestination(BI, PBI))
3063+
std::tie(Opc, InvertPredCond) = *Recepie;
30873064
else
30883065
continue;
30893066

@@ -3100,7 +3077,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
31003077
continue;
31013078
}
31023079

3103-
return performBranchToCommonDestFolding(BI, PBI, DTU, MSSAU, TTI);
3080+
return PerformBranchToCommonDestFolding(BI, PBI, DTU, MSSAU);
31043081
}
31053082
return Changed;
31063083
}

llvm/test/Transforms/PGOProfile/chr.ll

+7-7
Original file line numberDiff line numberDiff line change
@@ -1277,12 +1277,11 @@ define i32 @test_chr_14(i32* %i, i32* %j, i32 %sum0, i1 %pred, i32 %z) !prof !14
12771277
; CHECK-LABEL: @test_chr_14(
12781278
; CHECK-NEXT: entry:
12791279
; CHECK-NEXT: [[I0:%.*]] = load i32, i32* [[I:%.*]], align 4
1280-
; CHECK-NEXT: [[V1:%.*]] = icmp eq i32 [[Z:%.*]], 1
1281-
; CHECK-NEXT: br i1 [[V1]], label [[BB1:%.*]], label [[ENTRY_SPLIT_NONCHR:%.*]], !prof !15
1282-
; CHECK: entry.split.nonchr:
1280+
; CHECK-NEXT: [[V1:%.*]] = icmp ne i32 [[Z:%.*]], 1
12831281
; CHECK-NEXT: [[V0:%.*]] = icmp eq i32 [[Z]], 0
12841282
; CHECK-NEXT: [[V3_NONCHR:%.*]] = and i1 [[V0]], [[PRED:%.*]]
1285-
; CHECK-NEXT: br i1 [[V3_NONCHR]], label [[BB0_NONCHR:%.*]], label [[BB1]], !prof !16
1283+
; CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[V1]], [[V3_NONCHR]]
1284+
; CHECK-NEXT: br i1 [[OR_COND]], label [[BB0_NONCHR:%.*]], label [[BB1:%.*]], !prof !19
12861285
; CHECK: bb0.nonchr:
12871286
; CHECK-NEXT: call void @foo()
12881287
; CHECK-NEXT: br label [[BB1]]
@@ -1913,7 +1912,7 @@ define i32 @test_chr_21(i64 %i, i64 %k, i64 %j) !prof !14 {
19131912
; CHECK-NEXT: switch i64 [[I]], label [[BB2:%.*]] [
19141913
; CHECK-NEXT: i64 2, label [[BB3_NONCHR2:%.*]]
19151914
; CHECK-NEXT: i64 86, label [[BB2_NONCHR1:%.*]]
1916-
; CHECK-NEXT: ], !prof !19
1915+
; CHECK-NEXT: ], !prof !20
19171916
; CHECK: bb2:
19181917
; CHECK-NEXT: call void @foo()
19191918
; CHECK-NEXT: call void @foo()
@@ -2490,14 +2489,14 @@ define void @test_chr_24(i32* %i) !prof !14 {
24902489
; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* [[I:%.*]], align 4
24912490
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[TMP0]], 1
24922491
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 0
2493-
; CHECK-NEXT: br i1 [[TMP2]], label [[BB1:%.*]], label [[BB0:%.*]], !prof !20
2492+
; CHECK-NEXT: br i1 [[TMP2]], label [[BB1:%.*]], label [[BB0:%.*]], !prof !21
24942493
; CHECK: bb0:
24952494
; CHECK-NEXT: call void @foo()
24962495
; CHECK-NEXT: br label [[BB1]]
24972496
; CHECK: bb1:
24982497
; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP0]], 2
24992498
; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i32 [[TMP3]], 0
2500-
; CHECK-NEXT: br i1 [[TMP4]], label [[BB3:%.*]], label [[BB2:%.*]], !prof !20
2499+
; CHECK-NEXT: br i1 [[TMP4]], label [[BB3:%.*]], label [[BB2:%.*]], !prof !21
25012500
; CHECK: bb2:
25022501
; CHECK-NEXT: call void @foo()
25032502
; CHECK-NEXT: br label [[BB3]]
@@ -2551,3 +2550,4 @@ bb3:
25512550
; CHECK: !16 = !{!"branch_weights", i32 0, i32 1}
25522551
; CHECK: !17 = !{!"branch_weights", i32 1, i32 1}
25532552
; CHECK: !18 = !{!"branch_weights", i32 1, i32 0}
2553+
; CHECK: !19 = !{!"branch_weights", i32 0, i32 1000}

llvm/test/Transforms/SimplifyCFG/preserve-branchweights.ll

+19-41
Original file line numberDiff line numberDiff line change
@@ -636,17 +636,16 @@ exit:
636636
ret i32 %outval
637637
}
638638

639-
; Merging the icmps with logic-op defeats the purpose of the metadata.
639+
; FIXME: Merging the icmps with logic-op defeats the purpose of the metadata.
640640
; We can't tell which condition is expensive if they are combined.
641641

642642
define void @or_icmps_harmful(i32 %x, i32 %y, i8* %p) {
643643
; CHECK-LABEL: @or_icmps_harmful(
644644
; CHECK-NEXT: entry:
645645
; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sgt i32 [[X:%.*]], -1
646-
; CHECK-NEXT: br i1 [[EXPECTED_TRUE]], label [[EXIT:%.*]], label [[RARE:%.*]], !prof !19
647-
; CHECK: rare:
648646
; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
649-
; CHECK-NEXT: br i1 [[EXPENSIVE]], label [[EXIT]], label [[FALSE:%.*]]
647+
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[EXPECTED_TRUE]], [[EXPENSIVE]]
648+
; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof !19
650649
; CHECK: false:
651650
; CHECK-NEXT: store i8 42, i8* [[P:%.*]], align 1
652651
; CHECK-NEXT: br label [[EXIT]]
@@ -669,17 +668,16 @@ exit:
669668
ret void
670669
}
671670

672-
; Merging the icmps with logic-op defeats the purpose of the metadata.
671+
; FIXME: Merging the icmps with logic-op defeats the purpose of the metadata.
673672
; We can't tell which condition is expensive if they are combined.
674673

675674
define void @or_icmps_harmful_inverted(i32 %x, i32 %y, i8* %p) {
676675
; CHECK-LABEL: @or_icmps_harmful_inverted(
677676
; CHECK-NEXT: entry:
678-
; CHECK-NEXT: [[EXPECTED_FALSE:%.*]] = icmp sgt i32 [[X:%.*]], -1
679-
; CHECK-NEXT: br i1 [[EXPECTED_FALSE]], label [[RARE:%.*]], label [[EXIT:%.*]], !prof !20
680-
; CHECK: rare:
677+
; CHECK-NEXT: [[EXPECTED_FALSE:%.*]] = icmp sle i32 [[X:%.*]], -1
681678
; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
682-
; CHECK-NEXT: br i1 [[EXPENSIVE]], label [[EXIT]], label [[FALSE:%.*]]
679+
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[EXPECTED_FALSE]], [[EXPENSIVE]]
680+
; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof !19
683681
; CHECK: false:
684682
; CHECK-NEXT: store i8 42, i8* [[P:%.*]], align 1
685683
; CHECK-NEXT: br label [[EXIT]]
@@ -702,16 +700,15 @@ exit:
702700
ret void
703701
}
704702

705-
; The probability threshold is determined by a TTI setting.
706-
; In this example, we are just short of strongly expected, so speculate.
703+
; The probability threshold is set by a builtin_expect setting.
707704

708705
define void @or_icmps_not_that_harmful(i32 %x, i32 %y, i8* %p) {
709706
; CHECK-LABEL: @or_icmps_not_that_harmful(
710707
; CHECK-NEXT: entry:
711708
; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sgt i32 [[X:%.*]], -1
712709
; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
713710
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[EXPECTED_TRUE]], [[EXPENSIVE]]
714-
; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof !21
711+
; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof !20
715712
; CHECK: false:
716713
; CHECK-NEXT: store i8 42, i8* [[P:%.*]], align 1
717714
; CHECK-NEXT: br label [[EXIT]]
@@ -734,16 +731,13 @@ exit:
734731
ret void
735732
}
736733

737-
; The probability threshold is determined by a TTI setting.
738-
; In this example, we are just short of strongly expected, so speculate.
739-
740734
define void @or_icmps_not_that_harmful_inverted(i32 %x, i32 %y, i8* %p) {
741735
; CHECK-LABEL: @or_icmps_not_that_harmful_inverted(
742736
; CHECK-NEXT: entry:
743737
; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sgt i32 [[X:%.*]], -1
744738
; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
745739
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[EXPECTED_TRUE]], [[EXPENSIVE]]
746-
; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof !22
740+
; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof !21
747741
; CHECK: false:
748742
; CHECK-NEXT: store i8 42, i8* [[P:%.*]], align 1
749743
; CHECK-NEXT: br label [[EXIT]]
@@ -766,15 +760,13 @@ exit:
766760
ret void
767761
}
768762

769-
; The 1st cmp is probably true, so speculating the 2nd is probably a win.
770-
771763
define void @or_icmps_useful(i32 %x, i32 %y, i8* %p) {
772764
; CHECK-LABEL: @or_icmps_useful(
773765
; CHECK-NEXT: entry:
774766
; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sle i32 [[X:%.*]], -1
775767
; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
776768
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[EXPECTED_TRUE]], [[EXPENSIVE]]
777-
; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof !23
769+
; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof !22
778770
; CHECK: false:
779771
; CHECK-NEXT: store i8 42, i8* [[P:%.*]], align 1
780772
; CHECK-NEXT: br label [[EXIT]]
@@ -797,15 +789,13 @@ exit:
797789
ret void
798790
}
799791

800-
; The 1st cmp is probably false, so speculating the 2nd is probably a win.
801-
802792
define void @or_icmps_useful_inverted(i32 %x, i32 %y, i8* %p) {
803793
; CHECK-LABEL: @or_icmps_useful_inverted(
804794
; CHECK-NEXT: entry:
805795
; CHECK-NEXT: [[EXPECTED_FALSE:%.*]] = icmp sgt i32 [[X:%.*]], -1
806796
; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
807797
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[EXPECTED_FALSE]], [[EXPENSIVE]]
808-
; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof !23
798+
; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof !22
809799
; CHECK: false:
810800
; CHECK-NEXT: store i8 42, i8* [[P:%.*]], align 1
811801
; CHECK-NEXT: br label [[EXIT]]
@@ -859,17 +849,16 @@ exit:
859849
ret void
860850
}
861851

862-
; Merging the icmps with logic-op defeats the purpose of the metadata.
852+
; FIXME: Merging the icmps with logic-op defeats the purpose of the metadata.
863853
; We can't tell which condition is expensive if they are combined.
864854

865855
define void @and_icmps_harmful(i32 %x, i32 %y, i8* %p) {
866856
; CHECK-LABEL: @and_icmps_harmful(
867857
; CHECK-NEXT: entry:
868858
; CHECK-NEXT: [[EXPECTED_FALSE:%.*]] = icmp sgt i32 [[X:%.*]], -1
869-
; CHECK-NEXT: br i1 [[EXPECTED_FALSE]], label [[RARE:%.*]], label [[EXIT:%.*]], !prof !20
870-
; CHECK: rare:
871859
; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
872-
; CHECK-NEXT: br i1 [[EXPENSIVE]], label [[FALSE:%.*]], label [[EXIT]]
860+
; CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[EXPECTED_FALSE]], [[EXPENSIVE]]
861+
; CHECK-NEXT: br i1 [[OR_COND]], label [[FALSE:%.*]], label [[EXIT:%.*]], !prof !23
873862
; CHECK: false:
874863
; CHECK-NEXT: store i8 42, i8* [[P:%.*]], align 1
875864
; CHECK-NEXT: br label [[EXIT]]
@@ -892,17 +881,16 @@ exit:
892881
ret void
893882
}
894883

895-
; Merging the icmps with logic-op defeats the purpose of the metadata.
884+
; FIXME: Merging the icmps with logic-op defeats the purpose of the metadata.
896885
; We can't tell which condition is expensive if they are combined.
897886

898887
define void @and_icmps_harmful_inverted(i32 %x, i32 %y, i8* %p) {
899888
; CHECK-LABEL: @and_icmps_harmful_inverted(
900889
; CHECK-NEXT: entry:
901-
; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sgt i32 [[X:%.*]], -1
902-
; CHECK-NEXT: br i1 [[EXPECTED_TRUE]], label [[EXIT:%.*]], label [[RARE:%.*]], !prof !19
903-
; CHECK: rare:
890+
; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sle i32 [[X:%.*]], -1
904891
; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
905-
; CHECK-NEXT: br i1 [[EXPENSIVE]], label [[FALSE:%.*]], label [[EXIT]]
892+
; CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[EXPECTED_TRUE]], [[EXPENSIVE]]
893+
; CHECK-NEXT: br i1 [[OR_COND]], label [[FALSE:%.*]], label [[EXIT:%.*]], !prof !23
906894
; CHECK: false:
907895
; CHECK-NEXT: store i8 42, i8* [[P:%.*]], align 1
908896
; CHECK-NEXT: br label [[EXIT]]
@@ -925,9 +913,6 @@ exit:
925913
ret void
926914
}
927915

928-
; The probability threshold is determined by a TTI setting.
929-
; In this example, we are just short of strongly expected, so speculate.
930-
931916
define void @and_icmps_not_that_harmful(i32 %x, i32 %y, i8* %p) {
932917
; CHECK-LABEL: @and_icmps_not_that_harmful(
933918
; CHECK-NEXT: entry:
@@ -957,9 +942,6 @@ exit:
957942
ret void
958943
}
959944

960-
; The probability threshold is determined by a TTI setting.
961-
; In this example, we are just short of strongly expected, so speculate.
962-
963945
define void @and_icmps_not_that_harmful_inverted(i32 %x, i32 %y, i8* %p) {
964946
; CHECK-LABEL: @and_icmps_not_that_harmful_inverted(
965947
; CHECK-NEXT: entry:
@@ -989,8 +971,6 @@ exit:
989971
ret void
990972
}
991973

992-
; The 1st cmp is probably true, so speculating the 2nd is probably a win.
993-
994974
define void @and_icmps_useful(i32 %x, i32 %y, i8* %p) {
995975
; CHECK-LABEL: @and_icmps_useful(
996976
; CHECK-NEXT: entry:
@@ -1020,8 +1000,6 @@ exit:
10201000
ret void
10211001
}
10221002

1023-
; The 1st cmp is probably false, so speculating the 2nd is probably a win.
1024-
10251003
define void @and_icmps_useful_inverted(i32 %x, i32 %y, i8* %p) {
10261004
; CHECK-LABEL: @and_icmps_useful_inverted(
10271005
; CHECK-NEXT: entry:

0 commit comments

Comments
 (0)