Jlm
FNegTests.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 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 &)
20 {
21  using namespace jlm;
22 
23  bool hasInstruction = false;
24  auto controlFlowGraph =
25  dynamic_cast<const jlm::llvm::FunctionNode *>(module.ipgraph().find("f"))->cfg();
26  auto basicBlock =
27  dynamic_cast<const jlm::llvm::BasicBlock *>(controlFlowGraph->entry()->OutEdge(0)->sink());
28  for (auto threeAddressCode : *basicBlock)
29  hasInstruction = hasInstruction || jlm::llvm::is<OP>(threeAddressCode);
30 
31  return hasInstruction;
32 }
33 
34 TEST(FNegTests, TestFNegScalar)
35 {
36  auto Setup = [](llvm::LLVMContext & context)
37  {
38  using namespace llvm;
39 
40  std::unique_ptr<Module> module(new Module("module", context));
41 
42  auto doubleType = Type::getDoubleTy(context);
43 
44  auto functionArguments = std::vector<Type *>({ doubleType });
45  auto functionType = FunctionType::get(doubleType, functionArguments, false);
46  auto function = Function::Create(functionType, GlobalValue::ExternalLinkage, "f", module.get());
47 
48  auto basicBlock = BasicBlock::Create(context, "basicBlock", function);
49 
50  IRBuilder<> builder(basicBlock);
51  auto returnValue = builder.CreateFNeg(function->arg_begin());
52  builder.CreateRet(returnValue);
53 
54  return module;
55  };
56 
57  llvm::LLVMContext context;
58  auto llvmModule = Setup(context);
59  llvmModule->print(llvm::errs(), nullptr);
60 
61  auto ipgModule = jlm::llvm::ConvertLlvmModule(*llvmModule);
62  print(*ipgModule, stdout);
63 
64  EXPECT_TRUE(Contains<jlm::llvm::FNegOperation>(*ipgModule, "f"));
65 }
66 
67 TEST(FNegTests, TestFNegVector)
68 {
69  auto Setup = [](llvm::LLVMContext & context)
70  {
71  using namespace llvm;
72 
73  std::unique_ptr<Module> module(new Module("module", context));
74 
75  auto vectorType = VectorType::get(Type::getDoubleTy(context), 2, false);
76 
77  auto functionArguments = std::vector<Type *>({ vectorType });
78  auto functionType = FunctionType::get(vectorType, functionArguments, false);
79  auto function = Function::Create(functionType, GlobalValue::ExternalLinkage, "f", module.get());
80 
81  auto basicBlock = BasicBlock::Create(context, "basicBlock", function);
82 
83  IRBuilder<> builder(basicBlock);
84  auto returnValue = builder.CreateFNeg(function->arg_begin());
85  builder.CreateRet(returnValue);
86 
87  return module;
88  };
89 
90  llvm::LLVMContext context;
91  auto llvmModule = Setup(context);
92  llvmModule->print(llvm::errs(), nullptr);
93 
94  auto ipgModule = jlm::llvm::ConvertLlvmModule(*llvmModule);
95  print(*ipgModule, stdout);
96 
97  EXPECT_TRUE(Contains<jlm::llvm::VectorUnaryOperation>(*ipgModule, "f"));
98 }
TEST(FNegTests, TestFNegScalar)
Definition: FNegTests.cpp:34
static bool Contains(const jlm::llvm::InterProceduralGraphModule &module, const std::string &)
Definition: FNegTests.cpp:19
InterProceduralGraph & ipgraph() noexcept
const InterProceduralGraphNode * find(const std::string &name) const noexcept
Definition: ipgraph.cpp:86
void print(const AggregationNode &n, const AnnotationMap &dm, FILE *out)
Definition: print.cpp:120
std::unique_ptr< InterProceduralGraphModule > ConvertLlvmModule(::llvm::Module &llvmModule)