[llvm-commits] CVS: llvm/lib/VMCore/ConstantFolding.cpp

Chris Lattner lattner at cs.uiuc.edu
Sun Oct 10 20:57:44 PDT 2004



Changes in directory llvm/lib/VMCore:

ConstantFolding.cpp updated: 1.62 -> 1.63
---
Log message:

If we are trying to create a ConstantExpr cast that is really a GEP to the 
first element of an array, return a GEP instead of a cast.  This allows us
to transparently fold this:

int* getelementptr (int* cast ([100 x int]* %Gbody to int*), int 40)

into this:

int* getelementptr ([100 x int]* %Gbody, int 0, int 40)



---
Diffs of the changes:  (+11 -0)

Index: llvm/lib/VMCore/ConstantFolding.cpp
diff -u llvm/lib/VMCore/ConstantFolding.cpp:1.62 llvm/lib/VMCore/ConstantFolding.cpp:1.63
--- llvm/lib/VMCore/ConstantFolding.cpp:1.62	Sat Jul 17 18:47:01 2004
+++ llvm/lib/VMCore/ConstantFolding.cpp	Sun Oct 10 22:57:30 2004
@@ -562,6 +562,17 @@
         return ConstantExpr::getCast(CE->getOperand(0), DestTy);
     }
 
+  // Check to see if we are casting an array of X to a pointer to X.  If so, use
+  // a GEP to get to the first element of the array instead of a cast!
+  if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
+    if (const ArrayType *ATy = dyn_cast<ArrayType>(PTy->getElementType()))
+      if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy))
+        if (DPTy->getElementType() == ATy->getElementType()) {
+          std::vector<Constant*> IdxList(2,Constant::getNullValue(Type::IntTy));
+          return ConstantExpr::getGetElementPtr(const_cast<Constant*>(V),
+                                                IdxList);
+        }
+
   ConstRules &Rules = ConstRules::get(V, V);
 
   switch (DestTy->getTypeID()) {






More information about the llvm-commits mailing list