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