166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===- Local.cpp - Unit tests for Local -----------------------------------===//
266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//                     The LLVM Compiler Infrastructure
466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// This file is distributed under the University of Illinois Open Source
666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// License. See LICENSE.TXT for details.
766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===----------------------------------------------------------------------===//
966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "gtest/gtest.h"
1166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/BasicBlock.h"
1266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Instructions.h"
1366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/LLVMContext.h"
1466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Support/IRBuilder.h"
1566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Transforms/Utils/Local.h"
1666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanusing namespace llvm;
1866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1966b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(Local, RecursivelyDeleteDeadPHINodes) {
2066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  LLVMContext &C(getGlobalContext());
2166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  IRBuilder<> builder(C);
2366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Make blocks
2566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  BasicBlock *bb0 = BasicBlock::Create(C);
2666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  BasicBlock *bb1 = BasicBlock::Create(C);
2766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  builder.SetInsertPoint(bb0);
2966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  PHINode    *phi = builder.CreatePHI(Type::getInt32Ty(C), 2);
3066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  BranchInst *br0 = builder.CreateCondBr(builder.getTrue(), bb0, bb1);
3166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
3266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  builder.SetInsertPoint(bb1);
3366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  BranchInst *br1 = builder.CreateBr(bb0);
3466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
3566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  phi->addIncoming(phi, bb0);
3666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  phi->addIncoming(phi, bb1);
3766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
3866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // The PHI will be removed
3966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
4066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
4166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Make sure the blocks only contain the branches
4266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ(&bb0->front(), br0);
4366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_EQ(&bb1->front(), br1);
4466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
4566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  builder.SetInsertPoint(bb0);
4666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  phi = builder.CreatePHI(Type::getInt32Ty(C), 0);
4766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
4866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
4966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  builder.SetInsertPoint(bb0);
5166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  phi = builder.CreatePHI(Type::getInt32Ty(C), 0);
5266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  builder.CreateAdd(phi, phi);
5366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
5566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  bb0->dropAllReferences();
5766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  bb1->dropAllReferences();
5866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  delete bb0;
5966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  delete bb1;
6066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
61