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