blob: caa1ba49649fc8f0b37b5575966019ed66edfbd4 [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"
Tony Linthicumb4b54152011-12-12 21:14:40 +000018#include "llvm/CodeGen/Passes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/Module.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000020#include "llvm/PassManager.h"
Benjamin Kramerf3fd7ee2012-02-06 10:19:29 +000021#include "llvm/Support/CommandLine.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000022#include "llvm/Support/TargetRegistry.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Transforms/IPO/PassManagerBuilder.h"
24#include "llvm/Transforms/Scalar.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000025
Tony Linthicumb4b54152011-12-12 21:14:40 +000026using namespace llvm;
27
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +000028static cl:: opt<bool> DisableHardwareLoops("disable-hexagon-hwloops",
29 cl::Hidden, cl::desc("Disable Hardware Loops for Hexagon target"));
Tony Linthicumb4b54152011-12-12 21:14:40 +000030
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +000031static cl::opt<bool> DisableHexagonMISched("disable-hexagon-misched",
32 cl::Hidden, cl::ZeroOrMore, cl::init(false),
33 cl::desc("Disable Hexagon MI Scheduling"));
Sergei Larin3e590402012-09-04 14:49:56 +000034
Jyotsna Verma0f680702013-03-27 11:14:24 +000035static cl::opt<bool> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +000036 cl::Hidden, cl::ZeroOrMore, cl::init(false),
37 cl::desc("Disable Hexagon CFG Optimization"));
38
Jyotsna Verma0f680702013-03-27 11:14:24 +000039
Tony Linthicumb4b54152011-12-12 21:14:40 +000040/// HexagonTargetMachineModule - Note that this is used on hosts that
41/// cannot link in a library unless there are references into the
42/// library. In particular, it seems that it is not possible to get
43/// things to work on Win32 without this. Though it is unused, do not
44/// remove it.
45extern "C" int HexagonTargetMachineModule;
46int HexagonTargetMachineModule = 0;
47
48extern "C" void LLVMInitializeHexagonTarget() {
49 // Register the target.
50 RegisterTargetMachine<HexagonTargetMachine> X(TheHexagonTarget);
Tony Linthicumb4b54152011-12-12 21:14:40 +000051}
52
Sergei Larin3e590402012-09-04 14:49:56 +000053static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
54 return new VLIWMachineScheduler(C, new ConvergingVLIWScheduler());
55}
56
57static MachineSchedRegistry
58SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
59 createVLIWMachineSched);
Tony Linthicumb4b54152011-12-12 21:14:40 +000060
61/// HexagonTargetMachine ctor - Create an ILP32 architecture model.
62///
63
64/// Hexagon_TODO: Do I need an aggregate alignment?
65///
66HexagonTargetMachine::HexagonTargetMachine(const Target &T, StringRef TT,
67 StringRef CPU, StringRef FS,
Craig Topper1e0c9ab2012-03-17 09:24:09 +000068 const TargetOptions &Options,
Tony Linthicumb4b54152011-12-12 21:14:40 +000069 Reloc::Model RM,
70 CodeModel::Model CM,
71 CodeGenOpt::Level OL)
72 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Micah Villmow3574eca2012-10-08 16:38:25 +000073 DL("e-p:32:32:32-"
Sirish Pande7517bbc2012-05-10 20:20:25 +000074 "i64:64:64-i32:32:32-i16:16:16-i1:32:32-"
75 "f64:64:64-f32:32:32-a0:0-n32") ,
Benjamin Kramer90345622011-12-16 19:08:59 +000076 Subtarget(TT, CPU, FS), InstrInfo(Subtarget), TLInfo(*this),
Tony Linthicumb4b54152011-12-12 21:14:40 +000077 TSInfo(*this),
78 FrameLowering(Subtarget),
Chandler Carruthaeef83c2013-01-07 01:37:14 +000079 InstrItins(&Subtarget.getInstrItineraryData()) {
Jyotsna Verma0f680702013-03-27 11:14:24 +000080 setMCUseCFI(false);
Tony Linthicumb4b54152011-12-12 21:14:40 +000081}
82
83// addPassesForOptimizations - Allow the backend (target) to add Target
84// Independent Optimization passes to the Pass Manager.
85bool HexagonTargetMachine::addPassesForOptimizations(PassManagerBase &PM) {
Jyotsna Verma0f680702013-03-27 11:14:24 +000086 if (getOptLevel() != CodeGenOpt::None) {
87 PM.add(createConstantPropagationPass());
88 PM.add(createLoopSimplifyPass());
89 PM.add(createDeadCodeEliminationPass());
90 PM.add(createConstantPropagationPass());
91 PM.add(createLoopUnrollPass());
92 PM.add(createLoopStrengthReducePass());
93 }
Tony Linthicumb4b54152011-12-12 21:14:40 +000094 return true;
95}
96
Andrew Trick843ee2e2012-02-03 05:12:41 +000097namespace {
98/// Hexagon Code Generator Pass Configuration Options.
99class HexagonPassConfig : public TargetPassConfig {
100public:
Andrew Trick061efcf2012-02-04 02:56:59 +0000101 HexagonPassConfig(HexagonTargetMachine *TM, PassManagerBase &PM)
Sergei Larin3e590402012-09-04 14:49:56 +0000102 : TargetPassConfig(TM, PM) {
103 // Enable MI scheduler.
104 if (!DisableHexagonMISched) {
105 enablePass(&MachineSchedulerID);
106 MachineSchedRegistry::setDefault(createVLIWMachineSched);
107 }
108 }
Andrew Trick843ee2e2012-02-03 05:12:41 +0000109
110 HexagonTargetMachine &getHexagonTargetMachine() const {
111 return getTM<HexagonTargetMachine>();
112 }
113
114 virtual bool addInstSelector();
115 virtual bool addPreRegAlloc();
116 virtual bool addPostRegAlloc();
117 virtual bool addPreSched2();
118 virtual bool addPreEmitPass();
119};
120} // namespace
121
Andrew Trick061efcf2012-02-04 02:56:59 +0000122TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
123 return new HexagonPassConfig(this, PM);
Andrew Trick843ee2e2012-02-03 05:12:41 +0000124}
125
126bool HexagonPassConfig::addInstSelector() {
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000127 const HexagonTargetMachine &TM = getHexagonTargetMachine();
128 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Jyotsna Verma0f680702013-03-27 11:14:24 +0000129
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000130 if (!NoOpt)
131 addPass(createHexagonRemoveExtendArgs(TM));
Jyotsna Verma0f680702013-03-27 11:14:24 +0000132
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000133 addPass(createHexagonISelDag(TM, getOptLevel()));
Jyotsna Verma0f680702013-03-27 11:14:24 +0000134
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000135 if (!NoOpt) {
Jyotsna Verma0f680702013-03-27 11:14:24 +0000136 addPass(createHexagonPeephole());
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000137 printAndVerify("After hexagon peephole pass");
138 }
Jyotsna Verma0f680702013-03-27 11:14:24 +0000139
Tony Linthicumb4b54152011-12-12 21:14:40 +0000140 return false;
141}
142
Andrew Trick843ee2e2012-02-03 05:12:41 +0000143bool HexagonPassConfig::addPreRegAlloc() {
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000144 if (getOptLevel() != CodeGenOpt::None)
145 if (!DisableHardwareLoops)
146 addPass(createHexagonHardwareLoops());
Tony Linthicumb4b54152011-12-12 21:14:40 +0000147 return false;
148}
149
Andrew Trick843ee2e2012-02-03 05:12:41 +0000150bool HexagonPassConfig::addPostRegAlloc() {
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000151 const HexagonTargetMachine &TM = getHexagonTargetMachine();
152 if (getOptLevel() != CodeGenOpt::None)
153 if (!DisableHexagonCFGOpt)
154 addPass(createHexagonCFGOptimizer(TM));
155 return false;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000156}
157
Andrew Trick843ee2e2012-02-03 05:12:41 +0000158bool HexagonPassConfig::addPreSched2() {
Jyotsna Verma0f680702013-03-27 11:14:24 +0000159 if (getOptLevel() != CodeGenOpt::None)
160 addPass(&IfConverterID);
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000161 return false;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000162}
163
Andrew Trick843ee2e2012-02-03 05:12:41 +0000164bool HexagonPassConfig::addPreEmitPass() {
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000165 const HexagonTargetMachine &TM = getHexagonTargetMachine();
166 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Tony Linthicumb4b54152011-12-12 21:14:40 +0000167
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000168 if (!NoOpt)
Jyotsna Verma0f680702013-03-27 11:14:24 +0000169 addPass(createHexagonNewValueJump());
Sirish Pandeb3385702012-05-12 05:10:30 +0000170
Tony Linthicumb4b54152011-12-12 21:14:40 +0000171 // Expand Spill code for predicate registers.
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000172 addPass(createHexagonExpandPredSpillCode(TM));
Tony Linthicumb4b54152011-12-12 21:14:40 +0000173
174 // Split up TFRcondsets into conditional transfers.
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000175 addPass(createHexagonSplitTFRCondSets(TM));
Tony Linthicumb4b54152011-12-12 21:14:40 +0000176
Sirish Pande26f61a12012-05-03 21:52:53 +0000177 // Create Packets.
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000178 if (!NoOpt) {
179 if (!DisableHardwareLoops)
180 addPass(createHexagonFixupHwLoops());
Jyotsna Verma0f680702013-03-27 11:14:24 +0000181 addPass(createHexagonPacketizer());
Krzysztof Parzyszekb0720902013-05-06 21:25:45 +0000182 }
Sirish Pande26f61a12012-05-03 21:52:53 +0000183
Tony Linthicumb4b54152011-12-12 21:14:40 +0000184 return false;
185}