Index: docs/LangRef.html =================================================================== --- docs/LangRef.html (revision 164147) +++ docs/LangRef.html (working copy) @@ -5944,9 +5946,9 @@ a non-aggregate first class type. The bit sizes of value and the destination type, ty2, must be identical. If the source type is a pointer, the destination type must also be - a pointer. This instruction supports bitwise conversion of vectors to - integers and to vectors of other types (as long as they have the same - size).

+ a pointer of the same size. This instruction supports bitwise conversion of + vectors to integers and to vectors of other types (as long as they have the + same size).

Semantics:

The 'bitcast' instruction converts value to type @@ -5954,9 +5956,10 @@ this conversion. The conversion is done as if the value had been stored to memory and read back as type ty2. Pointer (or vector of pointers) types may only be converted to other pointer - (or vector of pointers) types with this instruction. To convert - pointers to other types, use the inttoptr or - ptrtoint instructions first.

+ (or vector of pointers) types with this instruction if the pointer sizes are + equal. To convert pointers to other types, or pointers of different sizes, + use the inttoptr or + ptrtoint instructions first.

Example:
Index: include/llvm/AutoUpgrade.h
===================================================================
--- include/llvm/AutoUpgrade.h	(revision 164147)
+++ include/llvm/AutoUpgrade.h	(working copy)
@@ -19,6 +19,7 @@
   class GlobalVariable;
   class Function;
   class CallInst;
+  class BitCastInst;
 
   /// This is a more granular function that simply checks an intrinsic function 
   /// for upgrading, and returns true if it requires upgrading. It may return
@@ -39,6 +40,11 @@
   /// This checks for global variables which should be upgraded. It returns true
   /// if it requires upgrading.
   bool UpgradeGlobalVariable(GlobalVariable *GV);
+  
+  // Upgrade bitcast into a PtrToInt/IntToPtr sequence of instruction if
+  // the bitcast is to pointers between address spaces and the pointers
+  // are of different sizes.
+  void UpgradeBitCasts(BitCastInst *BCI, Module *M);
 } // End llvm namespace
 
 #endif
Index: lib/Bitcode/Reader/BitcodeReader.cpp
===================================================================
--- lib/Bitcode/Reader/BitcodeReader.cpp	(revision 164147)
+++ lib/Bitcode/Reader/BitcodeReader.cpp	(working copy)
@@ -25,6 +25,7 @@
 #include "llvm/Support/DataStream.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/InstIterator.h"
 #include "llvm/OperandTraits.h"
 using namespace llvm;
 
@@ -1404,6 +1405,10 @@
     Function *NewFn;
     if (UpgradeIntrinsicFunction(FI, NewFn))
       UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
+    for (inst_iterator ib = inst_begin(FI), ie = inst_end(FI); ib != ie;) {
+      Instruction *Inst = &(*ib); ib++;
+      UpgradeBitCasts(dyn_cast(Inst), TheModule);
+    }
   }
 
   // Look for global variables which need to be renamed.
Index: lib/VMCore/AutoUpgrade.cpp
===================================================================
--- lib/VMCore/AutoUpgrade.cpp	(revision 164147)
+++ lib/VMCore/AutoUpgrade.cpp	(working copy)
@@ -22,6 +22,8 @@
 #include "llvm/Support/CFG.h"
 #include "llvm/Support/CallSite.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/IRBuilder.h"
+#include "llvm/Target/TargetData.h"
 #include 
 using namespace llvm;
 
@@ -157,6 +159,45 @@
   return false;
 }
 
