blob: 2de30f7245d096771daddbd5637da804011b8d42 [file] [log] [blame]
Chris Lattnerb4f68ed2002-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 Lattner5bcd95c2002-12-24 00:04:01 +00008#include "X86.h"
Chris Lattner155e68f2003-04-23 16:24:55 +00009#include "llvm/PassManager.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000010#include "llvm/Target/TargetMachineImpls.h"
Chris Lattner3dffa792002-10-30 00:47:49 +000011#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd91d86f2003-01-13 00:51:23 +000012#include "llvm/CodeGen/Passes.h"
Chris Lattner155e68f2003-04-23 16:24:55 +000013#include "llvm/Transforms/Scalar.h"
Chris Lattner439a27a2002-12-16 16:15:51 +000014#include "Support/CommandLine.h"
15#include "Support/Statistic.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000016#include <iostream>
17
Chris Lattner439a27a2002-12-16 16:15:51 +000018namespace {
Chris Lattnerddd5b412003-02-26 20:00:41 +000019 cl::opt<bool> NoLocalRA("disable-local-ra",
Chris Lattner14322cd32002-12-17 02:51:15 +000020 cl::desc("Use Simple RA instead of Local RegAlloc"));
Chris Lattner5bcd95c2002-12-24 00:04:01 +000021 cl::opt<bool> PrintCode("print-machineinstrs",
22 cl::desc("Print generated machine code"));
Chris Lattnerac0c8682003-08-11 14:59:22 +000023 cl::opt<bool> NoPatternISel("disable-pattern-isel", cl::init(true),
24 cl::desc("Use the 'simple' X86 instruction selector"));
Chris Lattner439a27a2002-12-16 16:15:51 +000025}
26
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000027// allocateX86TargetMachine - Allocate and return a subclass of TargetMachine
28// that implements the X86 backend.
29//
Chris Lattner5bcd95c2002-12-24 00:04:01 +000030TargetMachine *allocateX86TargetMachine(unsigned Configuration) {
31 return new X86TargetMachine(Configuration);
32}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000033
34
35/// X86TargetMachine ctor - Create an ILP32 architecture model
36///
Chris Lattner5bcd95c2002-12-24 00:04:01 +000037X86TargetMachine::X86TargetMachine(unsigned Config)
38 : TargetMachine("X86",
39 (Config & TM::EndianMask) == TM::LittleEndian,
Chris Lattner5bcd95c2002-12-24 00:04:01 +000040 (Config & TM::PtrSizeMask) == TM::PtrSize64 ? 8 : 4,
Chris Lattner98938f82003-04-25 06:05:57 +000041 (Config & TM::PtrSizeMask) == TM::PtrSize64 ? 8 : 4,
Chris Lattnerd282cfe2002-12-28 20:33:32 +000042 (Config & TM::PtrSizeMask) == TM::PtrSize64 ? 8 : 4),
Chris Lattnerf158da22003-01-16 02:20:12 +000043 FrameInfo(TargetFrameInfo::StackGrowsDown, 8/*16 for SSE*/, 4) {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000044}
45
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +000046
47// addPassesToEmitAssembly - We currently use all of the same passes as the JIT
48// does to emit statically compiled machine code.
Brian Gaekede3aa4f2003-06-18 21:43:21 +000049bool X86TargetMachine::addPassesToEmitAssembly(PassManager &PM,
50 std::ostream &Out) {
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +000051 addPassesToJITCompile(PM);
Brian Gaekede420ae2003-07-23 20:25:08 +000052 PM.add(createX86CodePrinterPass(Out, *this));
Brian Gaekede3aa4f2003-06-18 21:43:21 +000053 return false; // success!
54}
55
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000056/// addPassesToJITCompile - Add passes to the specified pass manager to
57/// implement a fast dynamic compiler for this target. Return true if this is
58/// not supported for this target.
59///
60bool X86TargetMachine::addPassesToJITCompile(PassManager &PM) {
Chris Lattner155e68f2003-04-23 16:24:55 +000061 // FIXME: Implement the switch instruction in the instruction selector!
62 PM.add(createLowerSwitchPass());
63
Chris Lattnerac0c8682003-08-11 14:59:22 +000064 if (NoPatternISel)
65 PM.add(createX86SimpleInstructionSelector(*this));
66 else
67 PM.add(createX86PatternInstructionSelector(*this));
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000068
69 // TODO: optional optimizations go here
70
Chris Lattnerd91d86f2003-01-13 00:51:23 +000071 // FIXME: Add SSA based peephole optimizer here.
72
Chris Lattner3dffa792002-10-30 00:47:49 +000073 // Print the instruction selected machine code...
Chris Lattner5bcd95c2002-12-24 00:04:01 +000074 if (PrintCode)
75 PM.add(createMachineFunctionPrinterPass());
Chris Lattner3dffa792002-10-30 00:47:49 +000076
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000077 // Perform register allocation to convert to a concrete x86 representation
Chris Lattner14322cd32002-12-17 02:51:15 +000078 if (NoLocalRA)
Chris Lattnerd282cfe2002-12-28 20:33:32 +000079 PM.add(createSimpleRegisterAllocator());
Chris Lattner14322cd32002-12-17 02:51:15 +000080 else
Chris Lattnerd282cfe2002-12-28 20:33:32 +000081 PM.add(createLocalRegisterAllocator());
82
83 if (PrintCode)
84 PM.add(createMachineFunctionPrinterPass());
85
Chris Lattnerd91d86f2003-01-13 00:51:23 +000086 PM.add(createX86FloatingPointStackifierPass());
87
88 if (PrintCode)
89 PM.add(createMachineFunctionPrinterPass());
90
Chris Lattnerd282cfe2002-12-28 20:33:32 +000091 // Insert prolog/epilog code. Eliminate abstract frame index references...
92 PM.add(createPrologEpilogCodeInserter());
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000093
Chris Lattnerd91d86f2003-01-13 00:51:23 +000094 PM.add(createX86PeepholeOptimizerPass());
95
Chris Lattner430cda72002-12-25 05:06:21 +000096 if (PrintCode) // Print the register-allocated code
Brian Gaekede420ae2003-07-23 20:25:08 +000097 PM.add(createX86CodePrinterPass(std::cerr, *this));
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000098 return false; // success!
99}
100