From 2d1969584a85772e5e91c57339f1b9a87b8b5234 Mon Sep 17 00:00:00 2001 From: Robert Lytton Date: Fri, 11 Oct 2013 10:29:34 +0000 Subject: [PATCH] XCore : Fix bug in XCoreABIInfo::EmitVAArg() Incorrect handling of 'double' and 'long long int' llvm-svn: 192436 --- clang/lib/CodeGen/TargetInfo.cpp | 40 +++++++++++++++++++++----------- clang/test/CodeGen/xcore-abi.c | 27 ++++++++++++++------- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp index 9ad17a454efc..8b8c90b3efc5 100644 --- a/clang/lib/CodeGen/TargetInfo.cpp +++ b/clang/lib/CodeGen/TargetInfo.cpp @@ -5426,40 +5426,54 @@ public: XcoreTargetCodeGenInfo(CodeGenTypes &CGT) :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} }; -} // end anonymous namespace +} // End anonymous namespace. llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, CodeGenFunction &CGF) const { - ABIArgInfo AI = classifyArgumentType(Ty); CGBuilderTy &Builder = CGF.Builder; - llvm::Type *ArgTy = CGT.ConvertType(Ty); - if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) - AI.setCoerceToType(ArgTy); - // handle the VAList + // Get the VAList. llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, CGF.Int8PtrPtrTy); llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); - llvm::Value *APN = Builder.CreateConstGEP1_32(AP, 4); - Builder.CreateStore(APN, VAListAddrAsBPP); - // handle the argument + // Handle the argument. + ABIArgInfo AI = classifyArgumentType(Ty); + llvm::Type *ArgTy = CGT.ConvertType(Ty); + if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) + AI.setCoerceToType(ArgTy); llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); + llvm::Value *Val; + uint64_t ArgSize; switch (AI.getKind()) { case ABIArgInfo::Expand: llvm_unreachable("Unsupported ABI kind for va_arg"); case ABIArgInfo::Ignore: - return llvm::UndefValue::get(ArgPtrTy); + Val = llvm::UndefValue::get(ArgPtrTy); + ArgSize = 0; + break; case ABIArgInfo::Extend: case ABIArgInfo::Direct: - return Builder.CreatePointerCast(AP, ArgPtrTy); + Val = Builder.CreatePointerCast(AP, ArgPtrTy); + ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); + if (ArgSize < 4) + ArgSize = 4; + break; case ABIArgInfo::Indirect: llvm::Value *ArgAddr; ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); ArgAddr = Builder.CreateLoad(ArgAddr); - return Builder.CreatePointerCast(ArgAddr, ArgPtrTy); + Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); + ArgSize = 4; + break; } - llvm_unreachable("Unknown ABI kind"); + + // Increment the VAList. + if (ArgSize) { + llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); + Builder.CreateStore(APN, VAListAddrAsBPP); + } + return Val; } //===----------------------------------------------------------------------===// diff --git a/clang/test/CodeGen/xcore-abi.c b/clang/test/CodeGen/xcore-abi.c index 1b12ce41d3c4..56bec3d6b31b 100644 --- a/clang/test/CodeGen/xcore-abi.c +++ b/clang/test/CodeGen/xcore-abi.c @@ -20,9 +20,9 @@ void testva (int n, ...) { char* v1 = va_arg (ap, char*); f(v1); // CHECK: [[I:%[a-z0-9]+]] = load i8** [[AP]] + // CHECK: [[P:%[a-z0-9]+]] = bitcast i8* [[I]] to i8** // CHECK: [[IN:%[a-z0-9]+]] = getelementptr i8* [[I]], i32 4 // CHECK: store i8* [[IN]], i8** [[AP]] - // CHECK: [[P:%[a-z0-9]+]] = bitcast i8* [[I]] to i8** // CHECK: [[V1:%[a-z0-9]+]] = load i8** [[P]] // CHECK: store i8* [[V1]], i8** [[V:%[a-z0-9]+]], align 4 // CHECK: [[V2:%[a-z0-9]+]] = load i8** [[V]], align 4 @@ -40,9 +40,9 @@ void testva (int n, ...) { int v3 = va_arg (ap, int); f(&v3); // CHECK: [[I:%[a-z0-9]+]] = load i8** [[AP]] + // CHECK: [[P:%[a-z0-9]+]] = bitcast i8* [[I]] to i32* // CHECK: [[IN:%[a-z0-9]+]] = getelementptr i8* [[I]], i32 4 // CHECK: store i8* [[IN]], i8** [[AP]] - // CHECK: [[P:%[a-z0-9]+]] = bitcast i8* [[I]] to i32* // CHECK: [[V1:%[a-z0-9]+]] = load i32* [[P]] // CHECK: store i32 [[V1]], i32* [[V:%[a-z0-9]+]], align 4 // CHECK: [[V2:%[a-z0-9]+]] = bitcast i32* [[V]] to i8* @@ -51,9 +51,9 @@ void testva (int n, ...) { long long int v4 = va_arg (ap, long long int); f(&v4); // CHECK: [[I:%[a-z0-9]+]] = load i8** [[AP]] - // CHECK: [[IN:%[a-z0-9]+]] = getelementptr i8* [[I]], i32 4 - // CHECK: store i8* [[IN]], i8** [[AP]] // CHECK: [[P:%[a-z0-9]+]] = bitcast i8* [[I]] to i64* + // CHECK: [[IN:%[a-z0-9]+]] = getelementptr i8* [[I]], i32 8 + // CHECK: store i8* [[IN]], i8** [[AP]] // CHECK: [[V1:%[a-z0-9]+]] = load i64* [[P]] // CHECK: store i64 [[V1]], i64* [[V:%[a-z0-9]+]], align 8 // CHECK:[[V2:%[a-z0-9]+]] = bitcast i64* [[V]] to i8* @@ -62,10 +62,10 @@ void testva (int n, ...) { struct x v5 = va_arg (ap, struct x); // typical agregate type f(&v5); // CHECK: [[I:%[a-z0-9]+]] = load i8** [[AP]] - // CHECK: [[IN:%[a-z0-9]+]] = getelementptr i8* [[I]], i32 4 - // CHECK: store i8* [[IN]], i8** [[AP]] // CHECK: [[I2:%[a-z0-9]+]] = bitcast i8* [[I]] to %struct.x** // CHECK: [[P:%[a-z0-9]+]] = load %struct.x** [[I2]] + // CHECK: [[IN:%[a-z0-9]+]] = getelementptr i8* [[I]], i32 4 + // CHECK: store i8* [[IN]], i8** [[AP]] // CHECK: [[V1:%[a-z0-9]+]] = bitcast %struct.x* [[V:%[a-z0-9]+]] to i8* // CHECK: [[P1:%[a-z0-9]+]] = bitcast %struct.x* [[P]] to i8* // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[V1]], i8* [[P1]], i32 20, i32 4, i1 false) @@ -75,10 +75,10 @@ void testva (int n, ...) { int* v6 = va_arg (ap, int[4]); // an unusual agregate type f(v6); // CHECK: [[I:%[a-z0-9]+]] = load i8** [[AP]] - // CHECK: [[IN:%[a-z0-9]+]] = getelementptr i8* [[I]], i32 4 - // CHECK: store i8* [[IN]], i8** [[AP]] // CHECK: [[I2:%[a-z0-9]+]] = bitcast i8* [[I]] to [4 x i32]** // CHECK: [[P:%[a-z0-9]+]] = load [4 x i32]** [[I2]] + // CHECK: [[IN:%[a-z0-9]+]] = getelementptr i8* [[I]], i32 4 + // CHECK: store i8* [[IN]], i8** [[AP]] // CHECK: [[V1:%[a-z0-9]+]] = bitcast [4 x i32]* [[V0:%[a-z0-9]+]] to i8* // CHECK: [[P1:%[a-z0-9]+]] = bitcast [4 x i32]* [[P]] to i8* // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[V1]], i8* [[P1]], i32 16, i32 4, i1 false) @@ -87,6 +87,17 @@ void testva (int n, ...) { // CHECK: [[V3:%[a-z0-9]+]] = load i32** [[V]], align 4 // CHECK: [[V4:%[a-z0-9]+]] = bitcast i32* [[V3]] to i8* // CHECK: call void @f(i8* [[V4]]) + + double v7 = va_arg (ap, double); + f(&v7); + // CHECK: [[I:%[a-z0-9]+]] = load i8** [[AP]] + // CHECK: [[P:%[a-z0-9]+]] = bitcast i8* [[I]] to double* + // CHECK: [[IN:%[a-z0-9]+]] = getelementptr i8* [[I]], i32 8 + // CHECK: store i8* [[IN]], i8** [[AP]] + // CHECK: [[V1:%[a-z0-9]+]] = load double* [[P]] + // CHECK: store double [[V1]], double* [[V:%[a-z0-9]+]], align 8 + // CHECK: [[V2:%[a-z0-9]+]] = bitcast double* [[V]] to i8* + // CHECK: call void @f(i8* [[V2]]) } void testbuiltin (void) {