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

#ifndef __LLVM_H
#define __LLVM_H

#include <memory>
#include <vector>
#include "llvm/ADT/STLExtras.h"
#include "llvm-types.h"
#include "llvm-debug.h"
#include "utils.h"

#if defined(ENABLE_MCJIT)
#include "llvm/ExecutionEngine/MCJIT.h"
#include "MCJITMemoryManager.h"
typedef class DefaultMCJITMemoryManager MemoryManager;
#else
#if defined(LLVM_V35)
#include "JIT.h"
#include "JITMemoryManager.h"
#else
#  error "LLVM version >3.5 supports MCJIT only. ENABLE_MCJIT must be enabled."
#endif
typedef class DefaultJITMemoryManager MemoryManager;
#endif


extern cl::OptionCategory CategoryHQEMU;

class LLVMTranslator;
class OptimizationInfo;
class TranslatedCode;

typedef std::unique_ptr<OptimizationInfo> OptRequest;


/*
 * LLVMEnv is the top level container of whole LLVM translation environment
 * which manages the LLVM translator(s) and globally shared resources. The
 * LLVMEnv instance must be initialized before using the underlying transaltion
 * service and can only be initialized ONCE.
 */
class LLVMEnv {
public:
    typedef std::vector<TranslatedCode *> TransCodeList;
    typedef std::map<uintptr_t, TranslatedCode *> TransCodeMap;
    typedef std::vector<uintptr_t> ChainSlot;
    typedef std::pair<size_t, uintptr_t> SlotInfo;

private:
    std::shared_ptr<MemoryManager> MM;        /* Trace cache manager */
    unsigned NumTranslator;                   /* The amount of LLVM translators */
    std::vector<LLVMTranslator *> Translator; /* LLVM translators */
    std::vector<pthread_t> HelperThread;      /* LLVM translation threads */
    std::vector<CPUState *> ThreadEnv;

    TransCodeList TransCode;  /* Translated traces. */
    TransCodeMap SortedCode;  /* Sorted traces in code cache address order. */
    ChainSlot ChainPoint;     /* Address of stubs for trace-to-block linking */

    bool UseThreading; /* Whether multithreaded translators are used or not. */
    unsigned NumFlush;

    LLVMEnv();

    /* Parse the command line options. */
    void ParseCommandLineOptions();

    /* Test whether HQEMU is running in Intel VTune. */
    void ProbeIntelVTune();

public:
    QemuMutex mutex;

    ~LLVMEnv();

    /* Start/stop/restart LLVM translators and worker threads. */
    void CreateTranslator();
    void DeleteTranslator();
    void RestartTranslator();
    void StartThread();
    void StopThread();

    /* Get the LLVM translator with index. */
    LLVMTranslator *getTranslator(unsigned ID) {
        if (ID >= Translator.size())
            hqemu_error("invalid translator ID.\n");
        return Translator[ID];
    }

    /* Acquire and lock the first LLVM translator. */
    LLVMTranslator *AcquireSingleTranslator();

    /* Release the first LLVM translator. */
    void ReleaseSingleTranslator();

    /* Get CPUState of the LLVM translator with index. */
    CPUState *getThreadEnv(int ID)              { return ThreadEnv[ID];  }

    std::vector<pthread_t> &getHelperThread()   { return HelperThread;   }
    std::shared_ptr<MemoryManager> getMemoryManager() { return MM;       }
    TransCodeList &getTransCode()               { return TransCode;      }
    TransCodeMap &getSortedCode()               { return SortedCode;     }
    ChainSlot &getChainPoint()                  { return ChainPoint;     }
    TraceID insertTransCode(TranslatedCode *TC);
    SlotInfo getChainSlot();

    bool isThreading()     { return UseThreading;      }
    void incNumFlush()     { NumFlush++;               }
    unsigned getNumFlush() { return NumFlush;          }

    /*
     * static public members
     */
    static bool InitOnce;  /* LLVMEnv is initialized or not? */
    static int TransMode;
    static uint8_t *TraceCache;
    static size_t TraceCacheSize;
    static bool RunWithVTune;

    static void CreateLLVMEnv();
    static void DeleteLLVMEnv();
    static int OptimizeBlock(CPUArchState *env, OptRequest Request);
    static int OptimizeTrace(CPUArchState *env, OptRequest Request);
    static void setTransMode(int Mode) { TransMode = Mode; }
    static int isTraceMode() {
        return (TransMode == TRANS_MODE_HYBRIDS ||
                TransMode == TRANS_MODE_HYBRIDM);
    }
};

