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