diff --git a/include/llvm/IR/DataLayout.h b/include/llvm/IR/DataLayout.h index ebdc159d907..65a07066895 100644 --- a/include/llvm/IR/DataLayout.h +++ b/include/llvm/IR/DataLayout.h @@ -441,8 +441,9 @@ public: /// \brief Returns the offset from the beginning of the type for the specified /// indices. /// + /// Note that this takes the element type, not the pointer type. /// This is used to implement getelementptr. - uint64_t getIndexedOffset(Type *Ty, ArrayRef Indices) const; + uint64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef Indices) const; /// \brief Returns a StructLayout object, indicating the alignment of the /// struct, its size, and the offsets of its fields. diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp index 54dd0d51d12..a23fb75d4d7 100644 --- a/lib/Analysis/ConstantFolding.cpp +++ b/lib/Analysis/ConstantFolding.cpp @@ -772,8 +772,8 @@ static Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP, unsigned BitWidth = DL.getTypeSizeInBits(IntPtrTy); APInt Offset = APInt(BitWidth, - DL.getIndexedOffset( - Ptr->getType(), + DL.getIndexedOffsetInType( + SrcElemTy, makeArrayRef((Value * const *)Ops.data() + 1, Ops.size() - 1))); Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy); @@ -792,7 +792,8 @@ static Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP, break; Ptr = cast(GEP->getOperand(0)); - Offset += APInt(BitWidth, DL.getIndexedOffset(Ptr->getType(), NestedOps)); + SrcElemTy = GEP->getSourceElementType(); + Offset += APInt(BitWidth, DL.getIndexedOffsetInType(SrcElemTy, NestedOps)); Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy); } diff --git a/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp index 6888211c9fe..8abb5ab61fb 100644 --- a/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ b/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -192,14 +192,14 @@ DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE( addToAccelTable = true; // GV is a merged global. DIELoc *Loc = new (DIEValueAllocator) DIELoc; - Value *Ptr = CE->getOperand(0); - MCSymbol *Sym = Asm->getSymbol(cast(Ptr)); + auto *Ptr = cast(CE->getOperand(0)); + MCSymbol *Sym = Asm->getSymbol(Ptr); DD->addArangeLabel(SymbolCU(this, Sym)); addOpAddress(*Loc, Sym); addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); SmallVector Idx(CE->op_begin() + 1, CE->op_end()); addUInt(*Loc, dwarf::DW_FORM_udata, - Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); + Asm->getDataLayout().getIndexedOffsetInType(Ptr->getValueType(), Idx)); addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); } diff --git a/lib/IR/DataLayout.cpp b/lib/IR/DataLayout.cpp index 0f2e5ba5895..de3dfb0754d 100644 --- a/lib/IR/DataLayout.cpp +++ b/lib/IR/DataLayout.cpp @@ -723,40 +723,33 @@ unsigned DataLayout::getLargestLegalIntTypeSize() const { return Max != LegalIntWidths.end() ? *Max : 0; } -uint64_t DataLayout::getIndexedOffset(Type *ptrTy, - ArrayRef Indices) const { - Type *Ty = ptrTy; - assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()"); +uint64_t DataLayout::getIndexedOffsetInType(Type *ElemTy, + ArrayRef Indices) const { uint64_t Result = 0; // We can use 0 as the address space as we don't need // to get pointer types back from gep_type_iterator. unsigned AS = 0; generic_gep_type_iterator - TI = gep_type_begin(ptrTy->getPointerElementType(), AS, Indices); - for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX; - ++CurIDX, ++TI) { - if (StructType *STy = dyn_cast(*TI)) { - assert(Indices[CurIDX]->getType() == - Type::getInt32Ty(ptrTy->getContext()) && + GTI = gep_type_begin(ElemTy, AS, Indices), + GTE = gep_type_end(ElemTy, AS, Indices); + for (; GTI != GTE; ++GTI) { + Value *Idx = GTI.getOperand(); + if (StructType *STy = dyn_cast(*GTI)) { + assert(Idx->getType() == + Type::getInt32Ty(ElemTy->getContext()) && "Illegal struct idx"); - unsigned FieldNo = cast(Indices[CurIDX])->getZExtValue(); + unsigned FieldNo = cast(Idx)->getZExtValue(); // Get structure layout information... const StructLayout *Layout = getStructLayout(STy); // Add in the offset, as calculated by the structure layout info... Result += Layout->getElementOffset(FieldNo); - - // Update Ty to refer to current element - Ty = STy->getElementType(FieldNo); } else { - // Update Ty to refer to current element - Ty = cast(Ty)->getElementType(); - // Get the array index and the size of each array element. - if (int64_t arrayIdx = cast(Indices[CurIDX])->getSExtValue()) - Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty); + if (int64_t arrayIdx = cast(Idx)->getSExtValue()) + Result += (uint64_t)arrayIdx * getTypeAllocSize(GTI.getIndexedType()); } } diff --git a/lib/Transforms/Scalar/ScalarReplAggregates.cpp b/lib/Transforms/Scalar/ScalarReplAggregates.cpp index 30160fa8f1e..6633388ae14 100644 --- a/lib/Transforms/Scalar/ScalarReplAggregates.cpp +++ b/lib/Transforms/Scalar/ScalarReplAggregates.cpp @@ -524,8 +524,8 @@ bool ConvertToScalarInfo::CanConvertToScalar(Value *V, uint64_t Offset, HadDynamicAccess = true; } else GEPNonConstantIdx = NonConstantIdx; - uint64_t GEPOffset = DL.getIndexedOffset(GEP->getPointerOperandType(), - Indices); + uint64_t GEPOffset = DL.getIndexedOffsetInType(GEP->getSourceElementType(), + Indices); // See if all uses can be converted. if (!CanConvertToScalar(GEP, Offset+GEPOffset, GEPNonConstantIdx)) return false; @@ -619,8 +619,8 @@ void ConvertToScalarInfo::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, GEPNonConstantIdx = Indices.pop_back_val(); } else GEPNonConstantIdx = NonConstantIdx; - uint64_t GEPOffset = DL.getIndexedOffset(GEP->getPointerOperandType(), - Indices); + uint64_t GEPOffset = DL.getIndexedOffsetInType(GEP->getSourceElementType(), + Indices); ConvertUsesToScalar(GEP, NewAI, Offset+GEPOffset*8, GEPNonConstantIdx); GEP->eraseFromParent(); continue; @@ -1736,7 +1736,7 @@ void SROA::isSafeGEP(GetElementPtrInst *GEPI, Indices.pop_back(); const DataLayout &DL = GEPI->getModule()->getDataLayout(); - Offset += DL.getIndexedOffset(GEPI->getPointerOperandType(), Indices); + Offset += DL.getIndexedOffsetInType(GEPI->getSourceElementType(), Indices); if (!TypeHasComponent(Info.AI->getAllocatedType(), Offset, NonConstantIdxSize, DL)) MarkUnsafe(Info, GEPI); @@ -2052,7 +2052,7 @@ void SROA::RewriteGEP(GetElementPtrInst *GEPI, AllocaInst *AI, uint64_t Offset, Value* NonConstantIdx = nullptr; if (!GEPI->hasAllConstantIndices()) NonConstantIdx = Indices.pop_back_val(); - Offset += DL.getIndexedOffset(GEPI->getPointerOperandType(), Indices); + Offset += DL.getIndexedOffsetInType(GEPI->getSourceElementType(), Indices); RewriteForScalarRepl(GEPI, AI, Offset, NewElts);