• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- Local.cpp - Unit tests for Local -----------------------------------===//
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 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Transforms/Utils/Local.h"
11 #include "llvm/IR/BasicBlock.h"
12 #include "llvm/IR/IRBuilder.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "gtest/gtest.h"
16 
17 using namespace llvm;
18 
TEST(Local,RecursivelyDeleteDeadPHINodes)19 TEST(Local, RecursivelyDeleteDeadPHINodes) {
20   LLVMContext &C(getGlobalContext());
21 
22   IRBuilder<> builder(C);
23 
24   // Make blocks
25   BasicBlock *bb0 = BasicBlock::Create(C);
26   BasicBlock *bb1 = BasicBlock::Create(C);
27 
28   builder.SetInsertPoint(bb0);
29   PHINode    *phi = builder.CreatePHI(Type::getInt32Ty(C), 2);
30   BranchInst *br0 = builder.CreateCondBr(builder.getTrue(), bb0, bb1);
31 
32   builder.SetInsertPoint(bb1);
33   BranchInst *br1 = builder.CreateBr(bb0);
34 
35   phi->addIncoming(phi, bb0);
36   phi->addIncoming(phi, bb1);
37 
38   // The PHI will be removed
39   EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
40 
41   // Make sure the blocks only contain the branches
42   EXPECT_EQ(&bb0->front(), br0);
43   EXPECT_EQ(&bb1->front(), br1);
44 
45   builder.SetInsertPoint(bb0);
46   phi = builder.CreatePHI(Type::getInt32Ty(C), 0);
47 
48   EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
49 
50   builder.SetInsertPoint(bb0);
51   phi = builder.CreatePHI(Type::getInt32Ty(C), 0);
52   builder.CreateAdd(phi, phi);
53 
54   EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
55 
56   bb0->dropAllReferences();
57   bb1->dropAllReferences();
58   delete bb0;
59   delete bb1;
60 }
61 
TEST(Local,RemoveDuplicatePHINodes)62 TEST(Local, RemoveDuplicatePHINodes) {
63   LLVMContext &C(getGlobalContext());
64   IRBuilder<> B(C);
65 
66   std::unique_ptr<Function> F(
67       Function::Create(FunctionType::get(B.getVoidTy(), false),
68                        GlobalValue::ExternalLinkage, "F"));
69   BasicBlock *Entry(BasicBlock::Create(C, "", F.get()));
70   BasicBlock *BB(BasicBlock::Create(C, "", F.get()));
71   BranchInst::Create(BB, Entry);
72 
73   B.SetInsertPoint(BB);
74 
75   AssertingVH<PHINode> P1 = B.CreatePHI(Type::getInt32Ty(C), 2);
76   P1->addIncoming(B.getInt32(42), Entry);
77 
78   PHINode *P2 = B.CreatePHI(Type::getInt32Ty(C), 2);
79   P2->addIncoming(B.getInt32(42), Entry);
80 
81   AssertingVH<PHINode> P3 = B.CreatePHI(Type::getInt32Ty(C), 2);
82   P3->addIncoming(B.getInt32(42), Entry);
83   P3->addIncoming(B.getInt32(23), BB);
84 
85   PHINode *P4 = B.CreatePHI(Type::getInt32Ty(C), 2);
86   P4->addIncoming(B.getInt32(42), Entry);
87   P4->addIncoming(B.getInt32(23), BB);
88 
89   P1->addIncoming(P3, BB);
90   P2->addIncoming(P4, BB);
91   BranchInst::Create(BB, BB);
92 
93   // Verify that we can eliminate PHIs that become duplicates after chaning PHIs
94   // downstream.
95   EXPECT_TRUE(EliminateDuplicatePHINodes(BB));
96   EXPECT_EQ(3U, BB->size());
97 }
98