Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions llvm/include/llvm/IR/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,12 @@ class IRBuilderBase {
// Type creation methods
//===--------------------------------------------------------------------===//

/// Fetch the type representing a byte.
IntegerType *getByteTy() {
const DataLayout &DL = BB->getDataLayout();
return Type::getIntNTy(Context, DL.getByteWidth());
}

/// Fetch the type representing a single bit
IntegerType *getInt1Ty() {
return Type::getInt1Ty(Context);
Expand Down Expand Up @@ -2038,12 +2044,12 @@ class IRBuilderBase {

Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW);
return CreateGEP(getByteTy(), Ptr, Offset, Name, NW);
}

Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
const Twine &Name = "") {
return CreateGEP(getInt8Ty(), Ptr, Offset, Name,
return CreateGEP(getByteTy(), Ptr, Offset, Name,
GEPNoWrapFlags::inBounds());
}

Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,13 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
GlobalVariable *SecEnd = new GlobalVariable(M, Ty, false, Linkage, nullptr,
getSectionEnd(Section));
SecEnd->setVisibility(GlobalValue::HiddenVisibility);
IRBuilder<> IRB(M.getContext());
if (!TargetTriple.isOSBinFormatCOFF())
return std::make_pair(SecStart, SecEnd);

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

Expand Down
22 changes: 22 additions & 0 deletions llvm/unittests/IR/IRBuilderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,14 @@ TEST_F(IRBuilderTest, DataLayout) {
EXPECT_FALSE(M->getDataLayout().isLegalInteger(32));
}

TEST_F(IRBuilderTest, GetByteTy) {
IRBuilder<> Builder(BB);

EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(8));
M->setDataLayout("b:32");
EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(32));
}

TEST_F(IRBuilderTest, GetIntTy) {
IRBuilder<> Builder(BB);
IntegerType *Ty1 = Builder.getInt1Ty();
Expand All @@ -532,6 +540,20 @@ TEST_F(IRBuilderTest, GetIntTy) {
EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
}

TEST_F(IRBuilderTest, CreatePtrAdd) {
IRBuilder<> Builder(BB);

M->setDataLayout("b:16-p:32:32");
Value *V = Builder.CreatePtrAdd(GV, ConstantInt::get(Ctx, APInt(32, 42)));
ASSERT_TRUE(isa<GEPOperator>(V));
EXPECT_TRUE(cast<GEPOperator>(V)->getResultElementType()->isIntegerTy(16));

M->setDataLayout("b:32-p:64:32");
V = Builder.CreateInBoundsPtrAdd(GV, ConstantInt::get(Ctx, APInt(64, 42)));
ASSERT_TRUE(isa<GEPOperator>(V));
EXPECT_TRUE(cast<GEPOperator>(V)->getResultElementType()->isIntegerTy(32));
}

TEST_F(IRBuilderTest, UnaryOperators) {
IRBuilder<NoFolder> Builder(BB);
Value *V = Builder.CreateLoad(GV->getValueType(), GV);
Expand Down
Loading