|
72 | 72 | #include "llvm/IR/Module.h" |
73 | 73 | #include "llvm/IR/PassManager.h" |
74 | 74 | #include "llvm/IR/PatternMatch.h" |
| 75 | +#include "llvm/IR/ProfDataUtils.h" |
75 | 76 | #include "llvm/IR/Type.h" |
76 | 77 | #include "llvm/IR/User.h" |
77 | 78 | #include "llvm/IR/Value.h" |
@@ -105,6 +106,7 @@ STATISTIC( |
105 | 106 | STATISTIC(NumShiftUntilZero, |
106 | 107 | "Number of uncountable loops recognized as 'shift until zero' idiom"); |
107 | 108 |
|
| 109 | +namespace llvm { |
108 | 110 | bool DisableLIRP::All; |
109 | 111 | static cl::opt<bool, true> |
110 | 112 | DisableLIRPAll("disable-" DEBUG_TYPE "-all", |
@@ -163,6 +165,10 @@ static cl::opt<bool> ForceMemsetPatternIntrinsic( |
163 | 165 | cl::desc("Use memset.pattern intrinsic whenever possible"), cl::init(false), |
164 | 166 | cl::Hidden); |
165 | 167 |
|
| 168 | +extern cl::opt<bool> ProfcheckDisableMetadataFixes; |
| 169 | + |
| 170 | +} // namespace llvm |
| 171 | + |
166 | 172 | namespace { |
167 | 173 |
|
168 | 174 | class LoopIdiomRecognize { |
@@ -3199,7 +3205,21 @@ bool LoopIdiomRecognize::recognizeShiftUntilBitTest() { |
3199 | 3205 | // The loop trip count check. |
3200 | 3206 | auto *IVCheck = Builder.CreateICmpEQ(IVNext, LoopTripCount, |
3201 | 3207 | CurLoop->getName() + ".ivcheck"); |
3202 | | - Builder.CreateCondBr(IVCheck, SuccessorBB, LoopHeaderBB); |
| 3208 | + SmallVector<uint32_t> BranchWeights; |
| 3209 | + const bool HasBranchWeights = |
| 3210 | + !ProfcheckDisableMetadataFixes && |
| 3211 | + extractBranchWeights(*LoopHeaderBB->getTerminator(), BranchWeights); |
| 3212 | + |
| 3213 | + auto *BI = Builder.CreateCondBr(IVCheck, SuccessorBB, LoopHeaderBB); |
| 3214 | + if (HasBranchWeights) { |
| 3215 | + if (SuccessorBB == LoopHeaderBB->getTerminator()->getSuccessor(1)) |
| 3216 | + std::swap(BranchWeights[0], BranchWeights[1]); |
| 3217 | + // We're not changing the loop profile, so we can reuse the original loop's |
| 3218 | + // profile. |
| 3219 | + setBranchWeights(*BI, BranchWeights, |
| 3220 | + /*IsExpected=*/false); |
| 3221 | + } |
| 3222 | + |
3203 | 3223 | LoopHeaderBB->getTerminator()->eraseFromParent(); |
3204 | 3224 |
|
3205 | 3225 | // Populate the IV PHI. |
@@ -3368,10 +3388,10 @@ static bool detectShiftUntilZeroIdiom(Loop *CurLoop, ScalarEvolution *SE, |
3368 | 3388 | /// %start = <...> |
3369 | 3389 | /// %extraoffset = <...> |
3370 | 3390 | /// <...> |
3371 | | -/// br label %for.cond |
| 3391 | +/// br label %loop |
3372 | 3392 | /// |
3373 | 3393 | /// loop: |
3374 | | -/// %iv = phi i8 [ %start, %entry ], [ %iv.next, %for.cond ] |
| 3394 | +/// %iv = phi i8 [ %start, %entry ], [ %iv.next, %loop ] |
3375 | 3395 | /// %nbits = add nsw i8 %iv, %extraoffset |
3376 | 3396 | /// %val.shifted = {{l,a}shr,shl} i8 %val, %nbits |
3377 | 3397 | /// %val.shifted.iszero = icmp eq i8 %val.shifted, 0 |
@@ -3533,7 +3553,19 @@ bool LoopIdiomRecognize::recognizeShiftUntilZero() { |
3533 | 3553 |
|
3534 | 3554 | // The loop terminator. |
3535 | 3555 | Builder.SetInsertPoint(LoopHeaderBB->getTerminator()); |
3536 | | - Builder.CreateCondBr(CIVCheck, SuccessorBB, LoopHeaderBB); |
| 3556 | + SmallVector<uint32_t> BranchWeights; |
| 3557 | + const bool HasBranchWeights = |
| 3558 | + !ProfcheckDisableMetadataFixes && |
| 3559 | + extractBranchWeights(*LoopHeaderBB->getTerminator(), BranchWeights); |
| 3560 | + |
| 3561 | + auto *BI = Builder.CreateCondBr(CIVCheck, SuccessorBB, LoopHeaderBB); |
| 3562 | + if (HasBranchWeights) { |
| 3563 | + if (InvertedCond) |
| 3564 | + std::swap(BranchWeights[0], BranchWeights[1]); |
| 3565 | + // We're not changing the loop profile, so we can reuse the original loop's |
| 3566 | + // profile. |
| 3567 | + setBranchWeights(*BI, BranchWeights, /*IsExpected=*/false); |
| 3568 | + } |
3537 | 3569 | LoopHeaderBB->getTerminator()->eraseFromParent(); |
3538 | 3570 |
|
3539 | 3571 | // Populate the IV PHI. |
|
0 commit comments