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