summaryrefslogtreecommitdiff
path: root/patches/binutils/2.24/907-xtensa-optimize-removed_by_actions.patch
blob: 9df8065307e390297fba419af5907cb57ca69f3e (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
From 3e3f60207399ab29dd55af109e5ae9facc7d8e83 Mon Sep 17 00:00:00 2001
From: Max Filippov <jcmvbkbc@gmail.com>
Date: Sat, 28 Mar 2015 08:46:28 +0300
Subject: [PATCH 2/4] xtensa: optimize removed_by_actions

The function removed_by_actions iterates through text actions to
calculate an offset applied by text actions to a given VMA. Although it
has a parameter p_start_action that allows for incremental offset
calculation, in many places it's used with p_start_action explicitly set
to the first action. After the first relaxation pass when the list of
text actions is finalized, an array of offsets sorted by VMA may be used
to speed up this function.

Original profile:

% time    self  children    called     name
-----------------------------------------
          0.35    0.00   33872/4808961     relax_section_symbols
          3.32    0.00  326022/4808961     relax_property_section
         12.83    0.00 1259379/4808961     offset_with_removed_text
         32.50    0.00 3189688/4808961     translate_reloc
  71.5   49.00    0.00 4808961         removed_by_actions
-----------------------------------------

Same data, after optimization:

% time    self  children    called     name
-----------------------------------------
          0.00    0.00   33872/4808537     relax_section_symbols
          0.01    0.00  326022/4808537     relax_property_section
          0.05    0.00 1258955/4808537     offset_with_removed_text_map
          0.13    0.00 3189688/4808537     translate_reloc
   1.0    0.20    0.00 4808537         removed_by_actions_map
          0.00    0.00     120/120         map_removal_by_action
-----------------------------------------

2015-04-01  Max Filippov  <jcmvbkbc@gmail.com>
bfd/
	* elf32-xtensa.c (removal_by_action_entry_struct,
	removal_by_action_map_struct): new structures.
	(removal_by_action_entry, removal_by_action_map): new typedefs.
	(text_action_list_struct): add new field: map.
	(map_removal_by_action, removed_by_actions_map,
	offset_with_removed_text_map): new functions.
	(relax_section): replace offset_with_removed_text with
	offset_with_removed_text_map.
	(translate_reloc, relax_property_section, relax_section_symbols):
	replace removed_by_actions with removed_by_actions_map.

Backported from: 071aa5c98a31c966f5fbfc573fcee61350fd1936
Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
---
 bfd/elf32-xtensa.c | 181 +++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 156 insertions(+), 25 deletions(-)

diff --git a/bfd/elf32-xtensa.c b/bfd/elf32-xtensa.c
index 872370b..21b2871 100644
--- a/bfd/elf32-xtensa.c
+++ b/bfd/elf32-xtensa.c
@@ -5420,11 +5420,28 @@ struct text_action_struct
   text_action *next;
 };
 
+struct removal_by_action_entry_struct
+{
+  bfd_vma offset;
+  int removed;
+  int eq_removed;
+  int eq_removed_before_fill;
+};
+typedef struct removal_by_action_entry_struct removal_by_action_entry;
+
+struct removal_by_action_map_struct
+{
+  unsigned n_entries;
+  removal_by_action_entry *entry;
+};
+typedef struct removal_by_action_map_struct removal_by_action_map;
+
 
 /* List of all of the actions taken on a text section.  */
 struct text_action_list_struct
 {
   text_action *head;
+  removal_by_action_map map;
 };
 
 
@@ -5636,6 +5653,101 @@ action_list_count (text_action_list *action_list)
   return count;
 }
 
