[llvm-commits] [patch] minor instcombine+bitcast improvement

Dan Gohman djg at cray.com
Fri Apr 6 09:01:42 PDT 2007


This patch makes instcombine convert this:

     %t = getelementptr float* cast ([2 x i32]* %str to float*), i32 %V

into this:

     %t = getelementptr float* cast ([2 x i32]* %str to [2 x float]*), i32 0, i32 %V

instead of this:

     %s = getelementptr [2 x i32]* %str, i32 0, i32 %V
     %t = bitcast i32* %s to float*

I'm not yet familiar with instcombine's canonicalization style; is this
a desireable change? It doesn't cause any regressions for me.

Dan

-- 
Dan Gohman, Cray Inc.
-------------- next part --------------
Index: InstructionCombining.cpp
===================================================================
RCS file: /var/cvs/llvm/llvm/lib/Transforms/Scalar/InstructionCombining.cpp,v
retrieving revision 1.731
diff -t -d -u -p -5 -r1.731 InstructionCombining.cpp
--- InstructionCombining.cpp
+++ InstructionCombining.cpp
@@ -8113,23 +8113,24 @@ Instruction *InstCombiner::visitGetEleme
             // is a leading zero) we can fold the cast into this GEP.
             GEP.setOperand(0, X);
             return &GEP;
           }
     } else if (GEP.getNumOperands() == 2) {
-      // Transform things like:
-      // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V
-      // into:  %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast
+      // transform: GEP cast ([2 x int]* %str to float*), uint %V
+      // into     : GEP cast ([2 x int]* %str to [2 x float]*), int 0, uint %V
       const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
       const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
       if (isa<ArrayType>(SrcElTy) &&
           TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
           TD->getTypeSize(ResElTy)) {
+        const Type *NewArrayTy =
+            ArrayType::get(ResElTy,
+                           cast<ArrayType>(SrcElTy)->getNumElements());
         Value *V = InsertNewInstBefore(
-               new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty),
-                                     GEP.getOperand(1), GEP.getName()), GEP);
-        // V and GEP are both pointer types --> BitCast
-        return new BitCastInst(V, GEP.getType());
+               new BitCastInst(X, PointerType::get(NewArrayTy)), GEP);
+        return new GetElementPtrInst(V, Constant::getNullValue(Type::Int32Ty),
+                                     GEP.getOperand(1), GEP.getName());
       }
       
       // Transform things like:
       // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp
       //   (where tmp = 8*tmp2) into:


More information about the llvm-commits mailing list