From 17c5ce914ce1bb9b8583a57c4fd566224104ecc0 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Wed, 13 Jul 2016 03:42:38 +0000 Subject: [PATCH] [IR] Make getIndexedOffsetInType return a signed result A GEPed offset can go negative, the result of getIndexedOffsetInType should according be a signed type. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275246 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/DataLayout.h | 2 +- lib/Analysis/ConstantFolding.cpp | 2 +- lib/Analysis/TypeMetadataUtils.cpp | 4 ++-- lib/IR/DataLayout.cpp | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/llvm/IR/DataLayout.h b/include/llvm/IR/DataLayout.h index ed71ae7c223..173121b72ff 100644 --- a/include/llvm/IR/DataLayout.h +++ b/include/llvm/IR/DataLayout.h @@ -440,7 +440,7 @@ public: /// /// Note that this takes the element type, not the pointer type. /// This is used to implement getelementptr. - uint64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef Indices) const; + int64_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 0a9d725db58..c6dae24a63b 100644 --- a/lib/Analysis/ConstantFolding.cpp +++ b/lib/Analysis/ConstantFolding.cpp @@ -920,7 +920,7 @@ Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, Type *DestTy, if (Instruction::isCast(Opcode)) return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL); - if(auto *GEP = dyn_cast(InstOrCE)) { + if (auto *GEP = dyn_cast(InstOrCE)) { if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI)) return C; diff --git a/lib/Analysis/TypeMetadataUtils.cpp b/lib/Analysis/TypeMetadataUtils.cpp index 8d173d77fb6..31e2b42075d 100644 --- a/lib/Analysis/TypeMetadataUtils.cpp +++ b/lib/Analysis/TypeMetadataUtils.cpp @@ -41,7 +41,7 @@ findCallsAtConstantOffset(SmallVectorImpl &DevirtCalls, static void findLoadCallsAtConstantOffset(Module *M, SmallVectorImpl &DevirtCalls, - Value *VPtr, uint64_t Offset) { + Value *VPtr, int64_t Offset) { for (const Use &U : VPtr->uses()) { Value *User = U.getUser(); if (isa(User)) { @@ -52,7 +52,7 @@ findLoadCallsAtConstantOffset(Module *M, // Take into account the GEP offset. if (VPtr == GEP->getPointerOperand() && GEP->hasAllConstantIndices()) { SmallVector Indices(GEP->op_begin() + 1, GEP->op_end()); - uint64_t GEPOffset = M->getDataLayout().getIndexedOffsetInType( + int64_t GEPOffset = M->getDataLayout().getIndexedOffsetInType( GEP->getSourceElementType(), Indices); findLoadCallsAtConstantOffset(M, DevirtCalls, User, Offset + GEPOffset); } diff --git a/lib/IR/DataLayout.cpp b/lib/IR/DataLayout.cpp index dc4b8981be7..20a15fb8831 100644 --- a/lib/IR/DataLayout.cpp +++ b/lib/IR/DataLayout.cpp @@ -723,9 +723,9 @@ unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const { return Max != LegalIntWidths.end() ? *Max : 0; } -uint64_t DataLayout::getIndexedOffsetInType(Type *ElemTy, - ArrayRef Indices) const { - uint64_t Result = 0; +int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy, + ArrayRef Indices) const { + int64_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. @@ -735,7 +735,7 @@ uint64_t DataLayout::getIndexedOffsetInType(Type *ElemTy, GTE = gep_type_end(ElemTy, AS, Indices); for (; GTI != GTE; ++GTI) { Value *Idx = GTI.getOperand(); - if (StructType *STy = dyn_cast(*GTI)) { + if (auto *STy = dyn_cast(*GTI)) { assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx"); unsigned FieldNo = cast(Idx)->getZExtValue(); @@ -747,7 +747,7 @@ uint64_t DataLayout::getIndexedOffsetInType(Type *ElemTy, } else { // Get the array index and the size of each array element. if (int64_t arrayIdx = cast(Idx)->getSExtValue()) - Result += (uint64_t)arrayIdx * getTypeAllocSize(GTI.getIndexedType()); + Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType()); } }