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

#ifndef __LLVM_PASS_H
#define __LLVM_PASS_H

#include <map>
#include <vector>
#include "llvm-types.h"

class IRFactory;


static inline Value *getPointerOperand(Value *I) {
    if (LoadInst *LI = dyn_cast<LoadInst>(I))
        return LI->getPointerOperand();
    if (StoreInst *SI = dyn_cast<StoreInst>(I))
        return SI->getPointerOperand();
    return nullptr;
}

static inline Value *getValueOperand(Value *I) {
    if (LoadInst *LI = dyn_cast<LoadInst>(I))
        return LI;
    if (StoreInst *SI = dyn_cast<StoreInst>(I))
        return SI->getValueOperand();
    return nullptr;
}

static inline unsigned getAddressSpaceOperand(Value *I) {
    if (LoadInst *LI = dyn_cast<LoadInst>(I))
        return LI->getPointerAddressSpace();
    if (StoreInst *SI = dyn_cast<StoreInst>(I))
        return SI->getPointerAddressSpace();
    return -1;
}

/* A CPU state reference. */
struct StateRef {
    StateRef(intptr_t Start, intptr_t End, Instruction *I)
        : Start(Start), End(End), I(I) {}
    intptr_t Start;
    intptr_t End;
    Instruction *I;

    intptr_t getSize() {
        return End - Start;
    }
    Type *getType() {
        return getValueOperand(I)->getType();
    }
};

/* A group of references to a CPU state. */
struct StateData {
    intptr_t Start;
    intptr_t End;
    std::vector<StateRef*> Refs;

    void reset(StateRef &Ref) {
        Start = Ref.Start;
        End = Ref.End;
        Refs.clear();
        Refs.push_back(&Ref);
    }
    void insert(StateRef &Ref) {
        End = std::max(End, Ref.End);
        Refs.push_back(&Ref);
    }
};

typedef std::map<intptr_t, intptr_t> StateRange;
typedef std::vector<StateData> StateList;
typedef std::vector<CallInst*> CallList;

/*
 * The purpose of StateAnalyzer is to analyze loads/stores of CPU states and
 * group loads/stores of the same CPU state into the same bucket (StateData).
 */
class StateAnalyzer {
    const DataLayout *DL;
    std::vector<StateRef> StateRefs;
    CallList Calls;
    StateList States;

    /* Sort state references by the state offset. */
    void sortStateRefs() {
        if (StateRefs.empty())
            return;
        std::sort(StateRefs.begin(), StateRefs.end(),
                  [](const StateRef &lhs, const StateRef &rhs) -> bool {
                     return lhs.Start < rhs.Start;
                  });
    }

public:
    StateAnalyzer(const DataLayout *DL) : DL(DL) {}

    void clear() {
        StateRefs.clear();
        Calls.clear();
        States.clear();
    }

    /* Add a CPU state reference. */
    void addStateRef(Instruction *I, intptr_t Off) {
        Type *Ty = getValueOperand(I)->getType();
        intptr_t Start = Off;
        intptr_t End = Off + DL->getTypeSizeInBits(Ty) / 8;
        StateRefs.push_back(StateRef(Start, End, I));
    }

    /* Add a helper function call. */
    void addCall(CallInst *CI) {
        Calls.push_back(CI);
    }

    /* Return non-overlapped ranges of states. */
    void computeStateRange(StateRange &Reads, StateRange &Writes) {
        computeState();
        if (StateRefs.empty())
            return;

        const uint8_t READ  = 0x1;
        const uint8_t WRITE = 0x2;
        for (auto &State : States) {
            uint8_t RW = 0;
            for (auto &Ref : State.Refs)
                RW |= isa<LoadInst>(Ref->I) ? READ : WRITE;
            if (RW & READ)
                Reads[State.Start] = State.End;
            if (RW & WRITE)
                Writes[State.Start] = State.End;
        }
    }

    /* Compute referenced states and group instructions. */
    void computeState() {
        /* Sort state refs by the offset. */
        sortStateRefs();
        if (StateRefs.empty())
            return;

        StateData State;
        State.reset(StateRefs.front());
        for (unsigned i = 1, e = StateRefs.size(); i != e; ++i) {
            StateRef &Next = StateRefs[i];
            if (State.End <= Next.Start) {
                /* The next reference is not overlapped with the previous
                 * reference. A new state is found. */
                States.push_back(State);
                /* Reset Curr to the next state. */
                State.reset(Next);
            } else {
                /* Overlap and merge. */
                State.insert(Next);
            }
        }
        /* The last state. */
        States.push_back(State);
    }

    StateList &getStateList() {
        return States;
    }

    CallList &getCalls() {
        return Calls;
    }
};


namespace llvm {
/* Passes */
FunctionPass *createReplaceIntrinsic();
FunctionPass *createFastMathPass();
FunctionPass *createProfileExec(IRFactory *IF);
FunctionPass *createStateMappingPass(IRFactory *IF);
FunctionPass *createRedundantStateElimination(IRFactory *IF);
FunctionPass *createCombineGuestMemory(IRFactory *IF);
FunctionPass *createCombineCasts(IRFactory *IF);
FunctionPass *createCombineZExtTrunc();
FunctionPass *createSimplifyPointer(IRFactory *IF);

void initializeReplaceIntrinsicPass(llvm::PassRegistry&);
void initializeFastMathPassPass(llvm::PassRegistry&);
void initializeProfileExecPass(llvm::PassRegistry&);
void initializeStateMappingPassPass(llvm::PassRegistry&);
void initializeRedundantStateEliminationPass(llvm::PassRegistry&);
void initializeCombineGuestMemoryPass(llvm::PassRegistry&);
void initializeCombineCastsPass(llvm::PassRegistry&);
void initializeCombineZExtTruncPass(llvm::PassRegistry&);
void initializeSimplifyPointerPass(llvm::PassRegistry&);

/* Analysis */
void initializeInnerLoopAnalysisWrapperPassPass(llvm::PassRegistry&);
}

#endif

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