|
| 1 | +//===--- EphemeralValuesCache.cpp -----------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/Analysis/EphemeralValuesCache.h" |
| 10 | +#include "llvm/Analysis/AssumptionCache.h" |
| 11 | +#include "llvm/AsmParser/Parser.h" |
| 12 | +#include "llvm/IR/LLVMContext.h" |
| 13 | +#include "llvm/IR/Module.h" |
| 14 | +#include "llvm/Support/SourceMgr.h" |
| 15 | +#include "gmock/gmock-matchers.h" |
| 16 | +#include "gtest/gtest.h" |
| 17 | + |
| 18 | +using namespace llvm; |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +struct EphemeralValuesCacheTest : public testing::Test { |
| 23 | + LLVMContext Ctx; |
| 24 | + std::unique_ptr<Module> M; |
| 25 | + |
| 26 | + void parseIR(const char *Assembly) { |
| 27 | + SMDiagnostic Error; |
| 28 | + M = parseAssemblyString(Assembly, Error, Ctx); |
| 29 | + if (!M) |
| 30 | + Error.print("EphemeralValuesCacheTest", errs()); |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +TEST_F(EphemeralValuesCacheTest, basic) { |
| 35 | + parseIR(R"IR( |
| 36 | +declare void @llvm.assume(i1) |
| 37 | +
|
| 38 | +define void @foo(i8 %arg0, i8 %arg1) { |
| 39 | + %c0 = icmp eq i8 %arg0, 0 |
| 40 | + call void @llvm.assume(i1 %c0) |
| 41 | + call void @foo(i8 %arg0, i8 %arg1) |
| 42 | + %c1 = icmp eq i8 %arg1, 0 |
| 43 | + call void @llvm.assume(i1 %c1) |
| 44 | + ret void |
| 45 | +} |
| 46 | +)IR"); |
| 47 | + Function *F = M->getFunction("foo"); |
| 48 | + auto *BB = &*F->begin(); |
| 49 | + AssumptionCache AC(*F); |
| 50 | + EphemeralValuesCache EVC(*F, AC); |
| 51 | + auto It = BB->begin(); |
| 52 | + auto *C0 = &*It++; |
| 53 | + auto *Assume0 = &*It++; |
| 54 | + [[maybe_unused]] auto *NotEph = &*It++; |
| 55 | + auto *C1 = &*It++; |
| 56 | + auto *Assume1 = &*It++; |
| 57 | + [[maybe_unused]] auto *Ret = &*It++; |
| 58 | + // Check emphemeral values. |
| 59 | + EXPECT_THAT(EVC.ephValues(), |
| 60 | + testing::UnorderedElementsAre(C0, Assume0, C1, Assume1)); |
| 61 | + // Clear the cache and try again. |
| 62 | + EVC.clear(); |
| 63 | + EXPECT_THAT(EVC.ephValues(), |
| 64 | + testing::UnorderedElementsAre(C0, Assume0, C1, Assume1)); |
| 65 | + // Modify the IR, clear cache and recompute. |
| 66 | + Assume1->eraseFromParent(); |
| 67 | + C1->eraseFromParent(); |
| 68 | + EXPECT_THAT(EVC.ephValues(), |
| 69 | + testing::UnorderedElementsAre(C0, Assume0, C1, Assume1)); |
| 70 | + EVC.clear(); |
| 71 | + EXPECT_THAT(EVC.ephValues(), testing::UnorderedElementsAre(C0, Assume0)); |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace |
0 commit comments