Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/LLVMTargetMachine.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the LLVMTargetMachine class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Analysis/Passes.h"
14
#include "llvm/CodeGen/AsmPrinter.h"
15
#include "llvm/CodeGen/BasicTTIImpl.h"
16
#include "llvm/CodeGen/MachineModuleInfo.h"
17
#include "llvm/CodeGen/Passes.h"
18
#include "llvm/CodeGen/TargetPassConfig.h"
19
#include "llvm/IR/LegacyPassManager.h"
20
#include "llvm/MC/MCAsmBackend.h"
21
#include "llvm/MC/MCAsmInfo.h"
22
#include "llvm/MC/MCCodeEmitter.h"
23
#include "llvm/MC/MCContext.h"
24
#include "llvm/MC/MCInstrInfo.h"
25
#include "llvm/MC/MCObjectWriter.h"
26
#include "llvm/MC/MCStreamer.h"
27
#include "llvm/MC/MCSubtargetInfo.h"
28
#include "llvm/Support/CommandLine.h"
29
#include "llvm/Support/ErrorHandling.h"
30
#include "llvm/Support/FormattedStream.h"
31
#include "llvm/Support/TargetRegistry.h"
32
#include "llvm/Target/TargetLoweringObjectFile.h"
33
#include "llvm/Target/TargetMachine.h"
34
#include "llvm/Target/TargetOptions.h"
35
using namespace llvm;
36
37
static cl::opt<bool> EnableTrapUnreachable("trap-unreachable",
38
  cl::Hidden, cl::ZeroOrMore, cl::init(false),
39
  cl::desc("Enable generating trap for unreachable"));
40
41
52.4k
void LLVMTargetMachine::initAsmInfo() {
42
52.4k
  MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str()));
43
52.4k
  MII.reset(TheTarget.createMCInstrInfo());
44
52.4k
  // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
45
52.4k
  // to some backends having subtarget feature dependent module level
46
52.4k
  // code generation. This is similar to the hack in the AsmPrinter for
47
52.4k
  // module level assembly etc.
48
52.4k
  STI.reset(TheTarget.createMCSubtargetInfo(
49
52.4k
      getTargetTriple().str(), getTargetCPU(), getTargetFeatureString()));
50
52.4k
51
52.4k
  MCAsmInfo *TmpAsmInfo =
52
52.4k
      TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str());
53
52.4k
  // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
54
52.4k
  // and if the old one gets included then MCAsmInfo will be NULL and
55
52.4k
  // we'll crash later.
56
52.4k
  // Provide the user with a useful error message about what's wrong.
57
52.4k
  assert(TmpAsmInfo && "MCAsmInfo not initialized. "
58
52.4k
         "Make sure you include the correct TargetSelect.h"
59
52.4k
         "and that InitializeAllTargetMCs() is being invoked!");
60
52.4k
61
52.4k
  if (Options.DisableIntegratedAS)
62
180
    TmpAsmInfo->setUseIntegratedAssembler(false);
63
52.4k
64
52.4k
  TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments);
65
52.4k
66
52.4k
  TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections);
67
52.4k
68
52.4k
  TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations);
69
52.4k
70
52.4k
  if (Options.ExceptionModel != ExceptionHandling::None)
71
1.60k
    TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
72
52.4k
73
52.4k
  AsmInfo.reset(TmpAsmInfo);
74
52.4k
}
75
76
LLVMTargetMachine::LLVMTargetMachine(const Target &T,
77
                                     StringRef DataLayoutString,
78
                                     const Triple &TT, StringRef CPU,
79
                                     StringRef FS, const TargetOptions &Options,
80
                                     Reloc::Model RM, CodeModel::Model CM,
81
                                     CodeGenOpt::Level OL)
82
52.5k
    : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
83
52.5k
  this->RM = RM;
84
52.5k
  this->CMModel = CM;
85
52.5k
  this->OptLevel = OL;
86
52.5k
87
52.5k
  if (EnableTrapUnreachable)
88
2
    this->Options.TrapUnreachable = true;
