block-range.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "block-range.h"
  3. #include "annotate.h"
  4. struct {
  5. struct rb_root root;
  6. u64 blocks;
  7. } block_ranges;
  8. static void block_range__debug(void)
  9. {
  10. /*
  11. * XXX still paranoid for now; see if we can make this depend on
  12. * DEBUG=1 builds.
  13. */
  14. #if 1
  15. struct rb_node *rb;
  16. u64 old = 0; /* NULL isn't executable */
  17. for (rb = rb_first(&block_ranges.root); rb; rb = rb_next(rb)) {
  18. struct block_range *entry = rb_entry(rb, struct block_range, node);
  19. assert(old < entry->start);
  20. assert(entry->start <= entry->end); /* single instruction block; jump to a jump */
  21. old = entry->end;
  22. }
  23. #endif
  24. }
  25. struct block_range *block_range__find(u64 addr)
  26. {
  27. struct rb_node **p = &block_ranges.root.rb_node;
  28. struct rb_node *parent = NULL;
  29. struct block_range *entry;
  30. while (*p != NULL) {
  31. parent = *p;
  32. entry = rb_entry(parent, struct block_range, node);
  33. if (addr < entry->start)
  34. p = &parent->rb_left;
  35. else if (addr > entry->end)
  36. p = &parent->rb_right;
  37. else
  38. return entry;
  39. }
  40. return NULL;
  41. }
  42. static inline void rb_link_left_of_node(struct rb_node *left, struct rb_node *node)
  43. {
  44. struct rb_node **p = &node->rb_left;
  45. while (*p) {
  46. node = *p;
  47. p = &node->rb_right;
  48. }
  49. rb_link_node(left, node, p);
  50. }
  51. static inline void rb_link_right_of_node(struct rb_node *right, struct rb_node *node)
  52. {
  53. struct rb_node **p = &node->rb_right;
  54. while (*p) {
  55. node = *p;
  56. p = &node->rb_left;
  57. }
  58. rb_link_node(right, node, p);
  59. }
  60. /**
  61. * block_range__create
  62. * @start: branch target starting this basic block
  63. * @end: branch ending this basic block
  64. *
  65. * Create all the required block ranges to precisely span the given range.
  66. */
  67. struct block_range_iter block_range__create(u64 start, u64 end)
  68. {
  69. struct rb_node **p = &block_ranges.root.rb_node;
  70. struct rb_node *n, *parent = NULL;
  71. struct block_range *next, *entry = NULL;
  72. struct block_range_iter iter = { NULL, NULL };
  73. while (*p != NULL) {
  74. parent = *p;
  75. entry = rb_entry(parent, struct block_range, node);
  76. if (start < entry->start)
  77. p = &parent->rb_left;
  78. else if (start > entry->end)
  79. p = &parent->rb_right;
  80. else
  81. break;
  82. }
  83. /*
  84. * Didn't find anything.. there's a hole at @start, however @end might
  85. * be inside/behind the next range.
  86. */
  87. if (!*p) {
  88. if (!entry) /* tree empty */
  89. goto do_whole;
  90. /*
  91. * If the last node is before, advance one to find the next.
  92. */
  93. n = parent;
  94. if (entry->end < start) {
  95. n = rb_next(n);
  96. if (!n)
  97. goto do_whole;
  98. }
  99. next = rb_entry(n, struct block_range, node);
  100. if (next->start <= end) { /* add head: [start...][n->start...] */
  101. struct block_range *head = malloc(sizeof(struct block_range));
  102. if (!head)
  103. return iter;
  104. *head = (struct block_range){
  105. .start = start,
  106. .end = next->start - 1,
  107. .is_target = 1,
  108. .is_branch = 0,
  109. };
  110. rb_link_left_of_node(&head->node, &next->node);
  111. rb_insert_color(&head->node, &block_ranges.root);
  112. block_range__debug();
  113. iter.start = head;
  114. goto do_tail;
  115. }
  116. do_whole:
  117. /*
  118. * The whole [start..end] range is non-overlapping.
  119. */
  120. entry = malloc(sizeof(struct block_range));
  121. if (!entry)
  122. return iter;
  123. *entry = (struct block_range){
  124. .start = start,
  125. .end = end,
  126. .is_target = 1,
  127. .is_branch = 1,
  128. };
  129. rb_link_node(&entry->node, parent, p);
  130. rb_insert_color(&entry->node, &block_ranges.root);
  131. block_range__debug();
  132. iter.start = entry;
  133. iter.end = entry;
  134. goto done;
  135. }
  136. /*
  137. * We found a range that overlapped with ours, split if needed.
  138. */
  139. if (entry->start < start) { /* split: [e->start...][start...] */
  140. struct block_range *head = malloc(sizeof(struct block_range));
  141. if (!head)
  142. return iter;
  143. *head = (struct block_range){
  144. .start = entry->start,
  145. .end = start - 1,
  146. .is_target = entry->is_target,
  147. .is_branch = 0,
  148. .coverage = entry->coverage,
  149. .entry = entry->entry,
  150. };
  151. entry->start = start;
  152. entry->is_target = 1;
  153. entry->entry = 0;
  154. rb_link_left_of_node(&head->node, &entry->node);
  155. rb_insert_color(&head->node, &block_ranges.root);
  156. block_range__debug();
  157. } else if (entry->start == start)
  158. entry->is_target = 1;
  159. iter.start = entry;
  160. do_tail:
  161. /*
  162. * At this point we've got: @iter.start = [@start...] but @end can still be
  163. * inside or beyond it.
  164. */
  165. entry = iter.start;
  166. for (;;) {
  167. /*
  168. * If @end is inside @entry, split.
  169. */
  170. if (end < entry->end) { /* split: [...end][...e->end] */
  171. struct block_range *tail = malloc(sizeof(struct block_range));
  172. if (!tail)
  173. return iter;
  174. *tail = (struct block_range){
  175. .start = end + 1,
  176. .end = entry->end,
  177. .is_target = 0,
  178. .is_branch = entry->is_branch,
  179. .coverage = entry->coverage,
  180. .taken = entry->taken,
  181. .pred = entry->pred,
  182. };
  183. entry->end = end;
  184. entry->is_branch = 1;
  185. entry->taken = 0;
  186. entry->pred = 0;
  187. rb_link_right_of_node(&tail->node, &entry->node);
  188. rb_insert_color(&tail->node, &block_ranges.root);
  189. block_range__debug();
  190. iter.end = entry;
  191. goto done;
  192. }
  193. /*
  194. * If @end matches @entry, done
  195. */
  196. if (end == entry->end) {
  197. entry->is_branch = 1;
  198. iter.end = entry;
  199. goto done;
  200. }
  201. next = block_range__next(entry);
  202. if (!next)
  203. goto add_tail;
  204. /*
  205. * If @end is in beyond @entry but not inside @next, add tail.
  206. */
  207. if (end < next->start) { /* add tail: [...e->end][...end] */
  208. struct block_range *tail;
  209. add_tail:
  210. tail = malloc(sizeof(struct block_range));
  211. if (!tail)
  212. return iter;
  213. *tail = (struct block_range){
  214. .start = entry->end + 1,
  215. .end = end,
  216. .is_target = 0,
  217. .is_branch = 1,
  218. };
  219. rb_link_right_of_node(&tail->node, &entry->node);
  220. rb_insert_color(&tail->node, &block_ranges.root);
  221. block_range__debug();
  222. iter.end = tail;
  223. goto done;
  224. }
  225. /*
  226. * If there is a hole between @entry and @next, fill it.
  227. */
  228. if (entry->end + 1 != next->start) {
  229. struct block_range *hole = malloc(sizeof(struct block_range));
  230. if (!hole)
  231. return iter;
  232. *hole = (struct block_range){
  233. .start = entry->end + 1,
  234. .end = next->start - 1,
  235. .is_target = 0,
  236. .is_branch = 0,
  237. };
  238. rb_link_left_of_node(&hole->node, &next->node);
  239. rb_insert_color(&hole->node, &block_ranges.root);
  240. block_range__debug();
  241. }
  242. entry = next;
  243. }
  244. done:
  245. assert(iter.start->start == start && iter.start->is_target);
  246. assert(iter.end->end == end && iter.end->is_branch);
  247. block_ranges.blocks++;
  248. return iter;
  249. }
  250. /*
  251. * Compute coverage as:
  252. *
  253. * br->coverage / br->sym->max_coverage
  254. *
  255. * This ensures each symbol has a 100% spot, to reflect that each symbol has a
  256. * most covered section.
  257. *
  258. * Returns [0-1] for coverage and -1 if we had no data what so ever or the
  259. * symbol does not exist.
  260. */
  261. double block_range__coverage(struct block_range *br)
  262. {
  263. struct symbol *sym;
  264. if (!br) {
  265. if (block_ranges.blocks)
  266. return 0;
  267. return -1;
  268. }
  269. sym = br->sym;
  270. if (!sym)
  271. return -1;
  272. return (double)br->coverage / symbol__annotation(sym)->max_coverage;
  273. }