6b760ef08330 — Leonard Ritter 27 days ago
* UVM: more optimizations
1 files changed, 42 insertions(+), 15 deletions(-)

M testing/test_node2.sc
M testing/test_node2.sc +42 -15
@@ 669,7 669,7 @@ struct Module
             let ca a = ('unthen self a)
             let cb b = ('unthen self b)
             'then self
-                'and self ca cb            
+                'and self ca cb
                 'nodeId self
                     Node op ('boolType self) a b
     let

          
@@ 1029,11 1029,16 @@ struct Module
         """"recursive substitution
         returning Id
         # see if there is a direct replacement
+        for k v in substmap
+            assert (k != v)
+        try
+            assert (('get substmap id) != id)
+        else;
         for id in ('reverse (topolist self id))
             local found = false
             # see if any change is necessary
             node := self.nodes @ id
-            fn visit-argument (self id substmap found)            
+            fn visit-argument (self id substmap found)
                 if (id != NoId)
                     if ('in? substmap id)
                         found = true

          
@@ 1051,10 1056,12 @@ struct Module
                 continue;
             # make the changes
             local node = (copy ((self.nodes @ id) as Node))
-            fn visit-argument (self id substmap found)            
+            fn visit-argument (self id substmap found)
                 if (id != NoId)
                     try
-                        id = ('get substmap id)
+                        let newid = ('get substmap id)
+                        assert (id != newid) (.. (idstr id) " == " (idstr newid) " -> " (('tostring self newid) as string))
+                        id = newid
                     else;
                 ;
             let visitctx... = substmap found

          
@@ 1065,7 1072,7 @@ struct Module
                 \ self node visitctx...
             let newid =
                 vvv copy
-                'nodeId self (deref node)            
+                'nodeId self (deref node)
             assert (id != newid)
             # update select statements that have become constant
             let newid =

          
@@ 1084,7 1091,8 @@ struct Module
                         cond
                     else newid
                 else newid
-            'set substmap id newid
+            if (id != newid)
+                'set substmap id newid
         try (copy ('get substmap id))
         else (copy id)
 

          
@@ 1115,7 1123,7 @@ struct Module
                                 u
                             this-function self v fvalue tvalue
                             this-function self w fvalue tvalue
-                    else 
+                    else
                         let b = ('getbool self cond)
                         let stmt =
                             if (b > 0) (copy tvalue)

          
@@ 1128,11 1136,11 @@ struct Module
                                         local substmap : (Map Id Id)
                                         'set substmap cond constfalse
                                         'subst self fvalue substmap
-                                let tvalue = 
+                                let tvalue =
                                     do
                                         local substmap : (Map Id Id)
                                         'set substmap cond consttrue
-                                        'subst self tvalue substmap 
+                                        'subst self tvalue substmap
                                 if (fvalue == tvalue)
                                     copy tvalue
                                 elseif ((fvalue == constfalse) & (tvalue == consttrue))

          
@@ 1169,8 1177,18 @@ struct Module
     then =
         fn... "then" (self, cond : Id, value : Id)
             returning Id
+            if (cond == value)
+                return value
+            #'select cond ('undef ('typeof value)
             let c1 cond = ('unthen self cond)
             let c2 value = ('unthen self value)
+            let value =
+                if (not ('isconstant? self cond))
+                    let consttrue = ('constBool self true)
+                    local substmap : (Map Id Id)
+                    'set substmap cond consttrue
+                    'subst self value substmap
+                else value
             let cond = ('and self ('and self c1 cond) c2)
             let b = ('getbool self cond)
             if (b > 0) (copy value)

          
@@ 1207,8 1225,17 @@ struct Module
             'select self value1 value1 value2
 
     not =
-        fn... "not" (self, value : Id)
-            'select self value ('constBool self true) ('constBool self false)
+        fn... "not" (self, _value : Id)
+            let cond value = ('unthen self _value)
+            let b = ('getbool self value)
+            if (b > 0)
+                'then self cond
+                    'constBool self false
+            elseif (b < 0)
+                'then self cond
+                    'constBool self true
+            else
+                'select self _value ('constBool self true) ('constBool self false)
 
 ################################################################################
 

          
@@ 1555,13 1582,13 @@ static-if main-module?
         let exit? =
             equal (constString "\n") readline
 
-        bind exit
+        #bind exit
             then exit? (constInt inttype 0)
 
         bind prompt
             then (or setup exit?) (constString "> ")
 
-        bind stdout
+        #bind stdout
             merge
                 then exit? (constString "exiting...\n")
                 else exit? readline

          
@@ 1574,8 1601,8 @@ static-if main-module?
     print;
 
     'cull module
-    'toposort module
-    'distsort module
+    #'toposort module
+    #'distsort module
     print
         'tostring module