[llvm] b93ff3e - [SimplifyCFG] Fix uint32_t overflow in cbranch to cbranch merge prevention check. (#72329)

via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 15 03:08:02 PST 2023


Author: Valery Pykhtin
Date: 2023-11-15T12:07:58+01:00
New Revision: b93ff3e5ce1ce0273c390ae5a3e829e89ee8292b

URL: https://github.com/llvm/llvm-project/commit/b93ff3e5ce1ce0273c390ae5a3e829e89ee8292b
DIFF: https://github.com/llvm/llvm-project/commit/b93ff3e5ce1ce0273c390ae5a3e829e89ee8292b.diff

LOG: [SimplifyCFG] Fix uint32_t overflow in cbranch to cbranch merge prevention check. (#72329)

This fixes https://github.com/llvm/llvm-project/issues/72323.

Resulted from
https://github.com/llvm/llvm-project/commit/f054947c0da99ec8b3c4bb043e5225672420a313

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 6009558efca06af..299a143f30fe211 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4351,10 +4351,11 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
   SmallVector<uint32_t, 2> PredWeights;
   if (!PBI->getMetadata(LLVMContext::MD_unpredictable) &&
       extractBranchWeights(*PBI, PredWeights) &&
-      (PredWeights[0] + PredWeights[1]) != 0) {
+      (static_cast<uint64_t>(PredWeights[0]) + PredWeights[1]) != 0) {
 
     BranchProbability CommonDestProb = BranchProbability::getBranchProbability(
-        PredWeights[PBIOp], PredWeights[0] + PredWeights[1]);
+        PredWeights[PBIOp],
+        static_cast<uint64_t>(PredWeights[0]) + PredWeights[1]);
 
     BranchProbability Likely = TTI.getPredictableBranchThreshold();
     if (CommonDestProb >= Likely)

diff  --git a/llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll b/llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll
index 5c21f163826ee25..0f540007b242e29 100644
--- a/llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll
+++ b/llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll
@@ -79,6 +79,26 @@ exit:
   ret void
 }
 
+define void @uint32_overflow_test(i1 %arg, i1 %arg1) {
+; CHECK-LABEL: @uint32_overflow_test(
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:    [[BRMERGE:%.*]] = select i1 [[ARG:%.*]], i1 true, i1 [[ARG1:%.*]]
+; CHECK-NEXT:    ret void
+;
+bb:
+  br i1 %arg, label %bb4, label %bb2, !prof !3
+
+bb2:
+  br i1 %arg1, label %bb4, label %bb3
+
+bb3:
+  br label %bb4
+
+bb4:
+  ret void
+}
+
 !0 = !{!"branch_weights", i32 1, i32 1000}
 !1 = !{!"branch_weights", i32 1000, i32 1}
 !2 = !{!"branch_weights", i32 3, i32 2}
+!3 = !{!"branch_weights", i32 -258677585, i32 -1212131848}


        


More information about the llvm-commits mailing list