diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst index 9b8aaa7a1fd2..bc4df0586460 100644 --- a/llvm/docs/LangRef.rst +++ b/llvm/docs/LangRef.rst @@ -6860,10 +6860,11 @@ Syntax: :: - = load [volatile] , * [, align ][, !nontemporal !][, !invariant.load !][, !invariant.group !][, !nonnull !][, !dereferenceable !][, !dereferenceable_or_null !] + = load [volatile] , * [, align ][, !nontemporal !][, !invariant.load !][, !invariant.group !][, !nonnull !][, !dereferenceable !][, !dereferenceable_or_null !][, !align !] = load atomic [volatile] * [singlethread] , align [, !invariant.group !] ! = !{ i32 1 } ! = !{i64 } + ! = !{ i64 } Overview: """"""""" @@ -6948,6 +6949,14 @@ value in the metadata node. This is analogous to the ''dereferenceable_or_null'' attribute on parameters and return values. This metadata can only be applied to loads of a pointer type. +The optional ``!align`` metadata must reference a single metadata name +```` corresponding to a metadata node with one ``i64`` entry. +The existence of the ``!align`` metadata on the instruction tells the +optimizer that the value loaded is known to be aligned to a boundary specified +by the integer value in the metadata node. The alignment must be a power of 2. +This is analogous to the ''align'' attribute on parameters and return values. +This metadata can only be applied to loads of a pointer type. + Semantics: """""""""" diff --git a/llvm/include/llvm/IR/LLVMContext.h b/llvm/include/llvm/IR/LLVMContext.h index 336aca2d5ba1..24e95f6a62fd 100644 --- a/llvm/include/llvm/IR/LLVMContext.h +++ b/llvm/include/llvm/IR/LLVMContext.h @@ -63,7 +63,8 @@ public: MD_dereferenceable_or_null = 13, // "dereferenceable_or_null" MD_make_implicit = 14, // "make.implicit" MD_unpredictable = 15, // "unpredictable" - MD_invariant_group = 16 // "invariant.group" + MD_invariant_group = 16, // "invariant.group" + MD_align = 17 // "align" }; /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 736077dc4776..5481e722e536 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -2979,6 +2979,11 @@ static bool isAligned(const Value *Base, APInt Offset, unsigned Align, BaseAlign = A->getParamAlignment(); else if (auto CS = ImmutableCallSite(Base)) BaseAlign = CS.getAttributes().getParamAlignment(AttributeSet::ReturnIndex); + else if (const LoadInst *LI = dyn_cast(Base)) + if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) { + ConstantInt *CI = mdconst::extract(MD->getOperand(0)); + BaseAlign = CI->getLimitedValue(); + } if (!BaseAlign) { Type *Ty = Base->getType()->getPointerElementType(); diff --git a/llvm/lib/IR/LLVMContext.cpp b/llvm/lib/IR/LLVMContext.cpp index ee971c1be194..cecc6335ef1b 100644 --- a/llvm/lib/IR/LLVMContext.cpp +++ b/llvm/lib/IR/LLVMContext.cpp @@ -123,6 +123,10 @@ LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { "invariant.group kind id drifted"); (void)InvariantGroupId; + // Create the 'align' metadata kind. + unsigned AlignID = getMDKindID("align"); + assert(AlignID == MD_align && "align kind id drifted"); + (void)AlignID; } LLVMContext::~LLVMContext() { delete pImpl; } diff --git a/llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll b/llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll index 550b01c4c9aa..c7ca21ab0614 100644 --- a/llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll +++ b/llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll @@ -122,6 +122,14 @@ entry: %load24 = load i32, i32* %deref_return, align 16 %load25 = load i32, i32* %deref_and_aligned_return, align 16 + ; Load from a dereferenceable and aligned load +; CHECK: %d4_unaligned_load{{.*}}(unaligned) +; CHECK: %d4_aligned_load{{.*}}(aligned) + %d4_unaligned_load = load i32*, i32** @globali32ptr, !dereferenceable !0 + %d4_aligned_load = load i32*, i32** @globali32ptr, !dereferenceable !0, !align !{i64 16} + %load26 = load i32, i32* %d4_unaligned_load, align 16 + %load27 = load i32, i32* %d4_aligned_load, align 16 + ret void }