+// Upgrade bitcast into a PtrToInt/IntToPtr sequence of instruction if
+// the bitcast is to pointers between address spaces and the pointers
+// are of different sizes.
+void llvm::UpgradeBitCasts(BitCastInst *BCI, Module *M) {
+  if (!BCI) return;
+  Type *DstTy = BCI->getType();
+  Type *SrcTy = BCI->getOperand(0)->getType();
+  // Don't upgrade if both types are not pointers.
+  if (!DstTy->isPointerTy() || !SrcTy->isPointerTy()) 
+    return;
+
+  PointerType *DstPtrTy = dyn_cast(DstTy);
+  PointerType *SrcPtrTy = dyn_cast(SrcTy);
+  unsigned DstAS = DstPtrTy->getAddressSpace();
+  unsigned SrcAS = SrcPtrTy->getAddressSpace();
+  // Don't upgrade if address spaces are equal.
+  if (DstAS == SrcAS)
+    return;
+
+  const TargetData TD = TargetData::TargetData(M);
+  // Don't upgrade if the pointer sizes are equal.
+  if (TD.getPointerSizeInBits(/*DstAS*/) ==
+      TD.getPointerSizeInBits(/*SrcAS*/)) {
+    return;
+  }
+  // Now that we know we have a bitcast between pointers of different
+  // sizes, convert into PtrToInt/IntToPtr pair.
+  LLVMContext &C = BCI->getContext();
+  IRBuilder<> Builder(C);
+  Builder.SetInsertPoint(BCI->getParent(), BCI);
+  // FIXME: Change to use the address space once the 
+  // the target data changes have gone in.
+  Value *Rep = Builder.CreatePtrToInt(BCI->getOperand(0),
+      Type::getIntNTy(C, TD.getPointerSizeInBits(/*DstAS*/)));
+  Rep = Builder.CreateIntToPtr(Rep, SrcTy);
+  BCI->replaceAllUsesWith(Rep);
+  BCI->eraseFromParent();
+}
+
 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
 // upgraded intrinsic. All argument and return casting must be provided in
 // order to seamlessly integrate with existing context.
Index: lib/VMCore/Verifier.cpp
===================================================================
--- lib/VMCore/Verifier.cpp	(revision 164147)
+++ lib/VMCore/Verifier.cpp	(working copy)
@@ -71,6 +71,7 @@
 #include "llvm/Support/ConstantRange.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Target/TargetData.h"
 #include 
 #include 
 using namespace llvm;
@@ -1102,6 +1103,17 @@
           "Bitcast operand must not be aggregate", &I);
   Assert1(!DestTy->isAggregateType(),
           "Bitcast type must not be aggregate", &I);
+  if (DestTy->isPointerTy() && SrcTy->isPointerTy()) {
+    const TargetData TD(I.getParent()->getParent()->getParent());
+    PointerType *DstPtrTy = dyn_cast(DestTy);
+    PointerType *SrcPtrTy = dyn_cast(SrcTy);
+    unsigned DstAS = DstPtrTy->getAddressSpace();
+    unsigned SrcAS = SrcPtrTy->getAddressSpace();
+    Assert1(TD.getPointerSizeInBits(DstAS) ==
+        TD.getPointerSizeInBits(SrcAS),
+        "Bitcasts between pointers of different address spaces, must have "
+        "the same size pointers, otherwise use PtrToInt/IntToPtr.", &I);
+  }
 
   visitInstruction(I);
 }
Index: test/Bitcode/bitcast.ll
===================================================================
--- test/Bitcode/bitcast.ll	(revision 0)
+++ test/Bitcode/bitcast.ll	(revision 0)
@@ -0,0 +1,9 @@
+; Test to make sure that bitcast between address spaces are auto-upgraded.
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+; CHECK: inttoptr i32 addrspace(0)* %A to i32
+; CHECK-NEXT: ptrtoint i32 %0 to i32 addrspace(1)*
+; CHECK-NEXT: return
+target datalayout = "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:32:64-v128:32:128-a0:0:32-n32"
+define i32 addrspace(1)* @bitcast_0_to_1(i32 addrspace(0) *%A) {
+   return bitcast i32 addrspace(0)* %A to i32 addrspace(1)*
+}