[cfe-commits] r106966 - in /cfe/trunk: lib/CodeGen/CGExpr.cpp test/CodeGen/address-space-field2.c test/CodeGen/address-space-field3.c test/CodeGen/address-space-field4.c test/CodeGen/exprs.c
Chris Lattner
sabre at nondot.org
Sat Jun 26 16:03:20 PDT 2010
Author: lattner
Date: Sat Jun 26 18:03:20 2010
New Revision: 106966
URL: http://llvm.org/viewvc/llvm-project?rev=106966&view=rev
Log:
Implement rdar://7530813 - collapse multiple GEP instructions in IRgen
This avoids generating two gep's for common array operations. Before
we would generate something like:
%tmp = load i32* %X.addr ; <i32> [#uses=1]
%arraydecay = getelementptr inbounds [100 x i32]* %A, i32 0, i32 0 ; <i32*> [#uses=1]
%arrayidx = getelementptr inbounds i32* %arraydecay, i32 %tmp ; <i32*> [#uses=1]
%tmp1 = load i32* %arrayidx ; <i32> [#uses=1]
Now we generate:
%tmp = load i32* %X.addr ; <i32> [#uses=1]
%arrayidx = getelementptr inbounds [100 x i32]* %A, i32 0, i32 %tmp ; <i32*> [#uses=1]
%tmp1 = load i32* %arrayidx ; <i32> [#uses=1]
Less IR is better at -O0.
Modified:
cfe/trunk/lib/CodeGen/CGExpr.cpp
cfe/trunk/test/CodeGen/address-space-field2.c
cfe/trunk/test/CodeGen/address-space-field3.c
cfe/trunk/test/CodeGen/address-space-field4.c
cfe/trunk/test/CodeGen/exprs.c
Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=106966&r1=106965&r2=106966&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Sat Jun 26 18:03:20 2010
@@ -1317,6 +1317,22 @@
return TrapBB;
}
+/// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an
+/// array to pointer, return the array subexpression.
+static const Expr *isSimpleArrayDecayOperand(const Expr *E) {
+ // If this isn't just an array->pointer decay, bail out.
+ const CastExpr *CE = dyn_cast<CastExpr>(E);
+ if (CE == 0 || CE->getCastKind() != CastExpr::CK_ArrayToPointerDecay)
+ return 0;
+
+ // If this is a decay from variable width array, bail out.
+ const Expr *SubExpr = CE->getSubExpr();
+ if (SubExpr->getType()->isVariableArrayType())
+ return 0;
+
+ return SubExpr;
+}
+
LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
// The index must always be an integer, which is not an aggregate. Emit it.
llvm::Value *Idx = EmitScalarExpr(E->getIdx());
@@ -1360,9 +1376,6 @@
}
}
- // The base must be a pointer, which is not an aggregate. Emit it.
- llvm::Value *Base = EmitScalarExpr(E->getBase());
-
// We know that the pointer points to a type of the correct size, unless the
// size is a VLA or Objective-C interface.
llvm::Value *Address = 0;
@@ -1378,9 +1391,13 @@
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");
- } else if (const ObjCObjectType *OIT =
- E->getType()->getAs<ObjCObjectType>()) {
+ } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){
+ // Indexing over an interface, as in "NSString *P; P[4];"
llvm::Value *InterfaceSize =
llvm::ConstantInt::get(Idx->getType(),
getContext().getTypeSizeInChars(OIT).getQuantity());
@@ -1388,10 +1405,28 @@
Idx = Builder.CreateMul(Idx, InterfaceSize);
const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
+
+ // The base must be a pointer, which is not an aggregate. Emit it.
+ llvm::Value *Base = EmitScalarExpr(E->getBase());
Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Idx, "arrayidx");
Address = Builder.CreateBitCast(Address, Base->getType());
+ } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
+ // If this is A[i] where A is an array, the frontend will have decayed the
+ // base to be a ArrayToPointerDecay implicit cast. While correct, it is
+ // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
+ // "gep x, i" here. Emit one "gep A, 0, i".
+ assert(Array->getType()->isArrayType() &&
+ "Array to pointer decay must have array source type!");
+ llvm::Value *ArrayPtr = EmitLValue(Array).getAddress();
+ const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
+ llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
+ llvm::Value *Args[] = { Zero, Idx };
+
+ Address = Builder.CreateInBoundsGEP(ArrayPtr, Args, Args+2, "arrayidx");
} else {
+ // 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");
}
Modified: cfe/trunk/test/CodeGen/address-space-field2.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/address-space-field2.c?rev=106966&r1=106965&r2=106966&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/address-space-field2.c (original)
+++ cfe/trunk/test/CodeGen/address-space-field2.c Sat Jun 26 18:03:20 2010
@@ -16,10 +16,6 @@
// CHECK: addrspace(1)
// CHECK: addrspace(1)
// CHECK: addrspace(1)
-// CHECK: addrspace(1)
-// CHECK: addrspace(1)
-// CHECK: addrspace(2)
-// CHECK: addrspace(2)
// CHECK: addrspace(2)
// CHECK: addrspace(2)
// CHECK: addrspace(2)
Modified: cfe/trunk/test/CodeGen/address-space-field3.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/address-space-field3.c?rev=106966&r1=106965&r2=106966&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/address-space-field3.c (original)
+++ cfe/trunk/test/CodeGen/address-space-field3.c Sat Jun 26 18:03:20 2010
@@ -16,10 +16,6 @@
// CHECK: addrspace(2)
// CHECK: addrspace(2)
// CHECK: addrspace(2)
-// CHECK: addrspace(2)
-// CHECK: addrspace(2)
-// CHECK: addrspace(1)
-// CHECK: addrspace(1)
// CHECK: addrspace(1)
// CHECK: addrspace(1)
// CHECK: addrspace(1)
Modified: cfe/trunk/test/CodeGen/address-space-field4.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/address-space-field4.c?rev=106966&r1=106965&r2=106966&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/address-space-field4.c (original)
+++ cfe/trunk/test/CodeGen/address-space-field4.c Sat Jun 26 18:03:20 2010
@@ -23,9 +23,6 @@
// CHECK: addrspace(3)
// CHECK: addrspace(3)
// CHECK: addrspace(1)
-// CHECK: addrspace(3)
-// CHECK: addrspace(3)
-// CHECK: addrspace(1)
// CHECK: addrspace(1)
// CHECK: addrspace(1)
// CHECK: addrspace(1)
@@ -35,9 +32,6 @@
// CHECK: addrspace(1)
// CHECK: addrspace(1)
// CHECK: addrspace(2)
-// CHECK: addrspace(1)
-// CHECK: addrspace(2)
-// CHECK: addrspace(2)
// CHECK: addrspace(2)
// Check the load and store are using the correct address space to access
Modified: cfe/trunk/test/CodeGen/exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/exprs.c?rev=106966&r1=106965&r2=106966&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/exprs.c (original)
+++ cfe/trunk/test/CodeGen/exprs.c Sat Jun 26 18:03:20 2010
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -emit-llvm -o -
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
// PR1895
// sizeof function
@@ -119,3 +119,15 @@
void f10() {
__builtin_sin(0);
}
+
+// rdar://7530813
+// CHECK: define i32 @f11
+int f11(long X) {
+ int A[100];
+ return A[X];
+
+// CHECK: load {{.*}}* %X.addr
+// CHECK-NEXT: getelementptr inbounds [100 x i32]* %A, i32 0,
+// CHECK-NEXT: load i32*
+}
+
More information about the cfe-commits
mailing list