Hexagon: Fix Small Data support to handle -G 0 correctly.
[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, new 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,
71                                            CodeModel::Model CM,
72                                            CodeGenOpt::Level OL)
73   : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
74     DL("e-p:32:32:32-"
75                 "i64:64:64-i32:32:32-i16:16:16-i1:32:32-"
76                 "f64:64:64-f32:32:32-a0:0-n32") ,
77     Subtarget(TT, CPU, FS), InstrInfo(Subtarget), TLInfo(*this),
78     TSInfo(*this),
79     FrameLowering(Subtarget),
80     InstrItins(&Subtarget.getInstrItineraryData()) {
81     setMCUseCFI(false);
82 }
83
84 // addPassesForOptimizations - Allow the backend (target) to add Target
85 // Independent Optimization passes to the Pass Manager.
86 bool HexagonTargetMachine::addPassesForOptimizations(PassManagerBase &PM) {
87   if (getOptLevel() != CodeGenOpt::None) {
88     PM.add(createConstantPropagationPass());
89     PM.add(createLoopSimplifyPass());
90     PM.add(createDeadCodeEliminationPass());
91     PM.add(createConstantPropagationPass());
92     PM.add(createLoopUnrollPass());
93     PM.add(createLoopStrengthReducePass());
94   }
95   return true;
96 }
97
98 namespace {
99 /// Hexagon Code Generator Pass Configuration Options.
100 class HexagonPassConfig : public TargetPassConfig {
101 public:
102   HexagonPassConfig(HexagonTargetMachine *TM, PassManagerBase &PM)
103     : TargetPassConfig(TM, PM) {
104     // Enable MI scheduler.
105     if (!DisableHexagonMISched) {
106       enablePass(&MachineSchedulerID);
107       MachineSchedRegistry::setDefault(createVLIWMachineSched);
108     }
109   }
110
111   HexagonTargetMachine &getHexagonTargetMachine() const {
112     return getTM<HexagonTargetMachine>();
113   }
114
115   virtual bool addInstSelector();
116   virtual bool addPreRegAlloc();
117   virtual bool addPostRegAlloc();
118   virtual bool addPreSched2();
119   virtual bool addPreEmitPass();
120 };
121 } // namespace
122
123 TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
124   return new HexagonPassConfig(this, PM);
125 }
126
127 bool HexagonPassConfig::addInstSelector() {
128   const HexagonTargetMachine &TM = getHexagonTargetMachine();
129   bool NoOpt = (getOptLevel() == CodeGenOpt::None);
130
131   if (!NoOpt)
132     addPass(createHexagonRemoveExtendArgs(TM));
133
134   addPass(createHexagonISelDag(TM, getOptLevel()));
135
136   if (!NoOpt) {
137     addPass(createHexagonPeephole());
138     printAndVerify("After hexagon peephole pass");
139   }
140
141   return false;
142 }
143
144 bool HexagonPassConfig::addPreRegAlloc() {
145   if (getOptLevel() != CodeGenOpt::None)
146     if (!DisableHardwareLoops)
147       addPass(createHexagonHardwareLoops());
148   return false;
149 }
150
151 bool HexagonPassConfig::addPostRegAlloc() {
152   const HexagonTargetMachine &TM = getHexagonTargetMachine();
153   if (getOptLevel() != CodeGenOpt::None)
154     if (!DisableHexagonCFGOpt)
155       addPass(createHexagonCFGOptimizer(TM));
156   return false;
157 }
158
159 bool HexagonPassConfig::addPreSched2() {
160   const HexagonTargetMachine &TM = getHexagonTargetMachine();
161   HexagonTargetObjectFile &TLOF =
162     (HexagonTargetObjectFile&)(getTargetLowering()->getObjFileLowering());
163
164   if (getOptLevel() != CodeGenOpt::None)
165     addPass(&IfConverterID);
166   if (!TLOF.IsSmallDataEnabled()) {
167     addPass(createHexagonSplitConst32AndConst64(TM));
168     printAndVerify("After hexagon split const32/64 pass");
169   }
170   return true;
171   if (getOptLevel() != CodeGenOpt::None)
172     addPass(&IfConverterID);
173   return false;
174 }
175
176 bool HexagonPassConfig::addPreEmitPass() {
177   const HexagonTargetMachine &TM = getHexagonTargetMachine();
178   bool NoOpt = (getOptLevel() == CodeGenOpt::None);
179
180   if (!NoOpt)
181     addPass(createHexagonNewValueJump());
182
183   // Expand Spill code for predicate registers.
184   addPass(createHexagonExpandPredSpillCode(TM));
185
186   // Split up TFRcondsets into conditional transfers.
187   addPass(createHexagonSplitTFRCondSets(TM));
188
189   // Create Packets.
190   if (!NoOpt) {
191     if (!DisableHardwareLoops)
192       addPass(createHexagonFixupHwLoops());
193     addPass(createHexagonPacketizer());
194   }
195
196   return false;
197 }