1 //===- DDGTest.cpp - DDGAnalysis unit tests -------------------------------===//
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/DDG.h"
10 #include "llvm/Analysis/AliasAnalysis.h"
11 #include "llvm/Analysis/BasicAliasAnalysis.h"
12 #include "llvm/Analysis/LoopInfo.h"
13 #include "llvm/Analysis/ScalarEvolution.h"
14 #include "llvm/Analysis/TargetLibraryInfo.h"
15 #include "llvm/AsmParser/Parser.h"
16 #include "llvm/IR/Dominators.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "gtest/gtest.h"
19
20 using namespace llvm;
21
22 /// Build the DDG analysis for a loop and run the given test \p Test.
runTest(Module & M,StringRef FuncName,function_ref<void (Function & F,LoopInfo & LI,DependenceInfo & DI,ScalarEvolution & SE)> Test)23 static void runTest(Module &M, StringRef FuncName,
24 function_ref<void(Function &F, LoopInfo &LI,
25 DependenceInfo &DI, ScalarEvolution &SE)>
26 Test) {
27 auto *F = M.getFunction(FuncName);
28 ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
29
30 TargetLibraryInfoImpl TLII;
31 TargetLibraryInfo TLI(TLII);
32 AssumptionCache AC(*F);
33 DominatorTree DT(*F);
34 LoopInfo LI(DT);
35 ScalarEvolution SE(*F, TLI, AC, DT, LI);
36 AAResults AA(TLI);
37 DependenceInfo DI(F, &AA, &SE, &LI);
38 Test(*F, LI, DI, SE);
39 }
40
makeLLVMModule(LLVMContext & Context,const char * ModuleStr)41 static std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
42 const char *ModuleStr) {
43 SMDiagnostic Err;
44 return parseAssemblyString(ModuleStr, Err, Context);
45 }
46
TEST(DDGTest,getDependencies)47 TEST(DDGTest, getDependencies) {
48 const char *ModuleStr =
49 "target datalayout = \"e-m:e-i64:64-n32:64\"\n"
50 "target triple = \"powerpc64le-unknown-linux-gnu\"\n"
51 "\n"
52 "define dso_local void @foo(i32 signext %n, i32* noalias %A, i32* "
53 "noalias %B) {\n"
54 "entry:\n"
55 " %cmp1 = icmp sgt i32 %n, 0\n"
56 " br i1 %cmp1, label %for.body.preheader, label %for.end\n"
57 "\n"
58 "for.body.preheader:\n"
59 " %wide.trip.count = zext i32 %n to i64\n"
60 " br label %for.body\n"
61 " \n"
62 " for.body:\n"
63 " %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ "
64 "%indvars.iv.next, %for.body ]\n"
65 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv\n"
66 " %0 = trunc i64 %indvars.iv to i32\n"
67 " store i32 %0, i32* %arrayidx, align 4\n"
68 " %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\n"
69 " %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 "
70 "%indvars.iv.next\n"
71 " %1 = load i32, i32* %arrayidx2, align 4\n"
72 " %add3 = add nsw i32 %1, 1\n"
73 " %arrayidx5 = getelementptr inbounds i32, i32* %B, i64 %indvars.iv\n"
74 " store i32 %add3, i32* %arrayidx5, align 4\n"
75 " %exitcond = icmp ne i64 %indvars.iv.next, %wide.trip.count\n"
76 " br i1 %exitcond, label %for.body, label %for.end.loopexit\n"
77 "\n"
78 "for.end.loopexit:\n"
79 " br label %for.end\n"
80 "\n"
81 "for.end:\n"
82 " ret void\n"
83 "}\n";
84
85 LLVMContext Context;
86 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
87
88 runTest(
89 *M, "foo",
90 [&](Function &F, LoopInfo &LI, DependenceInfo &DI, ScalarEvolution &SE) {
91 Loop *L = *LI.begin();
92 assert(L && "expected the loop to be identified.");
93
94 DataDependenceGraph DDG(*L, LI, DI);
95
96 // Collect all the nodes that have an outgoing memory edge
97 // while collecting all memory edges as well. There should
98 // only be one node with an outgoing memory edge and there
99 // should only be one memory edge in the entire graph.
100 std::vector<DDGNode *> DependenceSourceNodes;
101 std::vector<DDGEdge *> MemoryEdges;
102 for (DDGNode *N : DDG) {
103 for (DDGEdge *E : *N) {
104 bool SourceAdded = false;
105 if (E->isMemoryDependence()) {
106 MemoryEdges.push_back(E);
107 if (!SourceAdded) {
108 DependenceSourceNodes.push_back(N);
109 SourceAdded = true;
110 }
111 }
112 }
113 }
114
115 EXPECT_EQ(DependenceSourceNodes.size(), 1ull);
116 EXPECT_EQ(MemoryEdges.size(), 1ull);
117
118 DataDependenceGraph::DependenceList DL;
119 DDG.getDependencies(*DependenceSourceNodes.back(),
120 MemoryEdges.back()->getTargetNode(), DL);
121
122 EXPECT_EQ(DL.size(), 1ull);
123 EXPECT_TRUE(DL.back()->isAnti());
124 EXPECT_EQ(DL.back()->getLevels(), 1u);
125 EXPECT_NE(DL.back()->getDistance(1), nullptr);
126 EXPECT_EQ(DL.back()->getDistance(1),
127 SE.getOne(DL.back()->getDistance(1)->getType()));
128 });
129 }
130