|
| 1 | +//===- UtilsTest.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/SandboxIR/Utils.h" |
| 10 | +#include "llvm/AsmParser/Parser.h" |
| 11 | +#include "llvm/IR/BasicBlock.h" |
| 12 | +#include "llvm/IR/DataLayout.h" |
| 13 | +#include "llvm/IR/Function.h" |
| 14 | +#include "llvm/IR/Instruction.h" |
| 15 | +#include "llvm/IR/Module.h" |
| 16 | +#include "llvm/SandboxIR/SandboxIR.h" |
| 17 | +#include "llvm/Support/SourceMgr.h" |
| 18 | +#include "gtest/gtest.h" |
| 19 | + |
| 20 | +using namespace llvm; |
| 21 | + |
| 22 | +struct UtilsTest : public testing::Test { |
| 23 | + LLVMContext C; |
| 24 | + std::unique_ptr<Module> M; |
| 25 | + |
| 26 | + void parseIR(LLVMContext &C, const char *IR) { |
| 27 | + SMDiagnostic Err; |
| 28 | + M = parseAssemblyString(IR, Err, C); |
| 29 | + if (!M) |
| 30 | + Err.print("UtilsTest", errs()); |
| 31 | + } |
| 32 | + BasicBlock *getBasicBlockByName(Function &F, StringRef Name) { |
| 33 | + for (BasicBlock &BB : F) |
| 34 | + if (BB.getName() == Name) |
| 35 | + return &BB; |
| 36 | + llvm_unreachable("Expected to find basic block!"); |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +TEST_F(UtilsTest, getMemoryLocation) { |
| 41 | + parseIR(C, R"IR( |
| 42 | +define void @foo(ptr %arg0) { |
| 43 | + %ld = load i8, ptr %arg0 |
| 44 | + ret void |
| 45 | +} |
| 46 | +)IR"); |
| 47 | + llvm::Function *LLVMF = &*M->getFunction("foo"); |
| 48 | + auto *LLVMBB = &*LLVMF->begin(); |
| 49 | + auto *LLVMLd = cast<llvm::LoadInst>(&*LLVMBB->begin()); |
| 50 | + sandboxir::Context Ctx(C); |
| 51 | + sandboxir::Function *F = Ctx.createFunction(LLVMF); |
| 52 | + auto *BB = &*F->begin(); |
| 53 | + auto *Ld = cast<sandboxir::LoadInst>(&*BB->begin()); |
| 54 | + EXPECT_EQ(sandboxir::Utils::memoryLocationGetOrNone(Ld), |
| 55 | + MemoryLocation::getOrNone(LLVMLd)); |
| 56 | +} |
0 commit comments