Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 1 | //===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===// |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 10 | #include "clang/Frontend/CodeGenAction.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 | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 18 | #include "clang/Frontend/ASTConsumers.h" |
| 19 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 22 | #include "llvm/PassManager.h" |
| 23 | #include "llvm/ADT/OwningPtr.h" |
| 24 | #include "llvm/Assembly/PrintModulePass.h" |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/CallGraph.h" |
| 26 | #include "llvm/Analysis/Verifier.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 27 | #include "llvm/Bitcode/ReaderWriter.h" |
| 28 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 29 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 30 | #include "llvm/Support/FormattedStream.h" |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 31 | #include "llvm/Support/StandardPasses.h" |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Timer.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" |
Daniel Dunbar | 821e2eb | 2009-12-12 23:01:36 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetOptions.h" |
Daniel Dunbar | f7d47c0 | 2009-07-15 20:25:38 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetRegistry.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 38 | using namespace clang; |
| 39 | using namespace llvm; |
| 40 | |
| 41 | namespace { |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 42 | enum BackendAction { |
| 43 | Backend_EmitAssembly, ///< Emit native assembly files |
| 44 | Backend_EmitBC, ///< Emit LLVM bitcode files |
| 45 | Backend_EmitLL, ///< Emit human-readable LLVM assembly |
| 46 | Backend_EmitNothing, ///< Don't emit anything (benchmarking mode) |
| 47 | Backend_EmitObj ///< Emit native object files |
| 48 | }; |
| 49 | |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 50 | class BackendConsumer : public ASTConsumer { |
Daniel Dunbar | 125bbbe | 2009-12-04 08:17:40 +0000 | [diff] [blame] | 51 | Diagnostic &Diags; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 52 | BackendAction Action; |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 53 | const CodeGenOptions &CodeGenOpts; |
| 54 | const LangOptions &LangOpts; |
| 55 | const TargetOptions &TargetOpts; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 56 | llvm::raw_ostream *AsmOutStream; |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 57 | llvm::formatted_raw_ostream FormattedOutStream; |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 58 | ASTContext *Context; |
Daniel Dunbar | 90f4130 | 2008-10-29 08:50:02 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 60 | Timer LLVMIRGeneration; |
| 61 | Timer CodeGenerationTime; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 63 | llvm::OwningPtr<CodeGenerator> Gen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 65 | llvm::OwningPtr<llvm::Module> TheModule; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 66 | llvm::TargetData *TheTargetData; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 67 | |
| 68 | mutable FunctionPassManager *CodeGenPasses; |
| 69 | mutable PassManager *PerModulePasses; |
| 70 | mutable FunctionPassManager *PerFunctionPasses; |
| 71 | |
| 72 | FunctionPassManager *getCodeGenPasses() const; |
| 73 | PassManager *getPerModulePasses() const; |
| 74 | FunctionPassManager *getPerFunctionPasses() const; |
| 75 | |
| 76 | void CreatePasses(); |
| 77 | |
Daniel Dunbar | 125bbbe | 2009-12-04 08:17:40 +0000 | [diff] [blame] | 78 | /// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 79 | /// |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 80 | /// \return True on success. |
| 81 | bool AddEmitPasses(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 82 | |
| 83 | void EmitAssembly(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
| 85 | public: |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 86 | BackendConsumer(BackendAction action, Diagnostic &_Diags, |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 87 | const LangOptions &langopts, const CodeGenOptions &compopts, |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 88 | const TargetOptions &targetopts, bool TimePasses, |
| 89 | const std::string &infile, llvm::raw_ostream *OS, |
| 90 | LLVMContext& C) : |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 91 | Diags(_Diags), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | Action(action), |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 93 | CodeGenOpts(compopts), |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 94 | LangOpts(langopts), |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 95 | TargetOpts(targetopts), |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 96 | AsmOutStream(OS), |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 97 | LLVMIRGeneration("LLVM IR Generation Time"), |
| 98 | CodeGenerationTime("Code Generation Time"), |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame^] | 99 | Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)), |
| 100 | TheTargetData(0), |
Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 101 | CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 103 | if (AsmOutStream) |
| 104 | FormattedOutStream.setStream(*AsmOutStream, |
| 105 | formatted_raw_ostream::PRESERVE_STREAM); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 107 | llvm::TimePassesIsEnabled = TimePasses; |
Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 108 | } |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 109 | |
| 110 | ~BackendConsumer() { |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 111 | delete TheTargetData; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 112 | delete CodeGenPasses; |
| 113 | delete PerModulePasses; |
| 114 | delete PerFunctionPasses; |
| 115 | } |
| 116 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 117 | llvm::Module *takeModule() { return TheModule.take(); } |
| 118 | |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 119 | virtual void Initialize(ASTContext &Ctx) { |
| 120 | Context = &Ctx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 122 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 123 | LLVMIRGeneration.startTimer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 125 | Gen->Initialize(Ctx); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 126 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 127 | TheModule.reset(Gen->GetModule()); |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 128 | TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 130 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 131 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 132 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 134 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 135 | PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(), |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 136 | Context->getSourceManager(), |
| 137 | "LLVM IR generation of declaration"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 139 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 140 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 141 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 142 | Gen->HandleTopLevelDecl(D); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 143 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 144 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 145 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 146 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 148 | virtual void HandleTranslationUnit(ASTContext &C) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 149 | { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 150 | PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 151 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 152 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 153 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 154 | Gen->HandleTranslationUnit(C); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 155 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 156 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 157 | LLVMIRGeneration.stopTimer(); |
| 158 | } |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 160 | // EmitAssembly times and registers crash info itself. |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 161 | EmitAssembly(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 163 | // Force a flush here in case we never get released. |
| 164 | if (AsmOutStream) |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 165 | FormattedOutStream.flush(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 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 | virtual void HandleTagDeclDefinition(TagDecl *D) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 169 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 170 | Context->getSourceManager(), |
| 171 | "LLVM IR generation of declaration"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 172 | Gen->HandleTagDeclDefinition(D); |
| 173 | } |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 174 | |
| 175 | virtual void CompleteTentativeDefinition(VarDecl *D) { |
| 176 | Gen->CompleteTentativeDefinition(D); |
| 177 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | }; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | FunctionPassManager *BackendConsumer::getCodeGenPasses() const { |
| 182 | if (!CodeGenPasses) { |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 183 | CodeGenPasses = new FunctionPassManager(&*TheModule); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 184 | CodeGenPasses->add(new TargetData(*TheTargetData)); |
| 185 | } |
| 186 | |
| 187 | return CodeGenPasses; |
| 188 | } |
| 189 | |
| 190 | PassManager *BackendConsumer::getPerModulePasses() const { |
| 191 | if (!PerModulePasses) { |
| 192 | PerModulePasses = new PassManager(); |
| 193 | PerModulePasses->add(new TargetData(*TheTargetData)); |
| 194 | } |
| 195 | |
| 196 | return PerModulePasses; |
| 197 | } |
| 198 | |
| 199 | FunctionPassManager *BackendConsumer::getPerFunctionPasses() const { |
| 200 | if (!PerFunctionPasses) { |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 201 | PerFunctionPasses = new FunctionPassManager(&*TheModule); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 202 | PerFunctionPasses->add(new TargetData(*TheTargetData)); |
| 203 | } |
| 204 | |
| 205 | return PerFunctionPasses; |
| 206 | } |
| 207 | |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 208 | bool BackendConsumer::AddEmitPasses() { |
Daniel Dunbar | e8e2600 | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 209 | if (Action == Backend_EmitNothing) |
| 210 | return true; |
| 211 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 212 | if (Action == Backend_EmitBC) { |
Dan Gohman | b8d4239 | 2009-09-26 15:06:14 +0000 | [diff] [blame] | 213 | getPerModulePasses()->add(createBitcodeWriterPass(FormattedOutStream)); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 214 | } else if (Action == Backend_EmitLL) { |
Dan Gohman | b8d4239 | 2009-09-26 15:06:14 +0000 | [diff] [blame] | 215 | getPerModulePasses()->add(createPrintModulePass(&FormattedOutStream)); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 216 | } else { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 217 | bool Fast = CodeGenOpts.OptimizationLevel == 0; |
Daniel Dunbar | 4c877cc | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 218 | |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame^] | 219 | // Create the TargetMachine for generating code. |
| 220 | std::string Error; |
| 221 | std::string Triple = TheModule->getTargetTriple(); |
| 222 | const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); |
| 223 | if (!TheTarget) { |
| 224 | Diags.Report(diag::err_fe_unable_to_create_target) << Error; |
| 225 | return false; |
| 226 | } |
| 227 | |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 228 | // FIXME: Expose these capabilities via actual APIs!!!! Aside from just |
| 229 | // being gross, this is also totally broken if we ever care about |
| 230 | // concurrency. |
Daniel Dunbar | 821e2eb | 2009-12-12 23:01:36 +0000 | [diff] [blame] | 231 | llvm::NoFramePointerElim = CodeGenOpts.DisableFPElim; |
| 232 | if (CodeGenOpts.FloatABI == "soft") |
| 233 | llvm::FloatABIType = llvm::FloatABI::Soft; |
| 234 | else if (CodeGenOpts.FloatABI == "hard") |
| 235 | llvm::FloatABIType = llvm::FloatABI::Hard; |
| 236 | else { |
| 237 | assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!"); |
| 238 | llvm::FloatABIType = llvm::FloatABI::Default; |
| 239 | } |
| 240 | NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; |
| 241 | llvm::UseSoftFloat = CodeGenOpts.SoftFloat; |
| 242 | UnwindTablesMandatory = CodeGenOpts.UnwindTables; |
| 243 | |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame^] | 244 | TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose); |
| 245 | |
| 246 | // FIXME: Parse this earlier. |
| 247 | if (CodeGenOpts.RelocationModel == "static") { |
| 248 | TargetMachine::setRelocationModel(llvm::Reloc::Static); |
| 249 | } else if (CodeGenOpts.RelocationModel == "pic") { |
| 250 | TargetMachine::setRelocationModel(llvm::Reloc::PIC_); |
| 251 | } else { |
| 252 | assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" && |
| 253 | "Invalid PIC model!"); |
| 254 | TargetMachine::setRelocationModel(llvm::Reloc::DynamicNoPIC); |
| 255 | } |
| 256 | // FIXME: Parse this earlier. |
| 257 | if (CodeGenOpts.CodeModel == "small") { |
| 258 | TargetMachine::setCodeModel(llvm::CodeModel::Small); |
| 259 | } else if (CodeGenOpts.CodeModel == "kernel") { |
| 260 | TargetMachine::setCodeModel(llvm::CodeModel::Kernel); |
| 261 | } else if (CodeGenOpts.CodeModel == "medium") { |
| 262 | TargetMachine::setCodeModel(llvm::CodeModel::Medium); |
| 263 | } else if (CodeGenOpts.CodeModel == "large") { |
| 264 | TargetMachine::setCodeModel(llvm::CodeModel::Large); |
| 265 | } else { |
| 266 | assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!"); |
| 267 | TargetMachine::setCodeModel(llvm::CodeModel::Default); |
| 268 | } |
| 269 | |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 270 | std::vector<const char *> BackendArgs; |
| 271 | BackendArgs.push_back("clang"); // Fake program name. |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 272 | if (!CodeGenOpts.DebugPass.empty()) { |
| 273 | BackendArgs.push_back("-debug-pass"); |
| 274 | BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); |
| 275 | } |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 276 | if (!CodeGenOpts.LimitFloatPrecision.empty()) { |
| 277 | BackendArgs.push_back("-limit-float-precision"); |
| 278 | BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); |
| 279 | } |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 280 | if (llvm::TimePassesIsEnabled) |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 281 | BackendArgs.push_back("-time-passes"); |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 282 | BackendArgs.push_back(0); |
| 283 | llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, |
| 284 | (char**) &BackendArgs[0]); |
| 285 | |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame^] | 286 | std::string FeaturesStr; |
| 287 | if (TargetOpts.CPU.size() || TargetOpts.Features.size()) { |
| 288 | SubtargetFeatures Features; |
| 289 | Features.setCPU(TargetOpts.CPU); |
| 290 | for (std::vector<std::string>::const_iterator |
| 291 | it = TargetOpts.Features.begin(), |
| 292 | ie = TargetOpts.Features.end(); it != ie; ++it) |
| 293 | Features.AddFeature(*it); |
| 294 | FeaturesStr = Features.getString(); |
| 295 | } |
| 296 | TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr); |
| 297 | |
Daniel Dunbar | 8b7650e | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 298 | // Set register scheduler & allocation policy. |
| 299 | RegisterScheduler::setDefault(createDefaultScheduler); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator : |
| 301 | createLinearScanRegisterAllocator); |
Daniel Dunbar | 8b7650e | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 302 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 303 | // From llvm-gcc: |
| 304 | // If there are passes we have to run on the entire module, we do codegen |
| 305 | // as a separate "pass" after that happens. |
| 306 | // FIXME: This is disabled right now until bugs can be worked out. Reenable |
| 307 | // this for fast -O0 compiles! |
| 308 | FunctionPassManager *PM = getCodeGenPasses(); |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 309 | CodeGenOpt::Level OptLevel = CodeGenOpt::Default; |
| 310 | |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 311 | switch (CodeGenOpts.OptimizationLevel) { |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 312 | default: break; |
| 313 | case 0: OptLevel = CodeGenOpt::None; break; |
Bill Wendling | 6e9b8f6 | 2009-04-29 23:53:23 +0000 | [diff] [blame] | 314 | case 3: OptLevel = CodeGenOpt::Aggressive; break; |
| 315 | } |
| 316 | |
Dan Gohman | d68fc05 | 2010-02-28 00:55:40 +0000 | [diff] [blame] | 317 | // Request that addPassesToEmitFile run the Verifier after running |
| 318 | // passes which modify the IR. |
| 319 | #ifndef NDEBUG |
| 320 | bool DisableVerify = false; |
| 321 | #else |
| 322 | bool DisableVerify = true; |
| 323 | #endif |
| 324 | |
Daniel Dunbar | da1573f | 2010-02-03 01:18:43 +0000 | [diff] [blame] | 325 | // Normal mode, emit a .s or .o file by running the code generator. Note, |
| 326 | // this also adds codegenerator level optimization passes. |
| 327 | TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile; |
| 328 | if (Action == Backend_EmitObj) |
| 329 | CGFT = TargetMachine::CGFT_ObjectFile; |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame^] | 330 | if (TM->addPassesToEmitFile(*PM, FormattedOutStream, CGFT, OptLevel, |
| 331 | DisableVerify)) { |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 332 | Diags.Report(diag::err_fe_unable_to_interface_with_target); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 333 | return false; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 334 | } |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | void BackendConsumer::CreatePasses() { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 341 | unsigned OptLevel = CodeGenOpts.OptimizationLevel; |
| 342 | CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining; |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 343 | |
| 344 | // Handle disabling of LLVM optimization, where we want to preserve the |
| 345 | // internal module before any optimization. |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 346 | if (CodeGenOpts.DisableLLVMOpts) { |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 347 | OptLevel = 0; |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 348 | Inlining = CodeGenOpts.NoInlining; |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 351 | // 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] | 352 | if (CodeGenOpts.VerifyModule) |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 353 | getPerFunctionPasses()->add(createVerifierPass()); |
| 354 | |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 355 | // Assume that standard function passes aren't run for -O0. |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 356 | if (OptLevel > 0) |
| 357 | llvm::createStandardFunctionPasses(getPerFunctionPasses(), OptLevel); |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 358 | |
| 359 | llvm::Pass *InliningPass = 0; |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 360 | switch (Inlining) { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 361 | case CodeGenOptions::NoInlining: break; |
| 362 | case CodeGenOptions::NormalInlining: { |
Daniel Dunbar | 90de51f | 2009-12-08 23:15:55 +0000 | [diff] [blame] | 363 | // Set the inline threshold following llvm-gcc. |
| 364 | // |
| 365 | // FIXME: Derive these constants in a principled fashion. |
Daniel Dunbar | 9da5598 | 2010-02-05 07:32:37 +0000 | [diff] [blame] | 366 | unsigned Threshold = 225; |
Daniel Dunbar | 90de51f | 2009-12-08 23:15:55 +0000 | [diff] [blame] | 367 | if (CodeGenOpts.OptimizeSize) |
Daniel Dunbar | 9da5598 | 2010-02-05 07:32:37 +0000 | [diff] [blame] | 368 | Threshold = 75; |
Daniel Dunbar | 90de51f | 2009-12-08 23:15:55 +0000 | [diff] [blame] | 369 | else if (OptLevel > 2) |
Daniel Dunbar | 9da5598 | 2010-02-05 07:32:37 +0000 | [diff] [blame] | 370 | Threshold = 275; |
Eli Friedman | b9b7dd6 | 2009-06-11 20:33:41 +0000 | [diff] [blame] | 371 | InliningPass = createFunctionInliningPass(Threshold); |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 372 | break; |
Eli Friedman | b9b7dd6 | 2009-06-11 20:33:41 +0000 | [diff] [blame] | 373 | } |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 374 | case CodeGenOptions::OnlyAlwaysInlining: |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 375 | InliningPass = createAlwaysInlinerPass(); // Respect always_inline |
| 376 | break; |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // For now we always create per module passes. |
| 380 | PassManager *PM = getPerModulePasses(); |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 381 | llvm::createStandardModulePasses(PM, OptLevel, CodeGenOpts.OptimizeSize, |
| 382 | CodeGenOpts.UnitAtATime, |
| 383 | CodeGenOpts.UnrollLoops, |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 384 | /*SimplifyLibCalls=*/!LangOpts.NoBuiltin, |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 385 | /*HaveExceptions=*/true, |
| 386 | InliningPass); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /// EmitAssembly - Handle interaction with LLVM backend to generate |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | /// actual machine code. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 391 | void BackendConsumer::EmitAssembly() { |
| 392 | // Silently ignore if we weren't initialized for some reason. |
| 393 | if (!TheModule || !TheTargetData) |
| 394 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 396 | TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 397 | |
Daniel Dunbar | d611bac | 2008-10-27 20:40:41 +0000 | [diff] [blame] | 398 | // Make sure IR generation is happy with the module. This is |
| 399 | // released by the module provider. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 400 | Module *M = Gen->ReleaseModule(); |
| 401 | if (!M) { |
Daniel Dunbar | d611bac | 2008-10-27 20:40:41 +0000 | [diff] [blame] | 402 | // The module has been released by IR gen on failures, do not |
| 403 | // double free. |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 404 | TheModule.take(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 405 | return; |
| 406 | } |
| 407 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 408 | assert(TheModule.get() == M && |
| 409 | "Unexpected module change during IR generation"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 410 | |
| 411 | CreatePasses(); |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 412 | if (!AddEmitPasses()) |
| 413 | return; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 414 | |
| 415 | // Run passes. For now we do all passes at once, but eventually we |
| 416 | // would like to have the option of streaming code generation. |
| 417 | |
| 418 | if (PerFunctionPasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 419 | PrettyStackTraceString CrashInfo("Per-function optimization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 421 | PerFunctionPasses->doInitialization(); |
| 422 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 423 | if (!I->isDeclaration()) |
| 424 | PerFunctionPasses->run(*I); |
| 425 | PerFunctionPasses->doFinalization(); |
| 426 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 428 | if (PerModulePasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 429 | PrettyStackTraceString CrashInfo("Per-module optimization passes"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 430 | PerModulePasses->run(*M); |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 433 | if (CodeGenPasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 434 | PrettyStackTraceString CrashInfo("Code generation"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 435 | CodeGenPasses->doInitialization(); |
| 436 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 437 | if (!I->isDeclaration()) |
| 438 | CodeGenPasses->run(*I); |
| 439 | CodeGenPasses->doFinalization(); |
| 440 | } |
| 441 | } |
| 442 | |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 443 | // |
| 444 | |
| 445 | CodeGenAction::CodeGenAction(unsigned _Act) : Act(_Act) {} |
| 446 | |
Daniel Dunbar | 9ad1c02 | 2010-02-25 20:37:44 +0000 | [diff] [blame] | 447 | CodeGenAction::~CodeGenAction() {} |
| 448 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 449 | void CodeGenAction::EndSourceFileAction() { |
| 450 | // If the consumer creation failed, do nothing. |
| 451 | if (!getCompilerInstance().hasASTConsumer()) |
| 452 | return; |
| 453 | |
| 454 | // Steal the module from the consumer. |
| 455 | BackendConsumer *Consumer = static_cast<BackendConsumer*>( |
| 456 | &getCompilerInstance().getASTConsumer()); |
| 457 | |
| 458 | TheModule.reset(Consumer->takeModule()); |
| 459 | } |
| 460 | |
| 461 | llvm::Module *CodeGenAction::takeModule() { |
| 462 | return TheModule.take(); |
| 463 | } |
| 464 | |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 465 | ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI, |
| 466 | llvm::StringRef InFile) { |
| 467 | BackendAction BA = static_cast<BackendAction>(Act); |
| 468 | llvm::OwningPtr<llvm::raw_ostream> OS; |
| 469 | switch (BA) { |
| 470 | case Backend_EmitAssembly: |
| 471 | OS.reset(CI.createDefaultOutputFile(false, InFile, "s")); |
| 472 | break; |
| 473 | case Backend_EmitLL: |
| 474 | OS.reset(CI.createDefaultOutputFile(false, InFile, "ll")); |
| 475 | break; |
| 476 | case Backend_EmitBC: |
| 477 | OS.reset(CI.createDefaultOutputFile(true, InFile, "bc")); |
| 478 | break; |
| 479 | case Backend_EmitNothing: |
| 480 | break; |
| 481 | case Backend_EmitObj: |
| 482 | OS.reset(CI.createDefaultOutputFile(true, InFile, "o")); |
| 483 | break; |
| 484 | } |
| 485 | if (BA != Backend_EmitNothing && !OS) |
| 486 | return 0; |
| 487 | |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame^] | 488 | return new BackendConsumer(BA, CI.getDiagnostics(), CI.getLangOpts(), |
| 489 | CI.getCodeGenOpts(), CI.getTargetOpts(), |
| 490 | CI.getFrontendOpts().ShowTimers, InFile, OS.take(), |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 491 | CI.getLLVMContext()); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 492 | } |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 493 | |
| 494 | EmitAssemblyAction::EmitAssemblyAction() |
| 495 | : CodeGenAction(Backend_EmitAssembly) {} |
| 496 | |
| 497 | EmitBCAction::EmitBCAction() : CodeGenAction(Backend_EmitBC) {} |
| 498 | |
| 499 | EmitLLVMAction::EmitLLVMAction() : CodeGenAction(Backend_EmitLL) {} |
| 500 | |
| 501 | EmitLLVMOnlyAction::EmitLLVMOnlyAction() : CodeGenAction(Backend_EmitNothing) {} |
| 502 | |
| 503 | EmitObjAction::EmitObjAction() : CodeGenAction(Backend_EmitObj) {} |