[llvm] [SimplifyCFG] Fix the compile crash for invalid upper bound value (PR #71351)

via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 8 02:00:50 PST 2023


================
@@ -6631,6 +6630,30 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
 
   BranchInst *RangeCheckBranch = nullptr;
 
+  // Grow the table to cover all possible index values to avoid the range check.
+  // It will use the default result to fill in the table hole later, so make
+  // sure it exist.
+  if (UseSwitchConditionAsTableIndex && HasDefaultResults) {
+    ConstantRange CR = computeConstantRange(TableIndex, /* ForSigned */ false);
+    // Grow the table shouldn't have any size impact by checking
+    // WouldFitInRegister.
+    // TODO: Consider growing the table also when it doesn't fit in a register
+    // if no optsize is specified.
+    // Check that the constant range hasn't wrapped.
+    const APInt &UpperBound = CR.getUpper();
+    if (UpperBound.getZExtValue() > CR.getLower().getZExtValue() &&
----------------
vfdff wrote:

Apply your comment, thanks

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


More information about the llvm-commits mailing list