Don't eliminate bitcast instructions that change the type of a pointer
authorNate Begeman <natebegeman@mac.com>
Mon, 31 Mar 2008 00:22:16 +0000 (00:22 +0000)
committerNate Begeman <natebegeman@mac.com>
Mon, 31 Mar 2008 00:22:16 +0000 (00:22 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@48971 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp
lib/VMCore/ConstantFold.cpp

index 35715fb8caf9821f9d7cc30f6438602deefd0cb7..9e069fd05baf1b11c613217a86586b3a12111e87 100644 (file)
@@ -7575,6 +7575,11 @@ Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
     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))
index 7b21817cda46d8b93fd7918ab6e88deb0d840397..1f75fe57d61884421da7d33a4116bb81b6d195d4 100644 (file)
@@ -96,27 +96,29 @@ static Constant *FoldBitCast(Constant *V, const Type *DestTy) {
   // 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).