summaryrefslogtreecommitdiff
path: root/command_line/C++_Parser/Src/Library/OptimizerHelpers.c
blob: 40853635a27a674ee08cc1943de0b239ea611082 (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
#include "parser.h"

Boolean setOldOptimizerOptions;

typedef struct {
    Boolean commonsubs;
    Boolean loopinvariants;
    Boolean propagation;
    Boolean deadstore;
    Boolean strengthreduction;
    Boolean deadcode;
    Boolean lifetimes;
} PIROptimizer; // assumed name

static PIROptimizer pIrOptimizer = {0};

Pragma irPragmas[] = {
        &pIrOptimizer.commonsubs, "opt_common_subs", 0,
        &pIrOptimizer.loopinvariants, "opt_loop_invariants", 0,
        &pIrOptimizer.propagation, "opt_propagation", 0,
        &pIrOptimizer.deadstore, "opt_dead_assignments", 0,
        &pIrOptimizer.deadcode, "opt_dead_code", 0,
        &pIrOptimizer.strengthreduction, "opt_strength_reduction", 0,
        &pIrOptimizer.lifetimes, "opt_lifetimes", 0,
        0, 0, 0
};

int SetPragmaOptimizationsToUnspecified(const char *opt, void *, const char *, int) {
    if (setOldOptimizerOptions)
        Option_Warning(CLPStr62);

    memset(&pIrOptimizer, 0, sizeof(pIrOptimizer));
    TargetSetPragmaOptimizationsToUnspecified();
    return 1;
}

int SetOptFlags(const char *opt, void *str, const char *, int flags) {
    // this is very similar to ToolHelper
    // might also fail to match
    unsigned char *ptr;
    Boolean set;
    Boolean no;
    UInt16 flag;

    ptr = (unsigned char *) str;
    no = (Boolean) ((flags & PARAMPARSEFLAGS_8) >> 3);
    set = (Boolean) (no ^ 1);

    while (*ptr) {
        if (*ptr == '+') {
            set = !no;
        } else if (*ptr == '-') {
            set = no;
        } else if (*ptr == '|') {
            set = (Boolean) (no ^ 1);
        } else {
            flag = (ptr[0] << 8) | ptr[1];

            if ((flag >= 'G0' && flag <= 'G4') || (flag == 'Gs') || (flag == 'Gp'))
                SetPragmaOptimizationsToUnspecified(opt, NULL, NULL, 0);

            switch (flag) {
                case 'Cs':
                    pIrOptimizer.commonsubs = set ? 1 : 2;
                    setOldOptimizerOptions = 1;
                    break;
                case 'Li':
                    pIrOptimizer.loopinvariants = set ? 1 : 2;
                    setOldOptimizerOptions = 1;
                    break;
                case 'Pr':
                    pIrOptimizer.propagation = set ? 1 : 2;
                    setOldOptimizerOptions = 1;
                    break;
                case 'Ds':
                    pIrOptimizer.deadstore = set ? 1 : 2;
                    setOldOptimizerOptions = 1;
                    break;
                case 'Sr':
                    pIrOptimizer.strengthreduction = set ? 1 : 2;
                    setOldOptimizerOptions = 1;
                    break;
                case 'Dc':
                    pIrOptimizer.deadcode = set ? 1 : 2;
                    setOldOptimizerOptions = 1;
                    break;
                case 'Lt':
                    pIrOptimizer.lifetimes = set ? 1 : 2;
                    setOldOptimizerOptions = 1;
                    break;
                case 'G0':
                    pGlobalOptimizer.optimizationlevel = set ? 0 : 0;
                    break;
                case 'G1':
                    pGlobalOptimizer.optimizationlevel = set ? 1 : 0;
                    break;
                case 'G2':
                    pGlobalOptimizer.optimizationlevel = set ? 2 : 0;
                    break;
                case 'G3':
                    pGlobalOptimizer.optimizationlevel = set ? 3 : 0;
                    break;
                case 'G4':
                    pGlobalOptimizer.optimizationlevel = set ? 4 : 0;
                    break;
                case 'Gs':
                    pGlobalOptimizer.optfor = set ? 1 : 0;
                    break;
                case 'Gp':
                    pGlobalOptimizer.optfor = set ? 0 : 1;
                    break;
                default:
                    if (!TargetSetOptFlags(flag, set))
                        CLPFatalError("Bad optimizer settings in %s (%c%c)\n", str, ptr[0], ptr[1]);
            }

            ++ptr;
        }

        ++ptr;
    }

    return 1;
}

int DisplayOptimizationOptions(const char *, void *, const char *, int) {
    SInt32 oldsize;
    Handle h;

    h = NewHandle(0);
    if (!h)
        exit(-23);

    HPrintF(h, "\t- global optimizer level %d\n", pGlobalOptimizer.optimizationlevel);
    HPrintF(h, "\t- global optimize for %s\n", (pGlobalOptimizer.optfor == 0) ? "speed" : "size");

    oldsize = GetHandleSize(h);
    if (pIrOptimizer.commonsubs)
        HPrintF(h, "\t- common subexpression elimination %s\n", (pIrOptimizer.commonsubs == 1) ? "on" : "off");
    if (pIrOptimizer.loopinvariants)
        HPrintF(h, "\t- loop invariants %s\n", (pIrOptimizer.loopinvariants == 1) ? "on" : "off");
    if (pIrOptimizer.commonsubs)
        HPrintF(h, "\t- constant propagation %s\n", (pIrOptimizer.propagation == 1) ? "on" : "off");
    if (pIrOptimizer.deadstore)
        HPrintF(h, "\t- dead store elimination %s\n", (pIrOptimizer.deadstore == 1) ? "on" : "off");
    if (pIrOptimizer.deadcode)
        HPrintF(h, "\t- dead code elimination %s\n", (pIrOptimizer.deadcode == 1) ? "on" : "off");
    if (pIrOptimizer.strengthreduction)
        HPrintF(h, "\t- strength reduction %s\n", (pIrOptimizer.strengthreduction == 1) ? "on" : "off");
    if (pIrOptimizer.lifetimes)
        HPrintF(h, "\t- variable lifetimes %s\n", (pIrOptimizer.lifetimes == 1) ? "on" : "off");

    if (oldsize == GetHandleSize(h))
        HPrintF(h, "\t- no extra global optimizations\n");

    HPrintF(h, "Backend-specific optimizer options:\n");

    oldsize = GetHandleSize(h);
    TargetDisplayOptimizationOptions(h);
    if (oldsize == GetHandleSize(h))
        HPrintF(h, "\t- no extra backend-specific optimizations\n");

    ShowTextHandle(NULL, h);
    DisposeHandle(h);
    return 1;
}