llvm.org GIT mirror llvm / 965b15e
[TBAA] Don't generate invalid TBAA when merging nodes Summary: Fix a corner case in `MDNode::getMostGenericTBAA` where we can sometimes generate invalid TBAA metadata. Reviewers: chandlerc, hfinkel, mehdi_amini, manmanren Subscribers: mcrosier, llvm-commits Differential Revision: https://reviews.llvm.org/D26635 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@289403 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 2 years ago
2 changed file(s) with 39 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
456456 --IB;
457457 }
458458
459 if (!Ret)
459 // We either did not find a match, or the only common base "type" is
460 // the root node. In either case, we don't have any useful TBAA
461 // metadata to attach.
462 if (!Ret || Ret->getNumOperands() < 2)
460463 return nullptr;
464
461465 // We need to convert from a type node to a tag node.
462466 Type *Int64 = IntegerType::get(A->getContext(), 64);
463467 Metadata *Ops[3] = {Ret, Ret,
2121 namespace llvm {
2222 namespace {
2323
24 class OldTBAATest : public testing::Test {
24 class TBAATest : public testing::Test {
2525 protected:
26 OldTBAATest() : M("MixedTBAATest", C), MD(C) {}
26 TBAATest() : M("TBAATest", C), MD(C) {}
2727
2828 LLVMContext C;
2929 Module M;
3030 MDBuilder MD;
3131 };
3232
33 TEST_F(OldTBAATest, checkVerifierBehavior) {
34 // C++ unit test case to avoid going through the auto upgrade logic.
35
33 static StoreInst *getFunctionWithSingleStore(Module *M, StringRef Name) {
34 auto &C = M->getContext();
3635 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), {});
37 auto *F = cast(M.getOrInsertFunction("f", FTy));
36 auto *F = cast(M->getOrInsertFunction(Name, FTy));
3837 auto *BB = BasicBlock::Create(C, "entry", F);
3938 auto *IntType = Type::getInt32Ty(C);
4039 auto *PtrType = Type::getInt32PtrTy(C);
4241 ConstantPointerNull::get(PtrType), BB);
4342 ReturnInst::Create(C, nullptr, BB);
4443
44 return SI;
45 }
46
47 TEST_F(TBAATest, checkVerifierBehaviorForOldTBAA) {
48 auto *SI = getFunctionWithSingleStore(&M, "f1");
49 auto *F = SI->getFunction();
50
51 // C++ unit test case to avoid going through the auto upgrade logic.
4552 auto *RootMD = MD.createTBAARoot("Simple C/C++ TBAA");
4653 auto *MD1 = MD.createTBAANode("omnipotent char", RootMD);
4754 auto *MD2 = MD.createTBAANode("int", MD1);
5865 .startswith(ExpectedFailureMsg));
5966 }
6067
68 TEST_F(TBAATest, checkTBAAMerging) {
69 auto *SI = getFunctionWithSingleStore(&M, "f2");
70 auto *F = SI->getFunction();
71
72 auto *RootMD = MD.createTBAARoot("tbaa-root");
73 auto *MD1 = MD.createTBAANode("scalar-a", RootMD);
74 auto *StructTag1 = MD.createTBAAStructTagNode(MD1, MD1, 0);
75 auto *MD2 = MD.createTBAANode("scalar-b", RootMD);
76 auto *StructTag2 = MD.createTBAAStructTagNode(MD2, MD2, 0);
77
78 auto *GenericMD = MDNode::getMostGenericTBAA(StructTag1, StructTag2);
79
80 EXPECT_EQ(GenericMD, nullptr);
81
82 // Despite GenericMD being nullptr, we expect the setMetadata call to be well
83 // defined and produce a well-formed function.
84 SI->setMetadata(LLVMContext::MD_tbaa, GenericMD);
85
86 EXPECT_TRUE(!verifyFunction(*F));
87 }
88
6189 } // end anonymous namspace
6290 } // end llvm namespace