Daniel Dunbar | 85e44e2 | 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 | |
| 10 | #include "ASTConsumers.h" |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/AST/ASTConsumer.h" |
| 13 | #include "clang/AST/TranslationUnit.h" |
| 14 | #include "clang/Basic/TargetInfo.h" |
| 15 | #include "clang/CodeGen/ModuleBuilder.h" |
Daniel Dunbar | 68952de | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/CompileOptions.h" |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/ModuleProvider.h" |
| 19 | #include "llvm/PassManager.h" |
| 20 | #include "llvm/ADT/OwningPtr.h" |
| 21 | #include "llvm/Assembly/PrintModulePass.h" |
Daniel Dunbar | aa7a066 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/CallGraph.h" |
| 23 | #include "llvm/Analysis/Verifier.h" |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 24 | #include "llvm/Bitcode/ReaderWriter.h" |
| 25 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 26 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 2193db2 | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Timer.h" |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 30 | #include "llvm/System/Path.h" |
| 31 | #include "llvm/System/Program.h" |
Daniel Dunbar | 9101a63 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 32 | #include "llvm/Target/SubtargetFeature.h" |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetData.h" |
| 34 | #include "llvm/Target/TargetMachine.h" |
| 35 | #include "llvm/Target/TargetMachineRegistry.h" |
Daniel Dunbar | aa7a066 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Scalar.h" |
| 37 | #include "llvm/Transforms/IPO.h" |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 38 | using namespace clang; |
| 39 | using namespace llvm; |
| 40 | |
| 41 | namespace { |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 42 | class VISIBILITY_HIDDEN BackendConsumer : public ASTConsumer { |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 43 | BackendAction Action; |
Daniel Dunbar | aa7a066 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 44 | CompileOptions CompileOpts; |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 45 | const std::string &InputFile; |
| 46 | std::string OutputFile; |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 47 | ASTContext *Context; |
Daniel Dunbar | 1a27a19 | 2008-10-29 08:50:02 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 2193db2 | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 49 | Timer LLVMIRGeneration; |
| 50 | Timer CodeGenerationTime; |
| 51 | |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 52 | llvm::OwningPtr<CodeGenerator> Gen; |
| 53 | |
| 54 | llvm::Module *TheModule; |
| 55 | llvm::TargetData *TheTargetData; |
| 56 | llvm::raw_ostream *AsmOutStream; |
| 57 | |
Nuno Lopes | 74d9278 | 2008-10-24 22:51:00 +0000 | [diff] [blame] | 58 | mutable llvm::ModuleProvider *ModuleProvider; |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 59 | mutable FunctionPassManager *CodeGenPasses; |
| 60 | mutable PassManager *PerModulePasses; |
| 61 | mutable FunctionPassManager *PerFunctionPasses; |
| 62 | |
| 63 | FunctionPassManager *getCodeGenPasses() const; |
| 64 | PassManager *getPerModulePasses() const; |
| 65 | FunctionPassManager *getPerFunctionPasses() const; |
| 66 | |
| 67 | void CreatePasses(); |
| 68 | |
| 69 | /// AddEmitPasses - Add passes necessary to emit assembly or LLVM |
| 70 | /// IR. |
| 71 | /// |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 72 | /// \return True on success. On failure \arg Error will be set to |
| 73 | /// a user readable error message. |
Daniel Dunbar | 655d509 | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 74 | bool AddEmitPasses(std::string &Error); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 75 | |
| 76 | void EmitAssembly(); |
| 77 | |
| 78 | public: |
| 79 | BackendConsumer(BackendAction action, Diagnostic &Diags, |
Daniel Dunbar | 9101a63 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 80 | const LangOptions &langopts, const CompileOptions &compopts, |
Chris Lattner | 88cbb9b | 2009-03-09 22:00:34 +0000 | [diff] [blame] | 81 | const std::string &infile, const std::string &outfile) : |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 82 | Action(action), |
Daniel Dunbar | aa7a066 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 83 | CompileOpts(compopts), |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 84 | InputFile(infile), |
| 85 | OutputFile(outfile), |
Chris Lattner | 2193db2 | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 86 | LLVMIRGeneration("LLVM IR Generation Time"), |
| 87 | CodeGenerationTime("Code Generation Time"), |
Chris Lattner | f04a756 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 88 | Gen(CreateLLVMCodeGen(Diags, InputFile, compopts)), |
Nuno Lopes | 74d9278 | 2008-10-24 22:51:00 +0000 | [diff] [blame] | 89 | TheModule(0), TheTargetData(0), AsmOutStream(0), ModuleProvider(0), |
Chris Lattner | e8f7071 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 90 | CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) { |
| 91 | |
Chris Lattner | 2193db2 | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 92 | // Enable -time-passes if -ftime-report is enabled. |
Chris Lattner | e8f7071 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 93 | llvm::TimePassesIsEnabled = CompileOpts.TimePasses; |
| 94 | } |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 95 | |
| 96 | ~BackendConsumer() { |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 97 | delete AsmOutStream; |
| 98 | delete TheTargetData; |
Nuno Lopes | 74d9278 | 2008-10-24 22:51:00 +0000 | [diff] [blame] | 99 | delete ModuleProvider; |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 100 | delete CodeGenPasses; |
| 101 | delete PerModulePasses; |
| 102 | delete PerFunctionPasses; |
| 103 | } |
| 104 | |
Chris Lattner | fb88a5f | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 105 | virtual void Initialize(ASTContext &Ctx) { |
| 106 | Context = &Ctx; |
Chris Lattner | 2193db2 | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 107 | |
| 108 | if (CompileOpts.TimePasses) |
| 109 | LLVMIRGeneration.startTimer(); |
| 110 | |
Chris Lattner | fb88a5f | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 111 | Gen->Initialize(Ctx); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 112 | |
| 113 | TheModule = Gen->GetModule(); |
Nuno Lopes | d41e2b1 | 2008-10-24 23:27:18 +0000 | [diff] [blame] | 114 | ModuleProvider = new ExistingModuleProvider(TheModule); |
Chris Lattner | fb88a5f | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 115 | TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription()); |
Chris Lattner | 2193db2 | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 116 | |
| 117 | if (CompileOpts.TimePasses) |
| 118 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | virtual void HandleTopLevelDecl(Decl *D) { |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 122 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 123 | Context->getSourceManager(), |
| 124 | "LLVM IR generation of declaration"); |
Chris Lattner | 2193db2 | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 125 | if (CompileOpts.TimePasses) |
| 126 | LLVMIRGeneration.startTimer(); |
| 127 | |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 128 | Gen->HandleTopLevelDecl(D); |
Chris Lattner | 2193db2 | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 129 | |
| 130 | if (CompileOpts.TimePasses) |
| 131 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Chris Lattner | 2a594d0 | 2009-03-28 04:11:33 +0000 | [diff] [blame^] | 134 | virtual void HandleTranslationUnit(ASTContext &C) { |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 135 | { |
Chris Lattner | 4e16417 | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 136 | PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 137 | if (CompileOpts.TimePasses) |
| 138 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 2193db2 | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 2a594d0 | 2009-03-28 04:11:33 +0000 | [diff] [blame^] | 140 | Gen->HandleTranslationUnit(C); |
Daniel Dunbar | 622d6d0 | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 141 | |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 142 | if (CompileOpts.TimePasses) |
| 143 | LLVMIRGeneration.stopTimer(); |
| 144 | } |
Chris Lattner | 2193db2 | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 145 | |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 146 | // EmitAssembly times and registers crash info itself. |
Chris Lattner | 2193db2 | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 147 | EmitAssembly(); |
| 148 | |
Daniel Dunbar | 622d6d0 | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 149 | // Force a flush here in case we never get released. |
| 150 | if (AsmOutStream) |
| 151 | AsmOutStream->flush(); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | virtual void HandleTagDeclDefinition(TagDecl *D) { |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 155 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 156 | Context->getSourceManager(), |
| 157 | "LLVM IR generation of declaration"); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 158 | Gen->HandleTagDeclDefinition(D); |
| 159 | } |
| 160 | }; |
| 161 | } |
| 162 | |
| 163 | FunctionPassManager *BackendConsumer::getCodeGenPasses() const { |
| 164 | if (!CodeGenPasses) { |
Nuno Lopes | 74d9278 | 2008-10-24 22:51:00 +0000 | [diff] [blame] | 165 | CodeGenPasses = new FunctionPassManager(ModuleProvider); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 166 | CodeGenPasses->add(new TargetData(*TheTargetData)); |
| 167 | } |
| 168 | |
| 169 | return CodeGenPasses; |
| 170 | } |
| 171 | |
| 172 | PassManager *BackendConsumer::getPerModulePasses() const { |
| 173 | if (!PerModulePasses) { |
| 174 | PerModulePasses = new PassManager(); |
| 175 | PerModulePasses->add(new TargetData(*TheTargetData)); |
| 176 | } |
| 177 | |
| 178 | return PerModulePasses; |
| 179 | } |
| 180 | |
| 181 | FunctionPassManager *BackendConsumer::getPerFunctionPasses() const { |
| 182 | if (!PerFunctionPasses) { |
Nuno Lopes | d41e2b1 | 2008-10-24 23:27:18 +0000 | [diff] [blame] | 183 | PerFunctionPasses = new FunctionPassManager(ModuleProvider); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 184 | PerFunctionPasses->add(new TargetData(*TheTargetData)); |
| 185 | } |
| 186 | |
| 187 | return PerFunctionPasses; |
| 188 | } |
| 189 | |
Daniel Dunbar | 655d509 | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 190 | bool BackendConsumer::AddEmitPasses(std::string &Error) { |
Daniel Dunbar | d999a8e | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 191 | if (Action == Backend_EmitNothing) |
| 192 | return true; |
| 193 | |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 194 | if (OutputFile == "-" || (InputFile == "-" && OutputFile.empty())) { |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 195 | AsmOutStream = new raw_stdout_ostream(); |
| 196 | sys::Program::ChangeStdoutToBinary(); |
| 197 | } else { |
| 198 | if (OutputFile.empty()) { |
| 199 | llvm::sys::Path Path(InputFile); |
| 200 | Path.eraseSuffix(); |
| 201 | if (Action == Backend_EmitBC) { |
| 202 | Path.appendSuffix("bc"); |
| 203 | } else if (Action == Backend_EmitLL) { |
| 204 | Path.appendSuffix("ll"); |
| 205 | } else { |
| 206 | Path.appendSuffix("s"); |
| 207 | } |
| 208 | OutputFile = Path.toString(); |
| 209 | } |
| 210 | |
Daniel Dunbar | 8fc9ba6 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 211 | AsmOutStream = new raw_fd_ostream(OutputFile.c_str(), true, Error); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 212 | if (!Error.empty()) |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | if (Action == Backend_EmitBC) { |
Daniel Dunbar | e5dbb07 | 2008-10-22 17:40:45 +0000 | [diff] [blame] | 217 | getPerModulePasses()->add(createBitcodeWriterPass(*AsmOutStream)); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 218 | } else if (Action == Backend_EmitLL) { |
Daniel Dunbar | 092a744 | 2008-10-22 03:28:13 +0000 | [diff] [blame] | 219 | getPerModulePasses()->add(createPrintModulePass(AsmOutStream)); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 220 | } else { |
Daniel Dunbar | 655d509 | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 221 | bool Fast = CompileOpts.OptimizationLevel == 0; |
| 222 | |
Daniel Dunbar | aed93f2 | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 223 | // Create the TargetMachine for generating code. |
| 224 | const TargetMachineRegistry::entry *TME = |
| 225 | TargetMachineRegistry::getClosestStaticTargetForModule(*TheModule, Error); |
| 226 | if (!TME) { |
| 227 | Error = std::string("Unable to get target machine: ") + Error; |
| 228 | return false; |
| 229 | } |
Daniel Dunbar | 9101a63 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 230 | |
| 231 | std::string FeaturesStr; |
| 232 | if (CompileOpts.CPU.size() || CompileOpts.Features.size()) { |
| 233 | SubtargetFeatures Features; |
| 234 | Features.setCPU(CompileOpts.CPU); |
| 235 | for (std::vector<std::string>::iterator |
| 236 | it = CompileOpts.Features.begin(), |
| 237 | ie = CompileOpts.Features.end(); it != ie; ++it) |
| 238 | Features.AddFeature(*it); |
| 239 | FeaturesStr = Features.getString(); |
| 240 | } |
| 241 | TargetMachine *TM = TME->CtorFn(*TheModule, FeaturesStr); |
Daniel Dunbar | aed93f2 | 2008-10-22 18:29:51 +0000 | [diff] [blame] | 242 | |
| 243 | // Set register scheduler & allocation policy. |
| 244 | RegisterScheduler::setDefault(createDefaultScheduler); |
| 245 | RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator : |
| 246 | createLinearScanRegisterAllocator); |
| 247 | |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 248 | // From llvm-gcc: |
| 249 | // If there are passes we have to run on the entire module, we do codegen |
| 250 | // as a separate "pass" after that happens. |
| 251 | // FIXME: This is disabled right now until bugs can be worked out. Reenable |
| 252 | // this for fast -O0 compiles! |
| 253 | FunctionPassManager *PM = getCodeGenPasses(); |
| 254 | |
| 255 | // Normal mode, emit a .s file by running the code generator. |
| 256 | // Note, this also adds codegenerator level optimization passes. |
| 257 | switch (TM->addPassesToEmitFile(*PM, *AsmOutStream, |
| 258 | TargetMachine::AssemblyFile, Fast)) { |
| 259 | default: |
| 260 | case FileModel::Error: |
| 261 | Error = "Unable to interface with target machine!\n"; |
| 262 | return false; |
| 263 | case FileModel::AsmFile: |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | if (TM->addPassesToEmitFileFinish(*CodeGenPasses, 0, Fast)) { |
| 268 | Error = "Unable to interface with target machine!\n"; |
| 269 | return false; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | void BackendConsumer::CreatePasses() { |
Daniel Dunbar | aa7a066 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 277 | // In -O0 if checking is disabled, we don't even have per-function passes. |
| 278 | if (CompileOpts.VerifyModule) |
| 279 | getPerFunctionPasses()->add(createVerifierPass()); |
| 280 | |
| 281 | if (CompileOpts.OptimizationLevel > 0) { |
| 282 | FunctionPassManager *PM = getPerFunctionPasses(); |
| 283 | PM->add(createCFGSimplificationPass()); |
| 284 | if (CompileOpts.OptimizationLevel == 1) |
| 285 | PM->add(createPromoteMemoryToRegisterPass()); |
| 286 | else |
| 287 | PM->add(createScalarReplAggregatesPass()); |
| 288 | PM->add(createInstructionCombiningPass()); |
| 289 | } |
| 290 | |
| 291 | // For now we always create per module passes. |
| 292 | PassManager *PM = getPerModulePasses(); |
| 293 | if (CompileOpts.OptimizationLevel > 0) { |
| 294 | if (CompileOpts.UnitAtATime) |
| 295 | PM->add(createRaiseAllocationsPass()); // call %malloc -> malloc inst |
| 296 | PM->add(createCFGSimplificationPass()); // Clean up disgusting code |
| 297 | PM->add(createPromoteMemoryToRegisterPass()); // Kill useless allocas |
| 298 | if (CompileOpts.UnitAtATime) { |
| 299 | PM->add(createGlobalOptimizerPass()); // Optimize out global vars |
| 300 | PM->add(createGlobalDCEPass()); // Remove unused fns and globs |
| 301 | PM->add(createIPConstantPropagationPass()); // IP Constant Propagation |
| 302 | PM->add(createDeadArgEliminationPass()); // Dead argument elimination |
| 303 | } |
| 304 | PM->add(createInstructionCombiningPass()); // Clean up after IPCP & DAE |
| 305 | PM->add(createCFGSimplificationPass()); // Clean up after IPCP & DAE |
| 306 | if (CompileOpts.UnitAtATime) { |
| 307 | PM->add(createPruneEHPass()); // Remove dead EH info |
Bill Wendling | accd9b7 | 2008-12-31 19:51:31 +0000 | [diff] [blame] | 308 | PM->add(createFunctionAttrsPass()); // Set readonly/readnone attrs |
Daniel Dunbar | aa7a066 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 309 | } |
| 310 | if (CompileOpts.InlineFunctions) |
| 311 | PM->add(createFunctionInliningPass()); // Inline small functions |
| 312 | else |
| 313 | PM->add(createAlwaysInlinerPass()); // Respect always_inline |
| 314 | if (CompileOpts.OptimizationLevel > 2) |
| 315 | PM->add(createArgumentPromotionPass()); // Scalarize uninlined fn args |
| 316 | if (CompileOpts.SimplifyLibCalls) |
| 317 | PM->add(createSimplifyLibCallsPass()); // Library Call Optimizations |
| 318 | PM->add(createInstructionCombiningPass()); // Cleanup for scalarrepl. |
| 319 | PM->add(createJumpThreadingPass()); // Thread jumps. |
| 320 | PM->add(createCFGSimplificationPass()); // Merge & remove BBs |
| 321 | PM->add(createScalarReplAggregatesPass()); // Break up aggregate allocas |
| 322 | PM->add(createInstructionCombiningPass()); // Combine silly seq's |
| 323 | PM->add(createCondPropagationPass()); // Propagate conditionals |
| 324 | PM->add(createTailCallEliminationPass()); // Eliminate tail calls |
| 325 | PM->add(createCFGSimplificationPass()); // Merge & remove BBs |
| 326 | PM->add(createReassociatePass()); // Reassociate expressions |
| 327 | PM->add(createLoopRotatePass()); // Rotate Loop |
| 328 | PM->add(createLICMPass()); // Hoist loop invariants |
| 329 | PM->add(createLoopUnswitchPass(CompileOpts.OptimizeSize ? true : false)); |
Devang Patel | 62237e7 | 2008-11-26 05:01:52 +0000 | [diff] [blame] | 330 | // PM->add(createLoopIndexSplitPass()); // Split loop index |
Daniel Dunbar | aa7a066 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 331 | PM->add(createInstructionCombiningPass()); |
| 332 | PM->add(createIndVarSimplifyPass()); // Canonicalize indvars |
| 333 | PM->add(createLoopDeletionPass()); // Delete dead loops |
| 334 | if (CompileOpts.UnrollLoops) |
| 335 | PM->add(createLoopUnrollPass()); // Unroll small loops |
| 336 | PM->add(createInstructionCombiningPass()); // Clean up after the unroller |
| 337 | PM->add(createGVNPass()); // Remove redundancies |
| 338 | PM->add(createMemCpyOptPass()); // Remove memcpy / form memset |
| 339 | PM->add(createSCCPPass()); // Constant prop with SCCP |
| 340 | |
| 341 | // Run instcombine after redundancy elimination to exploit opportunities |
| 342 | // opened up by them. |
| 343 | PM->add(createInstructionCombiningPass()); |
| 344 | PM->add(createCondPropagationPass()); // Propagate conditionals |
| 345 | PM->add(createDeadStoreEliminationPass()); // Delete dead stores |
| 346 | PM->add(createAggressiveDCEPass()); // Delete dead instructions |
| 347 | PM->add(createCFGSimplificationPass()); // Merge & remove BBs |
| 348 | |
| 349 | if (CompileOpts.UnitAtATime) { |
| 350 | PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes |
| 351 | PM->add(createDeadTypeEliminationPass()); // Eliminate dead types |
| 352 | } |
| 353 | |
| 354 | if (CompileOpts.OptimizationLevel > 1 && CompileOpts.UnitAtATime) |
| 355 | PM->add(createConstantMergePass()); // Merge dup global constants |
| 356 | } else { |
Daniel Dunbar | 160cb62 | 2008-11-13 05:29:02 +0000 | [diff] [blame] | 357 | PM->add(createAlwaysInlinerPass()); |
Daniel Dunbar | aa7a066 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 358 | } |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /// EmitAssembly - Handle interaction with LLVM backend to generate |
| 362 | /// actual machine code. |
| 363 | void BackendConsumer::EmitAssembly() { |
| 364 | // Silently ignore if we weren't initialized for some reason. |
| 365 | if (!TheModule || !TheTargetData) |
| 366 | return; |
Chris Lattner | 2193db2 | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 367 | |
Chris Lattner | 908789c | 2009-02-18 18:22:50 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 908789c | 2009-02-18 18:22:50 +0000 | [diff] [blame] | 369 | TimeRegion Region(CompileOpts.TimePasses ? &CodeGenerationTime : 0); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 370 | |
Daniel Dunbar | bc14779 | 2008-10-27 20:40:41 +0000 | [diff] [blame] | 371 | // Make sure IR generation is happy with the module. This is |
| 372 | // released by the module provider. |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 373 | Module *M = Gen->ReleaseModule(); |
| 374 | if (!M) { |
Daniel Dunbar | bc14779 | 2008-10-27 20:40:41 +0000 | [diff] [blame] | 375 | // The module has been released by IR gen on failures, do not |
| 376 | // double free. |
| 377 | ModuleProvider->releaseModule(); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 378 | TheModule = 0; |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | assert(TheModule == M && "Unexpected module change during IR generation"); |
| 383 | |
| 384 | CreatePasses(); |
| 385 | |
| 386 | std::string Error; |
Daniel Dunbar | 655d509 | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 387 | if (!AddEmitPasses(Error)) { |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 388 | // FIXME: Don't fail this way. |
| 389 | llvm::cerr << "ERROR: " << Error << "\n"; |
| 390 | ::exit(1); |
| 391 | } |
| 392 | |
| 393 | // Run passes. For now we do all passes at once, but eventually we |
| 394 | // would like to have the option of streaming code generation. |
| 395 | |
| 396 | if (PerFunctionPasses) { |
Chris Lattner | 4e16417 | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 397 | PrettyStackTraceString CrashInfo("Per-function optimization"); |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 398 | |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 399 | PerFunctionPasses->doInitialization(); |
| 400 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 401 | if (!I->isDeclaration()) |
| 402 | PerFunctionPasses->run(*I); |
| 403 | PerFunctionPasses->doFinalization(); |
| 404 | } |
| 405 | |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 406 | if (PerModulePasses) { |
Chris Lattner | 4e16417 | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 407 | PrettyStackTraceString CrashInfo("Per-module optimization passes"); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 408 | PerModulePasses->run(*M); |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 409 | } |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 410 | |
| 411 | if (CodeGenPasses) { |
Chris Lattner | 4e16417 | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 412 | PrettyStackTraceString CrashInfo("Code generation"); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 413 | CodeGenPasses->doInitialization(); |
| 414 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 415 | if (!I->isDeclaration()) |
| 416 | CodeGenPasses->run(*I); |
| 417 | CodeGenPasses->doFinalization(); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | ASTConsumer *clang::CreateBackendConsumer(BackendAction Action, |
| 422 | Diagnostic &Diags, |
Daniel Dunbar | 9101a63 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 423 | const LangOptions &LangOpts, |
Daniel Dunbar | aa7a066 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 424 | const CompileOptions &CompileOpts, |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 425 | const std::string& InFile, |
Chris Lattner | 88cbb9b | 2009-03-09 22:00:34 +0000 | [diff] [blame] | 426 | const std::string& OutFile) { |
Chris Lattner | 57ff05c | 2009-02-12 01:50:58 +0000 | [diff] [blame] | 427 | // FIXME: If optimizing, disable all debug info generation. The LLVM |
| 428 | // optimizer and backend is not ready to handle it when optimizations |
| 429 | // are enabled. |
| 430 | if (CompileOpts.OptimizationLevel > 0) |
Chris Lattner | 88cbb9b | 2009-03-09 22:00:34 +0000 | [diff] [blame] | 431 | const_cast<CompileOptions&>(CompileOpts).DebugInfo = false; |
Daniel Dunbar | 9101a63 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 432 | |
| 433 | return new BackendConsumer(Action, Diags, LangOpts, CompileOpts, |
Chris Lattner | 88cbb9b | 2009-03-09 22:00:34 +0000 | [diff] [blame] | 434 | InFile, OutFile); |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 435 | } |