From 044eb2f6afba375a914ac9d8024f8f5142bb912e Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Mon, 18 Dec 2017 20:10:56 +0000 Subject: Vendor import of llvm trunk r321017: https://llvm.org/svn/llvm-project/llvm/trunk@321017 --- unittests/Transforms/Utils/CodeExtractor.cpp | 69 ++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 unittests/Transforms/Utils/CodeExtractor.cpp (limited to 'unittests/Transforms/Utils/CodeExtractor.cpp') diff --git a/unittests/Transforms/Utils/CodeExtractor.cpp b/unittests/Transforms/Utils/CodeExtractor.cpp new file mode 100644 index 000000000000..c229be6d6952 --- /dev/null +++ b/unittests/Transforms/Utils/CodeExtractor.cpp @@ -0,0 +1,69 @@ +//===- CodeExtractor.cpp - Unit tests for CodeExtractor -------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Transforms/Utils/CodeExtractor.h" +#include "llvm/AsmParser/Parser.h" +#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/Dominators.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/IR/Verifier.h" +#include "llvm/IRReader/IRReader.h" +#include "llvm/Support/SourceMgr.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { +TEST(CodeExtractor, ExitStub) { + LLVMContext Ctx; + SMDiagnostic Err; + std::unique_ptr M(parseAssemblyString(R"invalid( + define i32 @foo(i32 %x, i32 %y, i32 %z) { + header: + %0 = icmp ugt i32 %x, %y + br i1 %0, label %body1, label %body2 + + body1: + %1 = add i32 %z, 2 + br label %notExtracted + + body2: + %2 = mul i32 %z, 7 + br label %notExtracted + + notExtracted: + %3 = phi i32 [ %1, %body1 ], [ %2, %body2 ] + %4 = add i32 %3, %x + ret i32 %4 + } + )invalid", + Err, Ctx)); + + Function *Func = M->getFunction("foo"); + SmallVector Candidates; + for (auto &BB : *Func) { + if (BB.getName() == "body1") + Candidates.push_back(&BB); + if (BB.getName() == "body2") + Candidates.push_back(&BB); + } + // CodeExtractor requires the first basic block + // to dominate all the other ones. + Candidates.insert(Candidates.begin(), &Func->getEntryBlock()); + + DominatorTree DT(*Func); + CodeExtractor CE(Candidates, &DT); + EXPECT_TRUE(CE.isEligible()); + + Function *Outlined = CE.extractCodeRegion(); + EXPECT_TRUE(Outlined); + EXPECT_FALSE(verifyFunction(*Outlined)); +} +} // end anonymous namespace -- cgit v1.2.3