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" |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclGroup.h" |
| 14 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 15 | #include "clang/Basic/TargetOptions.h" |
| 16 | #include "clang/CodeGen/CodeGenOptions.h" |
| 17 | #include "clang/CodeGen/ModuleBuilder.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/ModuleProvider.h" |
| 20 | #include "llvm/PassManager.h" |
| 21 | #include "llvm/ADT/OwningPtr.h" |
| 22 | #include "llvm/Assembly/PrintModulePass.h" |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/CallGraph.h" |
| 24 | #include "llvm/Analysis/Verifier.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 25 | #include "llvm/Bitcode/ReaderWriter.h" |
| 26 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 27 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 28 | #include "llvm/Support/FormattedStream.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 | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 31 | #include "llvm/Target/SubtargetFeature.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetData.h" |
| 33 | #include "llvm/Target/TargetMachine.h" |
Daniel Dunbar | f7d47c0 | 2009-07-15 20:25:38 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetRegistry.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 35 | using namespace clang; |
| 36 | using namespace llvm; |
| 37 | |
| 38 | namespace { |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 39 | class BackendConsumer : public ASTConsumer { |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 40 | BackendAction Action; |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 41 | const CodeGenOptions &CodeGenOpts; |
| 42 | const LangOptions &LangOpts; |
| 43 | const TargetOptions &TargetOpts; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 44 | llvm::raw_ostream *AsmOutStream; |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 45 | llvm::formatted_raw_ostream FormattedOutStream; |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 46 | ASTContext *Context; |
Daniel Dunbar | 90f4130 | 2008-10-29 08:50:02 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 48 | Timer LLVMIRGeneration; |
| 49 | Timer CodeGenerationTime; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 51 | llvm::OwningPtr<CodeGenerator> Gen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 53 | llvm::Module *TheModule; |
| 54 | llvm::TargetData *TheTargetData; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 55 | |
Nuno Lopes | dd49267 | 2008-10-24 22:51:00 +0000 | [diff] [blame] | 56 | mutable llvm::ModuleProvider *ModuleProvider; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 57 | mutable FunctionPassManager *CodeGenPasses; |
| 58 | mutable PassManager *PerModulePasses; |
| 59 | mutable FunctionPassManager *PerFunctionPasses; |
| 60 | |
| 61 | FunctionPassManager *getCodeGenPasses() const; |
| 62 | PassManager *getPerModulePasses() const; |
| 63 | FunctionPassManager *getPerFunctionPasses() const; |
| 64 | |
| 65 | void CreatePasses(); |
| 66 | |
| 67 | /// AddEmitPasses - Add passes necessary to emit assembly or LLVM |
| 68 | /// IR. |
| 69 | /// |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 70 | /// \return True on success. On failure \arg Error will be set to |
| 71 | /// a user readable error message. |
Daniel Dunbar | 4c877cc | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 72 | bool AddEmitPasses(std::string &Error); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 73 | |
| 74 | void EmitAssembly(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
| 76 | public: |
| 77 | BackendConsumer(BackendAction action, Diagnostic &Diags, |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 78 | const LangOptions &langopts, const CodeGenOptions &compopts, |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 79 | const TargetOptions &targetopts, bool TimePasses, |
| 80 | const std::string &infile, llvm::raw_ostream *OS, |
| 81 | LLVMContext& C) : |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | Action(action), |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 83 | CodeGenOpts(compopts), |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 84 | LangOpts(langopts), |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 85 | TargetOpts(targetopts), |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 86 | AsmOutStream(OS), |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 87 | LLVMIRGeneration("LLVM IR Generation Time"), |
| 88 | CodeGenerationTime("Code Generation Time"), |
Owen Anderson | 42253cc | 2009-07-01 17:00:06 +0000 | [diff] [blame] | 89 | Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)), |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 90 | TheModule(0), TheTargetData(0), ModuleProvider(0), |
Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 91 | CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 93 | if (AsmOutStream) |
| 94 | FormattedOutStream.setStream(*AsmOutStream, |
| 95 | formatted_raw_ostream::PRESERVE_STREAM); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 97 | llvm::TimePassesIsEnabled = TimePasses; |
Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 98 | } |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 99 | |
| 100 | ~BackendConsumer() { |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 101 | delete TheTargetData; |
Nuno Lopes | dd49267 | 2008-10-24 22:51:00 +0000 | [diff] [blame] | 102 | delete ModuleProvider; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 103 | delete CodeGenPasses; |
| 104 | delete PerModulePasses; |
| 105 | delete PerFunctionPasses; |
| 106 | } |
| 107 | |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 108 | virtual void Initialize(ASTContext &Ctx) { |
| 109 | Context = &Ctx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 111 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 112 | LLVMIRGeneration.startTimer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 114 | Gen->Initialize(Ctx); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 115 | |
| 116 | TheModule = Gen->GetModule(); |
Nuno Lopes | 7d43a31 | 2008-10-24 23:27:18 +0000 | [diff] [blame] | 117 | ModuleProvider = new ExistingModuleProvider(TheModule); |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 118 | TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 120 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 121 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 124 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 125 | PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(), |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 126 | Context->getSourceManager(), |
| 127 | "LLVM IR generation of declaration"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 129 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 130 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 131 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 132 | Gen->HandleTopLevelDecl(D); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 133 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 134 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 135 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 136 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 138 | virtual void HandleTranslationUnit(ASTContext &C) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 139 | { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 140 | PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 141 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 142 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 143 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 144 | Gen->HandleTranslationUnit(C); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 145 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 146 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 147 | LLVMIRGeneration.stopTimer(); |
| 148 | } |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 150 | // EmitAssembly times and registers crash info itself. |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 151 | EmitAssembly(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 153 | // Force a flush here in case we never get released. |
| 154 | if (AsmOutStream) |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 155 | FormattedOutStream.flush(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 158 | virtual void HandleTagDeclDefinition(TagDecl *D) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 159 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 160 | Context->getSourceManager(), |
| 161 | "LLVM IR generation of declaration"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 162 | Gen->HandleTagDeclDefinition(D); |
| 163 | } |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 164 | |
| 165 | virtual void CompleteTentativeDefinition(VarDecl *D) { |
| 166 | Gen->CompleteTentativeDefinition(D); |
| 167 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | }; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | FunctionPassManager *BackendConsumer::getCodeGenPasses() const { |
| 172 | if (!CodeGenPasses) { |
Nuno Lopes | dd49267 | 2008-10-24 22:51:00 +0000 | [diff] [blame] | 173 | CodeGenPasses = new FunctionPassManager(ModuleProvider); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 174 | CodeGenPasses->add(new TargetData(*TheTargetData)); |
| 175 | } |
| 176 | |
| 177 | return CodeGenPasses; |
| 178 | } |
| 179 | |
| 180 | PassManager *BackendConsumer::getPerModulePasses() const { |
| 181 | if (!PerModulePasses) { |
| 182 | PerModulePasses = new PassManager(); |
| 183 | PerModulePasses->add(new TargetData(*TheTargetData)); |
| 184 | } |
| 185 | |
| 186 | return PerModulePasses; |
| 187 | } |
| 188 | |
| 189 | FunctionPassManager *BackendConsumer::getPerFunctionPasses() const { |
| 190 | if (!PerFunctionPasses) { |
Nuno Lopes | 7d43a31 | 2008-10-24 23:27:18 +0000 | [diff] [blame] | 191 | PerFunctionPasses = new FunctionPassManager(ModuleProvider); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 192 | PerFunctionPasses->add(new TargetData(*TheTargetData)); |
| 193 | } |
| 194 | |
| 195 | return PerFunctionPasses; |
| 196 | } |
| 197 | |
Daniel Dunbar | 4c877cc | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 198 | bool BackendConsumer::AddEmitPasses(std::string &Error) { |
Daniel Dunbar | e8e2600 | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 199 | if (Action == Backend_EmitNothing) |
| 200 | return true; |
| 201 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 202 | if (Action == Backend_EmitBC) { |
Dan Gohman | b8d4239 | 2009-09-26 15:06:14 +0000 | [diff] [blame] | 203 | getPerModulePasses()->add(createBitcodeWriterPass(FormattedOutStream)); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 204 | } else if (Action == Backend_EmitLL) { |
Dan Gohman | b8d4239 | 2009-09-26 15:06:14 +0000 | [diff] [blame] | 205 | getPerModulePasses()->add(createPrintModulePass(&FormattedOutStream)); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 206 | } else { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 207 | bool Fast = CodeGenOpts.OptimizationLevel == 0; |
Daniel Dunbar | 4c877cc | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 208 | |
Daniel Dunbar | 8b7650e | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 209 | // Create the TargetMachine for generating code. |
Daniel Dunbar | 82cfa7a | 2009-07-26 01:27:26 +0000 | [diff] [blame] | 210 | std::string Triple = TheModule->getTargetTriple(); |
Daniel Dunbar | 9ab76fa | 2009-08-03 04:21:41 +0000 | [diff] [blame] | 211 | const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); |
Daniel Dunbar | f7d47c0 | 2009-07-15 20:25:38 +0000 | [diff] [blame] | 212 | if (!TheTarget) { |
Daniel Dunbar | 8b7650e | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 213 | Error = std::string("Unable to get target machine: ") + Error; |
| 214 | return false; |
| 215 | } |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 216 | |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 217 | // FIXME: Expose these capabilities via actual APIs!!!! Aside from just |
| 218 | // being gross, this is also totally broken if we ever care about |
| 219 | // concurrency. |
| 220 | std::vector<const char *> BackendArgs; |
| 221 | BackendArgs.push_back("clang"); // Fake program name. |
| 222 | if (CodeGenOpts.AsmVerbose) |
| 223 | BackendArgs.push_back("-asm-verbose"); |
| 224 | if (!CodeGenOpts.CodeModel.empty()) { |
| 225 | BackendArgs.push_back("-code-model"); |
| 226 | BackendArgs.push_back(CodeGenOpts.CodeModel.c_str()); |
| 227 | } |
| 228 | if (!CodeGenOpts.DebugPass.empty()) { |
| 229 | BackendArgs.push_back("-debug-pass"); |
| 230 | BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); |
| 231 | } |
| 232 | if (CodeGenOpts.DisableFPElim) |
| 233 | BackendArgs.push_back("-disable-fp-elim"); |
Daniel Dunbar | 3b31526 | 2009-11-30 08:42:00 +0000 | [diff] [blame^] | 234 | if (!CodeGenOpts.FloatABI.empty()) { |
| 235 | BackendArgs.push_back("-float-abi"); |
| 236 | BackendArgs.push_back(CodeGenOpts.FloatABI.c_str()); |
| 237 | } |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 238 | if (!CodeGenOpts.LimitFloatPrecision.empty()) { |
| 239 | BackendArgs.push_back("-limit-float-precision"); |
| 240 | BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); |
| 241 | } |
| 242 | if (CodeGenOpts.NoZeroInitializedInBSS) |
| 243 | BackendArgs.push_back("-nozero-initialized-in-bss"); |
Daniel Dunbar | 3b31526 | 2009-11-30 08:42:00 +0000 | [diff] [blame^] | 244 | if (CodeGenOpts.SoftFloat) |
| 245 | BackendArgs.push_back("-soft-float"); |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 246 | BackendArgs.push_back("-relocation-model"); |
| 247 | BackendArgs.push_back(CodeGenOpts.RelocationModel.c_str()); |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 248 | if (llvm::TimePassesIsEnabled) |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 249 | BackendArgs.push_back("-time-passes"); |
| 250 | if (CodeGenOpts.UnwindTables) |
| 251 | BackendArgs.push_back("-unwind-tables"); |
| 252 | BackendArgs.push_back(0); |
| 253 | llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, |
| 254 | (char**) &BackendArgs[0]); |
| 255 | |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 256 | std::string FeaturesStr; |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 257 | if (TargetOpts.CPU.size() || TargetOpts.Features.size()) { |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 258 | SubtargetFeatures Features; |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 259 | Features.setCPU(TargetOpts.CPU); |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 260 | for (std::vector<std::string>::const_iterator |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 261 | it = TargetOpts.Features.begin(), |
| 262 | ie = TargetOpts.Features.end(); it != ie; ++it) |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 263 | Features.AddFeature(*it); |
| 264 | FeaturesStr = Features.getString(); |
| 265 | } |
Daniel Dunbar | 2b1f59f | 2009-08-04 04:02:57 +0000 | [diff] [blame] | 266 | TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Daniel Dunbar | 8b7650e | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 268 | // Set register scheduler & allocation policy. |
| 269 | RegisterScheduler::setDefault(createDefaultScheduler); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator : |
| 271 | createLinearScanRegisterAllocator); |
Daniel Dunbar | 8b7650e | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 272 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 273 | // From llvm-gcc: |
| 274 | // If there are passes we have to run on the entire module, we do codegen |
| 275 | // as a separate "pass" after that happens. |
| 276 | // FIXME: This is disabled right now until bugs can be worked out. Reenable |
| 277 | // this for fast -O0 compiles! |
| 278 | FunctionPassManager *PM = getCodeGenPasses(); |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 279 | CodeGenOpt::Level OptLevel = CodeGenOpt::Default; |
| 280 | |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 281 | switch (CodeGenOpts.OptimizationLevel) { |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 282 | default: break; |
| 283 | case 0: OptLevel = CodeGenOpt::None; break; |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 284 | case 3: OptLevel = CodeGenOpt::Aggressive; break; |
| 285 | } |
| 286 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 287 | // Normal mode, emit a .s file by running the code generator. |
| 288 | // Note, this also adds codegenerator level optimization passes. |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 289 | switch (TM->addPassesToEmitFile(*PM, FormattedOutStream, |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 290 | TargetMachine::AssemblyFile, OptLevel)) { |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 291 | default: |
| 292 | case FileModel::Error: |
| 293 | Error = "Unable to interface with target machine!\n"; |
| 294 | return false; |
| 295 | case FileModel::AsmFile: |
| 296 | break; |
| 297 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
Duncan Sands | 813a2bb | 2009-05-31 04:09:57 +0000 | [diff] [blame] | 299 | if (TM->addPassesToEmitFileFinish(*CodeGenPasses, (MachineCodeEmitter *)0, |
| 300 | OptLevel)) { |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 301 | Error = "Unable to interface with target machine!\n"; |
| 302 | return false; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | void BackendConsumer::CreatePasses() { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 310 | unsigned OptLevel = CodeGenOpts.OptimizationLevel; |
| 311 | CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining; |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 312 | |
| 313 | // Handle disabling of LLVM optimization, where we want to preserve the |
| 314 | // internal module before any optimization. |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 315 | if (CodeGenOpts.DisableLLVMOpts) { |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 316 | OptLevel = 0; |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 317 | Inlining = CodeGenOpts.NoInlining; |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 320 | // In -O0 if checking is disabled, we don't even have per-function passes. |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 321 | if (CodeGenOpts.VerifyModule) |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 322 | getPerFunctionPasses()->add(createVerifierPass()); |
| 323 | |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 324 | // Assume that standard function passes aren't run for -O0. |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 325 | if (OptLevel > 0) |
| 326 | llvm::createStandardFunctionPasses(getPerFunctionPasses(), OptLevel); |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 327 | |
| 328 | llvm::Pass *InliningPass = 0; |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 329 | switch (Inlining) { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 330 | case CodeGenOptions::NoInlining: break; |
| 331 | case CodeGenOptions::NormalInlining: { |
Eli Friedman | b9b7dd6 | 2009-06-11 20:33:41 +0000 | [diff] [blame] | 332 | // Inline small functions |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 333 | unsigned Threshold = (CodeGenOpts.OptimizeSize || OptLevel < 3) ? 50 : 200; |
Eli Friedman | b9b7dd6 | 2009-06-11 20:33:41 +0000 | [diff] [blame] | 334 | InliningPass = createFunctionInliningPass(Threshold); |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 335 | break; |
Eli Friedman | b9b7dd6 | 2009-06-11 20:33:41 +0000 | [diff] [blame] | 336 | } |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 337 | case CodeGenOptions::OnlyAlwaysInlining: |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 338 | InliningPass = createAlwaysInlinerPass(); // Respect always_inline |
| 339 | break; |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | // For now we always create per module passes. |
| 343 | PassManager *PM = getPerModulePasses(); |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 344 | llvm::createStandardModulePasses(PM, OptLevel, CodeGenOpts.OptimizeSize, |
| 345 | CodeGenOpts.UnitAtATime, |
| 346 | CodeGenOpts.UnrollLoops, |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 347 | /*SimplifyLibCalls=*/!LangOpts.NoBuiltin, |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 348 | /*HaveExceptions=*/true, |
| 349 | InliningPass); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /// EmitAssembly - Handle interaction with LLVM backend to generate |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | /// actual machine code. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 354 | void BackendConsumer::EmitAssembly() { |
| 355 | // Silently ignore if we weren't initialized for some reason. |
| 356 | if (!TheModule || !TheTargetData) |
| 357 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 359 | TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 360 | |
Daniel Dunbar | d611bac | 2008-10-27 20:40:41 +0000 | [diff] [blame] | 361 | // Make sure IR generation is happy with the module. This is |
| 362 | // released by the module provider. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 363 | Module *M = Gen->ReleaseModule(); |
| 364 | if (!M) { |
Daniel Dunbar | d611bac | 2008-10-27 20:40:41 +0000 | [diff] [blame] | 365 | // The module has been released by IR gen on failures, do not |
| 366 | // double free. |
| 367 | ModuleProvider->releaseModule(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 368 | TheModule = 0; |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | assert(TheModule == M && "Unexpected module change during IR generation"); |
| 373 | |
| 374 | CreatePasses(); |
| 375 | |
| 376 | std::string Error; |
Daniel Dunbar | 4c877cc | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 377 | if (!AddEmitPasses(Error)) { |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 378 | // FIXME: Don't fail this way. |
Chris Lattner | a85b352 | 2009-08-23 05:57:09 +0000 | [diff] [blame] | 379 | llvm::errs() << "ERROR: " << Error << "\n"; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 380 | ::exit(1); |
| 381 | } |
| 382 | |
| 383 | // Run passes. For now we do all passes at once, but eventually we |
| 384 | // would like to have the option of streaming code generation. |
| 385 | |
| 386 | if (PerFunctionPasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 387 | PrettyStackTraceString CrashInfo("Per-function optimization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 389 | PerFunctionPasses->doInitialization(); |
| 390 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 391 | if (!I->isDeclaration()) |
| 392 | PerFunctionPasses->run(*I); |
| 393 | PerFunctionPasses->doFinalization(); |
| 394 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 396 | if (PerModulePasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 397 | PrettyStackTraceString CrashInfo("Per-module optimization passes"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 398 | PerModulePasses->run(*M); |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 399 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 401 | if (CodeGenPasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 402 | PrettyStackTraceString CrashInfo("Code generation"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 403 | CodeGenPasses->doInitialization(); |
| 404 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 405 | if (!I->isDeclaration()) |
| 406 | CodeGenPasses->run(*I); |
| 407 | CodeGenPasses->doFinalization(); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | ASTConsumer *clang::CreateBackendConsumer(BackendAction Action, |
| 412 | Diagnostic &Diags, |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 413 | const LangOptions &LangOpts, |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 414 | const CodeGenOptions &CodeGenOpts, |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 415 | const TargetOptions &TargetOpts, |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 416 | bool TimePasses, |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 417 | const std::string& InFile, |
Owen Anderson | 42253cc | 2009-07-01 17:00:06 +0000 | [diff] [blame] | 418 | llvm::raw_ostream* OS, |
Owen Anderson | 8f1ca78 | 2009-07-01 23:14:14 +0000 | [diff] [blame] | 419 | LLVMContext& C) { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 420 | return new BackendConsumer(Action, Diags, LangOpts, CodeGenOpts, |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 421 | TargetOpts, TimePasses, InFile, OS, C); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 422 | } |