blob: d511de67212a702c023366294ae9c7f47497aca7 [file] [log] [blame]
Chris Lattner02a3d832002-10-29 22:37:54 +00001//===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
2//
3// This file defines the X86 specific subclass of TargetMachine.
4//
5//===----------------------------------------------------------------------===//
6
7#include "X86TargetMachine.h"
Chris Lattnera32b4052002-12-24 00:04:01 +00008#include "X86.h"
Chris Lattner4fd144a2003-08-24 19:49:48 +00009#include "llvm/Module.h"
Chris Lattner524608a2003-04-23 16:24:55 +000010#include "llvm/PassManager.h"
Chris Lattner02a3d832002-10-29 22:37:54 +000011#include "llvm/Target/TargetMachineImpls.h"
Chris Lattnerd7a85562002-10-30 00:47:49 +000012#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner962d5be2003-01-13 00:51:23 +000013#include "llvm/CodeGen/Passes.h"
Chris Lattner524608a2003-04-23 16:24:55 +000014#include "llvm/Transforms/Scalar.h"
Chris Lattnere0c25aa2002-12-16 16:15:51 +000015#include "Support/CommandLine.h"
16#include "Support/Statistic.h"
Chris Lattner02a3d832002-10-29 22:37:54 +000017
Chris Lattnere0c25aa2002-12-16 16:15:51 +000018namespace {
Chris Lattnera32b4052002-12-24 00:04:01 +000019 cl::opt<bool> PrintCode("print-machineinstrs",
20 cl::desc("Print generated machine code"));
Chris Lattnere61db422003-08-11 14:59:22 +000021 cl::opt<bool> NoPatternISel("disable-pattern-isel", cl::init(true),
22 cl::desc("Use the 'simple' X86 instruction selector"));
Chris Lattnere0c25aa2002-12-16 16:15:51 +000023}
24
Chris Lattner02a3d832002-10-29 22:37:54 +000025// allocateX86TargetMachine - Allocate and return a subclass of TargetMachine
26// that implements the X86 backend.
27//
Chris Lattner4fd144a2003-08-24 19:49:48 +000028TargetMachine *allocateX86TargetMachine(const Module &M) {
29 return new X86TargetMachine(M);
Chris Lattnera32b4052002-12-24 00:04:01 +000030}
Chris Lattner02a3d832002-10-29 22:37:54 +000031
32
33/// X86TargetMachine ctor - Create an ILP32 architecture model
34///
Chris Lattner4fd144a2003-08-24 19:49:48 +000035X86TargetMachine::X86TargetMachine(const Module &M)
Chris Lattnera32b4052002-12-24 00:04:01 +000036 : TargetMachine("X86",
Chris Lattner4fd144a2003-08-24 19:49:48 +000037 M.getEndianness() != Module::BigEndian,
38 M.getPointerSize() != Module::Pointer64 ? 4 : 8,
39 M.getPointerSize() != Module::Pointer64 ? 4 : 8,
40 M.getPointerSize() != Module::Pointer64 ? 4 : 8,
41 4, M.getPointerSize() != Module::Pointer64 ? 4 : 8),
42 FrameInfo(TargetFrameInfo::StackGrowsDown, 8/*16 for SSE*/, 4) {
Chris Lattner02a3d832002-10-29 22:37:54 +000043}
44
Chris Lattner1d6ba3e2003-08-05 16:34:44 +000045
46// addPassesToEmitAssembly - We currently use all of the same passes as the JIT
47// does to emit statically compiled machine code.
Brian Gaekeac94bab2003-06-18 21:43:21 +000048bool X86TargetMachine::addPassesToEmitAssembly(PassManager &PM,
49 std::ostream &Out) {
Brian Gaeke02cbe282003-08-13 18:15:52 +000050 // FIXME: Implement the switch instruction in the instruction selector!
51 PM.add(createLowerSwitchPass());
52
53 if (NoPatternISel)
54 PM.add(createX86SimpleInstructionSelector(*this));
55 else
56 PM.add(createX86PatternInstructionSelector(*this));
57
58 // TODO: optional optimizations go here
59
60 // FIXME: Add SSA based peephole optimizer here.
61
62 // Print the instruction selected machine code...
63 if (PrintCode)
64 PM.add(createMachineFunctionPrinterPass());
65
66 // Perform register allocation to convert to a concrete x86 representation
Alkis Evlogimenos5facafa2003-10-02 16:57:49 +000067 PM.add(createRegisterAllocator());
Brian Gaeke02cbe282003-08-13 18:15:52 +000068
69 if (PrintCode)
70 PM.add(createMachineFunctionPrinterPass());
71
72 PM.add(createX86FloatingPointStackifierPass());
73
74 if (PrintCode)
75 PM.add(createMachineFunctionPrinterPass());
76
77 // Insert prolog/epilog code. Eliminate abstract frame index references...
78 PM.add(createPrologEpilogCodeInserter());
79
80 PM.add(createX86PeepholeOptimizerPass());
81
82 if (PrintCode) // Print the register-allocated code
83 PM.add(createX86CodePrinterPass(std::cerr, *this));
84
Brian Gaekea92dce42003-07-23 20:25:08 +000085 PM.add(createX86CodePrinterPass(Out, *this));
Brian Gaekeac94bab2003-06-18 21:43:21 +000086 return false; // success!
87}
88
Chris Lattner02a3d832002-10-29 22:37:54 +000089/// addPassesToJITCompile - Add passes to the specified pass manager to
90/// implement a fast dynamic compiler for this target. Return true if this is
91/// not supported for this target.
92///
Brian Gaeke02cbe282003-08-13 18:15:52 +000093bool X86TargetMachine::addPassesToJITCompile(FunctionPassManager &PM) {
Chris Lattner524608a2003-04-23 16:24:55 +000094 // FIXME: Implement the switch instruction in the instruction selector!
95 PM.add(createLowerSwitchPass());
96
Chris Lattnere61db422003-08-11 14:59:22 +000097 if (NoPatternISel)
98 PM.add(createX86SimpleInstructionSelector(*this));
99 else
100 PM.add(createX86PatternInstructionSelector(*this));
Chris Lattner02a3d832002-10-29 22:37:54 +0000101
102 // TODO: optional optimizations go here
103
Chris Lattner962d5be2003-01-13 00:51:23 +0000104 // FIXME: Add SSA based peephole optimizer here.
105
Chris Lattnerd7a85562002-10-30 00:47:49 +0000106 // Print the instruction selected machine code...
Chris Lattnera32b4052002-12-24 00:04:01 +0000107 if (PrintCode)
108 PM.add(createMachineFunctionPrinterPass());
Chris Lattnerd7a85562002-10-30 00:47:49 +0000109
Chris Lattner02a3d832002-10-29 22:37:54 +0000110 // Perform register allocation to convert to a concrete x86 representation
Alkis Evlogimenos5facafa2003-10-02 16:57:49 +0000111 PM.add(createRegisterAllocator());
Chris Lattner9a81e692002-12-28 20:33:32 +0000112
113 if (PrintCode)
114 PM.add(createMachineFunctionPrinterPass());
115
Chris Lattner962d5be2003-01-13 00:51:23 +0000116 PM.add(createX86FloatingPointStackifierPass());
117
118 if (PrintCode)
119 PM.add(createMachineFunctionPrinterPass());
120
Chris Lattner9a81e692002-12-28 20:33:32 +0000121 // Insert prolog/epilog code. Eliminate abstract frame index references...
122 PM.add(createPrologEpilogCodeInserter());
Chris Lattner02a3d832002-10-29 22:37:54 +0000123
Chris Lattner962d5be2003-01-13 00:51:23 +0000124 PM.add(createX86PeepholeOptimizerPass());
125
Chris Lattner55aaff52002-12-25 05:06:21 +0000126 if (PrintCode) // Print the register-allocated code
Brian Gaekea92dce42003-07-23 20:25:08 +0000127 PM.add(createX86CodePrinterPass(std::cerr, *this));
Chris Lattner02a3d832002-10-29 22:37:54 +0000128 return false; // success!
129}
130