6.2 Common Subexpression Elimination (CSE)
6.2.1 Approach 1: Quantification Normalisation
The first approach exploits normalisation of quantifications in order to detect common subexpressions. First, quantifiers are normalised (prior to flattening) by renaming quan- tifiers and creating ahash-tableof all those quantifiers that range over thesamequantify- ing domain. Second, this hash-table is employed during flattening where to-be-flattened subexpressions are reformulated according to common quantifiers.
Quantification Normalisation
The normalisation of quantifiers has two aims: first, renaming quantifiers that range over the same domain so as to render them identical. For instance, consider the two quantifica- tions
f o r a l l j:i n t ( 1 . .n) . x[j] != y[j]
where both‘i’ and‘j’ range over‘(1..n)’, and normalisation renames‘j’ into‘i’, yielding
two identical subexpressions‘x[i]’.
f o r a l l i:i n t ( 1 . .n) . x[i] != i,
f o r a l l i:i n t ( 1 . .n) . x[i] != y[i]
The second aim is to rename all quantifying variables that occur elsewhere in the problem class, ranging over a different domain. As an example, in
f o r a l l s:i n t ( 0 . .n) . x[s] != s,
f o r a l l s:i n t ( 1 . .n−1).
f o r a l l j:i n t ( 0 . .m) . x[s] < y[j]+1
quantifier‘s’ ranges over different domains and is hence the latter occurrence is renamed:
f o r a l l s:i n t ( 0 . .n) . x[s] != s,
f o r a l l s1:i n t ( 1 . .n−1).
f o r a l l j:i n t ( 0 . .m) . x[s1] < y[j]+1
The third aim is to store all quantifying variables that range over the same domain, but which cannot be renamed. We denote such quantifying variablescommonquantifying vari- ables. For illustration, consider the excerpt of then-queens problem class (Example 6.1.1) again, where the quantifying variables ‘i’ and ‘j’ are common since they range over the
same domain,‘int (1..n)’, but which cannot be renamed:
f o r a l l i,j : i n t ( 1 . .n) .
(i < j) => (queens[i] + i != queens[j]+ j)
During normalisation, a hash-table is created that stores all common quantifying variables by mapping the quantifying domain to a list of the respective common quantifying vari- ables. In he example above, the hash-table have an entry‘int (1..n)’−→‘[i,j]’.
An Algorithm for Normalisation Quantifier Normalisation can be achieved by iterating
once over all constraints/subexpressions in the constraint model while collecting/matching quantifiers and their domains. The algorithm NORMALISE Q(L) takes a list of constraints Lwhere each quantificationqin every constraintcis normalised.
NORMALISE Q(L)
2. create an empty listQ 3. for each constraintcin listL
for each quantificationqin constraintc
for each quantifying variableiin quantificationq
(a) ifi’s domainDhas an entry in the hashmap, obtain themcommon quantifiers j1, . . . , jm and iterate over them:
i. ifiandjkare not in the same scope (with1≤k ≤m) renameitojk.
ii. otherwise, if allmcommon quantifiers are in the same scope (i.e. icannot be renamed) add i to the list of quantifiers,Q, and add an entry to hash- tableHof the formD−→[j1, . . . jm, i].
(b) otherwise
i. if the list of quantifiers,Q, contains a quantifier of the same name asi, then iteratively renameitoit(wheretis an integer, starting from 0) untilithas
no other entry inQ. Then additto Qand add the mappingD −→ [it]to
hash-tableH.
ii. otherwise add i to the list of quantifiers Q and add a mapping from i’s domainDtoiof the formD−→[i]into hash-tableH.
Eliminating Common Subexpressions Using Common Quantifying Variables
After normalising quantifiers as described above, the following statements hold for every normalised problem class:
• For every quantifier iin the problem class, there exists no other quantifier named i that ranges over a different domain
• If two quantifiersiandj (wherei%=j) range over the same domainD, then there is an entry in the quantifier list of the formD−→[i, j]
• There exist no two quantifying variablesiandjwhereiandjare in different scopes and range over the same domain.
Therefore, when flattening a quantified subexpression E that is quantified by variables i1, . . . , in , we know that any equivalent expressionoutsideE’s scope (equivalent wrt the
quantifying variables) is identical and can hence be matched using the hash-table approach from instance level. Furthermore, we know that any equivalent expressioninsideE’s scope (equivalent wrt the quantifying variables) can be detected by renaming E’s quantifying variables to the names of their common quantifying variables that are obtained from hash- tableH. Hence, we extend the recursive algorithm FLATTEN CLASS(Alg. 5.1) to algo- rithm FLATTEN CLASS CSE1 (E,bool) that performs the following additional operations: FLATTEN CLASS CSE1 (E,bool)
1. if e has a common subexpression in the hash-table, replace e with the respective auxiliary variableaux, otherwise goto 2
2. ifehas no common subexpression in the hash-table, andeis quantified by quantify- ing variablesi1, . . . , ingoto 3, otherwise goto 4.
3. for each quantifying variableikthat quantifiese(where1≤k ≤n):
(a) retrieve the list of common quantifiers L of ik from the common quantifiers
hash-tableH.
(b) if L is empty, stop. Otherwise iterate over the list of common quantifiers j1, . . . , jm:
i. replace every occurrence of i with jl (1 ≤ l ≤ m) in e, yielding e! and
check for a common subexpression ofe!.
ii. if successful, retrieve the corresponding auxiliary variable and use it fore and return.
4. flatteneto an auxiliary variableauxand replaceewithauxand return.
Summary
In summary, the first approach performs two essential steps: first, quantifiers are normalised so that every quantifying variable is unique and common quantifying variables (i.e. those that range over the same domain) are stored in a hash-table. The second step is the ex- ploitation of the hash-table when flattening a subexpressione: Whenever a subexpressione is flattened, the list of common quantifiers is used in order to reformulateewith a common quantifier in order to match identical subexpressions. This approach is easy to implement but can be very impractical if the number of equivalent quantifiers is very high: performing normalisation and checking all possible combinations of common quantifiers in a subex- pression can be quite expensive. Therefore, we want to investigate alternative approaches that reduce the detection effort.