blob: 7d9d02d49f469f24ebf28016be262fe4cb0ca17e [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 Carruth30d69c22015-02-13 10:01:29 +000020#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Module.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",
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000030 cl::Hidden, cl::desc("Disable Hardware Loops for Hexagon target"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000031
Jyotsna Verma653d8832013-03-27 11:14:24 +000032static cl::opt<bool> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000033 cl::Hidden, cl::ZeroOrMore, cl::init(false),
34 cl::desc("Disable Hexagon CFG Optimization"));
35
36static cl::opt<bool> EnableExpandCondsets("hexagon-expand-condsets",
37 cl::init(true), cl::Hidden, cl::ZeroOrMore,
38 cl::desc("Early expansion of MUX"));
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +000039
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +000040static cl::opt<bool> EnableGenInsert("hexagon-insert", cl::init(true),
41 cl::Hidden, cl::desc("Generate \"insert\" instructions"));
Jyotsna Verma653d8832013-03-27 11:14:24 +000042
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000043static cl::opt<bool> EnableCommGEP("hexagon-commgep", cl::init(true),
44 cl::Hidden, cl::ZeroOrMore, cl::desc("Enable commoning of GEP instructions"));
45
46
Tony Linthicum1213a7a2011-12-12 21:14:40 +000047/// HexagonTargetMachineModule - Note that this is used on hosts that
48/// cannot link in a library unless there are references into the
49/// library. In particular, it seems that it is not possible to get
50/// things to work on Win32 without this. Though it is unused, do not
51/// remove it.
52extern "C" int HexagonTargetMachineModule;
53int HexagonTargetMachineModule = 0;
54
55extern "C" void LLVMInitializeHexagonTarget() {
56 // Register the target.
57 RegisterTargetMachine<HexagonTargetMachine> X(TheHexagonTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000058}
59
Sergei Larin4d8986a2012-09-04 14:49:56 +000060static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
David Blaikie422b93d2014-04-21 20:32:32 +000061 return new VLIWMachineScheduler(C, make_unique<ConvergingVLIWScheduler>());
Sergei Larin4d8986a2012-09-04 14:49:56 +000062}
63
64static MachineSchedRegistry
65SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
66 createVLIWMachineSched);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000067
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000068namespace llvm {
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000069 FunctionPass *createHexagonCommonGEP();
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000070 FunctionPass *createHexagonExpandCondsets();
Colin LeMahieu56efafc2015-06-15 19:05:35 +000071 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
72 CodeGenOpt::Level OptLevel);
73 FunctionPass *createHexagonDelaySlotFillerPass(const TargetMachine &TM);
Colin LeMahieu56efafc2015-06-15 19:05:35 +000074 FunctionPass *createHexagonRemoveExtendArgs(const HexagonTargetMachine &TM);
75 FunctionPass *createHexagonCFGOptimizer();
76
77 FunctionPass *createHexagonSplitConst32AndConst64();
78 FunctionPass *createHexagonExpandPredSpillCode();
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +000079 FunctionPass *createHexagonGenInsert();
Colin LeMahieu56efafc2015-06-15 19:05:35 +000080 FunctionPass *createHexagonHardwareLoops();
81 FunctionPass *createHexagonPeephole();
82 FunctionPass *createHexagonFixupHwLoops();
83 FunctionPass *createHexagonNewValueJump();
84 FunctionPass *createHexagonCopyToCombine();
85 FunctionPass *createHexagonPacketizer();
86 FunctionPass *createHexagonNewValueJump();
Alexander Kornienkof00654e2015-06-23 09:49:53 +000087} // end namespace llvm;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000088
Tony Linthicum1213a7a2011-12-12 21:14:40 +000089/// HexagonTargetMachine ctor - Create an ILP32 architecture model.
90///
91
92/// Hexagon_TODO: Do I need an aggregate alignment?
93///
Daniel Sanders3e5de882015-06-11 19:41:26 +000094HexagonTargetMachine::HexagonTargetMachine(const Target &T, const Triple &TT,
Tony Linthicum1213a7a2011-12-12 21:14:40 +000095 StringRef CPU, StringRef FS,
Craig Topperb5454082012-03-17 09:24:09 +000096 const TargetOptions &Options,
Eric Christopher0d0b3602014-06-27 00:13:43 +000097 Reloc::Model RM, CodeModel::Model CM,
Tony Linthicum1213a7a2011-12-12 21:14:40 +000098 CodeGenOpt::Level OL)
Mehdi Amini93e1ea12015-03-12 00:07:24 +000099 : LLVMTargetMachine(T, "e-m:e-p:32:32-i1:32-i64:64-a:0-n32", TT, CPU, FS,
100 Options, RM, CM, OL),
Aditya Nandakumara2719322014-11-13 09:26:31 +0000101 TLOF(make_unique<HexagonTargetObjectFile>()),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000102 Subtarget(TT, CPU, FS, *this) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000103 initAsmInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000104}
105
Reid Kleckner357600e2014-11-20 23:37:18 +0000106HexagonTargetMachine::~HexagonTargetMachine() {}
107
Andrew Trickccb67362012-02-03 05:12:41 +0000108namespace {
109/// Hexagon Code Generator Pass Configuration Options.
110class HexagonPassConfig : public TargetPassConfig {
111public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000112 HexagonPassConfig(HexagonTargetMachine *TM, PassManagerBase &PM)
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000113 : TargetPassConfig(TM, PM) {
114 bool NoOpt = (TM->getOptLevel() == CodeGenOpt::None);
115 if (!NoOpt) {
116 if (EnableExpandCondsets) {
117 Pass *Exp = createHexagonExpandCondsets();
118 insertPass(&RegisterCoalescerID, IdentifyingPassPtr(Exp));
119 }
120 }
121 }
Andrew Trickccb67362012-02-03 05:12:41 +0000122
123 HexagonTargetMachine &getHexagonTargetMachine() const {
124 return getTM<HexagonTargetMachine>();
125 }
126
Craig Topper906c2cd2014-04-29 07:58:16 +0000127 ScheduleDAGInstrs *
128 createMachineScheduler(MachineSchedContext *C) const override {
Andrew Trick978674b2013-09-20 05:14:41 +0000129 return createVLIWMachineSched(C);
130 }
131
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000132 void addIRPasses() override;
Craig Topper906c2cd2014-04-29 07:58:16 +0000133 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000134 void addPreRegAlloc() override;
135 void addPostRegAlloc() override;
136 void addPreSched2() override;
137 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000138};
139} // namespace
140
Andrew Trickf8ea1082012-02-04 02:56:59 +0000141TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
142 return new HexagonPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000143}
144
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000145void HexagonPassConfig::addIRPasses() {
146 TargetPassConfig::addIRPasses();
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000147 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +0000148
149 addPass(createAtomicExpandPass(TM));
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000150 if (!NoOpt && EnableCommGEP)
151 addPass(createHexagonCommonGEP());
152}
153
Andrew Trickccb67362012-02-03 05:12:41 +0000154bool HexagonPassConfig::addInstSelector() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000155 HexagonTargetMachine &TM = getHexagonTargetMachine();
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000156 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Jyotsna Verma653d8832013-03-27 11:14:24 +0000157
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000158 if (!NoOpt)
159 addPass(createHexagonRemoveExtendArgs(TM));
Jyotsna Verma653d8832013-03-27 11:14:24 +0000160
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000161 addPass(createHexagonISelDag(TM, getOptLevel()));
Jyotsna Verma653d8832013-03-27 11:14:24 +0000162
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000163 if (!NoOpt) {
Jyotsna Verma653d8832013-03-27 11:14:24 +0000164 addPass(createHexagonPeephole());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000165 printAndVerify("After hexagon peephole pass");
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +0000166 if (EnableGenInsert)
167 addPass(createHexagonGenInsert(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000168 }
Jyotsna Verma653d8832013-03-27 11:14:24 +0000169
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000170 return false;
171}
172
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000173void HexagonPassConfig::addPreRegAlloc() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000174 if (getOptLevel() != CodeGenOpt::None)
175 if (!DisableHardwareLoops)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000176 addPass(createHexagonHardwareLoops(), false);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000177}
178
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000179void HexagonPassConfig::addPostRegAlloc() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000180 if (getOptLevel() != CodeGenOpt::None)
181 if (!DisableHexagonCFGOpt)
Eric Christopher5c3376a2015-02-02 18:46:27 +0000182 addPass(createHexagonCFGOptimizer(), false);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000183}
184
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000185void HexagonPassConfig::addPreSched2() {
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000186 addPass(createHexagonCopyToCombine(), false);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000187 if (getOptLevel() != CodeGenOpt::None)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000188 addPass(&IfConverterID, false);
Eric Christopher01f875e2015-02-02 22:11:43 +0000189 addPass(createHexagonSplitConst32AndConst64());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000190}
191
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000192void HexagonPassConfig::addPreEmitPass() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000193 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000194
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000195 if (!NoOpt)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000196 addPass(createHexagonNewValueJump(), false);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000197
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000198 // Expand Spill code for predicate registers.
Eric Christopher6ff7ed62015-02-02 18:46:31 +0000199 addPass(createHexagonExpandPredSpillCode(), false);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000200
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000201 // Create Packets.
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000202 if (!NoOpt) {
203 if (!DisableHardwareLoops)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000204 addPass(createHexagonFixupHwLoops(), false);
205 addPass(createHexagonPacketizer(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000206 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000207}