1 //===---------- LTOPostIPODriver.h - PostIPO Driver -----------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file defines the PostIPODriver class which is the driver for Post-IPO
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Analysis/AliasAnalysis.h"
14 #include "llvm/IR/DataLayout.h"
15 #include "llvm/PassManager.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/FormattedStream.h"
18 #include "llvm/Support/Program.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Transforms/ObjCARC.h"
22 #include "LTOPartition.h"
23 #include "LTOPostIPODriver.h"
28 // /////////////////////////////////////////////////////////////////////////////
30 // Declare all variants of Post-IPO drivers
32 // /////////////////////////////////////////////////////////////////////////////
35 /// \breif Base class for all driver variants.
37 class PostIPODrvBaseImpl {
39 PostIPODrvBaseImpl(TargetMachine *Targ, IPOPartMgr &IPM, IPOFileMgr &IFM,
41 PartMgr(IPM), FileMgr(IFM), MergedObjFile(0), Target(Targ),
42 MergeObjs(ToMergeObjs) {}
44 virtual ~PostIPODrvBaseImpl() {};
46 IPOPartMgr &getPartitionMgr() { return PartMgr; }
47 IPOFileMgr &getFileMgr() { return FileMgr; }
49 // Implement the PostIPODriver::getSingleObjFile()
50 virtual IPOFile *getSingleObjFile() const = 0;
52 bool IsToMergeObj() const { return MergeObjs; }
54 virtual bool Compile(std::string &ErrMsg) = 0;
57 // Populate post-IPO scalar optimization pass manager
58 bool PopulatePostIPOOptPM(PassManager &PM);
60 // Populate post-IPO machine-specific CodeGen pass manager
61 bool PopulateCodeGenPM(PassManager &PM, formatted_raw_ostream &OutFile,
67 IPOFile *MergedObjFile;
68 TargetMachine *Target;
72 /// \breif PostIPO driver for the compiling the entire program without
74 class PostIPODrvSerial : public PostIPODrvBaseImpl {
76 PostIPODrvSerial(TargetMachine *T, IPOPartMgr &IPM, IPOFileMgr &IFM,
78 PostIPODrvBaseImpl(T, IPM, IFM, ToMergeObjs) {}
80 virtual bool Compile(std::string &ErrMsg);
81 virtual IPOFile *getSingleObjFile() const;
84 Module *getModule() const { return (*PartMgr.begin())->getModule(); }
88 // ////////////////////////////////////////////////////////////////////////////
90 // Implemetation of PostIPODriver
92 // ////////////////////////////////////////////////////////////////////////////
94 PostIPODriver::PostIPODriver(VariantTy V, TargetMachine *TM, IPOPartMgr &IPM,
95 IPOFileMgr &IFM, bool ToMergeObjs) {
97 DrvImpl = new PostIPODrvSerial(TM, IPM, IFM, ToMergeObjs);
99 assert(false && "TBD");
102 bool PostIPODriver::Compile(std::string &ErrMsg) {
103 PostIPODrvBaseImpl *P = static_cast<PostIPODrvBaseImpl *>(DrvImpl);
104 return P->Compile(ErrMsg);
107 IPOFile *PostIPODriver::getSingleObjFile() const {
108 PostIPODrvBaseImpl *P = static_cast<PostIPODrvBaseImpl *>(DrvImpl);
109 return P->getSingleObjFile();
112 // ////////////////////////////////////////////////////////////////////////////
114 // Implemetation of PostIPODrvBaseImpl
116 // ////////////////////////////////////////////////////////////////////////////
118 bool PostIPODrvBaseImpl::PopulatePostIPOOptPM(PassManager &PM) {
123 bool PostIPODrvBaseImpl::PopulateCodeGenPM(PassManager &PM,
124 formatted_raw_ostream &OutFile,
126 PM.add(new DataLayout(*Target->getDataLayout()));
127 Target->addAnalysisPasses(PM);
129 // If the bitcode files contain ARC code and were compiled with optimization,
130 // the ObjCARCContractPass must be run, so do it unconditionally here.
131 PM.add(createObjCARCContractPass());
133 if (Target->addPassesToEmitFile(PM, OutFile,
134 TargetMachine::CGFT_ObjectFile)) {
135 Err = "target file type not supported";
141 // ////////////////////////////////////////////////////////////////////////////
143 // Implemetation of PostIPODrvSerial
145 // ////////////////////////////////////////////////////////////////////////////
147 bool PostIPODrvSerial::Compile(std::string &ErrMsg) {
148 Module *M = getModule();
150 // Step 1: Run the post-IPO scalar optimizations
153 PopulatePostIPOOptPM(SoptPM);
157 // Step 2: Run the post-IPO machine-specific code-generation passes
159 IPOFile &Obj = (*PartMgr.begin())->getObjFile();
160 raw_fd_ostream ros(Obj.getPath().c_str(), Obj.getLastErrStr(),
162 formatted_raw_ostream OutFile(ros);
164 PassManager CodGenPM;
165 if (!PopulateCodeGenPM(CodGenPM, OutFile, ErrMsg)) {
166 ErrMsg += Obj.getLastErrStr();
176 IPOFile *PostIPODrvSerial::getSingleObjFile() const {
177 assert(!MergedObjFile && "No need to *merge* a single object file");
178 IPOPartition *P = *PartMgr.begin();
179 return &P->getObjFile();