aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.6/function_reordering_plugin/callgraph.h
blob: 66ef5b33b50a5bf7a607bb7e42befbe0d1a268c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* Callgraph implementation.
   Copyright (C) 2011 Free Software Foundation, Inc.
   Contributed by Sriraman Tallam (tmsriram@google.com).

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#ifndef CALLGRAPH_H
#define CALLGRAPH_H

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <hashtab.h>
#include <string.h>
#include "libiberty.h"

struct edge_d;
typedef struct edge_d Edge;

/* Maintain a list of edges.  */
typedef struct edge_list_d
{
  Edge *edge;
  struct edge_list_d *next;
  struct edge_list_d *prev;
} Edge_list;

inline static Edge_list *
make_edge_list (Edge *e)
{
  Edge_list *list = XNEW (Edge_list);
  list->edge = e;
  list->next = NULL;
  list->prev = NULL;
  return list;
}

/* Represents a node in the call graph. */
typedef struct node_d
{
  unsigned int id;
  char *name;
  /* Chain all the Nodes created.  */
  struct node_d *next;
  /* Pointer to the next node in the chain of merged nodes.  */
  struct node_d *merge_next;
  /* List of all edges with this node.  */
  Edge_list *edge_list;
  /* Pointer to the last node in the chain of merged nodes.  */
  struct node_d *last_merge_node;
  unsigned int is_merged;
  /* 1 if the function corresponding to this node can be re-ordered.  */
  unsigned int is_real_node;
} Node;

inline static Node *
make_node (unsigned int id, char *name)
{
  Node *node = XNEW (Node);
  node->id = id;
  node->name = name;
  node->is_real_node = 0;
  node->next = NULL;
  node->edge_list = NULL;
  node->last_merge_node = node;
  node->is_merged = 0;
  node->merge_next = NULL;
  return node;
}

/* Chain the nodes that are merged. Maintain a pointer to the last
   node in the chain.  After merging at the end, the last node in the
   current chain is the last node in the chain of the merged node.  */
inline static void
merge_node (Node *merger, Node *mergee)
{
    merger->last_merge_node->merge_next = mergee;
    merger->last_merge_node = mergee->last_merge_node;
    mergee->is_merged = 1;
}

inline static void
add_edge_to_node (Node *n, Edge *e)
{
  Edge_list *list;
  assert (n != NULL && e != NULL);
  list = make_edge_list (e);
  list->next = n->edge_list;
  if (n->edge_list != NULL)
    n->edge_list->prev = list;
  n->edge_list = list;
}

/* A node is real only if the function can be reordered.  */
inline static void
set_as_real_node (Node *node)
{
  node->is_real_node = 1;
}

/* WEAK if one of the nodes is not real. STRONG if both
   nodes are real.  */
typedef enum edge_type_
{
  STRONG_EDGE = 0,
  WEAK_EDGE
} Edge_type;

/*Represents an edge in the call graph.  */
struct edge_d
{
  Node *first_function;
  Node *second_function;
  unsigned int weight;
  Edge_type type;
  /* 1 if the nodes corresponding to this edge have been merged.  */
  unsigned int is_merged;
  /* Doubly linked chain of created edges.  */
  struct edge_d *prev;
  struct edge_d *next;
};

inline static Edge *
make_edge (Node *first, Node *second, unsigned int weight)
{
  Edge *edge = XNEW (Edge);
  edge->first_function = first;
  edge->second_function = second;
  edge->weight = weight;
  edge->type = WEAK_EDGE;
  edge->is_merged = 0;
  edge->prev = NULL;
  edge->next = NULL;
  add_edge_to_node (first, edge);
  add_edge_to_node (second, edge);
  return edge;
}

/* Frees the chain of edges.  */
inline static void
free_edge_chain (Edge *edge_chain)
{
  Edge *edge;

  for (edge = edge_chain; edge != NULL; )
    {
      Edge *next_edge = edge->next;
      free (edge);
      edge = next_edge;
    }
}

inline static void
set_edge_type (Edge *edge)
{
  if (edge->first_function->is_real_node
      && edge->second_function->is_real_node)
    edge->type = STRONG_EDGE;
  else
    edge->type = WEAK_EDGE;
}

inline static unsigned int
edge_lower (Edge *e1, Edge *e2)
{
  if (e1->type == e2->type)
    return (e1->weight < e2->weight) ? 1 : 0;
  if (e1->type == STRONG_EDGE)
    return 0;
  return 1;
}

inline static void
reset_functions (Edge *e, Node *n1, Node *n2)
{
  /* No self edges.  */
  assert (n1->id != n2->id);
  if (n1->id < n2->id)
    {
      e->first_function = n1;
      e->second_function = n2;
    }
  else
    {
      e->first_function = n2;
      e->second_function = n1;
    }
}

/* A Section is represented by its object handle and the section index. */
typedef struct
{
  /* Name of the function.  */
  char *name;
  /* Full name of the section.  */
  char *full_name;
  void *handle;
  int shndx;
} Section_id;

inline static Section_id *
make_section_id (char *name, char *full_name, void *handle, int shndx)
{
  Section_id *s = XNEW (Section_id);
  s->name = name;
  s->full_name = full_name;
  s->handle = handle;
  s->shndx = shndx;

  return s;
}

/* A pair of nodes make a raw edge.  Also, N1->id < N2->id.  */
typedef struct
{
  Node *n1;
  Node *n2;
} Raw_edge;

inline static void
init_raw_edge (Raw_edge *r, Node *n1, Node *n2)
{
  assert (n1 ->id != n2->id);
  if (n1->id < n2->id)
    {
      r->n1 = n1;
      r->n2 = n2;
    }
  else
    {
      r->n1 = n2;
      r->n2 = n1;
    }
}

inline static int is_prefix_of (const char *prefix, const char *str)
{
  return strncmp (prefix, str, strlen (prefix)) == 0;
}

/* Maps the function corresponding to section name to its
   corresponding object handle and the section index.  */
void
map_section_name_to_index (char *section_name, void *handle, int shndx);

void
parse_callgraph_section_contents (unsigned char *section_contents,
                                  unsigned int length);

void dump_functions ();
void dump_edges ();
void find_pettis_hansen_function_layout (FILE *fp);

unsigned int get_layout (FILE *fp, void*** handles,
			 unsigned int** shndx);

void cleanup ();
/* Returns 1 if callgraph is empty.  */
unsigned int is_callgraph_empty ();
#endif