Skip to content

Commit 4d3f0b6

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 9a1fad3 commit 4d3f0b6

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
@@ -504,6 +504,12 @@ class IRBuilderBase {
504504
// Type creation methods
505505
//===--------------------------------------------------------------------===//
506506

507+
/// Fetch the type representing a byte.
508+
IntegerType *getByteTy() {
509+
const DataLayout &DL = BB->getDataLayout();
510+
return Type::getIntNTy(Context, DL.getByteWidth());
511+
}
512+
507513
/// Fetch the type representing a single bit
508514
IntegerType *getInt1Ty() {
509515
return Type::getInt1Ty(Context);
@@ -2001,12 +2007,12 @@ class IRBuilderBase {
20012007

20022008
Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
20032009
GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
2004-
return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW);
2010+
return CreateGEP(getByteTy(), Ptr, Offset, Name, NW);
20052011
}
20062012

20072013
Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
20082014
const Twine &Name = "") {
2009-
return CreateGEP(getInt8Ty(), Ptr, Offset, Name,
2015+
return CreateGEP(getByteTy(), Ptr, Offset, Name,
20102016
GEPNoWrapFlags::inBounds());
20112017
}
20122018

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

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

349348
// Account for the fact that on windows-msvc __start_* symbols actually
350349
// point to a uint64_t before the start of the array.
351-
auto GEP =
352-
IRB.CreatePtrAdd(SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
350+
Constant *GEP = ConstantExpr::getGetElementPtr(
351+
Int8Ty, SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
353352
return std::make_pair(GEP, SecEnd);
354353
}
355354

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)