Coverage Report

Created: 2017-04-27 19:33

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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
584
{
19
584
  if (!g)
20
0
    return NULL;
21
584
  free(g->node);
22
584
  free(g->stack);
23
584
  free(g->order);
24
584
  free(g);
25
584
  return NULL;
26
584
}
27
28
static struct isl_tarjan_graph *isl_tarjan_graph_alloc(isl_ctx *ctx, int len)
29
584
{
30
584
  struct isl_tarjan_graph *g;
31
584
  int i;
32
584
33
584
  g = isl_calloc_type(ctx, struct isl_tarjan_graph);
34
584
  if (!g)
35
0
    return NULL;
36
584
  g->len = len;
37
584
  g->node = isl_alloc_array(ctx, struct isl_tarjan_node, len);
38
584
  if (
len && 584
!g->node584
)
39
0
    goto error;
40
1.72k
  
for (i = 0; 584
i < len1.72k
;
++i1.14k
)
41
1.14k
    g->node[i].index = -1;
42
584
  g->stack = isl_alloc_array(ctx, int, len);
43
584
  if (
len && 584
!g->stack584
)
44
0
    goto error;
45
584
  
g->order = 584
isl_alloc_array584
(ctx, int, 2 * len);
46
584
  if (
len && 584
!g->order584
)
47
0
    goto error;
48
584
49
584
  g->sp = 0;
50
584
  g->index = 0;
51
584
  g->op = 0;
52
584
53
584
  return g;
54
0
error:
55
0
  isl_tarjan_graph_free(g);
56
0
  return NULL;
57
584
}
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.13k
{
65
1.13k
  int j;
66
1.13k
67
1.13k
  g->node[i].index = g->index;
68
1.13k
  g->node[i].min_index = g->index;
69
1.13k
  g->node[i].on_stack = 1;
70
1.13k
  g->index++;
71
1.13k
  g->stack[g->sp++] = i;
72
1.13k
73
4.53k
  for (j = g->len - 1; 
j >= 04.53k
;
--j3.39k
)
{3.39k
74
3.39k
    isl_bool f;
75
3.39k
76
3.39k
    if (j == i)
77
1.13k
      continue;
78
2.25k
    
if (2.25k
g->node[j].index >= 0 &&2.25k
79
1.23k
      (!g->node[j].on_stack ||
80
683
       g->node[j].index > g->node[i].min_index))
81
738
      continue;
82
2.25k
83
1.51k
    f = follows(i, j, user);
84
1.51k
    if (f < 0)
85
0
      return isl_stat_error;
86
1.51k
    
if (1.51k
!f1.51k
)
87
758
      continue;
88
1.51k
89
756
    
if (756
g->node[j].index < 0756
)
{432
90
432
      isl_tarjan_components(g, j, follows, user);
91
432
      if (g->node[j].min_index < g->node[i].min_index)
92
4
        g->node[i].min_index = g->node[j].min_index;
93
324
    } else 
if (324
g->node[j].index < g->node[i].min_index324
)
94
324
      g->node[i].min_index = g->node[j].index;
95
756
  }
96
1.13k
97
1.13k
  
if (1.13k
g->node[i].index != g->node[i].min_index1.13k
)
98
328
    return isl_stat_ok;
99
1.13k
100
1.13k
  
do 811
{1.13k
101
1.13k
    j = g->stack[--g->sp];
102
1.13k
    g->node[j].on_stack = 0;
103
1.13k
    g->order[g->op++] = j;
104
1.13k
  } while (j != i);
105
811
  g->order[g->op++] = -1;
106
811
107
811
  return isl_stat_ok;
108
1.13k
}
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
561
{
122
561
  int i;
123
561
  struct isl_tarjan_graph *g = NULL;
124
561
125
561
  g = isl_tarjan_graph_alloc(ctx, len);
126
561
  if (!g)
127
0
    return NULL;
128
1.63k
  
for (i = len - 1; 561
i >= 01.63k
;
--i1.07k
)
{1.07k
129
1.07k
    if (g->node[i].index >= 0)
130
394
      continue;
131
684
    
if (684
isl_tarjan_components(g, i, follows, user) < 0684
)
132
0
      return isl_tarjan_graph_free(g);
133
684
  }
134
561
135
561
  return g;
136
561
}
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
23
{
150
23
  struct isl_tarjan_graph *g;
151
23
152
23
  g = isl_tarjan_graph_alloc(ctx, len);
153
23
  if (!g)
154
0
    return NULL;
155
23
  
if (23
isl_tarjan_components(g, node, follows, user) < 023
)
156
0
    return isl_tarjan_graph_free(g);
157
23
158
23
  return g;
159
23
}