Jlm
SelectTests.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #include <gtest/gtest.h>
7 
11 #include <jlm/llvm/ir/print.hpp>
12 
13 #include <llvm/IR/BasicBlock.h>
14 #include <llvm/IR/IRBuilder.h>
15 #include <llvm/IR/Module.h>
16 
17 template<class OP>
18 static bool
19 contains(const jlm::llvm::InterProceduralGraphModule & module, const std::string & fctname)
20 {
21  using namespace jlm::llvm;
22 
23  bool has_select = false;
24  auto cfg = dynamic_cast<const FunctionNode *>(module.ipgraph().find(fctname))->cfg();
25  auto bb = dynamic_cast<const BasicBlock *>(cfg->entry()->OutEdge(0)->sink());
26  for (auto tac : *bb)
27  has_select = has_select || is<OP>(tac);
28 
29  return has_select;
30 }
31 
32 TEST(SelectTests, test_scalar_select)
33 {
34  auto setup = [](llvm::LLVMContext & ctx)
35  {
36  using namespace llvm;
37 
38  std::unique_ptr<Module> module(new Module("module", ctx));
39 
40  auto int1 = Type::getIntNTy(ctx, 1);
41  auto int64 = Type::getIntNTy(ctx, 64);
42 
43  auto fctargs = std::vector<Type *>({ int1, int64, int64 });
44  auto fcttype = FunctionType::get(int64, fctargs, false);
45  auto fct = Function::Create(fcttype, GlobalValue::ExternalLinkage, "f", module.get());
46 
47  auto bb = BasicBlock::Create(ctx, "bb", fct);
48 
49  IRBuilder<> builder(bb);
50  auto r = builder.CreateSelect(fct->arg_begin(), fct->arg_begin() + 1, fct->arg_begin() + 2);
51  builder.CreateRet(r);
52 
53  return module;
54  };
55 
56  llvm::LLVMContext ctx;
57  auto llvmModule = setup(ctx);
58  llvmModule->print(llvm::errs(), nullptr);
59 
60  auto ipgmod = jlm::llvm::ConvertLlvmModule(*llvmModule);
61  print(*ipgmod, stdout);
62 
63  EXPECT_TRUE(contains<jlm::llvm::SelectOperation>(*ipgmod, "f"));
64 }
65 
66 TEST(SelectTests, test_vector_select)
67 {
68  auto setup = [](llvm::LLVMContext & ctx)
69  {
70  using namespace llvm;
71 
72  std::unique_ptr<Module> module(new Module("module", ctx));
73 
74  auto vint1 = VectorType::get(Type::getIntNTy(ctx, 1), 4, false);
75  auto vint64 = VectorType::get(Type::getIntNTy(ctx, 64), 4, false);
76 
77  auto fctargs = std::vector<Type *>({ vint1, vint64, vint64 });
78  auto fcttype = FunctionType::get(vint64, fctargs, false);
79  auto fct = Function::Create(fcttype, GlobalValue::ExternalLinkage, "f", module.get());
80 
81  auto bb = BasicBlock::Create(ctx, "bb", fct);
82 
83  IRBuilder<> builder(bb);
84  auto r = builder.CreateSelect(fct->arg_begin(), fct->arg_begin() + 1, fct->arg_begin() + 2);
85  builder.CreateRet(r);
86 
87  return module;
88  };
89 
90  llvm::LLVMContext ctx;
91  auto llvmModule = setup(ctx);
92  llvmModule->print(llvm::errs(), nullptr);
93 
94  auto ipgmod = jlm::llvm::ConvertLlvmModule(*llvmModule);
95  print(*ipgmod, stdout);
96 
97  EXPECT_TRUE(contains<jlm::llvm::VectorSelectOperation>(*ipgmod, "f"));
98 }
static bool contains(const jlm::llvm::InterProceduralGraphModule &module, const std::string &fctname)
Definition: SelectTests.cpp:19
TEST(SelectTests, test_scalar_select)
Definition: SelectTests.cpp:32
InterProceduralGraph & ipgraph() noexcept
const InterProceduralGraphNode * find(const std::string &name) const noexcept
Definition: ipgraph.cpp:86
Global memory state passed between functions.
void print(const AggregationNode &n, const AnnotationMap &dm, FILE *out)
Definition: print.cpp:120
std::unique_ptr< InterProceduralGraphModule > ConvertLlvmModule(::llvm::Module &llvmModule)