1e517273eSChandler Carruth //===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===// 2e517273eSChandler Carruth // 3e517273eSChandler Carruth // The LLVM Compiler Infrastructure 4e517273eSChandler Carruth // 5e517273eSChandler Carruth // This file is distributed under the University of Illinois Open Source 6e517273eSChandler Carruth // License. See LICENSE.TXT for details. 7e517273eSChandler Carruth // 8e517273eSChandler Carruth //===----------------------------------------------------------------------===// 9e517273eSChandler Carruth 105fa5ecf8SDaniel Dunbar #include "llvm/ADT/SmallString.h" 11e517273eSChandler Carruth #include "llvm/Bitcode/BitstreamWriter.h" 12e517273eSChandler Carruth #include "llvm/Bitcode/ReaderWriter.h" 137a2990cfSDuncan P. N. Exon Smith #include "llvm/AsmParser/Parser.h" 149fb823bbSChandler Carruth #include "llvm/IR/Constants.h" 159fb823bbSChandler Carruth #include "llvm/IR/Instructions.h" 169fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h" 179fb823bbSChandler Carruth #include "llvm/IR/Module.h" 185ad5f15cSChandler Carruth #include "llvm/IR/Verifier.h" 197a2990cfSDuncan P. N. Exon Smith #include "llvm/Support/Debug.h" 20e517273eSChandler Carruth #include "llvm/Support/MemoryBuffer.h" 217a2990cfSDuncan P. N. Exon Smith #include "llvm/Support/SourceMgr.h" 22e517273eSChandler Carruth #include "gtest/gtest.h" 23e517273eSChandler Carruth 247a2990cfSDuncan P. N. Exon Smith using namespace llvm; 257a2990cfSDuncan P. N. Exon Smith 26e517273eSChandler Carruth namespace { 27e517273eSChandler Carruth 287a2990cfSDuncan P. N. Exon Smith std::unique_ptr<Module> parseAssembly(const char *Assembly) { 297a2990cfSDuncan P. N. Exon Smith SMDiagnostic Error; 3011c07d7eSRafael Espindola std::unique_ptr<Module> M = 3111c07d7eSRafael Espindola parseAssemblyString(Assembly, Error, getGlobalContext()); 32e517273eSChandler Carruth 337a2990cfSDuncan P. N. Exon Smith std::string ErrMsg; 347a2990cfSDuncan P. N. Exon Smith raw_string_ostream OS(ErrMsg); 357a2990cfSDuncan P. N. Exon Smith Error.print("", OS); 36e517273eSChandler Carruth 377a2990cfSDuncan P. N. Exon Smith // A failure here means that the test itself is buggy. 3811c07d7eSRafael Espindola if (!M) 397a2990cfSDuncan P. N. Exon Smith report_fatal_error(OS.str().c_str()); 40e517273eSChandler Carruth 417a2990cfSDuncan P. N. Exon Smith return M; 42e517273eSChandler Carruth } 43e517273eSChandler Carruth 447a2990cfSDuncan P. N. Exon Smith static void writeModuleToBuffer(std::unique_ptr<Module> Mod, 457a2990cfSDuncan P. N. Exon Smith SmallVectorImpl<char> &Buffer) { 465fa5ecf8SDaniel Dunbar raw_svector_ostream OS(Buffer); 47508baf79SNAKAMURA Takumi WriteBitcodeToFile(Mod.get(), OS); 48e517273eSChandler Carruth } 49e517273eSChandler Carruth 507a2990cfSDuncan P. N. Exon Smith static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context, 517a2990cfSDuncan P. N. Exon Smith SmallString<1024> &Mem, 527a2990cfSDuncan P. N. Exon Smith const char *Assembly) { 537a2990cfSDuncan P. N. Exon Smith writeModuleToBuffer(parseAssembly(Assembly), Mem); 543560ff2cSRafael Espindola std::unique_ptr<MemoryBuffer> Buffer = 553560ff2cSRafael Espindola MemoryBuffer::getMemBuffer(Mem.str(), "test", false); 5668812157SRafael Espindola ErrorOr<Module *> ModuleOrErr = 5768812157SRafael Espindola getLazyBitcodeModule(std::move(Buffer), Context); 587a2990cfSDuncan P. N. Exon Smith return std::unique_ptr<Module>(ModuleOrErr.get()); 59e517273eSChandler Carruth } 60e517273eSChandler Carruth 61*7480e4dbSPetar Jovanovic TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) { 62*7480e4dbSPetar Jovanovic SmallString<1024> Mem; 63*7480e4dbSPetar Jovanovic 64*7480e4dbSPetar Jovanovic LLVMContext Context; 65*7480e4dbSPetar Jovanovic std::unique_ptr<Module> M = getLazyModuleFromAssembly( 66*7480e4dbSPetar Jovanovic Context, Mem, "define internal i32 @func() {\n" 67*7480e4dbSPetar Jovanovic "ret i32 0\n" 68*7480e4dbSPetar Jovanovic "}\n"); 69*7480e4dbSPetar Jovanovic 70*7480e4dbSPetar Jovanovic EXPECT_FALSE(verifyModule(*M, &dbgs())); 71*7480e4dbSPetar Jovanovic 72*7480e4dbSPetar Jovanovic M->getFunction("func")->Materialize(); 73*7480e4dbSPetar Jovanovic EXPECT_FALSE(M->getFunction("func")->empty()); 74*7480e4dbSPetar Jovanovic EXPECT_TRUE(M->getFunction("func")->getLinkage() == 75*7480e4dbSPetar Jovanovic GlobalValue::InternalLinkage); 76*7480e4dbSPetar Jovanovic 77*7480e4dbSPetar Jovanovic // Check that the linkage type is preserved after dematerialization. 78*7480e4dbSPetar Jovanovic M->getFunction("func")->Dematerialize(); 79*7480e4dbSPetar Jovanovic EXPECT_TRUE(M->getFunction("func")->empty()); 80*7480e4dbSPetar Jovanovic EXPECT_TRUE(M->getFunction("func")->getLinkage() == 81*7480e4dbSPetar Jovanovic GlobalValue::InternalLinkage); 82*7480e4dbSPetar Jovanovic EXPECT_FALSE(verifyModule(*M, &dbgs())); 83*7480e4dbSPetar Jovanovic } 84*7480e4dbSPetar Jovanovic 857a2990cfSDuncan P. N. Exon Smith TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677 867a2990cfSDuncan P. N. Exon Smith SmallString<1024> Mem; 877a2990cfSDuncan P. N. Exon Smith 887a2990cfSDuncan P. N. Exon Smith LLVMContext Context; 897a2990cfSDuncan P. N. Exon Smith std::unique_ptr<Module> M = getLazyModuleFromAssembly( 907a2990cfSDuncan P. N. Exon Smith Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n" 917a2990cfSDuncan P. N. Exon Smith "define void @func() {\n" 927a2990cfSDuncan P. N. Exon Smith " unreachable\n" 937a2990cfSDuncan P. N. Exon Smith "bb:\n" 947a2990cfSDuncan P. N. Exon Smith " unreachable\n" 957a2990cfSDuncan P. N. Exon Smith "}\n"); 967a2990cfSDuncan P. N. Exon Smith EXPECT_FALSE(verifyModule(*M, &dbgs())); 97908d809bSDuncan P. N. Exon Smith 98908d809bSDuncan P. N. Exon Smith // Try (and fail) to dematerialize @func. 99908d809bSDuncan P. N. Exon Smith M->getFunction("func")->Dematerialize(); 100908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(M->getFunction("func")->empty()); 101908d809bSDuncan P. N. Exon Smith } 102908d809bSDuncan P. N. Exon Smith 103908d809bSDuncan P. N. Exon Smith TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) { 104908d809bSDuncan P. N. Exon Smith SmallString<1024> Mem; 105908d809bSDuncan P. N. Exon Smith 106908d809bSDuncan P. N. Exon Smith LLVMContext Context; 107908d809bSDuncan P. N. Exon Smith std::unique_ptr<Module> M = getLazyModuleFromAssembly( 108908d809bSDuncan P. N. Exon Smith Context, Mem, "define i8* @before() {\n" 109908d809bSDuncan P. N. Exon Smith " ret i8* blockaddress(@func, %bb)\n" 110908d809bSDuncan P. N. Exon Smith "}\n" 111908d809bSDuncan P. N. Exon Smith "define void @other() {\n" 112908d809bSDuncan P. N. Exon Smith " unreachable\n" 113908d809bSDuncan P. N. Exon Smith "}\n" 114908d809bSDuncan P. N. Exon Smith "define void @func() {\n" 115908d809bSDuncan P. N. Exon Smith " unreachable\n" 116908d809bSDuncan P. N. Exon Smith "bb:\n" 117908d809bSDuncan P. N. Exon Smith " unreachable\n" 118908d809bSDuncan P. N. Exon Smith "}\n"); 119908d809bSDuncan P. N. Exon Smith EXPECT_TRUE(M->getFunction("before")->empty()); 120908d809bSDuncan P. N. Exon Smith EXPECT_TRUE(M->getFunction("func")->empty()); 121908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(verifyModule(*M, &dbgs())); 122908d809bSDuncan P. N. Exon Smith 123908d809bSDuncan P. N. Exon Smith // Materialize @before, pulling in @func. 124908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(M->getFunction("before")->Materialize()); 125908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(M->getFunction("func")->empty()); 126908d809bSDuncan P. N. Exon Smith EXPECT_TRUE(M->getFunction("other")->empty()); 127908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(verifyModule(*M, &dbgs())); 128908d809bSDuncan P. N. Exon Smith 129908d809bSDuncan P. N. Exon Smith // Try (and fail) to dematerialize @func. 130908d809bSDuncan P. N. Exon Smith M->getFunction("func")->Dematerialize(); 131908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(M->getFunction("func")->empty()); 132908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(verifyModule(*M, &dbgs())); 133908d809bSDuncan P. N. Exon Smith } 134908d809bSDuncan P. N. Exon Smith 135908d809bSDuncan P. N. Exon Smith TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) { 136908d809bSDuncan P. N. Exon Smith SmallString<1024> Mem; 137908d809bSDuncan P. N. Exon Smith 138908d809bSDuncan P. N. Exon Smith LLVMContext Context; 139908d809bSDuncan P. N. Exon Smith std::unique_ptr<Module> M = getLazyModuleFromAssembly( 140908d809bSDuncan P. N. Exon Smith Context, Mem, "define void @func() {\n" 141908d809bSDuncan P. N. Exon Smith " unreachable\n" 142908d809bSDuncan P. N. Exon Smith "bb:\n" 143908d809bSDuncan P. N. Exon Smith " unreachable\n" 144908d809bSDuncan P. N. Exon Smith "}\n" 145908d809bSDuncan P. N. Exon Smith "define void @other() {\n" 146908d809bSDuncan P. N. Exon Smith " unreachable\n" 147908d809bSDuncan P. N. Exon Smith "}\n" 148908d809bSDuncan P. N. Exon Smith "define i8* @after() {\n" 149908d809bSDuncan P. N. Exon Smith " ret i8* blockaddress(@func, %bb)\n" 150908d809bSDuncan P. N. Exon Smith "}\n"); 151908d809bSDuncan P. N. Exon Smith EXPECT_TRUE(M->getFunction("after")->empty()); 152908d809bSDuncan P. N. Exon Smith EXPECT_TRUE(M->getFunction("func")->empty()); 153908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(verifyModule(*M, &dbgs())); 154908d809bSDuncan P. N. Exon Smith 155908d809bSDuncan P. N. Exon Smith // Materialize @after, pulling in @func. 156908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(M->getFunction("after")->Materialize()); 157908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(M->getFunction("func")->empty()); 158908d809bSDuncan P. N. Exon Smith EXPECT_TRUE(M->getFunction("other")->empty()); 159908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(verifyModule(*M, &dbgs())); 160908d809bSDuncan P. N. Exon Smith 161908d809bSDuncan P. N. Exon Smith // Try (and fail) to dematerialize @func. 162908d809bSDuncan P. N. Exon Smith M->getFunction("func")->Dematerialize(); 163908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(M->getFunction("func")->empty()); 164908d809bSDuncan P. N. Exon Smith EXPECT_FALSE(verifyModule(*M, &dbgs())); 165e517273eSChandler Carruth } 1667a2990cfSDuncan P. N. Exon Smith 1677a2990cfSDuncan P. N. Exon Smith } // end namespace 168