#include "llvm/Transforms/Utils/SSAUpdaterBulk.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "gtest/gtest.h"
using namespace llvm;
TEST(SSAUpdaterBulk, SimpleMerge) {
SSAUpdaterBulk Updater;
LLVMContext C;
Module M("SSAUpdaterTest", C);
IRBuilder<> B(C);
Type *I32Ty = B.getInt32Ty();
auto *F = Function::Create(FunctionType::get(B.getVoidTy(), {I32Ty}, false),
GlobalValue::ExternalLinkage, "F", &M);
Argument *FirstArg = &*(F->arg_begin());
BasicBlock *IfBB = BasicBlock::Create(C, "if", F);
BasicBlock *TrueBB = BasicBlock::Create(C, "true", F);
BasicBlock *FalseBB = BasicBlock::Create(C, "false", F);
BasicBlock *MergeBB = BasicBlock::Create(C, "merge", F);
B.SetInsertPoint(IfBB);
B.CreateCondBr(B.getTrue(), TrueBB, FalseBB);
B.SetInsertPoint(TrueBB);
Value *AddOp1 = B.CreateAdd(FirstArg, ConstantInt::get(I32Ty, 1));
Value *SubOp1 = B.CreateSub(FirstArg, ConstantInt::get(I32Ty, 2));
B.CreateBr(MergeBB);
B.SetInsertPoint(FalseBB);
Value *AddOp2 = B.CreateAdd(FirstArg, ConstantInt::get(I32Ty, 3));
Value *SubOp2 = B.CreateSub(FirstArg, ConstantInt::get(I32Ty, 4));
B.CreateBr(MergeBB);
B.SetInsertPoint(MergeBB, MergeBB->begin());
auto *I1 = cast<Instruction>(B.CreateAdd(AddOp1, ConstantInt::get(I32Ty, 5)));
auto *I2 = cast<Instruction>(B.CreateAdd(AddOp2, ConstantInt::get(I32Ty, 6)));
auto *I3 = cast<Instruction>(B.CreateAdd(SubOp1, SubOp2));
auto *I4 = cast<Instruction>(B.CreateSub(SubOp1, SubOp2));
unsigned VarNum = Updater.AddVariable("a", I32Ty);
Updater.AddAvailableValue(VarNum, TrueBB, AddOp1);
Updater.AddAvailableValue(VarNum, FalseBB, AddOp2);
Updater.AddUse(VarNum, &I1->getOperandUse(0));
Updater.AddUse(VarNum, &I2->getOperandUse(0));
VarNum = Updater.AddVariable("b", I32Ty);
Updater.AddAvailableValue(VarNum, TrueBB, SubOp1);
Updater.AddAvailableValue(VarNum, FalseBB, SubOp2);
Updater.AddUse(VarNum, &I3->getOperandUse(0));
Updater.AddUse(VarNum, &I3->getOperandUse(1));
DominatorTree DT(*F);
Updater.RewriteAllUses(&DT);
PHINode *UpdatePhiA = dyn_cast_or_null<PHINode>(I1->getOperand(0));
EXPECT_NE(UpdatePhiA, nullptr);
EXPECT_EQ(UpdatePhiA->getIncomingValueForBlock(TrueBB), AddOp1);
EXPECT_EQ(UpdatePhiA->getIncomingValueForBlock(FalseBB), AddOp2);
EXPECT_EQ(UpdatePhiA, dyn_cast_or_null<PHINode>(I1->getOperand(0)));
PHINode *UpdatePhiB = dyn_cast_or_null<PHINode>(I3->getOperand(0));
EXPECT_EQ(UpdatePhiB->getIncomingValueForBlock(TrueBB), SubOp1);
EXPECT_EQ(UpdatePhiB->getIncomingValueForBlock(FalseBB), SubOp2);
EXPECT_EQ(UpdatePhiB, dyn_cast_or_null<PHINode>(I3->getOperand(1)));
EXPECT_EQ(I4->getOperand(0), SubOp1);
EXPECT_EQ(I4->getOperand(1), SubOp2);
}
TEST(SSAUpdaterBulk, Irreducible) {
SSAUpdaterBulk Updater;
LLVMContext C;
Module M("SSAUpdaterTest", C);
IRBuilder<> B(C);
Type *I32Ty = B.getInt32Ty();
auto *F = Function::Create(FunctionType::get(B.getVoidTy(), {I32Ty}, false),
GlobalValue::ExternalLinkage, "F", &M);
Argument *FirstArg = &*F->arg_begin();
BasicBlock *IfBB = BasicBlock::Create(C, "if", F);
BasicBlock *LoopStartBB = BasicBlock::Create(C, "loopstart", F);
BasicBlock *LoopMainBB = BasicBlock::Create(C, "loopmain", F);
BasicBlock *AfterLoopBB = BasicBlock::Create(C, "afterloop", F);
B.SetInsertPoint(IfBB);
Value *AddOp1 = B.CreateAdd(FirstArg, ConstantInt::get(I32Ty, 1));
B.CreateCondBr(B.getTrue(), LoopMainBB, LoopStartBB);
B.SetInsertPoint(LoopStartBB);
Value *AddOp2 = B.CreateAdd(FirstArg, ConstantInt::get(I32Ty, 2));
B.CreateBr(LoopMainBB);
B.SetInsertPoint(LoopMainBB);
auto *I1 = cast<Instruction>(B.CreateAdd(AddOp1, ConstantInt::get(I32Ty, 3)));
B.CreateCondBr(B.getTrue(), LoopStartBB, AfterLoopBB);
B.SetInsertPoint(AfterLoopBB);
auto *I2 = cast<Instruction>(B.CreateAdd(AddOp2, ConstantInt::get(I32Ty, 4)));
ReturnInst *Return = B.CreateRet(FirstArg);
unsigned VarNum = Updater.AddVariable("c", I32Ty);
Updater.AddAvailableValue(VarNum, IfBB, AddOp1);
Updater.AddUse(VarNum, &I1->getOperandUse(0));
VarNum = Updater.AddVariable("b", I32Ty);
Updater.AddAvailableValue(VarNum, LoopStartBB, AddOp2);
Updater.AddUse(VarNum, &I2->getOperandUse(0));
VarNum = Updater.AddVariable("a", I32Ty);
Updater.AddAvailableValue(VarNum, &F->getEntryBlock(), FirstArg);
Updater.AddUse(VarNum, &Return->getOperandUse(0));
SmallVector<PHINode *, 8> Inserted;
DominatorTree DT(*F);
Updater.RewriteAllUses(&DT, &Inserted);
EXPECT_EQ(Inserted.size(), 1u);
EXPECT_EQ(I1->getOperand(0), AddOp1);
EXPECT_EQ(Return->getOperand(0), FirstArg);
PHINode *UpdatePhi = dyn_cast_or_null<PHINode>(I2->getOperand(0));
EXPECT_NE(UpdatePhi, nullptr);
EXPECT_EQ(UpdatePhi->getIncomingValueForBlock(LoopStartBB), AddOp2);
EXPECT_EQ(UpdatePhi->getIncomingValueForBlock(IfBB), UndefValue::get(I32Ty));
}