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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 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")); |
Evan Cheng | 8bd6035 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 29 | static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden, |
| 30 | cl::desc("Dump emitter generated instructions as assembly")); |
Chris Lattner | 85ef254 | 2007-06-19 05:47:49 +0000 | [diff] [blame] | 31 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 32 | // Hidden options to help debugging |
| 33 | static cl::opt<bool> |
| 34 | EnableSinking("enable-sinking", cl::init(false), cl::Hidden, |
| 35 | cl::desc("Perform sinking on machine code")); |
| 36 | |
| 37 | |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 38 | FileModel::Model |
| 39 | LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM, |
| 40 | std::ostream &Out, |
| 41 | CodeGenFileType FileType, |
| 42 | bool Fast) { |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 43 | // Standard LLVM-Level Passes. |
| 44 | |
| 45 | // Run loop strength reduction before anything else. |
Chris Lattner | 31442f9 | 2007-03-31 00:24:43 +0000 | [diff] [blame] | 46 | if (!Fast) { |
| 47 | PM.add(createLoopStrengthReducePass(getTargetLowering())); |
| 48 | if (PrintLSR) |
| 49 | PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr)); |
| 50 | } |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 51 | |
| 52 | // FIXME: Implement efficient support for garbage collection intrinsics. |
| 53 | PM.add(createLowerGCPass()); |
Duncan Sands | c375160 | 2007-07-11 16:59:20 +0000 | [diff] [blame] | 54 | |
Jim Laskey | a4e7cd9 | 2007-02-22 16:22:15 +0000 | [diff] [blame] | 55 | if (!ExceptionHandling) |
| 56 | PM.add(createLowerInvokePass(getTargetLowering())); |
Duncan Sands | c375160 | 2007-07-11 16:59:20 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 58 | // Make sure that no unreachable blocks are instruction selected. |
| 59 | PM.add(createUnreachableBlockEliminationPass()); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 60 | |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 61 | if (!Fast) |
| 62 | PM.add(createCodeGenPreparePass(getTargetLowering())); |
| 63 | |
| 64 | if (PrintISelInput) |
| 65 | PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n", |
| 66 | &cerr)); |
| 67 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 68 | // Ask the target for an isel. |
| 69 | if (addInstSelector(PM, Fast)) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 70 | return FileModel::Error; |
| 71 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 72 | // Print the instruction selected machine code... |
| 73 | if (PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 74 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 75 | |
| 76 | PM.add(createMachineLICMPass()); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 77 | |
| 78 | if (EnableSinking) |
| 79 | PM.add(createMachineSinkingPass()); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 81 | // Perform register allocation to convert to a concrete x86 representation |
| 82 | PM.add(createRegisterAllocator()); |
| 83 | |
| 84 | if (PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 85 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Christopher Lamb | ada779f | 2007-07-27 07:36:14 +0000 | [diff] [blame] | 86 | |
| 87 | PM.add(createLowerSubregsPass()); |
| 88 | |
| 89 | if (PrintMachineCode) // Print the subreg lowered code |
| 90 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 92 | // Run post-ra passes. |
| 93 | if (addPostRegAlloc(PM, Fast) && PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 94 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 95 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 96 | // Insert prolog/epilog code. Eliminate abstract frame index references... |
| 97 | PM.add(createPrologEpilogCodeInserter()); |
| 98 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 99 | // Second pass scheduler. |
Dale Johannesen | 72f1596 | 2007-07-13 17:31:29 +0000 | [diff] [blame] | 100 | if (!Fast) |
| 101 | PM.add(createPostRAScheduler()); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 4a84ad7 | 2006-10-13 20:45:56 +0000 | [diff] [blame] | 103 | // Branch folding must be run after regalloc and prolog/epilog insertion. |
Jim Laskey | 62d07d6 | 2006-10-24 16:11:49 +0000 | [diff] [blame] | 104 | if (!Fast) |
Dale Johannesen | e6e4354 | 2007-05-22 18:31:04 +0000 | [diff] [blame] | 105 | PM.add(createBranchFoldingPass(getEnableTailMergeDefault())); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 106 | |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 107 | // Fold redundant debug labels. |
| 108 | PM.add(createDebugLabelFoldingPass()); |
Chris Lattner | 4a84ad7 | 2006-10-13 20:45:56 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 110 | if (PrintMachineCode) // Print the register-allocated code |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 111 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 112 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 113 | if (addPreEmitPass(PM, Fast) && PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 114 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 115 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 116 | switch (FileType) { |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 117 | default: |
| 118 | break; |
| 119 | case TargetMachine::AssemblyFile: |
| 120 | if (addAssemblyEmitter(PM, Fast, Out)) |
| 121 | return FileModel::Error; |
| 122 | return FileModel::AsmFile; |
| 123 | case TargetMachine::ObjectFile: |
| 124 | if (getMachOWriterInfo()) |
| 125 | return FileModel::MachOFile; |
| 126 | else if (getELFWriterInfo()) |
| 127 | return FileModel::ElfFile; |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 128 | } |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 129 | |
| 130 | return FileModel::Error; |
| 131 | } |
| 132 | |
| 133 | /// addPassesToEmitFileFinish - If the passes to emit the specified file had to |
| 134 | /// be split up (e.g., to add an object writer pass), this method can be used to |
| 135 | /// finish up adding passes to emit the file, if necessary. |
| 136 | bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM, |
| 137 | MachineCodeEmitter *MCE, |
| 138 | bool Fast) { |
| 139 | if (MCE) |
Evan Cheng | 8bd6035 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 140 | addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 142 | // Delete machine code for this function |
| 143 | PM.add(createMachineCodeDeleter()); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 145 | return false; // success! |
| 146 | } |
| 147 | |
| 148 | /// addPassesToEmitMachineCode - Add passes to the specified pass manager to |
| 149 | /// get machine code emitted. This uses a MachineCodeEmitter object to handle |
| 150 | /// actually outputting the machine code and resolving things like the address |
| 151 | /// of functions. This method should returns true if machine code emission is |
| 152 | /// not supported. |
| 153 | /// |
| 154 | bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM, |
| 155 | MachineCodeEmitter &MCE, |
| 156 | bool Fast) { |
| 157 | // Standard LLVM-Level Passes. |
| 158 | |
| 159 | // Run loop strength reduction before anything else. |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 160 | if (!Fast) { |
| 161 | PM.add(createLoopStrengthReducePass(getTargetLowering())); |
| 162 | if (PrintLSR) |
| 163 | PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr)); |
| 164 | } |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 165 | |
| 166 | // FIXME: Implement efficient support for garbage collection intrinsics. |
| 167 | PM.add(createLowerGCPass()); |
| 168 | |
| 169 | // FIXME: Implement the invoke/unwind instructions! |
Duraid Madina | 2a0013f | 2006-09-04 06:21:35 +0000 | [diff] [blame] | 170 | PM.add(createLowerInvokePass(getTargetLowering())); |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 171 | |
| 172 | // Make sure that no unreachable blocks are instruction selected. |
| 173 | PM.add(createUnreachableBlockEliminationPass()); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 174 | |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 175 | if (!Fast) |
| 176 | PM.add(createCodeGenPreparePass(getTargetLowering())); |
| 177 | |
| 178 | if (PrintISelInput) |
| 179 | PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n", |
| 180 | &cerr)); |
| 181 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 182 | // Ask the target for an isel. |
| 183 | if (addInstSelector(PM, Fast)) |
| 184 | return true; |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 186 | // Print the instruction selected machine code... |
| 187 | if (PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 188 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 189 | |
| 190 | PM.add(createMachineLICMPass()); |
| 191 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 192 | // Perform register allocation to convert to a concrete x86 representation |
| 193 | PM.add(createRegisterAllocator()); |
| 194 | |
| 195 | if (PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 196 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Christopher Lamb | ada779f | 2007-07-27 07:36:14 +0000 | [diff] [blame] | 197 | |
| 198 | PM.add(createLowerSubregsPass()); |
| 199 | |
| 200 | if (PrintMachineCode) // Print the subreg lowered code |
| 201 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 203 | // Run post-ra passes. |
| 204 | if (addPostRegAlloc(PM, Fast) && PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 205 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 206 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 207 | // Insert prolog/epilog code. Eliminate abstract frame index references... |
| 208 | PM.add(createPrologEpilogCodeInserter()); |
| 209 | |
| 210 | if (PrintMachineCode) // Print the register-allocated code |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 211 | PM.add(createMachineFunctionPrinterPass(cerr)); |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 212 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 213 | // Second pass scheduler. |
Dale Johannesen | 72f1596 | 2007-07-13 17:31:29 +0000 | [diff] [blame] | 214 | if (!Fast) |
| 215 | PM.add(createPostRAScheduler()); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 216 | |
Chris Lattner | e01eaa0 | 2006-11-16 01:00:07 +0000 | [diff] [blame] | 217 | // Branch folding must be run after regalloc and prolog/epilog insertion. |
| 218 | if (!Fast) |
Dale Johannesen | e6e4354 | 2007-05-22 18:31:04 +0000 | [diff] [blame] | 219 | PM.add(createBranchFoldingPass(getEnableTailMergeDefault())); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 221 | if (addPreEmitPass(PM, Fast) && PrintMachineCode) |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 222 | PM.add(createMachineFunctionPrinterPass(cerr)); |
| 223 | |
Evan Cheng | 8bd6035 | 2007-07-20 21:56:13 +0000 | [diff] [blame] | 224 | addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE); |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 225 | |
| 226 | // Delete machine code for this function |
| 227 | PM.add(createMachineCodeDeleter()); |
| 228 | |
| 229 | return false; // success! |
| 230 | } |