Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 1 | //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVMTargetMachine class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Target/TargetMachine.h" |
| 15 | #include "llvm/PassManager.h" |
| 16 | #include "llvm/Pass.h" |
Chris Lattner | 31442f9 | 2007-03-31 00:24:43 +0000 | [diff] [blame] | 17 | #include "llvm/Assembly/PrintModulePass.h" |
Devang Patel | 0f54dcb | 2007-03-06 21:14:09 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LoopPass.h" |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/Passes.h" |
| 20 | #include "llvm/Target/TargetOptions.h" |
| 21 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 31442f9 | 2007-03-31 00:24:43 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chris Lattner | 85ef254 | 2007-06-19 05:47:49 +0000 | [diff] [blame^] | 25 | static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden, |
| 26 | cl::desc("Print LLVM IR produced by the loop-reduce pass")); |
| 27 | static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden, |
| 28 | cl::desc("Print LLVM IR input to isel pass")); |
| 29 | |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 30 | FileModel::Model |
| 31 | LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM, |
| 32 | std::ostream &Out, |
| 33 | CodeGenFileType FileType, |
| 34 | bool Fast) { |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 35 | // Standard LLVM-Level Passes. |
| 36 | |
| 37 | // Run loop strength reduction before anything else. |
Chris Lattner | 31442f9 | 2007-03-31 00:24:43 +0000 | [diff] [blame] | 38 | if (!Fast) { |
| 39 | PM.add(createLoopStrengthReducePass(getTargetLowering())); |
| 40 | if (PrintLSR) |
| 41 | PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr)); |
| 42 | } |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 43 | |
| 44 | // FIXME: Implement efficient support for garbage collection intrinsics. |
| 45 | PM.add(createLowerGCPass()); |
| 46 | |
| 47 | // FIXME: Implement the invoke/unwind instructions! |
Jim Laskey | a4e7cd9 | 2007-02-22 16:22:15 +0000 | [diff] [blame] | 48 | if (!ExceptionHandling) |
| 49 | PM.add(createLowerInvokePass(getTargetLowering())); |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 50 | |
| 51 | // Make sure that no unreachable blocks are instruction selected. |
| 52 | PM.add(createUnreachableBlockEliminationPass()); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 53 | |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 54 | if (!Fast) |
| 55 | PM.add(createCodeGenPreparePass(getTargetLowering())); |
| 56 | |
| 57 | if (PrintISelInput) |
| 58 | PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n", |
| 59 | &cerr)); |
| 60 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 61 | // Ask the target for an isel. |
| 62 | if (addInstSelector(PM, Fast)) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 63 | return FileModel::Error; |
| 64 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 65 | // Print the instruction selected machine code... |
| 66 | if (PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 67 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 68 | |
| 69 | // Perform register allocation to convert to a concrete x86 representation |
| 70 | PM.add(createRegisterAllocator()); |
| 71 | |
| 72 | if (PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 73 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 74 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 75 | // Run post-ra passes. |
| 76 | if (addPostRegAlloc(PM, Fast) && PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 77 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 78 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 79 | // Insert prolog/epilog code. Eliminate abstract frame index references... |
| 80 | PM.add(createPrologEpilogCodeInserter()); |
| 81 | |
Chris Lattner | 4a84ad7 | 2006-10-13 20:45:56 +0000 | [diff] [blame] | 82 | // Branch folding must be run after regalloc and prolog/epilog insertion. |
Jim Laskey | 62d07d6 | 2006-10-24 16:11:49 +0000 | [diff] [blame] | 83 | if (!Fast) |
Dale Johannesen | e6e4354 | 2007-05-22 18:31:04 +0000 | [diff] [blame] | 84 | PM.add(createBranchFoldingPass(getEnableTailMergeDefault())); |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 85 | |
| 86 | // Fold redundant debug labels. |
| 87 | PM.add(createDebugLabelFoldingPass()); |
Chris Lattner | 4a84ad7 | 2006-10-13 20:45:56 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 89 | if (PrintMachineCode) // Print the register-allocated code |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 90 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 91 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 92 | if (addPreEmitPass(PM, Fast) && PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 93 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 94 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 95 | switch (FileType) { |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 96 | default: |
| 97 | break; |
| 98 | case TargetMachine::AssemblyFile: |
| 99 | if (addAssemblyEmitter(PM, Fast, Out)) |
| 100 | return FileModel::Error; |
| 101 | return FileModel::AsmFile; |
| 102 | case TargetMachine::ObjectFile: |
| 103 | if (getMachOWriterInfo()) |
| 104 | return FileModel::MachOFile; |
| 105 | else if (getELFWriterInfo()) |
| 106 | return FileModel::ElfFile; |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 107 | } |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 108 | |
| 109 | return FileModel::Error; |
| 110 | } |
| 111 | |
| 112 | /// addPassesToEmitFileFinish - If the passes to emit the specified file had to |
| 113 | /// be split up (e.g., to add an object writer pass), this method can be used to |
| 114 | /// finish up adding passes to emit the file, if necessary. |
| 115 | bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM, |
| 116 | MachineCodeEmitter *MCE, |
| 117 | bool Fast) { |
| 118 | if (MCE) |
| 119 | addSimpleCodeEmitter(PM, Fast, *MCE); |
| 120 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 121 | // Delete machine code for this function |
| 122 | PM.add(createMachineCodeDeleter()); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 124 | return false; // success! |
| 125 | } |
| 126 | |
| 127 | /// addPassesToEmitMachineCode - Add passes to the specified pass manager to |
| 128 | /// get machine code emitted. This uses a MachineCodeEmitter object to handle |
| 129 | /// actually outputting the machine code and resolving things like the address |
| 130 | /// of functions. This method should returns true if machine code emission is |
| 131 | /// not supported. |
| 132 | /// |
| 133 | bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM, |
| 134 | MachineCodeEmitter &MCE, |
| 135 | bool Fast) { |
| 136 | // Standard LLVM-Level Passes. |
| 137 | |
| 138 | // Run loop strength reduction before anything else. |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 139 | if (!Fast) { |
| 140 | PM.add(createLoopStrengthReducePass(getTargetLowering())); |
| 141 | if (PrintLSR) |
| 142 | PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr)); |
| 143 | } |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 144 | |
| 145 | // FIXME: Implement efficient support for garbage collection intrinsics. |
| 146 | PM.add(createLowerGCPass()); |
| 147 | |
| 148 | // FIXME: Implement the invoke/unwind instructions! |
Duraid Madina | 2a0013f | 2006-09-04 06:21:35 +0000 | [diff] [blame] | 149 | PM.add(createLowerInvokePass(getTargetLowering())); |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 150 | |
| 151 | // Make sure that no unreachable blocks are instruction selected. |
| 152 | PM.add(createUnreachableBlockEliminationPass()); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 153 | |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 154 | if (!Fast) |
| 155 | PM.add(createCodeGenPreparePass(getTargetLowering())); |
| 156 | |
| 157 | if (PrintISelInput) |
| 158 | PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n", |
| 159 | &cerr)); |
| 160 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 161 | // Ask the target for an isel. |
| 162 | if (addInstSelector(PM, Fast)) |
| 163 | return true; |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 165 | // Print the instruction selected machine code... |
| 166 | if (PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 167 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 168 | |
| 169 | // Perform register allocation to convert to a concrete x86 representation |
| 170 | PM.add(createRegisterAllocator()); |
| 171 | |
| 172 | if (PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 173 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 174 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 175 | // Run post-ra passes. |
| 176 | if (addPostRegAlloc(PM, Fast) && PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 177 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 178 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 179 | // Insert prolog/epilog code. Eliminate abstract frame index references... |
| 180 | PM.add(createPrologEpilogCodeInserter()); |
| 181 | |
| 182 | if (PrintMachineCode) // Print the register-allocated code |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 183 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 184 | |
Chris Lattner | e01eaa0 | 2006-11-16 01:00:07 +0000 | [diff] [blame] | 185 | // Branch folding must be run after regalloc and prolog/epilog insertion. |
| 186 | if (!Fast) |
Dale Johannesen | e6e4354 | 2007-05-22 18:31:04 +0000 | [diff] [blame] | 187 | PM.add(createBranchFoldingPass(getEnableTailMergeDefault())); |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 188 | |
| 189 | if (addPreEmitPass(PM, Fast) && PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 190 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 191 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 192 | addCodeEmitter(PM, Fast, MCE); |
| 193 | |
| 194 | // Delete machine code for this function |
| 195 | PM.add(createMachineCodeDeleter()); |
| 196 | |
| 197 | return false; // success! |
| 198 | } |