[InstCombine] recognizeBSwapOrBitReverseIdiom - cleanup bswap/bitreverse detection loop. NFCI.

Early out if both pattern matches have failed (or we don't want them). Fix case of bit index iterator (and avoid Wshadow issue).
This commit is contained in:
Simon Pilgrim 2020-09-30 14:11:43 +01:00
parent 3f88c10a6b
commit 621c6c8962
1 changed files with 10 additions and 8 deletions

View File

@ -3038,18 +3038,20 @@ bool llvm::recognizeBSwapOrBitReverseIdiom(
// Now, is the bit permutation correct for a bswap or a bitreverse? We can // Now, is the bit permutation correct for a bswap or a bitreverse? We can
// only byteswap values with an even number of bytes. // only byteswap values with an even number of bytes.
unsigned DemandedBW = DemandedTy->getBitWidth(); unsigned DemandedBW = DemandedTy->getBitWidth();
bool OKForBSwap = DemandedBW % 16 == 0, OKForBitReverse = true; bool OKForBSwap = MatchBSwaps && (DemandedBW % 16) == 0;
for (unsigned i = 0; i < DemandedBW; ++i) { bool OKForBitReverse = MatchBitReversals;
OKForBSwap &= for (unsigned BitIdx = 0;
bitTransformIsCorrectForBSwap(BitProvenance[i], i, DemandedBW); (BitIdx < DemandedBW) && (OKForBSwap || OKForBitReverse); ++BitIdx) {
OKForBitReverse &= OKForBSwap &= bitTransformIsCorrectForBSwap(BitProvenance[BitIdx], BitIdx,
bitTransformIsCorrectForBitReverse(BitProvenance[i], i, DemandedBW); DemandedBW);
OKForBitReverse &= bitTransformIsCorrectForBitReverse(BitProvenance[BitIdx],
BitIdx, DemandedBW);
} }
Intrinsic::ID Intrin; Intrinsic::ID Intrin;
if (OKForBSwap && MatchBSwaps) if (OKForBSwap)
Intrin = Intrinsic::bswap; Intrin = Intrinsic::bswap;
else if (OKForBitReverse && MatchBitReversals) else if (OKForBitReverse)
Intrin = Intrinsic::bitreverse; Intrin = Intrinsic::bitreverse;
else else
return false; return false;