[llvm-commits] [llvm] r65832 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/2009-03-02-VarLengthArrayGEP.ll

Duncan Sands baldrick at free.fr
Mon Mar 2 01:18:21 PST 2009


Author: baldrick
Date: Mon Mar  2 03:18:21 2009
New Revision: 65832

URL: http://llvm.org/viewvc/llvm-project?rev=65832&view=rev
Log:
Fix PR3694: add an instcombine micro-optimization that helps
clean up when using variable length arrays in llvm-gcc.

Added:
    llvm/trunk/test/Transforms/InstCombine/2009-03-02-VarLengthArrayGEP.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=65832&r1=65831&r2=65832&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Mon Mar  2 03:18:21 2009
@@ -10766,15 +10766,25 @@
       // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
       // into     : GEP [10 x i8]* X, i32 0, ...
       //
+      // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ...
+      //           into     : GEP i8* X, ...
+      // 
       // This occurs when the program declares an array extern like "int X[];"
-      //
       const PointerType *CPTy = cast<PointerType>(PtrOp->getType());
       const PointerType *XTy = cast<PointerType>(X->getType());
-      if (const ArrayType *XATy =
-          dyn_cast<ArrayType>(XTy->getElementType()))
-        if (const ArrayType *CATy =
-            dyn_cast<ArrayType>(CPTy->getElementType()))
+      if (const ArrayType *CATy =
+          dyn_cast<ArrayType>(CPTy->getElementType())) {
+        // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ?
+        if (CATy->getElementType() == XTy->getElementType()) {
+          // -> GEP i8* X, ...
+          SmallVector<Value*, 8> Indices(GEP.idx_begin()+1, GEP.idx_end());
+          return GetElementPtrInst::Create(X, Indices.begin(), Indices.end(),
+                                           GEP.getName());
+        } else if (const ArrayType *XATy =
+                 dyn_cast<ArrayType>(XTy->getElementType())) {
+          // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ?
           if (CATy->getElementType() == XATy->getElementType()) {
+            // -> GEP [10 x i8]* X, i32 0, ...
             // At this point, we know that the cast source type is a pointer
             // to an array of the same type as the destination pointer
             // array.  Because the array type is never stepped over (there
@@ -10782,6 +10792,8 @@
             GEP.setOperand(0, X);
             return &GEP;
           }
+        }
+      }
     } else if (GEP.getNumOperands() == 2) {
       // Transform things like:
       // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V

Added: llvm/trunk/test/Transforms/InstCombine/2009-03-02-VarLengthArrayGEP.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/2009-03-02-VarLengthArrayGEP.ll?rev=65832&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/2009-03-02-VarLengthArrayGEP.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/2009-03-02-VarLengthArrayGEP.ll Mon Mar  2 03:18:21 2009
@@ -0,0 +1,14 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep {getelementptr i32}
+; PR3694
+
+define i32 @e(i32 %m, i32 %n) nounwind {
+entry:
+	%0 = alloca i32, i32 %n, align 4		; <i32*> [#uses=2]
+	%1 = bitcast i32* %0 to [0 x i32]*		; <[0 x i32]*> [#uses=1]
+	call void @f(i32* %0) nounwind
+	%2 = getelementptr [0 x i32]* %1, i32 0, i32 %m		; <i32*> [#uses=1]
+	%3 = load i32* %2, align 4		; <i32> [#uses=1]
+	ret i32 %3
+}
+
+declare void @f(i32*)





More information about the llvm-commits mailing list