Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
f3b0b977
Commit
f3b0b977
authored
Dec 13, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simplify constant folded comparison code a little and extend the test for it
parent
311c0b12
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
9 deletions
+73
-9
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+6
-9
tests/run/constant_folding.py
tests/run/constant_folding.py
+67
-0
No files found.
Cython/Compiler/Optimize.py
View file @
f3b0b977
...
...
@@ -3307,19 +3307,13 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
# collect partial cascades: [[value, CmpNode...], [value, CmpNode, ...], ...]
cascades
=
[[
node
.
operand1
]]
final_result
=
[
True
]
def
split_cascades
(
cmp_node
):
if
cmp_node
.
has_constant_result
():
if
not
cmp_node
.
constant_result
:
# False => short-circuit
cascades
.
append
([
self
.
_bool_node
(
cmp_node
,
True
),
ExprNodes
.
CascadedCmpNode
(
cmp_node
.
pos
,
operator
=
'=='
,
operand2
=
self
.
_bool_node
(
cmp_node
,
False
),
constant_result
=
False
)
])
final_result
[
0
]
=
False
return
else
:
# True => discard and start new cascade
...
...
@@ -3351,7 +3345,10 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
last_cmp_node
=
cmp_node
last_cmp_node
.
cascade
=
None
if
not
cmp_nodes
:
if
not
final_result
[
0
]:
# last cascade was constant False
cmp_nodes
.
append
(
self
.
_bool_node
(
cmp_node
,
False
))
elif
not
cmp_nodes
:
# only constants, but no False result
return
self
.
_bool_node
(
node
,
True
)
node
=
cmp_nodes
[
0
]
...
...
tests/run/constant_folding.py
View file @
f3b0b977
...
...
@@ -361,3 +361,70 @@ def combined():
True
"""
return
5
in
100
*
[
1
,
2
]
*
0
or
5
not
in
100
*
[]
*
10
@
cython
.
test_assert_path_exists
(
'//IntNode[@value = "2"]'
,
'//IntNode[@value = "4"]'
,
'//IntNode[@value = "5"]'
,
'//IntNode[@value = "7"]'
,
'//BoolBinopNode//PrimaryCmpNode'
,
'//BoolBinopNode[.//PrimaryCmpNode//IntNode[@value = "4"] and .//PrimaryCmpNode//IntNode[@value = "5"]]'
,
'//PrimaryCmpNode[.//IntNode[@value = "2"] and .//IntNode[@value = "4"]]'
,
'//PrimaryCmpNode[.//IntNode[@value = "5"] and .//IntNode[@value = "7"]]'
,
)
@
cython
.
test_fail_if_path_exists
(
'//IntNode[@value = "1"]'
,
'//IntNode[@value = "8"]'
,
'//PrimaryCmpNode[.//IntNode[@value = "4"] and .//IntNode[@value = "5"]]'
,
'//PrimaryCmpNode[.//IntNode[@value = "2"] and .//IntNode[@value = "7"]]'
,
'//BoolNode'
,
)
def
cascaded_cmp_with_partial_constants
(
a
,
b
):
"""
>>> cascaded_cmp_with_partial_constants(3, 6)
True
>>> cascaded_cmp_with_partial_constants(1, 6)
False
>>> cascaded_cmp_with_partial_constants(4, 6)
False
>>> cascaded_cmp_with_partial_constants(3, 7)
False
>>> cascaded_cmp_with_partial_constants(3, 6)
True
"""
return
1
<
2
<
a
<
4
<
5
<
b
<
7
<
8
@
cython
.
test_assert_path_exists
(
'//IntNode[@value = "2"]'
,
'//IntNode[@value = "4"]'
,
'//IntNode[@value = "5"]'
,
'//IntNode[@value = "7"]'
,
'//BoolBinopNode'
,
'//SingleAssignmentNode//BoolBinopNode'
,
'//SingleAssignmentNode//BoolBinopNode//NameNode[@name = "a"]'
,
'//SingleAssignmentNode//BoolBinopNode//NameNode[@name = "b"]'
,
'//BoolBinopNode[.//PrimaryCmpNode//IntNode[@value = "4"] and .//PrimaryCmpNode//IntNode[@value = "5"]]'
,
'//BoolNode[@value = False]'
,
)
@
cython
.
test_fail_if_path_exists
(
'//SingleAssignmentNode//NameNode[@name = "c"]'
,
'//IntNode[@value = "1"]'
,
'//PrimaryCmpNode[.//IntNode[@value = "4"] and .//IntNode[@value = "5"]]'
,
'//PrimaryCmpNode[.//IntNode[@value = "2"] and .//IntNode[@value = "7"]]'
,
'//BoolNode[@value = True]'
,
)
def
cascaded_cmp_with_partial_constants_and_false_end
(
a
,
b
,
c
):
"""
>>> cascaded_cmp_with_partial_constants_and_false_end(3, 6, 8)
False
>>> cascaded_cmp_with_partial_constants_and_false_end(1, 6, 8)
False
>>> cascaded_cmp_with_partial_constants_and_false_end(4, 6, 8)
False
>>> cascaded_cmp_with_partial_constants_and_false_end(3, 7, 8)
False
"""
x
=
1
<
2
<
a
<
4
<
5
<
b
<
7
<
7
<
c
return
x
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment