1 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the X86 specific subclass of TargetMachine.
12 //===----------------------------------------------------------------------===//
14 #include "X86TargetMachine.h"
16 #include "llvm/Module.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/Target/TargetMachineImpls.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/Transforms/Scalar.h"
22 #include "Support/CommandLine.h"
23 #include "Support/Statistic.h"
26 cl::opt<bool> PrintCode("print-machineinstrs",
27 cl::desc("Print generated machine code"));
28 cl::opt<bool> NoPatternISel("disable-pattern-isel", cl::init(true),
29 cl::desc("Use the 'simple' X86 instruction selector"));
32 // allocateX86TargetMachine - Allocate and return a subclass of TargetMachine
33 // that implements the X86 backend.
35 TargetMachine *allocateX86TargetMachine(const Module &M) {
36 return new X86TargetMachine(M);
40 /// X86TargetMachine ctor - Create an ILP32 architecture model
42 X86TargetMachine::X86TargetMachine(const Module &M)
43 : TargetMachine("X86", true, 4, 4, 4, 4, 4),
44 FrameInfo(TargetFrameInfo::StackGrowsDown, 8/*16 for SSE*/, 4) {
48 // addPassesToEmitAssembly - We currently use all of the same passes as the JIT
49 // does to emit statically compiled machine code.
50 bool X86TargetMachine::addPassesToEmitAssembly(PassManager &PM,
52 // FIXME: Implement the switch instruction in the instruction selector!
53 PM.add(createLowerSwitchPass());
55 // FIXME: Implement the invoke/unwind instructions!
56 PM.add(createLowerInvokePass());
58 // FIXME: The code generator does not properly handle functions with
59 // unreachable basic blocks.
60 PM.add(createCFGSimplificationPass());
63 PM.add(createX86SimpleInstructionSelector(*this));
65 PM.add(createX86PatternInstructionSelector(*this));
67 // TODO: optional optimizations go here
69 // FIXME: Add SSA based peephole optimizer here.
71 // Print the instruction selected machine code...
73 PM.add(createMachineFunctionPrinterPass());
75 // Perform register allocation to convert to a concrete x86 representation
76 PM.add(createRegisterAllocator());
79 PM.add(createMachineFunctionPrinterPass());
81 PM.add(createX86FloatingPointStackifierPass());
84 PM.add(createMachineFunctionPrinterPass());
86 // Insert prolog/epilog code. Eliminate abstract frame index references...
87 PM.add(createPrologEpilogCodeInserter());
89 PM.add(createX86PeepholeOptimizerPass());
91 if (PrintCode) // Print the register-allocated code
92 PM.add(createX86CodePrinterPass(std::cerr, *this));
94 PM.add(createX86CodePrinterPass(Out, *this));
95 return false; // success!
98 /// addPassesToJITCompile - Add passes to the specified pass manager to
99 /// implement a fast dynamic compiler for this target. Return true if this is
100 /// not supported for this target.
102 bool X86TargetMachine::addPassesToJITCompile(FunctionPassManager &PM) {
103 // FIXME: Implement the switch instruction in the instruction selector!
104 PM.add(createLowerSwitchPass());
106 // FIXME: Implement the invoke/unwind instructions!
107 PM.add(createLowerInvokePass());
109 // FIXME: The code generator does not properly handle functions with
110 // unreachable basic blocks.
111 PM.add(createCFGSimplificationPass());
114 PM.add(createX86SimpleInstructionSelector(*this));
116 PM.add(createX86PatternInstructionSelector(*this));
118 // TODO: optional optimizations go here
120 // FIXME: Add SSA based peephole optimizer here.
122 // Print the instruction selected machine code...
124 PM.add(createMachineFunctionPrinterPass());
126 // Perform register allocation to convert to a concrete x86 representation
127 PM.add(createRegisterAllocator());
130 PM.add(createMachineFunctionPrinterPass());
132 PM.add(createX86FloatingPointStackifierPass());
135 PM.add(createMachineFunctionPrinterPass());
137 // Insert prolog/epilog code. Eliminate abstract frame index references...
138 PM.add(createPrologEpilogCodeInserter());
140 PM.add(createX86PeepholeOptimizerPass());
142 if (PrintCode) // Print the register-allocated code
143 PM.add(createX86CodePrinterPass(std::cerr, *this));
144 return false; // success!
147 void X86TargetMachine::replaceMachineCodeForFunction (void *Old, void *New) {
148 // FIXME: This code could perhaps live in a more appropriate place.
149 char *OldByte = (char *) Old;
150 *OldByte++ = 0xE9; // Emit JMP opcode.
151 int32_t *OldWord = (int32_t *) OldByte;
152 int32_t NewAddr = (intptr_t) New;
153 int32_t OldAddr = (intptr_t) OldWord;
154 *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.