diff options
author | Nate Begeman <natebegeman@mac.com> | 2008-03-31 00:22:16 +0000 |
---|---|---|
committer | Nate Begeman <natebegeman@mac.com> | 2008-03-31 00:22:16 +0000 |
commit | df5b3617cfe24135225e6052cc9d8603682b023e (patch) | |
tree | f4a3fa7b8c2fc4d0a222c6c9278f930dc0eedd1d /lib/VMCore/ConstantFold.cpp | |
parent | 5f49cb25f3e7e5df3959c7d9e0f3b89a522797ba (diff) | |
download | external_llvm-df5b3617cfe24135225e6052cc9d8603682b023e.tar.gz external_llvm-df5b3617cfe24135225e6052cc9d8603682b023e.tar.bz2 external_llvm-df5b3617cfe24135225e6052cc9d8603682b023e.zip |
Don't eliminate bitcast instructions that change the type of a pointer
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@48971 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore/ConstantFold.cpp')
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 7b21817cda..1f75fe57d6 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -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). |