This fixes an issue with switch narrowing that gets exposed
by r274233 ( https://reviews.llvm.org/D12965 - [InstCombine] shrink
switch conditions better (PR24766)). The problem is that case constants are
narrowed (to, say, width "k"), then the condition like cond = expr + const is
simplified by adding const to the case constants, and finally the new case
constants are narrowed again. However, when the width for the second round
of narrowing increases to, say, "k+n", then cond is a "k" bit expression
which won't match properly any "k+n" wide case constants.
The fix is to do the cond optimization first and only then narrow the case
constants appropriately.
Details
Diff Detail
- Build Status
Buildable 1784 Build 1784: arc lint + arc unit
Event Timeline
lib/Transforms/InstCombine/InstructionCombining.cpp | ||
---|---|---|
2276 | Gone as part of the fix: the condition expression a+const is evaluated at full width before the case constants are inspected. | |
test/Transforms/InstCombine/narrow-switch.ll | ||
108 | The original code evaluates the case constants. The fix evaluates a+const in 64b and changes the case value to value -const (still 64b). Then it finds the case values to be 61b , and adjusts with of the values and cond accordingly. |
I'm sorry, but I'm having trouble fully understanding exactly what the bug is that this is fixing.
It seems that this patch is regressing the @trunc64to59 test since we're now failing to truncate the condition to i59.
It does seem reasonable to optimize the switch (x + C) before doing truncation, so this is probably the right thing to do, I'd just like to understand it better.
And if we're going to do truncation after we've changed the switch condition, don't we need to move the computeKnownBits part as well? With your patch, we're first computing known bits of the condition, then changing the condition, and then later using those known bits. That seems wrong.
lib/Transforms/InstCombine/InstructionCombining.cpp | ||
---|---|---|
2276 | Oh, I see now. |
Hans, you were right. The computeKnowBits etc parts should have been moved also. Sanjay committed the fix proper in r289442. I only kept the regression test in narrow-switch.ll (r293018).
-Gerolf
lib/Transforms/InstCombine/InstructionCombining.cpp | ||
---|---|---|
2247 | Range-for? |
Range-for?