llvm.org GIT mirror llvm / f3924f8
Merge from mainline. git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_20@37216 91177308-0d34-0410-b5e6-96231b3b80d8 Tanya Lattner 12 years ago
2 changed file(s) with 94 addition(s) and 45 deletion(s). Raw diff Collapse all Expand all
2323 #include "llvm/Support/MemoryBuffer.h"
2424 using namespace llvm;
2525
26 BitcodeReader::~BitcodeReader() {
26 void BitcodeReader::FreeState() {
2727 delete Buffer;
28 Buffer = 0;
29 std::vector().swap(TypeList);
30 ValueList.clear();
31 std::vector().swap(ParamAttrs);
32 std::vector().swap(FunctionBBs);
33 std::vector().swap(FunctionsWithBodies);
34 DeferredFunctionInfo.clear();
2835 }
2936
3037 //===----------------------------------------------------------------------===//
11011108 }
11021109
11031110
1104 bool BitcodeReader::materializeFunction(Function *F, std::string *ErrInfo) {
1105 // If it already is material, ignore the request.
1106 if (!F->hasNotBeenReadFromBytecode()) return false;
1107
1108 DenseMap >::iterator DFII =
1109 DeferredFunctionInfo.find(F);
1110 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
1111
1112 // Move the bit stream to the saved position of the deferred function body and
1113 // restore the real linkage type for the function.
1114 Stream.JumpToBit(DFII->second.first);
1115 F->setLinkage((GlobalValue::LinkageTypes)DFII->second.second);
1116 DeferredFunctionInfo.erase(DFII);
1117
1118 if (ParseFunctionBody(F)) {
1119 if (ErrInfo) *ErrInfo = ErrorString;
1120 return true;
1121 }
1122
1123 return false;
1124 }
1125
1126 Module *BitcodeReader::materializeModule(std::string *ErrInfo) {
1127 DenseMap >::iterator I =
1128 DeferredFunctionInfo.begin();
1129 while (!DeferredFunctionInfo.empty()) {
1130 Function *F = (*I++).first;
1131 assert(F->hasNotBeenReadFromBytecode() &&
1132 "Deserialized function found in map!");
1133 if (materializeFunction(F, ErrInfo))
1134 return 0;
1135 }
1136 return TheModule;
1137 }
1138
1139
11401111 /// ParseFunctionBody - Lazily parse the specified function body block.
11411112 bool BitcodeReader::ParseFunctionBody(Function *F) {
11421113 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
15851556 return false;
15861557 }
15871558
1559 //===----------------------------------------------------------------------===//
1560 // ModuleProvider implementation
1561 //===----------------------------------------------------------------------===//
1562
1563
1564 bool BitcodeReader::materializeFunction(Function *F, std::string *ErrInfo) {
1565 // If it already is material, ignore the request.
1566 if (!F->hasNotBeenReadFromBytecode()) return false;
1567
1568 DenseMap >::iterator DFII =
1569 DeferredFunctionInfo.find(F);
1570 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
1571
1572 // Move the bit stream to the saved position of the deferred function body and
1573 // restore the real linkage type for the function.
1574 Stream.JumpToBit(DFII->second.first);
1575 F->setLinkage((GlobalValue::LinkageTypes)DFII->second.second);
1576
1577 if (ParseFunctionBody(F)) {
1578 if (ErrInfo) *ErrInfo = ErrorString;
1579 return true;
1580 }
1581
1582 return false;
1583 }
1584
1585 void BitcodeReader::dematerializeFunction(Function *F) {
1586 // If this function isn't materialized, or if it is a proto, this is a noop.
1587 if (F->hasNotBeenReadFromBytecode() || F->isDeclaration())
1588 return;
1589
1590 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
1591
1592 // Just forget the function body, we can remat it later.
1593 F->deleteBody();
1594 F->setLinkage(GlobalValue::GhostLinkage);
1595 }
1596
1597
1598 Module *BitcodeReader::materializeModule(std::string *ErrInfo) {
1599 for (DenseMap >::iterator I =
1600 DeferredFunctionInfo.begin(), E = DeferredFunctionInfo.end(); I != E;
1601 ++I) {
1602 Function *F = I->first;
1603 if (F->hasNotBeenReadFromBytecode() &&
1604 materializeFunction(F, ErrInfo))
1605 return 0;
1606 }
1607 return TheModule;
1608 }
1609
1610
1611 /// This method is provided by the parent ModuleProvde class and overriden
1612 /// here. It simply releases the module from its provided and frees up our
1613 /// state.
1614 /// @brief Release our hold on the generated module
1615 Module *BitcodeReader::releaseModule(std::string *ErrInfo) {
1616 // Since we're losing control of this Module, we must hand it back complete
1617 Module *M = ModuleProvider::releaseModule(ErrInfo);
1618 FreeState();
1619 return M;
1620 }
1621
15881622
15891623 //===----------------------------------------------------------------------===//
15901624 // External interface
16141648 R = static_cast(getBitcodeModuleProvider(Buffer, ErrMsg));
16151649 if (!R) return 0;
16161650
1617 // Read the whole module, get a pointer to it, tell ModuleProvider not to
1618 // delete it when its dtor is run.
1619 Module *M = R->releaseModule(ErrMsg);
1620
1621 // Don't let the BitcodeReader dtor delete 'Buffer'.
1651 // Read in the entire module.
1652 Module *M = R->materializeModule(ErrMsg);
1653
1654 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
1655 // there was an error.
16221656 R->releaseMemoryBuffer();
1657
1658 // If there was no error, tell ModuleProvider not to delete it when its dtor
1659 // is run.
1660 if (M)
1661 M = R->releaseModule(ErrMsg);
1662
16231663 delete R;
16241664 return M;
16251665 }
3636 Uses.push_back(Use(V, this));
3737 OperandList = &Uses[0];
3838 ++NumOperands;
39 }
40
41 void clear() {
42 std::vector().swap(Uses);
3943 }
4044
4145 Value *operator[](unsigned i) const { return getOperand(i); }
110114 BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
111115 HasReversedFunctionsWithBodies = false;
112116 }
113 ~BitcodeReader();
114
117 ~BitcodeReader() {
118 FreeState();
119 }
120
121 void FreeState();
115122
116123 /// releaseMemoryBuffer - This causes the reader to completely forget about
117124 /// the memory buffer it contains, which prevents the buffer from being
122129
123130 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
124131 virtual Module *materializeModule(std::string *ErrInfo = 0);
125
132 virtual void dematerializeFunction(Function *F);
133 virtual Module *releaseModule(std::string *ErrInfo = 0);
134
126135 bool Error(const char *Str) {
127136 ErrorString = Str;
128137 return true;