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