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 | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 18 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 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 | 821e2eb | 2009-12-12 23:01:36 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetOptions.h" |
Daniel Dunbar | f7d47c0 | 2009-07-15 20:25:38 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetRegistry.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | using namespace llvm; |
| 38 | |
| 39 | namespace { |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 40 | class BackendConsumer : public ASTConsumer { |
Daniel Dunbar | 125bbbe | 2009-12-04 08:17:40 +0000 | [diff] [blame] | 41 | Diagnostic &Diags; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 42 | BackendAction Action; |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 43 | const CodeGenOptions &CodeGenOpts; |
| 44 | const LangOptions &LangOpts; |
| 45 | const TargetOptions &TargetOpts; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 46 | llvm::raw_ostream *AsmOutStream; |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 47 | llvm::formatted_raw_ostream FormattedOutStream; |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 48 | ASTContext *Context; |
Daniel Dunbar | 90f4130 | 2008-10-29 08:50:02 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 50 | Timer LLVMIRGeneration; |
| 51 | Timer CodeGenerationTime; |
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::OwningPtr<CodeGenerator> Gen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 55 | llvm::Module *TheModule; |
| 56 | llvm::TargetData *TheTargetData; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 57 | |
| 58 | mutable FunctionPassManager *CodeGenPasses; |
| 59 | mutable PassManager *PerModulePasses; |
| 60 | mutable FunctionPassManager *PerFunctionPasses; |
| 61 | |
| 62 | FunctionPassManager *getCodeGenPasses() const; |
| 63 | PassManager *getPerModulePasses() const; |
| 64 | FunctionPassManager *getPerFunctionPasses() const; |
| 65 | |
| 66 | void CreatePasses(); |
| 67 | |
Daniel Dunbar | 125bbbe | 2009-12-04 08:17:40 +0000 | [diff] [blame] | 68 | /// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 69 | /// |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 70 | /// \return True on success. |
| 71 | bool AddEmitPasses(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 72 | |
| 73 | void EmitAssembly(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
| 75 | public: |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 76 | BackendConsumer(BackendAction action, Diagnostic &_Diags, |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 77 | const LangOptions &langopts, const CodeGenOptions &compopts, |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 78 | const TargetOptions &targetopts, bool TimePasses, |
| 79 | const std::string &infile, llvm::raw_ostream *OS, |
| 80 | LLVMContext& C) : |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 81 | Diags(_Diags), |
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)), |
Jeffrey Yasskin | 8020806 | 2010-01-27 21:12:04 +0000 | [diff] [blame] | 90 | TheModule(0), TheTargetData(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; |
Jeffrey Yasskin | 8020806 | 2010-01-27 21:12:04 +0000 | [diff] [blame] | 102 | delete TheModule; |
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(); |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 117 | TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 119 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 120 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 121 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 123 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 124 | PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(), |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 125 | Context->getSourceManager(), |
| 126 | "LLVM IR generation of declaration"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 128 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 129 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 130 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 131 | Gen->HandleTopLevelDecl(D); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 132 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 133 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 134 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 135 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 137 | virtual void HandleTranslationUnit(ASTContext &C) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 138 | { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 139 | PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 140 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 141 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 142 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 143 | Gen->HandleTranslationUnit(C); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 144 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 145 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 146 | LLVMIRGeneration.stopTimer(); |
| 147 | } |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 149 | // EmitAssembly times and registers crash info itself. |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 150 | EmitAssembly(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 152 | // Force a flush here in case we never get released. |
| 153 | if (AsmOutStream) |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 154 | FormattedOutStream.flush(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 155 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 157 | virtual void HandleTagDeclDefinition(TagDecl *D) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 158 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 159 | Context->getSourceManager(), |
| 160 | "LLVM IR generation of declaration"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 161 | Gen->HandleTagDeclDefinition(D); |
| 162 | } |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 163 | |
| 164 | virtual void CompleteTentativeDefinition(VarDecl *D) { |
| 165 | Gen->CompleteTentativeDefinition(D); |
| 166 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | }; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | FunctionPassManager *BackendConsumer::getCodeGenPasses() const { |
| 171 | if (!CodeGenPasses) { |
Jeffrey Yasskin | 8020806 | 2010-01-27 21:12:04 +0000 | [diff] [blame] | 172 | CodeGenPasses = new FunctionPassManager(TheModule); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 173 | CodeGenPasses->add(new TargetData(*TheTargetData)); |
| 174 | } |
| 175 | |
| 176 | return CodeGenPasses; |
| 177 | } |
| 178 | |
| 179 | PassManager *BackendConsumer::getPerModulePasses() const { |
| 180 | if (!PerModulePasses) { |
| 181 | PerModulePasses = new PassManager(); |
| 182 | PerModulePasses->add(new TargetData(*TheTargetData)); |
| 183 | } |
| 184 | |
| 185 | return PerModulePasses; |
| 186 | } |
| 187 | |
| 188 | FunctionPassManager *BackendConsumer::getPerFunctionPasses() const { |
| 189 | if (!PerFunctionPasses) { |
Jeffrey Yasskin | 8020806 | 2010-01-27 21:12:04 +0000 | [diff] [blame] | 190 | PerFunctionPasses = new FunctionPassManager(TheModule); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 191 | PerFunctionPasses->add(new TargetData(*TheTargetData)); |
| 192 | } |
| 193 | |
| 194 | return PerFunctionPasses; |
| 195 | } |
| 196 | |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 197 | bool BackendConsumer::AddEmitPasses() { |
Daniel Dunbar | e8e2600 | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 198 | if (Action == Backend_EmitNothing) |
| 199 | return true; |
| 200 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 201 | if (Action == Backend_EmitBC) { |
Dan Gohman | b8d4239 | 2009-09-26 15:06:14 +0000 | [diff] [blame] | 202 | getPerModulePasses()->add(createBitcodeWriterPass(FormattedOutStream)); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 203 | } else if (Action == Backend_EmitLL) { |
Dan Gohman | b8d4239 | 2009-09-26 15:06:14 +0000 | [diff] [blame] | 204 | getPerModulePasses()->add(createPrintModulePass(&FormattedOutStream)); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 205 | } else { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 206 | bool Fast = CodeGenOpts.OptimizationLevel == 0; |
Daniel Dunbar | 4c877cc | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 207 | |
Daniel Dunbar | 8b7650e | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 208 | // Create the TargetMachine for generating code. |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 209 | std::string Error; |
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 | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 213 | Diags.Report(diag::err_fe_unable_to_create_target) << Error; |
Daniel Dunbar | 8b7650e | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 821e2eb | 2009-12-12 23:01:36 +0000 | [diff] [blame] | 220 | llvm::NoFramePointerElim = CodeGenOpts.DisableFPElim; |
| 221 | if (CodeGenOpts.FloatABI == "soft") |
| 222 | llvm::FloatABIType = llvm::FloatABI::Soft; |
| 223 | else if (CodeGenOpts.FloatABI == "hard") |
| 224 | llvm::FloatABIType = llvm::FloatABI::Hard; |
| 225 | else { |
| 226 | assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!"); |
| 227 | llvm::FloatABIType = llvm::FloatABI::Default; |
| 228 | } |
| 229 | NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; |
| 230 | llvm::UseSoftFloat = CodeGenOpts.SoftFloat; |
| 231 | UnwindTablesMandatory = CodeGenOpts.UnwindTables; |
| 232 | |
| 233 | TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose); |
| 234 | |
| 235 | // FIXME: Parse this earlier. |
| 236 | if (CodeGenOpts.RelocationModel == "static") { |
| 237 | TargetMachine::setRelocationModel(llvm::Reloc::Static); |
| 238 | } else if (CodeGenOpts.RelocationModel == "pic") { |
| 239 | TargetMachine::setRelocationModel(llvm::Reloc::PIC_); |
| 240 | } else { |
| 241 | assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" && |
| 242 | "Invalid PIC model!"); |
| 243 | TargetMachine::setRelocationModel(llvm::Reloc::DynamicNoPIC); |
| 244 | } |
| 245 | // FIXME: Parse this earlier. |
| 246 | if (CodeGenOpts.CodeModel == "small") { |
| 247 | TargetMachine::setCodeModel(llvm::CodeModel::Small); |
| 248 | } else if (CodeGenOpts.CodeModel == "kernel") { |
| 249 | TargetMachine::setCodeModel(llvm::CodeModel::Kernel); |
| 250 | } else if (CodeGenOpts.CodeModel == "medium") { |
| 251 | TargetMachine::setCodeModel(llvm::CodeModel::Medium); |
| 252 | } else if (CodeGenOpts.CodeModel == "large") { |
| 253 | TargetMachine::setCodeModel(llvm::CodeModel::Large); |
| 254 | } else { |
| 255 | assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!"); |
| 256 | TargetMachine::setCodeModel(llvm::CodeModel::Default); |
| 257 | } |
| 258 | |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 259 | std::vector<const char *> BackendArgs; |
| 260 | BackendArgs.push_back("clang"); // Fake program name. |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 261 | if (!CodeGenOpts.DebugPass.empty()) { |
| 262 | BackendArgs.push_back("-debug-pass"); |
| 263 | BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); |
| 264 | } |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 265 | if (!CodeGenOpts.LimitFloatPrecision.empty()) { |
| 266 | BackendArgs.push_back("-limit-float-precision"); |
| 267 | BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); |
| 268 | } |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 269 | if (llvm::TimePassesIsEnabled) |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 270 | BackendArgs.push_back("-time-passes"); |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 271 | BackendArgs.push_back(0); |
| 272 | llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, |
| 273 | (char**) &BackendArgs[0]); |
| 274 | |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 275 | std::string FeaturesStr; |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 276 | if (TargetOpts.CPU.size() || TargetOpts.Features.size()) { |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 277 | SubtargetFeatures Features; |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 278 | Features.setCPU(TargetOpts.CPU); |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 279 | for (std::vector<std::string>::const_iterator |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 280 | it = TargetOpts.Features.begin(), |
| 281 | ie = TargetOpts.Features.end(); it != ie; ++it) |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 282 | Features.AddFeature(*it); |
| 283 | FeaturesStr = Features.getString(); |
| 284 | } |
Daniel Dunbar | 2b1f59f | 2009-08-04 04:02:57 +0000 | [diff] [blame] | 285 | TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Daniel Dunbar | 8b7650e | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 287 | // Set register scheduler & allocation policy. |
| 288 | RegisterScheduler::setDefault(createDefaultScheduler); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator : |
| 290 | createLinearScanRegisterAllocator); |
Daniel Dunbar | 8b7650e | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 291 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 292 | // From llvm-gcc: |
| 293 | // If there are passes we have to run on the entire module, we do codegen |
| 294 | // as a separate "pass" after that happens. |
| 295 | // FIXME: This is disabled right now until bugs can be worked out. Reenable |
| 296 | // this for fast -O0 compiles! |
| 297 | FunctionPassManager *PM = getCodeGenPasses(); |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 298 | CodeGenOpt::Level OptLevel = CodeGenOpt::Default; |
| 299 | |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 300 | switch (CodeGenOpts.OptimizationLevel) { |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 301 | default: break; |
| 302 | case 0: OptLevel = CodeGenOpt::None; break; |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 303 | case 3: OptLevel = CodeGenOpt::Aggressive; break; |
| 304 | } |
| 305 | |
Daniel Dunbar | da1573f | 2010-02-03 01:18:43 +0000 | [diff] [blame] | 306 | // Normal mode, emit a .s or .o file by running the code generator. Note, |
| 307 | // this also adds codegenerator level optimization passes. |
| 308 | TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile; |
| 309 | if (Action == Backend_EmitObj) |
| 310 | CGFT = TargetMachine::CGFT_ObjectFile; |
Chris Lattner | cd0507c | 2010-02-03 05:55:22 +0000 | [diff] [blame] | 311 | if (TM->addPassesToEmitFile(*PM, FormattedOutStream, CGFT, OptLevel)) { |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 312 | Diags.Report(diag::err_fe_unable_to_interface_with_target); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 313 | return false; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 314 | } |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | void BackendConsumer::CreatePasses() { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 321 | unsigned OptLevel = CodeGenOpts.OptimizationLevel; |
| 322 | CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining; |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 323 | |
| 324 | // Handle disabling of LLVM optimization, where we want to preserve the |
| 325 | // internal module before any optimization. |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 326 | if (CodeGenOpts.DisableLLVMOpts) { |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 327 | OptLevel = 0; |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 328 | Inlining = CodeGenOpts.NoInlining; |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 331 | // 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] | 332 | if (CodeGenOpts.VerifyModule) |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 333 | getPerFunctionPasses()->add(createVerifierPass()); |
| 334 | |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 335 | // Assume that standard function passes aren't run for -O0. |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 336 | if (OptLevel > 0) |
| 337 | llvm::createStandardFunctionPasses(getPerFunctionPasses(), OptLevel); |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 338 | |
| 339 | llvm::Pass *InliningPass = 0; |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 340 | switch (Inlining) { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 341 | case CodeGenOptions::NoInlining: break; |
| 342 | case CodeGenOptions::NormalInlining: { |
Daniel Dunbar | 90de51f | 2009-12-08 23:15:55 +0000 | [diff] [blame] | 343 | // Set the inline threshold following llvm-gcc. |
| 344 | // |
| 345 | // FIXME: Derive these constants in a principled fashion. |
| 346 | unsigned Threshold = 200; |
| 347 | if (CodeGenOpts.OptimizeSize) |
| 348 | Threshold = 50; |
| 349 | else if (OptLevel > 2) |
| 350 | Threshold = 250; |
Eli Friedman | b9b7dd6 | 2009-06-11 20:33:41 +0000 | [diff] [blame] | 351 | InliningPass = createFunctionInliningPass(Threshold); |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 352 | break; |
Eli Friedman | b9b7dd6 | 2009-06-11 20:33:41 +0000 | [diff] [blame] | 353 | } |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 354 | case CodeGenOptions::OnlyAlwaysInlining: |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 355 | InliningPass = createAlwaysInlinerPass(); // Respect always_inline |
| 356 | break; |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // For now we always create per module passes. |
| 360 | PassManager *PM = getPerModulePasses(); |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 361 | llvm::createStandardModulePasses(PM, OptLevel, CodeGenOpts.OptimizeSize, |
| 362 | CodeGenOpts.UnitAtATime, |
| 363 | CodeGenOpts.UnrollLoops, |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 364 | /*SimplifyLibCalls=*/!LangOpts.NoBuiltin, |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 365 | /*HaveExceptions=*/true, |
| 366 | InliningPass); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /// EmitAssembly - Handle interaction with LLVM backend to generate |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | /// actual machine code. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 371 | void BackendConsumer::EmitAssembly() { |
| 372 | // Silently ignore if we weren't initialized for some reason. |
| 373 | if (!TheModule || !TheTargetData) |
| 374 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 376 | TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 377 | |
Daniel Dunbar | d611bac | 2008-10-27 20:40:41 +0000 | [diff] [blame] | 378 | // Make sure IR generation is happy with the module. This is |
| 379 | // released by the module provider. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 380 | Module *M = Gen->ReleaseModule(); |
| 381 | if (!M) { |
Daniel Dunbar | d611bac | 2008-10-27 20:40:41 +0000 | [diff] [blame] | 382 | // The module has been released by IR gen on failures, do not |
| 383 | // double free. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 384 | TheModule = 0; |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | assert(TheModule == M && "Unexpected module change during IR generation"); |
| 389 | |
| 390 | CreatePasses(); |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 391 | if (!AddEmitPasses()) |
| 392 | return; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 393 | |
| 394 | // Run passes. For now we do all passes at once, but eventually we |
| 395 | // would like to have the option of streaming code generation. |
| 396 | |
| 397 | if (PerFunctionPasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 398 | PrettyStackTraceString CrashInfo("Per-function optimization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 400 | PerFunctionPasses->doInitialization(); |
| 401 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 402 | if (!I->isDeclaration()) |
| 403 | PerFunctionPasses->run(*I); |
| 404 | PerFunctionPasses->doFinalization(); |
| 405 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 407 | if (PerModulePasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 408 | PrettyStackTraceString CrashInfo("Per-module optimization passes"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 409 | PerModulePasses->run(*M); |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 410 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 412 | if (CodeGenPasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 413 | PrettyStackTraceString CrashInfo("Code generation"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 414 | CodeGenPasses->doInitialization(); |
| 415 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 416 | if (!I->isDeclaration()) |
| 417 | CodeGenPasses->run(*I); |
| 418 | CodeGenPasses->doFinalization(); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | ASTConsumer *clang::CreateBackendConsumer(BackendAction Action, |
| 423 | Diagnostic &Diags, |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 424 | const LangOptions &LangOpts, |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 425 | const CodeGenOptions &CodeGenOpts, |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 426 | const TargetOptions &TargetOpts, |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 427 | bool TimePasses, |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 428 | const std::string& InFile, |
Owen Anderson | 42253cc | 2009-07-01 17:00:06 +0000 | [diff] [blame] | 429 | llvm::raw_ostream* OS, |
Owen Anderson | 8f1ca78 | 2009-07-01 23:14:14 +0000 | [diff] [blame] | 430 | LLVMContext& C) { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 431 | return new BackendConsumer(Action, Diags, LangOpts, CodeGenOpts, |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 432 | TargetOpts, TimePasses, InFile, OS, C); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 433 | } |