class QueueManager {
    std::vector<Queue *> ActiveQueue;
    Queue *CurrentQueue;

public:
    QueueManager();
    ~QueueManager();
    void Enqueue(OptimizationInfo *Opt);
    void *Dequeue();
    void Flush();
};

/*
 * OptimizationInfo is the description to an optimization request. It consists
 * of the optimization mode and the control-flow-graph of the trace.
 */
class OptimizationInfo {
public:
    typedef std::set<TranslationBlock *> TraceNode;
    typedef std::map<TranslationBlock *, TraceNode> TraceEdge;

    ~OptimizationInfo() {
        if (CFG)
            GraphNode::DeleteCFG(CFG);
    }

    void ComposeCFG();
    GraphNode *getCFG()    { return CFG;      }
    bool isTrace()         { return !isBlock; }

    static OptRequest CreateRequest(TranslationBlock *tb) {
        return OptRequest(new OptimizationInfo(tb));
    }
    static OptRequest CreateRequest(TBVec &trace, int idx) {
        return OptRequest(new OptimizationInfo(trace, idx));
    }
    static OptRequest CreateRequest(TranslationBlock *head, TraceEdge &edges) {
        return OptRequest(new OptimizationInfo(head, edges));
    }

private:
    TBVec Trace;       /* Trace of a list of TBs */
    int LoopHeadIdx;   /* Index to the loopback block */
    bool isUserTrace;  /* Trace of all user-mode blocks */
    bool isBlock;      /* Trace of a single block */
    GraphNode *CFG;    /* CFG of the trace */

    OptimizationInfo(TranslationBlock *tb)
        : isUserTrace(true), isBlock(true) {
        Trace.push_back(tb);
        LoopHeadIdx = -1;
        CFG = new GraphNode(tb);
    }
    OptimizationInfo(TBVec &trace, int idx)
        : isUserTrace(true), isBlock(false), CFG(nullptr) {
        if (trace.empty())
            hqemu_error("trace length cannot be zero.\n");
        Trace = trace;
        LoopHeadIdx = idx;
    }
    OptimizationInfo(TranslationBlock *HeadTB, TraceEdge &Edges);

    void SearchCycle(TraceNode &SearchNodes, TraceNode &Nodes,
                     TraceEdge &Edges, TBVec &Visited, int Depth);
    void ExpandTrace(TranslationBlock *HeadTB, TraceEdge &Edges);
};

class TraceInfo {
public:
    TBVec TBs;
    unsigned NumLoop;
    unsigned NumExit;
    unsigned NumIndirectBr;
    uint64_t **ExecCount;
    uint64_t TransTime;
    uint32_t Attribute;

    TraceInfo(NodeVec &Nodes, uint32_t Attr = A_None)
        : NumLoop(0), NumExit(0), NumIndirectBr(0), ExecCount(nullptr),
          TransTime(0), Attribute(Attr)
    {
        if (Nodes.empty())
            hqemu_error("number of nodes cannot be zero.\n");
        for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
            TBs.push_back(Nodes[i]->getTB());
    }

    TranslationBlock *getEntryTB() { return TBs[0]; }
    target_ulong getEntryPC() { return TBs[0]->pc; }
    unsigned getNumBlock()    { return TBs.size(); }
    void setTransTime(struct timeval *start, struct timeval *end) {
        struct timeval t;
        timersub(end, start, &t);
        TransTime = t.tv_sec * 1e6 + t.tv_usec;
    }
    bool hasAttribute(uint32_t Attr) {
        return Attribute & Attr;
    }
};

struct ChainInfo {
    std::vector<uintptr_t> Chains;
    std::vector<BlockID> DepTraces;

    void insertChain(uintptr_t addr) {
        Chains.push_back(addr);
    }
    void insertDepTrace(BlockID id) {
        DepTraces.push_back(id);
    }
    static ChainInfo *get(TranslationBlock *tb) {
        if (!tb->chain)
            tb->chain = (ChainInfo *)new ChainInfo;
        return (ChainInfo *)tb->chain;
    }
    static void free(TranslationBlock *tb) {
        delete (ChainInfo *)tb->chain;
        tb->chain = nullptr;
    }
};

class TranslatedCode {
public:
    TranslatedCode() : Trace(nullptr), SampleCount(0) {}
    ~TranslatedCode() {
        if (Trace)
            delete Trace;
    }

    bool Active;
    uint32_t Size;             /* Size of the translated host code */
    uint8_t *Code;             /* Start PC of the translated host code */
    TranslationBlock *EntryTB; /* The entry block of the region */
    RestoreVec Restore;
    TraceInfo *Trace;
    uint64_t SampleCount;
};


#endif

/*
 * vim: ts=8 sts=4 sw=4 expandtab
 */
OpenPOWER on IntegriCloud