1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
//===- SSAUpdaterBulk.cpp - Unit tests for SSAUpdaterBulk -----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#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);
// Generate a simple program:
// if:
// br i1 true, label %true, label %false
// true:
// %1 = add i32 %0, 1
// %2 = sub i32 %0, 2
// br label %merge
// false:
// %3 = add i32 %0, 3
// %4 = sub i32 %0, 4
// br label %merge
// merge:
// %5 = add i32 %1, 5
// %6 = add i32 %3, 6
// %7 = add i32 %2, %4
// %8 = sub i32 %2, %4
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));
// Now rewrite uses in instructions %5, %6, %7. They need to use a phi, which
// SSAUpdater should insert into %merge.
// Intentionally don't touch %8 to see that SSAUpdater only changes
// instructions that were explicitly specified.
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);
// Check how %5 and %6 were rewritten.
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)));
// Check how %7 was rewritten.
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)));
// Check that %8 was kept untouched.
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);
// Generate a small program with a multi-entry loop:
// if:
// %1 = add i32 %0, 1
// br i1 true, label %loopmain, label %loopstart
//
// loopstart:
// %2 = add i32 %0, 2
// br label %loopmain
//
// loopmain:
// %3 = add i32 %1, 3
// br i1 true, label %loopstart, label %afterloop
//
// afterloop:
// %4 = add i32 %2, 4
// ret i32 %0
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);
// Now rewrite uses in instructions %3, %4, and 'ret i32 %0'. Only %4 needs a
// new phi, others should be able to work with existing values.
// The phi for %4 should be inserted into LoopMainBB and should look like
// this:
// %b = phi i32 [ %2, %loopstart ], [ undef, %if ]
// No other rewrites should be made.
// Add use in %3.
unsigned VarNum = Updater.AddVariable("c", I32Ty);
Updater.AddAvailableValue(VarNum, IfBB, AddOp1);
Updater.AddUse(VarNum, &I1->getOperandUse(0));
// Add use in %4.
VarNum = Updater.AddVariable("b", I32Ty);
Updater.AddAvailableValue(VarNum, LoopStartBB, AddOp2);
Updater.AddUse(VarNum, &I2->getOperandUse(0));
// Add use in the return instruction.
VarNum = Updater.AddVariable("a", I32Ty);
Updater.AddAvailableValue(VarNum, &F->getEntryBlock(), FirstArg);
Updater.AddUse(VarNum, &Return->getOperandUse(0));
// Save all inserted phis into a vector.
SmallVector<PHINode *, 8> Inserted;
DominatorTree DT(*F);
Updater.RewriteAllUses(&DT, &Inserted);
// Only one phi should have been inserted.
EXPECT_EQ(Inserted.size(), 1u);
// I1 and Return should use the same values as they used before.
EXPECT_EQ(I1->getOperand(0), AddOp1);
EXPECT_EQ(Return->getOperand(0), FirstArg);
// I2 should use the new phi.
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));
}
|