From 3a2ea342164d6d52e154290f16d792f84122caa8 Mon Sep 17 00:00:00 2001 From: Argiris Kirtzidis Date: Tue, 9 Nov 2010 01:30:48 +0000 Subject: [PATCH] Fix miscompilation regarding VLAs; subscription of VLA pointers was incorrect. Fixes rdar://8644873 & http://llvm.org/PR8567. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@118468 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExpr.cpp | 11 ++++------- test/CodeGen/vla.c | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 1d86036c7..355c18f79 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -1433,17 +1433,14 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { Idx = Builder.CreateMul(Idx, VLASize); - QualType BaseType = getContext().getBaseElementType(VAT); + const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); - CharUnits BaseTypeSize = getContext().getTypeSizeInChars(BaseType); - Idx = Builder.CreateUDiv(Idx, - llvm::ConstantInt::get(Idx->getType(), - BaseTypeSize.getQuantity())); - // The base must be a pointer, which is not an aggregate. Emit it. llvm::Value *Base = EmitScalarExpr(E->getBase()); - Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx"); + Address = Builder.CreateInBoundsGEP(Builder.CreateBitCast(Base, i8PTy), + Idx, "arrayidx"); + Address = Builder.CreateBitCast(Address, Base->getType()); } else if (const ObjCObjectType *OIT = E->getType()->getAs()){ // Indexing over an interface, as in "NSString *P; P[4];" llvm::Value *InterfaceSize = diff --git a/test/CodeGen/vla.c b/test/CodeGen/vla.c index fa7a22fd0..f1f5a70a5 100644 --- a/test/CodeGen/vla.c +++ b/test/CodeGen/vla.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s +// RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -o - | FileCheck %s int b(char* x); @@ -85,3 +85,16 @@ int test2(int n) return GLOB; } +// http://llvm.org/PR8567 +// CHECK: define double @test_PR8567 +double test_PR8567(int n, double (*p)[n][5]) { + // CHECK: store [[vla_type:.*]] %p, + // CHECK: load i32* %n + // CHECK-NEXT: mul i32 40 + // CHECK-NEXT: [[byte_idx:%.*]] = mul i32 1 + // CHECK-NEXT: [[tmp_1:%.*]] = load [[vla_type]]* + // CHECK-NEXT: [[tmp_2:%.*]] = bitcast [[vla_type]] [[tmp_1]] to i8* + // CHECK-NEXT: [[idx:%.*]] = getelementptr inbounds i8* [[tmp_2]], i32 [[byte_idx]] + // CHECK-NEXT: bitcast i8* [[idx]] to [[vla_type]] + return p[1][2][3]; +} -- 2.11.4.GIT