Skip to content

Commit 2a28c47

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 3b86337 commit 2a28c47

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);
@@ -1995,12 +2001,12 @@ class IRBuilderBase {
19952001

19962002
Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
19972003
GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
1998-
return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW);
2004+
return CreateGEP(getByteTy(), Ptr, Offset, Name, NW);
19992005
}
20002006

20012007
Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
20022008
const Twine &Name = "") {
2003-
return CreateGEP(getInt8Ty(), Ptr, Offset, Name,
2009+
return CreateGEP(getByteTy(), Ptr, Offset, Name,
20042010
GEPNoWrapFlags::inBounds());
20052011
}
20062012

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,13 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
328328
new GlobalVariable(M, Ty, false, Linkage, nullptr,
329329
getSectionEnd(Section));
330330
SecEnd->setVisibility(GlobalValue::HiddenVisibility);
331-
IRBuilder<> IRB(M.getContext());
332331
if (!TargetTriple.isOSBinFormatCOFF())
333332
return std::make_pair(SecStart, SecEnd);
334333

335334
// Account for the fact that on windows-msvc __start_* symbols actually
336335
// point to a uint64_t before the start of the array.
337-
auto GEP =
338-
IRB.CreatePtrAdd(SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
336+
Constant *GEP = ConstantExpr::getGetElementPtr(
337+
Int8Ty, SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
339338
return std::make_pair(GEP, SecEnd);
340339
}
341340

llvm/unittests/IR/IRBuilderTest.cpp

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

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

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

0 commit comments

Comments
 (0)