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