[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.
This commit is contained in:
Sergei Barannikov 2024-08-22 15:10:58 +03:00
parent 555e531fde
commit b8c20416dc
3 changed files with 32 additions and 5 deletions

View File

@ -526,6 +526,12 @@ public:
// 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);
@ -2022,12 +2028,12 @@ public:
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());
}

View File

@ -345,14 +345,13 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
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);
}

View File

@ -533,6 +533,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();
@ -544,6 +552,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);