Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IR] Add getDataLayout() helpers to BasicBlock and Instruction #96902

Merged
merged 3 commits into from
Jun 27, 2024
Merged
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
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/MemorySSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ class upward_defs_iterator
if (WalkingPhi && Location.Ptr) {
PHITransAddr Translator(
const_cast<Value *>(Location.Ptr),
OriginalAccess->getBlock()->getModule()->getDataLayout(), nullptr);
OriginalAccess->getBlock()->getDataLayout(), nullptr);

if (Value *Addr =
Translator.translateValue(OriginalAccess->getBlock(),
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/BasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace llvm {

class AssemblyAnnotationWriter;
class CallInst;
class DataLayout;
class Function;
class LandingPadInst;
class LLVMContext;
Expand Down Expand Up @@ -218,6 +219,11 @@ class BasicBlock final : public Value, // Basic blocks are data objects also
static_cast<const BasicBlock *>(this)->getModule());
}

/// Get the data layout of the module this basic block belongs to.
///
/// Requires the basic block to have a parent module.
const DataLayout &getDataLayout() const;

/// Returns the terminator instruction if the block is well formed or null
/// if the block is not well formed.
const Instruction *getTerminator() const LLVM_READONLY {
Expand Down
14 changes: 7 additions & 7 deletions llvm/include/llvm/IR/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ class IRBuilderBase {

/// Create a call to llvm.stacksave
CallInst *CreateStackSave(const Twine &Name = "") {
const DataLayout &DL = BB->getModule()->getDataLayout();
const DataLayout &DL = BB->getDataLayout();
return CreateIntrinsic(Intrinsic::stacksave, {DL.getAllocaPtrType(Context)},
{}, nullptr, Name);
}
Expand Down Expand Up @@ -1770,14 +1770,14 @@ class IRBuilderBase {

AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
Value *ArraySize = nullptr, const Twine &Name = "") {
const DataLayout &DL = BB->getModule()->getDataLayout();
const DataLayout &DL = BB->getDataLayout();
Align AllocaAlign = DL.getPrefTypeAlign(Ty);
return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
}

AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
const Twine &Name = "") {
const DataLayout &DL = BB->getModule()->getDataLayout();
const DataLayout &DL = BB->getDataLayout();
Align AllocaAlign = DL.getPrefTypeAlign(Ty);
unsigned AddrSpace = DL.getAllocaAddrSpace();
return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
Expand Down Expand Up @@ -1815,7 +1815,7 @@ class IRBuilderBase {
LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
bool isVolatile, const Twine &Name = "") {
if (!Align) {
const DataLayout &DL = BB->getModule()->getDataLayout();
const DataLayout &DL = BB->getDataLayout();
Align = DL.getABITypeAlign(Ty);
}
return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name);
Expand All @@ -1824,7 +1824,7 @@ class IRBuilderBase {
StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align,
bool isVolatile = false) {
if (!Align) {
const DataLayout &DL = BB->getModule()->getDataLayout();
const DataLayout &DL = BB->getDataLayout();
Align = DL.getABITypeAlign(Val->getType());
}
return Insert(new StoreInst(Val, Ptr, isVolatile, *Align));
Expand All @@ -1841,7 +1841,7 @@ class IRBuilderBase {
AtomicOrdering FailureOrdering,
SyncScope::ID SSID = SyncScope::System) {
if (!Align) {
const DataLayout &DL = BB->getModule()->getDataLayout();
const DataLayout &DL = BB->getDataLayout();
Align = llvm::Align(DL.getTypeStoreSize(New->getType()));
}

Expand All @@ -1854,7 +1854,7 @@ class IRBuilderBase {
AtomicOrdering Ordering,
SyncScope::ID SSID = SyncScope::System) {
if (!Align) {
const DataLayout &DL = BB->getModule()->getDataLayout();
const DataLayout &DL = BB->getDataLayout();
Align = llvm::Align(DL.getTypeStoreSize(Val->getType()));
}

Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
namespace llvm {

class BasicBlock;
class DataLayout;
class DbgMarker;
class FastMathFlags;
class MDNode;
Expand Down Expand Up @@ -189,6 +190,11 @@ class Instruction : public User,
static_cast<const Instruction *>(this)->getFunction());
}

/// Get the data layout of the module this instruction belongs to.
///
/// Requires the instruction to have a parent module.
const DataLayout &getDataLayout() const;

/// This method unlinks 'this' from the containing basic block, but does not
/// delete it.
void removeFromParent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class InterestingMemoryOperand {
Value *MaybeStride = nullptr)
: IsWrite(IsWrite), OpType(OpType), Alignment(Alignment),
MaybeMask(MaybeMask), MaybeEVL(MaybeEVL), MaybeStride(MaybeStride) {
const DataLayout &DL = I->getModule()->getDataLayout();
const DataLayout &DL = I->getDataLayout();
TypeStoreSize = DL.getTypeStoreSizeInBits(OpType);
PtrUse = &I->getOperandUse(OperandNo);
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/BranchProbabilityInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ computeUnlikelySuccessors(const BasicBlock *BB, Loop *L,
if (!CmpLHSConst || !llvm::is_contained(successors(BB), B))
continue;
// First collapse InstChain
const DataLayout &DL = BB->getModule()->getDataLayout();
const DataLayout &DL = BB->getDataLayout();
for (Instruction *I : llvm::reverse(InstChain)) {
CmpLHSConst = ConstantFoldBinaryOpOperands(
I->getOpcode(), CmpLHSConst, cast<Constant>(I->getOperand(1)), DL);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/CaptureTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ UseCaptureKind llvm::DetermineUseCaptureKind(
// Comparing a dereferenceable_or_null pointer against null cannot
// lead to pointer escapes, because if it is not null it must be a
// valid (in-bounds) pointer.
const DataLayout &DL = I->getModule()->getDataLayout();
const DataLayout &DL = I->getDataLayout();
if (IsDereferenceableOrNull && IsDereferenceableOrNull(O, DL))
return UseCaptureKind::NO_CAPTURE;
}
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Analysis/DemandedBits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void DemandedBits::determineLiveOperandBits(
return;
KnownBitsComputed = true;

const DataLayout &DL = UserI->getModule()->getDataLayout();
const DataLayout &DL = UserI->getDataLayout();
Known = KnownBits(BitWidth);
computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT);

Expand Down Expand Up @@ -404,14 +404,14 @@ APInt DemandedBits::getDemandedBits(Instruction *I) {
if (Found != AliveBits.end())
return Found->second;

const DataLayout &DL = I->getModule()->getDataLayout();
const DataLayout &DL = I->getDataLayout();
return APInt::getAllOnes(DL.getTypeSizeInBits(I->getType()->getScalarType()));
}

APInt DemandedBits::getDemandedBits(Use *U) {
Type *T = (*U)->getType();
auto *UserI = cast<Instruction>(U->getUser());
const DataLayout &DL = UserI->getModule()->getDataLayout();
const DataLayout &DL = UserI->getDataLayout();
unsigned BitWidth = DL.getTypeSizeInBits(T->getScalarType());

// We only track integer uses, everything else produces a mask with all bits
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/IVDescriptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static std::pair<Type *, bool> computeRecurrenceType(Instruction *Exit,
AssumptionCache *AC,
DominatorTree *DT) {
bool IsSigned = false;
const DataLayout &DL = Exit->getModule()->getDataLayout();
const DataLayout &DL = Exit->getDataLayout();
uint64_t MaxBitWidth = DL.getTypeSizeInBits(Exit->getType());

if (DB) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/IVUsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static bool IVUseShouldUsePostIncValue(Instruction *User, Value *Operand,
/// add its users to the IVUsesByStride set and return true. Otherwise, return
/// false.
bool IVUsers::AddUsersIfInteresting(Instruction *I) {
const DataLayout &DL = I->getModule()->getDataLayout();
const DataLayout &DL = I->getDataLayout();

// Add this IV user to the Processed set before returning false to ensure that
// all IV users are members of the set. See IVUsers::isIVUserOrOperand.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7183,7 +7183,7 @@ static bool replaceAndRecursivelySimplifyImpl(
SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr) {
bool Simplified = false;
SmallSetVector<Instruction *, 8> Worklist;
const DataLayout &DL = I->getModule()->getDataLayout();
const DataLayout &DL = I->getDataLayout();

// If we have an explicit value to collapse to, do that round of the
// simplification loop by hand initially.
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Analysis/LazyValueInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ LazyValueInfoImpl::solveBlockValueExtractValue(ExtractValueInst *EVI,
// based on replaced with.overflow intrinsics.
if (Value *V = simplifyExtractValueInst(
EVI->getAggregateOperand(), EVI->getIndices(),
EVI->getModule()->getDataLayout()))
EVI->getDataLayout()))
return getBlockValue(V, BB, EVI);

LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
Expand Down Expand Up @@ -1387,7 +1387,7 @@ LazyValueInfoImpl::getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
// over the operands unnecessarily which can be expensive for
// instructions with many operands.
if (isa<IntegerType>(Usr->getType()) && isOperationFoldable(Usr)) {
const DataLayout &DL = BBTo->getModule()->getDataLayout();
const DataLayout &DL = BBTo->getDataLayout();
if (usesOperand(Usr, Condition)) {
// If Val has Condition as an operand and Val can be folded into a
// constant with either Condition == true or Condition == false,
Expand Down Expand Up @@ -1451,7 +1451,7 @@ LazyValueInfoImpl::getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
ConstantRange EdgeVal(CaseValue);
if (ValUsesConditionAndMayBeFoldable) {
User *Usr = cast<User>(Val);
const DataLayout &DL = BBTo->getModule()->getDataLayout();
const DataLayout &DL = BBTo->getDataLayout();
ValueLatticeElement EdgeLatticeVal =
constantFoldUser(Usr, Condition, CaseValue, DL);
if (EdgeLatticeVal.isOverdefined())
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Analysis/Lint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,22 +563,22 @@ static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT,
}

void Lint::visitSDiv(BinaryOperator &I) {
Check(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
Check(!isZero(I.getOperand(1), I.getDataLayout(), DT, AC),
"Undefined behavior: Division by zero", &I);
}

void Lint::visitUDiv(BinaryOperator &I) {
Check(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
Check(!isZero(I.getOperand(1), I.getDataLayout(), DT, AC),
"Undefined behavior: Division by zero", &I);
}

void Lint::visitSRem(BinaryOperator &I) {
Check(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
Check(!isZero(I.getOperand(1), I.getDataLayout(), DT, AC),
"Undefined behavior: Division by zero", &I);
}

void Lint::visitURem(BinaryOperator &I) {
Check(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
Check(!isZero(I.getOperand(1), I.getDataLayout(), DT, AC),
"Undefined behavior: Division by zero", &I);
}

Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Analysis/Loads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ bool llvm::isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L,
ScalarEvolution &SE,
DominatorTree &DT,
AssumptionCache *AC) {
auto &DL = LI->getModule()->getDataLayout();
auto &DL = LI->getDataLayout();
Value *Ptr = LI->getPointerOperand();

APInt EltSize(DL.getIndexTypeSizeInBits(Ptr->getType()),
Expand Down Expand Up @@ -588,7 +588,7 @@ Value *llvm::findAvailablePtrLoadStore(
if (MaxInstsToScan == 0)
MaxInstsToScan = ~0U;

const DataLayout &DL = ScanBB->getModule()->getDataLayout();
const DataLayout &DL = ScanBB->getDataLayout();
const Value *StrippedPtr = Loc.Ptr->stripPointerCasts();

while (ScanFrom != ScanBB->begin()) {
Expand Down Expand Up @@ -668,7 +668,7 @@ Value *llvm::findAvailablePtrLoadStore(
Value *llvm::FindAvailableLoadedValue(LoadInst *Load, BatchAAResults &AA,
bool *IsLoadCSE,
unsigned MaxInstsToScan) {
const DataLayout &DL = Load->getModule()->getDataLayout();
const DataLayout &DL = Load->getDataLayout();
Value *StrippedPtr = Load->getPointerOperand()->stripPointerCasts();
BasicBlock *ScanBB = Load->getParent();
Type *AccessTy = Load->getType();
Expand Down
14 changes: 7 additions & 7 deletions llvm/lib/Analysis/LoopAccessAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ getStartAndEndForAccess(const Loop *Lp, const SCEV *PtrExpr, Type *AccessTy,
assert(SE->isLoopInvariant(ScEnd, Lp)&& "ScEnd needs to be invariant");

// Add the size of the pointed element to ScEnd.
auto &DL = Lp->getHeader()->getModule()->getDataLayout();
auto &DL = Lp->getHeader()->getDataLayout();
Type *IdxTy = DL.getIndexType(PtrExpr->getType());
const SCEV *EltSizeSCEV = SE->getStoreSizeOfExpr(IdxTy, AccessTy);
ScEnd = SE->getAddExpr(ScEnd, EltSizeSCEV);
Expand Down Expand Up @@ -309,7 +309,7 @@ bool RuntimePointerChecking::tryToCreateDiffCheck(
return false;

const DataLayout &DL =
SinkAR->getLoop()->getHeader()->getModule()->getDataLayout();
SinkAR->getLoop()->getHeader()->getDataLayout();
unsigned AllocSize =
std::max(DL.getTypeAllocSize(SrcTy), DL.getTypeAllocSize(DstTy));

Expand Down Expand Up @@ -1494,7 +1494,7 @@ std::optional<int64_t> llvm::getPtrStride(PredicatedScalarEvolution &PSE,
return std::nullopt;
}

auto &DL = Lp->getHeader()->getModule()->getDataLayout();
auto &DL = Lp->getHeader()->getDataLayout();
TypeSize AllocSize = DL.getTypeAllocSize(AccessTy);
int64_t Size = AllocSize.getFixedValue();
const APInt &APStepVal = C->getAPInt();
Expand Down Expand Up @@ -1907,7 +1907,7 @@ MemoryDepChecker::getDependenceDistanceStrideAndSize(
const AccessAnalysis::MemAccessInfo &B, Instruction *BInst,
const DenseMap<Value *, SmallVector<const Value *, 16>>
&UnderlyingObjects) {
auto &DL = InnermostLoop->getHeader()->getModule()->getDataLayout();
auto &DL = InnermostLoop->getHeader()->getDataLayout();
auto &SE = *PSE.getSE();
auto [APtr, AIsWrite] = A;
auto [BPtr, BIsWrite] = B;
Expand Down Expand Up @@ -2027,7 +2027,7 @@ MemoryDepChecker::Dependence::DepType MemoryDepChecker::isDependent(
}

ScalarEvolution &SE = *PSE.getSE();
auto &DL = InnermostLoop->getHeader()->getModule()->getDataLayout();
auto &DL = InnermostLoop->getHeader()->getDataLayout();
uint64_t MaxStride = std::max(StrideA, StrideB);

// If the distance between the acecsses is larger than their maximum absolute
Expand Down Expand Up @@ -2805,7 +2805,7 @@ bool LoopAccessInfo::isInvariant(Value *V) const {
/// stores. This ignores trailing indices that have no effect on the final
/// pointer.
static unsigned getGEPInductionOperand(const GetElementPtrInst *Gep) {
const DataLayout &DL = Gep->getModule()->getDataLayout();
const DataLayout &DL = Gep->getDataLayout();
unsigned LastOperand = Gep->getNumOperands() - 1;
TypeSize GEPAllocSize = DL.getTypeAllocSize(Gep->getResultElementType());

Expand Down Expand Up @@ -2961,7 +2961,7 @@ void LoopAccessInfo::collectStridedAccess(Value *MemAccess) {
// Match the types so we can compare the stride and the MaxBTC.
// The Stride can be positive/negative, so we sign extend Stride;
// The backedgeTakenCount is non-negative, so we zero extend MaxBTC.
const DataLayout &DL = TheLoop->getHeader()->getModule()->getDataLayout();
const DataLayout &DL = TheLoop->getHeader()->getDataLayout();
uint64_t StrideTypeSizeBits = DL.getTypeSizeInBits(StrideExpr->getType());
uint64_t BETypeSizeBits = DL.getTypeSizeInBits(MaxBTC->getType());
const SCEV *CastedStride = StrideExpr;
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Analysis/LoopUnrollAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ bool UnrolledInstAnalyzer::visitBinaryOperator(BinaryOperator &I) {
RHS = SimpleRHS;

Value *SimpleV = nullptr;
const DataLayout &DL = I.getModule()->getDataLayout();
const DataLayout &DL = I.getDataLayout();
if (auto FI = dyn_cast<FPMathOperator>(&I))
SimpleV =
simplifyBinOp(I.getOpcode(), LHS, RHS, FI->getFastMathFlags(), DL);
Expand Down Expand Up @@ -157,7 +157,7 @@ bool UnrolledInstAnalyzer::visitCastInst(CastInst &I) {
// analysis, which operates on integers (and, e.g., might convert i8* null to
// i32 0).
if (CastInst::castIsValid(I.getOpcode(), Op, I.getType())) {
const DataLayout &DL = I.getModule()->getDataLayout();
const DataLayout &DL = I.getDataLayout();
if (Value *V = simplifyCastInst(I.getOpcode(), Op, I.getType(), DL)) {
SimplifiedValues[&I] = V;
return true;
Expand Down Expand Up @@ -194,7 +194,7 @@ bool UnrolledInstAnalyzer::visitCmpInst(CmpInst &I) {
}
}

const DataLayout &DL = I.getModule()->getDataLayout();
const DataLayout &DL = I.getDataLayout();
if (Value *V = simplifyCmpInst(I.getPredicate(), LHS, RHS, DL)) {
SimplifiedValues[&I] = V;
return true;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/MemoryBuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ llvm::getAllocSize(const CallBase *CB, const TargetLibraryInfo *TLI,

// Get the index type for this address space, results and intermediate
// computations are performed at that width.
auto &DL = CB->getModule()->getDataLayout();
auto &DL = CB->getDataLayout();
const unsigned IntTyBits = DL.getIndexTypeSizeInBits(CB->getType());

// Handle strdup-like functions separately.
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/MemoryDependenceAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ MemDepResult MemoryDependenceResults::getSimplePointerDependencyFrom(
BatchAAResults &BatchAA) {
bool isInvariantLoad = false;
Align MemLocAlign =
MemLoc.Ptr->getPointerAlignment(BB->getModule()->getDataLayout());
MemLoc.Ptr->getPointerAlignment(BB->getDataLayout());

unsigned DefaultLimit = getDefaultBlockScanLimit();
if (!Limit)
Expand Down Expand Up @@ -910,7 +910,7 @@ void MemoryDependenceResults::getNonLocalPointerDependency(
const_cast<Value *>(Loc.Ptr)));
return;
}
const DataLayout &DL = FromBB->getModule()->getDataLayout();
const DataLayout &DL = FromBB->getDataLayout();
PHITransAddr Address(const_cast<Value *>(Loc.Ptr), DL, &AC);

// This is the set of blocks we've inspected, and the pointer we consider in
Expand Down
Loading
Loading