89
52.5k
}
90
91
TargetTransformInfo
92
8.20k
LLVMTargetMachine::getTargetTransformInfo(const Function &F) {
93
8.20k
  return TargetTransformInfo(BasicTTIImpl(this, F));
94
8.20k
}
95
96
/// addPassesToX helper drives creation and initialization of TargetPassConfig.
97
static TargetPassConfig *
98
addPassesToGenerateCode(LLVMTargetMachine &TM, PassManagerBase &PM,
99
36.3k
                        bool DisableVerify, MachineModuleInfo &MMI) {
100
36.3k
  // Targets may override createPassConfig to provide a target-specific
101
36.3k
  // subclass.
102
36.3k
  TargetPassConfig *PassConfig = TM.createPassConfig(PM);
103
36.3k
  // Set PassConfig options provided by TargetMachine.
104
36.3k
  PassConfig->setDisableVerify(DisableVerify);
105
36.3k
  PM.add(PassConfig);
106
36.3k
  PM.add(&MMI);
107
36.3k
108
36.3k
  if (PassConfig->addISelPasses())
109
0
    return nullptr;
110
36.3k
  PassConfig->addMachinePasses();
111
36.3k
  PassConfig->setInitialized();
112
36.3k
  return PassConfig;
113
36.3k
}
114
115
bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM,
116
                                      raw_pwrite_stream &Out,
117
                                      raw_pwrite_stream *DwoOut,
118
                                      CodeGenFileType FileType,
119
35.7k
                                      MCContext &Context) {
120
35.7k
  if (Options.MCOptions.MCSaveTempLabels)
121
0
    Context.setAllowTemporaryLabels(false);
122
35.7k
123
35.7k
  const MCSubtargetInfo &STI = *getMCSubtargetInfo();
124
35.7k
  const MCAsmInfo &MAI = *getMCAsmInfo();
125
35.7k
  const MCRegisterInfo &MRI = *getMCRegisterInfo();
126
35.7k
  const MCInstrInfo &MII = *getMCInstrInfo();
127
35.7k
128
35.7k
  std::unique_ptr<MCStreamer> AsmStreamer;
129
35.7k
130
35.7k
  switch (FileType) {
131
35.7k
  case CGFT_AssemblyFile: {
132
21.5k
    MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
133
21.5k
        getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
134
21.5k
135
21.5k
    // Create a code emitter if asked to show the encoding.
136
21.5k
    std::unique_ptr<MCCodeEmitter> MCE;
137
21.5k
    if (Options.MCOptions.ShowMCEncoding)
138
399
      MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context));
139
21.5k
140
21.5k
    std::unique_ptr<MCAsmBackend> MAB(
141
21.5k
        getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions));
142
21.5k
    auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
143
21.5k
    MCStreamer *S = getTarget().createAsmStreamer(
144
21.5k
        Context, std::move(FOut), Options.MCOptions.AsmVerbose,
145
21.5k
        Options.MCOptions.MCUseDwarfDirectory, InstPrinter, std::move(MCE),
146
21.5k
        std::move(MAB), Options.MCOptions.ShowMCInst);
147
21.5k
    AsmStreamer.reset(S);
148
21.5k
    break;
149
35.7k
  }
150
35.7k
  case CGFT_ObjectFile: {
151
14.1k
    // Create the code emitter for the target if it exists.  If not, .o file
152
14.1k
    // emission fails.
153
14.1k
    MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
154
14.1k
    MCAsmBackend *MAB =
155
14.1k
        getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions);
156
14.1k
    if (!MCE || !MAB)
157
0
      return true;
158
14.1k
159
14.1k
    // Don't waste memory on names of temp labels.
160
14.1k
    Context.setUseNamesOnTempLabels(false);
161
14.1k
162
14.1k
    Triple T(getTargetTriple().str());
163
14.1k
    AsmStreamer.reset(getTarget().createMCObjectStreamer(
164
14.1k
        T, Context, std::unique_ptr<MCAsmBackend>(MAB),
165
14.1k
        DwoOut ? 
MAB->createDwoObjectWriter(Out, *DwoOut)10
166
14.1k
               : 
MAB->createObjectWriter(Out)14.1k
,
167
14.1k
        std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
168
14.1k
        Options.MCOptions.MCIncrementalLinkerCompatible,
169
14.1k
        /*DWARFMustBeAtTheEnd*/ true));
170
14.1k
    break;
171
14.1k
  }
172
14.1k
  case CGFT_Null:
173
15
    // The Null output is intended for use for performance analysis and testing,
174
15
    // not real users.