+static void
+map_removal_by_action (text_action_list *action_list)
+{
+  text_action *r;
+  int removed = 0;
+  removal_by_action_map map;
+  bfd_boolean eq_complete;
+
+  map.n_entries = 0;
+  map.entry = bfd_malloc (action_list_count (action_list) *
+			  sizeof (removal_by_action_entry));
+  eq_complete = FALSE;
+
+  for (r = action_list->head; r;)
+    {
+      removal_by_action_entry *ientry = map.entry + map.n_entries;
+
+      if (map.n_entries && (ientry - 1)->offset == r->offset)
+	{
+	  --ientry;
+	}
+      else
+	{
+	  ++map.n_entries;
+	  eq_complete = FALSE;
+	  ientry->offset = r->offset;
+	  ientry->eq_removed_before_fill = removed;
+	}
+
+      if (!eq_complete)
+	{
+	  if (r->action != ta_fill || r->removed_bytes >= 0)
+	    {
+	      ientry->eq_removed = removed;
+	      eq_complete = TRUE;
+	    }
+	  else
+	    ientry->eq_removed = removed + r->removed_bytes;
+	}
+
+      removed += r->removed_bytes;
+      ientry->removed = removed;
+      r = r->next;
+    }
+  action_list->map = map;
+}
+
+static int
+removed_by_actions_map (text_action_list *action_list, bfd_vma offset,
+			bfd_boolean before_fill)
+{
+  unsigned a, b;
+
+  if (!action_list->map.entry)
+    map_removal_by_action (action_list);
+
+  if (!action_list->map.n_entries)
+    return 0;
+
+  a = 0;
+  b = action_list->map.n_entries;
+
+  while (b - a > 1)
+    {
+      unsigned c = (a + b) / 2;
+
+      if (action_list->map.entry[c].offset <= offset)
+	a = c;
+      else
+	b = c;
+    }
+
+  if (action_list->map.entry[a].offset < offset)
+    {
+      return action_list->map.entry[a].removed;
+    }
+  else if (action_list->map.entry[a].offset == offset)
+    {
+      return before_fill ?
+	action_list->map.entry[a].eq_removed_before_fill :
+	action_list->map.entry[a].eq_removed;
+    }
+  else
+    {
+      return 0;
+    }
+}
+
+static bfd_vma
+offset_with_removed_text_map (text_action_list *action_list, bfd_vma offset)
+{
+  int removed = removed_by_actions_map (action_list, offset, FALSE);
+  return offset - removed;
+}
+
 
 /* The find_insn_action routine will only find non-fill actions.  */
 
@@ -5909,6 +6021,9 @@ init_xtensa_relax_info (asection *sec)
 
   relax_info->action_list.head = NULL;
 
+  relax_info->action_list.map.n_entries = 0;
+  relax_info->action_list.map.entry = NULL;
+
   relax_info->fix_list = NULL;
   relax_info->fix_array = NULL;
   relax_info->fix_array_count = 0;
@@ -9218,7 +9333,7 @@ relax_section (bfd *abfd, asection *sec, struct bfd_link_info *link_info)
 		  if (elf_hash_table (link_info)->dynamic_sections_created)
 		    shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
 		  irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
-		  irel->r_offset = offset_with_removed_text
+		  irel->r_offset = offset_with_removed_text_map
 		    (&relax_info->action_list, irel->r_offset);
 		  continue;
 		}
@@ -9255,7 +9370,7 @@ relax_section (bfd *abfd, asection *sec, struct bfd_link_info *link_info)
 		    }
 		}
 
-	      source_offset = offset_with_removed_text
+	      source_offset = offset_with_removed_text_map
 		(&relax_info->action_list, irel->r_offset);
 	      irel->r_offset = source_offset;
 	    }
@@ -9352,7 +9467,7 @@ relax_section (bfd *abfd, asection *sec, struct bfd_link_info *link_info)
 		      break;
 		    }
 
-		  new_end_offset = offset_with_removed_text
+		  new_end_offset = offset_with_removed_text_map
 		    (&target_relax_info->action_list,
 		     r_rel.target_offset + diff_value);
 		  diff_value = new_end_offset - new_reloc.target_offset;
@@ -9750,7 +9865,6 @@ translate_reloc (const r_reloc *orig_rel, r_reloc *new_rel, asection *sec)
   xtensa_relax_info *relax_info;
   removed_literal *removed;
   bfd_vma target_offset, base_offset;
-  text_action *act;
 
   *new_rel = *orig_rel;
 
@@ -9803,19 +9917,26 @@ translate_reloc (const r_reloc *orig_rel, r_reloc *new_rel, asection *sec)
      offset.  */
 
   base_offset = r_reloc_get_target_offset (new_rel) - new_rel->rela.r_addend;
