From a67d5abd53e7f93e6412b6c25f85a5491daf711b Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Tue, 5 May 2015 18:03:48 +0000 Subject: [PATCH] [opaque pointer type] Track explicit GEP pointee type through in-memory IR git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@236510 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/Instructions.h | 37 +++++++++++++------ lib/IR/Instructions.cpp | 9 +++-- .../InstCombine/InstructionCombining.cpp | 1 + lib/Transforms/Utils/ValueMapper.cpp | 3 ++ 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/include/llvm/IR/Instructions.h b/include/llvm/IR/Instructions.h index 6db34038083..ab1bf647dea 100644 --- a/include/llvm/IR/Instructions.h +++ b/include/llvm/IR/Instructions.h @@ -801,6 +801,8 @@ inline Type *checkGEPType(Type *Ty) { /// access elements of arrays and structs /// class GetElementPtrInst : public Instruction { + Type *SourceElementType; + GetElementPtrInst(const GetElementPtrInst &GEPI); void init(Value *Ptr, ArrayRef IdxList, const Twine &NameStr); @@ -823,6 +825,13 @@ public: const Twine &NameStr = "", Instruction *InsertBefore = nullptr) { unsigned Values = 1 + unsigned(IdxList.size()); + if (!PointeeType) + PointeeType = + cast(Ptr->getType()->getScalarType())->getElementType(); + else + assert( + PointeeType == + cast(Ptr->getType()->getScalarType())->getElementType()); return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, NameStr, InsertBefore); } @@ -831,6 +840,13 @@ public: const Twine &NameStr, BasicBlock *InsertAtEnd) { unsigned Values = 1 + unsigned(IdxList.size()); + if (!PointeeType) + PointeeType = + cast(Ptr->getType()->getScalarType())->getElementType(); + else + assert( + PointeeType == + cast(Ptr->getType()->getScalarType())->getElementType()); return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, NameStr, InsertAtEnd); } @@ -876,10 +892,9 @@ public: return cast(Instruction::getType()); } - Type *getSourceElementType() const { - return cast(getPointerOperandType()->getScalarType()) - ->getElementType(); - } + Type *getSourceElementType() const { return SourceElementType; } + + void setSourceElementType(Type *Ty) { SourceElementType = Ty; } Type *getResultElementType() const { return cast(getType()->getScalarType())->getElementType(); @@ -1002,23 +1017,21 @@ GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, ArrayRef IdxList, unsigned Values, const Twine &NameStr, Instruction *InsertBefore) - : Instruction(PointeeType ? getGEPReturnType(PointeeType, Ptr, IdxList) - : getGEPReturnType(Ptr, IdxList), - GetElementPtr, + : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, OperandTraits::op_end(this) - Values, - Values, InsertBefore) { + Values, InsertBefore), + SourceElementType(PointeeType) { init(Ptr, IdxList, NameStr); - assert(!PointeeType || PointeeType == getSourceElementType()); } GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, ArrayRef IdxList, unsigned Values, const Twine &NameStr, BasicBlock *InsertAtEnd) - : Instruction(getGEPReturnType(Ptr, IdxList), GetElementPtr, + : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, OperandTraits::op_end(this) - Values, - Values, InsertAtEnd) { + Values, InsertAtEnd), + SourceElementType(PointeeType) { init(Ptr, IdxList, NameStr); - assert(!PointeeType || PointeeType == getSourceElementType()); } diff --git a/lib/IR/Instructions.cpp b/lib/IR/Instructions.cpp index e46db2c4850..3e681380e9b 100644 --- a/lib/IR/Instructions.cpp +++ b/lib/IR/Instructions.cpp @@ -1243,10 +1243,11 @@ void GetElementPtrInst::init(Value *Ptr, ArrayRef IdxList, } GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) - : Instruction(GEPI.getType(), GetElementPtr, - OperandTraits::op_end(this) - - GEPI.getNumOperands(), - GEPI.getNumOperands()) { + : Instruction(GEPI.getType(), GetElementPtr, + OperandTraits::op_end(this) - + GEPI.getNumOperands(), + GEPI.getNumOperands()), + SourceElementType(GEPI.SourceElementType) { std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); SubclassOptionalData = GEPI.SubclassOptionalData; } diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index f62941fbf64..53ab81dbef2 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1602,6 +1602,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // is a leading zero) we can fold the cast into this GEP. if (StrippedPtrTy->getAddressSpace() == GEP.getAddressSpace()) { GEP.setOperand(0, StrippedPtr); + GEP.setSourceElementType(XATy); return &GEP; } // Cannot replace the base pointer directly because StrippedPtr's diff --git a/lib/Transforms/Utils/ValueMapper.cpp b/lib/Transforms/Utils/ValueMapper.cpp index 5c1518d72ac..cac80accc32 100644 --- a/lib/Transforms/Utils/ValueMapper.cpp +++ b/lib/Transforms/Utils/ValueMapper.cpp @@ -400,5 +400,8 @@ void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap, } if (auto *AI = dyn_cast(I)) AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType())); + if (auto *GEP = dyn_cast(I)) + GEP->setSourceElementType( + TypeMapper->remapType(GEP->getSourceElementType())); I->mutateType(TypeMapper->remapType(I->getType())); } -- 2.34.1