1 //===- Local.cpp - Unit tests for Local -----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/BasicBlock.h"
11 #include "llvm/IRBuilder.h"
12 #include "llvm/Instructions.h"
13 #include "llvm/LLVMContext.h"
14 #include "llvm/Transforms/Utils/Local.h"
16 #include "gtest/gtest.h"
20 TEST(Local, RecursivelyDeleteDeadPHINodes) {
21 LLVMContext &C(getGlobalContext());
23 IRBuilder<> builder(C);
26 BasicBlock *bb0 = BasicBlock::Create(C);
27 BasicBlock *bb1 = BasicBlock::Create(C);
29 builder.SetInsertPoint(bb0);
30 PHINode *phi = builder.CreatePHI(Type::getInt32Ty(C), 2);
31 BranchInst *br0 = builder.CreateCondBr(builder.getTrue(), bb0, bb1);
33 builder.SetInsertPoint(bb1);
34 BranchInst *br1 = builder.CreateBr(bb0);
36 phi->addIncoming(phi, bb0);
37 phi->addIncoming(phi, bb1);
39 // The PHI will be removed
40 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
42 // Make sure the blocks only contain the branches
43 EXPECT_EQ(&bb0->front(), br0);
44 EXPECT_EQ(&bb1->front(), br1);
46 builder.SetInsertPoint(bb0);
47 phi = builder.CreatePHI(Type::getInt32Ty(C), 0);
49 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
51 builder.SetInsertPoint(bb0);
52 phi = builder.CreatePHI(Type::getInt32Ty(C), 0);
53 builder.CreateAdd(phi, phi);
55 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
57 bb0->dropAllReferences();
58 bb1->dropAllReferences();