Nick Lewycky | c8a1569 | 2011-02-20 08:38:20 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 11 | #include "llvm/IR/BasicBlock.h" |
| 12 | #include "llvm/IR/IRBuilder.h" |
| 13 | #include "llvm/IR/Instructions.h" |
| 14 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
| 16 | |
Nick Lewycky | c8a1569 | 2011-02-20 08:38:20 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| 19 | TEST(Local, RecursivelyDeleteDeadPHINodes) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame^] | 20 | LLVMContext C; |
Nick Lewycky | c8a1569 | 2011-02-20 08:38:20 +0000 | [diff] [blame] | 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); |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 29 | PHINode *phi = builder.CreatePHI(Type::getInt32Ty(C), 2); |
Nick Lewycky | c8a1569 | 2011-02-20 08:38:20 +0000 | [diff] [blame] | 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 | |
Nick Lewycky | 183c24c | 2011-02-20 18:05:56 +0000 | [diff] [blame] | 45 | builder.SetInsertPoint(bb0); |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 46 | phi = builder.CreatePHI(Type::getInt32Ty(C), 0); |
Nick Lewycky | 183c24c | 2011-02-20 18:05:56 +0000 | [diff] [blame] | 47 | |
| 48 | EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi)); |
| 49 | |
Duncan Sands | 6dcd49b | 2011-02-21 16:27:36 +0000 | [diff] [blame] | 50 | builder.SetInsertPoint(bb0); |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 51 | phi = builder.CreatePHI(Type::getInt32Ty(C), 0); |
Duncan Sands | 6dcd49b | 2011-02-21 16:27:36 +0000 | [diff] [blame] | 52 | builder.CreateAdd(phi, phi); |
| 53 | |
| 54 | EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi)); |
| 55 | |
Nick Lewycky | c8a1569 | 2011-02-20 08:38:20 +0000 | [diff] [blame] | 56 | bb0->dropAllReferences(); |
| 57 | bb1->dropAllReferences(); |
| 58 | delete bb0; |
| 59 | delete bb1; |
| 60 | } |
Benjamin Kramer | f175e04 | 2015-09-02 19:52:23 +0000 | [diff] [blame] | 61 | |
| 62 | TEST(Local, RemoveDuplicatePHINodes) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame^] | 63 | LLVMContext C; |
Benjamin Kramer | f175e04 | 2015-09-02 19:52:23 +0000 | [diff] [blame] | 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 | } |