comparison lib/Bitcode/Writer/ValueEnumerator.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children c2174574ed3a
comparison
equal deleted inserted replaced
120:1172e4bd9c6f 121:803732b1fca8
1 //===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===// 1 //===- ValueEnumerator.cpp - Number values and types for bitcode writer ---===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
10 // This file implements the ValueEnumerator class. 10 // This file implements the ValueEnumerator class.
11 // 11 //
12 //===----------------------------------------------------------------------===// 12 //===----------------------------------------------------------------------===//
13 13
14 #include "ValueEnumerator.h" 14 #include "ValueEnumerator.h"
15 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/SmallPtrSet.h" 16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/IR/Constants.h" 17 #include "llvm/IR/Argument.h"
18 #include "llvm/IR/Attributes.h"
19 #include "llvm/IR/BasicBlock.h"
20 #include "llvm/IR/Constant.h"
18 #include "llvm/IR/DebugInfoMetadata.h" 21 #include "llvm/IR/DebugInfoMetadata.h"
19 #include "llvm/IR/DerivedTypes.h" 22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/GlobalAlias.h"
25 #include "llvm/IR/GlobalIFunc.h"
26 #include "llvm/IR/GlobalObject.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/Instruction.h"
20 #include "llvm/IR/Instructions.h" 30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/Metadata.h"
21 #include "llvm/IR/Module.h" 32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/Type.h"
34 #include "llvm/IR/Use.h"
22 #include "llvm/IR/UseListOrder.h" 35 #include "llvm/IR/UseListOrder.h"
36 #include "llvm/IR/User.h"
37 #include "llvm/IR/Value.h"
23 #include "llvm/IR/ValueSymbolTable.h" 38 #include "llvm/IR/ValueSymbolTable.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Debug.h" 41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/MathExtras.h"
25 #include "llvm/Support/raw_ostream.h" 43 #include "llvm/Support/raw_ostream.h"
26 #include <algorithm> 44 #include <algorithm>
45 #include <cassert>
46 #include <cstddef>
47 #include <iterator>
48 #include <tuple>
49 #include <utility>
50 #include <vector>
51
27 using namespace llvm; 52 using namespace llvm;
28 53
29 namespace { 54 namespace {
55
30 struct OrderMap { 56 struct OrderMap {
31 DenseMap<const Value *, std::pair<unsigned, bool>> IDs; 57 DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
32 unsigned LastGlobalConstantID; 58 unsigned LastGlobalConstantID = 0;
33 unsigned LastGlobalValueID; 59 unsigned LastGlobalValueID = 0;
34 60
35 OrderMap() : LastGlobalConstantID(0), LastGlobalValueID(0) {} 61 OrderMap() = default;
36 62
37 bool isGlobalConstant(unsigned ID) const { 63 bool isGlobalConstant(unsigned ID) const {
38 return ID <= LastGlobalConstantID; 64 return ID <= LastGlobalConstantID;
39 } 65 }
66
40 bool isGlobalValue(unsigned ID) const { 67 bool isGlobalValue(unsigned ID) const {
41 return ID <= LastGlobalValueID && !isGlobalConstant(ID); 68 return ID <= LastGlobalValueID && !isGlobalConstant(ID);
42 } 69 }
43 70
44 unsigned size() const { return IDs.size(); } 71 unsigned size() const { return IDs.size(); }
45 std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; } 72 std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
73
46 std::pair<unsigned, bool> lookup(const Value *V) const { 74 std::pair<unsigned, bool> lookup(const Value *V) const {
47 return IDs.lookup(V); 75 return IDs.lookup(V);
48 } 76 }
77
49 void index(const Value *V) { 78 void index(const Value *V) {
50 // Explicitly sequence get-size and insert-value operations to avoid UB. 79 // Explicitly sequence get-size and insert-value operations to avoid UB.
51 unsigned ID = IDs.size() + 1; 80 unsigned ID = IDs.size() + 1;
52 IDs[V].first = ID; 81 IDs[V].first = ID;
53 } 82 }
54 }; 83 };
55 } 84
85 } // end anonymous namespace
56 86
57 static void orderValue(const Value *V, OrderMap &OM) { 87 static void orderValue(const Value *V, OrderMap &OM) {
58 if (OM.lookup(V).first) 88 if (OM.lookup(V).first)
59 return; 89 return;
60 90
139 169
140 static void predictValueUseListOrderImpl(const Value *V, const Function *F, 170 static void predictValueUseListOrderImpl(const Value *V, const Function *F,
141 unsigned ID, const OrderMap &OM, 171 unsigned ID, const OrderMap &OM,
142 UseListOrderStack &Stack) { 172 UseListOrderStack &Stack) {
143 // Predict use-list order for this one. 173 // Predict use-list order for this one.
144 typedef std::pair<const Use *, unsigned> Entry; 174 using Entry = std::pair<const Use *, unsigned>;
145 SmallVector<Entry, 64> List; 175 SmallVector<Entry, 64> List;
146 for (const Use &U : V->uses()) 176 for (const Use &U : V->uses())
147 // Check if this user will be serialized. 177 // Check if this user will be serialized.
148 if (OM.lookup(U.getUser()).first) 178 if (OM.lookup(U.getUser()).first)
149 List.push_back(std::make_pair(&U, List.size())); 179 List.push_back(std::make_pair(&U, List.size()));
312 EnumerateValue(&GIF); 342 EnumerateValue(&GIF);
313 343
314 // Remember what is the cutoff between globalvalue's and other constants. 344 // Remember what is the cutoff between globalvalue's and other constants.
315 unsigned FirstConstant = Values.size(); 345 unsigned FirstConstant = Values.size();
316 346
317 // Enumerate the global variable initializers. 347 // Enumerate the global variable initializers and attributes.
318 for (const GlobalVariable &GV : M.globals()) 348 for (const GlobalVariable &GV : M.globals()) {
319 if (GV.hasInitializer()) 349 if (GV.hasInitializer())
320 EnumerateValue(GV.getInitializer()); 350 EnumerateValue(GV.getInitializer());
351 if (GV.hasAttributes())
352 EnumerateAttributes(GV.getAttributesAsList(AttributeList::FunctionIndex));
353 }
321 354
322 // Enumerate the aliasees. 355 // Enumerate the aliasees.
323 for (const GlobalAlias &GA : M.aliases()) 356 for (const GlobalAlias &GA : M.aliases())
324 EnumerateValue(GA.getAliasee()); 357 EnumerateValue(GA.getAliasee());
325 358
430 ValueMapType::const_iterator I = ValueMap.find(V); 463 ValueMapType::const_iterator I = ValueMap.find(V);
431 assert(I != ValueMap.end() && "Value not in slotcalculator!"); 464 assert(I != ValueMap.end() && "Value not in slotcalculator!");
432 return I->second-1; 465 return I->second-1;
433 } 466 }
434 467
468 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
435 LLVM_DUMP_METHOD void ValueEnumerator::dump() const { 469 LLVM_DUMP_METHOD void ValueEnumerator::dump() const {
436 print(dbgs(), ValueMap, "Default"); 470 print(dbgs(), ValueMap, "Default");
437 dbgs() << '\n'; 471 dbgs() << '\n';
438 print(dbgs(), MetadataMap, "MetaData"); 472 print(dbgs(), MetadataMap, "MetaData");
439 dbgs() << '\n'; 473 dbgs() << '\n';
440 } 474 }
475 #endif
441 476
442 void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map, 477 void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
443 const char *Name) const { 478 const char *Name) const {
444
445 OS << "Map Name: " << Name << "\n"; 479 OS << "Map Name: " << Name << "\n";
446 OS << "Size: " << Map.size() << "\n"; 480 OS << "Size: " << Map.size() << "\n";
447 for (ValueMapType::const_iterator I = Map.begin(), 481 for (ValueMapType::const_iterator I = Map.begin(),
448 E = Map.end(); I != E; ++I) { 482 E = Map.end(); I != E; ++I) {
449
450 const Value *V = I->first; 483 const Value *V = I->first;
451 if (V->hasName()) 484 if (V->hasName())
452 OS << "Value: " << V->getName(); 485 OS << "Value: " << V->getName();
453 else 486 else
454 OS << "Value: [null]\n"; 487 OS << "Value: [null]\n";
455 V->dump(); 488 V->print(errs());
489 errs() << '\n';
456 490
457 OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):"; 491 OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
458 for (const Use &U : V->uses()) { 492 for (const Use &U : V->uses()) {
459 if (&U != &*V->use_begin()) 493 if (&U != &*V->use_begin())
460 OS << ","; 494 OS << ",";
468 } 502 }
469 } 503 }
470 504
471 void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map, 505 void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map,
472 const char *Name) const { 506 const char *Name) const {
473
474 OS << "Map Name: " << Name << "\n"; 507 OS << "Map Name: " << Name << "\n";
475 OS << "Size: " << Map.size() << "\n"; 508 OS << "Size: " << Map.size() << "\n";
476 for (auto I = Map.begin(), E = Map.end(); I != E; ++I) { 509 for (auto I = Map.begin(), E = Map.end(); I != E; ++I) {
477 const Metadata *MD = I->first; 510 const Metadata *MD = I->first;
478 OS << "Metadata: slot = " << I->second.ID << "\n"; 511 OS << "Metadata: slot = " << I->second.ID << "\n";
510 // Rebuild the modified portion of ValueMap. 543 // Rebuild the modified portion of ValueMap.
511 for (; CstStart != CstEnd; ++CstStart) 544 for (; CstStart != CstEnd; ++CstStart)
512 ValueMap[Values[CstStart].first] = CstStart+1; 545 ValueMap[Values[CstStart].first] = CstStart+1;
513 } 546 }
514 547
515
516 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol 548 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
517 /// table into the values table. 549 /// table into the values table.
518 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) { 550 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
519 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end(); 551 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
520 VI != VE; ++VI) 552 VI != VE; ++VI)
547 } 579 }
548 580
549 void ValueEnumerator::dropFunctionFromMetadata( 581 void ValueEnumerator::dropFunctionFromMetadata(
550 MetadataMapType::value_type &FirstMD) { 582 MetadataMapType::value_type &FirstMD) {
551 SmallVector<const MDNode *, 64> Worklist; 583 SmallVector<const MDNode *, 64> Worklist;
552 auto push = [this, &Worklist](MetadataMapType::value_type &MD) { 584 auto push = [&Worklist](MetadataMapType::value_type &MD) {
553 auto &Entry = MD.second; 585 auto &Entry = MD.second;
554 586
555 // Nothing to do if this metadata isn't tagged. 587 // Nothing to do if this metadata isn't tagged.
556 if (!Entry.F) 588 if (!Entry.F)
557 return; 589 return;
882 914
883 EnumerateOperandType(Op); 915 EnumerateOperandType(Op);
884 } 916 }
885 } 917 }
886 918
887 void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) { 919 void ValueEnumerator::EnumerateAttributes(AttributeList PAL) {
888 if (PAL.isEmpty()) return; // null is always 0. 920 if (PAL.isEmpty()) return; // null is always 0.
889 921
890 // Do a lookup. 922 // Do a lookup.
891 unsigned &Entry = AttributeMap[PAL]; 923 unsigned &Entry = AttributeListMap[PAL];
892 if (Entry == 0) { 924 if (Entry == 0) {
893 // Never saw this before, add it. 925 // Never saw this before, add it.
894 Attribute.push_back(PAL); 926 AttributeLists.push_back(PAL);
895 Entry = Attribute.size(); 927 Entry = AttributeLists.size();
896 } 928 }
897 929
898 // Do lookups for all attribute groups. 930 // Do lookups for all attribute groups.
899 for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) { 931 for (unsigned i = PAL.index_begin(), e = PAL.index_end(); i != e; ++i) {
900 AttributeSet AS = PAL.getSlotAttributes(i); 932 AttributeSet AS = PAL.getAttributes(i);
901 unsigned &Entry = AttributeGroupMap[AS]; 933 if (!AS.hasAttributes())
934 continue;
935 IndexAndAttrSet Pair = {i, AS};
936 unsigned &Entry = AttributeGroupMap[Pair];
902 if (Entry == 0) { 937 if (Entry == 0) {
903 AttributeGroups.push_back(AS); 938 AttributeGroups.push_back(Pair);
904 Entry = AttributeGroups.size(); 939 Entry = AttributeGroups.size();
905 } 940 }
906 } 941 }
907 } 942 }
908 943