summaryrefslogtreecommitdiffstats
path: root/llvm/include/tracer.h
blob: 2813e0ec1d4006dfb24183ccfd29df3c64fbd7cf (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
/*
 *  (C) 2016 by Computer System Laboratory, IIS, Academia Sinica, Taiwan.
 *      See COPYRIGHT in top-level directory.
 */

#ifndef __TRACE_H
#define __TRACE_H

#include <vector>
#include <iostream>
#include "qemu-types.h"
#include "optimization.h"
#include "utils.h"


/* 
 * Base processor tracer
 */
class BaseTracer {
public:
    CPUArchState *Env;
    void *Perf;

    BaseTracer(CPUArchState *env) : Env(env), Perf(nullptr) {}
    virtual ~BaseTracer() {}
    virtual void Reset() {}
    virtual void Record(uintptr_t next_tb, TranslationBlock *tb) {}

    /* Create and return the tracer object based on LLVM_MODE. */
    static BaseTracer *CreateTracer(CPUArchState *env);

    /* Release the trace resources. */
    static void DeleteTracer(CPUArchState *env);
};


/*
 * Trace of a single basic block
 */
class SingleBlockTracer : public BaseTracer {
    TranslationBlock *TB;

public:
    SingleBlockTracer(CPUArchState *env);

    void Record(uintptr_t next_tb, TranslationBlock *tb) override;
};


/*
 * Trace with NET trace formation algorithm
 */
#define NET_PROFILE_THRESHOLD 50
#if defined(CONFIG_SOFTMMU)
#  define NET_PREDICT_THRESHOLD 16
#else
#  define NET_PREDICT_THRESHOLD 64
#endif
class NETTracer : public BaseTracer {
    bool isTraceHead(uintptr_t next_tb, TranslationBlock *tb, bool NewTB);

public:
    typedef std::vector<TranslationBlock *> TBVec;
    TBVec TBs;

    NETTracer(CPUArchState *env, int Mode);
    ~NETTracer();

    void Reset() override;
    void Record(uintptr_t next_tb, TranslationBlock *tb) override;
    inline void Profile(TranslationBlock *tb);
    inline void Predict(TranslationBlock *tb);
};

/* Return the address of the patch point to the trace code. */
static inline uintptr_t tb_get_jmp_entry(TranslationBlock *tb) {
    return (uintptr_t)tb->tc_ptr + tb->patch_jmp;
}
/* Return the initial jump target address of the patch point. */
static inline uintptr_t tb_get_jmp_next(TranslationBlock *tb) {
    return (uintptr_t)tb->tc_ptr + tb->patch_next;
}
static inline SingleBlockTracer &getSingleBlockTracer(CPUArchState *env) {
    return *static_cast<SingleBlockTracer *>(cpu_get_tracer(env));
}
static inline NETTracer &getNETTracer(CPUArchState *env) {
    return *static_cast<NETTracer *>(cpu_get_tracer(env));
}

static inline void delete_image(TranslationBlock *tb)
{
#if defined(CONFIG_LLVM) && defined(CONFIG_SOFTMMU)
    delete (char *)tb->image;
    tb->image = nullptr;
#endif
}

static inline bool update_tb_mode(TranslationBlock *tb, int from, int to) {
    if (tb->mode != from)
        return false;
    return Atomic<int>::testandset(&tb->mode, from, to);
}

#endif

/*
 * vim: ts=8 sts=4 sw=4 expandtab
 */

OpenPOWER on IntegriCloud