Files
clang-p2996/llvm/test/Transforms/InstCombine/wcslen-5.ll
Nikita Popov 90ba33099c [InstCombine] Canonicalize constant GEPs to i8 source element type (#68882)
This patch canonicalizes getelementptr instructions with constant
indices to use the `i8` source element type. This makes it easier for
optimizations to recognize that two GEPs are identical, because they
don't need to see past many different ways to express the same offset.

This is a first step towards
https://discourse.llvm.org/t/rfc-replacing-getelementptr-with-ptradd/68699.
This is limited to constant GEPs only for now, as they have a clear
canonical form, while we're not yet sure how exactly to deal with
variable indices.

The test llvm/test/Transforms/PhaseOrdering/switch_with_geps.ll gives
two representative examples of the kind of optimization improvement we
expect from this change. In the first test SimplifyCFG can now realize
that all switch branches are actually the same. In the second test it
can convert it into simple arithmetic. These are representative of
common optimization failures we see in Rust.

Fixes https://github.com/llvm/llvm-project/issues/69841.
2024-01-24 15:25:29 +01:00

148 lines
5.8 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; Verify that wcslen calls with conditional expressions involving constant
; string arguments with nonconstant offsets are folded as expected. See
; strlen-4.ll for the corresponding strlen test.
;
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
declare i64 @wcslen(ptr)
!0 = !{i32 1, !"wchar_size", i32 4}
!llvm.module.flags = !{!0}
@ws3 = constant [4 x i32] [i32 1, i32 2, i32 3, i32 0]
@ws5 = constant [6 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 0]
@ws5_3 = constant [10 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 0, i32 6, i32 7, i32 8, i32 0]
; Fold wcslen (x ? s3 + i: s5) to x ? 3 - i : 5.
define dso_local i64 @fold_wcslen_s3_pi_s5(i1 zeroext %0, i64 %1) {
; CHECK-LABEL: @fold_wcslen_s3_pi_s5(
; CHECK-NEXT: [[PS3_PI:%.*]] = getelementptr inbounds [4 x i32], ptr @ws3, i64 0, i64 [[TMP1:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP0:%.*]], ptr [[PS3_PI]], ptr @ws5
; CHECK-NEXT: [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull [[SEL]])
; CHECK-NEXT: ret i64 [[LEN]]
;
%ps3_pi = getelementptr inbounds [4 x i32], ptr @ws3, i64 0, i64 %1
%sel = select i1 %0, ptr %ps3_pi, ptr @ws5
%len = tail call i64 @wcslen(ptr %sel)
ret i64 %len
}
; More complex expressions like the one below are not handled yet.
; Fold: wcslen (x ? s3 + i + 1 : s5 + j + 2) to x ? 2 - i : 3 - j.
define dso_local i64 @fold_wcslen_s3_pi_p1_s5(i1 zeroext %0, i64 %1) {
; XFAIL-CHECK-LABEL: @fold_wcslen_s3_pi_p1_s5(
; XFAIL-CHECK-NEXT: [[DIF_I:%.*]] = sub i64 2, %1
; XFAIL-CHECK-NEXT: [[SEL:%.*]] = select i1 %0, i64 [[DIF_I]], i64 5
; XFAIL-CHECK-NEXT: ret i64 [[SEL]]
; CHECK-LABEL: @fold_wcslen_s3_pi_p1_s5(
; CHECK-NEXT: [[PS3_PI:%.*]] = getelementptr inbounds [4 x i32], ptr @ws3, i64 0, i64 [[TMP1:%.*]]
; CHECK-NEXT: [[PS3_PI_P1:%.*]] = getelementptr inbounds i8, ptr [[PS3_PI]], i64 4
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP0:%.*]], ptr [[PS3_PI_P1]], ptr @ws5
; CHECK-NEXT: [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull [[SEL]])
; CHECK-NEXT: ret i64 [[LEN]]
;
%ps3_pi = getelementptr inbounds [4 x i32], ptr @ws3, i64 0, i64 %1
%ps3_pi_p1 = getelementptr inbounds i32, ptr %ps3_pi, i64 1
%sel = select i1 %0, ptr %ps3_pi_p1, ptr @ws5
%len = tail call i64 @wcslen(ptr %sel)
ret i64 %len
}
; Avoid folding calls with conditional expressions involving constant
; string arguments with embedded nuls such as:
; wcslen (x ? s5_3 + i : s5).
define dso_local i64 @call_wcslen_s5_3_pi_s5(i1 zeroext %0, i64 %1) {
; CHECK-LABEL: @call_wcslen_s5_3_pi_s5(
; CHECK-NEXT: [[PS5_3_PI:%.*]] = getelementptr inbounds [10 x i32], ptr @ws5_3, i64 0, i64 [[TMP1:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP0:%.*]], ptr [[PS5_3_PI]], ptr @ws5
; CHECK-NEXT: [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull [[SEL]])
; CHECK-NEXT: ret i64 [[LEN]]
;
%ps5_3_pi = getelementptr inbounds [10 x i32], ptr @ws5_3, i64 0, i64 %1
%sel = select i1 %0, ptr %ps5_3_pi, ptr @ws5
%len = tail call i64 @wcslen(ptr %sel)
ret i64 %len
}
; But do fold wcslen (x ? s5_3 : s5 + j) to x ? 5 : 5 - j.
define dso_local i64 @call_wcslen_s5_3_s5_pj(i1 zeroext %0, i64 %1) {
; CHECK-LABEL: @call_wcslen_s5_3_s5_pj(
; CHECK-NEXT: [[PS5:%.*]] = getelementptr inbounds [6 x i32], ptr @ws5, i64 0, i64 [[TMP1:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP0:%.*]], ptr @ws5_3, ptr [[PS5]]
; CHECK-NEXT: [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull [[SEL]])
; CHECK-NEXT: ret i64 [[LEN]]
;
%ps5 = getelementptr inbounds [6 x i32], ptr @ws5, i64 0, i64 %1
%sel = select i1 %0, ptr @ws5_3, ptr %ps5
%len = tail call i64 @wcslen(ptr %sel)
ret i64 %len
}
; Fold wcslen (x ? s3: s5 + j) to x ? 3 : 5 - j.
define dso_local i64 @fold_wcslen_s3_s5_pj(i1 zeroext %0, i64 %1) {
; CHECK-LABEL: @fold_wcslen_s3_s5_pj(
; CHECK-NEXT: [[PS5_PJ:%.*]] = getelementptr inbounds [6 x i32], ptr @ws5, i64 0, i64 [[TMP1:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP0:%.*]], ptr @ws3, ptr [[PS5_PJ]]
; CHECK-NEXT: [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull [[SEL]])
; CHECK-NEXT: ret i64 [[LEN]]
;
%ps5_pj = getelementptr inbounds [6 x i32], ptr @ws5, i64 0, i64 %1
%sel = select i1 %0, ptr @ws3, ptr %ps5_pj
%len = tail call i64 @wcslen(ptr %sel)
ret i64 %len
}
; Same as above, avoid folding calls with conditional expressions involving
; constant string arguments with embedded nuls such as:
; wcslen (x ? s3 : s5_3 + j).
define dso_local i64 @call_wcslen_s3_s5_3_pj(i1 zeroext %0, i64 %1) {
; CHECK-LABEL: @call_wcslen_s3_s5_3_pj(
; CHECK-NEXT: [[PS5_3_PJ:%.*]] = getelementptr inbounds [10 x i32], ptr @ws5_3, i64 0, i64 [[TMP1:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP0:%.*]], ptr @ws3, ptr [[PS5_3_PJ]]
; CHECK-NEXT: [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull [[SEL]])
; CHECK-NEXT: ret i64 [[LEN]]
;
%ps5_3_pj = getelementptr inbounds [10 x i32], ptr @ws5_3, i64 0, i64 %1
%sel = select i1 %0, ptr @ws3, ptr %ps5_3_pj
%len = tail call i64 @wcslen(ptr %sel)
ret i64 %len
}
; Fold wcslen (x ? s3 + i: s5 + j) to x ? 3 - i : 5 - j.
define dso_local i64 @fold_wcslen_s3_pi_s5_pj(i1 zeroext %0, i64 %1, i64 %2) {
; CHECK-LABEL: @fold_wcslen_s3_pi_s5_pj(
; CHECK-NEXT: [[PS3_PI:%.*]] = getelementptr inbounds [4 x i32], ptr @ws3, i64 0, i64 [[TMP1:%.*]]
; CHECK-NEXT: [[PS5_PJ:%.*]] = getelementptr inbounds [6 x i32], ptr @ws5, i64 0, i64 [[TMP2:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP0:%.*]], ptr [[PS3_PI]], ptr [[PS5_PJ]]
; CHECK-NEXT: [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull [[SEL]])
; CHECK-NEXT: ret i64 [[LEN]]
;
%ps3_pi = getelementptr inbounds [4 x i32], ptr @ws3, i64 0, i64 %1
%ps5_pj = getelementptr inbounds [6 x i32], ptr @ws5, i64 0, i64 %2
%sel = select i1 %0, ptr %ps3_pi, ptr %ps5_pj
%len = tail call i64 @wcslen(ptr %sel)
ret i64 %len
}