You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Optimise min/max to avoid unnecessary temporary variables (pyccel#2028)
# PR Summary
Fixes: pyccel#2025
The PR optimizes the generated C code by reducing unnecessary temporary
variables in min/max functions when direct variable inputs are used.
For example:
```python
if __name__ == '__main__':
a = 3
b = 4
c = min(a,b)
d = max(a,b)
e = min(a+b, a-b)
```
previously leads to the following generated output:
```C
int main()
{
int64_t a;
int64_t b;
int64_t c;
int64_t d;
int64_t e;
int64_t Dummy_0000;
int64_t Dummy_0001;
int64_t Dummy_0002;
int64_t Dummy_0003;
int64_t Dummy_0004;
int64_t Dummy_0005;
a = INT64_C(3);
b = INT64_C(4);
Dummy_0000 = a;
Dummy_0001 = b;
c = (Dummy_0000 < Dummy_0001 ? Dummy_0000 : Dummy_0001);
Dummy_0002 = a;
Dummy_0003 = b;
d = (Dummy_0002 > Dummy_0003 ? Dummy_0002 : Dummy_0003);
Dummy_0004 = a + b;
Dummy_0005 = a - b;
e = (Dummy_0004 < Dummy_0005 ? Dummy_0004 : Dummy_0005);
return 0;
}
```
But now it will lead to the following generated output:
```C
int main()
{
int64_t a;
int64_t b;
int64_t c;
int64_t d;
int64_t e;
int64_t Dummy_0000;
int64_t Dummy_0001;
a = INT64_C(3);
b = INT64_C(4);
c = (a < b ? a : b);
d = (a > b ? a : b);
Dummy_0000 = a + b;
Dummy_0001 = a - b;
e = (Dummy_0000 < Dummy_0001 ? Dummy_0000 : Dummy_0001);
return 0;
}
```
---------
Signed-off-by: Emmanuel Ferdman <[email protected]>
Co-authored-by: Emily Bourne <[email protected]>
0 commit comments