Path: blob/main/contrib/llvm-project/llvm/lib/MC/ConstantPools.cpp
35233 views
//===- ConstantPools.cpp - ConstantPool class -----------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the ConstantPool and AssemblerConstantPools classes.9//10//===----------------------------------------------------------------------===//1112#include "llvm/MC/ConstantPools.h"13#include "llvm/MC/MCContext.h"14#include "llvm/MC/MCDirectives.h"15#include "llvm/MC/MCExpr.h"16#include "llvm/MC/MCStreamer.h"17#include "llvm/Support/Casting.h"1819using namespace llvm;2021//22// ConstantPool implementation23//24// Emit the contents of the constant pool using the provided streamer.25void ConstantPool::emitEntries(MCStreamer &Streamer) {26if (Entries.empty())27return;28Streamer.emitDataRegion(MCDR_DataRegion);29for (const ConstantPoolEntry &Entry : Entries) {30Streamer.emitValueToAlignment(Align(Entry.Size)); // align naturally31Streamer.emitLabel(Entry.Label);32Streamer.emitValue(Entry.Value, Entry.Size, Entry.Loc);33}34Streamer.emitDataRegion(MCDR_DataRegionEnd);35Entries.clear();36}3738const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,39unsigned Size, SMLoc Loc) {40const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Value);41const MCSymbolRefExpr *S = dyn_cast<MCSymbolRefExpr>(Value);4243// Check if there is existing entry for the same constant. If so, reuse it.44if (C) {45auto CItr = CachedConstantEntries.find(std::make_pair(C->getValue(), Size));46if (CItr != CachedConstantEntries.end())47return CItr->second;48}4950// Check if there is existing entry for the same symbol. If so, reuse it.51if (S) {52auto SItr =53CachedSymbolEntries.find(std::make_pair(&(S->getSymbol()), Size));54if (SItr != CachedSymbolEntries.end())55return SItr->second;56}5758MCSymbol *CPEntryLabel = Context.createTempSymbol();5960Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));61const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);62if (C)63CachedConstantEntries[std::make_pair(C->getValue(), Size)] = SymRef;64if (S)65CachedSymbolEntries[std::make_pair(&(S->getSymbol()), Size)] = SymRef;66return SymRef;67}6869bool ConstantPool::empty() { return Entries.empty(); }7071void ConstantPool::clearCache() {72CachedConstantEntries.clear();73CachedSymbolEntries.clear();74}7576//77// AssemblerConstantPools implementation78//79ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {80ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);81if (CP == ConstantPools.end())82return nullptr;8384return &CP->second;85}8687ConstantPool &88AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {89return ConstantPools[Section];90}9192static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,93ConstantPool &CP) {94if (!CP.empty()) {95Streamer.switchSection(Section);96CP.emitEntries(Streamer);97}98}99100void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {101// Dump contents of assembler constant pools.102for (auto &CPI : ConstantPools) {103MCSection *Section = CPI.first;104ConstantPool &CP = CPI.second;105106emitConstantPool(Streamer, Section, CP);107}108}109110void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {111MCSection *Section = Streamer.getCurrentSectionOnly();112if (ConstantPool *CP = getConstantPool(Section))113emitConstantPool(Streamer, Section, *CP);114}115116void AssemblerConstantPools::clearCacheForCurrentSection(MCStreamer &Streamer) {117MCSection *Section = Streamer.getCurrentSectionOnly();118if (ConstantPool *CP = getConstantPool(Section))119CP->clearCache();120}121122const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,123const MCExpr *Expr,124unsigned Size, SMLoc Loc) {125MCSection *Section = Streamer.getCurrentSectionOnly();126return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),127Size, Loc);128}129130131