diff options
author | Duncan Sands <baldrick@free.fr> | 2009-03-02 09:18:21 +0000 |
---|---|---|
committer | Duncan Sands <baldrick@free.fr> | 2009-03-02 09:18:21 +0000 |
commit | 5b7cfb02f7186dde37234e692a5f848adb68cc18 (patch) | |
tree | 54f5e651e02a569f68043419a2fb4dd4ed461a38 /lib | |
parent | 540d73f0cb32a3c520a6951c68393135008e8a17 (diff) | |
download | llvm-5b7cfb02f7186dde37234e692a5f848adb68cc18.tar.gz llvm-5b7cfb02f7186dde37234e692a5f848adb68cc18.tar.bz2 llvm-5b7cfb02f7186dde37234e692a5f848adb68cc18.tar.xz |
Fix PR3694: add an instcombine micro-optimization that helps
clean up when using variable length arrays in llvm-gcc.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@65832 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Transforms/Scalar/InstructionCombining.cpp | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 8e91b36d5d..7ddbb4ca7c 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -10766,15 +10766,25 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // 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 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { 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 |