[llvm] [SimplifyCFG] Delete the unnecessary range check for small mask operation (PR #65835)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 17 06:59:53 PDT 2023


================
@@ -6545,6 +6544,17 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
 
   BranchInst *RangeCheckBranch = nullptr;
 
+  if (UseSwitchConditionAsTableIndex) {
+    ConstantRange CR = computeConstantRange(TableIndex, /* ForSigned */ false);
+    if (DL.fitsInLegalInteger(CR.getUpper().getLimitedValue())) {
----------------
nikic wrote:

I think the correct predicate to use here is not plain fitsInLegalInteger, but rather WouldFitInRegister, which will make sure that the table size * element size fits. This means that whole bitfield bits into one integer.

Your current tests work with i1 values so it doesn't matter, but for larger values this would make a difference. Can you please add a test for that case?

I'm not sure checking optsize makes sense, as I think this actually reduces the size by omitting a branch? Though I guess it might increase the size if we generate an actual lookup table rather than an integer bit field -- if it's possible to do so in this code path, we should also test that.

https://github.com/llvm/llvm-project/pull/65835


More information about the llvm-commits mailing list