[llvm-commits] [llvm] r48971 - in /llvm/trunk/lib: Transforms/Scalar/InstructionCombining.cpp VMCore/ConstantFold.cpp

Nate Begeman natebegeman at mac.com
Sun Mar 30 17:22:16 PDT 2008


Author: sampo
Date: Sun Mar 30 19:22:16 2008
New Revision: 48971

URL: http://llvm.org/viewvc/llvm-project?rev=48971&view=rev
Log:
Don't eliminate bitcast instructions that change the type of a pointer

Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
    llvm/trunk/lib/VMCore/ConstantFold.cpp

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

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Sun Mar 30 19:22:16 2008
@@ -7575,6 +7575,11 @@
     const Type *DstElTy = DstPTy->getElementType();
     const Type *SrcElTy = SrcPTy->getElementType();
     
+    // If the address spaces don't match, don't eliminate the bitcast, which is
+    // required for changing types.
+    if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace())
+      return 0;
+    
     // If we are casting a malloc or alloca to a pointer to a type of the same
     // size, rewrite the allocation instruction to allocate the "right" type.
     if (AllocationInst *AI = dyn_cast<AllocationInst>(Src))

Modified: llvm/trunk/lib/VMCore/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/ConstantFold.cpp?rev=48971&r1=48970&r2=48971&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/ConstantFold.cpp (original)
+++ llvm/trunk/lib/VMCore/ConstantFold.cpp Sun Mar 30 19:22:16 2008
@@ -96,27 +96,29 @@
   // Check to see if we are casting a pointer to an aggregate to a pointer to
   // the first element.  If so, return the appropriate GEP instruction.
   if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
-    if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
-      SmallVector<Value*, 8> IdxList;
-      IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
-      const Type *ElTy = PTy->getElementType();
-      while (ElTy != DPTy->getElementType()) {
-        if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
-          if (STy->getNumElements() == 0) break;
-          ElTy = STy->getElementType(0);
-          IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
-        } else if (const SequentialType *STy = dyn_cast<SequentialType>(ElTy)) {
-          if (isa<PointerType>(ElTy)) break;  // Can't index into pointers!
-          ElTy = STy->getElementType();
-          IdxList.push_back(IdxList[0]);
-        } else {
-          break;
+    if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy))
+      if (PTy->getAddressSpace() == DPTy->getAddressSpace()) {
+        SmallVector<Value*, 8> IdxList;
+        IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
+        const Type *ElTy = PTy->getElementType();
+        while (ElTy != DPTy->getElementType()) {
+          if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
+            if (STy->getNumElements() == 0) break;
+            ElTy = STy->getElementType(0);
+            IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
+          } else if (const SequentialType *STy = 
+                     dyn_cast<SequentialType>(ElTy)) {
+            if (isa<PointerType>(ElTy)) break;  // Can't index into pointers!
+            ElTy = STy->getElementType();
+            IdxList.push_back(IdxList[0]);
+          } else {
+            break;
+          }
         }
+        
+        if (ElTy == DPTy->getElementType())
+          return ConstantExpr::getGetElementPtr(V, &IdxList[0], IdxList.size());
       }
-      
-      if (ElTy == DPTy->getElementType())
-        return ConstantExpr::getGetElementPtr(V, &IdxList[0], IdxList.size());
-    }
   
   // Handle casts from one vector constant to another.  We know that the src 
   // and dest type have the same size (otherwise its an illegal cast).





More information about the llvm-commits mailing list