blob: bb950a0ea75ae19c2653f7b3d8880ceffdf6e470 [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- HexagonTargetMachine.cpp - Define TargetMachine for Hexagon -------===//
Tony Linthicumb4b54152011-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 Liu31d157a2012-02-18 12:03:15 +000010// Implements the info about Hexagon target spec.
Tony Linthicumb4b54152011-12-12 21:14:40 +000011//
12//===----------------------------------------------------------------------===//
13
Tony Linthicumb4b54152011-12-12 21:14:40 +000014#include "HexagonTargetMachine.h"
15#include "Hexagon.h"
16#include "HexagonISelLowering.h"
Sergei Larin3e590402012-09-04 14:49:56 +000017#include "HexagonMachineScheduler.h"
Jyotsna Vermaf931f692013-05-07 19:53:00 +000018#include "HexagonTargetObjectFile.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000019#include "llvm/CodeGen/Passes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Module.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000021#include "llvm/PassManager.h"
Benjamin Kramerf3fd7ee2012-02-06 10:19:29 +000022#include "llvm/Support/CommandLine.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000023#include "llvm/Support/TargetRegistry.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Transforms/IPO/PassManagerBuilder.h"
25#include "llvm/Transforms/Scalar.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000026
Tony Linthicumb4b54152011-12-12 21:14:40 +000027using namespace llvm;
28
Krzysztof Parzyszekb0720902013-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 Linthicumb4b54152011-12-12 21:14:40 +000031
Krzysztof Parzyszekb0720902013-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 Larin3e590402012-09-04 14:49:56 +000035
Jyotsna Verma0f680702013-03-27 11:14:24 +000036static cl::opt<bool> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +000037 cl::Hidden, cl::ZeroOrMore, cl::init(false),
38 cl::desc("Disable Hexagon CFG Optimization"));
39
Jyotsna Verma0f680702013-03-27 11:14:24 +000040
Tony Linthicumb4b54152011-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 Linthicumb4b54152011-12-12 21:14:40 +000052}
53
Sergei Larin3e590402012-09-04 14:49:56 +000054static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
55 return new VLIWMachineScheduler(C, new ConvergingVLIWScheduler());
56}
57
58static MachineSchedRegistry
59SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
60 createVLIWMachineSched);
Tony Linthicumb4b54152011-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 Topper1e0c9ab2012-03-17 09:24:09 +000069 const TargetOptions &Options,
Tony Linthicumb4b54152011-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),
Micah Villmow3574eca2012-10-08 16:38:25 +000074 DL("e-p:32:32:32-"
Sirish Pande7517bbc2012-05-10 20:20:25 +000075 "i64:64:64-i32:32:32-i16:16:16-i1:32:32-"
76 "f64:64:64-f32:32:32-a0:0-n32") ,
Benjamin Kramer90345622011-12-16 19:08:59 +000077 Subtarget(TT, CPU, FS), InstrInfo(Subtarget), TLInfo(*this),
Tony Linthicumb4b54152011-12-12 21:14:40 +000078 TSInfo(*this),
79 FrameLowering(Subtarget),
Chandler Carruthaeef83c2013-01-07 01:37:14 +000080 InstrItins(&Subtarget.getInstrItineraryData()) {
Jyotsna Verma0f680702013-03-27 11:14:24 +000081 setMCUseCFI(false);
Rafael Espindola4a971702013-05-13 01:16:13 +000082 initAsmInfo();
Tony Linthicumb4b54152011-12-12 21:14:40 +000083}
84
85// addPassesForOptimizations - Allow the backend (target) to add Target
86// Independent Optimization passes to the Pass Manager.
87bool HexagonTargetMachine::addPassesForOptimizations(PassManagerBase &PM) {
Jyotsna Verma0f680702013-03-27 11:14:24 +000088 if (getOptLevel() != CodeGenOpt::None) {
89 PM.add(createConstantPropagationPass());
90 PM.add(createLoopSimplifyPass());
91 PM.add(createDeadCodeEliminationPass());
92 PM.add(createConstantPropagationPass());
93 PM.add(createLoopUnrollPass());
94 PM.add(createLoopStrengthReducePass());
95 }
Tony Linthicumb4b54152011-12-12 21:14:40 +000096 return true;
97}
98
Andrew Trick843ee2e2012-02-03 05:12:41 +000099namespace {
100/// Hexagon Code Generator Pass Configuration Options.
101class HexagonPassConfig : public TargetPassConfig {
102public:
Andrew Trick061efcf2012-02-04 02:56:59 +0000103 HexagonPassConfig(HexagonTargetMachine *TM, PassManagerBase &PM)
Sergei Larin3e590402012-09-04 14:49:56 +0000104 : TargetPassConfig(TM, PM) {
Andrew Trickf45edcc2013-09-20 05:14:41 +0000105 // FIXME: Rather than calling enablePass(&MachineSchedulerID) below, define
106 // HexagonSubtarget::enableMachineScheduler() { return true; }.
107 // That will bypass the SelectionDAG VLIW scheduler, which is probably just
108 // hurting compile time and will be removed eventually anyway.
109 if (DisableHexagonMISched)
110 disablePass(&MachineSchedulerID);
111 else
Sergei Larin3e590402012-09-04 14:49:56 +0000112 enablePass(&MachineSchedulerID);
Sergei Larin3e590402012-09-04 14:49:56 +0000113 }
Andrew Trick843ee2e2012-02-03 05:12:41 +0000114
115 HexagonTargetMachine &getHexagonTargetMachine() const {
116 return getTM<HexagonTargetMachine>();
117 }
118
Andrew Trickf45edcc2013-09-20 05:14:41 +0000119 virtual ScheduleDAGInstrs *
120 createMachineScheduler(MachineSchedContext *C) const {
121 return createVLIWMachineSched(C);
122 }
123
Andrew Trick843ee2e2012-02-03 05:12:41 +0000124 virtual bool addInstSelector();
125 virtual bool addPreRegAlloc();
126 virtual bool addPostRegAlloc();
127 virtual bool addPreSched2();
128 virtual bool addPreEmitPass();
129};
130} // namespace
131
Andrew Trick061efcf2012-02-04 02:56:59 +0000132TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
133 return new HexagonPassConfig(this, PM);
Andrew Trick843ee2e2012-02-03 05:12:41 +0000134}
135
136bool HexagonPassConfig::addInstSelector() {
Bill Wendlingba54bca2013-06-19 21:36:55 +0000137 HexagonTargetMachine &TM = getHexagonTargetMachine();
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000138 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Jyotsna Verma0f680702013-03-27 11:14:24 +0000139
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000140 if (!NoOpt)
141 addPass(createHexagonRemoveExtendArgs(TM));
Jyotsna Verma0f680702013-03-27 11:14:24 +0000142
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000143 addPass(createHexagonISelDag(TM, getOptLevel()));
Jyotsna Verma0f680702013-03-27 11:14:24 +0000144
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000145 if (!NoOpt) {
Jyotsna Verma0f680702013-03-27 11:14:24 +0000146 addPass(createHexagonPeephole());
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000147 printAndVerify("After hexagon peephole pass");
148 }
Jyotsna Verma0f680702013-03-27 11:14:24 +0000149
Tony Linthicumb4b54152011-12-12 21:14:40 +0000150 return false;
151}
152
Andrew Trick843ee2e2012-02-03 05:12:41 +0000153bool HexagonPassConfig::addPreRegAlloc() {
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000154 if (getOptLevel() != CodeGenOpt::None)
155 if (!DisableHardwareLoops)
156 addPass(createHexagonHardwareLoops());
Tony Linthicumb4b54152011-12-12 21:14:40 +0000157 return false;
158}
159
Andrew Trick843ee2e2012-02-03 05:12:41 +0000160bool HexagonPassConfig::addPostRegAlloc() {
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000161 const HexagonTargetMachine &TM = getHexagonTargetMachine();
162 if (getOptLevel() != CodeGenOpt::None)
163 if (!DisableHexagonCFGOpt)
164 addPass(createHexagonCFGOptimizer(TM));
165 return false;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000166}
167
Andrew Trick843ee2e2012-02-03 05:12:41 +0000168bool HexagonPassConfig::addPreSched2() {
Jyotsna Vermaf931f692013-05-07 19:53:00 +0000169 const HexagonTargetMachine &TM = getHexagonTargetMachine();
Benjamin Kramer033f3b72013-05-17 11:39:41 +0000170 const HexagonTargetObjectFile &TLOF =
171 (const HexagonTargetObjectFile &)getTargetLowering()->getObjFileLowering();
Jyotsna Vermaf931f692013-05-07 19:53:00 +0000172
Jyotsna Vermaa29a8962013-05-14 18:54:06 +0000173 addPass(createHexagonCopyToCombine());
Jyotsna Vermaf931f692013-05-07 19:53:00 +0000174 if (getOptLevel() != CodeGenOpt::None)
175 addPass(&IfConverterID);
176 if (!TLOF.IsSmallDataEnabled()) {
177 addPass(createHexagonSplitConst32AndConst64(TM));
178 printAndVerify("After hexagon split const32/64 pass");
179 }
180 return true;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000181}
182
Andrew Trick843ee2e2012-02-03 05:12:41 +0000183bool HexagonPassConfig::addPreEmitPass() {
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000184 const HexagonTargetMachine &TM = getHexagonTargetMachine();
185 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Tony Linthicumb4b54152011-12-12 21:14:40 +0000186
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000187 if (!NoOpt)
Jyotsna Verma0f680702013-03-27 11:14:24 +0000188 addPass(createHexagonNewValueJump());
Sirish Pandeb3385702012-05-12 05:10:30 +0000189
Tony Linthicumb4b54152011-12-12 21:14:40 +0000190 // Expand Spill code for predicate registers.
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000191 addPass(createHexagonExpandPredSpillCode(TM));
Tony Linthicumb4b54152011-12-12 21:14:40 +0000192
193 // Split up TFRcondsets into conditional transfers.
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000194 addPass(createHexagonSplitTFRCondSets(TM));
Tony Linthicumb4b54152011-12-12 21:14:40 +0000195
Sirish Pande26f61a12012-05-03 21:52:53 +0000196 // Create Packets.
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000197 if (!NoOpt) {
198 if (!DisableHardwareLoops)
199 addPass(createHexagonFixupHwLoops());
Jyotsna Verma0f680702013-03-27 11:14:24 +0000200 addPass(createHexagonPacketizer());
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000201 }
Sirish Pande26f61a12012-05-03 21:52:53 +0000202
Tony Linthicumb4b54152011-12-12 21:14:40 +0000203 return false;
204}