This patch changes the ownership of TLOF from TargetLoweringBase to TargetMachine...
[oota-llvm.git] / lib / Target / Hexagon / HexagonTargetMachine.cpp
1 //===-- HexagonTargetMachine.cpp - Define TargetMachine for Hexagon -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implements the info about Hexagon target spec.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "HexagonTargetMachine.h"
15 #include "Hexagon.h"
16 #include "HexagonISelLowering.h"
17 #include "HexagonMachineScheduler.h"
18 #include "HexagonTargetObjectFile.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/TargetRegistry.h"
24 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
25 #include "llvm/Transforms/Scalar.h"
26
27 using namespace llvm;
28
29 static cl:: opt<bool> DisableHardwareLoops("disable-hexagon-hwloops",
30       cl::Hidden, cl::desc("Disable Hardware Loops for Hexagon target"));
31
32 static cl::opt<bool> DisableHexagonMISched("disable-hexagon-misched",
33       cl::Hidden, cl::ZeroOrMore, cl::init(false),
34       cl::desc("Disable Hexagon MI Scheduling"));
35
36 static cl::opt<bool> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
37       cl::Hidden, cl::ZeroOrMore, cl::init(false),
38       cl::desc("Disable Hexagon CFG Optimization"));
39
40
41 /// HexagonTargetMachineModule - Note that this is used on hosts that
42 /// cannot link in a library unless there are references into the
43 /// library.  In particular, it seems that it is not possible to get
44 /// things to work on Win32 without this.  Though it is unused, do not
45 /// remove it.
46 extern "C" int HexagonTargetMachineModule;
47 int HexagonTargetMachineModule = 0;
48
49 extern "C" void LLVMInitializeHexagonTarget() {
50   // Register the target.
51   RegisterTargetMachine<HexagonTargetMachine> X(TheHexagonTarget);
52 }
53
54 static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
55   return new VLIWMachineScheduler(C, make_unique<ConvergingVLIWScheduler>());
56 }
57
58 static MachineSchedRegistry
59 SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
60                     createVLIWMachineSched);
61
62 /// HexagonTargetMachine ctor - Create an ILP32 architecture model.
63 ///
64
65 /// Hexagon_TODO: Do I need an aggregate alignment?
66 ///
67 HexagonTargetMachine::HexagonTargetMachine(const Target &T, StringRef TT,
68                                            StringRef CPU, StringRef FS,
69                                            const TargetOptions &Options,
70                                            Reloc::Model RM, CodeModel::Model CM,
71                                            CodeGenOpt::Level OL)
72     : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
73       TLOF(make_unique<HexagonTargetObjectFile>()),
74       Subtarget(TT, CPU, FS, *this) {
75     initAsmInfo();
76 }
77
78 namespace {
79 /// Hexagon Code Generator Pass Configuration Options.
80 class HexagonPassConfig : public TargetPassConfig {
81 public:
82   HexagonPassConfig(HexagonTargetMachine *TM, PassManagerBase &PM)
83     : TargetPassConfig(TM, PM) {
84     // FIXME: Rather than calling enablePass(&MachineSchedulerID) below, define
85     // HexagonSubtarget::enableMachineScheduler() { return true; }.
86     // That will bypass the SelectionDAG VLIW scheduler, which is probably just
87     // hurting compile time and will be removed eventually anyway.
88     if (DisableHexagonMISched)
89       disablePass(&MachineSchedulerID);
90     else
91       enablePass(&MachineSchedulerID);
92   }
93
94   HexagonTargetMachine &getHexagonTargetMachine() const {
95     return getTM<HexagonTargetMachine>();
96   }
97
98   ScheduleDAGInstrs *
99   createMachineScheduler(MachineSchedContext *C) const override {
100     return createVLIWMachineSched(C);
101   }
102
103   bool addInstSelector() override;
104   bool addPreRegAlloc() override;
105   bool addPostRegAlloc() override;
106   bool addPreSched2() override;
107   bool addPreEmitPass() override;
108 };
109 } // namespace
110
111 TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
112   return new HexagonPassConfig(this, PM);
113 }
114
115 bool HexagonPassConfig::addInstSelector() {
116   HexagonTargetMachine &TM = getHexagonTargetMachine();
117   bool NoOpt = (getOptLevel() == CodeGenOpt::None);
118
119   if (!NoOpt)
120     addPass(createHexagonRemoveExtendArgs(TM));
121
122   addPass(createHexagonISelDag(TM, getOptLevel()));
123
124   if (!NoOpt) {
125     addPass(createHexagonPeephole());
126     printAndVerify("After hexagon peephole pass");
127   }
128
129   return false;
130 }
131
132 bool HexagonPassConfig::addPreRegAlloc() {
133   if (getOptLevel() != CodeGenOpt::None)
134     if (!DisableHardwareLoops)
135       addPass(createHexagonHardwareLoops());
136   return false;
137 }
138
139 bool HexagonPassConfig::addPostRegAlloc() {
140   const HexagonTargetMachine &TM = getHexagonTargetMachine();
141   if (getOptLevel() != CodeGenOpt::None)
142     if (!DisableHexagonCFGOpt)
143       addPass(createHexagonCFGOptimizer(TM));
144   return false;
145 }
146
147 bool HexagonPassConfig::addPreSched2() {
148   const HexagonTargetMachine &TM = getHexagonTargetMachine();
149
150   addPass(createHexagonCopyToCombine());
151   if (getOptLevel() != CodeGenOpt::None)
152     addPass(&IfConverterID);
153   addPass(createHexagonSplitConst32AndConst64(TM));
154   printAndVerify("After hexagon split const32/64 pass");
155   return true;
156 }
157
158 bool HexagonPassConfig::addPreEmitPass() {
159   const HexagonTargetMachine &TM = getHexagonTargetMachine();
160   bool NoOpt = (getOptLevel() == CodeGenOpt::None);
161
162   if (!NoOpt)
163     addPass(createHexagonNewValueJump());
164
165   // Expand Spill code for predicate registers.
166   addPass(createHexagonExpandPredSpillCode(TM));
167
168   // Split up TFRcondsets into conditional transfers.
169   addPass(createHexagonSplitTFRCondSets(TM));
170
171   // Create Packets.
172   if (!NoOpt) {
173     if (!DisableHardwareLoops)
174       addPass(createHexagonFixupHwLoops());
175     addPass(createHexagonPacketizer());
176   }
177
178   return false;
179 }