https://github.com/s-barannikov updated https://github.com/llvm/llvm-project/pull/106539
>From 6d95f98f2de990ff8971400966de74f62ef339bb Mon Sep 17 00:00:00 2001 From: Sergei Barannikov <baranniko...@gmail.com> Date: Thu, 22 Aug 2024 15:10:58 +0300 Subject: [PATCH] [IRBuilder] Add getByteTy and use it in CreatePtrAdd The change requires DataLayout instance to be available, which, in turn, requires insertion point to be set. In-tree tests detected only one case when the function was called without setting an insertion point, it was changed to create a constant expression directly. --- llvm/include/llvm/IR/IRBuilder.h | 10 +++++++-- .../Instrumentation/SanitizerCoverage.cpp | 5 ++--- llvm/unittests/IR/IRBuilderTest.cpp | 22 +++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h index 833c91fd974619c..63a3f5182486943 100644 --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -526,6 +526,12 @@ class IRBuilderBase { // Type creation methods //===--------------------------------------------------------------------===// + /// Fetch the type representing a byte. + IntegerType *getByteTy() { + const DataLayout &DL = BB->getDataLayout(); + return Type::getIntNTy(Context, DL.getByteWidth()); + } + /// Fetch the type representing a single bit IntegerType *getInt1Ty() { return Type::getInt1Ty(Context); @@ -1986,12 +1992,12 @@ class IRBuilderBase { Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "", GEPNoWrapFlags NW = GEPNoWrapFlags::none()) { - return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW); + return CreateGEP(getByteTy(), Ptr, Offset, Name, NW); } Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "") { - return CreateGEP(getInt8Ty(), Ptr, Offset, Name, + return CreateGEP(getByteTy(), Ptr, Offset, Name, GEPNoWrapFlags::inBounds()); } diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp index ac033d92e30d854..23f7c0def69af7d 100644 --- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp +++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp @@ -345,14 +345,13 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section, new GlobalVariable(M, Ty, false, Linkage, nullptr, getSectionEnd(Section)); SecEnd->setVisibility(GlobalValue::HiddenVisibility); - IRBuilder<> IRB(M.getContext()); if (!TargetTriple.isOSBinFormatCOFF()) return std::make_pair(SecStart, SecEnd); // Account for the fact that on windows-msvc __start_* symbols actually // point to a uint64_t before the start of the array. - auto GEP = - IRB.CreatePtrAdd(SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t))); + Constant *GEP = ConstantExpr::getGetElementPtr( + Int8Ty, SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t))); return std::make_pair(GEP, SecEnd); } diff --git a/llvm/unittests/IR/IRBuilderTest.cpp b/llvm/unittests/IR/IRBuilderTest.cpp index 2fd52860e71b9f5..214eeddd24afd49 100644 --- a/llvm/unittests/IR/IRBuilderTest.cpp +++ b/llvm/unittests/IR/IRBuilderTest.cpp @@ -517,6 +517,14 @@ TEST_F(IRBuilderTest, DataLayout) { EXPECT_FALSE(M->getDataLayout().isLegalInteger(32)); } +TEST_F(IRBuilderTest, GetByteTy) { + IRBuilder<> Builder(BB); + + EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(8)); + M->setDataLayout("b:32"); + EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(32)); +} + TEST_F(IRBuilderTest, GetIntTy) { IRBuilder<> Builder(BB); IntegerType *Ty1 = Builder.getInt1Ty(); @@ -528,6 +536,20 @@ TEST_F(IRBuilderTest, GetIntTy) { EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize)); } +TEST_F(IRBuilderTest, CreatePtrAdd) { + IRBuilder<> Builder(BB); + + M->setDataLayout("b:16-p:32:32"); + Value *V = Builder.CreatePtrAdd(GV, ConstantInt::get(Ctx, APInt(32, 42))); + ASSERT_TRUE(isa<GEPOperator>(V)); + EXPECT_TRUE(cast<GEPOperator>(V)->getResultElementType()->isIntegerTy(16)); + + M->setDataLayout("b:32-p:64:32"); + V = Builder.CreateInBoundsPtrAdd(GV, ConstantInt::get(Ctx, APInt(64, 42))); + ASSERT_TRUE(isa<GEPOperator>(V)); + EXPECT_TRUE(cast<GEPOperator>(V)->getResultElementType()->isIntegerTy(32)); +} + TEST_F(IRBuilderTest, UnaryOperators) { IRBuilder<NoFolder> Builder(BB); Value *V = Builder.CreateLoad(GV->getValueType(), GV); _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits