diff --git a/llvm/include/llvm/IR/Type.h b/llvm/include/llvm/IR/Type.h --- a/llvm/include/llvm/IR/Type.h +++ b/llvm/include/llvm/IR/Type.h @@ -483,6 +483,7 @@ /// Return a pointer to the current type. This is equivalent to /// PointerType::get(Foo, AddrSpace). + /// TODO: Remove this after opaque pointer transition is complete. PointerType *getPointerTo(unsigned AddrSpace = 0) const; private: diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp --- a/llvm/lib/IR/Type.cpp +++ b/llvm/lib/IR/Type.cpp @@ -725,8 +725,13 @@ setSubclassData(AddrSpace); } -PointerType *Type::getPointerTo(unsigned addrs) const { - return PointerType::get(const_cast(this), addrs); +PointerType *Type::getPointerTo(unsigned AddrSpace) const { + // Pointer to opaque pointer is opaque pointer. + if (auto *PTy = dyn_cast(this)) + if (PTy->isOpaque()) + return PointerType::get(getContext(), AddrSpace); + + return PointerType::get(const_cast(this), AddrSpace); } bool PointerType::isValidElementType(Type *ElemTy) { diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -463,11 +463,11 @@ Value *Ptr = LI.getPointerOperand(); unsigned AS = LI.getPointerAddressSpace(); + Type *NewPtrTy = NewTy->getPointerTo(AS); Value *NewPtr = nullptr; if (!(match(Ptr, m_BitCast(m_Value(NewPtr))) && - NewPtr->getType()->getPointerElementType() == NewTy && - NewPtr->getType()->getPointerAddressSpace() == AS)) - NewPtr = Builder.CreateBitCast(Ptr, NewTy->getPointerTo(AS)); + NewPtr->getType() == NewPtrTy)) + NewPtr = Builder.CreateBitCast(Ptr, NewPtrTy); LoadInst *NewLoad = Builder.CreateAlignedLoad( NewTy, NewPtr, LI.getAlign(), LI.isVolatile(), LI.getName() + Suffix); diff --git a/llvm/test/Transforms/InstCombine/opaque-ptr.ll b/llvm/test/Transforms/InstCombine/opaque-ptr.ll --- a/llvm/test/Transforms/InstCombine/opaque-ptr.ll +++ b/llvm/test/Transforms/InstCombine/opaque-ptr.ll @@ -87,3 +87,24 @@ ; ret ptr getelementptr (i8, ptr bitcast (i8* @g to ptr), i32 3) } + +define ptr @load_bitcast_1(ptr %a) { +; CHECK-LABEL: @load_bitcast_1( +; CHECK-NEXT: [[B1:%.*]] = load ptr, ptr [[A:%.*]], align 8 +; CHECK-NEXT: ret ptr [[B1]] +; + %b = load i8*, ptr %a + %c = bitcast i8* %b to ptr + ret ptr %c +} + +define ptr @load_bitcast_2(ptr %a) { +; CHECK-LABEL: @load_bitcast_2( +; CHECK-NEXT: [[C1:%.*]] = load ptr, ptr [[A:%.*]], align 8 +; CHECK-NEXT: ret ptr [[C1]] +; + %b = bitcast ptr %a to i8** + %c = load i8*, i8** %b + %d = bitcast i8* %c to ptr + ret ptr %d +}