Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 1 | //===--- Backend.cpp - Interface to LLVM backend technologies -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Eli Friedman | 39d7c4d | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 10 | #include "clang/Frontend/ASTConsumers.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 11 | #include "clang/CodeGen/ModuleBuilder.h" |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 12 | #include "clang/Frontend/CompileOptions.h" |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
| 14 | #include "clang/AST/ASTConsumer.h" |
| 15 | #include "clang/AST/DeclGroup.h" |
| 16 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/ModuleProvider.h" |
| 19 | #include "llvm/PassManager.h" |
| 20 | #include "llvm/ADT/OwningPtr.h" |
| 21 | #include "llvm/Assembly/PrintModulePass.h" |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/CallGraph.h" |
| 23 | #include "llvm/Analysis/Verifier.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 24 | #include "llvm/Bitcode/ReaderWriter.h" |
| 25 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 26 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Compiler.h" |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 29 | #include "llvm/Support/StandardPasses.h" |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Timer.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 31 | #include "llvm/System/Path.h" |
| 32 | #include "llvm/System/Program.h" |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 33 | #include "llvm/Target/SubtargetFeature.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetData.h" |
| 35 | #include "llvm/Target/TargetMachine.h" |
| 36 | #include "llvm/Target/TargetMachineRegistry.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 37 | using namespace clang; |
| 38 | using namespace llvm; |
| 39 | |
| 40 | namespace { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 41 | class VISIBILITY_HIDDEN BackendConsumer : public ASTConsumer { |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 42 | BackendAction Action; |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 43 | CompileOptions CompileOpts; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 44 | llvm::raw_ostream *AsmOutStream; |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 45 | ASTContext *Context; |
Daniel Dunbar | 90f4130 | 2008-10-29 08:50:02 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 47 | Timer LLVMIRGeneration; |
| 48 | Timer CodeGenerationTime; |
| 49 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 50 | llvm::OwningPtr<CodeGenerator> Gen; |
| 51 | |
| 52 | llvm::Module *TheModule; |
| 53 | llvm::TargetData *TheTargetData; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 54 | |
Nuno Lopes | dd49267 | 2008-10-24 22:51:00 +0000 | [diff] [blame] | 55 | mutable llvm::ModuleProvider *ModuleProvider; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 56 | mutable FunctionPassManager *CodeGenPasses; |
| 57 | mutable PassManager *PerModulePasses; |
| 58 | mutable FunctionPassManager *PerFunctionPasses; |
| 59 | |
| 60 | FunctionPassManager *getCodeGenPasses() const; |
| 61 | PassManager *getPerModulePasses() const; |
| 62 | FunctionPassManager *getPerFunctionPasses() const; |
| 63 | |
| 64 | void CreatePasses(); |
| 65 | |
| 66 | /// AddEmitPasses - Add passes necessary to emit assembly or LLVM |
| 67 | /// IR. |
| 68 | /// |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 69 | /// \return True on success. On failure \arg Error will be set to |
| 70 | /// a user readable error message. |
Daniel Dunbar | 4c877cc | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 71 | bool AddEmitPasses(std::string &Error); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 72 | |
| 73 | void EmitAssembly(); |
| 74 | |
| 75 | public: |
| 76 | BackendConsumer(BackendAction action, Diagnostic &Diags, |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 77 | const LangOptions &langopts, const CompileOptions &compopts, |
Owen Anderson | 42253cc | 2009-07-01 17:00:06 +0000 | [diff] [blame] | 78 | const std::string &infile, llvm::raw_ostream* OS, |
| 79 | LLVMContext* C) : |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 80 | Action(action), |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 81 | CompileOpts(compopts), |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 82 | AsmOutStream(OS), |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 83 | LLVMIRGeneration("LLVM IR Generation Time"), |
| 84 | CodeGenerationTime("Code Generation Time"), |
Owen Anderson | 42253cc | 2009-07-01 17:00:06 +0000 | [diff] [blame] | 85 | Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)), |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 86 | TheModule(0), TheTargetData(0), ModuleProvider(0), |
Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 87 | CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) { |
| 88 | |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 89 | // Enable -time-passes if -ftime-report is enabled. |
Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 90 | llvm::TimePassesIsEnabled = CompileOpts.TimePasses; |
| 91 | } |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 92 | |
| 93 | ~BackendConsumer() { |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 94 | delete TheTargetData; |
Nuno Lopes | dd49267 | 2008-10-24 22:51:00 +0000 | [diff] [blame] | 95 | delete ModuleProvider; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 96 | delete CodeGenPasses; |
| 97 | delete PerModulePasses; |
| 98 | delete PerFunctionPasses; |
| 99 | } |
| 100 | |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 101 | virtual void Initialize(ASTContext &Ctx) { |
| 102 | Context = &Ctx; |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 103 | |
| 104 | if (CompileOpts.TimePasses) |
| 105 | LLVMIRGeneration.startTimer(); |
| 106 | |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 107 | Gen->Initialize(Ctx); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 108 | |
| 109 | TheModule = Gen->GetModule(); |
Nuno Lopes | 7d43a31 | 2008-10-24 23:27:18 +0000 | [diff] [blame] | 110 | ModuleProvider = new ExistingModuleProvider(TheModule); |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 111 | TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription()); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 112 | |
| 113 | if (CompileOpts.TimePasses) |
| 114 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 117 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 118 | PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(), |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 119 | Context->getSourceManager(), |
| 120 | "LLVM IR generation of declaration"); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 122 | if (CompileOpts.TimePasses) |
| 123 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 124 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 125 | Gen->HandleTopLevelDecl(D); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 126 | |
| 127 | if (CompileOpts.TimePasses) |
| 128 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 131 | virtual void HandleTranslationUnit(ASTContext &C) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 132 | { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 133 | PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 134 | if (CompileOpts.TimePasses) |
| 135 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 136 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 137 | Gen->HandleTranslationUnit(C); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 139 | if (CompileOpts.TimePasses) |
| 140 | LLVMIRGeneration.stopTimer(); |
| 141 | } |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 143 | // EmitAssembly times and registers crash info itself. |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 144 | EmitAssembly(); |
| 145 | |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 146 | // Force a flush here in case we never get released. |
| 147 | if (AsmOutStream) |
| 148 | AsmOutStream->flush(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | virtual void HandleTagDeclDefinition(TagDecl *D) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 152 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 153 | Context->getSourceManager(), |
| 154 | "LLVM IR generation of declaration"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 155 | Gen->HandleTagDeclDefinition(D); |
| 156 | } |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 157 | |
| 158 | virtual void CompleteTentativeDefinition(VarDecl *D) { |
| 159 | Gen->CompleteTentativeDefinition(D); |
| 160 | } |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 161 | }; |
| 162 | } |
| 163 | |
| 164 | FunctionPassManager *BackendConsumer::getCodeGenPasses() const { |
| 165 | if (!CodeGenPasses) { |
Nuno Lopes | dd49267 | 2008-10-24 22:51:00 +0000 | [diff] [blame] | 166 | CodeGenPasses = new FunctionPassManager(ModuleProvider); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 167 | CodeGenPasses->add(new TargetData(*TheTargetData)); |
| 168 | } |
| 169 | |
| 170 | return CodeGenPasses; |
| 171 | } |
| 172 | |
| 173 | PassManager *BackendConsumer::getPerModulePasses() const { |
| 174 | if (!PerModulePasses) { |
| 175 | PerModulePasses = new PassManager(); |
| 176 | PerModulePasses->add(new TargetData(*TheTargetData)); |
| 177 | } |
| 178 | |
| 179 | return PerModulePasses; |
| 180 | } |
| 181 | |
| 182 | FunctionPassManager *BackendConsumer::getPerFunctionPasses() const { |
| 183 | if (!PerFunctionPasses) { |
Nuno Lopes | 7d43a31 | 2008-10-24 23:27:18 +0000 | [diff] [blame] | 184 | PerFunctionPasses = new FunctionPassManager(ModuleProvider); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 185 | PerFunctionPasses->add(new TargetData(*TheTargetData)); |
| 186 | } |
| 187 | |
| 188 | return PerFunctionPasses; |
| 189 | } |
| 190 | |
Daniel Dunbar | 4c877cc | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 191 | bool BackendConsumer::AddEmitPasses(std::string &Error) { |
Daniel Dunbar | e8e2600 | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 192 | if (Action == Backend_EmitNothing) |
| 193 | return true; |
| 194 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 195 | if (Action == Backend_EmitBC) { |
Daniel Dunbar | ed2cb28 | 2008-10-22 17:40:45 +0000 | [diff] [blame] | 196 | getPerModulePasses()->add(createBitcodeWriterPass(*AsmOutStream)); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 197 | } else if (Action == Backend_EmitLL) { |
Daniel Dunbar | 11292b0 | 2008-10-22 03:28:13 +0000 | [diff] [blame] | 198 | getPerModulePasses()->add(createPrintModulePass(AsmOutStream)); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 199 | } else { |
Daniel Dunbar | 4c877cc | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 200 | bool Fast = CompileOpts.OptimizationLevel == 0; |
| 201 | |
Daniel Dunbar | 8b7650e | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 202 | // Create the TargetMachine for generating code. |
| 203 | const TargetMachineRegistry::entry *TME = |
| 204 | TargetMachineRegistry::getClosestStaticTargetForModule(*TheModule, Error); |
| 205 | if (!TME) { |
| 206 | Error = std::string("Unable to get target machine: ") + Error; |
| 207 | return false; |
| 208 | } |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 209 | |
| 210 | std::string FeaturesStr; |
| 211 | if (CompileOpts.CPU.size() || CompileOpts.Features.size()) { |
| 212 | SubtargetFeatures Features; |
| 213 | Features.setCPU(CompileOpts.CPU); |
| 214 | for (std::vector<std::string>::iterator |
| 215 | it = CompileOpts.Features.begin(), |
| 216 | ie = CompileOpts.Features.end(); it != ie; ++it) |
| 217 | Features.AddFeature(*it); |
| 218 | FeaturesStr = Features.getString(); |
| 219 | } |
| 220 | TargetMachine *TM = TME->CtorFn(*TheModule, FeaturesStr); |
Daniel Dunbar | 8b7650e | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 221 | |
| 222 | // Set register scheduler & allocation policy. |
| 223 | RegisterScheduler::setDefault(createDefaultScheduler); |
| 224 | RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator : |
| 225 | createLinearScanRegisterAllocator); |
| 226 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 227 | // From llvm-gcc: |
| 228 | // If there are passes we have to run on the entire module, we do codegen |
| 229 | // as a separate "pass" after that happens. |
| 230 | // FIXME: This is disabled right now until bugs can be worked out. Reenable |
| 231 | // this for fast -O0 compiles! |
| 232 | FunctionPassManager *PM = getCodeGenPasses(); |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 233 | CodeGenOpt::Level OptLevel = CodeGenOpt::Default; |
| 234 | |
| 235 | switch (CompileOpts.OptimizationLevel) { |
| 236 | default: break; |
| 237 | case 0: OptLevel = CodeGenOpt::None; break; |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 238 | case 3: OptLevel = CodeGenOpt::Aggressive; break; |
| 239 | } |
| 240 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 241 | // Normal mode, emit a .s file by running the code generator. |
| 242 | // Note, this also adds codegenerator level optimization passes. |
| 243 | switch (TM->addPassesToEmitFile(*PM, *AsmOutStream, |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 244 | TargetMachine::AssemblyFile, OptLevel)) { |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 245 | default: |
| 246 | case FileModel::Error: |
| 247 | Error = "Unable to interface with target machine!\n"; |
| 248 | return false; |
| 249 | case FileModel::AsmFile: |
| 250 | break; |
| 251 | } |
| 252 | |
Duncan Sands | 813a2bb | 2009-05-31 04:09:57 +0000 | [diff] [blame] | 253 | if (TM->addPassesToEmitFileFinish(*CodeGenPasses, (MachineCodeEmitter *)0, |
| 254 | OptLevel)) { |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 255 | Error = "Unable to interface with target machine!\n"; |
| 256 | return false; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | void BackendConsumer::CreatePasses() { |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 264 | // In -O0 if checking is disabled, we don't even have per-function passes. |
| 265 | if (CompileOpts.VerifyModule) |
| 266 | getPerFunctionPasses()->add(createVerifierPass()); |
| 267 | |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 268 | // Assume that standard function passes aren't run for -O0. |
| 269 | if (CompileOpts.OptimizationLevel > 0) |
| 270 | llvm::createStandardFunctionPasses(getPerFunctionPasses(), |
| 271 | CompileOpts.OptimizationLevel); |
| 272 | |
| 273 | llvm::Pass *InliningPass = 0; |
| 274 | switch (CompileOpts.Inlining) { |
| 275 | case CompileOptions::NoInlining: break; |
Eli Friedman | b9b7dd6 | 2009-06-11 20:33:41 +0000 | [diff] [blame] | 276 | case CompileOptions::NormalInlining: { |
| 277 | // Inline small functions |
| 278 | unsigned Threshold = (CompileOpts.OptimizeSize || |
| 279 | CompileOpts.OptimizationLevel < 3) ? 50 : 200; |
| 280 | InliningPass = createFunctionInliningPass(Threshold); |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 281 | break; |
Eli Friedman | b9b7dd6 | 2009-06-11 20:33:41 +0000 | [diff] [blame] | 282 | } |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 283 | case CompileOptions::OnlyAlwaysInlining: |
| 284 | InliningPass = createAlwaysInlinerPass(); // Respect always_inline |
| 285 | break; |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // For now we always create per module passes. |
| 289 | PassManager *PM = getPerModulePasses(); |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 290 | llvm::createStandardModulePasses(PM, CompileOpts.OptimizationLevel, |
| 291 | CompileOpts.OptimizeSize, |
| 292 | CompileOpts.UnitAtATime, |
| 293 | CompileOpts.UnrollLoops, |
| 294 | CompileOpts.SimplifyLibCalls, |
| 295 | /*HaveExceptions=*/true, |
| 296 | InliningPass); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /// EmitAssembly - Handle interaction with LLVM backend to generate |
| 300 | /// actual machine code. |
| 301 | void BackendConsumer::EmitAssembly() { |
| 302 | // Silently ignore if we weren't initialized for some reason. |
| 303 | if (!TheModule || !TheTargetData) |
| 304 | return; |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 8b76c0d | 2009-02-18 18:22:50 +0000 | [diff] [blame] | 306 | TimeRegion Region(CompileOpts.TimePasses ? &CodeGenerationTime : 0); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 307 | |
Daniel Dunbar | d611bac | 2008-10-27 20:40:41 +0000 | [diff] [blame] | 308 | // Make sure IR generation is happy with the module. This is |
| 309 | // released by the module provider. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 310 | Module *M = Gen->ReleaseModule(); |
| 311 | if (!M) { |
Daniel Dunbar | d611bac | 2008-10-27 20:40:41 +0000 | [diff] [blame] | 312 | // The module has been released by IR gen on failures, do not |
| 313 | // double free. |
| 314 | ModuleProvider->releaseModule(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 315 | TheModule = 0; |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | assert(TheModule == M && "Unexpected module change during IR generation"); |
| 320 | |
| 321 | CreatePasses(); |
| 322 | |
| 323 | std::string Error; |
Daniel Dunbar | 4c877cc | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 324 | if (!AddEmitPasses(Error)) { |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 325 | // FIXME: Don't fail this way. |
| 326 | llvm::cerr << "ERROR: " << Error << "\n"; |
| 327 | ::exit(1); |
| 328 | } |
| 329 | |
| 330 | // Run passes. For now we do all passes at once, but eventually we |
| 331 | // would like to have the option of streaming code generation. |
| 332 | |
| 333 | if (PerFunctionPasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 334 | PrettyStackTraceString CrashInfo("Per-function optimization"); |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 335 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 336 | PerFunctionPasses->doInitialization(); |
| 337 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 338 | if (!I->isDeclaration()) |
| 339 | PerFunctionPasses->run(*I); |
| 340 | PerFunctionPasses->doFinalization(); |
| 341 | } |
| 342 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 343 | if (PerModulePasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 344 | PrettyStackTraceString CrashInfo("Per-module optimization passes"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 345 | PerModulePasses->run(*M); |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 346 | } |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 347 | |
| 348 | if (CodeGenPasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 349 | PrettyStackTraceString CrashInfo("Code generation"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 350 | CodeGenPasses->doInitialization(); |
| 351 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 352 | if (!I->isDeclaration()) |
| 353 | CodeGenPasses->run(*I); |
| 354 | CodeGenPasses->doFinalization(); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | ASTConsumer *clang::CreateBackendConsumer(BackendAction Action, |
| 359 | Diagnostic &Diags, |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 360 | const LangOptions &LangOpts, |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 361 | const CompileOptions &CompileOpts, |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 362 | const std::string& InFile, |
Owen Anderson | 42253cc | 2009-07-01 17:00:06 +0000 | [diff] [blame] | 363 | llvm::raw_ostream* OS, |
| 364 | LLVMContext* C) { |
| 365 | return new BackendConsumer(Action, Diags, LangOpts, CompileOpts, |
| 366 | InFile, OS, C); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 367 | } |