Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/MC/ConstantPools.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ConstantPools.cpp - ConstantPool 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 ConstantPool and  AssemblerConstantPools classes.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/MC/ConstantPools.h"
14
#include "llvm/MC/MCContext.h"
15
#include "llvm/MC/MCDirectives.h"
16
#include "llvm/MC/MCExpr.h"
17
#include "llvm/MC/MCStreamer.h"
18
#include "llvm/Support/Casting.h"
19
20
using namespace llvm;
21
22
//
23
// ConstantPool implementation
24
//
25
// Emit the contents of the constant pool using the provided streamer.
26
191
void ConstantPool::emitEntries(MCStreamer &Streamer) {
27
191
  if (Entries.empty())
28
0
    return;
29
191
  Streamer.EmitDataRegion(MCDR_DataRegion);
30
300
  for (const ConstantPoolEntry &Entry : Entries) {
31
300
    Streamer.EmitCodeAlignment(Entry.Size); // align naturally
32
300
    Streamer.EmitLabel(Entry.Label);
33
300
    Streamer.EmitValue(Entry.Value, Entry.Size, Entry.Loc);
34
300
  }
35
191
  Streamer.EmitDataRegion(MCDR_DataRegionEnd);
36
191
  Entries.clear();
37
191
}
38
39
const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,
40
324
                                     unsigned Size, SMLoc Loc) {
41
324
  const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Value);
42
324
43
324
  // Check if there is existing entry for the same constant. If so, reuse it.
44
324
  auto Itr = C ? 
CachedEntries.find(C->getValue())235
:
CachedEntries.end()89
;
45
324
  if (Itr != CachedEntries.end())
46
20
    return Itr->second;
47
304
48
304
  MCSymbol *CPEntryLabel = Context.createTempSymbol();
49
304
50
304
  Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
51
304
  const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
52
304
  if (C)
53
215
    CachedEntries[C->getValue()] = SymRef;
54
304
  return SymRef;
55
304
}
56
57
211
bool ConstantPool::empty() { return Entries.empty(); }
58
59
34
void ConstantPool::clearCache() {
60
34
  CachedEntries.clear();
61
34
}
62
63
//
64
// AssemblerConstantPools implementation
65
//
66
88
ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
67
88
  ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
68
88
  if (CP == ConstantPools.end())
69
20
    return nullptr;
70
68
71
68
  return &CP->second;
72
68
}
73
74
ConstantPool &
75
324
AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
76
324
  return ConstantPools[Section];
77
324
}
78
79
static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
80
211
                             ConstantPool &CP) {
81
211
  if (!CP.empty()) {
82
191
    Streamer.SwitchSection(Section);
83
191
    CP.emitEntries(Streamer);
84
191
  }
85
211
}
86
87
10.2k
void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
88
10.2k
  // Dump contents of assembler constant pools.
89
10.2k
  for (auto &CPI : ConstantPools) {
90
177
    MCSection *Section = CPI.first;
91
177
    ConstantPool &CP = CPI.second;
92
177
93
177
    emitConstantPool(Streamer, Section, CP);
94
177
  }
95
10.2k
}
96
97
44
void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
98
44
  MCSection *Section = Streamer.getCurrentSectionOnly();
99
44
  if (ConstantPool *CP = getConstantPool(Section))
100
34
    emitConstantPool(Streamer, Section, *CP);
101
44
}
102
103
44
void AssemblerConstantPools::clearCacheForCurrentSection(MCStreamer &Streamer) {
104
44
  MCSection *Section = Streamer.getCurrentSectionOnly();
105
44
  if (ConstantPool *CP = getConstantPool(Section))
106
34
    CP->clearCache();
107
44
}
108
109
const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
110
                                               const MCExpr *Expr,
111
324
                                               unsigned Size, SMLoc Loc) {
112
324
  MCSection *Section = Streamer.getCurrentSectionOnly();
113
324
  return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
114
324
                                                   Size, Loc);
115
324
}