LLVM  6.0.0svn
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 Hexagon constant-extender optimization"));
33 
35  cl::init(true), cl::desc("Enable RDF-based optimizations"));
36 
37 static cl::opt<bool> DisableHardwareLoops("disable-hexagon-hwloops",
38  cl::Hidden, cl::desc("Disable Hardware Loops for Hexagon target"));
39 
40 static cl::opt<bool> DisableAModeOpt("disable-hexagon-amodeopt",
42  cl::desc("Disable Hexagon Addressing Mode Optimization"));
43 
44 static cl::opt<bool> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
46  cl::desc("Disable Hexagon CFG Optimization"));
47 
48 static cl::opt<bool> DisableHCP("disable-hcp", cl::init(false), cl::Hidden,
49  cl::ZeroOrMore, cl::desc("Disable Hexagon constant propagation"));
50 
51 static cl::opt<bool> DisableStoreWidening("disable-store-widen",
52  cl::Hidden, cl::init(false), cl::desc("Disable store widening"));
53 
54 static cl::opt<bool> EnableExpandCondsets("hexagon-expand-condsets",
56  cl::desc("Early expansion of MUX"));
57 
58 static cl::opt<bool> EnableEarlyIf("hexagon-eif", cl::init(true), cl::Hidden,
59  cl::ZeroOrMore, cl::desc("Enable early if-conversion"));
60 
61 static cl::opt<bool> EnableGenInsert("hexagon-insert", cl::init(true),
62  cl::Hidden, cl::desc("Generate \"insert\" instructions"));
63 
64 static cl::opt<bool> EnableCommGEP("hexagon-commgep", cl::init(true),
65  cl::Hidden, cl::ZeroOrMore, cl::desc("Enable commoning of GEP instructions"));
66 
67 static cl::opt<bool> EnableGenExtract("hexagon-extract", cl::init(true),
68  cl::Hidden, cl::desc("Generate \"extract\" instructions"));
69 
70 static cl::opt<bool> EnableGenMux("hexagon-mux", cl::init(true), cl::Hidden,
71  cl::desc("Enable converting conditional transfers into MUX instructions"));
72 
73 static cl::opt<bool> EnableGenPred("hexagon-gen-pred", cl::init(true),
74  cl::Hidden, cl::desc("Enable conversion of arithmetic operations to "
75  "predicate instructions"));
76 
77 static cl::opt<bool> EnableLoopPrefetch("hexagon-loop-prefetch",
79  cl::desc("Enable loop data prefetch on Hexagon"));
80 
81 static cl::opt<bool> DisableHSDR("disable-hsdr", cl::init(false), cl::Hidden,
82  cl::desc("Disable splitting double registers"));
83 
84 static cl::opt<bool> EnableBitSimplify("hexagon-bit", cl::init(true),
85  cl::Hidden, cl::desc("Bit simplification"));
86 
87 static cl::opt<bool> EnableLoopResched("hexagon-loop-resched", cl::init(true),
88  cl::Hidden, cl::desc("Loop rescheduling"));
89 
90 static cl::opt<bool> HexagonNoOpt("hexagon-noopt", cl::init(false),
91  cl::Hidden, cl::desc("Disable backend optimizations"));
92 
93 static cl::opt<bool> EnableVectorPrint("enable-hexagon-vector-print",
95  cl::desc("Enable Hexagon Vector print instr pass"));
96 
97 static cl::opt<bool> EnableTrapUnreachable("hexagon-trap-unreachable",
99  cl::desc("Enable generating trap for unreachable"));
100 
101 /// HexagonTargetMachineModule - Note that this is used on hosts that
102 /// cannot link in a library unless there are references into the
103 /// library. In particular, it seems that it is not possible to get
104 /// things to work on Win32 without this. Though it is unused, do not
105 /// remove it.
107 int HexagonTargetMachineModule = 0;
108 
110  ScheduleDAGMILive *DAG =
111  new VLIWMachineScheduler(C, make_unique<ConvergingVLIWScheduler>());
112  DAG->addMutation(make_unique<HexagonSubtarget::UsrOverflowMutation>());
113  DAG->addMutation(make_unique<HexagonSubtarget::HVXMemLatencyMutation>());
114  DAG->addMutation(make_unique<HexagonSubtarget::CallMutation>());
116  return DAG;
117 }
118 
120 SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
122 
123 namespace llvm {
124  extern char &HexagonExpandCondsetsID;
138 
155  CodeGenOpt::Level OptLevel);
167 } // end namespace llvm;
168 
170  if (!RM.hasValue())
171  return Reloc::Static;
172  return *RM;
173 }
174 
176  if (CM)
177  return *CM;
178  return CodeModel::Small;
179 }
180 
181 extern "C" void LLVMInitializeHexagonTarget() {
182  // Register the target.
184 
196 }
197 
199  StringRef CPU, StringRef FS,
200  const TargetOptions &Options,
203  CodeGenOpt::Level OL, bool JIT)
204  // Specify the vector alignment explicitly. For v512x1, the calculated
205  // alignment would be 512*alignment(i1), which is 512 bytes, instead of
206  // the required minimum of 64 bytes.
208  T,
209  "e-m:e-p:32:32:32-a:0-n16:32-"
210  "i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-"
211  "v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048",
212  TT, CPU, FS, Options, getEffectiveRelocModel(RM),
213  getEffectiveCodeModel(CM), (HexagonNoOpt ? CodeGenOpt::None : OL)),
216  this->Options.TrapUnreachable = true;
218  initAsmInfo();
219 }
220 
221 const HexagonSubtarget *
223  AttributeList FnAttrs = F.getAttributes();
224  Attribute CPUAttr =
225  FnAttrs.getAttribute(AttributeList::FunctionIndex, "target-cpu");
226  Attribute FSAttr =
227  FnAttrs.getAttribute(AttributeList::FunctionIndex, "target-features");
228 
229  std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
230  ? CPUAttr.getValueAsString().str()
231  : TargetCPU;
232  std::string FS = !FSAttr.hasAttribute(Attribute::None)
233  ? FSAttr.getValueAsString().str()
234  : TargetFS;
235 
236  auto &I = SubtargetMap[CPU + FS];
237  if (!I) {
238  // This needs to be done before we create a new subtarget since any
239  // creation will depend on the TM and the code generation flags on the
240  // function that reside in TargetOptions.
242  I = llvm::make_unique<HexagonSubtarget>(TargetTriple, CPU, FS, *this);
243  }
244  return I.get();
245 }
246 
248  PMB.addExtension(
250  [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
252  });
253  PMB.addExtension(
255  [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
257  });
258 }
259 
261  return TargetIRAnalysis([this](const Function &F) {
262  return TargetTransformInfo(HexagonTTIImpl(this, F));
263  });
264 }
265 
266 
268 
269 namespace {
270 /// Hexagon Code Generator Pass Configuration Options.
271 class HexagonPassConfig : public TargetPassConfig {
272 public:
273  HexagonPassConfig(HexagonTargetMachine &TM, PassManagerBase &PM)
274  : TargetPassConfig(TM, PM) {}
275 
276  HexagonTargetMachine &getHexagonTargetMachine() const {
277  return getTM<HexagonTargetMachine>();
278  }
279 
281  createMachineScheduler(MachineSchedContext *C) const override {
282  return createVLIWMachineSched(C);
283  }
284 
285  void addIRPasses() override;
286  bool addInstSelector() override;
287  void addPreRegAlloc() override;
288  void addPostRegAlloc() override;
289  void addPreSched2() override;
290  void addPreEmitPass() override;
291 };
292 } // namespace
293 
295  return new HexagonPassConfig(*this, PM);
296 }
297 
298 void HexagonPassConfig::addIRPasses() {
300  bool NoOpt = (getOptLevel() == CodeGenOpt::None);
301 
302  addPass(createAtomicExpandPass());
303  if (!NoOpt) {
304  if (EnableLoopPrefetch)
305  addPass(createLoopDataPrefetchPass());
306  if (EnableCommGEP)
307  addPass(createHexagonCommonGEP());
308  // Replace certain combinations of shifts and ands with extracts.
309  if (EnableGenExtract)
310  addPass(createHexagonGenExtract());
311  }
312 }
313 
314 bool HexagonPassConfig::addInstSelector() {
315  HexagonTargetMachine &TM = getHexagonTargetMachine();
316  bool NoOpt = (getOptLevel() == CodeGenOpt::None);
317 
318  if (!NoOpt)
320 
321  addPass(createHexagonISelDag(TM, getOptLevel()));
322 
323  if (!NoOpt) {
324  // Create logical operations on predicate registers.
325  if (EnableGenPred)
326  addPass(createHexagonGenPredicate());
327  // Rotate loops to expose bit-simplification opportunities.
328  if (EnableLoopResched)
330  // Split double registers.
331  if (!DisableHSDR)
332  addPass(createHexagonSplitDoubleRegs());
333  // Bit simplification.
334  if (EnableBitSimplify)
335  addPass(createHexagonBitSimplify());
336  addPass(createHexagonPeephole());
337  // Constant propagation.
338  if (!DisableHCP) {
341  }
342  if (EnableGenInsert)
343  addPass(createHexagonGenInsert());
344  if (EnableEarlyIf)
346  }
347 
348  return false;
349 }
350 
351 void HexagonPassConfig::addPreRegAlloc() {
352  if (getOptLevel() != CodeGenOpt::None) {
353  if (EnableCExtOpt)
354  addPass(createHexagonConstExtenders());
358  addPass(createHexagonStoreWidening());
360  addPass(createHexagonHardwareLoops());
361  }
362  if (TM->getOptLevel() >= CodeGenOpt::Default)
363  addPass(&MachinePipelinerID);
364 }
365 
366 void HexagonPassConfig::addPostRegAlloc() {
367  if (getOptLevel() != CodeGenOpt::None) {
368  if (EnableRDFOpt)
369  addPass(createHexagonRDFOpt());
371  addPass(createHexagonCFGOptimizer());
372  if (!DisableAModeOpt)
373  addPass(createHexagonOptAddrMode());
374  }
375 }
376 
377 void HexagonPassConfig::addPreSched2() {
378  addPass(createHexagonCopyToCombine());
379  if (getOptLevel() != CodeGenOpt::None)
380  addPass(&IfConverterID);
382 }
383 
384 void HexagonPassConfig::addPreEmitPass() {
385  bool NoOpt = (getOptLevel() == CodeGenOpt::None);
386 
387  if (!NoOpt)
388  addPass(createHexagonNewValueJump());
389 
391 
392  // Create Packets.
393  if (!NoOpt) {
395  addPass(createHexagonFixupHwLoops());
396  // Generate MUX from pairs of conditional transfers.
397  if (EnableGenMux)
398  addPass(createHexagonGenMux());
399 
400  addPass(createHexagonPacketizer(), false);
401  }
402  if (EnableVectorPrint)
403  addPass(createHexagonVectorPrint(), false);
404 
405  // Add CFI instructions if necessary.
406  addPass(createHexagonCallFrameInformation(), false);
407 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
const NoneType None
Definition: None.h:24
uint64_t CallInst * C
FunctionPass * createHexagonCopyToCombine()
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
char & HexagonExpandCondsetsID
static cl::opt< bool > EnableGenExtract("hexagon-extract", cl::init(true), cl::Hidden, cl::desc("Generate \xtract\instructions"))
void initializeHexagonRDFOptPass(PassRegistry &)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
PassManagerBuilder - This class is used to set up a standard optimization sequence for languages like...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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:76
void initializeHexagonHardwareLoopsPass(PassRegistry &)
static cl::opt< bool > DisableStoreWidening("disable-store-widen", cl::Hidden, cl::init(false), cl::desc("Disable store widening"))
static cl::opt< bool > EnableLoopPrefetch("hexagon-loop-prefetch", cl::init(false), cl::Hidden, cl::ZeroOrMore, cl::desc("Enable loop data prefetch on Hexagon"))
static cl::opt< bool > EnableCExtOpt("hexagon-cext", cl::Hidden, cl::ZeroOrMore, cl::init(true), cl::desc("Enable Hexagon constant-extender optimization"))
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 cl::opt< bool > EnableTrapUnreachable("hexagon-trap-unreachable", cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::desc("Enable generating trap for unreachable"))
F(f)
FunctionPass * createHexagonHardwareLoops()
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:899
MachineSchedRegistry provides a selection of available machine instruction schedulers.
ScheduleDAGMILive is an implementation of ScheduleDAGInstrs that schedules machine instructions while...
FunctionPass * createHexagonGenPredicate()
FunctionPass * createLoopDataPrefetchPass()
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)
void resetTargetOptions(const Function &F) const
Reset the target options based on the function&#39;s attributes.
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.
static cl::opt< bool > EnableGenPred("hexagon-gen-pred", cl::init(true), cl::Hidden, cl::desc("Enable conversion of arithmetic operations to " "predicate instructions"))
char & UnreachableMachineBlockElimID
UnreachableMachineBlockElimination - This pass removes unreachable machine basic blocks.
void initializeHexagonConstExtendersPass(PassRegistry &)
RegisterTargetMachine - Helper template for registering a target machine implementation, for use in the target machine initialization function.
void initializeHexagonNewValueJumpPass(PassRegistry &)
FunctionPass * createHexagonConstExtenders()
void initializeHexagonPacketizerPass(PassRegistry &)
FunctionPass * createHexagonBitSimplify()
static cl::opt< bool > EnableExpandCondsets("hexagon-expand-condsets", cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("Early expansion of MUX"))
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:205
static cl::opt< bool > EnableEarlyIf("hexagon-eif", cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("Enable early if-conversion"))
static CodeModel::Model getEffectiveCodeModel(Optional< CodeModel::Model > CM)
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...
void initializeHexagonVectorLoopCarriedReusePass(PassRegistry &)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:406
static cl::opt< bool > EnableVectorPrint("enable-hexagon-vector-print", cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::desc("Enable Hexagon Vector print instr pass"))
bool hasAttribute(AttrKind Val) const
Return true if the attribute is present.
Definition: Attributes.cpp:202
void initializeHexagonLoopIdiomRecognizePass(PassRegistry &)
FunctionPass * createHexagonEarlyIfConversion()
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
static ScheduleDAGInstrs * createVLIWMachineSched(MachineSchedContext *C)
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"))
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:285
FunctionPass * createHexagonSplitDoubleRegs()
FunctionPass * createHexagonLoopRescheduling()
TargetIRAnalysis getTargetIRAnalysis() override
Get a TargetIRAnalysis implementation for the target.
FunctionPass * createHexagonStoreWidening()
Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return the attribute object that exists at the given index.
void initializeHexagonEarlyIfConversionPass(PassRegistry &Registry)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
FunctionPass * createHexagonBranchRelaxation()
static MachineSchedRegistry SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler", createVLIWMachineSched)
FunctionPass * createHexagonNewValueJump()
HexagonTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT)
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&#39;s TargetSubtargetInf...
std::unique_ptr< ScheduleDAGMutation > createCopyConstrainDAGMutation(const TargetInstrInfo *TII, const TargetRegisterInfo *TRI)
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:77
void initializeHexagonOptAddrModePass(PassRegistry &)
A ScheduleDAG for scheduling lists of MachineInstr.
bool hasValue() const
Definition: Optional.h:137
FunctionPass * createHexagonSplitConst32AndConst64()
Pass * createHexagonVectorLoopCarriedReusePass()
const TargetRegisterInfo * TRI
Target processor register info.
Definition: ScheduleDAG.h:569
StringRef getValueAsString() const
Return the attribute&#39;s value as a string.
Definition: Attributes.cpp:195
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()
FunctionPass * createHexagonISelDag(HexagonTargetMachine &TM, CodeGenOpt::Level OptLevel)
const TargetInstrInfo * TII
Target instruction information.
Definition: ScheduleDAG.h:568
std::string TargetFS
Definition: TargetMachine.h:78
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:39
void adjustPassManager(PassManagerBuilder &PMB) override
Allow the target to modify the pass manager, e.g.
unsigned TrapUnreachable
Emit target-specific trap instruction for &#39;unreachable&#39; IR instructions.
void addExtension(ExtensionPointTy Ty, ExtensionFn Fn)
EP_LateLoopOptimizations - This extension point allows adding late loop canonicalization and simplifi...
FunctionPass * createAtomicExpandPass()
static cl::opt< bool > EnableGenInsert("hexagon-insert", cl::init(true), cl::Hidden, cl::desc("Generate \nsert\instructions"))
void initializeHexagonExpandCondsetsPass(PassRegistry &)
void LLVMInitializeHexagonTarget()
Target & getTheHexagonTarget()
EP_LoopOptimizerEnd - This extension point allows adding loop passes to the end of the loop optimizer...
FunctionPass * createHexagonCFGOptimizer()
void addMutation(std::unique_ptr< ScheduleDAGMutation > Mutation)
Add a postprocessing step to the DAG builder.