Skip to content

Commit b8c2041

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 555e531 commit b8c2041

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);
@@ -2022,12 +2028,12 @@ class IRBuilderBase {
20222028

20232029
Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
20242030
GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
2025-
return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW);
2031+
return CreateGEP(getByteTy(), Ptr, Offset, Name, NW);
20262032
}
20272033

20282034
Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
20292035
const Twine &Name = "") {
2030-
return CreateGEP(getInt8Ty(), Ptr, Offset, Name,
2036+
return CreateGEP(getByteTy(), Ptr, Offset, Name,
20312037
GEPNoWrapFlags::inBounds());
20322038
}
20332039

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
@@ -533,6 +533,14 @@ TEST_F(IRBuilderTest, DataLayout) {
533533
EXPECT_FALSE(M->getDataLayout().isLegalInteger(32));
534534
}
535535

536+
TEST_F(IRBuilderTest, GetByteTy) {
537+
IRBuilder<> Builder(BB);
538+
539+
EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(8));
540+
M->setDataLayout("b:32");
541+
EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(32));
542+
}
543+
536544
TEST_F(IRBuilderTest, GetIntTy) {
537545
IRBuilder<> Builder(BB);
538546
IntegerType *Ty1 = Builder.getInt1Ty();
@@ -544,6 +552,20 @@ TEST_F(IRBuilderTest, GetIntTy) {
544552
EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
545553
}
546554

555+
TEST_F(IRBuilderTest, CreatePtrAdd) {
556+
IRBuilder<> Builder(BB);
557+
558+
M->setDataLayout("b:16-p:32:32");
559+
Value *V = Builder.CreatePtrAdd(GV, ConstantInt::get(Ctx, APInt(32, 42)));
560+
ASSERT_TRUE(isa<GEPOperator>(V));
561+
EXPECT_TRUE(cast<GEPOperator>(V)->getResultElementType()->isIntegerTy(16));
562+
563+
M->setDataLayout("b:32-p:64:32");
564+
V = Builder.CreateInBoundsPtrAdd(GV, ConstantInt::get(Ctx, APInt(64, 42)));
565+
ASSERT_TRUE(isa<GEPOperator>(V));
566+
EXPECT_TRUE(cast<GEPOperator>(V)->getResultElementType()->isIntegerTy(32));
567+
}
568+
547569
TEST_F(IRBuilderTest, UnaryOperators) {
548570
IRBuilder<NoFolder> Builder(BB);
549571
Value *V = Builder.CreateLoad(GV->getValueType(), GV);

0 commit comments

Comments
 (0)