2017-05-26 11:10:00 +08:00
|
|
|
//===- llvm/unittest/IR/BasicBlockTest.cpp - BasicBlock unit tests --------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-05-26 11:10:00 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
[IR] Lazily number instructions for local dominance queries
Essentially, fold OrderedBasicBlock into BasicBlock, and make it
auto-invalidate the instruction ordering when new instructions are
added. Notably, we don't need to invalidate it when removing
instructions, which is helpful when a pass mostly delete dead
instructions rather than transforming them.
The downside is that Instruction grows from 56 bytes to 64 bytes. The
resulting LLVM code is substantially simpler and automatically handles
invalidation, which makes me think that this is the right speed and size
tradeoff.
The important change is in SymbolTableTraitsImpl.h, where the numbering
is invalidated. Everything else should be straightforward.
We probably want to implement a fancier re-numbering scheme so that
local updates don't invalidate the ordering, but I plan for that to be
future work, maybe for someone else.
Reviewed By: lattner, vsk, fhahn, dexonsmith
Differential Revision: https://reviews.llvm.org/D51664
2020-02-19 06:33:54 +08:00
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2017-05-26 11:10:00 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2020-12-08 10:57:20 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2017-05-26 11:10:00 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/NoFolder.h"
|
2020-12-08 10:57:20 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
[IR] Lazily number instructions for local dominance queries
Essentially, fold OrderedBasicBlock into BasicBlock, and make it
auto-invalidate the instruction ordering when new instructions are
added. Notably, we don't need to invalidate it when removing
instructions, which is helpful when a pass mostly delete dead
instructions rather than transforming them.
The downside is that Instruction grows from 56 bytes to 64 bytes. The
resulting LLVM code is substantially simpler and automatically handles
invalidation, which makes me think that this is the right speed and size
tradeoff.
The important change is in SymbolTableTraitsImpl.h, where the numbering
is invalidated. Everything else should be straightforward.
We probably want to implement a fancier re-numbering scheme so that
local updates don't invalidate the ordering, but I plan for that to be
future work, maybe for someone else.
Reviewed By: lattner, vsk, fhahn, dexonsmith
Differential Revision: https://reviews.llvm.org/D51664
2020-02-19 06:33:54 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2017-05-26 11:10:00 +08:00
|
|
|
#include "gmock/gmock-matchers.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(BasicBlockTest, PhiRange) {
|
|
|
|
LLVMContext Context;
|
|
|
|
|
|
|
|
// Create the main block.
|
|
|
|
std::unique_ptr<BasicBlock> BB(BasicBlock::Create(Context));
|
|
|
|
|
|
|
|
// Create some predecessors of it.
|
|
|
|
std::unique_ptr<BasicBlock> BB1(BasicBlock::Create(Context));
|
|
|
|
BranchInst::Create(BB.get(), BB1.get());
|
|
|
|
std::unique_ptr<BasicBlock> BB2(BasicBlock::Create(Context));
|
|
|
|
BranchInst::Create(BB.get(), BB2.get());
|
|
|
|
|
2017-12-30 03:25:53 +08:00
|
|
|
// Make sure this doesn't crash if there are no phis.
|
2021-03-19 22:43:36 +08:00
|
|
|
int PhiCount = 0;
|
2017-12-30 03:25:53 +08:00
|
|
|
for (auto &PN : BB->phis()) {
|
|
|
|
(void)PN;
|
2021-03-19 22:43:36 +08:00
|
|
|
PhiCount++;
|
2017-12-30 03:25:53 +08:00
|
|
|
}
|
2021-03-19 22:43:36 +08:00
|
|
|
ASSERT_EQ(PhiCount, 0) << "empty block should have no phis";
|
2017-12-30 03:25:53 +08:00
|
|
|
|
2017-05-26 11:10:00 +08:00
|
|
|
// Make it a cycle.
|
|
|
|
auto *BI = BranchInst::Create(BB.get(), BB.get());
|
|
|
|
|
|
|
|
// Now insert some PHI nodes.
|
|
|
|
auto *Int32Ty = Type::getInt32Ty(Context);
|
|
|
|
auto *P1 = PHINode::Create(Int32Ty, /*NumReservedValues*/ 3, "phi.1", BI);
|
|
|
|
auto *P2 = PHINode::Create(Int32Ty, /*NumReservedValues*/ 3, "phi.2", BI);
|
|
|
|
auto *P3 = PHINode::Create(Int32Ty, /*NumReservedValues*/ 3, "phi.3", BI);
|
|
|
|
|
|
|
|
// Some non-PHI nodes.
|
|
|
|
auto *Sum = BinaryOperator::CreateAdd(P1, P2, "sum", BI);
|
|
|
|
|
|
|
|
// Now wire up the incoming values that are interesting.
|
|
|
|
P1->addIncoming(P2, BB.get());
|
|
|
|
P2->addIncoming(P1, BB.get());
|
|
|
|
P3->addIncoming(Sum, BB.get());
|
|
|
|
|
|
|
|
// Finally, let's iterate them, which is the thing we're trying to test.
|
|
|
|
// We'll use this to wire up the rest of the incoming values.
|
|
|
|
for (auto &PN : BB->phis()) {
|
|
|
|
PN.addIncoming(UndefValue::get(Int32Ty), BB1.get());
|
|
|
|
PN.addIncoming(UndefValue::get(Int32Ty), BB2.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that we can use const iterators and generally that the iterators
|
|
|
|
// behave like iterators.
|
|
|
|
BasicBlock::const_phi_iterator CI;
|
2017-05-26 20:07:12 +08:00
|
|
|
CI = BB->phis().begin();
|
2017-05-26 11:10:00 +08:00
|
|
|
EXPECT_NE(CI, BB->phis().end());
|
|
|
|
|
2018-04-26 05:50:09 +08:00
|
|
|
// Test that filtering iterators work with basic blocks.
|
|
|
|
auto isPhi = [](Instruction &I) { return isa<PHINode>(&I); };
|
|
|
|
auto Phis = make_filter_range(*BB, isPhi);
|
|
|
|
auto ReversedPhis = reverse(make_filter_range(*BB, isPhi));
|
2018-05-17 07:20:42 +08:00
|
|
|
EXPECT_EQ(std::distance(Phis.begin(), Phis.end()), 3);
|
2018-04-26 05:50:09 +08:00
|
|
|
EXPECT_EQ(&*Phis.begin(), P1);
|
2018-05-17 07:20:42 +08:00
|
|
|
EXPECT_EQ(std::distance(ReversedPhis.begin(), ReversedPhis.end()), 3);
|
2018-04-26 05:50:09 +08:00
|
|
|
EXPECT_EQ(&*ReversedPhis.begin(), P3);
|
|
|
|
|
2017-05-26 11:10:00 +08:00
|
|
|
// And iterate a const range.
|
|
|
|
for (const auto &PN : const_cast<const BasicBlock *>(BB.get())->phis()) {
|
|
|
|
EXPECT_EQ(BB.get(), PN.getIncomingBlock(0));
|
|
|
|
EXPECT_EQ(BB1.get(), PN.getIncomingBlock(1));
|
|
|
|
EXPECT_EQ(BB2.get(), PN.getIncomingBlock(2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:48:07 +08:00
|
|
|
#define CHECK_ITERATORS(Range1, Range2) \
|
2018-05-17 07:20:42 +08:00
|
|
|
EXPECT_EQ(std::distance(Range1.begin(), Range1.end()), \
|
|
|
|
std::distance(Range2.begin(), Range2.end())); \
|
2018-04-19 17:48:07 +08:00
|
|
|
for (auto Pair : zip(Range1, Range2)) \
|
|
|
|
EXPECT_EQ(&std::get<0>(Pair), std::get<1>(Pair));
|
|
|
|
|
2018-04-19 20:06:26 +08:00
|
|
|
TEST(BasicBlockTest, TestInstructionsWithoutDebug) {
|
2018-04-19 17:48:07 +08:00
|
|
|
LLVMContext Ctx;
|
|
|
|
|
2018-04-19 20:06:26 +08:00
|
|
|
Module *M = new Module("MyModule", Ctx);
|
2018-04-19 17:48:07 +08:00
|
|
|
Type *ArgTy1[] = {Type::getInt32PtrTy(Ctx)};
|
|
|
|
FunctionType *FT = FunctionType::get(Type::getVoidTy(Ctx), ArgTy1, false);
|
2018-04-19 20:06:26 +08:00
|
|
|
Argument *V = new Argument(Type::getInt32Ty(Ctx));
|
|
|
|
Function *F = Function::Create(FT, Function::ExternalLinkage, "", M);
|
2018-04-19 17:48:07 +08:00
|
|
|
|
2019-02-02 04:43:25 +08:00
|
|
|
Function *DbgAddr = Intrinsic::getDeclaration(M, Intrinsic::dbg_addr);
|
|
|
|
Function *DbgDeclare = Intrinsic::getDeclaration(M, Intrinsic::dbg_declare);
|
|
|
|
Function *DbgValue = Intrinsic::getDeclaration(M, Intrinsic::dbg_value);
|
2018-04-19 17:48:07 +08:00
|
|
|
Value *DIV = MetadataAsValue::get(Ctx, (Metadata *)nullptr);
|
|
|
|
SmallVector<Value *, 3> Args = {DIV, DIV, DIV};
|
|
|
|
|
|
|
|
BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
|
|
|
|
const BasicBlock *BBConst = BB1;
|
|
|
|
IRBuilder<> Builder1(BB1);
|
|
|
|
|
|
|
|
AllocaInst *Var = Builder1.CreateAlloca(Builder1.getInt8Ty());
|
|
|
|
Builder1.CreateCall(DbgValue, Args);
|
|
|
|
Instruction *AddInst = cast<Instruction>(Builder1.CreateAdd(V, V));
|
|
|
|
Instruction *MulInst = cast<Instruction>(Builder1.CreateMul(AddInst, V));
|
|
|
|
Builder1.CreateCall(DbgDeclare, Args);
|
|
|
|
Instruction *SubInst = cast<Instruction>(Builder1.CreateSub(MulInst, V));
|
|
|
|
Builder1.CreateCall(DbgAddr, Args);
|
|
|
|
|
|
|
|
SmallVector<Instruction *, 4> Exp = {Var, AddInst, MulInst, SubInst};
|
|
|
|
CHECK_ITERATORS(BB1->instructionsWithoutDebug(), Exp);
|
|
|
|
CHECK_ITERATORS(BBConst->instructionsWithoutDebug(), Exp);
|
2018-04-19 20:06:26 +08:00
|
|
|
|
2019-09-20 21:22:59 +08:00
|
|
|
EXPECT_EQ(static_cast<size_t>(BB1->sizeWithoutDebug()), Exp.size());
|
|
|
|
EXPECT_EQ(static_cast<size_t>(BBConst->sizeWithoutDebug()), Exp.size());
|
|
|
|
|
2018-04-19 20:06:26 +08:00
|
|
|
delete M;
|
|
|
|
delete V;
|
2018-04-19 17:48:07 +08:00
|
|
|
}
|
|
|
|
|
[IR] Lazily number instructions for local dominance queries
Essentially, fold OrderedBasicBlock into BasicBlock, and make it
auto-invalidate the instruction ordering when new instructions are
added. Notably, we don't need to invalidate it when removing
instructions, which is helpful when a pass mostly delete dead
instructions rather than transforming them.
The downside is that Instruction grows from 56 bytes to 64 bytes. The
resulting LLVM code is substantially simpler and automatically handles
invalidation, which makes me think that this is the right speed and size
tradeoff.
The important change is in SymbolTableTraitsImpl.h, where the numbering
is invalidated. Everything else should be straightforward.
We probably want to implement a fancier re-numbering scheme so that
local updates don't invalidate the ordering, but I plan for that to be
future work, maybe for someone else.
Reviewed By: lattner, vsk, fhahn, dexonsmith
Differential Revision: https://reviews.llvm.org/D51664
2020-02-19 06:33:54 +08:00
|
|
|
TEST(BasicBlockTest, ComesBefore) {
|
|
|
|
const char *ModuleString = R"(define i32 @f(i32 %x) {
|
|
|
|
%add = add i32 %x, 42
|
|
|
|
ret i32 %add
|
|
|
|
})";
|
|
|
|
LLVMContext Ctx;
|
|
|
|
SMDiagnostic Err;
|
|
|
|
auto M = parseAssemblyString(ModuleString, Err, Ctx);
|
|
|
|
ASSERT_TRUE(M.get());
|
|
|
|
|
|
|
|
Function *F = M->getFunction("f");
|
|
|
|
BasicBlock &BB = F->front();
|
|
|
|
BasicBlock::iterator I = BB.begin();
|
|
|
|
Instruction *Add = &*I++;
|
|
|
|
Instruction *Ret = &*I++;
|
|
|
|
|
|
|
|
// Intentionally duplicated to verify cached and uncached are the same.
|
|
|
|
EXPECT_FALSE(BB.isInstrOrderValid());
|
|
|
|
EXPECT_FALSE(Add->comesBefore(Add));
|
|
|
|
EXPECT_TRUE(BB.isInstrOrderValid());
|
|
|
|
EXPECT_FALSE(Add->comesBefore(Add));
|
|
|
|
BB.invalidateOrders();
|
|
|
|
EXPECT_FALSE(BB.isInstrOrderValid());
|
|
|
|
EXPECT_TRUE(Add->comesBefore(Ret));
|
|
|
|
EXPECT_TRUE(BB.isInstrOrderValid());
|
|
|
|
EXPECT_TRUE(Add->comesBefore(Ret));
|
|
|
|
BB.invalidateOrders();
|
|
|
|
EXPECT_FALSE(Ret->comesBefore(Add));
|
|
|
|
EXPECT_FALSE(Ret->comesBefore(Add));
|
|
|
|
BB.invalidateOrders();
|
|
|
|
EXPECT_FALSE(Ret->comesBefore(Ret));
|
|
|
|
EXPECT_FALSE(Ret->comesBefore(Ret));
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:57:20 +08:00
|
|
|
TEST(BasicBlockTest, EmptyPhi) {
|
|
|
|
LLVMContext Ctx;
|
|
|
|
|
|
|
|
Module *M = new Module("MyModule", Ctx);
|
|
|
|
FunctionType *FT = FunctionType::get(Type::getVoidTy(Ctx), {}, false);
|
|
|
|
Function *F = Function::Create(FT, Function::ExternalLinkage, "", M);
|
|
|
|
|
|
|
|
BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
|
|
|
|
ReturnInst::Create(Ctx, BB1);
|
|
|
|
|
|
|
|
Type *Ty = Type::getInt32PtrTy(Ctx);
|
|
|
|
BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
|
|
|
|
PHINode::Create(Ty, 0, "", BB2);
|
|
|
|
ReturnInst::Create(Ctx, BB2);
|
|
|
|
EXPECT_FALSE(verifyModule(*M, &errs()));
|
|
|
|
}
|
|
|
|
|
[IR] Lazily number instructions for local dominance queries
Essentially, fold OrderedBasicBlock into BasicBlock, and make it
auto-invalidate the instruction ordering when new instructions are
added. Notably, we don't need to invalidate it when removing
instructions, which is helpful when a pass mostly delete dead
instructions rather than transforming them.
The downside is that Instruction grows from 56 bytes to 64 bytes. The
resulting LLVM code is substantially simpler and automatically handles
invalidation, which makes me think that this is the right speed and size
tradeoff.
The important change is in SymbolTableTraitsImpl.h, where the numbering
is invalidated. Everything else should be straightforward.
We probably want to implement a fancier re-numbering scheme so that
local updates don't invalidate the ordering, but I plan for that to be
future work, maybe for someone else.
Reviewed By: lattner, vsk, fhahn, dexonsmith
Differential Revision: https://reviews.llvm.org/D51664
2020-02-19 06:33:54 +08:00
|
|
|
class InstrOrderInvalidationTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
void SetUp() override {
|
|
|
|
M.reset(new Module("MyModule", Ctx));
|
|
|
|
Nop = Intrinsic::getDeclaration(M.get(), Intrinsic::donothing);
|
|
|
|
FunctionType *FT = FunctionType::get(Type::getVoidTy(Ctx), {}, false);
|
|
|
|
Function *F = Function::Create(FT, Function::ExternalLinkage, "foo", *M);
|
|
|
|
BB = BasicBlock::Create(Ctx, "entry", F);
|
|
|
|
|
|
|
|
IRBuilder<> Builder(BB);
|
|
|
|
I1 = Builder.CreateCall(Nop);
|
|
|
|
I2 = Builder.CreateCall(Nop);
|
|
|
|
I3 = Builder.CreateCall(Nop);
|
|
|
|
Ret = Builder.CreateRetVoid();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMContext Ctx;
|
|
|
|
std::unique_ptr<Module> M;
|
|
|
|
Function *Nop = nullptr;
|
|
|
|
BasicBlock *BB = nullptr;
|
|
|
|
Instruction *I1 = nullptr;
|
|
|
|
Instruction *I2 = nullptr;
|
|
|
|
Instruction *I3 = nullptr;
|
|
|
|
Instruction *Ret = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(InstrOrderInvalidationTest, InsertInvalidation) {
|
|
|
|
EXPECT_FALSE(BB->isInstrOrderValid());
|
|
|
|
EXPECT_TRUE(I1->comesBefore(I2));
|
|
|
|
EXPECT_TRUE(BB->isInstrOrderValid());
|
|
|
|
EXPECT_TRUE(I2->comesBefore(I3));
|
|
|
|
EXPECT_TRUE(I3->comesBefore(Ret));
|
|
|
|
EXPECT_TRUE(BB->isInstrOrderValid());
|
|
|
|
|
|
|
|
// Invalidate orders.
|
|
|
|
IRBuilder<> Builder(BB, I2->getIterator());
|
|
|
|
Instruction *I1a = Builder.CreateCall(Nop);
|
|
|
|
EXPECT_FALSE(BB->isInstrOrderValid());
|
|
|
|
EXPECT_TRUE(I1->comesBefore(I1a));
|
|
|
|
EXPECT_TRUE(BB->isInstrOrderValid());
|
|
|
|
EXPECT_TRUE(I1a->comesBefore(I2));
|
|
|
|
EXPECT_TRUE(I2->comesBefore(I3));
|
|
|
|
EXPECT_TRUE(I3->comesBefore(Ret));
|
|
|
|
EXPECT_TRUE(BB->isInstrOrderValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(InstrOrderInvalidationTest, SpliceInvalidation) {
|
|
|
|
EXPECT_TRUE(I1->comesBefore(I2));
|
|
|
|
EXPECT_TRUE(I2->comesBefore(I3));
|
|
|
|
EXPECT_TRUE(I3->comesBefore(Ret));
|
|
|
|
EXPECT_TRUE(BB->isInstrOrderValid());
|
|
|
|
|
|
|
|
// Use Instruction::moveBefore, which uses splice.
|
|
|
|
I2->moveBefore(I1);
|
|
|
|
EXPECT_FALSE(BB->isInstrOrderValid());
|
|
|
|
|
|
|
|
EXPECT_TRUE(I2->comesBefore(I1));
|
|
|
|
EXPECT_TRUE(I1->comesBefore(I3));
|
|
|
|
EXPECT_TRUE(I3->comesBefore(Ret));
|
|
|
|
EXPECT_TRUE(BB->isInstrOrderValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(InstrOrderInvalidationTest, RemoveNoInvalidation) {
|
|
|
|
// Cache the instruction order.
|
|
|
|
EXPECT_FALSE(BB->isInstrOrderValid());
|
|
|
|
EXPECT_TRUE(I1->comesBefore(I2));
|
|
|
|
EXPECT_TRUE(BB->isInstrOrderValid());
|
|
|
|
|
|
|
|
// Removing does not invalidate instruction order.
|
|
|
|
I2->removeFromParent();
|
|
|
|
I2->deleteValue();
|
|
|
|
I2 = nullptr;
|
|
|
|
EXPECT_TRUE(BB->isInstrOrderValid());
|
|
|
|
EXPECT_TRUE(I1->comesBefore(I3));
|
|
|
|
EXPECT_EQ(std::next(I1->getIterator()), I3->getIterator());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(InstrOrderInvalidationTest, EraseNoInvalidation) {
|
|
|
|
// Cache the instruction order.
|
|
|
|
EXPECT_FALSE(BB->isInstrOrderValid());
|
|
|
|
EXPECT_TRUE(I1->comesBefore(I2));
|
|
|
|
EXPECT_TRUE(BB->isInstrOrderValid());
|
|
|
|
|
|
|
|
// Removing does not invalidate instruction order.
|
|
|
|
I2->eraseFromParent();
|
|
|
|
I2 = nullptr;
|
|
|
|
EXPECT_TRUE(BB->isInstrOrderValid());
|
|
|
|
EXPECT_TRUE(I1->comesBefore(I3));
|
|
|
|
EXPECT_EQ(std::next(I1->getIterator()), I3->getIterator());
|
|
|
|
}
|
|
|
|
|
2017-05-26 11:10:00 +08:00
|
|
|
} // End anonymous namespace.
|
|
|
|
} // End llvm namespace.
|