[llvm] 6f194a6 - [SimplifyCFG] Avoid truncation in linear map overflow check
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 23 06:13:43 PDT 2024
Author: Nikita Popov
Date: 2024-09-23T15:13:32+02:00
New Revision: 6f194a6dea4b4067336431e699ea3588417d4b96
URL: https://github.com/llvm/llvm-project/commit/6f194a6dea4b4067336431e699ea3588417d4b96
DIFF: https://github.com/llvm/llvm-project/commit/6f194a6dea4b4067336431e699ea3588417d4b96.diff
LOG: [SimplifyCFG] Avoid truncation in linear map overflow check
This is supposed to test multiplication of the linear multiplifier
with the largest value it can be multiplied with. However, if
we truncate TableSize-1 here, it might not actually be the largest
value. I think in practice this still works out, because in cases
where we'd truncate the value here we'd also fail the NonMonotonic
check. But to match the intent of the code, we should treat the
truncating case as overflowing.
Added:
Modified:
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index aa31e6c0c444ac..1f2c9389c008bd 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6528,9 +6528,10 @@ SwitchLookupTable::SwitchLookupTable(
if (LinearMappingPossible) {
LinearOffset = cast<ConstantInt>(TableContents[0]);
LinearMultiplier = ConstantInt::get(M.getContext(), DistToPrev);
- bool MayWrap = false;
APInt M = LinearMultiplier->getValue();
- (void)M.smul_ov(APInt(M.getBitWidth(), TableSize - 1), MayWrap);
+ bool MayWrap = true;
+ if (isIntN(M.getBitWidth(), TableSize - 1))
+ (void)M.smul_ov(APInt(M.getBitWidth(), TableSize - 1), MayWrap);
LinearMapValWrapped = NonMonotonic || MayWrap;
Kind = LinearMapKind;
++NumLinearMaps;
diff --git a/llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll b/llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll
index 845c5008e3837b..9549ccdbfe9ec4 100644
--- a/llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll
+++ b/llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll
@@ -2118,6 +2118,31 @@ cond.end: ; preds = %entry, %cond.false
ret i8 %conv
}
+define i1 @linearmap_trunc_smaller_table_size(i8 %arg) {
+; CHECK-LABEL: @linearmap_trunc_smaller_table_size(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i8 [[ARG:%.*]], 10
+; CHECK-NEXT: [[SWITCH_IDX_CAST:%.*]] = trunc i8 [[ARG]] to i1
+; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[TMP0]], i1 [[SWITCH_IDX_CAST]], i1 false
+; CHECK-NEXT: ret i1 [[SPEC_SELECT]]
+;
+entry:
+ switch i8 %arg, label %exit [
+ i8 1, label %sw
+ i8 3, label %sw
+ i8 5, label %sw
+ i8 7, label %sw
+ i8 9, label %sw
+ ]
+
+sw:
+ br label %exit
+
+exit:
+ %phi = phi i1 [ true, %sw ], [ false, %entry ]
+ ret i1 %phi
+}
+
; Don't create a table with an unknown type
define { i8, i8 } @test_unknown_result_type(i8 %n) {
; CHECK-LABEL: @test_unknown_result_type(
More information about the llvm-commits
mailing list