Skip to content

Commit 29393ec

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 c5eb6f7 commit 29393ec

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);
@@ -2038,12 +2044,12 @@ class IRBuilderBase {
20382044

20392045
Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
20402046
GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
2041-
return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW);
2047+
return CreateGEP(getByteTy(), Ptr, Offset, Name, NW);
20422048
}
20432049

20442050
Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
20452051
const Twine &Name = "") {
2046-
return CreateGEP(getInt8Ty(), Ptr, Offset, Name,
2052+
return CreateGEP(getByteTy(), Ptr, Offset, Name,
20472053
GEPNoWrapFlags::inBounds());
20482054
}
20492055

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,13 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
369369
GlobalVariable *SecEnd = new GlobalVariable(M, Ty, false, Linkage, nullptr,
370370
getSectionEnd(Section));
371371
SecEnd->setVisibility(GlobalValue::HiddenVisibility);
372-
IRBuilder<> IRB(M.getContext());
373372
if (!TargetTriple.isOSBinFormatCOFF())
374373
return std::make_pair(SecStart, SecEnd);
375374

376375
// Account for the fact that on windows-msvc __start_* symbols actually
377376
// point to a uint64_t before the start of the array.
378-
auto GEP =
379-
IRB.CreatePtrAdd(SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
377+
Constant *GEP = ConstantExpr::getGetElementPtr(
378+
Int8Ty, SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
380379
return std::make_pair(GEP, SecEnd);
381380
}
382381

llvm/unittests/IR/IRBuilderTest.cpp

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

524+
TEST_F(IRBuilderTest, GetByteTy) {
525+
IRBuilder<> Builder(BB);
526+
527+
EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(8));
528+
M->setDataLayout("b:32");
529+
EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(32));
530+
}
531+
524532
TEST_F(IRBuilderTest, GetIntTy) {
525533
IRBuilder<> Builder(BB);
526534
IntegerType *Ty1 = Builder.getInt1Ty();
@@ -532,6 +540,20 @@ TEST_F(IRBuilderTest, GetIntTy) {
532540
EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
533541
}
534542

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

0 commit comments

Comments
 (0)