[llvm] r270222 - [SimplifyCFG] eliminate switch cases based on known range of switch condition
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Fri May 20 07:53:10 PDT 2016
Author: spatel
Date: Fri May 20 09:53:09 2016
New Revision: 270222
URL: http://llvm.org/viewvc/llvm-project?rev=270222&view=rev
Log:
[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
Modified:
llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/trunk/test/Transforms/SimplifyCFG/switch-masked-bits.ll
Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=270222&r1=270221&r2=270222&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Fri May 20 09:53:09 2016
@@ -3914,14 +3914,20 @@ static bool EliminateDeadSwitchCases(Swi
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");
}
}
Modified: llvm/trunk/test/Transforms/SimplifyCFG/switch-masked-bits.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/switch-masked-bits.ll?rev=270222&r1=270221&r2=270222&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/SimplifyCFG/switch-masked-bits.ll (original)
+++ llvm/trunk/test/Transforms/SimplifyCFG/switch-masked-bits.ll Fri May 20 09:53:09 2016
@@ -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:
More information about the llvm-commits
mailing list