[x86] fix assert with horizontal math + broadcast of vector (PR43402)
[llvm-core.git] / lib / Object / RecordStreamer.cpp
blobf39a6c28ed50ccaa1823f3d842c778bb8d301e8e
1 //===-- RecordStreamer.cpp - Record asm defined and used symbols ----------===//
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 //===----------------------------------------------------------------------===//
9 #include "RecordStreamer.h"
10 #include "llvm/IR/Mangler.h"
11 #include "llvm/IR/Module.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCSymbol.h"
15 using namespace llvm;
17 void RecordStreamer::markDefined(const MCSymbol &Symbol) {
18 State &S = Symbols[Symbol.getName()];
19 switch (S) {
20 case DefinedGlobal:
21 case Global:
22 S = DefinedGlobal;
23 break;
24 case NeverSeen:
25 case Defined:
26 case Used:
27 S = Defined;
28 break;
29 case DefinedWeak:
30 break;
31 case UndefinedWeak:
32 S = DefinedWeak;
36 void RecordStreamer::markGlobal(const MCSymbol &Symbol,
37 MCSymbolAttr Attribute) {
38 State &S = Symbols[Symbol.getName()];
39 switch (S) {
40 case DefinedGlobal:
41 case Defined:
42 S = (Attribute == MCSA_Weak) ? DefinedWeak : DefinedGlobal;
43 break;
45 case NeverSeen:
46 case Global:
47 case Used:
48 S = (Attribute == MCSA_Weak) ? UndefinedWeak : Global;
49 break;
50 case UndefinedWeak:
51 case DefinedWeak:
52 break;
56 void RecordStreamer::markUsed(const MCSymbol &Symbol) {
57 State &S = Symbols[Symbol.getName()];
58 switch (S) {
59 case DefinedGlobal:
60 case Defined:
61 case Global:
62 case DefinedWeak:
63 case UndefinedWeak:
64 break;
66 case NeverSeen:
67 case Used:
68 S = Used;
69 break;
73 void RecordStreamer::visitUsedSymbol(const MCSymbol &Sym) { markUsed(Sym); }
75 RecordStreamer::RecordStreamer(MCContext &Context, const Module &M)
76 : MCStreamer(Context), M(M) {}
78 RecordStreamer::const_iterator RecordStreamer::begin() {
79 return Symbols.begin();
82 RecordStreamer::const_iterator RecordStreamer::end() { return Symbols.end(); }
84 void RecordStreamer::EmitInstruction(const MCInst &Inst,
85 const MCSubtargetInfo &STI) {
86 MCStreamer::EmitInstruction(Inst, STI);
89 void RecordStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) {
90 MCStreamer::EmitLabel(Symbol);
91 markDefined(*Symbol);
94 void RecordStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
95 markDefined(*Symbol);
96 MCStreamer::EmitAssignment(Symbol, Value);
99 bool RecordStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
100 MCSymbolAttr Attribute) {
101 if (Attribute == MCSA_Global || Attribute == MCSA_Weak)
102 markGlobal(*Symbol, Attribute);
103 if (Attribute == MCSA_LazyReference)
104 markUsed(*Symbol);
105 return true;
108 void RecordStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
109 uint64_t Size, unsigned ByteAlignment,
110 SMLoc Loc) {
111 markDefined(*Symbol);
114 void RecordStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
115 unsigned ByteAlignment) {
116 markDefined(*Symbol);
119 RecordStreamer::State RecordStreamer::getSymbolState(const MCSymbol *Sym) {
120 auto SI = Symbols.find(Sym->getName());
121 if (SI == Symbols.end())
122 return NeverSeen;
123 return SI->second;
126 void RecordStreamer::emitELFSymverDirective(StringRef AliasName,
127 const MCSymbol *Aliasee) {
128 SymverAliasMap[Aliasee].push_back(AliasName);
131 iterator_range<RecordStreamer::const_symver_iterator>
132 RecordStreamer::symverAliases() {
133 return {SymverAliasMap.begin(), SymverAliasMap.end()};
136 void RecordStreamer::flushSymverDirectives() {
137 // Mapping from mangled name to GV.
138 StringMap<const GlobalValue *> MangledNameMap;
139 // The name in the assembler will be mangled, but the name in the IR
140 // might not, so we first compute a mapping from mangled name to GV.
141 Mangler Mang;
142 SmallString<64> MangledName;
143 for (const GlobalValue &GV : M.global_values()) {
144 if (!GV.hasName())
145 continue;
146 MangledName.clear();
147 MangledName.reserve(GV.getName().size() + 1);
148 Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false);
149 MangledNameMap[MangledName] = &GV;
152 // Walk all the recorded .symver aliases, and set up the binding
153 // for each alias.
154 for (auto &Symver : SymverAliasMap) {
155 const MCSymbol *Aliasee = Symver.first;
156 MCSymbolAttr Attr = MCSA_Invalid;
157 bool IsDefined = false;
159 // First check if the aliasee binding was recorded in the asm.
160 RecordStreamer::State state = getSymbolState(Aliasee);
161 switch (state) {
162 case RecordStreamer::Global:
163 case RecordStreamer::DefinedGlobal:
164 Attr = MCSA_Global;
165 break;
166 case RecordStreamer::UndefinedWeak:
167 case RecordStreamer::DefinedWeak:
168 Attr = MCSA_Weak;
169 break;
170 default:
171 break;
174 switch (state) {
175 case RecordStreamer::Defined:
176 case RecordStreamer::DefinedGlobal:
177 case RecordStreamer::DefinedWeak:
178 IsDefined = true;
179 break;
180 case RecordStreamer::NeverSeen:
181 case RecordStreamer::Global:
182 case RecordStreamer::Used:
183 case RecordStreamer::UndefinedWeak:
184 break;
187 if (Attr == MCSA_Invalid || !IsDefined) {
188 const GlobalValue *GV = M.getNamedValue(Aliasee->getName());
189 if (!GV) {
190 auto MI = MangledNameMap.find(Aliasee->getName());
191 if (MI != MangledNameMap.end())
192 GV = MI->second;
194 if (GV) {
195 // If we don't have a symbol attribute from assembly, then check if
196 // the aliasee was defined in the IR.
197 if (Attr == MCSA_Invalid) {
198 if (GV->hasExternalLinkage())
199 Attr = MCSA_Global;
200 else if (GV->hasLocalLinkage())
201 Attr = MCSA_Local;
202 else if (GV->isWeakForLinker())
203 Attr = MCSA_Weak;
205 IsDefined = IsDefined || !GV->isDeclarationForLinker();
209 // Set the detected binding on each alias with this aliasee.
210 for (auto AliasName : Symver.second) {
211 std::pair<StringRef, StringRef> Split = AliasName.split("@@@");
212 SmallString<128> NewName;
213 if (!Split.second.empty() && !Split.second.startswith("@")) {
214 // Special processing for "@@@" according
215 // https://sourceware.org/binutils/docs/as/Symver.html
216 const char *Separator = IsDefined ? "@@" : "@";
217 AliasName =
218 (Split.first + Separator + Split.second).toStringRef(NewName);
220 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
221 // TODO: Handle "@@@". Depending on SymbolAttribute value it needs to be
222 // converted into @ or @@.
223 const MCExpr *Value = MCSymbolRefExpr::create(Aliasee, getContext());
224 if (IsDefined)
225 markDefined(*Alias);
226 // Don't use EmitAssignment override as it always marks alias as defined.
227 MCStreamer::EmitAssignment(Alias, Value);
228 if (Attr != MCSA_Invalid)
229 EmitSymbolAttribute(Alias, Attr);