175
15
    AsmStreamer.reset(getTarget().createNullStreamer(Context));
176
15
    break;
177
35.7k
  }
178
35.7k
179
35.7k
  // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
180
35.7k
  FunctionPass *Printer =
181
35.7k
      getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
182
35.7k
  if (!Printer)
183
0
    return true;
184
35.7k
185
35.7k
  PM.add(Printer);
186
35.7k
  return false;
187
35.7k
}
188
189
bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
190
                                            raw_pwrite_stream &Out,
191
                                            raw_pwrite_stream *DwoOut,
192
                                            CodeGenFileType FileType,
193
                                            bool DisableVerify,
194
36.0k
                                            MachineModuleInfo *MMI) {
195
36.0k
  // Add common CodeGen passes.
196
36.0k
  if (!MMI)
197
13.1k
    MMI = new MachineModuleInfo(this);
198
36.0k
  TargetPassConfig *PassConfig =
199
36.0k
      addPassesToGenerateCode(*this, PM, DisableVerify, *MMI);
200
36.0k
  if (!PassConfig)
201
0
    return true;
202
36.0k
203
36.0k
  if (!TargetPassConfig::willCompleteCodeGenPipeline()) {
204
315
    if (this->getTargetTriple().isOSAIX()) {
205
4
      // On AIX, we might manifest MCSymbols during SDAG lowering. For MIR
206
4
      // testing to be meaningful, we need to ensure that the symbols created
207
4
      // are MCSymbolXCOFF variants, which requires that
208
4
      // the TargetLoweringObjectFile instance has been initialized.
209
4
      MCContext &Ctx = MMI->getContext();
210
4
      const_cast<TargetLoweringObjectFile &>(*this->getObjFileLowering())
211
4
          .Initialize(Ctx, *this);
212
4
    }
213
315
    PM.add(createPrintMIRPass(Out));
214
35.7k
  } else if (addAsmPrinter(PM, Out, DwoOut, FileType, MMI->getContext()))
215
0
    return true;
216
36.0k
217
36.0k
  PM.add(createFreeMachineFunctionPass());
218
36.0k
  return false;
219
36.0k
}
220
221
/// addPassesToEmitMC - Add passes to the specified pass manager to get
222
/// machine code emitted with the MCJIT. This method returns true if machine
223
/// code is not supported. It fills the MCContext Ctx pointer which can be
224
/// used to build custom MCStreamer.
225
///
226
bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
227
                                          raw_pwrite_stream &Out,
228
303
                                          bool DisableVerify) {
229
303
  // Add common CodeGen passes.
230
303
  MachineModuleInfo *MMI = new MachineModuleInfo(this);
231
303
  TargetPassConfig *PassConfig =
232
303
      addPassesToGenerateCode(*this, PM, DisableVerify, *MMI);
233
303
  if (!PassConfig)
234
0
    return true;
235
303
  assert(TargetPassConfig::willCompleteCodeGenPipeline() &&
236
303
         "Cannot emit MC with limited codegen pipeline");
237
303
238
303
  Ctx = &MMI->getContext();
239
303
  if (Options.MCOptions.MCSaveTempLabels)
240
0
    Ctx->setAllowTemporaryLabels(false);
241
303
242
303
  // Create the code emitter for the target if it exists.  If not, .o file
243
303
  // emission fails.
244
303
  const MCSubtargetInfo &STI = *getMCSubtargetInfo();
245
303
  const MCRegisterInfo &MRI = *getMCRegisterInfo();
246
303
  MCCodeEmitter *MCE =
247
303
      getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
248
303
  MCAsmBackend *MAB =
249
303
      getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions);
250
303
  if (!MCE || !MAB)
251
0
    return true;
252
303
253
303
  const Triple &T = getTargetTriple();
254
303
  std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
255
303
      T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out),
256
303
      std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
257
303
      Options.MCOptions.MCIncrementalLinkerCompatible,
258
303
      /*DWARFMustBeAtTheEnd*/ true));
259
303
260
303
  // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
261
303
  FunctionPass *Printer =
262
303
      getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
263
303
  if (!Printer)
264
0
    return true;
265
303
266
303
  PM.add(Printer);
267
303
  PM.add(createFreeMachineFunctionPass());
268
303
269
303
  return false; // success!
270
303
}