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