[PATCH v3 3/4] perf annotate: add powerpc support
From: Ravi Bangoria
Date: Thu Jun 30 2016 - 03:54:10 EST
From: Naveen N. Rao <naveen.n.rao@xxxxxxxxxxxxxxxxxx>
Powerpc has long list of branch instructions and hardcoding them in
table appears to be error-prone. So, add new function to find
instruction instead of creating table. This function dynamically
create table(list of 'struct ins'), and instead of creating object
every time, first check if list already contain object for that
instruction.
Signed-off-by: Naveen N. Rao <naveen.n.rao@xxxxxxxxxxxxxxxxxx>
Signed-off-by: Ravi Bangoria <ravi.bangoria@xxxxxxxxxxxxxxxxxx>
---
Changes in v3:
- Optimized code
- Corrected one memory leak
tools/perf/util/annotate.c | 126 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 126 insertions(+)
diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index 36a5825..b87eac7 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -461,6 +461,11 @@ static struct ins instructions_arm[] = {
{ .name = "bne", .ops = &jump_ops, },
};
+struct instructions_powerpc {
+ struct ins *ins;
+ struct list_head list;
+};
+
static int ins__key_cmp(const void *name, const void *insp)
{
const struct ins *ins = insp;
@@ -476,6 +481,125 @@ static int ins__cmp(const void *a, const void *b)
return strcmp(ia->name, ib->name);
}
+static struct ins *list_add__ins_powerpc(struct instructions_powerpc *head,
+ const char *name, struct ins_ops *ops)
+{
+ struct instructions_powerpc *ins_powerpc;
+ struct ins *ins;
+
+ ins = zalloc(sizeof(struct ins));
+ if (!ins)
+ return NULL;
+
+ ins_powerpc = zalloc(sizeof(struct instructions_powerpc));
+ if (!ins_powerpc)
+ goto out_free_ins;
+
+ ins->name = strdup(name);
+ if (!ins->name)
+ goto out_free_ins_power;
+
+ ins->ops = ops;
+ ins_powerpc->ins = ins;
+ list_add_tail(&(ins_powerpc->list), &(head->list));
+
+ return ins;
+
+out_free_ins_power:
+ zfree(&ins_powerpc);
+out_free_ins:
+ zfree(&ins);
+ return NULL;
+}
+
+static struct ins *list_search__ins_powerpc(struct instructions_powerpc *head,
+ const char *name)
+{
+ struct instructions_powerpc *pos;
+
+ list_for_each_entry(pos, &head->list, list) {
+ if (!strcmp(pos->ins->name, name))
+ return pos->ins;
+ }
+ return NULL;
+}
+
+static struct ins *ins__find_powerpc(const char *name)
+{
+ int i;
+ struct ins *ins;
+ struct ins_ops *ops;
+ static struct instructions_powerpc head;
+ static bool list_initialized;
+
+ /*
+ * - Interested only if instruction starts with 'b'.
+ * - Few start with 'b', but aren't branch instructions.
+ * - Let's also ignore instructions involving 'ctr' and
+ * 'tar' since target branch addresses for those can't
+ * be determined statically.
+ */
+ if (name[0] != 'b' ||
+ !strncmp(name, "bcd", 3) ||
+ !strncmp(name, "brinc", 5) ||
+ !strncmp(name, "bper", 4) ||
+ strstr(name, "ctr") ||
+ strstr(name, "tar"))
+ return NULL;
+
+ if (!list_initialized) {
+ INIT_LIST_HEAD(&head.list);
+ list_initialized = true;
+ }
+
+ /*
+ * Return if we already have object of 'struct ins' for this
+ * instruction
+ */
+ ins = list_search__ins_powerpc(&head, name);
+ if (ins)
+ return ins;
+
+ ops = &jump_ops;
+
+ i = strlen(name) - 1;
+ if (i < 0)
+ return NULL;
+
+ /* ignore optional hints at the end of the instructions */
+ if (name[i] == '+' || name[i] == '-')
+ i--;
+
+ if (name[i] == 'l' || (name[i] == 'a' && name[i-1] == 'l')) {
+ /*
+ * if the instruction ends up with 'l' or 'la', then
+ * those are considered 'calls' since they update LR.
+ * ... except for 'bnl' which is branch if not less than
+ * and the absolute form of the same.
+ */
+ if (strcmp(name, "bnl") && strcmp(name, "bnl+") &&
+ strcmp(name, "bnl-") && strcmp(name, "bnla") &&
+ strcmp(name, "bnla+") && strcmp(name, "bnla-"))
+ ops = &call_ops;
+ }
+ if (name[i] == 'r' && name[i-1] == 'l')
+ /*
+ * instructions ending with 'lr' are considered to be
+ * return instructions
+ */
+ ops = &ret_ops;
+
+ /*
+ * Add instruction to list so next time no need to
+ * allocate memory for it.
+ */
+ ins = list_add__ins_powerpc(&head, name, ops);
+ if (ins)
+ return ins;
+
+ return NULL;
+}
+
static void ins__sort(struct ins *instructions, int nmemb)
{
qsort(instructions, nmemb, sizeof(struct ins), ins__cmp);
@@ -511,6 +635,8 @@ static struct ins *ins__find(const char *name, const char *norm_arch)
} else if (!strcmp(norm_arch, "arm")) {
instructions = instructions_arm;
nmemb = ARRAY_SIZE(instructions_arm);
+ } else if (!strcmp(norm_arch, "powerpc")) {
+ return ins__find_powerpc(name);
} else {
pr_err("perf annotate not supported by %s arch\n", norm_arch);
return NULL;
--
2.5.5