6 #include <gtest/gtest.h>
24 std::unique_ptr<jlm::llvm::aa::PointsToGraph>
35 std::unique_ptr<jlm::llvm::aa::PointsToGraph>
42 static std::unique_ptr<jlm::llvm::aa::PointsToGraph>
46 return analysis.
Analyze(rvsdgModule);
55 for (
auto & node : region.
Nodes())
57 if (jlm::rvsdg::is<AllocaOperation>(&node))
59 auto simpleNode = jlm::util::assertedCast<jlm::rvsdg::SimpleNode>(&node);
60 auto ptgAllocaNode =
pointsToGraph_->addNodeForAlloca(*simpleNode,
false);
62 pointsToGraph_->mapRegisterToNode(*node.output(0), ptgRegisterNode);
65 else if (jlm::rvsdg::is<MallocOperation>(&node))
67 auto simpleNode = jlm::util::assertedCast<jlm::rvsdg::SimpleNode>(&node);
68 auto ptgMallocNode =
pointsToGraph_->addNodeForMalloc(*simpleNode,
true);
70 pointsToGraph_->mapRegisterToNode(MallocOperation::addressOutput(node), ptgRegisterNode);
75 auto ptgDeltaNode =
pointsToGraph_->addNodeForDelta(*deltaNode,
true);
77 pointsToGraph_->mapRegisterToNode(*node.output(0), ptgRegisterNode);
84 auto ptgLambdaNode =
pointsToGraph_->addNodeForLambda(*lambdaNode,
true);
86 pointsToGraph_->mapRegisterToNode(*node.output(0), ptgRegisterNode);
100 for (
size_t n = 0; n < rootRegion.narguments(); n++)
102 auto & graphImport = *jlm::util::assertedCast<const LlvmGraphImport>(rootRegion.argument(n));
104 const auto ptgImportNode =
pointsToGraph_->addNodeForImport(graphImport,
true);
105 const auto ptgRegisterNode =
pointsToGraph_->addNodeForRegisters();
114 TEST(PointsToGraphTests, TestNodeIterators)
124 EXPECT_EQ(pointsToGraph->numImportNodes(), 1u);
125 for (
auto importNode : pointsToGraph->importNodes())
127 EXPECT_EQ(pointsToGraph->getNodeKind(importNode), NodeKind::ImportNode);
128 EXPECT_EQ(&pointsToGraph->getImportForNode(importNode), &test.
GetImportOutput());
131 EXPECT_EQ(pointsToGraph->numLambdaNodes(), 1u);
132 for (
auto & lambdaNode : pointsToGraph->lambdaNodes())
134 EXPECT_EQ(pointsToGraph->getNodeKind(lambdaNode), NodeKind::LambdaNode);
135 EXPECT_EQ(&pointsToGraph->getLambdaForNode(lambdaNode), &test.
GetLambdaNode());
138 EXPECT_EQ(pointsToGraph->numDeltaNodes(), 1u);
139 for (
auto & deltaNode : pointsToGraph->deltaNodes())
141 EXPECT_EQ(pointsToGraph->getNodeKind(deltaNode), NodeKind::DeltaNode);
142 EXPECT_EQ(&pointsToGraph->getDeltaForNode(deltaNode), &test.
GetDeltaNode());
145 EXPECT_EQ(pointsToGraph->numAllocaNodes(), 1u);
146 for (
auto & allocaNode : pointsToGraph->allocaNodes())
148 EXPECT_EQ(pointsToGraph->getNodeKind(allocaNode), NodeKind::AllocaNode);
149 EXPECT_EQ(&pointsToGraph->getAllocaForNode(allocaNode), &test.
GetAllocaNode());
152 EXPECT_EQ(pointsToGraph->numMallocNodes(), 1u);
153 for (
auto & mallocNode : pointsToGraph->mallocNodes())
155 EXPECT_EQ(pointsToGraph->getNodeKind(mallocNode), NodeKind::MallocNode);
156 EXPECT_EQ(&pointsToGraph->getMallocForNode(mallocNode), &test.
GetMallocNode());
160 EXPECT_EQ(pointsToGraph->numRegisterNodes(), 5u);
162 for (
auto & registerNode : pointsToGraph->registerNodes())
164 EXPECT_EQ(pointsToGraph->getNodeKind(registerNode), NodeKind::RegisterNode);
165 EXPECT_EQ(pointsToGraph->getExplicitTargets(registerNode).Size(), 1u);
166 seenRegisterNodes.
insert(registerNode);
168 EXPECT_EQ(seenRegisterNodes.
Size(), 5u);
170 const auto ptgImportRegister = pointsToGraph->getNodeForRegister(test.
GetImportOutput());
171 EXPECT_TRUE(seenRegisterNodes.
Contains(ptgImportRegister));
172 const auto ptgLambdaRegister = pointsToGraph->getNodeForRegister(test.
GetLambdaOutput());
173 EXPECT_TRUE(seenRegisterNodes.
Contains(ptgLambdaRegister));
174 const auto ptgDeltaRegister = pointsToGraph->getNodeForRegister(test.
GetDeltaOutput());
175 EXPECT_TRUE(seenRegisterNodes.
Contains(ptgDeltaRegister));
176 const auto ptgAllocaRegister = pointsToGraph->getNodeForRegister(test.
GetAllocaOutput());
177 EXPECT_TRUE(seenRegisterNodes.
Contains(ptgAllocaRegister));
178 const auto ptgMallocRegister = pointsToGraph->getNodeForRegister(test.
GetMallocOutput());
179 EXPECT_TRUE(seenRegisterNodes.
Contains(ptgMallocRegister));
182 const auto ptgImportNode = pointsToGraph->getNodeForImport(test.
GetImportOutput());
183 EXPECT_TRUE(pointsToGraph->getExplicitTargets(ptgImportRegister).Contains(ptgImportNode));
184 const auto ptgLambdaNode = pointsToGraph->getNodeForLambda(test.
GetLambdaNode());
185 EXPECT_TRUE(pointsToGraph->getExplicitTargets(ptgLambdaRegister).Contains(ptgLambdaNode));
186 const auto ptgDeltaNode = pointsToGraph->getNodeForDelta(test.
GetDeltaNode());
187 EXPECT_TRUE(pointsToGraph->getExplicitTargets(ptgDeltaRegister).Contains(ptgDeltaNode));
188 const auto ptgAllocaNode = pointsToGraph->getNodeForAlloca(test.
GetAllocaNode());
189 EXPECT_TRUE(pointsToGraph->getExplicitTargets(ptgAllocaRegister).Contains(ptgAllocaNode));
190 const auto ptgMallocNode = pointsToGraph->getNodeForMalloc(test.
GetMallocNode());
191 EXPECT_TRUE(pointsToGraph->getExplicitTargets(ptgMallocRegister).Contains(ptgMallocNode));
194 TEST(PointsToGraphTests, TestIsSupergraphOf)
197 auto graph0 = PointsToGraph::create();
198 auto graph1 = PointsToGraph::create();
201 EXPECT_TRUE(graph0->isSupergraphOf(*graph1));
202 EXPECT_TRUE(graph1->isSupergraphOf(*graph0));
208 const auto alloca0 = graph0->addNodeForAlloca(rvsdg.
GetAllocaNode(),
false);
209 EXPECT_TRUE(graph0->isSupergraphOf(*graph1));
210 EXPECT_FALSE(graph1->isSupergraphOf(*graph0));
213 const auto alloca1 = graph1->addNodeForAlloca(rvsdg.
GetAllocaNode(),
false);
214 EXPECT_TRUE(graph0->isSupergraphOf(*graph1));
215 EXPECT_TRUE(graph1->isSupergraphOf(*graph0));
218 const auto register0 = graph0->addNodeForRegisters();
220 EXPECT_TRUE(graph0->isSupergraphOf(*graph1));
221 EXPECT_FALSE(graph1->isSupergraphOf(*graph0));
224 const auto register1 = graph1->addNodeForRegisters();
227 EXPECT_FALSE(graph0->isSupergraphOf(*graph1));
228 EXPECT_TRUE(graph1->isSupergraphOf(*graph0));
231 const auto deltaRegister0 = graph0->addNodeForRegisters();
232 graph0->mapRegisterToNode(rvsdg.
GetDeltaOutput(), deltaRegister0);
233 EXPECT_TRUE(graph0->isSupergraphOf(*graph1));
234 EXPECT_TRUE(graph1->isSupergraphOf(*graph0));
237 graph0->addTarget(register0, alloca0);
238 EXPECT_TRUE(graph0->isSupergraphOf(*graph1));
239 EXPECT_FALSE(graph1->isSupergraphOf(*graph0));
242 graph1->addTarget(register1, alloca1);
243 EXPECT_FALSE(graph0->isSupergraphOf(*graph1));
244 EXPECT_TRUE(graph1->isSupergraphOf(*graph0));
247 graph0->addTarget(deltaRegister0, alloca0);
248 EXPECT_TRUE(graph0->isSupergraphOf(*graph1));
249 EXPECT_TRUE(graph1->isSupergraphOf(*graph0));
252 graph0->markAsTargetsAllExternallyAvailable(alloca0);
253 EXPECT_TRUE(graph0->isSupergraphOf(*graph1));
254 EXPECT_FALSE(graph1->isSupergraphOf(*graph0));
257 graph1->markAsTargetsAllExternallyAvailable(alloca1);
258 EXPECT_TRUE(graph0->isSupergraphOf(*graph1));
259 EXPECT_TRUE(graph1->isSupergraphOf(*graph0));
264 const auto import0 = graph0->addNodeForImport(rvsdg.
GetImportOutput(),
true);
265 const auto import1 = graph1->addNodeForImport(rvsdg.
GetImportOutput(),
true);
266 const auto lambda0 = graph0->addNodeForLambda(rvsdg.
GetLambdaNode(),
false);
267 const auto lambda1 = graph1->addNodeForLambda(rvsdg.
GetLambdaNode(),
false);
268 const auto malloc0 = graph0->addNodeForMalloc(rvsdg.
GetMallocNode(),
false);
269 const auto malloc1 = graph1->addNodeForMalloc(rvsdg.
GetMallocNode(),
false);
270 EXPECT_TRUE(graph0->isSupergraphOf(*graph1));
271 EXPECT_TRUE(graph1->isSupergraphOf(*graph0));
274 graph0->addTarget(malloc0, import0);
275 EXPECT_TRUE(graph0->isSupergraphOf(*graph1));
276 EXPECT_FALSE(graph1->isSupergraphOf(*graph0));
278 graph1->addTarget(import1, lambda1);
279 EXPECT_FALSE(graph0->isSupergraphOf(*graph1));
282 graph1->addTarget(malloc1, import1);
283 graph0->addTarget(import0, lambda0);
284 EXPECT_TRUE(graph0->isSupergraphOf(*graph1));
285 EXPECT_TRUE(graph1->isSupergraphOf(*graph0));
288 TEST(PointsToGraphTests, testMemoryNodeSize)
297 auto ptg = aa::PointsToGraph::create();
298 const auto deltaG1 = ptg->addNodeForDelta(test.
DeltaG1(),
false);
299 const auto deltaG2 = ptg->addNodeForDelta(test.
DeltaG2(),
false);
300 const auto f = ptg->addNodeForLambda(test.
LambdaF(),
false);
303 EXPECT_EQ(ptg->tryGetNodeSize(deltaG1), 4);
304 EXPECT_EQ(ptg->tryGetNodeSize(deltaG2), 8);
305 EXPECT_EQ(ptg->tryGetNodeSize(f), 0);
313 auto ptg = aa::PointsToGraph::create();
314 const auto allocaD = ptg->addNodeForAlloca(*test.
alloca_d,
false);
315 const auto allocaC = ptg->addNodeForAlloca(*test.
alloca_c,
false);
318 EXPECT_EQ(ptg->tryGetNodeSize(allocaD), 4);
319 EXPECT_EQ(ptg->tryGetNodeSize(allocaC), 8);
327 auto ptg = aa::PointsToGraph::create();
328 const auto allocaNode = ptg->addNodeForAlloca(test.
GetAllocaNode(),
false);
329 const auto mallocNode = ptg->addNodeForMalloc(test.
GetMallocNode(),
false);
330 const auto deltaNode = ptg->addNodeForDelta(test.
GetDeltaNode(),
true);
331 const auto lambdaNode = ptg->addNodeForLambda(test.
GetLambdaNode(),
true);
332 const auto importNode = ptg->addNodeForImport(test.
GetImportOutput(),
true);
335 EXPECT_EQ(ptg->tryGetNodeSize(allocaNode), 8);
336 EXPECT_EQ(ptg->tryGetNodeSize(mallocNode), 4);
337 EXPECT_EQ(ptg->tryGetNodeSize(deltaNode), 8);
338 EXPECT_EQ(ptg->tryGetNodeSize(importNode), 4);
340 EXPECT_EQ(ptg->tryGetNodeSize(lambdaNode), 0);
344 TEST(PointsToGraphTests, testIsMemoryNodeConstant)
353 auto ptg = aa::PointsToGraph::create();
354 const auto allocaNode = ptg->addNodeForAlloca(test.
GetAllocaNode(),
false);
355 const auto mallocNode = ptg->addNodeForMalloc(test.
GetMallocNode(),
false);
356 const auto deltaNode = ptg->addNodeForDelta(test.
GetDeltaNode(),
true);
357 const auto lambdaNode = ptg->addNodeForLambda(test.
GetLambdaNode(),
true);
358 const auto importNode = ptg->addNodeForImport(test.
GetImportOutput(),
true);
361 EXPECT_FALSE(ptg->isNodeConstant(allocaNode));
362 EXPECT_FALSE(ptg->isNodeConstant(mallocNode));
363 EXPECT_FALSE(ptg->isNodeConstant(deltaNode));
364 EXPECT_FALSE(ptg->isNodeConstant(importNode));
366 EXPECT_TRUE(ptg->isNodeConstant(lambdaNode));
375 auto & constImport = LlvmGraphImport::createGlobalImport(
380 Linkage::externalLinkage,
383 auto & nonConstImport = LlvmGraphImport::createGlobalImport(
388 Linkage::externalLinkage,
394 DeltaOperation::Create(intType,
"constGlobal", Linkage::internalLinkage,
"data",
true, 4));
395 const auto & int2 = IntegerConstantOperation::Create(*constDelta.subregion(), 32, 2);
396 constDelta.finalize(int2.output(0));
400 DeltaOperation::Create(intType,
"global", Linkage::internalLinkage,
"data",
false, 4));
401 const auto & int8 = IntegerConstantOperation::Create(*nonConstDelta.subregion(), 32, 8);
402 nonConstDelta.finalize(int8.output(0));
404 auto ptg = aa::PointsToGraph::create();
405 const auto constImportMemoryNode = ptg->addNodeForImport(constImport,
true);
406 const auto nonConstImportMemoryNode = ptg->addNodeForImport(nonConstImport,
true);
408 const auto & constDeltaMemoryNode = ptg->addNodeForDelta(constDelta,
false);
409 const auto & nonConstDeltaMemoryNode = ptg->addNodeForDelta(nonConstDelta,
false);
412 EXPECT_TRUE(ptg->isNodeConstant(constImportMemoryNode));
413 EXPECT_FALSE(ptg->isNodeConstant(nonConstImportMemoryNode));
414 EXPECT_TRUE(ptg->isNodeConstant(constDeltaMemoryNode));
415 EXPECT_FALSE(ptg->isNodeConstant(nonConstDeltaMemoryNode));
static jlm::util::StatisticsCollector statisticsCollector
TEST(PointsToGraphTests, TestNodeIterators)
std::unique_ptr< jlm::llvm::aa::PointsToGraph > pointsToGraph_
std::unique_ptr< jlm::llvm::aa::PointsToGraph > Analyze(const jlm::rvsdg::RvsdgModule &rvsdgModule, jlm::util::StatisticsCollector &) override
Analyze RVSDG module.
static std::unique_ptr< jlm::llvm::aa::PointsToGraph > CreateAndAnalyze(const jlm::llvm::LlvmRvsdgModule &rvsdgModule)
void AnalyzeRegion(jlm::rvsdg::Region ®ion)
void AnalyzeImports(const jlm::rvsdg::Graph &rvsdg)
std::unique_ptr< jlm::llvm::aa::PointsToGraph > Analyze(const jlm::llvm::LlvmRvsdgModule &rvsdgModule)
RVSDG module with one of each memory node type.
const rvsdg::SimpleNode & GetAllocaNode() const noexcept
const rvsdg::Output & GetLambdaOutput() const noexcept
const jlm::rvsdg::LambdaNode & GetLambdaNode() const noexcept
const jlm::rvsdg::Output & GetAllocaOutput() const noexcept
const rvsdg::Output & GetDeltaOutput() const noexcept
const llvm::LlvmGraphImport & GetImportOutput() const noexcept
const rvsdg::SimpleNode & GetMallocNode() const noexcept
const jlm::rvsdg::DeltaNode & GetDeltaNode() const noexcept
const jlm::rvsdg::Output & GetMallocOutput() const noexcept
const jlm::rvsdg::DeltaNode & DeltaG2() const noexcept
const jlm::rvsdg::DeltaNode & DeltaG1() const noexcept
const jlm::rvsdg::LambdaNode & LambdaF() const noexcept
static std::shared_ptr< const PointerType > Create()
jlm::llvm::LlvmRvsdgModule & module()
rvsdg::SimpleNode * alloca_c
rvsdg::SimpleNode * alloca_d
Points-to Analysis Interface.
static std::unique_ptr< PointsToGraph > create()
static std::shared_ptr< const BitType > Create(std::size_t nbits)
Creates bit type of specified width.
static DeltaNode * Create(rvsdg::Region *parent, std::unique_ptr< DeltaOperation > op)
Region & GetRootRegion() const noexcept
Represent acyclic RVSDG subgraphs.
NodeRange Nodes() noexcept
bool insert(ItemType item)
std::size_t Size() const noexcept
bool Contains(const ItemType &item) const noexcept
Global memory state passed between functions.