[llvm-commits] [llvm] r57442 - /llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Matthijs Kooijman matthijs at stdin.nl
Mon Oct 13 08:17:02 PDT 2008


Author: matthijs
Date: Mon Oct 13 10:17:01 2008
New Revision: 57442

URL: http://llvm.org/viewvc/llvm-project?rev=57442&view=rev
Log:
Make InstructionCombining::getBitCastOperand() recognize GEP instructions and
constant expression with all zero indices as being the same as a bitcast.

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=57442&r1=57441&r2=57442&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Mon Oct 13 10:17:01 2008
@@ -436,14 +436,34 @@
   return Ty;
 }
 
-/// getBitCastOperand - If the specified operand is a CastInst or a constant 
-/// expression bitcast,  return the operand value, otherwise return null.
+/// getBitCastOperand - If the specified operand is a CastInst, a constant
+/// expression bitcast, or a GetElementPtrInst with all zero indices, return the
+/// operand value, otherwise return null.
 static Value *getBitCastOperand(Value *V) {
   if (BitCastInst *I = dyn_cast<BitCastInst>(V))
+    // BitCastInst?
     return I->getOperand(0);
-  else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
+  else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
+    // GetElementPtrInst?
+    if (GEP->hasAllZeroIndices())
+      return GEP->getOperand(0);
+  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
     if (CE->getOpcode() == Instruction::BitCast)
+      // BitCast ConstantExp?
       return CE->getOperand(0);
+    else if (CE->getOpcode() == Instruction::GetElementPtr) {
+      // GetElementPtr ConstantExp?
+      for (User::op_iterator I = CE->op_begin() + 1, E = CE->op_end();
+           I != E; ++I) {
+        ConstantInt *CI = dyn_cast<ConstantInt>(I);
+        if (!CI || !CI->isZero())
+          // Any non-zero indices? Not cast-like.
+          return 0;
+      }
+      // All-zero indices? This is just like casting.
+      return CE->getOperand(0);
+    }
+  }
   return 0;
 }
 





More information about the llvm-commits mailing list