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