blob: 2572e11ae9e1a40edc283e7086a3857bd664a1eb [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonTargetMachine.cpp - Define TargetMachine for Hexagon -------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
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//
Jia Liub22310f2012-02-18 12:03:15 +000010// Implements the info about Hexagon target spec.
Tony Linthicum1213a7a2011-12-12 21:14:40 +000011//
12//===----------------------------------------------------------------------===//
13
Tony Linthicum1213a7a2011-12-12 21:14:40 +000014#include "HexagonTargetMachine.h"
15#include "Hexagon.h"
16#include "HexagonISelLowering.h"
Sergei Larin4d8986a2012-09-04 14:49:56 +000017#include "HexagonMachineScheduler.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000018#include "HexagonTargetObjectFile.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000019#include "llvm/CodeGen/Passes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Module.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000021#include "llvm/PassManager.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000022#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Transforms/IPO/PassManagerBuilder.h"
25#include "llvm/Transforms/Scalar.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026
Tony Linthicum1213a7a2011-12-12 21:14:40 +000027using namespace llvm;
28
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +000029static cl:: opt<bool> DisableHardwareLoops("disable-hexagon-hwloops",
30 cl::Hidden, cl::desc("Disable Hardware Loops for Hexagon target"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000031
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +000032static cl::opt<bool> DisableHexagonMISched("disable-hexagon-misched",
33 cl::Hidden, cl::ZeroOrMore, cl::init(false),
34 cl::desc("Disable Hexagon MI Scheduling"));
Sergei Larin4d8986a2012-09-04 14:49:56 +000035
Jyotsna Verma653d8832013-03-27 11:14:24 +000036static cl::opt<bool> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +000037 cl::Hidden, cl::ZeroOrMore, cl::init(false),
38 cl::desc("Disable Hexagon CFG Optimization"));
39
Jyotsna Verma653d8832013-03-27 11:14:24 +000040
Tony Linthicum1213a7a2011-12-12 21:14:40 +000041/// 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.
46extern "C" int HexagonTargetMachineModule;
47int HexagonTargetMachineModule = 0;
48
49extern "C" void LLVMInitializeHexagonTarget() {
50 // Register the target.
51 RegisterTargetMachine<HexagonTargetMachine> X(TheHexagonTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000052}
53
Sergei Larin4d8986a2012-09-04 14:49:56 +000054static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
David Blaikie422b93d2014-04-21 20:32:32 +000055 return new VLIWMachineScheduler(C, make_unique<ConvergingVLIWScheduler>());
Sergei Larin4d8986a2012-09-04 14:49:56 +000056}
57
58static MachineSchedRegistry
59SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
60 createVLIWMachineSched);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000061
62/// HexagonTargetMachine ctor - Create an ILP32 architecture model.
63///
64
65/// Hexagon_TODO: Do I need an aggregate alignment?
66///
67HexagonTargetMachine::HexagonTargetMachine(const Target &T, StringRef TT,
68 StringRef CPU, StringRef FS,
Craig Topperb5454082012-03-17 09:24:09 +000069 const TargetOptions &Options,
Tony Linthicum1213a7a2011-12-12 21:14:40 +000070 Reloc::Model RM,
71 CodeModel::Model CM,
72 CodeGenOpt::Level OL)
73 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Rafael Espindola58873562014-01-03 19:21:54 +000074 DL("e-m:e-p:32:32-i1:32-i64:64-a:0-n32") ,
Benjamin Kramer9ca2e722011-12-16 19:08:59 +000075 Subtarget(TT, CPU, FS), InstrInfo(Subtarget), TLInfo(*this),
Tony Linthicum1213a7a2011-12-12 21:14:40 +000076 TSInfo(*this),
77 FrameLowering(Subtarget),
Chandler Carruth664e3542013-01-07 01:37:14 +000078 InstrItins(&Subtarget.getInstrItineraryData()) {
Rafael Espindola227144c2013-05-13 01:16:13 +000079 initAsmInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +000080}
81
Andrew Trickccb67362012-02-03 05:12:41 +000082namespace {
83/// Hexagon Code Generator Pass Configuration Options.
84class HexagonPassConfig : public TargetPassConfig {
85public:
Andrew Trickf8ea1082012-02-04 02:56:59 +000086 HexagonPassConfig(HexagonTargetMachine *TM, PassManagerBase &PM)
Sergei Larin4d8986a2012-09-04 14:49:56 +000087 : TargetPassConfig(TM, PM) {
Andrew Trick978674b2013-09-20 05:14:41 +000088 // FIXME: Rather than calling enablePass(&MachineSchedulerID) below, define
89 // HexagonSubtarget::enableMachineScheduler() { return true; }.
90 // That will bypass the SelectionDAG VLIW scheduler, which is probably just
91 // hurting compile time and will be removed eventually anyway.
92 if (DisableHexagonMISched)
93 disablePass(&MachineSchedulerID);
94 else
Sergei Larin4d8986a2012-09-04 14:49:56 +000095 enablePass(&MachineSchedulerID);
Sergei Larin4d8986a2012-09-04 14:49:56 +000096 }
Andrew Trickccb67362012-02-03 05:12:41 +000097
98 HexagonTargetMachine &getHexagonTargetMachine() const {
99 return getTM<HexagonTargetMachine>();
100 }
101
Craig Topper906c2cd2014-04-29 07:58:16 +0000102 ScheduleDAGInstrs *
103 createMachineScheduler(MachineSchedContext *C) const override {
Andrew Trick978674b2013-09-20 05:14:41 +0000104 return createVLIWMachineSched(C);
105 }
106
Craig Topper906c2cd2014-04-29 07:58:16 +0000107 bool addInstSelector() override;
108 bool addPreRegAlloc() override;
109 bool addPostRegAlloc() override;
110 bool addPreSched2() override;
111 bool addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000112};
113} // namespace
114
Andrew Trickf8ea1082012-02-04 02:56:59 +0000115TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
116 return new HexagonPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000117}
118
119bool HexagonPassConfig::addInstSelector() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000120 HexagonTargetMachine &TM = getHexagonTargetMachine();
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000121 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Jyotsna Verma653d8832013-03-27 11:14:24 +0000122
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000123 if (!NoOpt)
124 addPass(createHexagonRemoveExtendArgs(TM));
Jyotsna Verma653d8832013-03-27 11:14:24 +0000125
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000126 addPass(createHexagonISelDag(TM, getOptLevel()));
Jyotsna Verma653d8832013-03-27 11:14:24 +0000127
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000128 if (!NoOpt) {
Jyotsna Verma653d8832013-03-27 11:14:24 +0000129 addPass(createHexagonPeephole());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000130 printAndVerify("After hexagon peephole pass");
131 }
Jyotsna Verma653d8832013-03-27 11:14:24 +0000132
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000133 return false;
134}
135
Andrew Trickccb67362012-02-03 05:12:41 +0000136bool HexagonPassConfig::addPreRegAlloc() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000137 if (getOptLevel() != CodeGenOpt::None)
138 if (!DisableHardwareLoops)
139 addPass(createHexagonHardwareLoops());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000140 return false;
141}
142
Andrew Trickccb67362012-02-03 05:12:41 +0000143bool HexagonPassConfig::addPostRegAlloc() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000144 const HexagonTargetMachine &TM = getHexagonTargetMachine();
145 if (getOptLevel() != CodeGenOpt::None)
146 if (!DisableHexagonCFGOpt)
147 addPass(createHexagonCFGOptimizer(TM));
148 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000149}
150
Andrew Trickccb67362012-02-03 05:12:41 +0000151bool HexagonPassConfig::addPreSched2() {
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000152 const HexagonTargetMachine &TM = getHexagonTargetMachine();
Benjamin Kramer2057a2b2013-05-17 11:39:41 +0000153 const HexagonTargetObjectFile &TLOF =
154 (const HexagonTargetObjectFile &)getTargetLowering()->getObjFileLowering();
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000155
Jyotsna Verma803e5062013-05-14 18:54:06 +0000156 addPass(createHexagonCopyToCombine());
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000157 if (getOptLevel() != CodeGenOpt::None)
158 addPass(&IfConverterID);
159 if (!TLOF.IsSmallDataEnabled()) {
160 addPass(createHexagonSplitConst32AndConst64(TM));
161 printAndVerify("After hexagon split const32/64 pass");
162 }
163 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000164}
165
Andrew Trickccb67362012-02-03 05:12:41 +0000166bool HexagonPassConfig::addPreEmitPass() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000167 const HexagonTargetMachine &TM = getHexagonTargetMachine();
168 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000169
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000170 if (!NoOpt)
Jyotsna Verma653d8832013-03-27 11:14:24 +0000171 addPass(createHexagonNewValueJump());
Sirish Pande4bd20c52012-05-12 05:10:30 +0000172
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000173 // Expand Spill code for predicate registers.
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000174 addPass(createHexagonExpandPredSpillCode(TM));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000175
176 // Split up TFRcondsets into conditional transfers.
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000177 addPass(createHexagonSplitTFRCondSets(TM));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000178
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000179 // Create Packets.
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000180 if (!NoOpt) {
181 if (!DisableHardwareLoops)
182 addPass(createHexagonFixupHwLoops());
Jyotsna Verma653d8832013-03-27 11:14:24 +0000183 addPass(createHexagonPacketizer());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000184 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000185
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000186 return false;
187}