-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathlast_use.cpp
More file actions
120 lines (111 loc) · 4.5 KB
/
Copy pathlast_use.cpp
File metadata and controls
120 lines (111 loc) · 4.5 KB
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
/*
* Main authors:
* Mats Carlsson <mats.carlsson@ri.se>
* Noric Couderc <noric@sics.se>
*
* This file is part of Unison, see http://unison-code.github.io
*
* Copyright (c) 2016, RISE SICS AB
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <functional>
#include "./last_use.hpp"
void last_use(PresolverAsserts& PA, Parameters& input) {
map<temporary, vector<operand>> M;
unsigned int prev_op_type = IN;
// For all o in JSON.O where o is mandatory
for(operation o : input.O) {
// For all p in OperUses(o) where p is mandatory
if(is_mandatory(input, o))
for (operand p : input.operands[o])
if (input.use[p]) {
// if TempDef(min(OpndTempsButNull(p))) is mandatory
temporary min_temp = first_temp_but_null(input, p);
if(first_temp(input, input.definer[min_temp]) != NULL_TEMPORARY) {
// AddToMap(min(OpndTempsButNull(p)) -> p, M)
M[min_temp].push_back(p);
}
}
unsigned int op_type = oper_type(input, o);
switch (op_type) {
case OUT:
case KILL:
// For all p in OperUses(o)
for (operand p : input.operands[o])
if (input.use[p])
// LastUse <- LastUse U {p}
input.last_use.push_back(p);
break;
// if OperType(o) = FUNCTION
case FUN:
if (prev_op_type != TAILCALL) // after tail-recursive (fun), there will be (out) which can reuse operands!
// For all p in OperUses(o)
for (operand p : input.operands[o])
if (input.use[p] && p_preassigned_caller_saved(input, p)) // caller-saved, but not reserved
input.last_use.push_back(p);
break;
case LINEAR:
// If there exist C in JSON.strictly_congr, def d in C, use u in C
// where {u, d} is a subset of OperOpnds(o)
// then LastUse <- LastUse U {u}
for (pair<operand,operand> ud : input.redefined[o])
input.last_use.push_back(ud.first);
break;
}
prev_op_type = op_type;
}
// For all _ -> P in M
for(const pair<temporary, vector<operand>>& kps : M) {
vector<operand> P = kps.second;
// b <- the block containing P
block b = block_containing(input, P);
if(b < 0) { continue; }
Digraph data_dependencies = PA.dd_graph[b];
// for all p in P where OperType(OpndOper(p)) != BRANCH while b >= 0
for(operand p : P) { // For all p in P
// Where OperType(OpndOper(p)) != BRANCH
if(oper_type(input, opnd_oper(input, p)) == BRANCH) {
continue;
}
// if for all q in P\{p}:
// OpndOper(p) in Reachable(OpndOper(q), DDGraph(b))
// DDGraph(b)
bool reachable = std::all_of(P.begin(), P.end(),
[&data_dependencies, &input, &p](operand q) {
if(p == q) { return true; }
vector<vertex> reachables =
data_dependencies.reachables(opnd_oper(input, q));
return ord_contains(reachables, opnd_oper(input, p));
});
if(reachable) {
input.last_use.push_back(p);
b = -1;
}
}
}
std::sort(input.last_use.begin(), input.last_use.end());
input.last_use.erase(unique(input.last_use.begin(), input.last_use.end()), input.last_use.end());
}