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