LLVM  5.0.0
HexagonTargetMachine.cpp
Go to the documentation of this file.
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"
20 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/IR/Module.h"
27 #include "llvm/Transforms/Scalar.h"
28 
29 using namespace llvm;
30 
32  cl::init(true), cl::desc("Enable RDF-based optimizations"));
33 
34 static cl::opt<bool> DisableHardwareLoops("disable-hexagon-hwloops",
35  cl::Hidden, cl::desc("Disable Hardware Loops for Hexagon target"));
36 
37 static cl::opt<bool> DisableAModeOpt("disable-hexagon-amodeopt",
39  cl::desc("Disable Hexagon Addressing Mode Optimization"));
40 
41 static cl::opt<bool> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
43  cl::desc("Disable Hexagon CFG Optimization"));
44 
45 static cl::opt<bool> DisableHCP("disable-hcp", cl::init(false), cl::Hidden,
46  cl::ZeroOrMore, cl::desc("Disable Hexagon constant propagation"));
47 
48 static cl::opt<bool> DisableStoreWidening("disable-store-widen",
49  cl::Hidden, cl::init(false), cl::desc("Disable store widening"));
50 
51 static cl::opt<bool> EnableExpandCondsets("hexagon-expand-condsets",
53  cl::desc("Early expansion of MUX"));
54 
55 static cl::opt<bool> EnableEarlyIf("hexagon-eif", cl::init(true), cl::Hidden,
56  cl::ZeroOrMore, cl::desc("Enable early if-conversion"));
57 
58 static cl::opt<bool> EnableGenInsert("hexagon-insert", cl::init(true),
59  cl::Hidden, cl::desc("Generate \"insert\" instructions"));
60 
61 static cl::opt<bool> EnableCommGEP("hexagon-commgep", cl::init(true),
62  cl::Hidden, cl::ZeroOrMore, cl::desc("Enable commoning of GEP instructions"));
63 
64 static cl::opt<bool> EnableGenExtract("hexagon-extract", cl::init(true),
65  cl::Hidden, cl::desc("Generate \"extract\" instructions"));
66 
67 static cl::opt<bool> EnableGenMux("hexagon-mux", cl::init(true), cl::Hidden,
68  cl::desc("Enable converting conditional transfers into MUX instructions"));
69 
70 static cl::opt<bool> EnableGenPred("hexagon-gen-pred", cl::init(true),
71  cl::Hidden, cl::desc("Enable conversion of arithmetic operations to "
72  "predicate instructions"));
73 
74 static cl::opt<bool> EnableLoopPrefetch("hexagon-loop-prefetch",
76  cl::desc("Enable loop data prefetch on Hexagon"));
77 
78 static cl::opt<bool> DisableHSDR("disable-hsdr", cl::init(false), cl::Hidden,
79  cl::desc("Disable splitting double registers"));
80 
81 static cl::opt<bool> EnableBitSimplify("hexagon-bit", cl::init(true),
82  cl::Hidden, cl::desc("Bit simplification"));
83 
84 static cl::opt<bool> EnableLoopResched("hexagon-loop-resched", cl::init(true),
85  cl::Hidden, cl::desc("Loop rescheduling"));
86 
87 static cl::opt<bool> HexagonNoOpt("hexagon-noopt", cl::init(false),
88  cl::Hidden, cl::desc("Disable backend optimizations"));
89 
90 static cl::opt<bool> EnableVectorPrint("enable-hexagon-vector-print",
92  cl::desc("Enable Hexagon Vector print instr pass"));
93 
94 /// HexagonTargetMachineModule - Note that this is used on hosts that
95 /// cannot link in a library unless there are references into the
96 /// library. In particular, it seems that it is not possible to get
97 /// things to work on Win32 without this. Though it is unused, do not
98 /// remove it.
101 
103  return new VLIWMachineScheduler(C, make_unique<ConvergingVLIWScheduler>());
104 }
105 
107 SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
109 
110 namespace llvm {
111  extern char &HexagonExpandCondsetsID;
119 
135  CodeGenOpt::Level OptLevel);
147 } // end namespace llvm;
148 
150  if (!RM.hasValue())
151  return Reloc::Static;
152  return *RM;
153 }
154 
155 extern "C" void LLVMInitializeHexagonTarget() {
156  // Register the target.
158 
165 }
166 
168  StringRef CPU, StringRef FS,
169  const TargetOptions &Options,
171  CodeModel::Model CM,
173  // Specify the vector alignment explicitly. For v512x1, the calculated
174  // alignment would be 512*alignment(i1), which is 512 bytes, instead of
175  // the required minimum of 64 bytes.
177  T, "e-m:e-p:32:32:32-a:0-n16:32-"
178  "i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-"
179  "v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048",
180  TT, CPU, FS, Options, getEffectiveRelocModel(RM), CM,
181  (HexagonNoOpt ? CodeGenOpt::None : OL)),
184  initAsmInfo();
185 }
186 
187 const HexagonSubtarget *
189  AttributeList FnAttrs = F.getAttributes();
190  Attribute CPUAttr =
191  FnAttrs.getAttribute(AttributeList::FunctionIndex, "target-cpu");
192  Attribute FSAttr =
193  FnAttrs.getAttribute(AttributeList::FunctionIndex, "target-features");
194 
195  std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
196  ? CPUAttr.getValueAsString().str()
197  : TargetCPU;
198  std::string FS = !FSAttr.hasAttribute(Attribute::None)
199  ? FSAttr.getValueAsString().str()
200  : TargetFS;
201 
202  auto &I = SubtargetMap[CPU + FS];
203  if (!I) {
204  // This needs to be done before we create a new subtarget since any
205  // creation will depend on the TM and the code generation flags on the
206  // function that reside in TargetOptions.
208  I = llvm::make_unique<HexagonSubtarget>(TargetTriple, CPU, FS, *this);
209  }
210  return I.get();
211 }
212 
214  PMB.addExtension(
216  [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
218  });
219 }
220 
222  return TargetIRAnalysis([this](const Function &F) {
223  return TargetTransformInfo(HexagonTTIImpl(this, F));
224  });
225 }
226 
227 
229 
230 namespace {
231 /// Hexagon Code Generator Pass Configuration Options.
232 class HexagonPassConfig : public TargetPassConfig {
233 public:
234  HexagonPassConfig(HexagonTargetMachine &TM, PassManagerBase &PM)
235  : TargetPassConfig(TM, PM) {}
236 
237  HexagonTargetMachine &getHexagonTargetMachine() const {
238  return getTM<HexagonTargetMachine>();
239  }
240 
242  createMachineScheduler(MachineSchedContext *C) const override {
243  return createVLIWMachineSched(C);
244  }
245 
246  void addIRPasses() override;
247  bool addInstSelector() override;
248  void addPreRegAlloc() override;
249  void addPostRegAlloc() override;
250  void addPreSched2() override;
251  void addPreEmitPass() override;
252 };
253 } // namespace
254 
256  return new HexagonPassConfig(*this, PM);
257 }
258 
259 void HexagonPassConfig::addIRPasses() {
261  bool NoOpt = (getOptLevel() == CodeGenOpt::None);
262 
263  addPass(createAtomicExpandPass());
264  if (!NoOpt) {
265  if (EnableLoopPrefetch)
266  addPass(createLoopDataPrefetchPass());
267  if (EnableCommGEP)
268  addPass(createHexagonCommonGEP());
269  // Replace certain combinations of shifts and ands with extracts.
270  if (EnableGenExtract)
271  addPass(createHexagonGenExtract());
272  }
273 }
274 
275 bool HexagonPassConfig::addInstSelector() {
276  HexagonTargetMachine &TM = getHexagonTargetMachine();
277  bool NoOpt = (getOptLevel() == CodeGenOpt::None);
278 
279  if (!NoOpt)
281 
282  addPass(createHexagonISelDag(TM, getOptLevel()));
283 
284  if (!NoOpt) {
285  // Create logical operations on predicate registers.
286  if (EnableGenPred)
287  addPass(createHexagonGenPredicate());
288  // Rotate loops to expose bit-simplification opportunities.
289  if (EnableLoopResched)
291  // Split double registers.
292  if (!DisableHSDR)
293  addPass(createHexagonSplitDoubleRegs());
294  // Bit simplification.
295  if (EnableBitSimplify)
296  addPass(createHexagonBitSimplify());
297  addPass(createHexagonPeephole());
298  // Constant propagation.
299  if (!DisableHCP) {
302  }
303  if (EnableGenInsert)
304  addPass(createHexagonGenInsert());
305  if (EnableEarlyIf)
307  }
308 
309  return false;
310 }
311 
312 void HexagonPassConfig::addPreRegAlloc() {
313  if (getOptLevel() != CodeGenOpt::None) {
317  addPass(createHexagonStoreWidening());
319  addPass(createHexagonHardwareLoops());
320  }
321  if (TM->getOptLevel() >= CodeGenOpt::Default)
322  addPass(&MachinePipelinerID);
323 }
324 
325 void HexagonPassConfig::addPostRegAlloc() {
326  if (getOptLevel() != CodeGenOpt::None) {
327  if (EnableRDFOpt)
328  addPass(createHexagonRDFOpt());
330  addPass(createHexagonCFGOptimizer());
331  if (!DisableAModeOpt)
332  addPass(createHexagonOptAddrMode());
333  }
334 }
335 
336 void HexagonPassConfig::addPreSched2() {
337  addPass(createHexagonCopyToCombine());
338  if (getOptLevel() != CodeGenOpt::None)
339  addPass(&IfConverterID);
341 }
342 
343 void HexagonPassConfig::addPreEmitPass() {
344  bool NoOpt = (getOptLevel() == CodeGenOpt::None);
345 
346  if (!NoOpt)
347  addPass(createHexagonNewValueJump());
348 
350 
351  // Create Packets.
352  if (!NoOpt) {
354  addPass(createHexagonFixupHwLoops());
355  // Generate MUX from pairs of conditional transfers.
356  if (EnableGenMux)
357  addPass(createHexagonGenMux());
358 
359  addPass(createHexagonPacketizer(), false);
360  }
361  if (EnableVectorPrint)
362  addPass(createHexagonVectorPrint(), false);
363 
364  // Add CFI instructions if necessary.
365  addPass(createHexagonCallFrameInformation(), false);
366 }
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:81
const NoneType None
Definition: None.h:24
FunctionPass * createHexagonCopyToCombine()
char & HexagonExpandCondsetsID
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PassManagerBuilder - This class is used to set up a standard optimization sequence for languages like...
bool hasValue() const
Definition: Optional.h:133
Extend the standard ScheduleDAGMI to provide more context and override the top-level schedule() drive...
virtual void addIRPasses()
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
FunctionPass * createHexagonVectorPrint()
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with...
Definition: TargetMachine.h:75
static cl::opt< bool > DisableStoreWidening("disable-store-widen", cl::Hidden, cl::init(false), cl::desc("Disable store widening"))
Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return the attribute object that exists at the given index.
static cl::opt< bool > EnableLoopPrefetch("hexagon-loop-prefetch", cl::init(false), cl::Hidden, cl::ZeroOrMore, cl::desc("Enable loop data prefetch on Hexagon"))
char & RegisterCoalescerID
RegisterCoalescer - This pass merges live ranges to eliminate copies.
Analysis pass providing the TargetTransformInfo.
static cl::opt< bool > DisableHCP("disable-hcp", cl::init(false), cl::Hidden, cl::ZeroOrMore, cl::desc("Disable Hexagon constant propagation"))
virtual void add(Pass *P)=0
Add a pass to the queue of passes to run.
static MachineSchedRegistry SchedCustomRegistry("hexagon","Run Hexagon's custom scheduler", createVLIWMachineSched)
FunctionPass * createHexagonHardwareLoops()
MachineSchedRegistry provides a selection of available machine instruction schedulers.
bool hasAttribute(AttrKind Val) const
Return true if the attribute is present.
Definition: Attributes.cpp:202
FunctionPass * createHexagonGenPredicate()
FunctionPass * createLoopDataPrefetchPass()
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:197
FunctionPass * createHexagonCommonGEP()
static cl::opt< bool > EnableRDFOpt("rdf-opt", cl::Hidden, cl::ZeroOrMore, cl::init(true), cl::desc("Enable RDF-based optimizations"))
void initializeHexagonGenMuxPass(PassRegistry &Registry)
FunctionPass * createHexagonOptimizeSZextends()
static cl::opt< bool > DisableHardwareLoops("disable-hexagon-hwloops", cl::Hidden, cl::desc("Disable Hardware Loops for Hexagon target"))
No attributes have been set.
Definition: Attributes.h:72
Target-Independent Code Generator Pass Configuration Options.
char & UnreachableMachineBlockElimID
UnreachableMachineBlockElimination - This pass removes unreachable machine basic blocks.
#define F(x, y, z)
Definition: MD5.cpp:55
void initializeHexagonNewValueJumpPass(PassRegistry &)
void initializeHexagonPacketizerPass(PassRegistry &)
FunctionPass * createHexagonBitSimplify()
static cl::opt< bool > EnableGenPred("hexagon-gen-pred", cl::init(true), cl::Hidden, cl::desc("Enable conversion of arithmetic operations to ""predicate instructions"))
static cl::opt< bool > EnableExpandCondsets("hexagon-expand-condsets", cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("Early expansion of MUX"))
static cl::opt< bool > EnableEarlyIf("hexagon-eif", cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("Enable early if-conversion"))
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
HexagonTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, CodeModel::Model CM, CodeGenOpt::Level OL)
int HexagonTargetMachineModule
HexagonTargetMachineModule - Note that this is used on hosts that cannot link in a library unless the...
FunctionPass * createHexagonConstPropagationPass()
This file implements a TargetTransformInfo analysis pass specific to the Hexagon target machine...
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
static cl::opt< bool > EnableVectorPrint("enable-hexagon-vector-print", cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::desc("Enable Hexagon Vector print instr pass"))
void initializeHexagonLoopIdiomRecognizePass(PassRegistry &)
FunctionPass * createHexagonEarlyIfConversion()
static ScheduleDAGInstrs * createVLIWMachineSched(MachineSchedContext *C)
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
Definition: STLExtras.h:943
FunctionPass * createHexagonPacketizer()
static Reloc::Model getEffectiveRelocModel(Optional< Reloc::Model > RM)
static cl::opt< bool > EnableCommGEP("hexagon-commgep", cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("Enable commoning of GEP instructions"))
static cl::opt< bool > EnableGenMux("hexagon-mux", cl::init(true), cl::Hidden, cl::desc("Enable converting conditional transfers into MUX instructions"))
static cl::opt< bool > EnableBitSimplify("hexagon-bit", cl::init(true), cl::Hidden, cl::desc("Bit simplification"))
static cl::opt< bool > DisableHSDR("disable-hsdr", cl::init(false), cl::Hidden, cl::desc("Disable splitting double registers"))
static cl::opt< bool > DisableAModeOpt("disable-hexagon-amodeopt", cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::desc("Disable Hexagon Addressing Mode Optimization"))
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This class describes a target machine that is implemented with the LLVM target-independent code gener...
static cl::opt< bool > DisableHexagonCFGOpt("disable-hexagon-cfgopt", cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::desc("Disable Hexagon CFG Optimization"))
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
FunctionPass * createHexagonSplitDoubleRegs()
FunctionPass * createHexagonLoopRescheduling()
TargetIRAnalysis getTargetIRAnalysis() override
Get a TargetIRAnalysis implementation for the target.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
FunctionPass * createHexagonStoreWidening()
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
FunctionPass * createHexagonBranchRelaxation()
FunctionPass * createHexagonNewValueJump()
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
FunctionPass * createHexagonRDFOpt()
Module.h This file contains the declarations for the Module class.
FunctionPass * createHexagonGenMux()
FunctionPass * createHexagonPeephole()
char & MachinePipelinerID
This pass performs software pipelining on machine instructions.
const HexagonSubtarget * getSubtargetImpl(const Function &F) const override
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...
FunctionPass * createHexagonGenInsert()
Target - Wrapper for Target specific information.
static cl::opt< bool > HexagonNoOpt("hexagon-noopt", cl::init(false), cl::Hidden, cl::desc("Disable backend optimizations"))
FunctionPass * createHexagonCallFrameInformation()
std::string TargetCPU
Definition: TargetMachine.h:76
void initializeHexagonOptAddrModePass(PassRegistry &)
A ScheduleDAG for scheduling lists of MachineInstr.
FunctionPass * createHexagonSplitConst32AndConst64()
#define C
Definition: LargeTest.cpp:25
FunctionPass * createHexagonFixupHwLoops()
char & IfConverterID
IfConverter - This pass performs machine code if conversion.
MachineSchedContext provides enough context from the MachineScheduler pass for the target to instanti...
#define I(x, y, z)
Definition: MD5.cpp:58
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
Pass * createHexagonLoopIdiomPass()
void resetTargetOptions(const Function &F) const
Reset the target options based on the function's attributes.
FunctionPass * createHexagonISelDag(HexagonTargetMachine &TM, CodeGenOpt::Level OptLevel)
std::string TargetFS
Definition: TargetMachine.h:77
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:195
RegisterTargetMachine - Helper template for registering a target machine implementation, for use in the target machine initialization function.
FunctionPass * createHexagonGenExtract()
FunctionPass * createHexagonOptAddrMode()
static cl::opt< bool > EnableLoopResched("hexagon-loop-resched", cl::init(true), cl::Hidden, cl::desc("Loop rescheduling"))
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:40
void adjustPassManager(PassManagerBuilder &PMB) override
Allow the target to modify the pass manager, e.g.
static cl::opt< bool > EnableGenExtract("hexagon-extract", cl::init(true), cl::Hidden, cl::desc("Generate \"extract\" instructions"))
void addExtension(ExtensionPointTy Ty, ExtensionFn Fn)
EP_LateLoopOptimizations - This extension point allows adding late loop canonicalization and simplifi...
FunctionPass * createAtomicExpandPass()
void initializeHexagonExpandCondsetsPass(PassRegistry &)
static cl::opt< bool > EnableGenInsert("hexagon-insert", cl::init(true), cl::Hidden, cl::desc("Generate \"insert\" instructions"))
void LLVMInitializeHexagonTarget()
Target & getTheHexagonTarget()
FunctionPass * createHexagonCFGOptimizer()