blob: 1bb2f65c0417d86d42021b7bf8a9f66dacd1ae19 [file]
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; Test that the modular format string library call simplifier works correctly.
;
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
@.str.int = constant [3 x i8] c"%d\00"
@.str.float = constant [3 x i8] c"%f\00"
@.str.multi = constant [6 x i8] c"%f %d\00"
@.str.noargs = constant [1 x i8] c"\00"
;; No aspects are specified, so no transformation occurs.
define void @test_basic(i32 %arg) {
; CHECK-LABEL: @test_basic(
; CHECK-NEXT: call void (ptr, ...) @basic(ptr nonnull @.str.int, i32 [[ARG:%.*]])
; CHECK-NEXT: ret void
;
call void (ptr, ...) @basic(ptr @.str.int, i32 %arg)
ret void
}
declare void @basic(ptr, ...) #0
;; The "float" aspect is present and needed, so no transformation occurs.
define void @test_float_present(double %arg) {
; CHECK-LABEL: @test_float_present(
; CHECK-NEXT: call void (ptr, ...) @float_present(ptr nonnull @.str.float, double [[ARG:%.*]])
; CHECK-NEXT: ret void
;
call void (ptr, ...) @float_present(ptr @.str.float, double %arg)
ret void
}
declare void @float_present(ptr, ...) #1
;; The "float" aspect is present but not needed, so the call is transformed.
define void @test_float_absent(i32 %arg) {
; CHECK-LABEL: @test_float_absent(
; CHECK-NEXT: call void (ptr, ...) @float_present_mod(ptr nonnull @.str.int, i32 [[ARG:%.*]])
; CHECK-NEXT: ret void
;
call void (ptr, ...) @float_absent(ptr @.str.int, i32 %arg)
ret void
}
declare void @float_absent(ptr, ...) #1
;; Unknown aspects are always considered needed, so no transformation occurs.
define void @test_unknown_aspects(i32 %arg) {
; CHECK-LABEL: @test_unknown_aspects(
; CHECK-NEXT: call void (ptr, ...) @unknown_aspects(ptr nonnull @.str.int, i32 [[ARG:%.*]])
; CHECK-NEXT: ret void
;
call void (ptr, ...) @unknown_aspects(ptr @.str.int, i32 %arg)
ret void
}
declare void @unknown_aspects(ptr, ...) #2
;; The call has no arguments to check, so the "float" aspect is not needed and
;; the call is transformed.
define void @test_no_args_to_check() {
; CHECK-LABEL: @test_no_args_to_check(
; CHECK-NEXT: call void (ptr, ...) @float_present_mod(ptr nonnull @.str.noargs)
; CHECK-NEXT: ret void
;
call void (ptr, ...) @no_args_to_check(ptr @.str.noargs)
ret void
}
declare void @no_args_to_check(ptr, ...) #1
;; The first argument index is not 2. The "float" aspect is needed, so no
;; transformation occurs.
define void @test_first_arg_idx(i32 %ignored, double %arg) {
; CHECK-LABEL: @test_first_arg_idx(
; CHECK-NEXT: call void (i32, ptr, ...) @first_arg_idx(i32 [[IGNORED:%.*]], ptr nonnull @.str.float, double [[ARG:%.*]])
; CHECK-NEXT: ret void
;
call void (i32, ptr, ...) @first_arg_idx(i32 %ignored, ptr @.str.float, double %arg)
ret void
}
declare void @first_arg_idx(i32, ptr, ...) #3
;; One aspect ("unknown") is needed, but one ("float") is not. The call is
;; transformed, and a reference to the needed aspect is emitted.
define void @test_partial_aspects(i32 %arg) {
; CHECK-LABEL: @test_partial_aspects(
; CHECK-NEXT: call void (ptr, ...) @multiple_aspects_mod(ptr nonnull @.str.int, i32 [[ARG:%.*]])
; CHECK-NEXT: call void @llvm.reloc.none(metadata !"basic_impl_unknown")
; CHECK-NEXT: ret void
;
call void (ptr, ...) @partial_aspects(ptr @.str.int, i32 %arg)
ret void
}
declare void @partial_aspects(ptr, ...) #4
;; The first argument index is 0 (e.g. vsnprintf). We cannot optimize it, so
;; no transformation occurs and it shouldn't crash.
define void @test_zero_first_arg(ptr %ap) {
; CHECK-LABEL: @test_zero_first_arg(
; CHECK-NEXT: call void @zero_first_arg(ptr nonnull @.str.float, ptr [[AP:%.*]])
; CHECK-NEXT: ret void
;
call void @zero_first_arg(ptr @.str.float, ptr %ap)
ret void
}
;; The first argument index is 0, and the format string is constant and doesn't
;; contain a float specifier. The "float" aspect is optimized away, and it is
;; transformed to call the modular implementation.
define void @test_zero_first_arg_optimized(ptr %ap) {
; CHECK-LABEL: @test_zero_first_arg_optimized(
; CHECK-NEXT: call void @zero_first_arg_mod(ptr nonnull @.str.int, ptr [[AP:%.*]])
; CHECK-NEXT: ret void
;
call void @zero_first_arg(ptr @.str.int, ptr %ap)
ret void
}
declare void @zero_first_arg(ptr, ptr) #5
@.str.fixed = constant [3 x i8] c"%r\00"
@.str.complex_float = constant [16 x i8] c"%% %1$+#0*.*hlf\00"
@.str.dynamic = global ptr null
;; Both "float" and "fixed" aspects are present. The format string is "%r" (fixed-point),
;; so the "float" aspect is optimized away, and a reference to the needed "fixed" aspect is emitted.
define void @test_fixed_needed(i32 %arg) {
; CHECK-LABEL: @test_fixed_needed(
; CHECK-NEXT: call void (ptr, ...) @multiple_aspects_mod(ptr nonnull @.str.fixed, i32 [[ARG:%.*]])
; CHECK-NEXT: call void @llvm.reloc.none(metadata !"basic_impl_fixed")
; CHECK-NEXT: ret void
;
call void (ptr, ...) @fixed_needed(ptr @.str.fixed, i32 %arg)
ret void
}
declare void @fixed_needed(ptr, ...) #6
;; Both "float" and "fixed" aspects are present. The format string is "%f" (floating-point),
;; so the "fixed" aspect is optimized away, and a reference to the needed "float" aspect is emitted.
define void @test_float_needed(double %arg) {
; CHECK-LABEL: @test_float_needed(
; CHECK-NEXT: call void (ptr, ...) @multiple_aspects_mod(ptr nonnull @.str.float, double [[ARG:%.*]])
; CHECK-NEXT: call void @llvm.reloc.none(metadata !"basic_impl_float")
; CHECK-NEXT: ret void
;
call void (ptr, ...) @float_needed(ptr @.str.float, double %arg)
ret void
}
declare void @float_needed(ptr, ...) #6
;; Both "float" and "fixed" aspects are present. The format string is dynamic (not constant),
;; so both aspects are conservatively assumed to be needed, and no transformation occurs.
define void @test_dynamic_format(double %arg) {
; CHECK-LABEL: @test_dynamic_format(
; CHECK-NEXT: [[FMT:%.*]] = load ptr, ptr @.str.dynamic, align 4
; CHECK-NEXT: call void (ptr, ...) @fixed_needed(ptr [[FMT]], double [[ARG:%.*]])
; CHECK-NEXT: ret void
;
%fmt = load ptr, ptr @.str.dynamic, align 4
call void (ptr, ...) @fixed_needed(ptr %fmt, double %arg)
ret void
}
;; Both "float" and "fixed" aspects are present. The format string is dynamic (not constant),
;; but there are no floating-point arguments passed (only an integer). The "float" aspect is
;; optimized away, and only a reference to the needed "fixed" aspect is emitted.
define void @test_dynamic_format_no_float(i32 %arg) {
; CHECK-LABEL: @test_dynamic_format_no_float(
; CHECK-NEXT: [[FMT:%.*]] = load ptr, ptr @.str.dynamic, align 4
; CHECK-NEXT: call void (ptr, ...) @multiple_aspects_mod(ptr [[FMT]], i32 [[ARG:%.*]])
; CHECK-NEXT: call void @llvm.reloc.none(metadata !"basic_impl_fixed")
; CHECK-NEXT: ret void
;
%fmt = load ptr, ptr @.str.dynamic, align 4
call void (ptr, ...) @fixed_needed(ptr %fmt, i32 %arg)
ret void
}
;; The format string contains a complex float specifier (testing escaped %, positional args,
;; flags, width, precision, length modifiers) but no fixed specifier.
;; The "fixed" aspect is optimized away, and only a reference to the needed "float" aspect is emitted.
define void @test_complex_format(i32 %width, i32 %prec, double %val) {
; CHECK-LABEL: @test_complex_format(
; CHECK-NEXT: call void (ptr, ...) @multiple_aspects_mod(ptr nonnull @.str.complex_float, i32 [[WIDTH:%.*]], i32 [[PREC:%.*]], double [[VAL:%.*]])
; CHECK-NEXT: call void @llvm.reloc.none(metadata !"basic_impl_float")
; CHECK-NEXT: ret void
;
call void (ptr, ...) @fixed_needed(ptr @.str.complex_float, i32 %width, i32 %prec, double %val)
ret void
}
attributes #0 = { "modular-format"="printf,1,2,basic_mod,basic_impl" }
attributes #1 = { "modular-format"="printf,1,2,float_present_mod,basic_impl,float" }
attributes #2 = { "modular-format"="printf,1,2,unknown_aspects_mod,basic_impl,unknown1,unknown2" }
attributes #3 = { "modular-format"="printf,2,3,first_arg_idx_mod,basic_impl,float" }
attributes #4 = { "modular-format"="printf,1,2,multiple_aspects_mod,basic_impl,float,unknown" }
attributes #5 = { "modular-format"="printf,1,0,zero_first_arg_mod,basic_impl,float" }
attributes #6 = { "modular-format"="printf,1,2,multiple_aspects_mod,basic_impl,float,fixed" }