From c940b116a75fe61494e9be41b0fa967ac421d42e Mon Sep 17 00:00:00 2001 From: Pietro Fezzardi Date: Fri, 16 Feb 2018 10:31:48 +0100 Subject: [PATCH] Use `getTypeAllocSize()` instead of `getTypeSizeInBits()` The function `getTypeSizeInBits()` was wrongly used in many places when reasoning about memory allocation, memory accesses, and memory offsets, The result was often divided by 8 (possibly losing spare bits) or even multiplied by 8, which makes no sense. These uses were error prone, even if they didn't cause problems yet. The `getTypeAllocSize()` is better suited for these uses, because it returns the number of bytes necessary to allocate an object of the given Type. --- generatedcodebasicinfo.cpp | 2 +- memoryaccess.h | 4 ++-- variablemanager.cpp | 7 +++---- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/generatedcodebasicinfo.cpp b/generatedcodebasicinfo.cpp index bfba628d8..f53287866 100644 --- a/generatedcodebasicinfo.cpp +++ b/generatedcodebasicinfo.cpp @@ -41,7 +41,7 @@ bool GeneratedCodeBasicInfo::runOnFunction(llvm::Function &F) { SP = M->getGlobalVariable(QMD.extract(Tuple, 3), true); Type *PCType = PC->getType()->getPointerElementType(); - PCRegSize = M->getDataLayout().getTypeSizeInBits(PCType) / 8; + PCRegSize = M->getDataLayout().getTypeAllocSize(PCType); for (BasicBlock &BB : F) { if (!BB.empty()) { diff --git a/memoryaccess.h b/memoryaccess.h index 6e752eaf6..2cd6b39ab 100644 --- a/memoryaccess.h +++ b/memoryaccess.h @@ -27,7 +27,7 @@ public: if (CacheIt != Cache.end()) { return CacheIt->second; } else { - auto Result = DL.getTypeSizeInBits(T) * 8; + auto Result = DL.getTypeAllocSize(T); Cache[T] = Result; return Result; } @@ -161,7 +161,7 @@ private: llvm::Value *PointeeValue, const llvm::DataLayout &DL) { // Set the size - Size = DL.getTypeSizeInBits(PointeeValue->getType()) * 8; + Size = DL.getTypeAllocSize(PointeeValue->getType()); initialize(Pointer); } diff --git a/variablemanager.cpp b/variablemanager.cpp index e1b4d7ae2..1574aa398 100644 --- a/variablemanager.cpp +++ b/variablemanager.cpp @@ -139,7 +139,7 @@ bool CorrectCPUStateUsagePass::runOnModule(Module& TheModule) { bool Success = false; if (Load != nullptr) { - unsigned Size = DL.getTypeSizeInBits(TheUser->getType()) / 8; + unsigned Size = DL.getTypeAllocSize(TheUser->getType()); assert(Size != 0); unsigned CurrentEnvOffset = CurrentOffset - EnvOffset; @@ -151,7 +151,7 @@ bool CorrectCPUStateUsagePass::runOnModule(Module& TheModule) { TheUser->replaceAllUsesWith(Loaded); } else { Value *ToStore = Store->getValueOperand(); - unsigned Size = DL.getTypeSizeInBits(ToStore->getType()) / 8; + unsigned Size = DL.getTypeAllocSize(ToStore->getType()); assert(Size != 0); unsigned CurrentEnvOffset = CurrentOffset - EnvOffset; @@ -262,7 +262,7 @@ bool CorrectCPUStateUsagePass::runOnModule(Module& TheModule) { } Type *PointeeTy = Var->getType()->getPointerElementType(); - uint64_t Size = DL.getTypeSizeInBits(PointeeTy) / 8; + uint64_t Size = DL.getTypeAllocSize(PointeeTy); Value *Address = Builder.CreateAdd(Builder.getInt64(Offset), BasePtr); @@ -561,7 +561,6 @@ VariableManager::VariableManager(Module& TheModule, if (startsWith(HelperFunction.getName(), HelperPrefix) && HelperFunction.getFunctionType()->getNumParams() > 1) { - for (Type *Candidate : HelperType->params()) { Structs.insert(dyn_cast(Candidate)); if (Candidate->isPointerTy()) {