fc2f082fef51f850da976c4915513ead78ace52e
[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 "HexagonTargetTransformInfo.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/IR/LegacyPassManager.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/TargetRegistry.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> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
33   cl::Hidden, cl::ZeroOrMore, cl::init(false),
34   cl::desc("Disable Hexagon CFG Optimization"));
35
36 static cl::opt<bool> DisableStoreWidening("disable-store-widen",
37   cl::Hidden, cl::init(false), cl::desc("Disable store widening"));
38
39 static cl::opt<bool> EnableExpandCondsets("hexagon-expand-condsets",
40   cl::init(true), cl::Hidden, cl::ZeroOrMore,
41   cl::desc("Early expansion of MUX"));
42
43 static cl::opt<bool> EnableEarlyIf("hexagon-eif", cl::init(true), cl::Hidden,
44   cl::ZeroOrMore, cl::desc("Enable early if-conversion"));
45
46 static cl::opt<bool> EnableGenInsert("hexagon-insert", cl::init(true),
47   cl::Hidden, cl::desc("Generate \"insert\" instructions"));
48
49 static cl::opt<bool> EnableCommGEP("hexagon-commgep", cl::init(true),
50   cl::Hidden, cl::ZeroOrMore, cl::desc("Enable commoning of GEP instructions"));
51
52 static cl::opt<bool> EnableGenExtract("hexagon-extract", cl::init(true),
53   cl::Hidden, cl::desc("Generate \"extract\" instructions"));
54
55 static cl::opt<bool> EnableGenMux("hexagon-mux", cl::init(true), cl::Hidden,
56   cl::desc("Enable converting conditional transfers into MUX instructions"));
57
58 static cl::opt<bool> EnableGenPred("hexagon-gen-pred", cl::init(true),
59   cl::Hidden, cl::desc("Enable conversion of arithmetic operations to "
60   "predicate instructions"));
61
62 /// HexagonTargetMachineModule - Note that this is used on hosts that
63 /// cannot link in a library unless there are references into the
64 /// library.  In particular, it seems that it is not possible to get
65 /// things to work on Win32 without this.  Though it is unused, do not
66 /// remove it.
67 extern "C" int HexagonTargetMachineModule;
68 int HexagonTargetMachineModule = 0;
69
70 extern "C" void LLVMInitializeHexagonTarget() {
71   // Register the target.
72   RegisterTargetMachine<HexagonTargetMachine> X(TheHexagonTarget);
73 }
74
75 static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
76   return new VLIWMachineScheduler(C, make_unique<ConvergingVLIWScheduler>());
77 }
78
79 static MachineSchedRegistry
80 SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
81                     createVLIWMachineSched);
82
83 namespace llvm {
84   FunctionPass *createHexagonCFGOptimizer();
85   FunctionPass *createHexagonCommonGEP();
86   FunctionPass *createHexagonCopyToCombine();
87   FunctionPass *createHexagonEarlyIfConversion();
88   FunctionPass *createHexagonExpandCondsets();
89   FunctionPass *createHexagonExpandPredSpillCode();
90   FunctionPass *createHexagonFixupHwLoops();
91   FunctionPass *createHexagonGenExtract();
92   FunctionPass *createHexagonGenInsert();
93   FunctionPass *createHexagonGenMux();
94   FunctionPass *createHexagonGenPredicate();
95   FunctionPass *createHexagonHardwareLoops();
96   FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
97                                      CodeGenOpt::Level OptLevel);
98   FunctionPass *createHexagonNewValueJump();
99   FunctionPass *createHexagonPacketizer();
100   FunctionPass *createHexagonPeephole();
101   FunctionPass *createHexagonSplitConst32AndConst64();
102   FunctionPass *createHexagonStoreWidening();
103 } // end namespace llvm;
104
105 /// HexagonTargetMachine ctor - Create an ILP32 architecture model.
106 ///
107
108 /// Hexagon_TODO: Do I need an aggregate alignment?
109 ///
110 HexagonTargetMachine::HexagonTargetMachine(const Target &T, const Triple &TT,
111                                            StringRef CPU, StringRef FS,
112                                            const TargetOptions &Options,
113                                            Reloc::Model RM, CodeModel::Model CM,
114                                            CodeGenOpt::Level OL)
115     : LLVMTargetMachine(T, "e-m:e-p:32:32-i1:32-i64:64-a:0-n32", TT, CPU, FS,
116                         Options, RM, CM, OL),
117       TLOF(make_unique<HexagonTargetObjectFile>()) {
118   initAsmInfo();
119 }
120
121 const HexagonSubtarget *
122 HexagonTargetMachine::getSubtargetImpl(const Function &F) const {
123   AttributeSet FnAttrs = F.getAttributes();
124   Attribute CPUAttr =
125       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
126   Attribute FSAttr =
127       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
128
129   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
130                         ? CPUAttr.getValueAsString().str()
131                         : TargetCPU;
132   std::string FS = !FSAttr.hasAttribute(Attribute::None)
133                        ? FSAttr.getValueAsString().str()
134                        : TargetFS;
135
136   auto &I = SubtargetMap[CPU + FS];
137   if (!I) {
138     // This needs to be done before we create a new subtarget since any
139     // creation will depend on the TM and the code generation flags on the
140     // function that reside in TargetOptions.
141     resetTargetOptions(F);
142     I = llvm::make_unique<HexagonSubtarget>(TargetTriple, CPU, FS, *this);
143   }
144   return I.get();
145 }
146
147 TargetIRAnalysis HexagonTargetMachine::getTargetIRAnalysis() {
148   return TargetIRAnalysis([this](const Function &F) {
149     return TargetTransformInfo(HexagonTTIImpl(this, F));
150   });
151 }
152
153
154 HexagonTargetMachine::~HexagonTargetMachine() {}
155
156 namespace {
157 /// Hexagon Code Generator Pass Configuration Options.
158 class HexagonPassConfig : public TargetPassConfig {
159 public:
160   HexagonPassConfig(HexagonTargetMachine *TM, PassManagerBase &PM)
161     : TargetPassConfig(TM, PM) {
162     bool NoOpt = (TM->getOptLevel() == CodeGenOpt::None);
163     if (!NoOpt) {
164       if (EnableExpandCondsets) {
165         Pass *Exp = createHexagonExpandCondsets();
166         insertPass(&RegisterCoalescerID, IdentifyingPassPtr(Exp));
167       }
168     }
169   }
170
171   HexagonTargetMachine &getHexagonTargetMachine() const {
172     return getTM<HexagonTargetMachine>();
173   }
174
175   ScheduleDAGInstrs *
176   createMachineScheduler(MachineSchedContext *C) const override {
177     return createVLIWMachineSched(C);
178   }
179
180   void addIRPasses() override;
181   bool addInstSelector() override;
182   void addPreRegAlloc() override;
183   void addPostRegAlloc() override;
184   void addPreSched2() override;
185   void addPreEmitPass() override;
186 };
187 } // namespace
188
189 TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
190   return new HexagonPassConfig(this, PM);
191 }
192
193 void HexagonPassConfig::addIRPasses() {
194   TargetPassConfig::addIRPasses();
195   bool NoOpt = (getOptLevel() == CodeGenOpt::None);
196
197   addPass(createAtomicExpandPass(TM));
198   if (!NoOpt) {
199     if (EnableCommGEP)
200       addPass(createHexagonCommonGEP());
201     // Replace certain combinations of shifts and ands with extracts.
202     if (EnableGenExtract)
203       addPass(createHexagonGenExtract());
204   }
205 }
206
207 bool HexagonPassConfig::addInstSelector() {
208   HexagonTargetMachine &TM = getHexagonTargetMachine();
209   bool NoOpt = (getOptLevel() == CodeGenOpt::None);
210
211   addPass(createHexagonISelDag(TM, getOptLevel()));
212
213   if (!NoOpt) {
214     // Create logical operations on predicate registers.
215     if (EnableGenPred)
216       addPass(createHexagonGenPredicate(), false);
217     addPass(createHexagonPeephole());
218     printAndVerify("After hexagon peephole pass");
219     if (EnableGenInsert)
220       addPass(createHexagonGenInsert(), false);
221     if (EnableEarlyIf)
222       addPass(createHexagonEarlyIfConversion(), false);
223   }
224
225   return false;
226 }
227
228 void HexagonPassConfig::addPreRegAlloc() {
229   if (getOptLevel() != CodeGenOpt::None) {
230     if (!DisableStoreWidening)
231       addPass(createHexagonStoreWidening(), false);
232     if (!DisableHardwareLoops)
233       addPass(createHexagonHardwareLoops(), false);
234   }
235 }
236
237 void HexagonPassConfig::addPostRegAlloc() {
238   if (getOptLevel() != CodeGenOpt::None)
239     if (!DisableHexagonCFGOpt)
240       addPass(createHexagonCFGOptimizer(), false);
241 }
242
243 void HexagonPassConfig::addPreSched2() {
244   addPass(createHexagonCopyToCombine(), false);
245   if (getOptLevel() != CodeGenOpt::None)
246     addPass(&IfConverterID, false);
247   addPass(createHexagonSplitConst32AndConst64());
248 }
249
250 void HexagonPassConfig::addPreEmitPass() {
251   bool NoOpt = (getOptLevel() == CodeGenOpt::None);
252
253   if (!NoOpt)
254     addPass(createHexagonNewValueJump(), false);
255
256   // Expand Spill code for predicate registers.
257   addPass(createHexagonExpandPredSpillCode(), false);
258
259   // Create Packets.
260   if (!NoOpt) {
261     if (!DisableHardwareLoops)
262       addPass(createHexagonFixupHwLoops(), false);
263     // Generate MUX from pairs of conditional transfers.
264     if (EnableGenMux)
265       addPass(createHexagonGenMux(), false);
266
267     addPass(createHexagonPacketizer(), false);
268   }
269 }