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