Skip to content

Commit 6d95f98

Browse files
committed
[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.
1 parent 67765a6 commit 6d95f98

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,12 @@ class IRBuilderBase {
526526
// Type creation methods
527527
//===--------------------------------------------------------------------===//
528528

529+
/// Fetch the type representing a byte.
530+
IntegerType *getByteTy() {
531+
const DataLayout &DL = BB->getDataLayout();
532+
return Type::getIntNTy(Context, DL.getByteWidth());
533+
}
534+
529535
/// Fetch the type representing a single bit
530536
IntegerType *getInt1Ty() {
531537
return Type::getInt1Ty(Context);
@@ -1986,12 +1992,12 @@ class IRBuilderBase {
19861992

19871993
Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
19881994
GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
1989-
return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW);
1995+
return CreateGEP(getByteTy(), Ptr, Offset, Name, NW);
19901996
}
19911997

19921998
Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
19931999
const Twine &Name = "") {
1994-
return CreateGEP(getInt8Ty(), Ptr, Offset, Name,
2000+
return CreateGEP(getByteTy(), Ptr, Offset, Name,
19952001
GEPNoWrapFlags::inBounds());
19962002
}
19972003

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,13 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
345345
new GlobalVariable(M, Ty, false, Linkage, nullptr,
346346
getSectionEnd(Section));
347347
SecEnd->setVisibility(GlobalValue::HiddenVisibility);
348-
IRBuilder<> IRB(M.getContext());
349348
if (!TargetTriple.isOSBinFormatCOFF())
350349
return std::make_pair(SecStart, SecEnd);
351350

352351
// Account for the fact that on windows-msvc __start_* symbols actually
353352
// point to a uint64_t before the start of the array.
354-
auto GEP =
355-
IRB.CreatePtrAdd(SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
353+
Constant *GEP = ConstantExpr::getGetElementPtr(
354+
Int8Ty, SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
356355
return std::make_pair(GEP, SecEnd);
357356
}
358357

llvm/unittests/IR/IRBuilderTest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,14 @@ TEST_F(IRBuilderTest, DataLayout) {
517517
EXPECT_FALSE(M->getDataLayout().isLegalInteger(32));
518518
}
519519

520+
TEST_F(IRBuilderTest, GetByteTy) {
521+
IRBuilder<> Builder(BB);
522+
523+
EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(8));
524+
M->setDataLayout("b:32");
525+
EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(32));
526+
}
527+
520528
TEST_F(IRBuilderTest, GetIntTy) {
521529
IRBuilder<> Builder(BB);
522530
IntegerType *Ty1 = Builder.getInt1Ty();
@@ -528,6 +536,20 @@ TEST_F(IRBuilderTest, GetIntTy) {
528536
EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
529537
}
530538

539+
TEST_F(IRBuilderTest, CreatePtrAdd) {
540+
IRBuilder<> Builder(BB);
541+
542+
M->setDataLayout("b:16-p:32:32");
543+
Value *V = Builder.CreatePtrAdd(GV, ConstantInt::get(Ctx, APInt(32, 42)));
544+
ASSERT_TRUE(isa<GEPOperator>(V));
545+
EXPECT_TRUE(cast<GEPOperator>(V)->getResultElementType()->isIntegerTy(16));
546+
547+
M->setDataLayout("b:32-p:64:32");
548+
V = Builder.CreateInBoundsPtrAdd(GV, ConstantInt::get(Ctx, APInt(64, 42)));
549+
ASSERT_TRUE(isa<GEPOperator>(V));
550+
EXPECT_TRUE(cast<GEPOperator>(V)->getResultElementType()->isIntegerTy(32));
551+
}
552+
531553
TEST_F(IRBuilderTest, UnaryOperators) {
532554
IRBuilder<NoFolder> Builder(BB);
533555
Value *V = Builder.CreateLoad(GV->getValueType(), GV);

0 commit comments

Comments
 (0)