diff options
author | Sanjay Patel <spatel@rotateright.com> | 2016-05-20 14:53:09 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2016-05-20 14:53:09 +0000 |
commit | f5a8ebbd9712ed96d84655d541ab4f37cba48360 (patch) | |
tree | 850c46e65e61455a8820e2a30762d6377c7b6bb0 | |
parent | a3d21bd41c8c69d12de309ede91efa514af4a5b2 (diff) |
[SimplifyCFG] eliminate switch cases based on known range of switch condition
This was noted in PR24766:
https://llvm.org/bugs/show_bug.cgi?id=24766#c2
We may not know whether the sign bit(s) are zero or one, but we can still
optimize based on knowing that the sign bit is repeated.
Differential Revision: http://reviews.llvm.org/D20275
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270222 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Transforms/Utils/SimplifyCFG.cpp | 14 | ||||
-rw-r--r-- | test/Transforms/SimplifyCFG/switch-masked-bits.ll | 4 |
2 files changed, 10 insertions, 8 deletions
diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index 16e886569e5..fb6a13fadc5 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -3914,14 +3914,20 @@ static bool EliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC, APInt KnownZero(Bits, 0), KnownOne(Bits, 0); computeKnownBits(Cond, KnownZero, KnownOne, DL, 0, AC, SI); + // We can also eliminate cases by determining that their values are outside of + // the limited range of the condition based on how many significant (non-sign) + // bits are in the condition value. + unsigned ExtraSignBits = ComputeNumSignBits(Cond, DL, 0, AC, SI) - 1; + unsigned MaxSignificantBitsInCond = Bits - ExtraSignBits; + // Gather dead cases. SmallVector<ConstantInt *, 8> DeadCases; for (auto &Case : SI->cases()) { - if ((Case.getCaseValue()->getValue() & KnownZero) != 0 || - (Case.getCaseValue()->getValue() & KnownOne) != KnownOne) { + APInt CaseVal = Case.getCaseValue()->getValue(); + if ((CaseVal & KnownZero) != 0 || (CaseVal & KnownOne) != KnownOne || + (CaseVal.getMinSignedBits() > MaxSignificantBitsInCond)) { DeadCases.push_back(Case.getCaseValue()); - DEBUG(dbgs() << "SimplifyCFG: switch case '" << Case.getCaseValue() - << "' is dead.\n"); + DEBUG(dbgs() << "SimplifyCFG: switch case " << CaseVal << " is dead.\n"); } } diff --git a/test/Transforms/SimplifyCFG/switch-masked-bits.ll b/test/Transforms/SimplifyCFG/switch-masked-bits.ll index 284f7e3cb15..2d46aac23f6 100644 --- a/test/Transforms/SimplifyCFG/switch-masked-bits.ll +++ b/test/Transforms/SimplifyCFG/switch-masked-bits.ll @@ -49,14 +49,10 @@ c: define i1 @repeated_signbits(i8 %condition) { ; CHECK-LABEL: @repeated_signbits( ; CHECK: switch i32 -; CHECK-DAG: i32 -2147483648, label %a -; CHECK-DAG: i32 -129, label %a ; CHECK-DAG: i32 -128, label %a ; CHECK-DAG: i32 -1, label %a ; CHECK-DAG: i32 0, label %a ; CHECK-DAG: i32 127, label %a -; CHECK-DAG: i32 128, label %a -; CHECK-DAG: i32 2147483647, label %a ; CHECK-NEXT: ] ; entry: |