Skip to content

Commit 46a4355

Browse files
committed
Make DataLayout Non-Optional in the Module
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
1 parent 2ae03e1 commit 46a4355

File tree

163 files changed

+617
-973
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+617
-973
lines changed

llvm/examples/ExceptionDemo/ExceptionDemo.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1973,7 +1973,6 @@ int main(int argc, char *argv[]) {
19731973
// Start with registering info about how the
19741974
// target lays out data structures.
19751975
module->setDataLayout(executionEngine->getDataLayout());
1976-
fpm.add(new llvm::DataLayoutPass());
19771976

19781977
// Optimizations turned on
19791978
#ifdef ADD_OPT_PASSES

llvm/examples/Kaleidoscope/Chapter4/toy.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,6 @@ void *MCJITHelper::getPointerToFunction(Function *F) {
561561
// Set up the optimizer pipeline. Start with registering info about how the
562562
// target lays out data structures.
563563
OpenModule->setDataLayout(NewEngine->getDataLayout());
564-
FPM->add(new DataLayoutPass());
565564
// Provide basic AliasAnalysis support for GVN.
566565
FPM->add(createBasicAliasAnalysisPass());
567566
// Promote allocas to registers.

llvm/examples/Kaleidoscope/Chapter5/toy.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,6 @@ int main() {
914914
// Set up the optimizer pipeline. Start with registering info about how the
915915
// target lays out data structures.
916916
TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
917-
OurFPM.add(new DataLayoutPass());
918917
// Provide basic AliasAnalysis support for GVN.
919918
OurFPM.add(createBasicAliasAnalysisPass());
920919
// Do simple "peephole" optimizations and bit-twiddling optzns.

llvm/examples/Kaleidoscope/Chapter6/toy.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,6 @@ int main() {
10351035
// Set up the optimizer pipeline. Start with registering info about how the
10361036
// target lays out data structures.
10371037
TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
1038-
OurFPM.add(new DataLayoutPass());
10391038
// Provide basic AliasAnalysis support for GVN.
10401039
OurFPM.add(createBasicAliasAnalysisPass());
10411040
// Do simple "peephole" optimizations and bit-twiddling optzns.

llvm/examples/Kaleidoscope/Chapter7/toy.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,6 @@ int main() {
12091209
// Set up the optimizer pipeline. Start with registering info about how the
12101210
// target lays out data structures.
12111211
TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
1212-
OurFPM.add(new DataLayoutPass());
12131212
// Provide basic AliasAnalysis support for GVN.
12141213
OurFPM.add(createBasicAliasAnalysisPass());
12151214
// Promote allocas to registers.

llvm/examples/Kaleidoscope/Chapter8/toy.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,6 @@ int main() {
14601460
// Set up the optimizer pipeline. Start with registering info about how the
14611461
// target lays out data structures.
14621462
TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
1463-
OurFPM.add(new DataLayoutPass());
14641463
#if 0
14651464
// Provide basic AliasAnalysis support for GVN.
14661465
OurFPM.add(createBasicAliasAnalysisPass());

llvm/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ class IRGenContext {
716716
M(new Module(GenerateUniqueName("jit_module_"),
717717
Session.getLLVMContext())),
718718
Builder(Session.getLLVMContext()) {
719-
M->setDataLayout(Session.getTarget().getDataLayout());
719+
M->setDataLayout(*Session.getTarget().getDataLayout());
720720
}
721721

722722
SessionContext& getSession() { return Session; }

llvm/examples/Kaleidoscope/Orc/initial/toy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ class IRGenContext {
715715
M(new Module(GenerateUniqueName("jit_module_"),
716716
Session.getLLVMContext())),
717717
Builder(Session.getLLVMContext()) {
718-
M->setDataLayout(Session.getTarget().getDataLayout());
718+
M->setDataLayout(*Session.getTarget().getDataLayout());
719719
}
720720

721721
SessionContext& getSession() { return Session; }

llvm/examples/Kaleidoscope/Orc/lazy_codegen/toy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ class IRGenContext {
715715
M(new Module(GenerateUniqueName("jit_module_"),
716716
Session.getLLVMContext())),
717717
Builder(Session.getLLVMContext()) {
718-
M->setDataLayout(Session.getTarget().getDataLayout());
718+
M->setDataLayout(*Session.getTarget().getDataLayout());
719719
}
720720

721721
SessionContext& getSession() { return Session; }

llvm/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ class IRGenContext {
715715
M(new Module(GenerateUniqueName("jit_module_"),
716716
Session.getLLVMContext())),
717717
Builder(Session.getLLVMContext()) {
718-
M->setDataLayout(Session.getTarget().getDataLayout());
718+
M->setDataLayout(*Session.getTarget().getDataLayout());
719719
}
720720

721721
SessionContext& getSession() { return Session; }

llvm/include/llvm/Analysis/AliasAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class AliasAnalysis {
6868
/// typically called by the run* methods of these subclasses. This may be
6969
/// called multiple times.
7070
///
71-
void InitializeAliasAnalysis(Pass *P);
71+
void InitializeAliasAnalysis(Pass *P, const DataLayout *DL);
7272

7373
/// getAnalysisUsage - All alias analysis implementations should invoke this
7474
/// directly (using AliasAnalysis::getAnalysisUsage(AU)).

llvm/include/llvm/Analysis/LibCallAliasAnalysis.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_ANALYSIS_LIBCALLALIASANALYSIS_H
1616

1717
#include "llvm/Analysis/AliasAnalysis.h"
18+
#include "llvm/IR/Module.h"
1819
#include "llvm/Pass.h"
1920

2021
namespace llvm {
@@ -48,11 +49,8 @@ namespace llvm {
4849

4950
void getAnalysisUsage(AnalysisUsage &AU) const override;
5051

51-
bool runOnFunction(Function &F) override {
52-
InitializeAliasAnalysis(this); // set up super class
53-
return false;
54-
}
55-
52+
bool runOnFunction(Function &F) override;
53+
5654
/// getAdjustedAnalysisPointer - This method is used when a pass implements
5755
/// an analysis interface through multiple inheritance. If needed, it
5856
/// should override this to adjust the this pointer as needed for the

llvm/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ template <typename BaseLayerT> class LazyEmittingLayer {
183183
auto Names = llvm::make_unique<StringMap<bool>>();
184184

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

188188
for (const auto &GV : M->globals())
189189
if (addGlobalValue(*Names, GV, Mang, SearchName, ExportedSymbolsOnly))

llvm/include/llvm/IR/DataLayout.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ class DataLayout {
116116
/// \brief Primitive type alignment data.
117117
SmallVector<LayoutAlignElem, 16> Alignments;
118118

119+
/// \brief The string representation used to create this DataLayout
120+
std::string StringRepresentation;
121+
119122
typedef SmallVector<PointerAlignElem, 8> PointersTy;
120123
PointersTy Pointers;
121124

@@ -185,6 +188,7 @@ class DataLayout {
185188

186189
DataLayout &operator=(const DataLayout &DL) {
187190
clear();
191+
StringRepresentation = DL.StringRepresentation;
188192
BigEndian = DL.isBigEndian();
189193
StackNaturalAlign = DL.StackNaturalAlign;
190194
ManglingMode = DL.ManglingMode;
@@ -209,8 +213,12 @@ class DataLayout {
209213
/// \brief Returns the string representation of the DataLayout.
210214
///
211215
/// This representation is in the same format accepted by the string
212-
/// constructor above.
213-
std::string getStringRepresentation() const;
216+
/// constructor above. This should not be used to compare two DataLayout as
217+
/// different string can represent the same layout.
218+
std::string getStringRepresentation() const { return StringRepresentation; }
219+
220+
/// \brief Test if the DataLayout was constructed from an empty string.
221+
bool isDefault() const { return StringRepresentation.empty(); }
214222

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

454-
class DataLayoutPass : public ImmutablePass {
455-
DataLayout DL;
456-
457-
public:
458-
/// This has to exist, because this is a pass, but it should never be used.
459-
DataLayoutPass();
460-
~DataLayoutPass();
461-
462-
const DataLayout &getDataLayout() const { return DL; }
463-
464-
static char ID; // Pass identification, replacement for typeid
465-
466-
bool doFinalization(Module &M) override;
467-
bool doInitialization(Module &M) override;
468-
};
469-
470462
/// Used to lazily calculate structure layout information for a target machine,
471463
/// based on the DataLayout structure.
472464
class StructLayout {

llvm/include/llvm/IR/Module.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,7 @@ class Module {
219219
std::string TargetTriple; ///< Platform target triple Module compiled on
220220
///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
221221
void *NamedMDSymTab; ///< NamedMDNode names.
222-
223-
// We need to keep the string because the C API expects us to own the string
224-
// representation.
225-
// Since we have it, we also use an empty string to represent a module without
226-
// a DataLayout. If it has a DataLayout, these variables are in sync and the
227-
// string is just a cache of getDataLayout()->getStringRepresentation().
228-
std::string DataLayoutStr;
229-
DataLayout DL;
222+
DataLayout DL; ///< DataLayout associated with the module
230223

231224
friend class Constant;
232225

@@ -256,10 +249,12 @@ class Module {
256249

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

261256
/// Get the data layout for the module's target platform.
262-
const DataLayout *getDataLayout() const;
257+
const DataLayout &getDataLayout() const;
263258

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

294289
/// Set the data layout
295290
void setDataLayout(StringRef Desc);
296-
void setDataLayout(const DataLayout *Other);
291+
void setDataLayout(const DataLayout &Other);
297292

298293
/// Set the target triple.
299294
void setTargetTriple(StringRef T) { TargetTriple = T; }

llvm/include/llvm/Transforms/Utils/Cloning.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,13 @@ void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
192192
class InlineFunctionInfo {
193193
public:
194194
explicit InlineFunctionInfo(CallGraph *cg = nullptr,
195-
const DataLayout *DL = nullptr,
196195
AliasAnalysis *AA = nullptr,
197196
AssumptionCacheTracker *ACT = nullptr)
198-
: CG(cg), DL(DL), AA(AA), ACT(ACT) {}
197+
: CG(cg), AA(AA), ACT(ACT) {}
199198

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

llvm/lib/Analysis/AliasAnalysis.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,8 @@ AliasAnalysis::~AliasAnalysis() {}
462462
/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
463463
/// AliasAnalysis interface before any other methods are called.
464464
///
465-
void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
466-
DataLayoutPass *DLP = P->getAnalysisIfAvailable<DataLayoutPass>();
467-
DL = DLP ? &DLP->getDataLayout() : nullptr;
465+
void AliasAnalysis::InitializeAliasAnalysis(Pass *P, const DataLayout *NewDL) {
466+
DL = NewDL;
468467
auto *TLIP = P->getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
469468
TLI = TLIP ? &TLIP->getTLI() : nullptr;
470469
AA = &P->getAnalysis<AliasAnalysis>();

llvm/lib/Analysis/AliasAnalysisCounter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "llvm/Analysis/Passes.h"
1616
#include "llvm/Analysis/AliasAnalysis.h"
17+
#include "llvm/IR/Module.h"
1718
#include "llvm/Pass.h"
1819
#include "llvm/Support/CommandLine.h"
1920
#include "llvm/Support/Debug.h"
@@ -76,7 +77,7 @@ namespace {
7677

7778
bool runOnModule(Module &M) override {
7879
this->M = &M;
79-
InitializeAliasAnalysis(this);
80+
InitializeAliasAnalysis(this, &M.getDataLayout());
8081
return false;
8182
}
8283

llvm/lib/Analysis/AliasDebugger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace {
4444
}
4545

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

4949
for(Module::global_iterator I = M.global_begin(),
5050
E = M.global_end(); I != E; ++I) {

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,7 @@ namespace {
461461
initializeBasicAliasAnalysisPass(*PassRegistry::getPassRegistry());
462462
}
463463

464-
void initializePass() override {
465-
InitializeAliasAnalysis(this);
466-
}
464+
bool doInitialization(Module &M) override;
467465

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

816+
bool BasicAliasAnalysis::doInitialization(Module &M) {
817+
InitializeAliasAnalysis(this, &M.getDataLayout());
818+
return true;
819+
}
820+
818821
/// getModRefInfo - Check to see if the specified callsite can clobber the
819822
/// specified memory object. Since we only look at local properties of this
820823
/// function, we really can't say much about this query. We do, however, use

llvm/lib/Analysis/CFLAliasAnalysis.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ struct CFLAliasAnalysis : public ImmutablePass, public AliasAnalysis {
240240
return QueryResult;
241241
}
242242

243-
void initializePass() override { InitializeAliasAnalysis(this); }
243+
bool doInitialization(Module &M) override;
244244
};
245245

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

10351035
return AliasAnalysis::NoAlias;
10361036
}
1037+
1038+
bool CFLAliasAnalysis::doInitialization(Module &M) {
1039+
InitializeAliasAnalysis(this, &M.getDataLayout());
1040+
return true;
1041+
}

llvm/lib/Analysis/IPA/GlobalsModRef.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ namespace {
9696
}
9797

9898
bool runOnModule(Module &M) override {
99-
InitializeAliasAnalysis(this);
99+
InitializeAliasAnalysis(this, &M.getDataLayout());
100100

101101
// Find non-addr taken globals.
102102
AnalyzeGlobals(M);

llvm/lib/Analysis/IPA/InlineCost.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ bool CallAnalyzer::visitBitCast(BitCastInst &I) {
396396
}
397397

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

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

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

llvm/lib/Analysis/IVUsers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/IR/DerivedTypes.h"
2323
#include "llvm/IR/Dominators.h"
2424
#include "llvm/IR/Instructions.h"
25+
#include "llvm/IR/Module.h"
2526
#include "llvm/IR/Type.h"
2627
#include "llvm/Support/Debug.h"
2728
#include "llvm/Support/raw_ostream.h"
@@ -253,8 +254,7 @@ bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) {
253254
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
254255
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
255256
SE = &getAnalysis<ScalarEvolution>();
256-
DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
257-
DL = DLP ? &DLP->getDataLayout() : nullptr;
257+
DL = &L->getHeader()->getModule()->getDataLayout();
258258

259259
// Find all uses of induction variables in this loop, and categorize
260260
// them by stride. Start by finding all of the PHI nodes in the header for

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,7 @@ bool LazyValueInfo::runOnFunction(Function &F) {
11171117
getAnalysisIfAvailable<DominatorTreeWrapperPass>();
11181118
DT = DTWP ? &DTWP->getDomTree() : nullptr;
11191119

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

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

0 commit comments

Comments
 (0)