git: c272602429d5 - stable/13 - Apply fix for clang incorrectly optimizing part of dns/bind916

From: Dimitry Andric <dim_at_FreeBSD.org>
Date: Sat, 18 Dec 2021 11:24:37 UTC
The branch stable/13 has been updated by dim:

URL: https://cgit.FreeBSD.org/src/commit/?id=c272602429d5d7efcea95d97a2ceb48f9d9d2b3c

commit c272602429d5d7efcea95d97a2ceb48f9d9d2b3c
Author:     Dimitry Andric <dim@FreeBSD.org>
AuthorDate: 2021-12-15 19:56:12 +0000
Commit:     Dimitry Andric <dim@FreeBSD.org>
CommitDate: 2021-12-18 11:24:06 +0000

    Apply fix for clang incorrectly optimizing part of dns/bind916
    
    Merge commit e5a8af7a90c6 from llvm git (by Gulfem Savrun Yeniceri):
    
      [Passes] Fix relative lookup table converter pass
    
      This patch fixes the relative table converter pass for the lookup table
      accesses that are resulted in an instruction sequence, where gep is not
      immediately followed by a load, such as gep being hoisted outside the loop
      or another instruction is inserted in between them. The fix inserts the
      call to load.relative.instrinsic in the original place of load instead of gep.
      Issue is reported by FreeBSD via https://bugs.freebsd.org/259921.
    
      Differential Revision: https://reviews.llvm.org/D115571
    
    PR:             259921
    Reported by:    O. Hartmann <freebsd@walstatt-de.de>
    
    (cherry picked from commit 5a925e4644665b9a7a5cdd664764fb0a4d1c5797)
---
 .../llvm/lib/Transforms/Utils/RelLookupTableConverter.cpp             | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/contrib/llvm-project/llvm/lib/Transforms/Utils/RelLookupTableConverter.cpp b/contrib/llvm-project/llvm/lib/Transforms/Utils/RelLookupTableConverter.cpp
index 85e5adaeaf5e..3127432dc6c9 100644
--- a/contrib/llvm-project/llvm/lib/Transforms/Utils/RelLookupTableConverter.cpp
+++ b/contrib/llvm-project/llvm/lib/Transforms/Utils/RelLookupTableConverter.cpp
@@ -144,6 +144,10 @@ static void convertToRelLookupTable(GlobalVariable &LookupTable) {
   Value *Offset =
       Builder.CreateShl(Index, ConstantInt::get(IntTy, 2), "reltable.shift");
 
+  // Insert the call to load.relative instrinsic before LOAD.
+  // GEP might not be immediately followed by a LOAD, like it can be hoisted
+  // outside the loop or another instruction might be inserted them in between.
+  Builder.SetInsertPoint(Load);
   Function *LoadRelIntrinsic = llvm::Intrinsic::getDeclaration(
       &M, Intrinsic::load_relative, {Index->getType()});
   Value *Base = Builder.CreateBitCast(RelLookupTable, Builder.getInt8PtrTy());