[ARM] Prefer indirect calls in minsize mode
[llvm-core.git] / unittests / IR / VerifierTest.cpp
blobc33c92a6f7c58d21df12500841696278fcbcdcda
1 //===- llvm/unittest/IR/VerifierTest.cpp - Verifier unit tests --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "llvm/IR/Constants.h"
11 #include "llvm/IR/DIBuilder.h"
12 #include "llvm/IR/DerivedTypes.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/GlobalAlias.h"
15 #include "llvm/IR/GlobalVariable.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/LegacyPassManager.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Verifier.h"
21 #include "gtest/gtest.h"
23 namespace llvm {
24 namespace {
26 TEST(VerifierTest, Branch_i1) {
27 LLVMContext C;
28 Module M("M", C);
29 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
30 Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
31 BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
32 BasicBlock *Exit = BasicBlock::Create(C, "exit", F);
33 ReturnInst::Create(C, Exit);
35 // To avoid triggering an assertion in BranchInst::Create, we first create
36 // a branch with an 'i1' condition ...
38 Constant *False = ConstantInt::getFalse(C);
39 BranchInst *BI = BranchInst::Create(Exit, Exit, False, Entry);
41 // ... then use setOperand to redirect it to a value of different type.
43 Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0);
44 BI->setOperand(0, Zero32);
46 EXPECT_TRUE(verifyFunction(*F));
49 TEST(VerifierTest, InvalidRetAttribute) {
50 LLVMContext C;
51 Module M("M", C);
52 FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false);
53 Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
54 AttributeSet AS = F->getAttributes();
55 F->setAttributes(AS.addAttribute(C, AttributeSet::ReturnIndex,
56 Attribute::UWTable));
58 std::string Error;
59 raw_string_ostream ErrorOS(Error);
60 EXPECT_TRUE(verifyModule(M, &ErrorOS));
61 EXPECT_TRUE(StringRef(ErrorOS.str()).startswith(
62 "Attribute 'uwtable' only applies to functions!"));
65 TEST(VerifierTest, CrossModuleRef) {
66 LLVMContext C;
67 Module M1("M1", C);
68 Module M2("M2", C);
69 Module M3("M3", C);
70 FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false);
71 Function *F1 = cast<Function>(M1.getOrInsertFunction("foo1", FTy));
72 Function *F2 = cast<Function>(M2.getOrInsertFunction("foo2", FTy));
73 Function *F3 = cast<Function>(M3.getOrInsertFunction("foo3", FTy));
75 BasicBlock *Entry1 = BasicBlock::Create(C, "entry", F1);
76 BasicBlock *Entry3 = BasicBlock::Create(C, "entry", F3);
78 // BAD: Referencing function in another module
79 CallInst::Create(F2,"call",Entry1);
81 // BAD: Referencing personality routine in another module
82 F3->setPersonalityFn(F2);
84 // Fill in the body
85 Constant *ConstZero = ConstantInt::get(Type::getInt32Ty(C), 0);
86 ReturnInst::Create(C, ConstZero, Entry1);
87 ReturnInst::Create(C, ConstZero, Entry3);
89 std::string Error;
90 raw_string_ostream ErrorOS(Error);
91 EXPECT_TRUE(verifyModule(M2, &ErrorOS));
92 EXPECT_TRUE(StringRef(ErrorOS.str())
93 .equals("Global is used by function in a different module\n"
94 "i32 ()* @foo2\n"
95 "; ModuleID = 'M2'\n"
96 "i32 ()* @foo3\n"
97 "; ModuleID = 'M3'\n"
98 "Global is referenced in a different module!\n"
99 "i32 ()* @foo2\n"
100 "; ModuleID = 'M2'\n"
101 " %call = call i32 @foo2()\n"
102 "i32 ()* @foo1\n"
103 "; ModuleID = 'M1'\n"));
105 Error.clear();
106 EXPECT_TRUE(verifyModule(M1, &ErrorOS));
107 EXPECT_TRUE(StringRef(ErrorOS.str()).equals(
108 "Referencing function in another module!\n"
109 " %call = call i32 @foo2()\n"
110 "; ModuleID = 'M1'\n"
111 "i32 ()* @foo2\n"
112 "; ModuleID = 'M2'\n"));
114 Error.clear();
115 EXPECT_TRUE(verifyModule(M3, &ErrorOS));
116 EXPECT_TRUE(StringRef(ErrorOS.str()).startswith(
117 "Referencing personality function in another module!"));
119 // Erase bad methods to avoid triggering an assertion failure on destruction
120 F1->eraseFromParent();
121 F3->eraseFromParent();
124 TEST(VerifierTest, CrossModuleMetadataRef) {
125 LLVMContext C;
126 Module M1("M1", C);
127 Module M2("M2", C);
128 GlobalVariable *newGV =
129 new GlobalVariable(M1, Type::getInt8Ty(C), false,
130 GlobalVariable::ExternalLinkage, nullptr,
131 "Some Global");
133 DIBuilder dbuilder(M2);
134 auto CU = dbuilder.createCompileUnit(dwarf::DW_LANG_Julia, "test.jl", ".",
135 "unittest", false, "", 0);
136 auto File = dbuilder.createFile("test.jl", ".");
137 auto Ty = dbuilder.createBasicType("Int8", 8, 8, dwarf::DW_ATE_signed);
138 dbuilder.createGlobalVariable(CU, "_SOME_GLOBAL", "_SOME_GLOBAL", File, 1, Ty,
139 false, newGV);
140 dbuilder.finalize();
142 std::string Error;
143 raw_string_ostream ErrorOS(Error);
144 EXPECT_TRUE(verifyModule(M2, &ErrorOS));
145 EXPECT_TRUE(StringRef(ErrorOS.str())
146 .startswith("Referencing global in another module!"));
149 TEST(VerifierTest, InvalidVariableLinkage) {
150 LLVMContext C;
151 Module M("M", C);
152 new GlobalVariable(M, Type::getInt8Ty(C), false,
153 GlobalValue::LinkOnceODRLinkage, nullptr, "Some Global");
154 std::string Error;
155 raw_string_ostream ErrorOS(Error);
156 EXPECT_TRUE(verifyModule(M, &ErrorOS));
157 EXPECT_TRUE(
158 StringRef(ErrorOS.str()).startswith("Global is external, but doesn't "
159 "have external or weak linkage!"));
162 TEST(VerifierTest, InvalidFunctionLinkage) {
163 LLVMContext C;
164 Module M("M", C);
166 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
167 Function::Create(FTy, GlobalValue::LinkOnceODRLinkage, "foo", &M);
168 std::string Error;
169 raw_string_ostream ErrorOS(Error);
170 EXPECT_TRUE(verifyModule(M, &ErrorOS));
171 EXPECT_TRUE(
172 StringRef(ErrorOS.str()).startswith("Global is external, but doesn't "
173 "have external or weak linkage!"));
176 #ifndef _MSC_VER
177 // FIXME: This test causes an ICE in MSVC 2013.
178 TEST(VerifierTest, StripInvalidDebugInfo) {
179 LLVMContext C;
180 Module M("M", C);
181 DIBuilder DIB(M);
182 DIB.createCompileUnit(dwarf::DW_LANG_C89, "broken.c", "/",
183 "unittest", false, "", 0);
184 DIB.finalize();
185 EXPECT_FALSE(verifyModule(M));
187 // Now break it.
188 auto *File = DIB.createFile("not-a-CU.f", ".");
189 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
190 NMD->addOperand(File);
191 EXPECT_TRUE(verifyModule(M));
193 ModulePassManager MPM(true);
194 MPM.addPass(VerifierPass(false));
195 ModuleAnalysisManager MAM(true);
196 MAM.registerPass([&] { return VerifierAnalysis(); });
197 MPM.run(M, MAM);
198 EXPECT_FALSE(verifyModule(M));
200 #endif
202 TEST(VerifierTest, StripInvalidDebugInfoLegacy) {
203 LLVMContext C;
204 Module M("M", C);
205 DIBuilder DIB(M);
206 DIB.createCompileUnit(dwarf::DW_LANG_C89, "broken.c", "/",
207 "unittest", false, "", 0);
208 DIB.finalize();
209 EXPECT_FALSE(verifyModule(M));
211 // Now break it.
212 auto *File = DIB.createFile("not-a-CU.f", ".");
213 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
214 NMD->addOperand(File);
215 EXPECT_TRUE(verifyModule(M));
217 legacy::PassManager Passes;
218 Passes.add(createVerifierPass(false));
219 Passes.run(M);
220 EXPECT_FALSE(verifyModule(M));
223 } // end anonymous namespace
224 } // end namespace llvm