Coverage Report

Created: 2018-12-13 20:48

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_tarjan.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2010-2011 INRIA Saclay
3
 * Copyright 2012      Ecole Normale Superieure
4
 *
5
 * Use of this software is governed by the MIT license
6
 *
7
 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8
 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9
 * 91893 Orsay, France
10
 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
11
 */
12
13
#include <stdlib.h>
14
#include <isl/ctx.h>
15
#include <isl_tarjan.h>
16
17
struct isl_tarjan_graph *isl_tarjan_graph_free(struct isl_tarjan_graph *g)
18
929
{
19
929
  if (!g)
20
0
    return NULL;
21
929
  free(g->node);
22
929
  free(g->stack);
23
929
  free(g->order);
24
929
  free(g);
25
929
  return NULL;
26
929
}
27
28
static struct isl_tarjan_graph *isl_tarjan_graph_alloc(isl_ctx *ctx, int len)
29
929
{
30
929
  struct isl_tarjan_graph *g;
31
929
  int i;
32
929
33
929
  g = isl_calloc_type(ctx, struct isl_tarjan_graph);
34
929
  if (!g)
35
0
    return NULL;
36
929
  g->len = len;
37
929
  g->node = isl_alloc_array(ctx, struct isl_tarjan_node, len);
38
929
  if (len && !g->node)
39
0
    goto error;
40
2.63k
  
for (i = 0; 929
i < len;
++i1.70k
)
41
1.70k
    g->node[i].index = -1;
42
929
  g->stack = isl_alloc_array(ctx, int, len);
43
929
  if (len && !g->stack)
44
0
    goto error;
45
929
  g->order = isl_alloc_array(ctx, int, 2 * len);
46
929
  if (len && !g->order)
47
0
    goto error;
48
929
49
929
  g->sp = 0;
50
929
  g->index = 0;
51
929
  g->op = 0;
52
929
53
929
  return g;
54
0
error:
55
0
  isl_tarjan_graph_free(g);
56
0
  return NULL;
57
929
}
58
59
/* Perform Tarjan's algorithm for computing the strongly connected components
60
 * in the graph with g->len nodes and with edges defined by "follows".
61
 */
62
static isl_stat isl_tarjan_components(struct isl_tarjan_graph *g, int i,
63
  isl_bool (*follows)(int i, int j, void *user), void *user)
64
1.70k
{
65
1.70k
  int j;
66
1.70k
67
1.70k
  g->node[i].index = g->index;
68
1.70k
  g->node[i].min_index = g->index;
69
1.70k
  g->node[i].on_stack = 1;
70
1.70k
  g->index++;
71
1.70k
  g->stack[g->sp++] = i;
72
1.70k
73
6.33k
  for (j = g->len - 1; j >= 0; 
--j4.63k
) {
74
4.63k
    isl_bool f;
75
4.63k
76
4.63k
    if (j == i)
77
1.70k
      continue;
78
2.93k
    if (g->node[j].index >= 0 &&
79
2.93k
      
(1.66k
!g->node[j].on_stack1.66k
||
80
1.66k
       
g->node[j].index > g->node[i].min_index989
))
81
1.06k
      continue;
82
1.87k
83
1.87k
    f = follows(i, j, user);
84
1.87k
    if (f < 0)
85
0
      return isl_stat_error;
86
1.87k
    if (!f)
87
873
      continue;
88
997
89
997
    if (g->node[j].index < 0) {
90
552
      isl_tarjan_components(g, j, follows, user);
91
552
      if (g->node[j].min_index < g->node[i].min_index)
92
2
        g->node[i].min_index = g->node[j].min_index;
93
552
    } else 
if (445
g->node[j].index < g->node[i].min_index445
)
94
445
      g->node[i].min_index = g->node[j].index;
95
997
  }
96
1.70k
97
1.70k
  if (g->node[i].index != g->node[i].min_index)
98
447
    return isl_stat_ok;
99
1.25k
100
1.70k
  
do 1.25k
{
101
1.70k
    j = g->stack[--g->sp];
102
1.70k
    g->node[j].on_stack = 0;
103
1.70k
    g->order[g->op++] = j;
104
1.70k
  } while (j != i);
105
1.25k
  g->order[g->op++] = -1;
106
1.25k
107
1.25k
  return isl_stat_ok;
108
1.25k
}
109
110
/* Decompose the graph with "len" nodes and edges defined by "follows"
111
 * into strongly connected components (SCCs).
112
 * follows(i, j, user) should return 1 if "i" follows "j" and 0 otherwise.
113
 * It should return -1 on error.
114
 *
115
 * If SCC a contains a node i that follows a node j in another SCC b
116
 * (i.e., follows(i, j, user) returns 1), then SCC a will appear after SCC b
117
 * in the result.
118
 */
119
struct isl_tarjan_graph *isl_tarjan_graph_init(isl_ctx *ctx, int len,
120
  isl_bool (*follows)(int i, int j, void *user), void *user)
121
893
{
122
893
  int i;
123
893
  struct isl_tarjan_graph *g = NULL;
124
893
125
893
  g = isl_tarjan_graph_alloc(ctx, len);
126
893
  if (!g)
127
0
    return NULL;
128
2.50k
  
for (i = len - 1; 893
i >= 0;
--i1.61k
) {
129
1.61k
    if (g->node[i].index >= 0)
130
502
      continue;
131
1.11k
    if (isl_tarjan_components(g, i, follows, user) < 0)
132
0
      return isl_tarjan_graph_free(g);
133
1.11k
  }
134
893
135
893
  return g;
136
893
}
137
138
/* Decompose the graph with "len" nodes and edges defined by "follows"
139
 * into the strongly connected component (SCC) that contains "node"
140
 * as well as all SCCs that are followed by this SCC.
141
 * follows(i, j, user) should return 1 if "i" follows "j" and 0 otherwise.
142
 * It should return -1 on error.
143
 *
144
 * The SCC containing "node" will appear as the last component
145
 * in g->order.
146
 */
147
struct isl_tarjan_graph *isl_tarjan_graph_component(isl_ctx *ctx, int len,
148
  int node, isl_bool (*follows)(int i, int j, void *user), void *user)
149
36
{
150
36
  struct isl_tarjan_graph *g;
151
36
152
36
  g = isl_tarjan_graph_alloc(ctx, len);
153
36
  if (!g)
154
0
    return NULL;
155
36
  if (isl_tarjan_components(g, node, follows, user) < 0)
156
0
    return isl_tarjan_graph_free(g);
157
36
158
36
  return g;
159
36
}