Skip to content

Commit dba7313

Browse files
committed
getAllocAlignment: respect allocalign attribute if present
As with allocsize(), we prefer the table data to attributes. Differential Revision: https://reviews.llvm.org/D118263
1 parent b32735d commit dba7313

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

llvm/include/llvm/Analysis/MemoryBuiltins.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
100100
/// insertion or speculative execution of allocation routines.
101101
bool isAllocRemovable(const CallBase *V, const TargetLibraryInfo *TLI);
102102

103-
/// Gets the alignment argument for an aligned_alloc-like function
103+
/// Gets the alignment argument for an aligned_alloc-like function, using either
104+
/// built-in knowledge based on fuction names/signatures or allocalign
105+
/// attributes. Note: the Value returned may not indicate a valid alignment, per
106+
/// the definition of the allocalign attribute.
104107
Value *getAllocAlignment(const CallBase *V, const TargetLibraryInfo *TLI);
105108

106109
/// Return the size of the requested allocation. With a trivial mapper, this is

llvm/lib/Analysis/MemoryBuiltins.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,15 @@ Value *llvm::getAllocAlignment(const CallBase *V,
334334
assert(isAllocationFn(V, TLI));
335335

336336
const Optional<AllocFnsTy> FnData = getAllocationData(V, AnyAlloc, TLI);
337-
if (!FnData.hasValue() || FnData->AlignParam < 0) {
338-
return nullptr;
337+
if (FnData.hasValue() && FnData->AlignParam >= 0) {
338+
return V->getOperand(FnData->AlignParam);
339+
}
340+
unsigned AllocAlignParam;
341+
if (V->getAttributes().hasAttrSomewhere(Attribute::AllocAlign,
342+
&AllocAlignParam)) {
343+
return V->getOperand(AllocAlignParam-1);
339344
}
340-
return V->getOperand(FnData->AlignParam);
345+
return nullptr;
341346
}
342347

343348
/// When we're compiling N-bit code, and the user uses parameters that are

0 commit comments

Comments
 (0)