1226031Sstas//===-- SystemZTargetMachine.cpp - Define TargetMachine for SystemZ -------===//
2226031Sstas//
3226031Sstas//                     The LLVM Compiler Infrastructure
4226031Sstas//
5226031Sstas// This file is distributed under the University of Illinois Open Source
6226031Sstas// License. See LICENSE.TXT for details.
7226031Sstas//
8226031Sstas//===----------------------------------------------------------------------===//
9226031Sstas
10226031Sstas#include "SystemZTargetMachine.h"
11226031Sstas#include "llvm/CodeGen/Passes.h"
12226031Sstas#include "llvm/Support/TargetRegistry.h"
13226031Sstas#include "llvm/Transforms/Scalar.h"
14226031Sstas
15226031Sstasusing namespace llvm;
16226031Sstas
17226031Sstasextern "C" void LLVMInitializeSystemZTarget() {
18226031Sstas  // Register the target.
19226031Sstas  RegisterTargetMachine<SystemZTargetMachine> X(TheSystemZTarget);
20226031Sstas}
21226031Sstas
22226031SstasSystemZTargetMachine::SystemZTargetMachine(const Target &T, StringRef TT,
23226031Sstas                                           StringRef CPU, StringRef FS,
24226031Sstas                                           const TargetOptions &Options,
25226031Sstas                                           Reloc::Model RM,
26226031Sstas                                           CodeModel::Model CM,
27226031Sstas                                           CodeGenOpt::Level OL)
28226031Sstas  : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
29226031Sstas    Subtarget(TT, CPU, FS),
30226031Sstas    // Make sure that global data has at least 16 bits of alignment by default,
31226031Sstas    // so that we can refer to it using LARL.  We don't have any special
32226031Sstas    // requirements for stack variables though.
33226031Sstas    DL("E-p:64:64:64-i1:8:16-i8:8:16-i16:16-i32:32-i64:64"
34226031Sstas       "-f32:32-f64:64-f128:64-a0:8:16-n32:64"),
35226031Sstas    InstrInfo(*this), TLInfo(*this), TSInfo(*this),
36226031Sstas    FrameLowering(*this, Subtarget) {
37226031Sstas  initAsmInfo();
38226031Sstas}
39226031Sstas
40226031Sstasnamespace {
41226031Sstas/// SystemZ Code Generator Pass Configuration Options.
42226031Sstasclass SystemZPassConfig : public TargetPassConfig {
43226031Sstaspublic:
44226031Sstas  SystemZPassConfig(SystemZTargetMachine *TM, PassManagerBase &PM)
45226031Sstas    : TargetPassConfig(TM, PM) {}
46226031Sstas
47226031Sstas  SystemZTargetMachine &getSystemZTargetMachine() const {
48226031Sstas    return getTM<SystemZTargetMachine>();
49226031Sstas  }
50226031Sstas
51226031Sstas  virtual void addIRPasses() LLVM_OVERRIDE;
52226031Sstas  virtual bool addInstSelector() LLVM_OVERRIDE;
53226031Sstas  virtual bool addPreSched2() LLVM_OVERRIDE;
54226031Sstas  virtual bool addPreEmitPass() LLVM_OVERRIDE;
55226031Sstas};
56226031Sstas} // end anonymous namespace
57226031Sstas
58226031Sstasvoid SystemZPassConfig::addIRPasses() {
59226031Sstas  TargetPassConfig::addIRPasses();
60226031Sstas  addPass(createPartiallyInlineLibCallsPass());
61226031Sstas}
62226031Sstas
63226031Sstasbool SystemZPassConfig::addInstSelector() {
64226031Sstas  addPass(createSystemZISelDag(getSystemZTargetMachine(), getOptLevel()));
65226031Sstas  return false;
66226031Sstas}
67226031Sstas
68226031Sstasbool SystemZPassConfig::addPreSched2() {
69226031Sstas  if (getSystemZTargetMachine().getSubtargetImpl()->hasLoadStoreOnCond())
70226031Sstas    addPass(&IfConverterID);
71226031Sstas  return true;
72226031Sstas}
73226031Sstas
74226031Sstasbool SystemZPassConfig::addPreEmitPass() {
75226031Sstas  // We eliminate comparisons here rather than earlier because some
76226031Sstas  // transformations can change the set of available CC values and we
77226031Sstas  // generally want those transformations to have priority.  This is
78226031Sstas  // especially true in the commonest case where the result of the comparison
79226031Sstas  // is used by a single in-range branch instruction, since we will then
80226031Sstas  // be able to fuse the compare and the branch instead.
81226031Sstas  //
82226031Sstas  // For example, two-address NILF can sometimes be converted into
83226031Sstas  // three-address RISBLG.  NILF produces a CC value that indicates whether
84226031Sstas  // the low word is zero, but RISBLG does not modify CC at all.  On the
85226031Sstas  // other hand, 64-bit ANDs like NILL can sometimes be converted to RISBG.
86226031Sstas  // The CC value produced by NILL isn't useful for our purposes, but the
87226031Sstas  // value produced by RISBG can be used for any comparison with zero
88226031Sstas  // (not just equality).  So there are some transformations that lose
89226031Sstas  // CC values (while still being worthwhile) and others that happen to make
90226031Sstas  // the CC result more useful than it was originally.
91226031Sstas  //
92226031Sstas  // Another reason is that we only want to use BRANCH ON COUNT in cases
93226031Sstas  // where we know that the count register is not going to be spilled.
94226031Sstas  //
95226031Sstas  // Doing it so late makes it more likely that a register will be reused
96226031Sstas  // between the comparison and the branch, but it isn't clear whether
97226031Sstas  // preventing that would be a win or not.
98226031Sstas  if (getOptLevel() != CodeGenOpt::None)
99226031Sstas    addPass(createSystemZElimComparePass(getSystemZTargetMachine()));
100226031Sstas  if (getOptLevel() != CodeGenOpt::None)
101226031Sstas    addPass(createSystemZShortenInstPass(getSystemZTargetMachine()));
102226031Sstas  addPass(createSystemZLongBranchPass(getSystemZTargetMachine()));
103226031Sstas  return true;
104226031Sstas}
105226031Sstas
106226031SstasTargetPassConfig *SystemZTargetMachine::createPassConfig(PassManagerBase &PM) {
107226031Sstas  return new SystemZPassConfig(this, PM);
108226031Sstas}
109226031Sstas