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