// RUN: %clang_cc1 -no-opaque-pointers -triple csky -emit-llvm %s -o - | FileCheck %s
// RUN: %clang_cc1 -no-opaque-pointers -triple csky -target-feature +fpuv2_df -target-feature +fpuv2_sf \
// RUN: -target-feature +hard-float -target-feature +hard-float-abi -emit-llvm %s -o - | FileCheck %s
// This file contains test cases that will have the same output for the hard-float
// and soft-float ABIs.
// CHECK-LABEL: define{{.*}} void @f_void()
void
// Scalar arguments and return values smaller than the word size are extended
// according to the sign of their type, up to 32 bits
// CHECK-LABEL: define{{.*}} zeroext i1 @f_scalar_0(i1 noundef zeroext %x)
_Bool
// CHECK-LABEL: define{{.*}} signext i8 @f_scalar_1(i8 noundef signext %x)
int8_t
// CHECK-LABEL: define{{.*}} zeroext i8 @f_scalar_2(i8 noundef zeroext %x)
uint8_t
// CHECK-LABEL: define{{.*}} i32 @f_scalar_3(i32 noundef %x)
int32_t
// CHECK-LABEL: define{{.*}} i64 @f_scalar_4(i64 noundef %x)
int64_t
// CHECK-LABEL: define{{.*}} float @f_fp_scalar_1(float noundef %x)
float
// CHECK-LABEL: define{{.*}} double @f_fp_scalar_2(double noundef %x)
double
// CHECK-LABEL: define{{.*}} double @f_fp_scalar_3(double noundef %x)
long double
// Empty structs or unions are ignored.
;
// CHECK-LABEL: define{{.*}} void @f_agg_empty_struct()
struct empty_s
;
// CHECK-LABEL: define{{.*}} void @f_agg_empty_union()
union empty_u
// Aggregates <= 4*xlen may be passed in registers, so will be coerced to
// integer arguments. The rules for return are <= 2*xlen.
;
// CHECK-LABEL: define{{.*}} void @f_agg_tiny(i32 %x.coerce)
void
// CHECK-LABEL: define{{.*}} i32 @f_agg_tiny_ret()
struct tiny
;
// CHECK-LABEL: define{{.*}} void @f_agg_small([2 x i32] %x.coerce)
void
// CHECK-LABEL: define{{.*}} [2 x i32] @f_agg_small_ret()
struct small
;
// CHECK-LABEL: define{{.*}} void @f_agg_small_aligned(i64 %x.coerce)
void
// CHECK-LABEL: define{{.*}} i64 @f_agg_small_aligned_ret(i64 %x.coerce)
struct small_aligned
// For argument type, the first 4*XLen parts of aggregate will be passed
// in registers, and the rest will be passed in stack.
// So we can coerce to integers directly and let backend handle it correctly.
// For return type, aggregate which <= 2*XLen will be returned in registers.
// Otherwise, aggregate will be returned indirectly.
;
// CHECK-LABEL: define{{.*}} void @f_agg_large([4 x i32] %x.coerce)
void
// The address where the struct should be written to will be the first
// argument
// CHECK-LABEL: define{{.*}} void @f_agg_large_ret(%struct.large* noalias sret(%struct.large) align 4 %agg.result, i32 noundef %i, i8 noundef signext %j)
struct large
typedef unsigned char v16i8 ;
// CHECK-LABEL: define{{.*}} void @f_vec_large_v16i8(<16 x i8> noundef %x)
void
// CHECK-LABEL: define{{.*}} <16 x i8> @f_vec_large_v16i8_ret()
v16i8
// CHECK-LABEL: define{{.*}} i32 @f_scalar_stack_1(i32 %a.coerce, [2 x i32] %b.coerce, i64 %c.coerce, [4 x i32] %d.coerce, i8 noundef zeroext %e, i8 noundef signext %f, i8 noundef zeroext %g, i8 noundef signext %h)
int
// Ensure that scalars passed on the stack are still determined correctly in
// the presence of large return values that consume a register due to the need
// to pass a pointer.
// CHECK-LABEL: define{{.*}} void @f_scalar_stack_2(%struct.large* noalias sret(%struct.large) align 4 %agg.result, i32 noundef %a, i64 noundef %b, i64 noundef %c, double noundef %d, i8 noundef zeroext %e, i8 noundef signext %f, i8 noundef zeroext %g)
struct large
// CHECK-LABEL: define{{.*}} double @f_scalar_stack_4(i32 noundef %a, i64 noundef %b, i64 noundef %c, double noundef %d, i8 noundef zeroext %e, i8 noundef signext %f, i8 noundef zeroext %g)
long double
// Aggregates should be coerced integer arrary.
// CHECK-LABEL: define{{.*}} void @f_scalar_stack_5(double noundef %a, i64 noundef %b, double noundef %c, i64 noundef %d, i32 noundef %e, i64 noundef %f, float noundef %g, double noundef %h, double noundef %i)
void
// CHECK-LABEL: define{{.*}} void @f_agg_stack(double noundef %a, i64 noundef %b, double noundef %c, i64 noundef %d, i32 %e.coerce, [2 x i32] %f.coerce, i64 %g.coerce, [4 x i32] %h.coerce)
void
// Ensure that ABI lowering happens as expected for vararg calls. For CSKY
// with the base integer calling convention there will be no observable
// differences in the lowered IR for a call with varargs vs without.
int ;
// CHECK-LABEL: define{{.*}} void @f_va_caller()
// CHECK: call i32 (i32, ...) @f_va_callee(i32 noundef 1, i32 noundef 2, i64 noundef 3, double noundef 4.000000e+00, double noundef 5.000000e+00, i32 {{%.*}}, [2 x i32] {{%.*}}, i64 {{%.*}}, [4 x i32] {{%.*}})
void
// CHECK-LABEL: define{{.*}} i32 @f_va_1(i8* noundef %fmt, ...) {{.*}} {
// CHECK: [[FMT_ADDR:%.*]] = alloca i8*, align 4
// CHECK: [[VA:%.*]] = alloca i8*, align 4
// CHECK: [[V:%.*]] = alloca i32, align 4
// CHECK: store i8* %fmt, i8** [[FMT_ADDR]], align 4
// CHECK: [[VA1:%.*]] = bitcast i8** [[VA]] to i8*
// CHECK: call void @llvm.va_start(i8* [[VA1]])
// CHECK: [[ARGP_CUR:%.*]] = load i8*, i8** [[VA]], align 4
// CHECK: [[ARGP_NEXT:%.*]] = getelementptr inbounds i8, i8* [[ARGP_CUR]], i32 4
// CHECK: store i8* [[ARGP_NEXT]], i8** [[VA]], align 4
// CHECK: [[TMP0:%.*]] = bitcast i8* [[ARGP_CUR]] to i32*
// CHECK: [[TMP1:%.*]] = load i32, i32* [[TMP0]], align 4
// CHECK: store i32 [[TMP1]], i32* [[V]], align 4
// CHECK: [[VA2:%.*]] = bitcast i8** [[VA]] to i8*
// CHECK: call void @llvm.va_end(i8* [[VA2]])
// CHECK: [[TMP2:%.*]] = load i32, i32* [[V]], align 4
// CHECK: ret i32 [[TMP2]]
// CHECK: }
int
// CHECK-LABEL: @f_va_2(
// CHECK: [[FMT_ADDR:%.*]] = alloca i8*, align 4
// CHECK-NEXT: [[VA:%.*]] = alloca i8*, align 4
// CHECK-NEXT: [[V:%.*]] = alloca double, align 4
// CHECK-NEXT: store i8* [[FMT:%.*]], i8** [[FMT_ADDR]], align 4
// CHECK-NEXT: [[VA1:%.*]] = bitcast i8** [[VA]] to i8*
// CHECK-NEXT: call void @llvm.va_start(i8* [[VA1]])
// CHECK-NEXT: [[ARGP_CUR:%.*]] = load i8*, i8** [[VA]], align 4
// CHECK-NEXT: [[ARGP_NEXT:%.*]] = getelementptr inbounds i8, i8* [[ARGP_CUR]], i32 8
// CHECK-NEXT: store i8* [[ARGP_NEXT]], i8** [[VA]], align 4
// CHECK-NEXT: [[TMP3:%.*]] = bitcast i8* [[ARGP_CUR]] to double*
// CHECK-NEXT: [[TMP4:%.*]] = load double, double* [[TMP3]], align 4
// CHECK-NEXT: store double [[TMP4]], double* [[V]], align 4
// CHECK-NEXT: [[VA2:%.*]] = bitcast i8** [[VA]] to i8*
// CHECK-NEXT: call void @llvm.va_end(i8* [[VA2]])
// CHECK-NEXT: [[TMP5:%.*]] = load double, double* [[V]], align 4
// CHECK-NEXT: ret double [[TMP5]]
double
// CHECK-LABEL: @f_va_3(
// CHECK: [[FMT_ADDR:%.*]] = alloca i8*, align 4
// CHECK-NEXT: [[VA:%.*]] = alloca i8*, align 4
// CHECK-NEXT: [[V:%.*]] = alloca double, align 4
// CHECK-NEXT: [[W:%.*]] = alloca i32, align 4
// CHECK-NEXT: [[X:%.*]] = alloca double, align 4
// CHECK-NEXT: store i8* [[FMT:%.*]], i8** [[FMT_ADDR]], align 4
// CHECK-NEXT: [[VA1:%.*]] = bitcast i8** [[VA]] to i8*
// CHECK-NEXT: call void @llvm.va_start(i8* [[VA1]])
// CHECK-NEXT: [[ARGP_CUR:%.*]] = load i8*, i8** [[VA]], align 4
// CHECK-NEXT: [[ARGP_NEXT:%.*]] = getelementptr inbounds i8, i8* [[ARGP_CUR]], i32 8
// CHECK-NEXT: store i8* [[ARGP_NEXT]], i8** [[VA]], align 4
// CHECK-NEXT: [[TMP3:%.*]] = bitcast i8* [[ARGP_CUR]] to double*
// CHECK-NEXT: [[TMP4:%.*]] = load double, double* [[TMP3]], align 4
// CHECK-NEXT: store double [[TMP4]], double* [[V]], align 4
// CHECK-NEXT: [[ARGP_CUR2:%.*]] = load i8*, i8** [[VA]], align 4
// CHECK-NEXT: [[ARGP_NEXT3:%.*]] = getelementptr inbounds i8, i8* [[ARGP_CUR2]], i32 4
// CHECK-NEXT: store i8* [[ARGP_NEXT3]], i8** [[VA]], align 4
// CHECK-NEXT: [[TMP5:%.*]] = bitcast i8* [[ARGP_CUR2]] to i32*
// CHECK-NEXT: [[TMP6:%.*]] = load i32, i32* [[TMP5]], align 4
// CHECK-NEXT: store i32 [[TMP6]], i32* [[W]], align 4
// CHECK-NEXT: [[ARGP_CUR4:%.*]] = load i8*, i8** [[VA]], align 4
// CHECK-NEXT: [[ARGP_NEXT5:%.*]] = getelementptr inbounds i8, i8* [[ARGP_CUR4]], i32 8
// CHECK-NEXT: store i8* [[ARGP_NEXT5]], i8** [[VA]], align 4
// CHECK-NEXT: [[TMP10:%.*]] = bitcast i8* [[ARGP_CUR4]] to double*
// CHECK-NEXT: [[TMP11:%.*]] = load double, double* [[TMP10]], align 4
// CHECK-NEXT: store double [[TMP11]], double* [[X]], align 4
// CHECK-NEXT: [[VA6:%.*]] = bitcast i8** [[VA]] to i8*
// CHECK-NEXT: call void @llvm.va_end(i8* [[VA6]])
// CHECK-NEXT: [[TMP12:%.*]] = load double, double* [[V]], align 4
// CHECK-NEXT: [[TMP13:%.*]] = load double, double* [[X]], align 4
// CHECK-NEXT: [[ADD:%.*]] = fadd double [[TMP12]], [[TMP13]]
// CHECK-NEXT: ret double [[ADD]]
double
// CHECK-LABEL: define{{.*}} i32 @f_va_4(i8* noundef %fmt, ...) {{.*}} {
// CHECK: [[FMT_ADDR:%.*]] = alloca i8*, align 4
// CHECK-NEXT: [[VA:%.*]] = alloca i8*, align 4
// CHECK-NEXT: [[V:%.*]] = alloca i32, align 4
// CHECK-NEXT: [[LD:%.*]] = alloca double, align 4
// CHECK-NEXT: [[TS:%.*]] = alloca [[STRUCT_TINY:%.*]], align 1
// CHECK-NEXT: [[SS:%.*]] = alloca [[STRUCT_SMALL:%.*]], align 4
// CHECK-NEXT: [[LS:%.*]] = alloca [[STRUCT_LARGE:%.*]], align 4
// CHECK-NEXT: [[RET:%.*]] = alloca i32, align 4
// CHECK-NEXT: store i8* [[FMT:%.*]], i8** [[FMT_ADDR]], align 4
// CHECK-NEXT: [[VA1:%.*]] = bitcast i8** [[VA]] to i8*
// CHECK-NEXT: call void @llvm.va_start(i8* [[VA1]])
// CHECK-NEXT: [[ARGP_CUR:%.*]] = load i8*, i8** [[VA]], align 4
// CHECK-NEXT: [[ARGP_NEXT:%.*]] = getelementptr inbounds i8, i8* [[ARGP_CUR]], i32 4
// CHECK-NEXT: store i8* [[ARGP_NEXT]], i8** [[VA]], align 4
// CHECK-NEXT: [[TMP0:%.*]] = bitcast i8* [[ARGP_CUR]] to i32*
// CHECK-NEXT: [[TMP1:%.*]] = load i32, i32* [[TMP0]], align 4
// CHECK-NEXT: store i32 [[TMP1]], i32* [[V]], align 4
// CHECK-NEXT: [[ARGP_CUR2:%.*]] = load i8*, i8** [[VA]], align 4
// CHECK-NEXT: [[ARGP_NEXT3:%.*]] = getelementptr inbounds i8, i8* [[ARGP_CUR2]], i32 8
// CHECK-NEXT: store i8* [[ARGP_NEXT3]], i8** [[VA]], align 4
// CHECK-NEXT: [[TMP2:%.*]] = bitcast i8* [[ARGP_CUR2]] to double*
// CHECK-NEXT: [[TMP4:%.*]] = load double, double* [[TMP2]], align 4
// CHECK-NEXT: store double [[TMP4]], double* [[LD]], align 4
// CHECK-NEXT: [[ARGP_CUR4:%.*]] = load i8*, i8** [[VA]], align 4
// CHECK-NEXT: [[ARGP_NEXT5:%.*]] = getelementptr inbounds i8, i8* [[ARGP_CUR4]], i32 4
// CHECK-NEXT: store i8* [[ARGP_NEXT5]], i8** [[VA]], align 4
// CHECK-NEXT: [[TMP5:%.*]] = bitcast i8* [[ARGP_CUR4]] to %struct.tiny*
// CHECK-NEXT: [[TMP6:%.*]] = bitcast %struct.tiny* [[TS]] to i8*
// CHECK-NEXT: [[TMP7:%.*]] = bitcast %struct.tiny* [[TMP5]] to i8*
// CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 [[TMP6]], i8* align 4 [[TMP7]], i32 4, i1 false)
// CHECK-NEXT: [[ARGP_CUR6:%.*]] = load i8*, i8** [[VA]], align 4
// CHECK-NEXT: [[ARGP_NEXT7:%.*]] = getelementptr inbounds i8, i8* [[ARGP_CUR6]], i32 8
// CHECK-NEXT: store i8* [[ARGP_NEXT7]], i8** [[VA]], align 4
// CHECK-NEXT: [[TMP8:%.*]] = bitcast i8* [[ARGP_CUR6]] to %struct.small*
// CHECK-NEXT: [[TMP9:%.*]] = bitcast %struct.small* [[SS]] to i8*
// CHECK-NEXT: [[TMP10:%.*]] = bitcast %struct.small* [[TMP8]] to i8*
// CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 [[TMP9]], i8* align 4 [[TMP10]], i32 8, i1 false)
// CHECK-NEXT: [[ARGP_CUR8:%.*]] = load i8*, i8** [[VA]], align 4
// CHECK-NEXT: [[ARGP_NEXT9:%.*]] = getelementptr inbounds i8, i8* [[ARGP_CUR8]], i32 16
// CHECK-NEXT: store i8* [[ARGP_NEXT9]], i8** [[VA]], align 4
// CHECK-NEXT: [[TMP11:%.*]] = bitcast i8* [[ARGP_CUR8]] to %struct.large*
// CHECK-NEXT: [[TMP13:%.*]] = bitcast %struct.large* [[LS]] to i8*
// CHECK-NEXT: [[TMP14:%.*]] = bitcast %struct.large* [[TMP11]] to i8*
// CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 [[TMP13]], i8* align 4 [[TMP14]], i32 16, i1 false)
// CHECK-NEXT: [[VA10:%.*]] = bitcast i8** [[VA]] to i8*
// CHECK-NEXT: call void @llvm.va_end(i8* [[VA10]])
int