Skip to content

Commit

Permalink
[IR] Add getDataLayout() helpers to Function and GlobalValue (#96919)
Browse files Browse the repository at this point in the history
Similar to #96902, this adds
`getDataLayout()` helpers to Function and GlobalValue, replacing the
current `getParent()->getDataLayout()` pattern.
  • Loading branch information
nikic committed Jun 28, 2024
1 parent 519e0bb commit 9df71d7
Show file tree
Hide file tree
Showing 178 changed files with 313 additions and 293 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/ScalarEvolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ class ScalarEvolution {
/// Return the DataLayout associated with the module this SCEV instance is
/// operating on.
const DataLayout &getDataLayout() const {
return F.getParent()->getDataLayout();
return F.getDataLayout();
}

const SCEVPredicate *getEqualPredicate(const SCEV *LHS, const SCEV *RHS);
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class MachineIRBuilder {
}

const DataLayout &getDataLayout() const {
return getMF().getFunction().getParent()->getDataLayout();
return getMF().getFunction().getDataLayout();
}

LLVMContext &getContext() const {
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typedef unsigned ID;
class AssemblyAnnotationWriter;
class Constant;
class ConstantRange;
class DataLayout;
struct DenormalMode;
class DISubprogram;
enum LibFunc : unsigned;
Expand Down Expand Up @@ -214,6 +215,11 @@ class LLVM_EXTERNAL_VISIBILITY Function : public GlobalObject,
/// function.
LLVMContext &getContext() const;

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

/// isVarArg - Return true if this function takes a variable number of
/// arguments.
bool isVarArg() const { return getFunctionType()->isVarArg(); }
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/GlobalValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace llvm {

class Comdat;
class ConstantRange;
class DataLayout;
class Error;
class GlobalObject;
class Module;
Expand Down Expand Up @@ -655,6 +656,11 @@ class GlobalValue : public Constant {
Module *getParent() { return Parent; }
const Module *getParent() const { return Parent; }

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

// Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Value *V) {
return V->getValueID() == Value::FunctionVal ||
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/AliasAnalysisEvaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ PreservedAnalyses AAEvaluator::run(Function &F, FunctionAnalysisManager &AM) {
}

void AAEvaluator::runInternal(Function &F, AAResults &AA) {
const DataLayout &DL = F.getParent()->getDataLayout();
const DataLayout &DL = F.getDataLayout();

++FunctionCount;

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/BasicAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,7 @@ BasicAAResult BasicAA::run(Function &F, FunctionAnalysisManager &AM) {
auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
auto &AC = AM.getResult<AssumptionAnalysis>(F);
auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
return BasicAAResult(F.getParent()->getDataLayout(), F, TLI, AC, DT);
return BasicAAResult(F.getDataLayout(), F, TLI, AC, DT);
}

BasicAAWrapperPass::BasicAAWrapperPass() : FunctionPass(ID) {
Expand Down Expand Up @@ -1947,7 +1947,7 @@ bool BasicAAWrapperPass::runOnFunction(Function &F) {
auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
auto &DTWP = getAnalysis<DominatorTreeWrapperPass>();

Result.reset(new BasicAAResult(F.getParent()->getDataLayout(), F,
Result.reset(new BasicAAResult(F.getDataLayout(), F,
TLIWP.getTLI(F), ACT.getAssumptionCache(F),
&DTWP.getDomTree()));

Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ Constant *llvm::ReadByteArrayFromGlobal(const GlobalVariable *GV,
if (!GV->isConstant() || !GV->hasDefinitiveInitializer())
return nullptr;

const DataLayout &DL = GV->getParent()->getDataLayout();
const DataLayout &DL = GV->getDataLayout();
Constant *Init = const_cast<Constant *>(GV->getInitializer());
TypeSize InitSize = DL.getTypeAllocSize(Init->getType());
if (InitSize < Offset)
Expand Down Expand Up @@ -3485,15 +3485,15 @@ Constant *llvm::ConstantFoldCall(const CallBase *Call, Function *F,
StringRef Name = F->getName();
if (auto *FVTy = dyn_cast<FixedVectorType>(Ty))
return ConstantFoldFixedVectorCall(
Name, IID, FVTy, Operands, F->getParent()->getDataLayout(), TLI, Call);
Name, IID, FVTy, Operands, F->getDataLayout(), TLI, Call);

if (auto *SVTy = dyn_cast<ScalableVectorType>(Ty))
return ConstantFoldScalableVectorCall(
Name, IID, SVTy, Operands, F->getParent()->getDataLayout(), TLI, Call);
Name, IID, SVTy, Operands, F->getDataLayout(), TLI, Call);

if (auto *StTy = dyn_cast<StructType>(Ty))
return ConstantFoldStructCall(Name, IID, StTy, Operands,
F->getParent()->getDataLayout(), TLI, Call);
F->getDataLayout(), TLI, Call);

// TODO: If this is a library function, we already discovered that above,
// so we should pass the LibFunc, not the name (and it might be better
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/DependenceAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3607,7 +3607,7 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst,
Value *SrcPtr = getLoadStorePointerOperand(Src);
Value *DstPtr = getLoadStorePointerOperand(Dst);

switch (underlyingObjectsAlias(AA, F->getParent()->getDataLayout(),
switch (underlyingObjectsAlias(AA, F->getDataLayout(),
MemoryLocation::get(Dst),
MemoryLocation::get(Src))) {
case AliasResult::MayAlias:
Expand Down Expand Up @@ -4034,7 +4034,7 @@ const SCEV *DependenceInfo::getSplitIteration(const Dependence &Dep,
Value *SrcPtr = getLoadStorePointerOperand(Src);
Value *DstPtr = getLoadStorePointerOperand(Dst);
assert(underlyingObjectsAlias(
AA, F->getParent()->getDataLayout(), MemoryLocation::get(Dst),
AA, F->getDataLayout(), MemoryLocation::get(Dst),
MemoryLocation::get(Src)) == AliasResult::MustAlias);

// establish loop nesting levels
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/InlineCost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
ProfileSummaryInfo *PSI = nullptr,
OptimizationRemarkEmitter *ORE = nullptr)
: TTI(TTI), GetAssumptionCache(GetAssumptionCache), GetBFI(GetBFI),
PSI(PSI), F(Callee), DL(F.getParent()->getDataLayout()), ORE(ORE),
PSI(PSI), F(Callee), DL(F.getDataLayout()), ORE(ORE),
CandidateCall(Call) {}

InlineResult analyze();
Expand Down Expand Up @@ -2999,7 +2999,7 @@ std::optional<InlineResult> llvm::getAttributeBasedInliningDecision(
// alloca, the inlined code would need to be adjusted to handle that the
// argument is in the alloca address space (so it is a little bit complicated
// to solve).
unsigned AllocaAS = Callee->getParent()->getDataLayout().getAllocaAddrSpace();
unsigned AllocaAS = Callee->getDataLayout().getAllocaAddrSpace();
for (unsigned I = 0, E = Call.arg_size(); I != E; ++I)
if (Call.isByValArgument(I)) {
PointerType *PTy = cast<PointerType>(Call.getArgOperand(I)->getType());
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7248,7 +7248,7 @@ const SimplifyQuery getBestSimplifyQuery(Pass &P, Function &F) {
auto *TLI = TLIWP ? &TLIWP->getTLI(F) : nullptr;
auto *ACWP = P.getAnalysisIfAvailable<AssumptionCacheTracker>();
auto *AC = ACWP ? &ACWP->getAssumptionCache(F) : nullptr;
return {F.getParent()->getDataLayout(), TLI, DT, AC};
return {F.getDataLayout(), TLI, DT, AC};
}

const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &AR,
Expand All @@ -7262,7 +7262,7 @@ const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &AM,
auto *DT = AM.template getCachedResult<DominatorTreeAnalysis>(F);
auto *TLI = AM.template getCachedResult<TargetLibraryAnalysis>(F);
auto *AC = AM.template getCachedResult<AssumptionAnalysis>(F);
return {F.getParent()->getDataLayout(), TLI, DT, AC};
return {F.getDataLayout(), TLI, DT, AC};
}
template const SimplifyQuery getBestSimplifyQuery(AnalysisManager<Function> &,
Function &);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/LazyValueInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,7 @@ LazyValueInfo LazyValueAnalysis::run(Function &F,
FunctionAnalysisManager &FAM) {
auto &AC = FAM.getResult<AssumptionAnalysis>(F);

return LazyValueInfo(&AC, &F.getParent()->getDataLayout());
return LazyValueInfo(&AC, &F.getDataLayout());
}

/// Returns true if we can statically tell that this value will never be a
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/Lint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ Value *Lint::findValueImpl(Value *V, bool OffsetOk,

PreservedAnalyses LintPass::run(Function &F, FunctionAnalysisManager &AM) {
auto *Mod = F.getParent();
auto *DL = &F.getParent()->getDataLayout();
auto *DL = &F.getDataLayout();
auto *AA = &AM.getResult<AAManager>(F);
auto *AC = &AM.getResult<AssumptionAnalysis>(F);
auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/MemDerefPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ PreservedAnalyses MemDerefPrinterPass::run(Function &F,
SmallVector<Value *, 4> Deref;
SmallPtrSet<Value *, 4> DerefAndAligned;

const DataLayout &DL = F.getParent()->getDataLayout();
const DataLayout &DL = F.getDataLayout();
for (auto &I : instructions(F)) {
if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
Value *PO = LI->getPointerOperand();
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/ObjCARCAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@ ModRefInfo ObjCARCAAResult::getModRefInfo(const CallBase *Call,
AnalysisKey ObjCARCAA::Key;

ObjCARCAAResult ObjCARCAA::run(Function &F, FunctionAnalysisManager &AM) {
return ObjCARCAAResult(F.getParent()->getDataLayout());
return ObjCARCAAResult(F.getDataLayout());
}
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/StackLifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void StackLifetime::collectMarkers() {
DenseMap<const BasicBlock *, SmallDenseMap<const IntrinsicInst *, Marker>>
BBMarkerSet;

const DataLayout &DL = F.getParent()->getDataLayout();
const DataLayout &DL = F.getDataLayout();

// Compute the set of start/end markers per basic block.
for (const BasicBlock *BB : depth_first(&F)) {
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/StackSafetyAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class StackSafetyLocalAnalysis {

public:
StackSafetyLocalAnalysis(Function &F, ScalarEvolution &SE)
: F(F), DL(F.getParent()->getDataLayout()), SE(SE),
: F(F), DL(F.getDataLayout()), SE(SE),
PointerSize(DL.getPointerSizeInBits()),
UnknownRange(PointerSize, true) {}

Expand Down Expand Up @@ -852,7 +852,7 @@ GVToSSI createGlobalStackSafetyInfo(
}

uint32_t PointerSize =
Copy.begin()->first->getParent()->getDataLayout().getPointerSizeInBits();
Copy.begin()->first->getDataLayout().getPointerSizeInBits();
StackSafetyDataFlowAnalysis<GlobalValue> SSDFA(PointerSize, std::move(Copy));

for (const auto &F : SSDFA.run()) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
AnalysisKey TargetIRAnalysis::Key;

TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
return Result(F.getParent()->getDataLayout());
return Result(F.getDataLayout());
}

// Register the basic pass.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6115,7 +6115,7 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
// Fail if V is not based on constant global object.
return false;

const DataLayout &DL = GV->getParent()->getDataLayout();
const DataLayout &DL = GV->getDataLayout();
APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);

if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8282,7 +8282,7 @@ int LLParser::parseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
return error(NewLoc, "cmpxchg operand must be a first class value");

const Align DefaultAlignment(
PFS.getFunction().getParent()->getDataLayout().getTypeStoreSize(
PFS.getFunction().getDataLayout().getTypeStoreSize(
Cmp->getType()));

AtomicCmpXchgInst *CXI =
Expand Down Expand Up @@ -8388,13 +8388,13 @@ int LLParser::parseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
}

unsigned Size =
PFS.getFunction().getParent()->getDataLayout().getTypeStoreSizeInBits(
PFS.getFunction().getDataLayout().getTypeStoreSizeInBits(
Val->getType());
if (Size < 8 || (Size & (Size - 1)))
return error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
" integer");
const Align DefaultAlignment(
PFS.getFunction().getParent()->getDataLayout().getTypeStoreSize(
PFS.getFunction().getDataLayout().getTypeStoreSize(
Val->getType()));
AtomicRMWInst *RMWI =
new AtomicRMWInst(Operation, Ptr, Val,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/Analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ bool llvm::returnTypeIsEligibleForTailCall(const Function *F,
// index is compatible with the value we return.
if (!slotOnlyDiscardsData(RetVal, CallVal, TmpRetPath, TmpCallPath,
AllowDifferingSizes, TLI,
F->getParent()->getDataLayout()))
F->getDataLayout()))
return false;

CallEmpty = !nextRealType(CallSubTypes, CallPath);
Expand Down
22 changes: 11 additions & 11 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {

SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);

const DataLayout &DL = GV->getParent()->getDataLayout();
const DataLayout &DL = GV->getDataLayout();
uint64_t Size = DL.getTypeAllocSize(GV->getValueType());

// If the alignment is specified, we *must* obey it. Overaligning a global
Expand Down Expand Up @@ -871,7 +871,7 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
emitAlignment(Alignment, GV);
OutStreamer->emitLabel(MangSym);

emitGlobalConstant(GV->getParent()->getDataLayout(),
emitGlobalConstant(GV->getDataLayout(),
GV->getInitializer());
}

Expand Down Expand Up @@ -911,7 +911,7 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
if (LocalAlias != EmittedInitSym)
OutStreamer->emitLabel(LocalAlias);

emitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
emitGlobalConstant(GV->getDataLayout(), GV->getInitializer());

if (MAI->hasDotTypeDotSizeDirective())
// .size foo, 42
Expand All @@ -935,7 +935,7 @@ void AsmPrinter::emitFunctionPrefix(ArrayRef<const Constant *> Prefix) {
const Function &F = MF->getFunction();
if (!MAI->hasSubsectionsViaSymbols()) {
for (auto &C : Prefix)
emitGlobalConstant(F.getParent()->getDataLayout(), C);
emitGlobalConstant(F.getDataLayout(), C);
return;
}
// Preserving prefix-like data on platforms which use subsections-via-symbols
Expand All @@ -945,7 +945,7 @@ void AsmPrinter::emitFunctionPrefix(ArrayRef<const Constant *> Prefix) {
OutStreamer->emitLabel(OutContext.createLinkerPrivateTempSymbol());

for (auto &C : Prefix) {
emitGlobalConstant(F.getParent()->getDataLayout(), C);
emitGlobalConstant(F.getDataLayout(), C);
}

// Emit an .alt_entry directive for the actual function symbol.
Expand Down Expand Up @@ -1080,7 +1080,7 @@ void AsmPrinter::emitFunctionHeader() {

// Emit the prologue data.
if (F.hasPrologueData())
emitGlobalConstant(F.getParent()->getDataLayout(), F.getPrologueData());
emitGlobalConstant(F.getDataLayout(), F.getPrologueData());
}

/// EmitFunctionEntryLabel - Emit the label that is the entrypoint for the
Expand Down Expand Up @@ -1528,7 +1528,7 @@ void AsmPrinter::emitKCFITrapEntry(const MachineFunction &MF,
void AsmPrinter::emitKCFITypeId(const MachineFunction &MF) {
const Function &F = MF.getFunction();
if (const MDNode *MD = F.getMetadata(LLVMContext::MD_kcfi_type))
emitGlobalConstant(F.getParent()->getDataLayout(),
emitGlobalConstant(F.getDataLayout(),
mdconst::extract<ConstantInt>(MD->getOperand(0)));
}

Expand Down Expand Up @@ -1678,7 +1678,7 @@ void AsmPrinter::emitPCSections(const MachineFunction &MF) {
for (const MDOperand &AuxMDO : AuxMDs->operands()) {
assert(isa<ConstantAsMetadata>(AuxMDO) && "expecting a constant");
const Constant *C = cast<ConstantAsMetadata>(AuxMDO)->getValue();
const DataLayout &DL = F.getParent()->getDataLayout();
const DataLayout &DL = F.getDataLayout();
const uint64_t Size = DL.getTypeStoreSize(C->getType());

if (auto *CI = dyn_cast<ConstantInt>(C);
Expand Down Expand Up @@ -2926,14 +2926,14 @@ bool AsmPrinter::emitSpecialLLVMGlobal(const GlobalVariable *GV) {
assert(GV->hasInitializer() && "Not a special LLVM global!");

if (GV->getName() == "llvm.global_ctors") {
emitXXStructorList(GV->getParent()->getDataLayout(), GV->getInitializer(),
emitXXStructorList(GV->getDataLayout(), GV->getInitializer(),
/* isCtor */ true);

return true;
}

if (GV->getName() == "llvm.global_dtors") {
emitXXStructorList(GV->getParent()->getDataLayout(), GV->getInitializer(),
emitXXStructorList(GV->getDataLayout(), GV->getInitializer(),
/* isCtor */ false);

return true;
Expand Down Expand Up @@ -3147,7 +3147,7 @@ void AsmPrinter::emitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
void AsmPrinter::emitAlignment(Align Alignment, const GlobalObject *GV,
unsigned MaxBytesToEmit) const {
if (GV)
Alignment = getGVAlignment(GV, GV->getParent()->getDataLayout(), Alignment);
Alignment = getGVAlignment(GV, GV->getDataLayout(), Alignment);

if (Alignment == Align(1))
return; // 1-byte aligned: no need to emit alignment.
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2184,7 +2184,7 @@ static AssignmentTrackingLowering::OverlapMap buildOverlapMapAndRecordDeclares(
if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {
ProcessDbgRecord(DII, InstDeclares);
} else if (auto Info = getUntaggedStoreAssignmentInfo(
I, Fn.getParent()->getDataLayout())) {
I, Fn.getDataLayout())) {
// Find markers linked to this alloca.
auto HandleDbgAssignForStore = [&](auto *Assign) {
std::optional<DIExpression::FragmentInfo> FragInfo;
Expand Down Expand Up @@ -2800,7 +2800,7 @@ DebugAssignmentTrackingAnalysis::run(Function &F,
if (!isAssignmentTrackingEnabled(*F.getParent()))
return FunctionVarLocs();

auto &DL = F.getParent()->getDataLayout();
auto &DL = F.getDataLayout();

FunctionVarLocsBuilder Builder;
analyzeFunction(F, DL, &Builder);
Expand Down
Loading

0 comments on commit 9df71d7

Please sign in to comment.