Skip to content

Commit caa10b7

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 4650b11 commit caa10b7

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
@@ -543,6 +543,12 @@ class IRBuilderBase {
543543
// Type creation methods
544544
//===--------------------------------------------------------------------===//
545545

546+
/// Fetch the type representing a byte.
547+
IntegerType *getByteTy() {
548+
const DataLayout &DL = BB->getDataLayout();
549+
return Type::getIntNTy(Context, DL.getByteWidth());
550+
}
551+
546552
/// Fetch the type representing a single bit
547553
IntegerType *getInt1Ty() {
548554
return Type::getInt1Ty(Context);
@@ -2040,12 +2046,12 @@ class IRBuilderBase {
20402046

20412047
Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
20422048
GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
2043-
return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW);
2049+
return CreateGEP(getByteTy(), Ptr, Offset, Name, NW);
20442050
}
20452051

20462052
Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
20472053
const Twine &Name = "") {
2048-
return CreateGEP(getInt8Ty(), Ptr, Offset, Name,
2054+
return CreateGEP(getByteTy(), Ptr, Offset, Name,
20492055
GEPNoWrapFlags::inBounds());
20502056
}
20512057

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,13 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
357357
GlobalVariable *SecEnd = new GlobalVariable(M, Ty, false, Linkage, nullptr,
358358
getSectionEnd(Section));
359359
SecEnd->setVisibility(GlobalValue::HiddenVisibility);
360-
IRBuilder<> IRB(M.getContext());
361360
if (!TargetTriple.isOSBinFormatCOFF())
362361
return std::make_pair(SecStart, SecEnd);
363362

364363
// Account for the fact that on windows-msvc __start_* symbols actually
365364
// point to a uint64_t before the start of the array.
366-
auto GEP =
367-
IRB.CreatePtrAdd(SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
365+
Constant *GEP = ConstantExpr::getGetElementPtr(
366+
Int8Ty, SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
368367
return std::make_pair(GEP, SecEnd);
369368
}
370369

llvm/unittests/IR/IRBuilderTest.cpp

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

528+
TEST_F(IRBuilderTest, GetByteTy) {
529+
IRBuilder<> Builder(BB);
530+
531+
EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(8));
532+
M->setDataLayout("b:32");
533+
EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(32));
534+
}
535+
528536
TEST_F(IRBuilderTest, GetIntTy) {
529537
IRBuilder<> Builder(BB);
530538
IntegerType *Ty1 = Builder.getInt1Ty();
@@ -536,6 +544,20 @@ TEST_F(IRBuilderTest, GetIntTy) {
536544
EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
537545
}
538546

547+
TEST_F(IRBuilderTest, CreatePtrAdd) {
548+
IRBuilder<> Builder(BB);
549+
550+
M->setDataLayout("b:16-p:32:32");
551+
Value *V = Builder.CreatePtrAdd(GV, ConstantInt::get(Ctx, APInt(32, 42)));
552+
ASSERT_TRUE(isa<GEPOperator>(V));
553+
EXPECT_TRUE(cast<GEPOperator>(V)->getResultElementType()->isIntegerTy(16));
554+
555+
M->setDataLayout("b:32-p:64:32");
556+
V = Builder.CreateInBoundsPtrAdd(GV, ConstantInt::get(Ctx, APInt(64, 42)));
557+
ASSERT_TRUE(isa<GEPOperator>(V));
558+
EXPECT_TRUE(cast<GEPOperator>(V)->getResultElementType()->isIntegerTy(32));
559+
}
560+
539561
TEST_F(IRBuilderTest, UnaryOperators) {
540562
IRBuilder<NoFolder> Builder(BB);
541563
Value *V = Builder.CreateLoad(GV->getValueType(), GV);

0 commit comments

Comments
 (0)