Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- RISCVTargetObjectFile.cpp - RISCV Object Info -----------------===//
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
#include "RISCVTargetObjectFile.h"
10
#include "RISCVTargetMachine.h"
11
#include "llvm/BinaryFormat/ELF.h"
12
#include "llvm/MC/MCContext.h"
13
#include "llvm/MC/MCSectionELF.h"
14
15
using namespace llvm;
16
17
void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
18
329
                                          const TargetMachine &TM) {
19
329
  TargetLoweringObjectFileELF::Initialize(Ctx, TM);
20
329
  InitializeELF(TM.Options.UseInitArray);
21
329
22
329
  SmallDataSection = getContext().getELFSection(
23
329
      ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
24
329
  SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
25
329
                                               ELF::SHF_WRITE | ELF::SHF_ALLOC);
26
329
}
27
28
// A address must be loaded from a small section if its size is less than the
29
// small section size threshold. Data in this section could be addressed by
30
// using gp_rel operator.
31
155
bool RISCVELFTargetObjectFile::isInSmallSection(uint64_t Size) const {
32
155
  // gcc has traditionally not treated zero-sized objects as small data, so this
33
155
  // is effectively part of the ABI.
34
155
  return Size > 0 && Size <= SSThreshold;
35
155
}
36
37
// Return true if this global address should be placed into small data/bss
38
// section.
39
bool RISCVELFTargetObjectFile::isGlobalInSmallSection(
40
67
    const GlobalObject *GO, const TargetMachine &TM) const {
41
67
  // Only global variables, not functions.
42
67
  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
43
67
  if (!GVA)
44
0
    return false;
45
67
46
67
  // If the variable has an explicit section, it is placed in that section.
47
67
  if (GVA->hasSection()) {
48
0
    StringRef Section = GVA->getSection();
49
0
50
0
    // Explicitly placing any variable in the small data section overrides
51
0
    // the global -G value.
52
0
    if (Section == ".sdata" || Section == ".sbss")
53
0
      return true;
54
0
55
0
    // Otherwise reject putting the variable to small section if it has an
56
0
    // explicit section name.
57
0
    return false;
58
0
  }
59
67
60
67
  if (((GVA->hasExternalLinkage() && 
GVA->isDeclaration()59
) ||
61
67
       GVA->hasCommonLinkage()))
62
0
    return false;
63
67
64
67
  Type *Ty = GVA->getValueType();
65
67
  // It is possible that the type of the global is unsized, i.e. a declaration
66
67
  // of a extern struct. In this case don't presume it is in the small data
67
67
  // section. This happens e.g. when building the FreeBSD kernel.
68
67
  if (!Ty->isSized())
69
0
    return false;
70
67
71
67
  return isInSmallSection(
72
67
      GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
73
67
}
74
75
MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
76
3.43k
    const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
77
3.43k
  // Handle Small Section classification here.
78
3.43k
  if (Kind.isBSS() && 
isGlobalInSmallSection(GO, TM)50
)
79
24
    return SmallBSSSection;
80
3.40k
  if (Kind.isData() && 
isGlobalInSmallSection(GO, TM)17
)
81
9
    return SmallDataSection;
82
3.40k
83
3.40k
  // Otherwise, we work the same as ELF.
84
3.40k
  return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
85
3.40k
}
86
87
329
void RISCVELFTargetObjectFile::getModuleMetadata(Module &M) {
88
329
  SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
89
329
  M.getModuleFlagsMetadata(ModuleFlags);
90
329
91
329
  for (const auto &MFE : ModuleFlags) {
92
26
    StringRef Key = MFE.Key->getString();
93
26
    if (Key == "SmallDataLimit") {
94
8
      SSThreshold = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
95
8
      break;
96
8
    }
97
26
  }
98
329
}
99
100
/// Return true if this constant should be placed into small data section.
101
bool RISCVELFTargetObjectFile::isConstantInSmallSection(
102
88
    const DataLayout &DL, const Constant *CN) const {
103
88
  return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
104
88
}
105
106
MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
107
    const DataLayout &DL, SectionKind Kind, const Constant *C,
108
88
    unsigned &Align) const {
109
88
  if (isConstantInSmallSection(DL, C))
110
88
    return SmallDataSection;
111
0
112
0
  // Otherwise, we work the same as ELF.
113
0
  return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
114
0
}