From dba73135c8b7a02afb535328a7475e0a6890c271 Mon Sep 17 00:00:00 2001 From: Augie Fackler Date: Tue, 25 Jan 2022 23:50:22 -0500 Subject: [PATCH] getAllocAlignment: respect allocalign attribute if present As with allocsize(), we prefer the table data to attributes. Differential Revision: https://reviews.llvm.org/D118263 --- llvm/include/llvm/Analysis/MemoryBuiltins.h | 5 ++++- llvm/lib/Analysis/MemoryBuiltins.cpp | 11 ++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/llvm/include/llvm/Analysis/MemoryBuiltins.h b/llvm/include/llvm/Analysis/MemoryBuiltins.h index 0d53239e3c8f..0c62731a7797 100644 --- a/llvm/include/llvm/Analysis/MemoryBuiltins.h +++ b/llvm/include/llvm/Analysis/MemoryBuiltins.h @@ -100,7 +100,10 @@ inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) { /// insertion or speculative execution of allocation routines. bool isAllocRemovable(const CallBase *V, const TargetLibraryInfo *TLI); -/// Gets the alignment argument for an aligned_alloc-like function +/// Gets the alignment argument for an aligned_alloc-like function, using either +/// built-in knowledge based on fuction names/signatures or allocalign +/// attributes. Note: the Value returned may not indicate a valid alignment, per +/// the definition of the allocalign attribute. Value *getAllocAlignment(const CallBase *V, const TargetLibraryInfo *TLI); /// Return the size of the requested allocation. With a trivial mapper, this is diff --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp index 6e7240579efa..4b7582b6e05e 100644 --- a/llvm/lib/Analysis/MemoryBuiltins.cpp +++ b/llvm/lib/Analysis/MemoryBuiltins.cpp @@ -334,10 +334,15 @@ Value *llvm::getAllocAlignment(const CallBase *V, assert(isAllocationFn(V, TLI)); const Optional FnData = getAllocationData(V, AnyAlloc, TLI); - if (!FnData.hasValue() || FnData->AlignParam < 0) { - return nullptr; + if (FnData.hasValue() && FnData->AlignParam >= 0) { + return V->getOperand(FnData->AlignParam); } - return V->getOperand(FnData->AlignParam); + unsigned AllocAlignParam; + if (V->getAttributes().hasAttrSomewhere(Attribute::AllocAlign, + &AllocAlignParam)) { + return V->getOperand(AllocAlignParam-1); + } + return nullptr; } /// When we're compiling N-bit code, and the user uses parameters that are