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"
28 cl::opt<bool> PrintCode("print-machineinstrs",
29 cl::desc("Print generated machine code"));
30 cl::opt<bool> NoPatternISel("disable-pattern-isel", cl::init(true),
31 cl::desc("Use the 'simple' X86 instruction selector"));
34 // allocateX86TargetMachine - Allocate and return a subclass of TargetMachine
35 // that implements the X86 backend.
37 TargetMachine *allocateX86TargetMachine(const Module &M) {
38 return new X86TargetMachine(M);
42 /// X86TargetMachine ctor - Create an ILP32 architecture model
44 X86TargetMachine::X86TargetMachine(const Module &M)
45 : TargetMachine("X86", true, 4, 4, 4, 4, 4),
46 FrameInfo(TargetFrameInfo::StackGrowsDown, 8/*16 for SSE*/, 4) {
50 // addPassesToEmitAssembly - We currently use all of the same passes as the JIT
51 // does to emit statically compiled machine code.
52 bool X86TargetMachine::addPassesToEmitAssembly(PassManager &PM,
54 // FIXME: Implement the switch instruction in the instruction selector!
55 PM.add(createLowerSwitchPass());
57 // FIXME: Implement the invoke/unwind instructions!
58 PM.add(createLowerInvokePass());
60 // FIXME: The code generator does not properly handle functions with
61 // unreachable basic blocks.
62 PM.add(createCFGSimplificationPass());
65 PM.add(createX86SimpleInstructionSelector(*this));
67 PM.add(createX86PatternInstructionSelector(*this));
69 // TODO: optional optimizations go here
71 // FIXME: Add SSA based peephole optimizer here.
73 // Print the instruction selected machine code...
75 PM.add(createMachineFunctionPrinterPass());
77 // Perform register allocation to convert to a concrete x86 representation
78 PM.add(createRegisterAllocator());
81 PM.add(createMachineFunctionPrinterPass());
83 PM.add(createX86FloatingPointStackifierPass());
86 PM.add(createMachineFunctionPrinterPass());
88 // Insert prolog/epilog code. Eliminate abstract frame index references...
89 PM.add(createPrologEpilogCodeInserter());
91 PM.add(createX86PeepholeOptimizerPass());
93 if (PrintCode) // Print the register-allocated code
94 PM.add(createX86CodePrinterPass(std::cerr, *this));
96 PM.add(createX86CodePrinterPass(Out, *this));
97 return false; // success!
100 /// addPassesToJITCompile - Add passes to the specified pass manager to
101 /// implement a fast dynamic compiler for this target. Return true if this is
102 /// not supported for this target.
104 bool X86TargetMachine::addPassesToJITCompile(FunctionPassManager &PM) {
105 // FIXME: Implement the switch instruction in the instruction selector!
106 PM.add(createLowerSwitchPass());
108 // FIXME: Implement the invoke/unwind instructions!
109 PM.add(createLowerInvokePass());
111 // FIXME: The code generator does not properly handle functions with
112 // unreachable basic blocks.
113 PM.add(createCFGSimplificationPass());
116 PM.add(createX86SimpleInstructionSelector(*this));
118 PM.add(createX86PatternInstructionSelector(*this));
120 // TODO: optional optimizations go here
122 // FIXME: Add SSA based peephole optimizer here.
124 // Print the instruction selected machine code...
126 PM.add(createMachineFunctionPrinterPass());
128 // Perform register allocation to convert to a concrete x86 representation
129 PM.add(createRegisterAllocator());
132 PM.add(createMachineFunctionPrinterPass());
134 PM.add(createX86FloatingPointStackifierPass());
137 PM.add(createMachineFunctionPrinterPass());
139 // Insert prolog/epilog code. Eliminate abstract frame index references...
140 PM.add(createPrologEpilogCodeInserter());
142 PM.add(createX86PeepholeOptimizerPass());
144 if (PrintCode) // Print the register-allocated code
145 PM.add(createX86CodePrinterPass(std::cerr, *this));
146 return false; // success!
149 void X86TargetMachine::replaceMachineCodeForFunction (void *Old, void *New) {
150 // FIXME: This code could perhaps live in a more appropriate place.
151 char *OldByte = (char *) Old;
152 *OldByte++ = 0xE9; // Emit JMP opcode.
153 int32_t *OldWord = (int32_t *) OldByte;
154 int32_t NewAddr = (intptr_t) New;
155 int32_t OldAddr = (intptr_t) OldWord;
156 *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
159 } // End llvm namespace