-  act = relax_info->action_list.head;
   if (base_offset <= target_offset)
     {
-      int base_removed = removed_by_actions (&act, base_offset, FALSE);
-      int addend_removed = removed_by_actions (&act, target_offset, FALSE);
+      int base_removed = removed_by_actions_map (&relax_info->action_list,
+						 base_offset, FALSE);
+      int addend_removed = removed_by_actions_map (&relax_info->action_list,
+						   target_offset, FALSE) -
+	base_removed;
+
       new_rel->target_offset = target_offset - base_removed - addend_removed;
       new_rel->rela.r_addend -= addend_removed;
     }
   else
     {
       /* Handle a negative addend.  The base offset comes first.  */
-      int tgt_removed = removed_by_actions (&act, target_offset, FALSE);
-      int addend_removed = removed_by_actions (&act, base_offset, FALSE);
+      int tgt_removed = removed_by_actions_map (&relax_info->action_list,
+						target_offset, FALSE);
+      int addend_removed = removed_by_actions_map (&relax_info->action_list,
+						   base_offset, FALSE) -
+	tgt_removed;
+
       new_rel->target_offset = target_offset - tgt_removed;
       new_rel->rela.r_addend += addend_removed;
     }
@@ -10138,9 +10259,10 @@ relax_property_section (bfd *abfd,
 	      bfd_vma old_offset = val.r_rel.target_offset;
 	      bfd_vma new_offset;
 	      long old_size, new_size;
-	      text_action *act = target_relax_info->action_list.head;
-	      new_offset = old_offset -
-		removed_by_actions (&act, old_offset, FALSE);
+	      int removed_by_old_offset =
+		removed_by_actions_map (&target_relax_info->action_list,
+					old_offset, FALSE);
+	      new_offset = old_offset - removed_by_old_offset;
 
 	      /* Assert that we are not out of bounds.  */
 	      old_size = bfd_get_32 (abfd, size_p);
@@ -10164,9 +10286,10 @@ relax_property_section (bfd *abfd,
 
 		      /* Recompute the new_offset, but this time don't
 			 include any fill inserted by relaxation.  */
-		      act = target_relax_info->action_list.head;
-		      new_offset = old_offset -
-			removed_by_actions (&act, old_offset, TRUE);
+		      removed_by_old_offset =
+			removed_by_actions_map (&target_relax_info->action_list,
+						old_offset, TRUE);
+		      new_offset = old_offset - removed_by_old_offset;
 
 		      /* If it is not unreachable and we have not yet
 			 seen an unreachable at this address, place it
@@ -10182,8 +10305,12 @@ relax_property_section (bfd *abfd,
 		    }
 		}
 	      else
-		new_size -=
-		    removed_by_actions (&act, old_offset + old_size, TRUE);
+		{
+		  int removed_by_old_offset_size =
+		    removed_by_actions_map (&target_relax_info->action_list,
+					    old_offset + old_size, TRUE);
+		  new_size -= removed_by_old_offset_size - removed_by_old_offset;
+		}
 
 	      if (new_size != old_size)
 		{
@@ -10441,14 +10568,16 @@ relax_section_symbols (bfd *abfd, asection *sec)
 
       if (isym->st_shndx == sec_shndx)
 	{
-	  text_action *act = relax_info->action_list.head;
 	  bfd_vma orig_addr = isym->st_value;
+	  int removed = removed_by_actions_map (&relax_info->action_list,
+						orig_addr, FALSE);
 
-	  isym->st_value -= removed_by_actions (&act, orig_addr, FALSE);
-
+	  isym->st_value -= removed;
 	  if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC)
 	    isym->st_size -=
-	      removed_by_actions (&act, orig_addr + isym->st_size, FALSE);
+	      removed_by_actions_map (&relax_info->action_list,
+				      orig_addr + isym->st_size, FALSE) -
+	      removed;
 	}
     }
 
@@ -10466,15 +10595,17 @@ relax_section_symbols (bfd *abfd, asection *sec)
 	   || sym_hash->root.type == bfd_link_hash_defweak)
 	  && sym_hash->root.u.def.section == sec)
 	{
-	  text_action *act = relax_info->action_list.head;
 	  bfd_vma orig_addr = sym_hash->root.u.def.value;
+	  int removed = removed_by_actions_map (&relax_info->action_list,
+						orig_addr, FALSE);
 
-	  sym_hash->root.u.def.value -=
-	    removed_by_actions (&act, orig_addr, FALSE);
+	  sym_hash->root.u.def.value -= removed;
 
 	  if (sym_hash->type == STT_FUNC)
 	    sym_hash->size -=
-	      removed_by_actions (&act, orig_addr + sym_hash->size, FALSE);
+	      removed_by_actions_map (&relax_info->action_list,
+				      orig_addr + sym_hash->size, FALSE) -
+	      removed;
 	}
     }
 
-- 
1.8.1.4