Skip to content

Commit

Permalink
Make DataLayout Non-Optional in the Module
Browse files Browse the repository at this point in the history
Summary:
DataLayout keeps the string used for its creation.

As a side effect it is no longer needed in the Module.
This is "almost" NFC, the string is no longer
canonicalized, you can't rely on two "equals" DataLayout
having the same string returned by getStringRepresentation().

Get rid of DataLayoutPass: the DataLayout is in the Module

The DataLayout is "per-module", let's enforce this by not
duplicating it more than necessary.
One more step toward non-optionality of the DataLayout in the
module.

Make DataLayout Non-Optional in the Module

Module->getDataLayout() will never returns nullptr anymore.

Reviewers: echristo

Subscribers: resistor, llvm-commits, jholewinski

Differential Revision: http://reviews.llvm.org/D7992

From: Mehdi Amini <mehdi.amini@apple.com>
llvm-svn: 231270
  • Loading branch information
joker-eph committed Mar 4, 2015
1 parent 2ae03e1 commit 46a4355
Show file tree
Hide file tree
Showing 163 changed files with 617 additions and 973 deletions.
1 change: 0 additions & 1 deletion llvm/examples/ExceptionDemo/ExceptionDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1973,7 +1973,6 @@ int main(int argc, char *argv[]) {
// Start with registering info about how the
// target lays out data structures.
module->setDataLayout(executionEngine->getDataLayout());
fpm.add(new llvm::DataLayoutPass());

// Optimizations turned on
#ifdef ADD_OPT_PASSES
Expand Down
1 change: 0 additions & 1 deletion llvm/examples/Kaleidoscope/Chapter4/toy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,6 @@ void *MCJITHelper::getPointerToFunction(Function *F) {
// Set up the optimizer pipeline. Start with registering info about how the
// target lays out data structures.
OpenModule->setDataLayout(NewEngine->getDataLayout());
FPM->add(new DataLayoutPass());
// Provide basic AliasAnalysis support for GVN.
FPM->add(createBasicAliasAnalysisPass());
// Promote allocas to registers.
Expand Down
1 change: 0 additions & 1 deletion llvm/examples/Kaleidoscope/Chapter5/toy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,6 @@ int main() {
// Set up the optimizer pipeline. Start with registering info about how the
// target lays out data structures.
TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
OurFPM.add(new DataLayoutPass());
// Provide basic AliasAnalysis support for GVN.
OurFPM.add(createBasicAliasAnalysisPass());
// Do simple "peephole" optimizations and bit-twiddling optzns.
Expand Down
1 change: 0 additions & 1 deletion llvm/examples/Kaleidoscope/Chapter6/toy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,6 @@ int main() {
// Set up the optimizer pipeline. Start with registering info about how the
// target lays out data structures.
TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
OurFPM.add(new DataLayoutPass());
// Provide basic AliasAnalysis support for GVN.
OurFPM.add(createBasicAliasAnalysisPass());
// Do simple "peephole" optimizations and bit-twiddling optzns.
Expand Down
1 change: 0 additions & 1 deletion llvm/examples/Kaleidoscope/Chapter7/toy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,6 @@ int main() {
// Set up the optimizer pipeline. Start with registering info about how the
// target lays out data structures.
TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
OurFPM.add(new DataLayoutPass());
// Provide basic AliasAnalysis support for GVN.
OurFPM.add(createBasicAliasAnalysisPass());
// Promote allocas to registers.
Expand Down
1 change: 0 additions & 1 deletion llvm/examples/Kaleidoscope/Chapter8/toy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,6 @@ int main() {
// Set up the optimizer pipeline. Start with registering info about how the
// target lays out data structures.
TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
OurFPM.add(new DataLayoutPass());
#if 0
// Provide basic AliasAnalysis support for GVN.
OurFPM.add(createBasicAliasAnalysisPass());
Expand Down
2 changes: 1 addition & 1 deletion llvm/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ class IRGenContext {
M(new Module(GenerateUniqueName("jit_module_"),
Session.getLLVMContext())),
Builder(Session.getLLVMContext()) {
M->setDataLayout(Session.getTarget().getDataLayout());
M->setDataLayout(*Session.getTarget().getDataLayout());
}

SessionContext& getSession() { return Session; }
Expand Down
2 changes: 1 addition & 1 deletion llvm/examples/Kaleidoscope/Orc/initial/toy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ class IRGenContext {
M(new Module(GenerateUniqueName("jit_module_"),
Session.getLLVMContext())),
Builder(Session.getLLVMContext()) {
M->setDataLayout(Session.getTarget().getDataLayout());
M->setDataLayout(*Session.getTarget().getDataLayout());
}

SessionContext& getSession() { return Session; }
Expand Down
2 changes: 1 addition & 1 deletion llvm/examples/Kaleidoscope/Orc/lazy_codegen/toy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ class IRGenContext {
M(new Module(GenerateUniqueName("jit_module_"),
Session.getLLVMContext())),
Builder(Session.getLLVMContext()) {
M->setDataLayout(Session.getTarget().getDataLayout());
M->setDataLayout(*Session.getTarget().getDataLayout());
}

SessionContext& getSession() { return Session; }
Expand Down
2 changes: 1 addition & 1 deletion llvm/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ class IRGenContext {
M(new Module(GenerateUniqueName("jit_module_"),
Session.getLLVMContext())),
Builder(Session.getLLVMContext()) {
M->setDataLayout(Session.getTarget().getDataLayout());
M->setDataLayout(*Session.getTarget().getDataLayout());
}

SessionContext& getSession() { return Session; }
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/AliasAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class AliasAnalysis {
/// typically called by the run* methods of these subclasses. This may be
/// called multiple times.
///
void InitializeAliasAnalysis(Pass *P);
void InitializeAliasAnalysis(Pass *P, const DataLayout *DL);

/// getAnalysisUsage - All alias analysis implementations should invoke this
/// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Expand Down
8 changes: 3 additions & 5 deletions llvm/include/llvm/Analysis/LibCallAliasAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define LLVM_ANALYSIS_LIBCALLALIASANALYSIS_H

#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"

namespace llvm {
Expand Down Expand Up @@ -48,11 +49,8 @@ namespace llvm {

void getAnalysisUsage(AnalysisUsage &AU) const override;

bool runOnFunction(Function &F) override {
InitializeAliasAnalysis(this); // set up super class
return false;
}

bool runOnFunction(Function &F) override;

/// getAdjustedAnalysisPointer - This method is used when a pass implements
/// an analysis interface through multiple inheritance. If needed, it
/// should override this to adjust the this pointer as needed for the
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ template <typename BaseLayerT> class LazyEmittingLayer {
auto Names = llvm::make_unique<StringMap<bool>>();

for (const auto &M : Ms) {
Mangler Mang(M->getDataLayout());
Mangler Mang(&M->getDataLayout());

for (const auto &GV : M->globals())
if (addGlobalValue(*Names, GV, Mang, SearchName, ExportedSymbolsOnly))
Expand Down
28 changes: 10 additions & 18 deletions llvm/include/llvm/IR/DataLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class DataLayout {
/// \brief Primitive type alignment data.
SmallVector<LayoutAlignElem, 16> Alignments;

/// \brief The string representation used to create this DataLayout
std::string StringRepresentation;

typedef SmallVector<PointerAlignElem, 8> PointersTy;
PointersTy Pointers;

Expand Down Expand Up @@ -185,6 +188,7 @@ class DataLayout {

DataLayout &operator=(const DataLayout &DL) {
clear();
StringRepresentation = DL.StringRepresentation;
BigEndian = DL.isBigEndian();
StackNaturalAlign = DL.StackNaturalAlign;
ManglingMode = DL.ManglingMode;
Expand All @@ -209,8 +213,12 @@ class DataLayout {
/// \brief Returns the string representation of the DataLayout.
///
/// This representation is in the same format accepted by the string
/// constructor above.
std::string getStringRepresentation() const;
/// constructor above. This should not be used to compare two DataLayout as
/// different string can represent the same layout.
std::string getStringRepresentation() const { return StringRepresentation; }

/// \brief Test if the DataLayout was constructed from an empty string.
bool isDefault() const { return StringRepresentation.empty(); }

/// \brief Returns true if the specified type is known to be a native integer
/// type supported by the CPU.
Expand Down Expand Up @@ -451,22 +459,6 @@ inline LLVMTargetDataRef wrap(const DataLayout *P) {
return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P));
}

class DataLayoutPass : public ImmutablePass {
DataLayout DL;

public:
/// This has to exist, because this is a pass, but it should never be used.
DataLayoutPass();
~DataLayoutPass();

const DataLayout &getDataLayout() const { return DL; }

static char ID; // Pass identification, replacement for typeid

bool doFinalization(Module &M) override;
bool doInitialization(Module &M) override;
};

/// Used to lazily calculate structure layout information for a target machine,
/// based on the DataLayout structure.
class StructLayout {
Expand Down
17 changes: 6 additions & 11 deletions llvm/include/llvm/IR/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,7 @@ class Module {
std::string TargetTriple; ///< Platform target triple Module compiled on
///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
void *NamedMDSymTab; ///< NamedMDNode names.

// We need to keep the string because the C API expects us to own the string
// representation.
// Since we have it, we also use an empty string to represent a module without
// a DataLayout. If it has a DataLayout, these variables are in sync and the
// string is just a cache of getDataLayout()->getStringRepresentation().
std::string DataLayoutStr;
DataLayout DL;
DataLayout DL; ///< DataLayout associated with the module

friend class Constant;

Expand Down Expand Up @@ -256,10 +249,12 @@ class Module {

/// Get the data layout string for the module's target platform. This is
/// equivalent to getDataLayout()->getStringRepresentation().
const std::string &getDataLayoutStr() const { return DataLayoutStr; }
const std::string getDataLayoutStr() const {
return DL.getStringRepresentation();
}

/// Get the data layout for the module's target platform.
const DataLayout *getDataLayout() const;
const DataLayout &getDataLayout() const;

/// Get the target triple which is a string describing the target host.
/// @returns a string containing the target triple.
Expand Down Expand Up @@ -293,7 +288,7 @@ class Module {

/// Set the data layout
void setDataLayout(StringRef Desc);
void setDataLayout(const DataLayout *Other);
void setDataLayout(const DataLayout &Other);

/// Set the target triple.
void setTargetTriple(StringRef T) { TargetTriple = T; }
Expand Down
4 changes: 1 addition & 3 deletions llvm/include/llvm/Transforms/Utils/Cloning.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,13 @@ void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
class InlineFunctionInfo {
public:
explicit InlineFunctionInfo(CallGraph *cg = nullptr,
const DataLayout *DL = nullptr,
AliasAnalysis *AA = nullptr,
AssumptionCacheTracker *ACT = nullptr)
: CG(cg), DL(DL), AA(AA), ACT(ACT) {}
: CG(cg), AA(AA), ACT(ACT) {}

/// CG - If non-null, InlineFunction will update the callgraph to reflect the
/// changes it makes.
CallGraph *CG;
const DataLayout *DL;
AliasAnalysis *AA;
AssumptionCacheTracker *ACT;

Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Analysis/AliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,8 @@ AliasAnalysis::~AliasAnalysis() {}
/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
/// AliasAnalysis interface before any other methods are called.
///
void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
DataLayoutPass *DLP = P->getAnalysisIfAvailable<DataLayoutPass>();
DL = DLP ? &DLP->getDataLayout() : nullptr;
void AliasAnalysis::InitializeAliasAnalysis(Pass *P, const DataLayout *NewDL) {
DL = NewDL;
auto *TLIP = P->getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
TLI = TLIP ? &TLIP->getTLI() : nullptr;
AA = &P->getAnalysis<AliasAnalysis>();
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Analysis/AliasAnalysisCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
Expand Down Expand Up @@ -76,7 +77,7 @@ namespace {

bool runOnModule(Module &M) override {
this->M = &M;
InitializeAliasAnalysis(this);
InitializeAliasAnalysis(this, &M.getDataLayout());
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/AliasDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace {
}

bool runOnModule(Module &M) override {
InitializeAliasAnalysis(this); // set up super class
InitializeAliasAnalysis(this, &M.getDataLayout()); // set up super class

for(Module::global_iterator I = M.global_begin(),
E = M.global_end(); I != E; ++I) {
Expand Down
9 changes: 6 additions & 3 deletions llvm/lib/Analysis/BasicAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,7 @@ namespace {
initializeBasicAliasAnalysisPass(*PassRegistry::getPassRegistry());
}

void initializePass() override {
InitializeAliasAnalysis(this);
}
bool doInitialization(Module &M) override;

void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<AliasAnalysis>();
Expand Down Expand Up @@ -815,6 +813,11 @@ static bool isAssumeIntrinsic(ImmutableCallSite CS) {
return false;
}

bool BasicAliasAnalysis::doInitialization(Module &M) {
InitializeAliasAnalysis(this, &M.getDataLayout());
return true;
}

/// getModRefInfo - Check to see if the specified callsite can clobber the
/// specified memory object. Since we only look at local properties of this
/// function, we really can't say much about this query. We do, however, use
Expand Down
7 changes: 6 additions & 1 deletion llvm/lib/Analysis/CFLAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ struct CFLAliasAnalysis : public ImmutablePass, public AliasAnalysis {
return QueryResult;
}

void initializePass() override { InitializeAliasAnalysis(this); }
bool doInitialization(Module &M) override;
};

void FunctionHandle::removeSelfFromCache() {
Expand Down Expand Up @@ -1034,3 +1034,8 @@ CFLAliasAnalysis::query(const AliasAnalysis::Location &LocA,

return AliasAnalysis::NoAlias;
}

bool CFLAliasAnalysis::doInitialization(Module &M) {
InitializeAliasAnalysis(this, &M.getDataLayout());
return true;
}
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/IPA/GlobalsModRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace {
}

bool runOnModule(Module &M) override {
InitializeAliasAnalysis(this);
InitializeAliasAnalysis(this, &M.getDataLayout());

// Find non-addr taken globals.
AnalyzeGlobals(M);
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/Analysis/IPA/InlineCost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ bool CallAnalyzer::visitBitCast(BitCastInst &I) {
}

bool CallAnalyzer::visitPtrToInt(PtrToIntInst &I) {
const DataLayout *DL = I.getModule()->getDataLayout();
// Propagate constants through ptrtoint.
Constant *COp = dyn_cast<Constant>(I.getOperand(0));
if (!COp)
Expand All @@ -410,7 +409,8 @@ bool CallAnalyzer::visitPtrToInt(PtrToIntInst &I) {
// Track base/offset pairs when converted to a plain integer provided the
// integer is large enough to represent the pointer.
unsigned IntegerSize = I.getType()->getScalarSizeInBits();
if (DL && IntegerSize >= DL->getPointerSizeInBits()) {
const DataLayout &DL = I.getModule()->getDataLayout();
if (IntegerSize >= DL.getPointerSizeInBits()) {
std::pair<Value *, APInt> BaseAndOffset
= ConstantOffsetPtrs.lookup(I.getOperand(0));
if (BaseAndOffset.first)
Expand All @@ -433,7 +433,6 @@ bool CallAnalyzer::visitPtrToInt(PtrToIntInst &I) {
}

bool CallAnalyzer::visitIntToPtr(IntToPtrInst &I) {
const DataLayout *DL = I.getModule()->getDataLayout();
// Propagate constants through ptrtoint.
Constant *COp = dyn_cast<Constant>(I.getOperand(0));
if (!COp)
Expand All @@ -448,7 +447,8 @@ bool CallAnalyzer::visitIntToPtr(IntToPtrInst &I) {
// modifications provided the integer is not too large.
Value *Op = I.getOperand(0);
unsigned IntegerSize = Op->getType()->getScalarSizeInBits();
if (DL && IntegerSize <= DL->getPointerSizeInBits()) {
const DataLayout &DL = I.getModule()->getDataLayout();
if (IntegerSize <= DL.getPointerSizeInBits()) {
std::pair<Value *, APInt> BaseAndOffset = ConstantOffsetPtrs.lookup(Op);
if (BaseAndOffset.first)
ConstantOffsetPtrs[&I] = BaseAndOffset;
Expand Down Expand Up @@ -1333,7 +1333,7 @@ InlineCost InlineCostAnalysis::getInlineCost(CallSite CS, Function *Callee,
DEBUG(llvm::dbgs() << " Analyzing call of " << Callee->getName()
<< "...\n");

CallAnalyzer CA(Callee->getParent()->getDataLayout(), TTIWP->getTTI(*Callee),
CallAnalyzer CA(&Callee->getParent()->getDataLayout(), TTIWP->getTTI(*Callee),
ACT, *Callee, Threshold);
bool ShouldInline = CA.analyzeCall(CS);

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/IVUsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -253,8 +254,7 @@ bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) {
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
SE = &getAnalysis<ScalarEvolution>();
DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
DL = DLP ? &DLP->getDataLayout() : nullptr;
DL = &L->getHeader()->getModule()->getDataLayout();

// Find all uses of induction variables in this loop, and categorize
// them by stride. Start by finding all of the PHI nodes in the header for
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Analysis/LazyValueInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,8 +1117,7 @@ bool LazyValueInfo::runOnFunction(Function &F) {
getAnalysisIfAvailable<DominatorTreeWrapperPass>();
DT = DTWP ? &DTWP->getDomTree() : nullptr;

DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
DL = DLP ? &DLP->getDataLayout() : nullptr;
DL = &F.getParent()->getDataLayout();

TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();

Expand Down
Loading

0 comments on commit 46a4355

Please sign in to comment.