[m-rev.] diff: eliminate some dependencies

Zoltan Somogyi zs at csse.unimelb.edu.au
Thu Aug 5 13:06:26 AEST 2010


Eliminate dependencies between the processing of different predicates or
procedures in as many cases as possible in passes_aux.m and in optimize.m,
so that they can be processed in parallel.

The effect of this diff on performance is negligible (a 0.1% speedup,
within the noise limit).

compiler/passes_aux.m:
	Don't export predicates that do not need to be exported. Since they
	were each used for one thing, inline them into their callers.
	
	Simplify the main traversal predicates to do just what is needed,
	and specialize them to their tasks. For tasks that do not update
	the module_info, the I/O state or anything else while updating
	a proc_info, allow the procedures of the module to be processed
	in parallel.

	Use a systematic naming scheme for the tasks. Use pred_proc_ids
	to identify the procedure being processed in all tasks, to avoid
	unnecessary variations in whether the predicates invoked by the tasks
	need the pred_id, the proc_id or both.

compiler/mercury_compile_front_end.m:
compiler/mercury_compile_middle_end.m:
	Update this code to conform to the changes in passes_aux.m. In several
	cases, conform to changes in the predicates inside the tasks as well,
	since (after the changes described below) several don't take a pair
	of I/O states anymore.

compiler/mercury_compile_llds_back_end.m:
	Update this code to conform to the changes in passes_aux.m. In several
	cases, conform to changes in the predicates inside the tasks as well,
	since (after the changes described below) several don't take a pair
	of I/O states anymore.

	Allow the optimization of different procedures to be done in parallel
	with --trad-passes.

compiler/accumulator.m:
compiler/complexity.m:
compiler/follow_code.m:
compiler/loop_inv.m:
compiler/mark_tail_calls.m:
compiler/size_prof.m:
compiler/ssdebug.m:
compiler/store_alloc.m:
compiler/structure_sharing.analysis.m:
compiler/tupling.m:
	Conform to the change in passes_aux.m.

compiler/deforest.m:
compiler/delay_construct.m:
compiler/liveness.m:
compiler/optimize.m:
compiler/pd_info.m:
compiler/pd_util.m:
compiler/saved_vars.m:
compiler/stack_alloc.m:
compiler/stack_opt.m:
compiler/unneeded_code.m:
	Conform to the change in passes_aux.m.

	Consistently use trace goals for progress messages, and eliminate
	I/O states outside those trace goals.

Zoltan.

cvs diff: Diffing compiler
Index: compiler/accumulator.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/accumulator.m,v
retrieving revision 1.77
diff -u -b -r1.77 accumulator.m
--- compiler/accumulator.m	30 Mar 2010 23:57:25 -0000	1.77
+++ compiler/accumulator.m	5 Aug 2010 02:49:18 -0000
@@ -150,7 +150,7 @@
 
 :- import_module io.
 
-:- pred accumulator.process_proc(pred_id::in, proc_id::in,
+:- pred accumulator.process_proc(pred_proc_id::in,
     proc_info::in, proc_info::out, module_info::in, module_info::out,
     io::di, io::uo) is det.
 
@@ -230,7 +230,7 @@
 
     % Attempt to transform a procedure into accumulator recursive form.
     %
-process_proc(PredId, ProcId, !ProcInfo, !ModuleInfo, !IO) :-
+process_proc(proc(PredId, ProcId), !ProcInfo, !ModuleInfo, !IO) :-
     module_info_get_globals(!.ModuleInfo, Globals),
     globals.lookup_bool_option(Globals,
         optimize_constructor_last_call_accumulator, DoLCO),
@@ -243,9 +243,11 @@
         globals.lookup_bool_option(Globals, very_verbose, VeryVerbose),
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Accumulators introduced into ", !IO),
             write_pred_id(!.ModuleInfo, PredId, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
Index: compiler/complexity.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/complexity.m,v
retrieving revision 1.35
diff -u -b -r1.35 complexity.m
--- compiler/complexity.m	30 Mar 2010 23:57:25 -0000	1.35
+++ compiler/complexity.m	5 Aug 2010 02:49:18 -0000
@@ -50,7 +50,7 @@
     % Transform the given procedure if it is in the complexity map.
     %
 :- pred process_proc_msg(int::in, complexity_proc_map::in,
-    pred_id::in, proc_id::in, proc_info::in, proc_info::out,
+    pred_proc_id::in, proc_info::in, proc_info::out,
     module_info::in, module_info::out, io::di, io::uo) is det.
 
 %-----------------------------------------------------------------------------%
@@ -157,8 +157,8 @@
 
 %-----------------------------------------------------------------------------%
 
-process_proc_msg(NumProcs, ProcMap, PredId, ProcId, !ProcInfo, !ModuleInfo,
-        !IO) :-
+process_proc_msg(NumProcs, ProcMap, proc(PredId, ProcId), !ProcInfo,
+        !ModuleInfo, !IO) :-
     IsInMap = is_in_complexity_proc_map(ProcMap, !.ModuleInfo,
         PredId, ProcId),
     (
Index: compiler/deforest.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/deforest.m,v
retrieving revision 1.93
diff -u -b -r1.93 deforest.m
--- compiler/deforest.m	14 Oct 2009 05:28:31 -0000	1.93
+++ compiler/deforest.m	5 Aug 2010 02:49:18 -0000
@@ -98,7 +98,7 @@
 
     % Find out which arguments of each procedure are switched on at the top
     % level or are constructed in a way which is possibly deforestable.
-    Task0 = update_module_cookie(get_branch_vars_proc, UnivProcArgInfo0),
+    Task0 = update_module_cookie(get_branch_vars_proc_univ, UnivProcArgInfo0),
     process_all_nonimported_procs_update(Task0, Task, !ModuleInfo, !IO),
     (
         Task = update_module_cookie(_, UnivProcArgInfo),
@@ -118,7 +118,7 @@
     list.condense(DepOrdering, DepList),
 
     pd_info_init(!.ModuleInfo, ProcArgInfo, PDInfo0),
-    list.foldl2(deforest_proc, DepList, PDInfo0, PDInfo, !IO),
+    list.foldl(deforest_proc, DepList, PDInfo0, PDInfo),
     pd_info_get_module_info(PDInfo, !:ModuleInfo),
     module_info_clobber_dependency_info(!ModuleInfo),
     pd_info_get_versions(PDInfo, VersionIndex),
@@ -171,34 +171,35 @@
 proc_arg_info_init(ProcArgInfo0) :-
     map.init(ProcArgInfo0).
 
-:- pred get_branch_vars_proc(pred_id::in, proc_id::in,
-    proc_info::in, proc_info::out, univ::in, univ::out,
-    module_info::in, module_info::out) is det.
+:- pred get_branch_vars_proc_univ(pred_proc_id::in,
+    proc_info::in, proc_info::out, module_info::in, module_info::out,
+    univ::in, univ::out) is det.
 
-get_branch_vars_proc(PredId, ProcId, ProcInfo, ProcInfo,
-        UnivProcArgInfo0, UnivProcArgInfo, !ModuleInfo) :-
+get_branch_vars_proc_univ(PredProcId, ProcInfo, ProcInfo,
+        !ModuleInfo, UnivProcArgInfo0, UnivProcArgInfo) :-
     ( univ_to_type(UnivProcArgInfo0, ProcArgInfo0) ->
-        pd_util.get_branch_vars_proc(proc(PredId, ProcId), ProcInfo,
-            ProcArgInfo0, ProcArgInfo, !ModuleInfo),
+        pd_util.get_branch_vars_proc(PredProcId, ProcInfo,
+            !ModuleInfo, ProcArgInfo0, ProcArgInfo),
         type_to_univ(ProcArgInfo, UnivProcArgInfo)
     ;
-        unexpected(this_file, "get_branch_vars_proc")
+        unexpected(this_file, "get_branch_vars_proc_univ")
     ).
 
-:- pred deforest_proc(pred_proc_id::in, pd_info::in, pd_info::out,
-    io::di, io::uo) is det.
+:- pred deforest_proc(pred_proc_id::in, pd_info::in, pd_info::out) is det.
 
-deforest_proc(PredProcId, !PDInfo, !IO) :-
-    deforest_proc_2(PredProcId, _, _, !PDInfo, !IO).
+deforest_proc(PredProcId, !PDInfo) :-
+    deforest_proc_deltas(PredProcId, _, _, !PDInfo).
 
-:- pred deforest_proc_2(pred_proc_id::in, int::out, int::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+:- pred deforest_proc_deltas(pred_proc_id::in, int::out, int::out,
+    pd_info::in, pd_info::out) is det.
 
-deforest_proc_2(proc(PredId, ProcId), CostDelta, SizeDelta, !PDInfo, !IO) :-
+deforest_proc_deltas(proc(PredId, ProcId), CostDelta, SizeDelta, !PDInfo) :-
     some [!ModuleInfo, !PredInfo, !ProcInfo, !Goal] (
         pd_info_get_module_info(!.PDInfo, !:ModuleInfo),
+        trace [io(!IO)] (
         write_proc_progress_message("% Deforesting ",
-            PredId, ProcId, !.ModuleInfo, !IO),
+                PredId, ProcId, !.ModuleInfo, !IO)
+        ),
         module_info_pred_proc_info(!.ModuleInfo, PredId, ProcId,
             !:PredInfo, !:ProcInfo),
         pd_info_init_unfold_info(proc(PredId, ProcId), !.PredInfo, !.ProcInfo,
@@ -210,10 +211,12 @@
         simplify.find_simplifications(no, Globals, Simplifications),
         pd_util.pd_simplify_goal(Simplifications, !Goal, !PDInfo),
 
-        pd_util.propagate_constraints(!Goal, !PDInfo, !IO),
+        pd_util.propagate_constraints(!Goal, !PDInfo),
 
-        pd_debug_output_goal(!.PDInfo, "after constraints\n", !.Goal, !IO),
-        deforest_goal(!Goal, !PDInfo, !IO),
+        trace [io(!IO)] (
+            pd_debug_output_goal(!.PDInfo, "after constraints\n", !.Goal, !IO)
+        ),
+        deforest_goal(!Goal, !PDInfo),
 
         pd_info_get_proc_info(!.PDInfo, !:ProcInfo),
         proc_info_set_goal(!.Goal, !ProcInfo),
@@ -251,7 +254,7 @@
             % Recompute the branch_info for the procedure.
             pd_info_get_proc_arg_info(!.PDInfo, ProcArgInfo0),
             pd_util.get_branch_vars_proc(proc(PredId, ProcId), !.ProcInfo,
-                ProcArgInfo0, ProcArgInfo, !ModuleInfo),
+                !ModuleInfo, ProcArgInfo0, ProcArgInfo),
             pd_info_set_proc_arg_info(ProcArgInfo, !PDInfo),
             pd_info_set_module_info(!.ModuleInfo, !PDInfo)
         ;
@@ -264,26 +267,27 @@
         ),
 
         pd_info_get_module_info(!.PDInfo, !:ModuleInfo),
+        trace [io(!IO)] (
         write_proc_progress_message("% Finished deforesting ",
-            PredId, ProcId, !.ModuleInfo, !IO),
+                PredId, ProcId, !.ModuleInfo, !IO)
+        ),
         pd_info_get_cost_delta(!.PDInfo, CostDelta),
         pd_info_get_size_delta(!.PDInfo, SizeDelta),
         pd_info_unset_unfold_info(!PDInfo)
     ).
 
 :- pred deforest_goal(hlds_goal::in, hlds_goal::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
-deforest_goal(Goal0, Goal, !PDInfo, !IO) :-
+deforest_goal(Goal0, Goal, !PDInfo) :-
     Goal0 = hlds_goal(GoalExpr0, GoalInfo0),
-    deforest_goal_expr(GoalExpr0, GoalExpr, GoalInfo0, GoalInfo, !PDInfo, !IO),
+    deforest_goal_expr(GoalExpr0, GoalExpr, GoalInfo0, GoalInfo, !PDInfo),
     Goal = hlds_goal(GoalExpr, GoalInfo).
 
 :- pred deforest_goal_expr(hlds_goal_expr::in, hlds_goal_expr::out,
-    hlds_goal_info::in, hlds_goal_info::out, pd_info::in, pd_info::out,
-    io::di, io::uo) is det.
+    hlds_goal_info::in, hlds_goal_info::out, pd_info::in, pd_info::out) is det.
 
-deforest_goal_expr(GoalExpr0, GoalExpr, !GoalInfo, !PDInfo, !IO) :-
+deforest_goal_expr(GoalExpr0, GoalExpr, !GoalInfo, !PDInfo) :-
     (
         GoalExpr0 = conj(ConjType, Goals0),
         some [!Goals] (
@@ -291,8 +295,7 @@
             (
                 ConjType = plain_conj,
                 pd_info_get_instmap(!.PDInfo, InstMap0),
-                partially_evaluate_conj_goals(!.Goals, [], !:Goals,
-                    !PDInfo, !IO),
+                partially_evaluate_conj_goals(!.Goals, [], !:Goals, !PDInfo),
                 pd_info_set_instmap(InstMap0, !PDInfo),
                 NonLocals = goal_info_get_nonlocals(!.GoalInfo),
                 pd_info_get_module_info(!.PDInfo, ModuleInfo),
@@ -303,8 +306,7 @@
                     Deforestation = yes,
                     compute_goal_infos(!Goals, !PDInfo),
                     pd_info_set_instmap(InstMap0, !PDInfo),
-                    deforest_conj(!.Goals, NonLocals, [], !:Goals, !PDInfo,
-                        !IO)
+                    deforest_conj(!.Goals, NonLocals, [], !:Goals, !PDInfo)
                 ;
                     Deforestation = no
                 ),
@@ -314,7 +316,7 @@
                 (
                     Constraints = yes,
                     propagate_conj_constraints(!.Goals, NonLocals, [], !:Goals,
-                        !PDInfo, !IO)
+                        !PDInfo)
                 ;
                     Constraints = no
                 ),
@@ -328,39 +330,39 @@
         GoalExpr = conj(ConjType, Goals)
     ;
         GoalExpr0 = disj(Goals0),
-        deforest_disj(Goals0, Goals, !PDInfo, !IO),
+        deforest_disj(Goals0, Goals, !PDInfo),
         GoalExpr = disj(Goals)
     ;
         GoalExpr0 = if_then_else(Vars, Cond0, Then0, Else0),
         pd_info_get_instmap(!.PDInfo, InstMap0),
-        deforest_goal(Cond0, Cond, !PDInfo, !IO),
+        deforest_goal(Cond0, Cond, !PDInfo),
         pd_info_update_goal(Cond, !PDInfo),
-        deforest_goal(Then0, Then, !PDInfo, !IO),
+        deforest_goal(Then0, Then, !PDInfo),
         pd_info_set_instmap(InstMap0, !PDInfo),
-        deforest_goal(Else0, Else, !PDInfo, !IO),
+        deforest_goal(Else0, Else, !PDInfo),
         pd_info_set_instmap(InstMap0, !PDInfo),
         GoalExpr = if_then_else(Vars, Cond, Then, Else)
     ;
         GoalExpr0 = switch(Var, CanFail, Cases0),
-        deforest_cases(Var, Cases0, Cases, !PDInfo, !IO),
+        deforest_cases(Var, Cases0, Cases, !PDInfo),
         GoalExpr = switch(Var, CanFail, Cases)
     ;
         GoalExpr0 = negation(SubGoal0),
-        deforest_goal(SubGoal0, SubGoal, !PDInfo, !IO),
+        deforest_goal(SubGoal0, SubGoal, !PDInfo),
         GoalExpr = negation(SubGoal)
     ;
         GoalExpr0 = scope(Reason, SubGoal0),
         ( Reason = from_ground_term(_, from_ground_term_construct) ->
             SubGoal = SubGoal0
         ;
-            deforest_goal(SubGoal0, SubGoal, !PDInfo, !IO)
+            deforest_goal(SubGoal0, SubGoal, !PDInfo)
         ),
         GoalExpr = scope(Reason, SubGoal)
     ;
         GoalExpr0 = plain_call(PredId, ProcId, Args, BuiltinState, _, Name),
         deforest_call(PredId, ProcId, Args, Name, BuiltinState,
             hlds_goal(GoalExpr0, !.GoalInfo), hlds_goal(GoalExpr, !:GoalInfo),
-            !PDInfo, !IO)
+            !PDInfo)
     ;
         ( GoalExpr0 = call_foreign_proc(_, _, _, _, _, _, _)
         ; GoalExpr0 = generic_call(_, _, _, _)
@@ -376,31 +378,31 @@
 %-----------------------------------------------------------------------------%
 
 :- pred deforest_disj(list(hlds_goal)::in, list(hlds_goal)::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
-deforest_disj([], [], !PDInfo, !IO).
-deforest_disj([Goal0 | Goals0], [Goal | Goals], !PDInfo, !IO) :-
+deforest_disj([], [], !PDInfo).
+deforest_disj([Goal0 | Goals0], [Goal | Goals], !PDInfo) :-
     pd_info_get_instmap(!.PDInfo, InstMap0),
-    deforest_goal(Goal0, Goal, !PDInfo, !IO),
+    deforest_goal(Goal0, Goal, !PDInfo),
     pd_info_set_instmap(InstMap0, !PDInfo),
-    deforest_disj(Goals0, Goals, !PDInfo, !IO).
+    deforest_disj(Goals0, Goals, !PDInfo).
 
 %-----------------------------------------------------------------------------%
 
 :- pred deforest_cases(prog_var::in, list(case)::in, list(case)::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
-deforest_cases(_, [], [], !PDInfo, !IO).
-deforest_cases(Var, [Case0 | Cases0], [Case | Cases], !PDInfo, !IO) :-
+deforest_cases(_, [], [], !PDInfo).
+deforest_cases(Var, [Case0 | Cases0], [Case | Cases], !PDInfo) :-
     Case0 = case(MainConsId, OtherConsIds, Goal0),
     % Bind Var to MainConsId or one of the OtherConsIds in the instmap
     % before processing this case.
     pd_info_get_instmap(!.PDInfo, InstMap0),
     pd_info_bind_var_to_functors(Var, MainConsId, OtherConsIds, !PDInfo),
-    deforest_goal(Goal0, Goal, !PDInfo, !IO),
+    deforest_goal(Goal0, Goal, !PDInfo),
     Case = case(MainConsId, OtherConsIds, Goal),
     pd_info_set_instmap(InstMap0, !PDInfo),
-    deforest_cases(Var, Cases0, Cases, !PDInfo, !IO).
+    deforest_cases(Var, Cases0, Cases, !PDInfo).
 
 %-----------------------------------------------------------------------------%
 
@@ -408,13 +410,12 @@
     %
 :- pred partially_evaluate_conj_goals(list(hlds_goal)::in,
     list(hlds_goal)::in, list(hlds_goal)::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
-partially_evaluate_conj_goals([], RevGoals, Goals, !PDInfo, !IO) :-
+partially_evaluate_conj_goals([], RevGoals, Goals, !PDInfo) :-
     list.reverse(RevGoals, Goals).
-partially_evaluate_conj_goals([Goal0 | Goals0], RevGoals0, Goals,
-        !PDInfo, !IO) :-
-    deforest_goal(Goal0, Goal1, !PDInfo, !IO),
+partially_evaluate_conj_goals([Goal0 | Goals0], RevGoals0, Goals, !PDInfo) :-
+    deforest_goal(Goal0, Goal1, !PDInfo),
     pd_info_update_goal(Goal1, !PDInfo),
     ( Goal1 = hlds_goal(conj(plain_conj, Goals1), _) ->
         list.reverse(Goals1, RevGoals1),
@@ -422,7 +423,7 @@
     ;
         RevGoals2 = [Goal1 | RevGoals0]
     ),
-    partially_evaluate_conj_goals(Goals0, RevGoals2, Goals, !PDInfo, !IO).
+    partially_evaluate_conj_goals(Goals0, RevGoals2, Goals, !PDInfo).
 
 %-----------------------------------------------------------------------------%
 
@@ -478,12 +479,12 @@
 
 :- pred propagate_conj_constraints(list(hlds_goal)::in,
     set(prog_var)::in, list(hlds_goal)::in, list(hlds_goal)::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
-propagate_conj_constraints([], _, RevGoals, Goals, !PDInfo, !IO) :-
+propagate_conj_constraints([], _, RevGoals, Goals, !PDInfo) :-
     list.reverse(RevGoals, Goals).
 propagate_conj_constraints([Goal0 | Goals0], NonLocals, RevGoals0, Goals,
-        !PDInfo, !IO) :-
+        !PDInfo) :-
     pd_info_get_module_info(!.PDInfo, ModuleInfo),
     (
         % constraint.m ensures that only constraints relevant
@@ -501,29 +502,31 @@
         SymNameString = sym_name_to_string(SymName),
         module_info_get_globals(ModuleInfo, Globals),
         globals.lookup_bool_option(Globals, debug_pd, DebugPD),
-        pd_debug_message(DebugPD, "propagating constraints into call to %s\n",
-            [s(SymNameString)], !IO),
+        trace [io(!IO)] (
+            pd_debug_message(DebugPD,
+                "propagating constraints into call to %s\n",
+                [s(SymNameString)], !IO)
+        ),
 
         get_sub_conj_nonlocals(NonLocals, RevGoals0, [],
             Goal0, Constraints, no, [], Goals1, ConjNonLocals),
-        call_call(ConjNonLocals, Goal0, Constraints, no, MaybeGoal,
-            !PDInfo, !IO),
+        call_call(ConjNonLocals, Goal0, Constraints, no, MaybeGoal, !PDInfo),
         (
             MaybeGoal = yes(Goal),
             pd_info_set_rerun_det(yes, !PDInfo),
             pd_info_update_goal(Goal, !PDInfo),
             propagate_conj_constraints(Goals1, NonLocals,
-                [Goal | RevGoals0], Goals, !PDInfo, !IO)
+                [Goal | RevGoals0], Goals, !PDInfo)
         ;
             MaybeGoal = no,
             pd_info_update_goal(Goal0, !PDInfo),
             propagate_conj_constraints(Goals0, NonLocals,
-                [Goal0 | RevGoals0], Goals, !PDInfo, !IO)
+                [Goal0 | RevGoals0], Goals, !PDInfo)
         )
     ;
         pd_info_update_goal(Goal0, !PDInfo),
         propagate_conj_constraints(Goals0, NonLocals,
-            [Goal0 | RevGoals0], Goals, !PDInfo, !IO)
+            [Goal0 | RevGoals0], Goals, !PDInfo)
     ).
 
 %-----------------------------------------------------------------------------%
@@ -533,12 +536,12 @@
 
 :- pred deforest_conj(annotated_conj::in, set(prog_var)::in,
     list(hlds_goal)::in, list(hlds_goal)::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
-deforest_conj([], _, RevGoals, Goals, !PDInfo, !IO) :-
+deforest_conj([], _, RevGoals, Goals, !PDInfo) :-
     list.reverse(RevGoals, Goals).
 deforest_conj([Goal0 - MaybeBranchInfo | Goals0], NonLocals,
-        RevGoals0, RevGoals, !PDInfo, !IO) :-
+        RevGoals0, RevGoals, !PDInfo) :-
     (
         % Look for a goal later in the conjunction to deforest with.
         MaybeBranchInfo = yes(GoalBranchInfo),
@@ -546,20 +549,20 @@
             DeforestInfo)
     ->
         handle_deforestation(NonLocals, DeforestInfo,
-            RevGoals0, RevGoals1, Goals1, Goals2, Optimized, !PDInfo, !IO),
+            RevGoals0, RevGoals1, Goals1, Goals2, Optimized, !PDInfo),
         (
             Optimized = yes,
-            deforest_conj(Goals2, NonLocals, RevGoals1, RevGoals, !PDInfo, !IO)
+            deforest_conj(Goals2, NonLocals, RevGoals1, RevGoals, !PDInfo)
         ;
             Optimized = no,
             pd_info_update_goal(Goal0, !PDInfo),
             RevGoals2 = [Goal0 | RevGoals0],
-            deforest_conj(Goals0, NonLocals, RevGoals2, RevGoals, !PDInfo, !IO)
+            deforest_conj(Goals0, NonLocals, RevGoals2, RevGoals, !PDInfo)
         )
     ;
         pd_info_update_goal(Goal0, !PDInfo),
         RevGoals1 = [Goal0 | RevGoals0],
-        deforest_conj(Goals0, NonLocals, RevGoals1, RevGoals, !PDInfo, !IO)
+        deforest_conj(Goals0, NonLocals, RevGoals1, RevGoals, !PDInfo)
     ).
 
 %-----------------------------------------------------------------------------%
@@ -646,10 +649,10 @@
 :- pred handle_deforestation(set(prog_var)::in, deforest_info::in,
     list(hlds_goal)::in, list(hlds_goal)::out,
     annotated_conj::in, annotated_conj::out, bool::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
 handle_deforestation(NonLocals, DeforestInfo0, !RevBeforeGoals, !AfterGoals,
-        Optimized, !PDInfo, !IO) :-
+        Optimized, !PDInfo) :-
     pd_info_get_module_info(!.PDInfo, ModuleInfo),
     module_info_get_globals(ModuleInfo, Globals),
     globals.lookup_bool_option(Globals, debug_pd, DebugPD),
@@ -658,11 +661,13 @@
     pd_info_get_created_versions(!.PDInfo, CreatedVersions0),
 
     pd_info_get_depth(!.PDInfo, Depth0),
+    trace [io(!IO)] (
     pd_debug_message(DebugPD,
-        "checking for deforestation at depth %i\n", [i(Depth0)], !IO),
+            "checking for deforestation at depth %i\n", [i(Depth0)], !IO)
+    ),
 
     reorder_conj(DeforestInfo0, DeforestInfo,
-        BeforeIrrelevant, AfterIrrelevant, !.PDInfo, !IO),
+        BeforeIrrelevant, AfterIrrelevant, !.PDInfo),
 
     get_sub_conj_nonlocals(NonLocals, DeforestInfo, !.RevBeforeGoals,
         BeforeIrrelevant, AfterIrrelevant, !.AfterGoals, ConjNonLocals),
@@ -693,10 +698,12 @@
             % conjunction to be deforested and process it.
             PredName1 = predicate_name(ModuleInfo, PredId1),
             PredName2 = predicate_name(ModuleInfo, PredId2),
+            trace [io(!IO)] (
             pd_debug_message(DebugPD, "deforesting calls to %s and %s\n",
-                [s(PredName1), s(PredName2)], !IO),
+                    [s(PredName1), s(PredName2)], !IO)
+            ),
             call_call(ConjNonLocals, EarlierGoal, BetweenGoals,
-                yes(LaterGoal), MaybeGoal, !PDInfo, !IO),
+                yes(LaterGoal), MaybeGoal, !PDInfo),
             (
                 MaybeGoal = yes(Goal),
                 Optimized0 = yes,
@@ -719,12 +726,14 @@
             \+ set.member(PredProcId, Parents0)
         ->
             CurrPredName = predicate_name(ModuleInfo, PredId),
+            trace [io(!IO)] (
             pd_debug_message(DebugPD, "Pushing call to %s into goal\n",
-                [s(CurrPredName)], !IO),
+                    [s(CurrPredName)], !IO)
+            ),
             set.insert(Parents0, proc(PredId, ProcId), Parents),
             pd_info_set_parents(Parents, !PDInfo),
             push_goal_into_goal(ConjNonLocals, DeforestBranches,
-                EarlierGoal, BetweenGoals, LaterGoal, Goal, !PDInfo, !IO),
+                EarlierGoal, BetweenGoals, LaterGoal, Goal, !PDInfo),
             Goals = [Goal],
             Optimized0 = yes
         ;
@@ -735,9 +744,11 @@
             goal_util.goal_is_branched(EarlierGoalExpr),
             goal_util.goal_is_branched(LaterGoalExpr)
         ->
-            pd_debug_message(DebugPD, "Pushing goal into goal\n", [], !IO),
+            trace [io(!IO)] (
+                pd_debug_message(DebugPD, "Pushing goal into goal\n", [], !IO)
+            ),
             push_goal_into_goal(ConjNonLocals, DeforestBranches,
-                EarlierGoal, BetweenGoals, LaterGoal, Goal, !PDInfo, !IO),
+                EarlierGoal, BetweenGoals, LaterGoal, Goal, !PDInfo),
             Goals = [Goal],
             goals_size([EarlierGoal | BetweenGoals], ConjSize1),
             goal_size(LaterGoal, ConjSize2),
@@ -746,7 +757,9 @@
             pd_info_incr_size_delta(SizeDiff, !PDInfo),
             Optimized0 = yes
         ;
-            pd_debug_message(DebugPD, "not optimizing\n", [], !IO),
+            trace [io(!IO)] (
+                pd_debug_message(DebugPD, "not optimizing\n", [], !IO)
+            ),
             Goals = [],
             Optimized0 = no
         )
@@ -788,8 +801,10 @@
         pd_info_set_changed(yes, !PDInfo),
         pd_info_set_rerun_det(yes, !PDInfo)
     ),
+    trace [io(!IO)] (
     pd_debug_message(DebugPD,
-        "finished deforestation at depth %i\n", [i(Depth0)], !IO),
+            "finished deforestation at depth %i\n", [i(Depth0)], !IO)
+    ),
     pd_info_set_parents(Parents0, !PDInfo).
 
     % Check whether deforestation is legal and worthwhile.
@@ -838,11 +853,10 @@
     ).
 
 :- pred can_optimize_conj(hlds_goal::in, list(hlds_goal)::in,
-    maybe(hlds_goal)::in, bool::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    maybe(hlds_goal)::in, bool::out, pd_info::in, pd_info::out) is det.
 
 can_optimize_conj(EarlierGoal, BetweenGoals, MaybeLaterGoal, ShouldTry,
-        !PDInfo, !IO) :-
+        !PDInfo) :-
     pd_info_get_pred_info(!.PDInfo, PredInfo),
     pd_info_get_depth(!.PDInfo, Depth0),
     pd_info_get_module_info(!.PDInfo, ModuleInfo),
@@ -886,7 +900,9 @@
         SizeLimit \= -1,
         CalledGoalSize > SizeLimit
     ->
-        pd_debug_message(DebugPD, "goal too large\n", [], !IO),
+        trace [io(!IO)] (
+            pd_debug_message(DebugPD, "goal too large\n", [], !IO)
+        ),
         ShouldTry = no
     ;
         % Check whether either of the goals to be deforested can't be inlined.
@@ -908,7 +924,9 @@
         \+ inlining.can_inline_proc(PredId, ProcId, BuiltinState,
             InlinePromisedPure, CallerMarkers, ModuleInfo)
     ->
-        pd_debug_message(DebugPD, "non-inlineable calls\n", [], !IO),
+        trace [io(!IO)] (
+            pd_debug_message(DebugPD, "non-inlineable calls\n", [], !IO)
+        ),
         ShouldTry = no
     ;
         %
@@ -917,8 +935,10 @@
         %
         \+ is_simple_goal_list(BetweenGoals)
     ->
+        trace [io(!IO)] (
         pd_debug_message(DebugPD,
-            "between goals not simple enough\n", [], !IO),
+                "between goals not simple enough\n", [], !IO)
+        ),
         ShouldTry = no
     ;
         % Give up if there are any impure goals involved.
@@ -930,8 +950,10 @@
         ImpureGoal = hlds_goal(_, ImpureGoalInfo),
         \+ goal_info_get_purity(ImpureGoalInfo) = purity_pure
     ->
+        trace [io(!IO)] (
         pd_debug_message(DebugPD,
-            "goal list contains impure goal(s)\n", [], !IO),
+                "goal list contains impure goal(s)\n", [], !IO)
+        ),
         ShouldTry = no
     ;
         % Check whether interleaving the execution of the goals could alter
@@ -945,8 +967,10 @@
         \+ goal_util.reordering_maintains_termination_old(ModuleInfo,
             FullyStrict, EarlierGoal, OtherGoal)
     ->
+        trace [io(!IO)] (
         pd_debug_message(DebugPD, "interleaving execution could change " ++
-            "termination behaviour\n", [], !IO),
+                "termination behaviour\n", [], !IO)
+        ),
         ShouldTry = no
     ;
         ShouldTry = yes
@@ -995,16 +1019,16 @@
     %
 :- pred call_call(set(prog_var)::in, hlds_goal::in,
     list(hlds_goal)::in, maybe(hlds_goal)::in, maybe(hlds_goal)::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
 call_call(ConjNonLocals, EarlierGoal, BetweenGoals, MaybeLaterGoal, MaybeGoal,
-        !PDInfo, !IO) :-
+        !PDInfo) :-
     can_optimize_conj(EarlierGoal, BetweenGoals, MaybeLaterGoal, ShouldTry,
-        !PDInfo, !IO),
+        !PDInfo),
     (
         ShouldTry = yes,
         call_call(ConjNonLocals, EarlierGoal, BetweenGoals, MaybeLaterGoal,
-            MaybeGoal, !PDInfo, !IO)
+            MaybeGoal, !PDInfo)
     ;
         ShouldTry = no,
         MaybeGoal = no
@@ -1014,14 +1038,14 @@
     %
 :- pred call_call_2(set(prog_var)::in, hlds_goal::in,
     list(hlds_goal)::in, maybe(hlds_goal)::in, maybe(hlds_goal)::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
 call_call_2(ConjNonLocals, EarlierGoal, BetweenGoals, MaybeLaterGoal,
-        MaybeGoal, !PDInfo, !IO) :-
+        MaybeGoal, !PDInfo) :-
     create_conj(EarlierGoal, BetweenGoals, MaybeLaterGoal, ConjNonLocals,
         FoldGoal),
 
-    pd_info.search_version(!.PDInfo, FoldGoal, MaybeVersion, !IO),
+    pd_info.search_version(!.PDInfo, FoldGoal, MaybeVersion),
     pd_info_get_parent_versions(!.PDInfo, Parents),
 
     pd_info_get_module_info(!.PDInfo, ModuleInfo),
@@ -1035,7 +1059,10 @@
         VersionPredProcId = proc(VersionPredId, _),
         pd_info_get_module_info(!.PDInfo, ModuleInfo0),
         FoldPredName = predicate_name(ModuleInfo0, VersionPredId),
-        pd_debug_message(DebugPD, "Folded with %s\n", [s(FoldPredName)], !IO),
+        trace [io(!IO)] (
+            pd_debug_message(DebugPD, "Folded with %s\n", [s(FoldPredName)],
+                !IO)
+        ),
         ( set.member(VersionPredProcId, Parents) ->
             FoldCostDelta = cost_of_recursive_fold
         ;
@@ -1052,9 +1079,11 @@
         pd_info_get_global_term_info(!.PDInfo, TermInfo0),
         pd_info_get_parent_versions(!.PDInfo, ParentVersions0),
 
+        trace [io(!IO)] (
         pd_debug_do_io(DebugPD, io.write_string("Parents: "), !IO),
         pd_debug_write(DebugPD, ParentVersions0, !IO),
-        pd_debug_do_io(DebugPD, io.nl, !IO),
+            pd_debug_do_io(DebugPD, io.nl, !IO)
+        ),
 
         pd_info_get_versions(!.PDInfo, Versions),
         pd_info_get_instmap(!.PDInfo, InstMap),
@@ -1063,15 +1092,18 @@
             CheckResult),
         (
             CheckResult = ok(ProcPair, Size),
+            trace [io(!IO)] (
             pd_debug_message(DebugPD,
-                "global termination check succeeded - creating new version\n",
-                [], !IO),
+                    "global termination check succeeded - " ++
+                    "creating new version\n",
+                    [], !IO)
+            ),
             pd_info_set_global_term_info(TermInfo, !PDInfo),
             RunModes = no,
             MaybeGeneralised = no,
             create_deforest_goal(EarlierGoal, BetweenGoals,
                 MaybeLaterGoal, FoldGoal, ConjNonLocals, RunModes, ProcPair,
-                Size, MaybeGeneralised, MaybeGoal, !PDInfo, !IO)
+                Size, MaybeGeneralised, MaybeGoal, !PDInfo)
         ;
             CheckResult = possible_loop(ProcPair, Size,
                 CoveringPredProcId),
@@ -1081,11 +1113,13 @@
             % then keep on going.
             try_generalisation(EarlierGoal, BetweenGoals,
                 MaybeLaterGoal, FoldGoal, ConjNonLocals, ProcPair, Size,
-                CoveringPredProcId, MaybeGoal, !PDInfo, !IO)
+                CoveringPredProcId, MaybeGoal, !PDInfo)
         ;
             CheckResult = loop,
+            trace [io(!IO)] (
             pd_debug_message(DebugPD,
-                "global termination check failed\n", [], !IO),
+                    "global termination check failed\n", [], !IO)
+            ),
             MaybeGoal = no
         ),
         pd_info_set_global_term_info(TermInfo0, !PDInfo)
@@ -1099,11 +1133,11 @@
 :- pred create_deforest_goal(hlds_goal::in, hlds_goals::in,
     maybe(hlds_goal)::in, hlds_goal::in, set(prog_var)::in, bool::in,
     proc_pair::in, int::in, maybe(pred_proc_id)::in, maybe(hlds_goal)::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
 create_deforest_goal(EarlierGoal, BetweenGoals, MaybeLaterGoal,
         FoldGoal0, NonLocals, RunModes, ProcPair, Size,
-        MaybeGeneralised, MaybeCallGoal, !PDInfo, !IO) :-
+        MaybeGeneralised, MaybeCallGoal, !PDInfo) :-
     pd_info_get_module_info(!.PDInfo, ModuleInfo0),
     module_info_get_globals(ModuleInfo0, Globals),
     globals.lookup_int_option(Globals, deforestation_vars_threshold, VarsOpt),
@@ -1141,10 +1175,12 @@
 
         pd_info_get_instmap(!.PDInfo, InstMap0),
         pd_info_get_proc_info(!.PDInfo, ProcInfo0),
-        pd_debug_message(DebugPD, "unfolding first call\n", [], !IO),
+        trace [io(!IO)] (
+            pd_debug_message(DebugPD, "unfolding first call\n", [], !IO)
+        ),
 
         unfold_call(no, no, PredId1, ProcId1, Args1, EarlierGoal, UnfoldedCall,
-            DidUnfold, !PDInfo, !IO),
+            DidUnfold, !PDInfo),
         create_conj(UnfoldedCall, BetweenGoals, MaybeLaterGoal, NonLocals,
             DeforestGoal0),
         set.to_sorted_list(NonLocals, NonLocalsList),
@@ -1161,12 +1197,14 @@
             % to a particular functor but the extra information was
             % generalised away.
 
+            trace [io(!IO)] (
             pd_debug_message(DebugPD,
-                "running modes on deforest goal\n", [], !IO),
-            pd_util.unique_modecheck_goal(DeforestGoal0,
-                DeforestGoal, Errors1, !PDInfo, !IO),
+                    "running modes on deforest goal\n", [], !IO)
+            ),
+            pd_util.unique_modecheck_goal(DeforestGoal0, DeforestGoal,
+                Errors1, !PDInfo),
             pd_util.unique_modecheck_goal(FoldGoal0, FoldGoal,
-                Errors2, !PDInfo, !IO),
+                Errors2, !PDInfo),
             Errors = Errors1 ++ Errors2
         ;
             DeforestGoal = DeforestGoal0,
@@ -1190,8 +1228,10 @@
             pd_info_get_module_info(!.PDInfo, ModuleInfo),
 
             PredName = predicate_name(ModuleInfo, PredId),
+            trace [io(!IO)] (
             pd_debug_message(DebugPD,
-                "\nCreated predicate %s\n", [s(PredName)], !IO),
+                    "\nCreated predicate %s\n", [s(PredName)], !IO)
+            ),
             (
                 MaybeLaterGoal = yes(
                     hlds_goal(plain_call(PredId2, ProcId2, _, _, _, _), _)
@@ -1214,22 +1254,26 @@
             pd_info_set_global_term_info(TermInfo, !PDInfo),
             set.insert_list(Parents0, [PredProcId | CalledPreds], Parents),
             pd_info_set_parent_versions(Parents, !PDInfo),
-            pd_info.register_version(PredProcId, VersionInfo, !PDInfo, !IO),
+            pd_info.register_version(PredProcId, VersionInfo, !PDInfo),
 
             % Run deforestation on the new predicate to do the folding.
             pd_info_get_unfold_info(!.PDInfo, UnfoldInfo),
-            deforest_proc_2(PredProcId, CostDelta, SizeDelta, !PDInfo, !IO),
+            deforest_proc_deltas(PredProcId, CostDelta, SizeDelta, !PDInfo),
             pd_info_set_unfold_info(UnfoldInfo, !PDInfo),
             pd_info_incr_cost_delta(CostDelta, !PDInfo),
             pd_info_incr_size_delta(SizeDelta, !PDInfo),
             pd_info_set_parent_versions(Parents0, !PDInfo),
             pd_info_get_pred_proc_id(!.PDInfo, proc(CurrPredId, CurrProcId)),
+            trace [io(!IO)] (
             write_proc_progress_message("% Back in ",
-                CurrPredId, CurrProcId, ModuleInfo, !IO),
+                    CurrPredId, CurrProcId, ModuleInfo, !IO)
+            ),
             MaybeCallGoal = yes(CallGoal)
         ;
+            trace [io(!IO)] (
             pd_debug_message(DebugPD,
-                "Generalisation produced mode errors\n", [], !IO),
+                    "Generalisation produced mode errors\n", [], !IO)
+            ),
             MaybeCallGoal = no
         ),
 
@@ -1239,7 +1283,9 @@
         pd_info_set_proc_info(ProcInfo0, !PDInfo),
         pd_info_set_instmap(InstMap0, !PDInfo)
     ;
-        pd_debug_message(DebugPD, "vars threshold exceeded\n", [], !IO),
+        trace [io(!IO)] (
+            pd_debug_message(DebugPD, "vars threshold exceeded\n", [], !IO)
+        ),
         MaybeCallGoal = no
     ).
 
@@ -1355,15 +1401,17 @@
 :- pred try_generalisation(hlds_goal::in, list(hlds_goal)::in,
     maybe(hlds_goal)::in, hlds_goal::in, set(prog_var)::in,
     proc_pair::in, int::in, pred_proc_id::in, maybe(hlds_goal)::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
 try_generalisation(EarlierGoal, BetweenGoals, MaybeLaterGoal,
         FoldGoal, ConjNonLocals, ProcPair, Size,
-        CoveringPredProcId, MaybeGoal, !PDInfo, !IO) :-
+        CoveringPredProcId, MaybeGoal, !PDInfo) :-
     pd_info_get_module_info(!.PDInfo, ModuleInfo),
     module_info_get_globals(ModuleInfo, Globals),
     globals.lookup_bool_option(Globals, debug_pd, DebugPD),
-    pd_debug_message(DebugPD, "trying generalisation\n", [], !IO),
+    trace [io(!IO)] (
+        pd_debug_message(DebugPD, "trying generalisation\n", [], !IO)
+    ),
     pd_info_get_versions(!.PDInfo, VersionIndex),
     map.lookup(VersionIndex, CoveringPredProcId, Version),
     Version = version_info(VersionGoal, _, VersionArgs,
@@ -1377,7 +1425,7 @@
     ->
         do_generalisation(VersionArgs, Renaming, VersionInstMap,
             EarlierGoal, BetweenGoals, MaybeLaterGoal, FoldGoal, ConjNonLocals,
-            ProcPair, Size, CoveringPredProcId, MaybeGoal, !PDInfo, !IO)
+            ProcPair, Size, CoveringPredProcId, MaybeGoal, !PDInfo)
     ;
         % If the earlier goal is a generalisation of another version, try
         % matching against that. This happens when attempting two
@@ -1389,13 +1437,17 @@
             MaybeLaterGoal, ConjNonLocals, VarSet, VarTypes, Versions,
             Renaming)
     ->
+        trace [io(!IO)] (
         pd_debug_message(DebugPD, "matched with generalised version\n", [],
-            !IO),
+                !IO)
+        ),
         do_generalisation(VersionArgs, Renaming, VersionInstMap,
             EarlierGoal, BetweenGoals, MaybeLaterGoal, FoldGoal, ConjNonLocals,
-            ProcPair, Size, CoveringPredProcId, MaybeGoal, !PDInfo, !IO)
+            ProcPair, Size, CoveringPredProcId, MaybeGoal, !PDInfo)
     ;
-        pd_debug_message(DebugPD, "goals don't match\n", [], !IO),
+        trace [io(!IO)] (
+            pd_debug_message(DebugPD, "goals don't match\n", [], !IO)
+        ),
         MaybeGoal = no
     ).
 
@@ -1404,15 +1456,17 @@
     list(hlds_goal)::in, maybe(hlds_goal)::in, hlds_goal::in,
     set(prog_var)::in, proc_pair::in, int::in,
     pred_proc_id::in, maybe(hlds_goal)::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
 do_generalisation(VersionArgs, Renaming, VersionInstMap, EarlierGoal,
         BetweenGoals, MaybeLaterGoal, FoldGoal, ConjNonLocals,
-        ProcPair, Size, Generalised, MaybeGoal, !PDInfo, !IO) :-
+        ProcPair, Size, Generalised, MaybeGoal, !PDInfo) :-
     pd_info_get_module_info(!.PDInfo, ModuleInfo),
     module_info_get_globals(ModuleInfo, Globals),
     globals.lookup_bool_option(Globals, debug_pd, DebugPD),
-    pd_debug_message(DebugPD, "goals match, trying MSG\n", [], !IO),
+    trace [io(!IO)] (
+        pd_debug_message(DebugPD, "goals match, trying MSG\n", [], !IO)
+    ),
     pd_info_get_instmap(!.PDInfo, InstMap0),
     instmap_lookup_vars(VersionInstMap, VersionArgs, VersionInsts),
     pd_util.inst_list_size(ModuleInfo, VersionInsts, VersionInstSizes),
@@ -1426,13 +1480,17 @@
         pd_util.inst_list_size(ModuleInfo, ArgInsts, NewInstSizes),
         NewInstSizes < VersionInstSizes
     ->
-        pd_debug_message(DebugPD, "MSG succeeded", [], !IO),
+        trace [io(!IO)] (
+            pd_debug_message(DebugPD, "MSG succeeded", [], !IO)
+        ),
         pd_info_set_instmap(InstMap, !PDInfo),
         create_deforest_goal(EarlierGoal, BetweenGoals,
             MaybeLaterGoal, FoldGoal, ConjNonLocals, yes, ProcPair,
-            Size, yes(Generalised), MaybeGoal, !PDInfo, !IO)
+            Size, yes(Generalised), MaybeGoal, !PDInfo)
     ;
-        pd_debug_message(DebugPD, "MSG failed\n", [], !IO),
+        trace [io(!IO)] (
+            pd_debug_message(DebugPD, "MSG failed\n", [], !IO)
+        ),
         MaybeGoal = no
     ),
     pd_info_set_instmap(InstMap0, !PDInfo).
@@ -1597,15 +1655,16 @@
     % We favor moving goals backward to avoid removing tail recursion.
     %
 :- pred reorder_conj(deforest_info::in, deforest_info::out,
-    list(hlds_goal)::out, list(hlds_goal)::out, pd_info::in,
-    io::di, io::uo) is det.
+    list(hlds_goal)::out, list(hlds_goal)::out, pd_info::in) is det.
 
 reorder_conj(DeforestInfo0, DeforestInfo,
-        BeforeIrrelevant, AfterIrrelevant, PDInfo, !IO) :-
+        BeforeIrrelevant, AfterIrrelevant, PDInfo) :-
     pd_info_get_module_info(PDInfo, ModuleInfo),
     module_info_get_globals(ModuleInfo, Globals),
     globals.lookup_bool_option(Globals, debug_pd, DebugPD),
-    pd_debug_message(DebugPD, "Reordering conjunction\n", [], !IO),
+    trace [io(!IO)] (
+        pd_debug_message(DebugPD, "Reordering conjunction\n", [], !IO)
+    ),
     DeforestInfo0 = deforest_info(EarlierGoal, EarlierBranchInfo,
         BetweenGoals0, LaterGoal, LaterBranchInfo, DeforestBranches),
 
@@ -1679,17 +1738,17 @@
     %
 :- pred push_goal_into_goal(set(prog_var)::in, set(int)::in,
     hlds_goal::in, hlds_goals::in, hlds_goal::in, hlds_goal::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
 push_goal_into_goal(NonLocals, DeforestInfo, EarlierGoal,
-        BetweenGoals, LaterGoal, Goal, !PDInfo, !IO) :-
+        BetweenGoals, LaterGoal, Goal, !PDInfo) :-
     pd_info_get_instmap(!.PDInfo, InstMap0),
     EarlierGoal = hlds_goal(EarlierGoalExpr, _),
     (
         EarlierGoalExpr = switch(Var1, CanFail1, Cases1),
         set.insert(NonLocals, Var1, CaseNonLocals),
         append_goal_to_cases(Var1, BetweenGoals, LaterGoal,
-            CaseNonLocals, 1, DeforestInfo, Cases1, Cases, !PDInfo, !IO),
+            CaseNonLocals, 1, DeforestInfo, Cases1, Cases, !PDInfo),
         GoalExpr = switch(Var1, CanFail1, Cases)
     ;
         EarlierGoalExpr = if_then_else(Vars, Cond, Then0, Else0),
@@ -1698,15 +1757,15 @@
         CondNonLocals = goal_info_get_nonlocals(CondInfo),
         set.union(CondNonLocals, NonLocals, ThenNonLocals),
         append_goal(Then0, BetweenGoals, LaterGoal,
-            ThenNonLocals, 1, DeforestInfo, Then, !PDInfo, !IO),
+            ThenNonLocals, 1, DeforestInfo, Then, !PDInfo),
         pd_info_set_instmap(InstMap0, !PDInfo),
         append_goal(Else0, BetweenGoals, LaterGoal,
-            NonLocals, 2, DeforestInfo, Else, !PDInfo, !IO),
+            NonLocals, 2, DeforestInfo, Else, !PDInfo),
         GoalExpr = if_then_else(Vars, Cond, Then, Else)
     ;
         EarlierGoalExpr = disj(Disjuncts0),
         append_goal_to_disjuncts(BetweenGoals, LaterGoal,
-            NonLocals, 1, DeforestInfo, Disjuncts0, Disjuncts, !PDInfo, !IO),
+            NonLocals, 1, DeforestInfo, Disjuncts0, Disjuncts, !PDInfo),
         GoalExpr = disj(Disjuncts)
     ;
         ( EarlierGoalExpr = unify(_, _, _, _, _)
@@ -1747,55 +1806,53 @@
     pd_info_set_instmap(InstMap0, !PDInfo),
 
     % Perform any folding which may now be possible.
-    deforest_goal(Goal3, Goal, !PDInfo, !IO),
+    deforest_goal(Goal3, Goal, !PDInfo),
     pd_info_set_instmap(InstMap0, !PDInfo).
 
 :- pred append_goal_to_disjuncts(hlds_goals::in, hlds_goal::in,
     set(prog_var)::in, int::in, set(int)::in, hlds_goals::in, hlds_goals::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
-append_goal_to_disjuncts(_, _, _, _, _, [], [], !PDInfo, !IO).
+append_goal_to_disjuncts(_, _, _, _, _, [], [], !PDInfo).
 append_goal_to_disjuncts(BetweenGoals, GoalToAppend, NonLocals,
-        CurrBranch, Branches, [Goal0 | Goals0], [Goal | Goals],
-        !PDInfo, !IO) :-
+        CurrBranch, Branches, [Goal0 | Goals0], [Goal | Goals], !PDInfo) :-
     pd_info_get_instmap(!.PDInfo, InstMap0),
     append_goal(Goal0, BetweenGoals, GoalToAppend,
-        NonLocals, CurrBranch, Branches, Goal, !PDInfo, !IO),
+        NonLocals, CurrBranch, Branches, Goal, !PDInfo),
     NextBranch = CurrBranch + 1,
     pd_info_set_instmap(InstMap0, !PDInfo),
     append_goal_to_disjuncts(BetweenGoals, GoalToAppend,
-        NonLocals, NextBranch, Branches, Goals0, Goals, !PDInfo, !IO).
+        NonLocals, NextBranch, Branches, Goals0, Goals, !PDInfo).
 
 :- pred append_goal_to_cases(prog_var::in, hlds_goals::in,
     hlds_goal::in, set(prog_var)::in, int::in, set(int)::in,
-    list(case)::in,list(case)::out, pd_info::in, pd_info::out,
-    io::di, io::uo) is det.
+    list(case)::in,list(case)::out, pd_info::in, pd_info::out) is det.
 
-append_goal_to_cases(_, _, _, _, _, _, [], [], !PDInfo, !IO).
+append_goal_to_cases(_, _, _, _, _, _, [], [], !PDInfo).
 append_goal_to_cases(Var, BetweenGoals, GoalToAppend, NonLocals,
-        CurrCase, Branches, [Case0 | Cases0], [Case | Cases], !PDInfo, !IO) :-
+        CurrCase, Branches, [Case0 | Cases0], [Case | Cases], !PDInfo) :-
     Case0 = case(MainConsId, OtherConsIds, Goal0),
     pd_info_get_instmap(!.PDInfo, InstMap0),
     pd_info_bind_var_to_functors(Var, MainConsId, OtherConsIds, !PDInfo),
     append_goal(Goal0, BetweenGoals, GoalToAppend, NonLocals,
-        CurrCase, Branches, Goal, !PDInfo, !IO),
+        CurrCase, Branches, Goal, !PDInfo),
     Case = case(MainConsId, OtherConsIds, Goal),
     NextCase = CurrCase + 1,
     pd_info_set_instmap(InstMap0, !PDInfo),
     append_goal_to_cases(Var, BetweenGoals, GoalToAppend,
-        NonLocals, NextCase, Branches, Cases0, Cases, !PDInfo, !IO).
+        NonLocals, NextCase, Branches, Cases0, Cases, !PDInfo).
 
 :- pred append_goal(hlds_goal::in, hlds_goals::in,
     hlds_goal::in, set(prog_var)::in, int::in, set(int)::in,
-    hlds_goal::out, pd_info::in, pd_info::out, io::di, io::uo) is det.
+    hlds_goal::out, pd_info::in, pd_info::out) is det.
 
 append_goal(Goal0, BetweenGoals, GoalToAppend0, NonLocals0,
-        CurrBranch, Branches, Goal, !PDInfo, !IO) :-
+        CurrBranch, Branches, Goal, !PDInfo) :-
     ( set.member(CurrBranch, Branches) ->
         % Unfold the call.
         pd_info_get_instmap(!.PDInfo, InstMap0),
         list.foldl(pd_info_update_goal, [Goal0 | BetweenGoals], !PDInfo),
-        deforest_goal(GoalToAppend0, GoalToAppend, !PDInfo, !IO),
+        deforest_goal(GoalToAppend0, GoalToAppend, !PDInfo),
         pd_info_set_instmap(InstMap0, !PDInfo)
     ;
         GoalToAppend = GoalToAppend0
@@ -1818,10 +1875,10 @@
 
 :- pred deforest_call(pred_id::in, proc_id::in, list(prog_var)::in,
     sym_name::in, builtin_state::in, hlds_goal::in, hlds_goal::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
 deforest_call(PredId, ProcId, Args, SymName, BuiltinState, Goal0, Goal,
-        !PDInfo, !IO) :-
+        !PDInfo) :-
     pd_info_get_proc_arg_info(!.PDInfo, ProcArgInfos),
     pd_info_get_module_info(!.PDInfo, ModuleInfo),
     pd_info_get_instmap(!.PDInfo, InstMap),
@@ -1866,44 +1923,52 @@
         ; CalledGoalSize < SizeThreshold
         )
     ->
+        trace [io(!IO)] (
         pd_debug_message_context(DebugPD, Context,
             "Found extra information for call to %s/%i\n",
-            [s(Name), i(Arity)], !IO),
+                [s(Name), i(Arity)], !IO)
+        ),
         (
             pd_term.local_check(ModuleInfo, Goal0, InstMap,
                 LocalTermInfo0, LocalTermInfo)
         ->
+            trace [io(!IO)] (
             pd_debug_message(DebugPD,
-                "Local termination check succeeded\n", [], !IO),
+                    "Local termination check succeeded\n", [], !IO)
+            ),
             pd_info_set_local_term_info(LocalTermInfo, !PDInfo),
             unfold_call(yes, yes, PredId, ProcId,
-                Args, Goal0, Goal1, Optimized, !PDInfo, !IO),
+                Args, Goal0, Goal1, Optimized, !PDInfo),
             (
                 Optimized = yes,
-                deforest_goal(Goal1, Goal, !PDInfo, !IO)
+                deforest_goal(Goal1, Goal, !PDInfo)
             ;
                 Optimized = no,
                 Goal = Goal1
             ),
             pd_info_set_local_term_info(LocalTermInfo0, !PDInfo)
         ;
+            trace [io(!IO)] (
             pd_debug_message(DebugPD,
-                "Local termination check failed\n", [], !IO),
+                    "Local termination check failed\n", [], !IO)
+            ),
             Goal = hlds_goal(GoalExpr0, GoalInfo0)
         )
     ;
+        trace [io(!IO)] (
         pd_debug_message_context(DebugPD, Context,
             "No extra information for call to %s/%i\n",
-            [s(Name), i(Arity)], !IO),
+                [s(Name), i(Arity)], !IO)
+        ),
         Goal = Goal0
     ).
 
 :- pred unfold_call(bool::in, bool::in, pred_id::in, proc_id::in,
     list(prog_var)::in, hlds_goal::in, hlds_goal::out, bool::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
 unfold_call(CheckImprovement, CheckVars, PredId, ProcId, Args,
-        Goal0, Goal, Optimized, !PDInfo, !IO) :-
+        Goal0, Goal, Optimized, !PDInfo) :-
     pd_info_get_module_info(!.PDInfo, ModuleInfo),
     module_info_get_globals(ModuleInfo, Globals),
     globals.lookup_int_option(Globals, deforestation_vars_threshold, VarsOpt),
@@ -1967,13 +2032,15 @@
         ),
 
         % Push the extra information from the call through the goal.
-        pd_debug_message(DebugPD, "Running unique modes\n", [], !IO),
+        trace [io(!IO)] (
+            pd_debug_message(DebugPD, "Running unique modes\n", [], !IO)
+        ),
         proc_info_arglives(CalledProcInfo, ModuleInfo, ArgLives),
         get_live_vars(Args, ArgLives, LiveVars0),
         set.list_to_set(LiveVars0, LiveVars1),
         set.intersect(NonLocals, LiveVars1, LiveVars),
-        pd_util.unique_modecheck_goal(LiveVars, Goal2, Goal3, Errors,
-            !PDInfo, !IO),
+        pd_util.unique_modecheck_goal_live_vars(LiveVars, Goal2, Goal3, Errors,
+            !PDInfo),
 
         (
             Errors = [],
@@ -1985,7 +2052,9 @@
             Optimized0 = no
         ),
 
-        pd_debug_message(DebugPD, "Running simplify\n", [], !IO),
+        trace [io(!IO)] (
+            pd_debug_message(DebugPD, "Running simplify\n", [], !IO)
+        ),
         simplify.find_simplifications(no, Globals, Simplifications),
         pd_util.pd_simplify_goal(Simplifications, Goal3, Goal4, !PDInfo),
 
Index: compiler/delay_construct.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/delay_construct.m,v
retrieving revision 1.29
diff -u -b -r1.29 delay_construct.m
--- compiler/delay_construct.m	14 Oct 2009 05:28:31 -0000	1.29
+++ compiler/delay_construct.m	5 Aug 2010 02:49:18 -0000
@@ -30,12 +30,10 @@
 :- import_module hlds.hlds_module.
 :- import_module hlds.hlds_pred.
 
-:- import_module io.
-
 %-----------------------------------------------------------------------------%
 
-:- pred delay_construct_proc(pred_id::in, proc_id::in, module_info::in,
-    proc_info::in, proc_info::out, io::di, io::uo) is det.
+:- pred delay_construct_proc(module_info::in, pred_proc_id::in,
+    proc_info::in, proc_info::out) is det.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -52,23 +50,20 @@
 :- import_module parse_tree.prog_data.
 
 :- import_module bool.
+:- import_module io.
 :- import_module list.
 :- import_module pair.
 :- import_module set.
 
 %-----------------------------------------------------------------------------%
 
-delay_construct_proc(PredId, ProcId, ModuleInfo, !ProcInfo, !IO) :-
+delay_construct_proc(ModuleInfo, proc(PredId, ProcId), !ProcInfo) :-
+    trace [io(!IO)] (
     write_proc_progress_message("% Delaying construction unifications in ",
-        PredId, ProcId, ModuleInfo, !IO),
+            PredId, ProcId, ModuleInfo, !IO)
+    ),
     module_info_get_globals(ModuleInfo, Globals),
     module_info_pred_info(ModuleInfo, PredId, PredInfo),
-    delay_construct_proc_no_io(PredInfo, ModuleInfo, Globals, !ProcInfo).
-
-:- pred delay_construct_proc_no_io(pred_info::in, module_info::in, globals::in,
-    proc_info::in, proc_info::out) is det.
-
-delay_construct_proc_no_io(PredInfo, ModuleInfo, Globals, !ProcInfo) :-
     body_should_use_typeinfo_liveness(PredInfo, Globals, BodyTypeinfoLiveness),
     proc_info_get_vartypes(!.ProcInfo, VarTypes),
     proc_info_get_rtti_varmaps(!.ProcInfo, RttiVarMaps),
Index: compiler/follow_code.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/follow_code.m,v
retrieving revision 1.97
diff -u -b -r1.97 follow_code.m
--- compiler/follow_code.m	8 Sep 2009 02:43:32 -0000	1.97
+++ compiler/follow_code.m	5 Aug 2010 02:49:19 -0000
@@ -36,7 +36,7 @@
 
 %-----------------------------------------------------------------------------%
 
-:- pred move_follow_code_in_proc(pred_id::in, proc_id::in, pred_info::in,
+:- pred move_follow_code_in_proc(pred_proc_id::in,
     proc_info::in, proc_info::out, module_info::in, module_info::out) is det.
 
 %-----------------------------------------------------------------------------%
@@ -63,8 +63,7 @@
 
 %-----------------------------------------------------------------------------%
 
-move_follow_code_in_proc(_PredId, _ProcId, _PredInfo, !ProcInfo,
-        !ModuleInfo) :-
+move_follow_code_in_proc(_PredProcId, !ProcInfo, !ModuleInfo) :-
     module_info_get_globals(!.ModuleInfo, Globals),
     globals.lookup_bool_option(Globals, follow_code, FollowCode),
     (
Index: compiler/liveness.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/liveness.m,v
retrieving revision 1.174
diff -u -b -r1.174 liveness.m
--- compiler/liveness.m	30 Jul 2010 05:16:12 -0000	1.174
+++ compiler/liveness.m	5 Aug 2010 02:49:19 -0000
@@ -158,7 +158,6 @@
 :- import_module hlds.hlds_pred.
 :- import_module parse_tree.prog_data.
 
-:- import_module io.
 :- import_module set.
 
 %-----------------------------------------------------------------------------%
@@ -166,12 +165,12 @@
     % Add liveness annotations to the goal of the procedure. This consists of
     % the {pre,post}{birth,death} sets and resume point information.
     %
-:- pred detect_liveness_proc(pred_id::in, proc_id::in, module_info::in,
-    proc_info::in, proc_info::out, io::di, io::uo) is det.
+:- pred detect_liveness_proc(module_info::in, pred_proc_id::in,
+    proc_info::in, proc_info::out) is det.
 
     % Add liveness annotations to the goals of nonimported procedures in the
-    % module.  Attempt to do so in parallel.  Debugging liveness is not
-    % supported in this version.
+    % module. Attempt to do so in parallel. Debugging liveness is not supported
+    % in this version.
     %
 :- pred detect_liveness_preds_parallel(module_info::in, module_info::out)
     is det.
@@ -207,6 +206,7 @@
 
 :- import_module assoc_list.
 :- import_module bool.
+:- import_module io.
 :- import_module list.
 :- import_module map.
 :- import_module maybe.
@@ -219,19 +219,6 @@
 
 %-----------------------------------------------------------------------------%
 
-:- typeclass debug_liveness_io(T) where [
-    pred maybe_debug_liveness(string::in, int::in, int::in, hlds_goal::in,
-        prog_varset::in, module_info::in, T::di, T::uo) is det
-].
-
-:- instance debug_liveness_io(io) where [
-    pred(maybe_debug_liveness/8) is io_maybe_debug_liveness
-].
-
-:- instance debug_liveness_io(unit) where [
-    maybe_debug_liveness(_, _, _, _, _, _, unit, unit)
-].
-
 %-----------------------------------------------------------------------------%
 
 detect_liveness_preds_parallel(!HLDS) :-
@@ -273,19 +260,18 @@
 
 detect_liveness_pred_proc(ModuleInfo, PredId, ProcId, !PredInfo) :-
     pred_info_proc_info(!.PredInfo, ProcId, ProcInfo0),
-    detect_liveness_proc_2(PredId, ModuleInfo, ProcInfo0, ProcInfo, unit, _),
+    detect_liveness_proc_2(ModuleInfo, PredId, ProcInfo0, ProcInfo),
     pred_info_set_proc_info(ProcId, ProcInfo, !PredInfo).
 
 %-----------------------------------------------------------------------------%
 
-detect_liveness_proc(PredId, _ProcId, ModuleInfo, !ProcInfo, !IO) :-
-    detect_liveness_proc_2(PredId, ModuleInfo, !ProcInfo, !IO).
+detect_liveness_proc(ModuleInfo, proc(PredId, _ProcId), !ProcInfo) :-
+    detect_liveness_proc_2(ModuleInfo, PredId, !ProcInfo).
 
-:- pred detect_liveness_proc_2(pred_id::in, module_info::in,
-    proc_info::in, proc_info::out, IO::di, IO::uo) is det
-    <= debug_liveness_io(IO).
+:- pred detect_liveness_proc_2(module_info::in, pred_id::in, 
+    proc_info::in, proc_info::out) is det.
 
-detect_liveness_proc_2(PredId, ModuleInfo, !ProcInfo, !IO) :-
+detect_liveness_proc_2(ModuleInfo, PredId, !ProcInfo) :-
     requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
 
     proc_info_get_goal(!.ProcInfo, Goal0),
@@ -300,19 +286,25 @@
 
     globals.lookup_int_option(Globals, debug_liveness, DebugLiveness),
     pred_id_to_int(PredId, PredIdInt),
+    trace [io(!IO)] (
     maybe_debug_liveness("\nbefore liveness",
-        DebugLiveness, PredIdInt, Goal0, VarSet, ModuleInfo, !IO),
+            DebugLiveness, PredIdInt, Goal0, VarSet, ModuleInfo, !IO)
+    ),
 
     initial_liveness(!.ProcInfo, PredId, ModuleInfo, Liveness0),
     detect_liveness_in_goal(Goal0, Goal1, Liveness0, _, LiveInfo),
 
+    trace [io(!IO)] (
     maybe_debug_liveness("\nafter liveness",
-        DebugLiveness, PredIdInt, Goal1, VarSet, ModuleInfo, !IO),
+            DebugLiveness, PredIdInt, Goal1, VarSet, ModuleInfo, !IO)
+    ),
 
     initial_deadness(!.ProcInfo, LiveInfo, ModuleInfo, Deadness0),
     detect_deadness_in_goal(Goal1, Goal2, Deadness0, _, Liveness0, LiveInfo),
+    trace [io(!IO)] (
     maybe_debug_liveness("\nafter deadness",
-        DebugLiveness, PredIdInt, Goal2, VarSet, ModuleInfo, !IO),
+            DebugLiveness, PredIdInt, Goal2, VarSet, ModuleInfo, !IO)
+    ),
 
     (
         globals.get_trace_level(Globals, TraceLevel),
@@ -322,8 +314,10 @@
         DelayDeath = yes
     ->
         delay_death_proc_body(Goal2, Goal3, VarSet, Liveness0),
+        trace [io(!IO)] (
         maybe_debug_liveness("\nafter delay death",
             DebugLiveness, PredIdInt, Goal3, VarSet, ModuleInfo, !IO)
+        )
     ;
         Goal3 = Goal2
     ),
@@ -339,15 +333,17 @@
     ),
     detect_resume_points_in_goal(Goal3, Goal, Liveness0, _,
         LiveInfo, ResumeVars0),
+    trace [io(!IO)] (
     maybe_debug_liveness("\nafter resume point",
-        DebugLiveness, PredIdInt, Goal, VarSet, ModuleInfo, !IO),
+            DebugLiveness, PredIdInt, Goal, VarSet, ModuleInfo, !IO)
+    ),
     proc_info_set_goal(Goal, !ProcInfo),
     proc_info_set_liveness_info(Liveness0, !ProcInfo).
 
-:- pred io_maybe_debug_liveness(string::in, int::in, int::in,
+:- pred maybe_debug_liveness(string::in, int::in, int::in,
     hlds_goal::in, prog_varset::in, module_info::in, io::di, io::uo) is det.
 
-io_maybe_debug_liveness(Message, DebugLiveness, PredIdInt,
+maybe_debug_liveness(Message, DebugLiveness, PredIdInt,
         Goal, VarSet, ModuleInfo, !IO) :-
     ( DebugLiveness = PredIdInt ->
         io.write_string(Message, !IO),
Index: compiler/loop_inv.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/loop_inv.m,v
retrieving revision 1.53
diff -u -b -r1.53 loop_inv.m
--- compiler/loop_inv.m	10 Jun 2010 00:58:17 -0000	1.53
+++ compiler/loop_inv.m	5 Aug 2010 02:49:19 -0000
@@ -102,13 +102,13 @@
 
 %-----------------------------------------------------------------------------%
 
-    % hoist_loop_invariants(PredId, ProcId, PredInfo,
+    % hoist_loop_invariants(PredProcId, PredInfo,
     %       ProcInfo0, ProcInfo, ModuleInfo0, ModuleInfo)
     %
     % Analyze the procedure identified by PredProcId and, if appropriate,
     % split it into two applying the loop invariant hoisting optimization.
     %
-:- pred hoist_loop_invariants(pred_id::in, proc_id::in, pred_info::in,
+:- pred hoist_loop_invariants(pred_proc_id::in, pred_info::in,
     proc_info::in, proc_info::out, module_info::in, module_info::out) is det.
 
 %-----------------------------------------------------------------------------%
@@ -141,7 +141,7 @@
 
 %-----------------------------------------------------------------------------%
 
-hoist_loop_invariants(PredId, ProcId, PredInfo, !ProcInfo, !ModuleInfo) :-
+hoist_loop_invariants(PredProcId, PredInfo, !ProcInfo, !ModuleInfo) :-
     (
 
         % We only want to apply this optimization to pure preds (e.g.
@@ -151,7 +151,6 @@
         % Next, work out whether this predicate is optimizable and
         % compute some auxiliary results along the way.
 
-        PredProcId = proc(PredId, ProcId),
         proc_info_get_goal(!.ProcInfo, Body),
         proc_info_get_headvars(!.ProcInfo, HeadVars),
         proc_info_get_argmodes(!.ProcInfo, HeadVarModes),
@@ -236,9 +235,8 @@
         % computed invariant vars to the argument list, replacing invariant
         % goals in InProc with `true', and recursive calls at the end of
         % recursive paths with calls to the auxiliary procedure.
-        gen_aux_proc(InvGoals, PredProcId,
-            AuxPredProcId, CallAux, Body, AuxPredInfo, AuxProcInfo,
-            !ModuleInfo),
+        gen_aux_proc(InvGoals, PredProcId, AuxPredProcId, CallAux, Body,
+            AuxPredInfo, AuxProcInfo, !ModuleInfo),
 
         % We construct OutProc by replacing recursive calls to the InProc
         % at the end of recursive paths with calls to the auxiliary procedure.
Index: compiler/mark_tail_calls.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mark_tail_calls.m,v
retrieving revision 1.1
diff -u -b -r1.1 mark_tail_calls.m
--- compiler/mark_tail_calls.m	25 Nov 2008 07:46:41 -0000	1.1
+++ compiler/mark_tail_calls.m	5 Aug 2010 02:49:19 -0000
@@ -25,8 +25,8 @@
 :- import_module hlds.hlds_module.
 :- import_module hlds.hlds_pred.
 
-:- pred mark_tail_calls(goal_feature::in, pred_id::in, proc_id::in,
-    module_info::in, pred_info::in, proc_info::in, proc_info::out) is det.
+:- pred mark_tail_calls(goal_feature::in, module_info::in, pred_proc_id::in,
+    pred_info::in, proc_info::in, proc_info::out) is det.
 
 %-----------------------------------------------------------------------------%
 
@@ -44,7 +44,8 @@
     --->    found_tail_calls
     ;       not_found_tail_calls.
 
-mark_tail_calls(Feature, PredId, ProcId, ModuleInfo, PredInfo, !ProcInfo) :-
+mark_tail_calls(Feature, ModuleInfo, proc(PredId, ProcId), PredInfo,
+        !ProcInfo) :-
     pred_info_get_arg_types(PredInfo, Types),
     proc_info_get_goal(!.ProcInfo, Goal0),
     proc_info_interface_determinism(!.ProcInfo, Detism),
Index: compiler/mercury_compile_front_end.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mercury_compile_front_end.m,v
retrieving revision 1.4
diff -u -b -r1.4 mercury_compile_front_end.m
--- compiler/mercury_compile_front_end.m	30 Jul 2010 05:16:12 -0000	1.4
+++ compiler/mercury_compile_front_end.m	5 Aug 2010 02:49:19 -0000
@@ -884,7 +884,7 @@
         maybe_write_string(Verbose, "% Simplifying goals...\n", !IO),
         maybe_flush_output(Verbose, !IO),
         Simplifications = list_to_simplifications(SimpList),
-        process_all_nonimported_procs_errors(
+        process_all_nonimported_preds_errors(
             update_pred_error(simplify_pred(Simplifications)),
             !HLDS, [], SimplifySpecs, !IO),
         (
Index: compiler/mercury_compile_llds_back_end.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mercury_compile_llds_back_end.m,v
retrieving revision 1.6
diff -u -b -r1.6 mercury_compile_llds_back_end.m
--- compiler/mercury_compile_llds_back_end.m	4 Aug 2010 02:34:09 -0000	1.6
+++ compiler/mercury_compile_llds_back_end.m	5 Aug 2010 02:49:19 -0000
@@ -139,7 +139,7 @@
     global_data::in, global_data::out, list(c_procedure)::out,
     dump_info::in, dump_info::out, io::di, io::uo) is det.
 
-llds_backend_pass_by_phases(!HLDS, !GlobalData, LLDS, !DumpInfo, !IO) :-
+llds_backend_pass_by_phases(!HLDS, !GlobalData, !:LLDS, !DumpInfo, !IO) :-
     module_info_get_globals(!.HLDS, Globals),
     globals.lookup_bool_option(Globals, verbose, Verbose),
     globals.lookup_bool_option(Globals, statistics, Stats),
@@ -174,15 +174,14 @@
     maybe_goal_paths(Verbose, Stats, !HLDS, !IO),
     maybe_dump_hlds(!.HLDS, 345, "precodegen", !DumpInfo, !IO),
 
-    generate_llds_code_for_module(!.HLDS, Verbose, Stats, !GlobalData, LLDS1,
+    generate_llds_code_for_module(!.HLDS, Verbose, Stats, !GlobalData, !:LLDS,
         !IO),
 
-    maybe_generate_stack_layouts(!.HLDS, LLDS1, Verbose, Stats, !GlobalData,
+    maybe_generate_stack_layouts(!.HLDS, !.LLDS, Verbose, Stats, !GlobalData,
         !IO),
     % maybe_dump_global_data(!.GlobalData, !IO),
 
-    maybe_optimize_llds(!.HLDS, !.GlobalData, Verbose, Stats, LLDS1, LLDS,
-        !IO).
+    maybe_optimize_llds(!.HLDS, !.GlobalData, Verbose, Stats, !LLDS, !IO).
 
 :- pred llds_backend_pass_by_preds(module_info::in, module_info::out,
     global_data::in, global_data::out, list(c_procedure)::out,
@@ -280,28 +279,29 @@
 
 llds_backend_pass_for_pred([], _, _, !HLDS, !GlobalData, [], !IO).
 llds_backend_pass_for_pred([ProcId | ProcIds], PredId, PredInfo, !HLDS,
-        !GlobalData, [ProcLabel - Proc | Procs], !IO) :-
+        !GlobalData, [ProcLabel - ProcCode | ProcCodes], !IO) :-
     ProcLabel = make_proc_label(!.HLDS, PredId, ProcId),
     pred_info_get_procedures(PredInfo, ProcTable),
     map.lookup(ProcTable, ProcId, ProcInfo),
-    llds_backend_pass_for_procs(PredInfo, ProcInfo, _, ProcId, PredId, !HLDS,
-        !GlobalData, Proc, !IO),
+    llds_backend_pass_for_proc(PredInfo, ProcInfo, ProcId, PredId, !HLDS,
+        !GlobalData, ProcCode, !IO),
     llds_backend_pass_for_pred(ProcIds, PredId, PredInfo, !HLDS, !GlobalData,
-        Procs, !IO).
+        ProcCodes, !IO).
 
-:- pred llds_backend_pass_for_procs(pred_info::in,
-    proc_info::in, proc_info::out, proc_id::in, pred_id::in,
-    module_info::in, module_info::out, global_data::in, global_data::out,
-    c_procedure::out, io::di, io::uo) is det.
+:- pred llds_backend_pass_for_proc(pred_info::in, proc_info::in,
+    proc_id::in, pred_id::in, module_info::in, module_info::out,
+    global_data::in, global_data::out, c_procedure::out, io::di, io::uo)
+    is det.
 
-llds_backend_pass_for_procs(PredInfo, !ProcInfo, ProcId, PredId, !HLDS,
+llds_backend_pass_for_proc(PredInfo, !.ProcInfo, ProcId, PredId, !HLDS,
         !GlobalData, ProcCode, !IO) :-
+    PredProcId = proc(PredId, ProcId),
     module_info_get_globals(!.HLDS, Globals),
     globals.lookup_bool_option(Globals, optimize_saved_vars_const,
         SavedVarsConst),
     (
         SavedVarsConst = yes,
-        saved_vars_proc(PredId, ProcId, !ProcInfo, !HLDS, !IO)
+        saved_vars_proc(proc(PredId, ProcId), !ProcInfo, !HLDS)
     ;
         SavedVarsConst = no
     ),
@@ -309,14 +309,14 @@
         SavedVarsCell),
     (
         SavedVarsCell = yes,
-        stack_opt_cell(PredId, ProcId, !ProcInfo, !HLDS, !IO)
+        stack_opt_cell(PredProcId, !ProcInfo, !HLDS)
     ;
         SavedVarsCell = no
     ),
     globals.lookup_bool_option(Globals, follow_code, FollowCode),
     (
         FollowCode = yes,
-        move_follow_code_in_proc(PredId, ProcId, PredInfo, !ProcInfo, !HLDS)
+        move_follow_code_in_proc(PredProcId, !ProcInfo, !HLDS)
     ;
         FollowCode = no
     ),
@@ -351,25 +351,25 @@
     simplify_proc(Simplifications, PredId, ProcId, !HLDS, !ProcInfo),
     write_proc_progress_message("% Computing liveness in ", PredId, ProcId,
         !.HLDS, !IO),
-    detect_liveness_proc(PredId, ProcId, !.HLDS, !ProcInfo, !IO),
+    detect_liveness_proc(!.HLDS, PredProcId, !ProcInfo),
     globals.lookup_bool_option(Globals, exec_trace_tail_rec, ExecTraceTailRec),
     (
         ExecTraceTailRec = yes,
         write_proc_progress_message(
             "% Marking directly tail recursive calls in ", PredId, ProcId,
             !.HLDS, !IO),
-        mark_tail_calls(feature_debug_tail_rec_call, PredId, ProcId,
-            !.HLDS, PredInfo, !ProcInfo)
+        mark_tail_calls(feature_debug_tail_rec_call, !.HLDS, PredProcId,
+            PredInfo, !ProcInfo)
     ;
         ExecTraceTailRec = no
     ),
     write_proc_progress_message("% Allocating stack slots in ", PredId,
         ProcId, !.HLDS, !IO),
-    allocate_stack_slots_in_proc(PredId, ProcId, !.HLDS, !ProcInfo, !IO),
+    allocate_stack_slots_in_proc(!.HLDS, PredProcId, !ProcInfo),
     write_proc_progress_message(
         "% Allocating storage locations for live vars in ",
         PredId, ProcId, !.HLDS, !IO),
-    allocate_store_maps(final_allocation, PredId, !.HLDS, !ProcInfo),
+    allocate_store_maps(final_allocation, !.HLDS, PredProcId, !ProcInfo),
     globals.get_trace_level(Globals, TraceLevel),
     ( given_trace_level_is_none(TraceLevel) = no ->
         write_proc_progress_message("% Calculating goal paths in ",
@@ -385,12 +385,11 @@
     globals.lookup_bool_option(Globals, optimize, Optimize),
     (
         Optimize = yes,
-        optimize_proc(Globals, !.GlobalData, ProcCode0, ProcCode, !IO)
+        optimize_proc(Globals, !.GlobalData, ProcCode0, ProcCode)
     ;
         Optimize = no,
         ProcCode = ProcCode0
     ),
-    PredProcId = ProcCode ^ cproc_id,
     Instructions = ProcCode ^ cproc_code,
     write_proc_progress_message(
         "% Generating call continuation information for ",
@@ -422,7 +421,7 @@
         maybe_write_string(Verbose,
             "% Minimizing variable saves using constants...\n", !IO),
         maybe_flush_output(Verbose, !IO),
-        process_all_nonimported_procs(update_module_io(saved_vars_proc),
+        process_all_nonimported_procs(update_module(saved_vars_proc),
             !HLDS, !IO),
         maybe_write_string(Verbose, "% done.\n", !IO),
         maybe_report_stats(Stats, !IO)
@@ -441,7 +440,7 @@
         maybe_write_string(Verbose,
             "% Minimizing variable saves using cells...\n", !IO),
         maybe_flush_output(Verbose, !IO),
-        process_all_nonimported_procs(update_module_io(stack_opt_cell),
+        process_all_nonimported_procs(update_module(stack_opt_cell),
             !HLDS, !IO),
         maybe_write_string(Verbose, "% done.\n", !IO),
         maybe_report_stats(Stats, !IO)
@@ -482,7 +481,7 @@
     ->
         detect_liveness_preds_parallel(!HLDS)
     ;
-        process_all_nonimported_procs(update_proc_io(detect_liveness_proc),
+        process_all_nonimported_procs(update_proc_ids(detect_liveness_proc),
             !HLDS, !IO)
     ),
     maybe_write_string(Verbose, "% done.\n", !IO),
@@ -500,8 +499,7 @@
             "% Marking directly tail recursive calls...", !IO),
         maybe_flush_output(Verbose, !IO),
         process_all_nonimported_procs(
-            update_proc_predprocid(
-                mark_tail_calls(feature_debug_tail_rec_call)),
+            update_proc_ids_pred(mark_tail_calls(feature_debug_tail_rec_call)),
             !HLDS, !IO),
         maybe_write_string(Verbose, " done.\n", !IO),
         maybe_report_stats(Stats, !IO)
@@ -516,7 +514,7 @@
     maybe_write_string(Verbose, "% Computing stack vars...", !IO),
     maybe_flush_output(Verbose, !IO),
     process_all_nonimported_procs(
-        update_proc_io(allocate_stack_slots_in_proc), !HLDS, !IO),
+        update_proc_ids(allocate_stack_slots_in_proc), !HLDS, !IO),
     maybe_write_string(Verbose, " done.\n", !IO),
     maybe_report_stats(Stats, !IO).
 
@@ -527,7 +525,7 @@
     maybe_write_string(Verbose, "% Allocating store map...", !IO),
     maybe_flush_output(Verbose, !IO),
     process_all_nonimported_procs(
-        update_proc_predid(allocate_store_maps(final_allocation)), !HLDS, !IO),
+        update_proc_ids(allocate_store_maps(final_allocation)), !HLDS, !IO),
     maybe_write_string(Verbose, " done.\n", !IO),
     maybe_report_stats(Stats, !IO).
 
@@ -570,7 +568,7 @@
         Optimize = yes,
         maybe_write_string(Verbose, "% Doing optimizations...\n", !IO),
         maybe_flush_output(Verbose, !IO),
-        optimize_procs(Globals, GlobalData, !LLDS, !IO),
+        optimize_procs(Globals, GlobalData, !LLDS),
         maybe_write_string(Verbose, "% done.\n", !IO),
         maybe_report_stats(Stats, !IO)
     ;
Index: compiler/mercury_compile_middle_passes.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mercury_compile_middle_passes.m,v
retrieving revision 1.5
diff -u -b -r1.5 mercury_compile_middle_passes.m
--- compiler/mercury_compile_middle_passes.m	4 Jul 2010 10:24:08 -0000	1.5
+++ compiler/mercury_compile_middle_passes.m	5 Aug 2010 02:49:19 -0000
@@ -951,7 +951,8 @@
 
         maybe_write_string(Verbose, "% Hoisting loop invariants...\n", !IO),
         maybe_flush_output(Verbose, !IO),
-        process_all_nonimported_procs(update_module(hoist_loop_invariants),
+        process_all_nonimported_procs(
+            update_module_pred(hoist_loop_invariants),
             !HLDS, !IO),
         maybe_write_string(Verbose, "% done.\n", !IO),
         maybe_report_stats(Stats, !IO)
@@ -1015,7 +1016,7 @@
         maybe_write_string(Verbose,
             "% Delaying construction unifications ...\n", !IO),
         maybe_flush_output(Verbose, !IO),
-        process_all_nonimported_procs(update_proc_io(delay_construct_proc),
+        process_all_nonimported_procs(update_proc_ids(delay_construct_proc),
             !HLDS, !IO),
         maybe_write_string(Verbose, "% done.\n", !IO),
         maybe_report_stats(Stats, !IO)
@@ -1117,8 +1118,8 @@
         maybe_write_string(Verbose,
             "% Removing unneeded code from procedure bodies...\n", !IO),
         maybe_flush_output(Verbose, !IO),
-        process_all_nonimported_procs(
-            update_module_io(unneeded_process_proc_msg), !HLDS, !IO),
+        process_all_nonimported_procs(update_module(unneeded_process_proc_msg),
+            !HLDS, !IO),
         maybe_write_string(Verbose, "% done.\n", !IO),
         maybe_report_stats(Stats, !IO)
     ;
Index: compiler/optimize.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/optimize.m,v
retrieving revision 1.68
diff -u -b -r1.68 optimize.m
--- compiler/optimize.m	30 Jul 2010 05:32:54 -0000	1.68
+++ compiler/optimize.m	5 Aug 2010 02:49:19 -0000
@@ -20,16 +20,15 @@
 :- import_module ll_backend.global_data.
 :- import_module ll_backend.llds.
 
-:- import_module io.
 :- import_module list.
 
 %-----------------------------------------------------------------------------%
 
 :- pred optimize_procs(globals::in, global_data::in,
-    list(c_procedure)::in, list(c_procedure)::out, io::di, io::uo) is det.
+    list(c_procedure)::in, list(c_procedure)::out) is det.
 
 :- pred optimize_proc(globals::in, global_data::in,
-    c_procedure::in, c_procedure::out, io::di, io::uo) is det.
+    c_procedure::in, c_procedure::out) is det.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -61,6 +60,7 @@
 :- import_module counter.
 :- import_module dir.
 :- import_module int.
+:- import_module io.
 :- import_module map.
 :- import_module maybe.
 :- import_module set.
@@ -68,16 +68,30 @@
 
 %-----------------------------------------------------------------------------%
 
-optimize_procs(Globals, GlobalData, !Procs, !IO) :-
-    list.map_foldl(optimize_proc(Globals, GlobalData), !Procs, !IO).
+optimize_procs(_, _, [], []).
+optimize_procs(Globals, GlobalData, [Proc0 | Procs0], [Proc | Procs]) :-
+    optimize_proc(Globals, GlobalData, Proc0, Proc),
+    optimize_procs(Globals, GlobalData, Procs0, Procs).
 
-optimize_proc(Globals, GlobalData, CProc0, CProc, !IO) :-
+optimize_proc(Globals, GlobalData, CProc0, CProc) :-
     Info = init_llds_opt_info(Globals),
     some [!OptDebugInfo, !C, !Instrs] (
         CProc0 = c_procedure(Name, Arity, PredProcId, CodeModel, !:Instrs,
             ProcLabel, !:C, MayAlterRtti, CGlobalVars),
-        init_opt_debug_info(Info, Name, Arity, PredProcId, ProcLabel,
-            !.Instrs, !.C, !:OptDebugInfo, !IO),
+        need_opt_debug_info(Info, Name, Arity, PredProcId, MaybeBaseName),
+        (
+            MaybeBaseName = no,
+            !:OptDebugInfo = no_opt_debug_info
+        ;
+            MaybeBaseName = yes(BaseName),
+            FirstFileName = BaseName ++ ".opt" ++ num_to_str(0),
+            trace [io(!IO)] (
+                output_first_opt_debug(Info, FirstFileName, ProcLabel,
+                    !.Instrs, !.C, !IO)
+            ),
+            !:OptDebugInfo = opt_debug_info(BaseName, 0, FirstFileName, 0,
+                FirstFileName, !.Instrs)
+        ),
         Repeat = Info ^ lopt_opt_repeat,
         (
             global_data_maybe_get_proc_layout(GlobalData,
@@ -94,16 +108,22 @@
         ),
         Statistics = Info ^ lopt_detailed_statistics,
         optimize_initial(Info, LayoutLabelSet, ProcLabel, CodeModel,
-            MayAlterRtti, !C, !OptDebugInfo, !Instrs, !IO),
+            MayAlterRtti, !C, !OptDebugInfo, !Instrs),
         optimize_repeat(Info, Repeat, LayoutLabelSet, ProcLabel,
-            MayAlterRtti, !C, !OptDebugInfo, !Instrs, !IO),
-        maybe_report_stats(Statistics, !IO),
+            MayAlterRtti, !C, !OptDebugInfo, !Instrs),
+        trace [io(!IO)] (
+            maybe_report_stats(Statistics, !IO)
+        ),
         optimize_middle(Info, yes, LayoutLabelSet, ProcLabel, CodeModel,
-            MayAlterRtti, !C, !OptDebugInfo, !Instrs, !IO),
-        maybe_report_stats(Statistics, !IO),
+            MayAlterRtti, !C, !OptDebugInfo, !Instrs),
+        trace [io(!IO)] (
+            maybe_report_stats(Statistics, !IO)
+        ),
         optimize_last(Info, LayoutLabelSet, ProcLabel, !C, !.OptDebugInfo,
-            !Instrs, !IO),
-        maybe_report_stats(Statistics, !IO),
+            !Instrs),
+        trace [io(!IO)] (
+            maybe_report_stats(Statistics, !IO)
+        ),
         CProc = c_procedure(Name, Arity, PredProcId, CodeModel, !.Instrs,
             ProcLabel, !.C, MayAlterRtti, CGlobalVars)
     ).
@@ -129,12 +149,10 @@
             )
     ;       no_opt_debug_info.
 
-:- pred init_opt_debug_info(llds_opt_info::in, string::in, int::in,
-    pred_proc_id::in, proc_label::in, list(instruction)::in, counter::in,
-    opt_debug_info::out, io::di, io::uo) is det.
+:- pred need_opt_debug_info(llds_opt_info::in, string::in, int::in,
+    pred_proc_id::in, maybe(string)::out) is det.
 
-init_opt_debug_info(Info, Name, Arity, PredProcId, ProcLabel, Instrs0,
-        Counter, OptDebugInfo, !IO) :-
+need_opt_debug_info(Info, Name, Arity, PredProcId, MaybeBaseName) :-
     DebugOpt = Info ^ lopt_debug_opt,
     DebugOptPredIdStrs = Info ^ lopt_debug_opt_pred_ids,
     DebugOptPredNames = Info ^ lopt_debug_opt_pred_names,
@@ -176,14 +194,17 @@
             ++ mangle_name_as_filename(Name) ++ "_" ++ int_to_string(Arity)
             ++ ".pred" ++ int_to_string(PredIdInt)
             ++ ".proc" ++ int_to_string(ProcIdInt),
+        MaybeBaseName = yes(BaseName)
+    ;
+        MaybeBaseName = no
+    ).
+
+:- pred output_first_opt_debug(llds_opt_info::in, string::in, proc_label::in,
+    list(instruction)::in, counter::in, io::di, io::uo) is det.
 
+output_first_opt_debug(Info, FileName, ProcLabel, Instrs0, Counter, !IO) :-
         io.call_system("mkdir -p " ++ opt_subdir_name, MkdirRes, !IO),
         ( MkdirRes = ok(0) ->
-            true
-        ;
-            unexpected(this_file, "cannot make " ++ opt_subdir_name)
-        ),
-        FileName = BaseName ++ ".opt" ++ num_to_str(0),
         io.open_output(FileName, Res, !IO),
         (
             Res = ok(FileStream),
@@ -191,18 +212,16 @@
             counter.allocate(NextLabel, Counter, _),
             opt_debug.msg(yes, NextLabel, "before optimization", !IO),
             AutoComments = Info ^ lopt_auto_comments,
-            opt_debug.maybe_write_instrs(yes, AutoComments, yes(ProcLabel),
-                Instrs0, !IO),
+            opt_debug.maybe_write_instrs(yes, AutoComments,
+                yes(ProcLabel), Instrs0, !IO),
             io.set_output_stream(OutputStream, _, !IO),
             io.close_output(FileStream, !IO)
         ;
             Res = error(_),
             unexpected(this_file, "cannot open " ++ FileName)
-        ),
-        OptDebugInfo = opt_debug_info(BaseName, 0, FileName, 0, FileName,
-            Instrs0)
+        )
     ;
-        OptDebugInfo = no_opt_debug_info
+        unexpected(this_file, "cannot make " ++ opt_subdir_name)
     ).
 
 :- func opt_subdir_name = string.
@@ -220,23 +239,28 @@
 
 :- pred maybe_opt_debug(llds_opt_info::in, list(instruction)::in, counter::in,
     string::in, string::in, proc_label::in,
-    opt_debug_info::in, opt_debug_info::out, io::di, io::uo) is det.
+    opt_debug_info::in, opt_debug_info::out) is det.
 
 maybe_opt_debug(Info, Instrs, Counter, Suffix, Msg, ProcLabel,
-        !OptDebugInfo, !IO) :-
+        !OptDebugInfo) :-
     (
         !.OptDebugInfo = opt_debug_info(BaseName, OptNum0, _OptFileName0,
             PrevNum, PrevFileName, PrevInstrs),
-        ( Instrs = PrevInstrs ->
-            Same = yes
-        ;
-            Same = no
-        ),
         OptNum = OptNum0 + 1,
         OptFileName = BaseName ++ ".opt" ++ num_to_str(OptNum)
             ++ "." ++ Suffix,
         DiffFileName = BaseName ++ ".diff" ++ num_to_str(OptNum)
             ++ "." ++ Suffix,
+        ( Instrs = PrevInstrs ->
+            Same = yes,
+            !:OptDebugInfo = opt_debug_info(BaseName, OptNum, OptFileName,
+                PrevNum, PrevFileName, Instrs)
+        ;
+            Same = no,
+            !:OptDebugInfo = opt_debug_info(BaseName, OptNum, OptFileName,
+                OptNum, OptFileName, Instrs)
+        ),
+        trace [io(!IO)] (
         io.open_output(OptFileName, Res, !IO),
         (
             Res = ok(FileStream),
@@ -249,8 +273,8 @@
             ;
                 Same = no,
                 AutoComments = Info ^ lopt_auto_comments,
-                opt_debug.maybe_write_instrs(yes, AutoComments, yes(ProcLabel),
-                    Instrs, !IO)
+                    opt_debug.maybe_write_instrs(yes, AutoComments,
+                        yes(ProcLabel), Instrs, !IO)
             ),
             io.set_output_stream(OutputStream, _, !IO),
             io.close_output(FileStream, !IO)
@@ -260,19 +284,16 @@
             unexpected(this_file, ErrorMsg)
         ),
         (
-            Same = yes,
-            !:OptDebugInfo = opt_debug_info(BaseName, OptNum, OptFileName,
-                PrevNum, PrevFileName, Instrs)
+                Same = yes
         ;
             Same = no,
-            % Although the -u is not fully portable, it is available
-            % on all the systems we intend to use it on, and the main user
+                % Although the -u is not fully portable, it is available on
+                % all the systems we intend to use it on, and the main user
             % of --debug-opt (zs) strongly prefers -u to -c.
-            DiffCommand = "diff -u '" ++ PrevFileName ++ "' '" ++ OptFileName
-                ++ "' > '" ++ DiffFileName ++ "'",
-            io.call_system(DiffCommand, _, !IO),
-            !:OptDebugInfo = opt_debug_info(BaseName, OptNum, OptFileName,
-                OptNum, OptFileName, Instrs)
+                DiffCommand = "diff -u '" ++ PrevFileName ++ "' '" ++
+                    OptFileName ++ "' > '" ++ DiffFileName ++ "'",
+                io.call_system(DiffCommand, _, !IO)
+            )
         )
     ;
         !.OptDebugInfo = no_opt_debug_info
@@ -283,10 +304,10 @@
 :- pred optimize_initial(llds_opt_info::in, set(label)::in, proc_label::in,
     code_model::in, may_alter_rtti::in, counter::in, counter::out,
     opt_debug_info::in, opt_debug_info::out,
-    list(instruction)::in, list(instruction)::out, io::di, io::uo) is det.
+    list(instruction)::in, list(instruction)::out) is det.
 
 optimize_initial(Info, LayoutLabelSet, ProcLabel, CodeModel, MayAlterRtti,
-        !C, !OptDebugInfo, !Instrs, !IO) :-
+        !C, !OptDebugInfo, !Instrs) :-
     LabelStr = opt_util.format_proc_label(ProcLabel),
     OptFrames = Info ^ lopt_opt_frames,
     (
@@ -297,16 +318,18 @@
         VeryVerbose = Info ^ lopt_very_verbose,
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Optimizing nondet frames for ", !IO),
             io.write_string(LabelStr, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
         frameopt_keep_nondet_frame(ProcLabel, LayoutLabelSet,
             !C, !Instrs, _Mod),
         maybe_opt_debug(Info, !.Instrs, !.C, "ndframeopt",
-            "after nondet frame opt", ProcLabel, !OptDebugInfo, !IO)
+            "after nondet frame opt", ProcLabel, !OptDebugInfo)
     ;
         true
     ).
@@ -316,10 +339,10 @@
 :- pred optimize_repeat(llds_opt_info::in, int::in, set(label)::in,
     proc_label::in, may_alter_rtti::in, counter::in, counter::out,
     opt_debug_info::in, opt_debug_info::out,
-    list(instruction)::in, list(instruction)::out, io::di, io::uo) is det.
+    list(instruction)::in, list(instruction)::out) is det.
 
 optimize_repeat(Info, CurIter, LayoutLabelSet, ProcLabel,
-        MayAlterRtti, !C, !OptDebugInfo, !Instrs, !IO) :-
+        MayAlterRtti, !C, !OptDebugInfo, !Instrs) :-
     ( CurIter > 0 ->
         NextIter = CurIter - 1,
         ( NextIter = 0 ->
@@ -328,11 +351,11 @@
             Final = no
         ),
         optimize_repeated(Info, Final, LayoutLabelSet, ProcLabel, MayAlterRtti,
-            !C, !OptDebugInfo, !Instrs, Mod, !IO),
+            !C, !OptDebugInfo, !Instrs, Mod),
         (
             Mod = yes,
             optimize_repeat(Info, NextIter, LayoutLabelSet, ProcLabel,
-                MayAlterRtti, !C, !OptDebugInfo, !Instrs, !IO)
+                MayAlterRtti, !C, !OptDebugInfo, !Instrs)
         ;
             Mod = no
         )
@@ -346,11 +369,10 @@
 :- pred optimize_repeated(llds_opt_info::in, bool::in, set(label)::in,
     proc_label::in, may_alter_rtti::in, counter::in, counter::out,
     opt_debug_info::in, opt_debug_info::out,
-    list(instruction)::in, list(instruction)::out, bool::out,
-    io::di, io::uo) is det.
+    list(instruction)::in, list(instruction)::out, bool::out) is det.
 
 optimize_repeated(Info, Final, LayoutLabelSet, ProcLabel, MayAlterRtti,
-        !C, !OptDebugInfo, !Instrs, Mod, !IO) :-
+        !C, !OptDebugInfo, !Instrs, Mod) :-
     InstrsAtStart = !.Instrs,
     LabelStr = opt_util.format_proc_label(ProcLabel),
     VeryVerbose = Info ^ lopt_very_verbose,
@@ -362,9 +384,11 @@
         OptJump = yes,
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Optimizing jumps for ", !IO),
             io.write_string(LabelStr, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
@@ -372,7 +396,7 @@
             OptFullJump, Final, PessimizeTailCalls, CheckedNondetTailCalls,
             !C, !Instrs, Mod1),
         maybe_opt_debug(Info, !.Instrs, !.C, "jump", "after jump opt",
-            ProcLabel, !OptDebugInfo, !IO)
+            ProcLabel, !OptDebugInfo)
     ;
         OptJump = no,
         Mod1 = no
@@ -382,16 +406,18 @@
         Peephole = yes,
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Optimizing locally for ", !IO),
             io.write_string(LabelStr, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
         GC_Method = Info ^ lopt_gc_method,
         peephole_optimize(GC_Method, !Instrs, Mod2),
         maybe_opt_debug(Info, !.Instrs, !.C, "peep", "after peephole",
-            ProcLabel, !OptDebugInfo, !IO)
+            ProcLabel, !OptDebugInfo)
     ;
         Peephole = no,
         Mod2 = no
@@ -401,15 +427,17 @@
         OptLabels = yes,
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Optimizing labels for ", !IO),
             io.write_string(LabelStr, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
         labelopt_main(Final, LayoutLabelSet, !Instrs, Mod3),
         maybe_opt_debug(Info, !.Instrs, !.C, "label", "after label opt",
-            ProcLabel, !OptDebugInfo, !IO)
+            ProcLabel, !OptDebugInfo)
     ;
         OptLabels = no,
         Mod3 = no
@@ -419,15 +447,17 @@
         DupElim = yes,
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Optimizing duplicates for ", !IO),
             io.write_string(LabelStr, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
         dupelim_main(ProcLabel, !C, !Instrs),
         maybe_opt_debug(Info, !.Instrs, !.C, "dup", "after duplicates",
-            ProcLabel, !OptDebugInfo, !IO)
+            ProcLabel, !OptDebugInfo)
     ;
         DupElim = no
     ),
@@ -436,16 +466,18 @@
     ;
         Mod = yes
     ),
+    trace [io(!IO)] (
     Statistics = Info ^ lopt_detailed_statistics,
-    maybe_report_stats(Statistics, !IO).
+        maybe_report_stats(Statistics, !IO)
+    ).
 
 :- pred optimize_middle(llds_opt_info::in, bool::in, set(label)::in,
     proc_label::in, code_model::in, may_alter_rtti::in,
     counter::in, counter::out, opt_debug_info::in, opt_debug_info::out,
-    list(instruction)::in, list(instruction)::out, io::di, io::uo) is det.
+    list(instruction)::in, list(instruction)::out) is det.
 
 optimize_middle(Info, Final, LayoutLabelSet, ProcLabel, CodeModel,
-        MayAlterRtti, !C, !OptDebugInfo, !Instrs, !IO) :-
+        MayAlterRtti, !C, !OptDebugInfo, !Instrs) :-
     VeryVerbose = Info ^ lopt_very_verbose,
     LabelStr = opt_util.format_proc_label(ProcLabel),
 
@@ -454,9 +486,11 @@
         OptFrames = yes,
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Optimizing frames for ", !IO),
             io.write_string(LabelStr, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
@@ -473,9 +507,11 @@
                 FrameOptComments, Mod1)
         ),
         maybe_opt_debug(Info, !.Instrs, !.C, "frame", "after frame opt",
-            ProcLabel, !OptDebugInfo, !IO),
+            ProcLabel, !OptDebugInfo),
         Statistics = Info ^ lopt_detailed_statistics,
-        maybe_report_stats(Statistics, !IO),
+        trace [io(!IO)] (
+            maybe_report_stats(Statistics, !IO)
+        ),
 
         OptFullJump = Info ^ lopt_opt_fulljumps,
         PessimizeTailCalls = Info ^ lopt_pes_tailcalls,
@@ -487,9 +523,11 @@
         ->
             (
                 VeryVerbose = yes,
+                trace [io(!IO)] (
                 io.write_string("% Optimizing jumps for ", !IO),
                 io.write_string(LabelStr, !IO),
                 io.write_string("\n", !IO)
+                )
             ;
                 VeryVerbose = no
             ),
@@ -497,7 +535,7 @@
                 OptFullJump, Final, PessimizeTailCalls, CheckedNondetTailCalls,
                 !C, !Instrs, _Mod2),
             maybe_opt_debug(Info, !.Instrs, !.C, "jump", "after jumps",
-                ProcLabel, !OptDebugInfo, !IO)
+                ProcLabel, !OptDebugInfo)
         ;
             true
         ),
@@ -505,15 +543,17 @@
             Mod1 = yes,
             (
                 VeryVerbose = yes,
+                trace [io(!IO)] (
                 io.write_string("% Optimizing labels for ", !IO),
                 io.write_string(LabelStr, !IO),
                 io.write_string("\n", !IO)
+                )
             ;
                 VeryVerbose = no
             ),
             labelopt_main(Final, LayoutLabelSet, !Instrs, _Mod3),
             maybe_opt_debug(Info, !.Instrs, !.C, "label", "after labels",
-                ProcLabel, !OptDebugInfo, !IO)
+                ProcLabel, !OptDebugInfo)
         ;
             Mod1 = no
         ),
@@ -521,16 +561,18 @@
             Mod1 = yes,
             (
                 VeryVerbose = yes,
+                trace [io(!IO)] (
                 io.write_string("% Optimizing locally for ", !IO),
                 io.write_string(LabelStr, !IO),
                 io.write_string("\n", !IO)
+                )
             ;
                 VeryVerbose = no
             ),
             GC_Method = Info ^ lopt_gc_method,
             peephole_optimize(GC_Method, !Instrs, _Mod),
             maybe_opt_debug(Info, !.Instrs, !.C, "peep", "after peephole",
-                ProcLabel, !OptDebugInfo, !IO)
+                ProcLabel, !OptDebugInfo)
         ;
             Mod1 = no
         )
@@ -542,9 +584,11 @@
         UseLocalVars = yes,
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Optimizing local vars for ", !IO),
             io.write_string(LabelStr, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
@@ -554,17 +598,16 @@
         use_local_vars_proc(!Instrs, NumRealRRegs, AccessThreshold,
             AutoComments, ProcLabel, !C),
         maybe_opt_debug(Info, !.Instrs, !.C, "use_local",
-            "after use_local_vars", ProcLabel, !OptDebugInfo, !IO)
+            "after use_local_vars", ProcLabel, !OptDebugInfo)
     ;
         UseLocalVars = no
     ).
 
 :- pred optimize_last(llds_opt_info::in, set(label)::in, proc_label::in,
     counter::in, counter::out, opt_debug_info::in,
-    list(instruction)::in, list(instruction)::out, io::di, io::uo) is det.
+    list(instruction)::in, list(instruction)::out) is det.
 
-optimize_last(Info, LayoutLabelSet, ProcLabel, !C, !.OptDebugInfo,
-        !Instrs, !IO) :-
+optimize_last(Info, LayoutLabelSet, ProcLabel, !C, !.OptDebugInfo, !Instrs) :-
     VeryVerbose = Info ^ lopt_very_verbose,
     LabelStr = opt_util.format_proc_label(ProcLabel),
 
@@ -583,15 +626,17 @@
         % since they can confuse reassign, wrap_blocks and delay_slot.
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Optimizing labels for ", !IO),
             io.write_string(LabelStr, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
         labelopt_main(no, LayoutLabelSet, !Instrs, _Mod1),
         maybe_opt_debug(Info, !.Instrs, !.C, "label", "after label opt",
-            ProcLabel, !OptDebugInfo, !IO)
+            ProcLabel, !OptDebugInfo)
     ;
         true
     ),
@@ -599,15 +644,17 @@
         Reassign = yes,
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Optimizing reassign for ", !IO),
             io.write_string(LabelStr, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
         remove_reassign(!Instrs),
         maybe_opt_debug(Info, !.Instrs, !.C, "reassign", "after reassign",
-            ProcLabel, !OptDebugInfo, !IO)
+            ProcLabel, !OptDebugInfo)
     ;
         Reassign = no
     ),
@@ -615,42 +662,48 @@
         DelaySlot = yes,
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Optimizing delay slot for ", !IO),
             io.write_string(LabelStr, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
         fill_branch_delay_slot(!Instrs),
         maybe_opt_debug(Info, !.Instrs, !.C, "delay_slot", "after delay slots",
-            ProcLabel, !OptDebugInfo, !IO)
+            ProcLabel, !OptDebugInfo)
     ;
         DelaySlot = no
     ),
     (
         VeryVerbose = yes,
+        trace [io(!IO)] (
         io.write_string("% Optimizing returns for ", !IO),
         io.write_string(LabelStr, !IO),
         io.write_string("\n", !IO)
+        )
     ;
         VeryVerbose = no
     ),
     combine_decr_sp(!Instrs),
     maybe_opt_debug(Info, !.Instrs, !.C, "decr_sp", "after combine decr_sp",
-        ProcLabel, !OptDebugInfo, !IO),
+        ProcLabel, !OptDebugInfo),
     (
         StdLabels = yes,
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Standardizing labels for ", !IO),
             io.write_string(LabelStr, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
         standardize_labels(!Instrs, !C),
         maybe_opt_debug(Info, !.Instrs, !.C, "stdlabel",
-            "after standard labels", ProcLabel, !OptDebugInfo, !IO)
+            "after standard labels", ProcLabel, !OptDebugInfo)
     ;
         StdLabels = no
     ),
@@ -658,15 +711,17 @@
         UseLocalVars = yes,
         (
             VeryVerbose = yes,
+            trace [io(!IO)] (
             io.write_string("% Wrapping blocks for ", !IO),
             io.write_string(LabelStr, !IO),
             io.write_string("\n", !IO)
+            )
         ;
             VeryVerbose = no
         ),
         wrap_blocks(!Instrs),
         maybe_opt_debug(Info, !.Instrs, !.C, "wrapblocks", "after wrap blocks",
-            ProcLabel, !.OptDebugInfo, _OptDebugInfo, !IO)
+            ProcLabel, !.OptDebugInfo, _OptDebugInfo)
     ;
         UseLocalVars = no
     ).
Index: compiler/passes_aux.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/passes_aux.m,v
retrieving revision 1.96
diff -u -b -r1.96 passes_aux.m
--- compiler/passes_aux.m	30 Jul 2010 05:16:14 -0000	1.96
+++ compiler/passes_aux.m	5 Aug 2010 02:49:19 -0000
@@ -30,101 +30,128 @@
 
 %-----------------------------------------------------------------------------%
 
-:- type task
-    --->    update_proc(pred(module_info, proc_info, proc_info))
-    ;       update_proc_predid(pred(pred_id, module_info, proc_info,
-                proc_info))
-    ;       update_proc_predprocid(pred(pred_id, proc_id, module_info,
-                pred_info, proc_info, proc_info))
-    ;       update_proc_io(pred(pred_id, proc_id, module_info,
-                proc_info, proc_info, io, io))
-    ;       update_proc_error(pred(pred_id, proc_id, module_info, module_info,
-                proc_info, proc_info, int, int, io, io))
-    ;       update_pred_error(pred_error_task)
-    ;       update_module(pred(pred_id, proc_id, pred_info,
-                proc_info, proc_info, module_info, module_info))
-    ;       update_module_io(pred(pred_id, proc_id, proc_info, proc_info,
-                module_info, module_info, io, io))
-            % It would be better to use an existentially-quantified type
-            % rather than `univ' here, but the current version of Mercury
-            % doesn't support existentially-quantified types.
-    ;       update_module_cookie(pred(pred_id, proc_id, proc_info, proc_info,
-                univ, univ, module_info, module_info), univ).
+:- type pred_error_task ==
+    pred(pred_id, module_info, module_info, pred_info, pred_info,
+        list(error_spec), list(error_spec)).
+:- inst pred_error_task ==
+    (pred(in, in, out, in, out, in, out) is det).
+
+:- type update_pred_task
+    --->    update_pred_error(pred_error_task).
+:- inst update_pred_task ==
+    bound(
+            update_pred_error(pred_error_task)
+    ).
+:- mode update_pred_task == update_pred_task >> update_pred_task.
+
+:- pred process_all_nonimported_preds_errors(
+    update_pred_task::update_pred_task, module_info::in, module_info::out,
+    list(error_spec)::in, list(error_spec)::out, io::di, io::uo) is det.
+
+%-----------------------------------------------------------------------------%
 
 % Note that update_module_cookie causes some difficulties.
 % Ideally, it should be implemented using existential types:
 %
-%   :- type task
+%   :- type update_proc_task
 %   --->
 %           ...
-%   ;       some [T] update_module_cookie(pred(pred_id, proc_id,
-%               proc_info, proc_info, T, T, module_info, module_info), T)
+%   ;       some [T] update_module_cookie(pred(pred_proc_id,
+%               proc_info, proc_info, module_info, module_info, T, T), T)
 %
 % That would avoid the need for messing about with type_to_univ and
 % univ_to_type.
 %
-% Originally, it was implemented by changing `task' to `task(T)':
+% Originally, it was implemented by changing `update_proc_task' to
+% `update_proc_task(T)':
 %
-%   :- type task(T)
+%   :- type update_proc_task(T)
 %   --->
 %           ...
-%   ;       update_module_cookie(pred(pred_id, proc_id, proc_info, proc_info,
-%               T, T, module_info, module_info), T)
+%   ;       update_module_cookie(pred(pred_proc_id, proc_info, proc_info,
+%               module_info, module_info, T, T), T)
 %
 % but that is not a good solution, because it causes a lot of warnings
 % about unbound type variables.
 
-:- inst task ==
-    bound(( update_proc(pred(in, in, out) is det)
-        ;   update_proc_predid(pred(in, in, in, out) is det)
-        ;   update_proc_predprocid(pred(in, in, in, in, in, out) is det)
-        ;   update_proc_io(pred(in, in, in, in, out, di, uo) is det)
-        ;   update_proc_error(pred(in, in, in, out, in, out, out, out, di, uo)
-                is det)
-        ;   update_pred_error(pred(in, in, out, in, out, in, out) is det)
-        ;   update_module(pred(in, in, in, in, out, in, out) is det)
-        ;   update_module_io(pred(in, in, in, out, in, out, di, uo) is det)
-        ;   update_module_cookie(pred(in, in, in, out, in, out, in, out)
-                is det, ground)
-        )).
+:- type proc_task ==
+    pred(module_info, proc_info, proc_info).
+:- inst proc_task ==
+    (pred(in, in, out) is det).
+
+:- type proc_ids_task ==
+    pred(module_info, pred_proc_id, proc_info, proc_info).
+:- inst proc_ids_task ==
+    (pred(in, in, in, out) is det).
+
+:- type proc_ids_pred_task ==
+    pred(module_info, pred_proc_id, pred_info, proc_info, proc_info).
+:- inst proc_ids_pred_task ==
+    (pred(in, in, in, in, out) is det).
+
+:- type proc_ids_io_task ==
+    pred(module_info, pred_proc_id, proc_info, proc_info, io, io).
+:- inst proc_ids_io_task ==
+    (pred(in, in, in, out, di, uo) is det).
+
+:- type module_task ==
+    pred(pred_proc_id, proc_info, proc_info,
+        module_info, module_info).
+:- inst module_task ==
+    (pred(in, in, out, in, out) is det).
+
+:- type module_pred_task ==
+    pred(pred_proc_id, pred_info, proc_info, proc_info,
+        module_info, module_info).
+:- inst module_pred_task ==
+    (pred(in, in, in, out, in, out) is det).
+
+:- type module_io_task ==
+    pred(pred_proc_id, proc_info, proc_info,
+        module_info, module_info, io, io).
+:- inst module_io_task ==
+    (pred(in, in, out, in, out, di, uo) is det).
+
+:- type module_cookie_task ==
+    pred(pred_proc_id, proc_info, proc_info,
+        module_info, module_info, univ, univ).
+:- inst module_cookie_task ==
+    (pred(in, in, out, in, out, in, out) is det).
 
-:- mode task == task >> task.
+%-----------------------------------------------------------------------------%
 
-:- type pred_error_task ==
-        pred(pred_id, module_info, module_info, pred_info, pred_info,
-            list(error_spec), list(error_spec)).
-:- inst pred_error_task ==
-    (pred(in, in, out, in, out, in, out) is det).
+:- type update_proc_task
+    --->    update_proc(proc_task)
+    ;       update_proc_ids(proc_ids_task)
+    ;       update_proc_ids_pred(proc_ids_pred_task)
+    ;       update_proc_ids_io(proc_ids_io_task)
+    ;       update_module(module_task)
+    ;       update_module_pred(module_pred_task)
+    ;       update_module_io(module_io_task)
+    ;       update_module_cookie(module_cookie_task, univ).
+:- inst update_proc_task ==
+    bound(
+            update_proc(proc_task)
+        ;   update_proc_ids(proc_ids_task)
+        ;   update_proc_ids_pred(proc_ids_pred_task)
+        ;   update_proc_ids_io(proc_ids_io_task)
+        ;   update_module(module_task)
+        ;   update_module_pred(module_pred_task)
+        ;   update_module_io(module_io_task)
+        ;   update_module_cookie(module_cookie_task, ground)
+    ).
+:- mode update_proc_task == update_proc_task >> update_proc_task.
 
-:- pred process_all_nonimported_procs_errors(task::task,
-    module_info::in, module_info::out,
-    list(error_spec)::in, list(error_spec)::out, io::di, io::uo) is det.
-:- pred process_all_nonimported_procs(task::task,
-    module_info::in, module_info::out, io::di, io::uo) is det.
+%-----------------------------------------------------------------------------%
 
-:- pred process_all_nonimported_procs_update_errors(
-    task::task, task::out(task), module_info::in, module_info::out,
-    list(error_spec)::in, list(error_spec)::out, io::di, io::uo) is det.
-:- pred process_all_nonimported_procs_update(task::task, task::out(task),
+:- pred process_all_nonimported_procs(update_proc_task::update_proc_task,
     module_info::in, module_info::out, io::di, io::uo) is det.
 
-    % Process procedures for which a given test succeeds.
-    %
-:- pred process_matching_nonimported_procs_errors(task::task,
-    pred(pred_info)::in(pred(in) is semidet),
-    module_info::in, module_info::out,
-    list(error_spec)::in, list(error_spec)::out, io::di, io::uo) is det.
-:- pred process_matching_nonimported_procs(task::task,
-    pred(pred_info)::in(pred(in) is semidet),
+:- pred process_all_nonimported_procs_update(
+    update_proc_task::update_proc_task, update_proc_task::out(update_proc_task),
     module_info::in, module_info::out, io::di, io::uo) is det.
 
-:- pred process_matching_nonimported_procs_update_errors(
-    task::task, task::out(task), pred(pred_info)::in(pred(in) is semidet),
-    module_info::in, module_info::out,
-    list(error_spec)::in, list(error_spec)::out, io::di, io::uo) is det.
-:- pred process_matching_nonimported_procs_update(
-    task::task, task::out(task), pred(pred_info)::in(pred(in) is semidet),
-    module_info::in, module_info::out, io::di, io::uo) is det.
+%-----------------------------------------------------------------------------%
 
 :- pred write_pred_progress_message(string::in, pred_id::in, module_info::in,
     io::di, io::uo) is det.
@@ -197,148 +224,184 @@
 :- import_module parse_tree.prog_out.
 :- import_module parse_tree.prog_util.
 
+:- import_module assoc_list.
 :- import_module benchmarking.
 :- import_module bool.
 :- import_module int.
 :- import_module map.
+:- import_module pair.
+:- import_module set_tree234.
 :- import_module string.
 :- import_module varset.
 
 %-----------------------------------------------------------------------------%
 
-process_all_nonimported_procs_errors(Task, !ModuleInfo, !Specs, !IO) :-
-    True = (pred(_PredInfo::in) is semidet :- true),
-    process_matching_nonimported_procs_errors(Task, True, !ModuleInfo,
-        !Specs, !IO).
+process_all_nonimported_preds_errors(Task, !ModuleInfo, !Specs, !IO) :-
+    module_info_get_valid_predids(PredIds, !ModuleInfo),
+    list.foldl2(process_nonimported_pred(Task), PredIds, !ModuleInfo, !Specs).
+
+:- pred process_nonimported_pred(update_pred_task::in(update_pred_task),
+    pred_id::in, module_info::in, module_info::out,
+    list(error_spec)::in, list(error_spec)::out) is det.
+
+process_nonimported_pred(Task, PredId, !ModuleInfo, !Specs) :-
+    module_info_pred_info(!.ModuleInfo, PredId, PredInfo0),
+    ( pred_info_is_imported(PredInfo0) ->
+        true
+    ;
+        (
+            Task = update_pred_error(Closure),
+            Closure(PredId, !ModuleInfo, PredInfo0, PredInfo, !Specs)
+        ),
+        module_info_set_pred_info(PredId, PredInfo, !ModuleInfo)
+    ).
+
+%-----------------------------------------------------------------------------%
+
+:- inst par_proc_task ==
+    bound(
+            update_proc(proc_task)
+        ;   update_proc_ids(proc_ids_task)
+        ;   update_proc_ids_pred(proc_ids_pred_task)
+    ).
+:- mode par_proc_task == par_proc_task >> par_proc_task.
+
+:- inst seq_proc_task ==
+    bound(
+            update_proc_ids_io(proc_ids_io_task)
+        ;   update_module(module_task)
+        ;   update_module_pred(module_pred_task)
+        ;   update_module_io(module_io_task)
+        ;   update_module_cookie(module_cookie_task, ground)
+    ).
+:- mode seq_proc_task == seq_proc_task >> seq_proc_task.
 
 process_all_nonimported_procs(Task, !ModuleInfo, !IO) :-
-    process_all_nonimported_procs_errors(Task, !ModuleInfo, [], Specs, !IO),
-    expect(unify(Specs, []), this_file,
-        "process_all_nonimported_procs: Specs").
-
-process_all_nonimported_procs_update_errors(!Task, !ModuleInfo, !Specs, !IO) :-
-    True = (pred(_PredInfo::in) is semidet :- true),
-    process_matching_nonimported_procs_update_errors(!Task, True, !ModuleInfo,
-        !Specs, !IO).
+    process_all_nonimported_procs_update(Task, _, !ModuleInfo, !IO).
 
 process_all_nonimported_procs_update(!Task, !ModuleInfo, !IO) :-
-    process_all_nonimported_procs_update_errors(!Task, !ModuleInfo,
-        [], Specs, !IO),
-    expect(unify(Specs, []), this_file,
-        "process_all_nonimported_procs_update: Specs").
-
-process_matching_nonimported_procs_errors(Task, Filter, !ModuleInfo,
-        !Specs, !IO) :-
-    module_info_get_valid_predids(PredIds, !ModuleInfo),
-    ( Task = update_pred_error(Pred) ->
-        list.foldl2(process_nonimported_pred(Pred, Filter), PredIds,
-            !ModuleInfo, !Specs)
-    ;
-        process_nonimported_procs_in_preds(PredIds, Task, _, Filter,
+    module_info_get_valid_predids(ValidPredIds, !ModuleInfo),
+    (
+        ( !.Task = update_proc(_)
+        ; !.Task = update_proc_ids(_)
+        ; !.Task = update_proc_ids_pred(_)
+        ),
+        ValidPredIdSet = set_tree234.list_to_set(ValidPredIds),
+        module_info_get_preds(!.ModuleInfo, PredMap0),
+        map.to_assoc_list(PredMap0, PredIdsInfos0),
+        par_process_nonimported_procs_in_preds(!.ModuleInfo, !.Task,
+            ValidPredIdSet, PredIdsInfos0, PredIdsInfos),
+        map.from_sorted_assoc_list(PredIdsInfos, PredMap),
+        module_info_set_preds(PredMap, !ModuleInfo)
+    ;
+        ( !.Task = update_proc_ids_io(_)
+        ; !.Task = update_module(_)
+        ; !.Task = update_module_pred(_)
+        ; !.Task = update_module_io(_)
+        ; !.Task = update_module_cookie(_, _)
+        ),
+        seq_process_nonimported_procs_in_preds(ValidPredIds, !Task,
             !ModuleInfo, !IO)
     ).
 
-process_matching_nonimported_procs(Task, Filter, !ModuleInfo, !IO) :-
-    process_matching_nonimported_procs_errors(Task, Filter, !ModuleInfo,
-        [], Specs, !IO),
-    expect(unify(Specs, []), this_file,
-        "process_matching_nonimported_procs: Specs").
+%-----------------------------------------------------------------------------%
 
-process_matching_nonimported_procs_update_errors(Task0, Task, Filter,
-        !ModuleInfo, !Specs, !IO) :-
-    module_info_get_valid_predids(PredIds, !ModuleInfo),
-    process_nonimported_procs_in_preds(PredIds, Task0, Task, Filter,
-        !ModuleInfo, !IO).
+:- pred par_process_nonimported_procs_in_preds(module_info::in,
+    update_proc_task::par_proc_task, set_tree234(pred_id)::in,
+    assoc_list(pred_id, pred_info)::in, assoc_list(pred_id, pred_info)::out)
+    is det.
+
+par_process_nonimported_procs_in_preds(_, _, _, [], []).
+par_process_nonimported_procs_in_preds(ModuleInfo, Task, ValidPredIdSet,
+        [PredIdInfo0 | PredIdsInfos0], [PredIdInfo | PredIdsInfos]) :-
+    PredIdInfo0 = PredId - PredInfo0,
+    ( set_tree234.member(ValidPredIdSet, PredId) ->
+        ProcIds = pred_info_non_imported_procids(PredInfo0),
+        par_process_nonimported_procs(ModuleInfo, Task, PredId, ProcIds,
+            PredInfo0, PredInfo),
+        PredIdInfo = PredId - PredInfo
+    ;
+        PredIdInfo = PredIdInfo0
+    ),
+    par_process_nonimported_procs_in_preds(ModuleInfo, Task, ValidPredIdSet,
+        PredIdsInfos0, PredIdsInfos).
 
-process_matching_nonimported_procs_update(Task0, Task, Filter,
-        !ModuleInfo, !IO) :-
-    process_matching_nonimported_procs_update_errors(Task0, Task, Filter,
-        !ModuleInfo, [], Specs, !IO),
-    expect(unify(Specs, []), this_file,
-        "process_matching_nonimported_procs_update_errors: Specs").
-
-:- pred process_nonimported_pred(pred_error_task::in(pred_error_task),
-    pred(pred_info)::in(pred(in) is semidet), pred_id::in,
-    module_info::in, module_info::out,
-    list(error_spec)::in, list(error_spec)::out) is det.
+:- pred par_process_nonimported_procs(module_info::in,
+    update_proc_task::par_proc_task, pred_id::in, list(proc_id)::in,
+    pred_info::in, pred_info::out) is det.
 
-process_nonimported_pred(Task, Filter, PredId, !ModuleInfo, !Specs) :-
-    module_info_pred_info(!.ModuleInfo, PredId, PredInfo0),
+par_process_nonimported_procs(_, _, _, [], !PredInfo).
+par_process_nonimported_procs(ModuleInfo, Task, PredId, [ProcId | ProcIds],
+        !PredInfo) :-
+    pred_info_get_procedures(!.PredInfo, ProcMap0),
+    map.lookup(ProcMap0, ProcId, Proc0),
+
+    PredProcId = proc(PredId, ProcId),
     (
-        ( pred_info_is_imported(PredInfo0)
-        ; \+ call(Filter, PredInfo0)
-        )
-    ->
-        true
+        Task = update_proc(Closure),
+        Closure(ModuleInfo, Proc0, Proc)
     ;
-        Task(PredId, !ModuleInfo, PredInfo0, PredInfo, !Specs),
-        module_info_set_pred_info(PredId, PredInfo, !ModuleInfo)
-    ).
+        Task = update_proc_ids(Closure),
+        Closure(ModuleInfo, PredProcId, Proc0, Proc)
+    ;
+        Task = update_proc_ids_pred(Closure),
+        Closure(ModuleInfo, PredProcId, !.PredInfo, Proc0, Proc)
+    ),
 
-:- pred process_nonimported_procs_in_preds(list(pred_id)::in,
-    task::task, task::out(task), pred(pred_info)::in(pred(in) is semidet),
+    map.det_update(ProcMap0, ProcId, Proc, ProcMap),
+    pred_info_set_procedures(ProcMap, !PredInfo),
+
+    par_process_nonimported_procs(ModuleInfo, Task, PredId, ProcIds,
+        !PredInfo).
+
+%-----------------------------------------------------------------------------%
+
+:- pred seq_process_nonimported_procs_in_preds(list(pred_id)::in,
+    update_proc_task::seq_proc_task, update_proc_task::out(seq_proc_task),
     module_info::in, module_info::out, io::di, io::uo) is det.
 
-process_nonimported_procs_in_preds([], !Task, _, !ModuleInfo, !IO).
-process_nonimported_procs_in_preds([PredId | PredIds], !Task, Filter,
+seq_process_nonimported_procs_in_preds([], !Task, !ModuleInfo, !IO).
+seq_process_nonimported_procs_in_preds([PredId | PredIds], !Task,
         !ModuleInfo, !IO) :-
     module_info_get_preds(!.ModuleInfo, PredTable),
     map.lookup(PredTable, PredId, PredInfo),
-    ( call(Filter, PredInfo) ->
         ProcIds = pred_info_non_imported_procids(PredInfo),
-        process_nonimported_procs(ProcIds, PredId, !Task, !ModuleInfo, !IO)
-    ;
-        true
-    ),
-    process_nonimported_procs_in_preds(PredIds, !Task, Filter,
-        !ModuleInfo, !IO).
+    seq_process_nonimported_procs(PredId, ProcIds, !Task, !ModuleInfo, !IO),
+    seq_process_nonimported_procs_in_preds(PredIds, !Task, !ModuleInfo, !IO).
 
-:- pred process_nonimported_procs(list(proc_id)::in, pred_id::in,
-    task::task, task::out(task),
+:- pred seq_process_nonimported_procs(pred_id::in, list(proc_id)::in,
+    update_proc_task::seq_proc_task, update_proc_task::out(seq_proc_task),
     module_info::in, module_info::out, io::di, io::uo) is det.
 
-process_nonimported_procs([], _PredId, !Task, !ModuleInfo, !IO).
-process_nonimported_procs([ProcId | ProcIds], PredId, !Task, !ModuleInfo,
+seq_process_nonimported_procs(_PredId, [], !Task, !ModuleInfo, !IO).
+seq_process_nonimported_procs(PredId, [ProcId | ProcIds], !Task, !ModuleInfo,
         !IO) :-
     module_info_get_preds(!.ModuleInfo, Preds0),
     map.lookup(Preds0, PredId, Pred0),
     pred_info_get_procedures(Pred0, Procs0),
     map.lookup(Procs0, ProcId, Proc0),
 
+    PredProcId = proc(PredId, ProcId),
     (
-        !.Task = update_module(Closure),
-        Closure(PredId, ProcId, Pred0, Proc0, Proc, !ModuleInfo)
-    ;
-        !.Task = update_module_io(Closure),
-        Closure(PredId, ProcId, Proc0, Proc, !ModuleInfo, !IO)
-    ;
-        !.Task = update_proc(Closure),
-        Closure(!.ModuleInfo, Proc0, Proc)
+        !.Task = update_proc_ids_io(Closure),
+        Closure(!.ModuleInfo, PredProcId, Proc0, Proc, !IO)
     ;
-        !.Task = update_proc_predid(Closure),
-        Closure(PredId, !.ModuleInfo, Proc0, Proc)
-    ;
-        !.Task = update_proc_predprocid(Closure),
-        Closure(PredId, ProcId, !.ModuleInfo, Pred0, Proc0, Proc)
-    ;
-        !.Task = update_proc_io(Closure),
-        Closure(PredId, ProcId, !.ModuleInfo, Proc0, Proc, !IO)
+        !.Task = update_module(Closure),
+        Closure(PredProcId, Proc0, Proc, !ModuleInfo)
     ;
-        !.Task = update_proc_error(Closure),
-        Closure(PredId, ProcId, !ModuleInfo, Proc0, Proc, WarnCnt, ErrCnt,
-            !IO),
-        passes_aux.handle_errors(WarnCnt, ErrCnt, !ModuleInfo, !IO)
+        !.Task = update_module_pred(Closure),
+        Closure(PredProcId, Pred0, Proc0, Proc, !ModuleInfo)
     ;
-        !.Task = update_pred_error(_),
-        unexpected(this_file, "process_non_imported_procs")
+        !.Task = update_module_io(Closure),
+        Closure(PredProcId, Proc0, Proc, !ModuleInfo, !IO)
     ;
         !.Task = update_module_cookie(Closure, Cookie0),
-        Closure(PredId, ProcId, Proc0, Proc, Cookie0, Cookie1, !ModuleInfo),
-        !:Task = update_module_cookie(Closure, Cookie1)
+        Closure(PredProcId, Proc0, Proc, !ModuleInfo, Cookie0, Cookie),
+        !:Task = update_module_cookie(Closure, Cookie)
     ),
 
     % If the pass changed the module_info, it may have changed the pred table
-    % or the proc table for this pred_id.  Don't take any chances.
+    % or the proc table for this pred_id. Do not take any chances.
 
     module_info_get_preds(!.ModuleInfo, Preds8),
     map.lookup(Preds8, PredId, Pred8),
@@ -349,7 +412,9 @@
     map.det_update(Preds8, PredId, Pred, Preds),
     module_info_set_preds(Preds, !ModuleInfo),
 
-    process_nonimported_procs(ProcIds, PredId, !Task, !ModuleInfo, !IO).
+    seq_process_nonimported_procs(PredId, ProcIds, !Task, !ModuleInfo, !IO).
+
+%-----------------------------------------------------------------------------%
 
 write_pred_progress_message(Message, PredId, ModuleInfo, !IO) :-
     module_info_get_globals(ModuleInfo, Globals),
@@ -378,25 +443,7 @@
         VeryVerbose = no
     ).
 
-:- pred handle_errors(int::in, int::in, module_info::in, module_info::out,
-    io::di, io::uo) is det.
-
-handle_errors(WarnCnt, ErrCnt, !ModuleInfo, !IO) :-
-    module_info_get_globals(!.ModuleInfo, Globals),
-    globals.lookup_bool_option(Globals, halt_at_warn, HaltAtWarn),
-    (
-        (
-            ErrCnt > 0
-        ;
-            WarnCnt > 0,
-            HaltAtWarn = yes
-        )
-    ->
-        io.set_exit_status(1, !IO),
-        module_info_incr_errors(!ModuleInfo)
-    ;
-        true
-    ).
+%-----------------------------------------------------------------------------%
 
 maybe_report_sizes(HLDS, !IO) :-
     module_info_get_globals(HLDS, Globals),
Index: compiler/pd_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/pd_info.m,v
retrieving revision 1.40
diff -u -b -r1.40 pd_info.m
--- compiler/pd_info.m	10 Jun 2010 00:58:17 -0000	1.40
+++ compiler/pd_info.m	5 Aug 2010 02:49:19 -0000
@@ -25,7 +25,6 @@
 
 :- import_module bool.
 :- import_module counter.
-:- import_module io.
 :- import_module list.
 :- import_module map.
 :- import_module maybe.
@@ -129,6 +128,7 @@
 
 :- import_module bool.
 :- import_module int.
+:- import_module io.
 :- import_module term.
 
 %-----------------------------------------------------------------------------%
@@ -389,14 +389,14 @@
 
 :- interface.
 
-    % Find the deforestation procedure which most closely
-    % matches the given goal.
+    % Find the deforestation procedure which most closely matches
+    % the given goal.
     %
-:- pred pd_info.search_version(pd_info::in, hlds_goal::in, maybe_version::out,
-    io::di, io::uo) is det.
+:- pred pd_info.search_version(pd_info::in, hlds_goal::in, maybe_version::out)
+    is det.
 
-    % Create a new predicate for the input goal, returning a
-    % goal which calls the new predicate.
+    % Create a new predicate for the input goal, returning a goal
+    % which calls the new predicate.
     %
 :- pred pd_info.define_new_pred(pred_origin::in, hlds_goal::in,
     pred_proc_id::out, hlds_goal::out, pd_info::in, pd_info::out) is det.
@@ -404,7 +404,7 @@
     % Add a version to the table.
     %
 :- pred pd_info.register_version(pred_proc_id::in, version_info::in,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
     % Remove a version and make sure it is never recreated.
     %
@@ -461,8 +461,10 @@
 
 :- implementation.
 
-pd_info.search_version(PDInfo, Goal, MaybeVersion, !IO) :-
-    pd_debug_output_goal(PDInfo, "Searching for version:\n", Goal, !IO),
+pd_info.search_version(PDInfo, Goal, MaybeVersion) :-
+    trace [io(!IO)] (
+        pd_debug_output_goal(PDInfo, "Searching for version:\n", Goal, !IO)
+    ),
     pd_util.goal_get_calls(Goal, CalledPreds),
     pd_info_get_versions(PDInfo, Versions),
     pd_info_get_goal_version_index(PDInfo, GoalVersionIndex),
@@ -479,7 +481,9 @@
     ;
         MaybeVersion = no_version
     ),
-    pd_debug_search_version_result(PDInfo, MaybeVersion, !IO).
+    trace [io(!IO)] (
+        pd_debug_search_version_result(PDInfo, MaybeVersion, !IO)
+    ).
 
 %-----------------------------------------------------------------------------%
 
@@ -654,8 +658,10 @@
 
 %-----------------------------------------------------------------------------%
 
-pd_info.register_version(PredProcId, Version, !PDInfo, !IO) :-
-    pd_debug_register_version(!.PDInfo, PredProcId, Version, !IO),
+pd_info.register_version(PredProcId, Version, !PDInfo) :-
+    trace [io(!IO)] (
+        pd_debug_register_version(!.PDInfo, PredProcId, Version, !IO)
+    ),
     pd_info_get_goal_version_index(!.PDInfo, GoalVersionIndex0),
     Goal = Version ^ version_orig_goal,
     pd_util.goal_get_calls(Goal, Calls),
Index: compiler/pd_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/pd_util.m,v
retrieving revision 1.75
diff -u -b -r1.75 pd_util.m
--- compiler/pd_util.m	14 Oct 2009 05:28:42 -0000	1.75
+++ compiler/pd_util.m	5 Aug 2010 02:49:19 -0000
@@ -25,7 +25,6 @@
 :- import_module transform_hlds.pd_info.
 
 :- import_module bool.
-:- import_module io.
 :- import_module list.
 :- import_module map.
 :- import_module maybe.
@@ -41,7 +40,7 @@
     % can fail are executed as early as possible.
     %
 :- pred propagate_constraints(hlds_goal::in, hlds_goal::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
     % Apply simplify.m to the goal.
     %
@@ -51,21 +50,20 @@
     % Apply unique_modes.m to the goal.
     %
 :- pred unique_modecheck_goal(hlds_goal::in, hlds_goal::out,
-    list(mode_error_info)::out, pd_info::in, pd_info::out,
-    io::di, io::uo) is det.
+    list(mode_error_info)::out, pd_info::in, pd_info::out) is det.
 
     % Apply unique_modes.m to the goal.
     %
-:- pred unique_modecheck_goal(set(prog_var)::in,
+:- pred unique_modecheck_goal_live_vars(set(prog_var)::in,
     hlds_goal::in, hlds_goal::out, list(mode_error_info)::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
     % Find out which arguments of the procedure are interesting
     % for deforestation.
     %
 :- pred get_branch_vars_proc(pred_proc_id::in, proc_info::in,
-    pd_arg_info::in, pd_arg_info::out,
-    module_info::in, module_info::out) is det.
+    module_info::in, module_info::out, pd_arg_info::in, pd_arg_info::out)
+    is det.
 
     % Find out which variables of the goal are interesting
     % for deforestation.
@@ -176,6 +174,7 @@
 
 :- import_module assoc_list.
 :- import_module int.
+:- import_module io.
 :- import_module pair.
 :- import_module set.
 :- import_module term.
@@ -192,7 +191,7 @@
 
 %-----------------------------------------------------------------------------%
 
-propagate_constraints(!Goal, !PDInfo, !IO) :-
+propagate_constraints(!Goal, !PDInfo) :-
     pd_info_get_module_info(!.PDInfo, ModuleInfo0),
     module_info_get_globals(ModuleInfo0, Globals),
     globals.lookup_bool_option(Globals, local_constraint_propagation,
@@ -201,8 +200,10 @@
     (
         ConstraintProp = yes,
         Goal0 = !.Goal,
+        trace [io(!IO)] (
         pd_debug_message(DebugPD, "%% Propagating constraints\n", [], !IO),
-        pd_debug_output_goal(!.PDInfo, "before constraints\n", Goal0, !IO),
+            pd_debug_output_goal(!.PDInfo, "before constraints\n", Goal0, !IO)
+        ),
         pd_info_get_proc_info(!.PDInfo, ProcInfo0),
         pd_info_get_instmap(!.PDInfo, InstMap),
         proc_info_get_vartypes(ProcInfo0, VarTypes0),
@@ -219,11 +220,13 @@
         pd_info_set_proc_info(ProcInfo, !PDInfo),
         (
             Changed = yes,
+            trace [io(!IO)] (
             pd_debug_output_goal(!.PDInfo,
-                "after constraints, before recompute\n", !.Goal, !IO),
+                    "after constraints, before recompute\n", !.Goal, !IO)
+            ),
             pd_requantify_goal(NonLocals, !Goal, !PDInfo),
             pd_recompute_instmap_delta(!Goal, !PDInfo),
-            rerun_det_analysis(!Goal, !PDInfo, !IO),
+            rerun_det_analysis(!Goal, !PDInfo),
             simplify.find_simplifications(no, Globals, Simplifications),
             pd_simplify_goal(Simplifications, !Goal, !PDInfo)
         ;
@@ -271,12 +274,11 @@
 
 %-----------------------------------------------------------------------------%
 
-unique_modecheck_goal(Goal0, Goal, Errors, !PDInfo, !IO) :-
+unique_modecheck_goal(Goal0, Goal, Errors, !PDInfo) :-
     get_goal_live_vars(!.PDInfo, Goal0, LiveVars),
-    unique_modecheck_goal(LiveVars, Goal0, Goal, Errors,
-        !PDInfo, !IO).
+    unique_modecheck_goal_live_vars(LiveVars, Goal0, Goal, Errors, !PDInfo).
 
-unique_modecheck_goal(LiveVars, Goal0, Goal, Errors, !PDInfo, !IO) :-
+unique_modecheck_goal_live_vars(LiveVars, Goal0, Goal, Errors, !PDInfo) :-
     % Construct a mode_info.
     pd_info_get_pred_proc_id(!.PDInfo, PredProcId),
     PredProcId = proc(PredId, ProcId),
@@ -301,9 +303,11 @@
     mode_info_get_errors(ModeInfo, Errors),
     (
         Debug = yes,
+        trace [io(!IO)] (
         ErrorSpecs = list.map(mode_error_info_to_spec(ModeInfo), Errors),
-        write_error_specs(ErrorSpecs, Globals, 0, _NumWarnings, 0, _NumErrors,
-            !IO)
+            write_error_specs(ErrorSpecs, Globals,
+                0, _NumWarnings, 0, _NumErrors, !IO)
+        )
     ;
         Debug = no
     ),
@@ -357,9 +361,9 @@
 %-----------------------------------------------------------------------------%
 
 :- pred rerun_det_analysis(hlds_goal::in, hlds_goal::out,
-    pd_info::in, pd_info::out, io::di, io::uo) is det.
+    pd_info::in, pd_info::out) is det.
 
-rerun_det_analysis(Goal0, Goal, !PDInfo, !IO) :-
+rerun_det_analysis(Goal0, Goal, !PDInfo) :-
     Goal0 = hlds_goal(_, GoalInfo0),
 
     Detism = goal_info_get_determinism(GoalInfo0),
@@ -387,8 +391,8 @@
     % Make sure there were no errors.
     module_info_get_globals(ModuleInfo, Globals),
     disable_det_warnings(_OptionsToRestore, Globals, GlobalsToUse),
-    write_error_specs(Specs, GlobalsToUse, 0, _NumWarnings, 0, NumErrors, !IO),
-    expect(unify(NumErrors, 0), this_file,
+    ContainsErrors = contains_errors(GlobalsToUse, Specs),
+    expect(unify(ContainsErrors, no), this_file,
         "rerun_det_analysis: determinism errors").
 
 %-----------------------------------------------------------------------------%
@@ -422,7 +426,7 @@
 
 :- type pd_var_info     ==  branch_info_map(prog_var).
 
-get_branch_vars_proc(PredProcId, ProcInfo, !ArgInfo, !ModuleInfo) :-
+get_branch_vars_proc(PredProcId, ProcInfo, !ModuleInfo, !ArgInfo) :-
     proc_info_get_goal(ProcInfo, Goal),
     proc_info_get_vartypes(ProcInfo, VarTypes),
     instmap.init_reachable(InstMap0),
Index: compiler/saved_vars.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/saved_vars.m,v
retrieving revision 1.84
diff -u -b -r1.84 saved_vars.m
--- compiler/saved_vars.m	6 Nov 2009 02:22:02 -0000	1.84
+++ compiler/saved_vars.m	5 Aug 2010 02:49:19 -0000
@@ -30,13 +30,10 @@
 :- import_module hlds.hlds_module.
 :- import_module hlds.hlds_pred.
 
-:- import_module io.
-
 %-----------------------------------------------------------------------------%
 
-:- pred saved_vars_proc(pred_id::in, proc_id::in,
-    proc_info::in, proc_info::out, module_info::in, module_info::out,
-    io::di, io::uo) is det.
+:- pred saved_vars_proc(pred_proc_id::in,
+    proc_info::in, proc_info::out, module_info::in, module_info::out) is det.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -47,6 +44,9 @@
 :- import_module check_hlds.polymorphism.
 :- import_module hlds.goal_util.
 :- import_module hlds.hlds_goal.
+:- import_module hlds.hlds_out.
+:- import_module hlds.hlds_out.hlds_out_goal.
+:- import_module hlds.hlds_out.hlds_out_util.
 :- import_module hlds.hlds_rtti.
 :- import_module hlds.passes_aux.
 :- import_module hlds.quantification.
@@ -54,6 +54,7 @@
 :- import_module parse_tree.prog_data.
 
 :- import_module bool.
+:- import_module io.
 :- import_module list.
 :- import_module map.
 :- import_module pair.
@@ -63,18 +64,16 @@
 
 %-----------------------------------------------------------------------------%
 
-saved_vars_proc(PredId, ProcId, ProcInfo0, ProcInfo, !ModuleInfo, !IO) :-
+saved_vars_proc(proc(PredId, ProcId), !ProcInfo, !ModuleInfo) :-
+    trace [io(!IO)] (
     write_proc_progress_message("% Minimizing saved vars in ",
-        PredId, ProcId, !.ModuleInfo, !IO),
+            PredId, ProcId, !.ModuleInfo, !IO)
+    ),
+
     module_info_get_globals(!.ModuleInfo, Globals),
     module_info_pred_info(!.ModuleInfo, PredId, PredInfo),
     body_should_use_typeinfo_liveness(PredInfo, Globals, TypeInfoLiveness),
-    saved_vars_proc_no_io(TypeInfoLiveness, ProcInfo0, ProcInfo, !ModuleInfo).
-
-:- pred saved_vars_proc_no_io(bool::in, proc_info::in, proc_info::out,
-    module_info::in, module_info::out) is det.
 
-saved_vars_proc_no_io(TypeInfoLiveness, !ProcInfo, !ModuleInfo) :-
     proc_info_get_goal(!.ProcInfo, Goal0),
     proc_info_get_varset(!.ProcInfo, Varset0),
     proc_info_get_vartypes(!.ProcInfo, VarTypes0),
@@ -87,8 +86,6 @@
     final_slot_info(Varset1, VarTypes1, RttiVarMaps1, SlotInfo),
     proc_info_get_headvars(!.ProcInfo, HeadVars),
 
-    % hlds_out.write_goal(Goal1, !.ModuleInfo, Varset1, 0, "\n"),
-
     % Recompute the nonlocals for each goal.
     implicitly_quantify_clause_body_general(ordinary_nonlocals_no_lambda,
         HeadVars, _Warnings, Goal1, Goal2,
@@ -98,7 +95,18 @@
     recompute_instmap_delta(do_not_recompute_atomic_instmap_deltas,
         Goal2, Goal, VarTypes, InstVarSet, InstMap0, !ModuleInfo),
 
-    % hlds_out.write_goal(Goal, !.ModuleInfo, Varset, 0, "\n"),
+    trace [io(!IO), compile_time(flag("debug_saved_vars"))] (
+        OutInfo = hlds_out_util.init_hlds_out_info(Globals),
+        io.write_string("initial version:\n", !IO),
+        hlds_out_goal.write_goal(OutInfo, Goal0, !.ModuleInfo, Varset0,
+            yes, 0, "\n", !IO),
+        io.write_string("after transformation:\n", !IO),
+        hlds_out_goal.write_goal(OutInfo, Goal1, !.ModuleInfo, Varset1,
+            yes, 0, "\n", !IO),
+        io.write_string("final version:\n", !IO),
+        hlds_out_goal.write_goal(OutInfo, Goal, !.ModuleInfo, Varset,
+            yes, 0, "\n", !IO)
+    ),
 
     proc_info_set_goal(Goal, !ProcInfo),
     proc_info_set_varset(Varset, !ProcInfo),
Index: compiler/size_prof.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/size_prof.m,v
retrieving revision 1.64
diff -u -b -r1.64 size_prof.m
--- compiler/size_prof.m	4 Nov 2009 03:44:50 -0000	1.64
+++ compiler/size_prof.m	5 Aug 2010 02:49:19 -0000
@@ -96,8 +96,7 @@
 
     % Perform the transformation on the specified predicate.
     %
-:- pred size_prof_process_proc_msg(construct_transform::in,
-    pred_id::in, proc_id::in, pred_info::in,
+:- pred size_prof_process_proc_msg(construct_transform::in, pred_proc_id::in,
     proc_info::in, proc_info::out, module_info::in, module_info::out) is det.
 
 %-----------------------------------------------------------------------------%
@@ -117,6 +116,7 @@
 :- import_module hlds.hlds_out.hlds_out_util.
 :- import_module hlds.hlds_rtti.
 :- import_module hlds.instmap.
+:- import_module hlds.passes_aux.
 :- import_module hlds.pred_table.
 :- import_module hlds.quantification.
 :- import_module libs.compiler_util.
@@ -216,33 +216,23 @@
                 spi_module_info             :: module_info
             ).
 
-size_prof_process_proc_msg(Transform, PredId, ProcId, _PredInfo,
-        ProcInfo0, ProcInfo, !ModuleInfo) :-
-    module_info_get_globals(!.ModuleInfo, Globals),
-    globals.lookup_bool_option(Globals, very_verbose, VeryVerbose),
-    (
-        VeryVerbose = yes,
+size_prof_process_proc_msg(Transform, PredProcId, !ProcInfo, !ModuleInfo) :-
         trace [io(!IO)] (
-            io.write_string("% Adding typeinfos in ", !IO),
-            write_pred_proc_id_pair(!.ModuleInfo, PredId, ProcId, !IO),
-            io.write_string(": ", !IO)
+        write_proc_progress_message("% Adding typeinfos in ",
+            PredProcId, !.ModuleInfo, !IO)
         ),
-        size_prof_process_proc(Transform, PredId, ProcId, ProcInfo0, ProcInfo,
-            !ModuleInfo),
+    size_prof_process_proc(Transform, PredProcId, !ProcInfo, !ModuleInfo),
         trace [io(!IO)] (
-            io.write_string("done.\n", !IO)
-        )
-    ;
-        VeryVerbose = no,
-        size_prof_process_proc(Transform, PredId, ProcId, ProcInfo0, ProcInfo,
-            !ModuleInfo)
+        write_proc_progress_message("done.\n",
+            PredProcId, !.ModuleInfo, !IO)
     ).
 
-:- pred size_prof_process_proc(construct_transform::in,
-    pred_id::in, proc_id::in,
-    proc_info::in, proc_info::out, module_info::in, module_info::out) is det.
+:- pred size_prof_process_proc(construct_transform::in, pred_proc_id::in,
+    proc_info::in, proc_info::out,
+    module_info::in, module_info::out) is det.
 
-size_prof_process_proc(Transform, PredId, ProcId, !ProcInfo, !ModuleInfo) :-
+size_prof_process_proc(Transform, proc(PredId, ProcId), !ProcInfo,
+        !ModuleInfo) :-
     Simplifications = list_to_simplifications([]),
     simplify_proc_return_msgs(Simplifications, PredId, ProcId,
         !ModuleInfo, !ProcInfo, _Msgs),
Index: compiler/ssdebug.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ssdebug.m,v
retrieving revision 1.29
diff -u -b -r1.29 ssdebug.m
--- compiler/ssdebug.m	30 Jul 2010 05:16:16 -0000	1.29
+++ compiler/ssdebug.m	5 Aug 2010 02:49:19 -0000
@@ -470,13 +470,13 @@
 
 %-----------------------------------------------------------------------------%
 %
-% The main transformation
+% The main transformation.
 %
 
-:- pred process_proc(pred_id::in, proc_id::in, pred_info::in,
-    proc_info::in, proc_info::out, module_info::in, module_info::out) is det.
+:- pred process_proc(pred_proc_id::in, proc_info::in, proc_info::out,
+    module_info::in, module_info::out) is det.
 
-process_proc(PredId, ProcId, _PredInfo, !ProcInfo, !ModuleInfo) :-
+process_proc(proc(PredId, ProcId), !ProcInfo, !ModuleInfo) :-
     proc_info_get_argmodes(!.ProcInfo, ArgModes),
     ( check_arguments_modes(!.ModuleInfo, ArgModes) ->
         % We have different transformations for procedures of different
Index: compiler/stack_alloc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/stack_alloc.m,v
retrieving revision 1.24
diff -u -b -r1.24 stack_alloc.m
--- compiler/stack_alloc.m	23 Nov 2007 07:35:24 -0000	1.24
+++ compiler/stack_alloc.m	5 Aug 2010 02:49:19 -0000
@@ -28,12 +28,10 @@
 :- import_module hlds.hlds_module.
 :- import_module hlds.hlds_pred.
 
-:- import_module io.
-
 %-----------------------------------------------------------------------------%
 
-:- pred allocate_stack_slots_in_proc(pred_id::in, proc_id::in, module_info::in,
-    proc_info::in, proc_info::out, io::di, io::uo) is det.
+:- pred allocate_stack_slots_in_proc(module_info::in, pred_proc_id::in,
+    proc_info::in, proc_info::out) is det.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -54,6 +52,7 @@
 
 :- import_module bool.
 :- import_module int.
+:- import_module io.
 :- import_module list.
 :- import_module map.
 :- import_module maybe.
@@ -63,7 +62,7 @@
 
 %-----------------------------------------------------------------------------%
 
-allocate_stack_slots_in_proc(PredId, _ProcId, ModuleInfo, !ProcInfo, !IO) :-
+allocate_stack_slots_in_proc(ModuleInfo, proc(PredId, _ProcId), !ProcInfo) :-
     initial_liveness(!.ProcInfo, PredId, ModuleInfo, Liveness0),
     module_info_pred_info(ModuleInfo, PredId, PredInfo),
     module_info_get_globals(ModuleInfo, Globals),
Index: compiler/stack_opt.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/stack_opt.m,v
retrieving revision 1.43
diff -u -b -r1.43 stack_opt.m
--- compiler/stack_opt.m	4 Nov 2009 03:44:50 -0000	1.43
+++ compiler/stack_opt.m	5 Aug 2010 02:49:19 -0000
@@ -74,12 +74,10 @@
 :- import_module hlds.hlds_module.
 :- import_module hlds.hlds_pred.
 
-:- import_module io.
-
 %-----------------------------------------------------------------------------%
 
-:- pred stack_opt_cell(pred_id::in, proc_id::in, proc_info::in, proc_info::out,
-    module_info::in, module_info::out, io::di, io::uo) is det.
+:- pred stack_opt_cell(pred_proc_id::in, proc_info::in, proc_info::out,
+    module_info::in, module_info::out) is det.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -113,6 +111,7 @@
 :- import_module bool.
 :- import_module counter.
 :- import_module int.
+:- import_module io.
 :- import_module list.
 :- import_module map.
 :- import_module maybe.
@@ -165,13 +164,14 @@
                 soi_matching_results    :: list(matching_result)
             ).
 
-stack_opt_cell(PredId, ProcId, !ProcInfo, !ModuleInfo, !IO) :-
+stack_opt_cell(PredProcId, !ProcInfo, !ModuleInfo) :-
+    PredProcId = proc(PredId, ProcId),
     % This simplication is necessary to fix some bad inputs from
     % getting to the liveness computation.
     % (see tests/valid/stack_opt_simplify.m)
     Simplications = list_to_simplifications([]),
     simplify_proc(Simplications, PredId, ProcId, !ModuleInfo, !ProcInfo),
-    detect_liveness_proc(PredId, ProcId, !.ModuleInfo, !ProcInfo, !IO),
+    detect_liveness_proc(!.ModuleInfo, PredProcId, !ProcInfo),
     initial_liveness(!.ProcInfo, PredId, !.ModuleInfo, Liveness0),
     module_info_get_globals(!.ModuleInfo, Globals),
     module_info_pred_info(!.ModuleInfo, PredId, PredInfo),
@@ -189,24 +189,32 @@
         OptStackAlloc0, OptStackAlloc, Liveness0, _Liveness,
         NondetLiveness0, _NondetLiveness),
     proc_info_set_goal(Goal, !ProcInfo),
-    allocate_store_maps(for_stack_opt, PredId, !.ModuleInfo, !ProcInfo),
+    allocate_store_maps(for_stack_opt, !.ModuleInfo, PredProcId, !ProcInfo),
     globals.lookup_int_option(Globals, debug_stack_opt, DebugStackOpt),
     pred_id_to_int(PredId, PredIdInt),
+    trace [io(!IO)] (
     maybe_write_progress_message("\nbefore stack opt cell",
-        DebugStackOpt, PredIdInt, !.ProcInfo, !.ModuleInfo, !IO),
+            DebugStackOpt, PredIdInt, !.ProcInfo, !.ModuleInfo, !IO)
+    ),
     optimize_live_sets(!.ModuleInfo, OptStackAlloc, !ProcInfo,
-        Changed, DebugStackOpt, PredIdInt, !IO),
+        Changed, DebugStackOpt, PredIdInt),
     (
         Changed = yes,
+        trace [io(!IO)] (
         maybe_write_progress_message("\nafter stack opt transformation",
-            DebugStackOpt, PredIdInt, !.ProcInfo, !.ModuleInfo, !IO),
+                DebugStackOpt, PredIdInt, !.ProcInfo, !.ModuleInfo, !IO)
+        ),
         requantify_proc_general(ordinary_nonlocals_no_lambda, !ProcInfo),
+        trace [io(!IO)] (
         maybe_write_progress_message("\nafter stack opt requantify",
-            DebugStackOpt, PredIdInt, !.ProcInfo, !.ModuleInfo, !IO),
+                DebugStackOpt, PredIdInt, !.ProcInfo, !.ModuleInfo, !IO)
+        ),
         recompute_instmap_delta_proc(recompute_atomic_instmap_deltas,
             !ProcInfo, !ModuleInfo),
+        trace [io(!IO)] (
         maybe_write_progress_message("\nafter stack opt recompute instmaps",
             DebugStackOpt, PredIdInt, !.ProcInfo, !.ModuleInfo, !IO)
+        )
     ;
         Changed = no
     ).
@@ -216,11 +224,10 @@
 init_opt_stack_alloc = opt_stack_alloc(set.init).
 
 :- pred optimize_live_sets(module_info::in, opt_stack_alloc::in,
-    proc_info::in, proc_info::out, bool::out, int::in, int::in,
-    io::di, io::uo) is det.
+    proc_info::in, proc_info::out, bool::out, int::in, int::in) is det.
 
 optimize_live_sets(ModuleInfo, OptAlloc, !ProcInfo, Changed, DebugStackOpt,
-        PredIdInt, !IO) :-
+        PredIdInt) :-
     proc_info_get_goal(!.ProcInfo, Goal0),
     proc_info_get_vartypes(!.ProcInfo, VarTypes0),
     proc_info_get_varset(!.ProcInfo, VarSet0),
@@ -286,8 +293,10 @@
     build_interval_info_in_goal(Goal0, IntervalInfo0, IntervalInfo,
         StackOptInfo0, StackOptInfo),
     ( DebugStackOpt = PredIdInt ->
+        trace [io(!IO)] (
         dump_interval_info(IntervalInfo, !IO),
         dump_stack_opt_info(StackOptInfo, !IO)
+        )
     ;
         true
     ),
Index: compiler/store_alloc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/store_alloc.m,v
retrieving revision 1.110
diff -u -b -r1.110 store_alloc.m
--- compiler/store_alloc.m	23 Dec 2008 01:37:40 -0000	1.110
+++ compiler/store_alloc.m	5 Aug 2010 02:49:19 -0000
@@ -37,8 +37,8 @@
     --->    final_allocation
     ;       for_stack_opt.
 
-:- pred allocate_store_maps(store_map_run_type::in, pred_id::in,
-    module_info::in, proc_info::in, proc_info::out) is det.
+:- pred allocate_store_maps(store_map_run_type::in, module_info::in,
+    pred_proc_id::in, proc_info::in, proc_info::out) is det.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -70,7 +70,7 @@
 
 %-----------------------------------------------------------------------------%
 
-allocate_store_maps(RunType, PredId, ModuleInfo, !ProcInfo) :-
+allocate_store_maps(RunType, ModuleInfo, proc(PredId, _), !ProcInfo) :-
     module_info_get_globals(ModuleInfo, Globals),
     (
         RunType = final_allocation,
Index: compiler/structure_sharing.analysis.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_sharing.analysis.m,v
retrieving revision 1.47
diff -u -b -r1.47 structure_sharing.analysis.m
--- compiler/structure_sharing.analysis.m	30 Jul 2010 05:16:16 -0000	1.47
+++ compiler/structure_sharing.analysis.m	5 Aug 2010 02:49:19 -0000
@@ -340,26 +340,25 @@
     % used by the liveness pass (liveness.m). This information is used to
     % eliminate useless sharing pairs during sharing analysis.
     %
-:- pred annotate_liveness(module_info::in, module_info::out, io::di,
-    io::uo) is det.
+:- pred annotate_liveness(module_info::in, module_info::out,
+    io::di, io::uo) is det.
 
 annotate_liveness(!ModuleInfo, !IO) :-
     process_all_nonimported_procs(
-        update_module_io(simplify_and_detect_liveness_proc),
+        update_module(simplify_and_detect_liveness_proc),
         !ModuleInfo, !IO).
 
-:- pred simplify_and_detect_liveness_proc(pred_id::in, proc_id::in,
-    proc_info::in, proc_info::out, module_info::in, module_info::out,
-    io::di, io::uo) is det.
+:- pred simplify_and_detect_liveness_proc(pred_proc_id::in,
+    proc_info::in, proc_info::out, module_info::in, module_info::out) is det.
 
-simplify_and_detect_liveness_proc(PredId, ProcId, !ProcInfo, !ModuleInfo,
-        !IO) :-
-    % Liveness annotation expects the procedure to have been simplified.  For
-    % example, an if-then-else with an `erroneous' condition will cause an
-    % assertion failure if it is not simplified away.
+simplify_and_detect_liveness_proc(PredProcId, !ProcInfo, !ModuleInfo) :-
+    % Liveness annotation expects the procedure to have been simplified.
+    % For example, an if-then-else with an `erroneous' condition will cause
+    % an assertion failure if it is not simplified away.
     Simplifications = list_to_simplifications([]),
+    PredProcId = proc(PredId, ProcId),
     simplify_proc(Simplifications, PredId, ProcId, !ModuleInfo, !ProcInfo),
-    detect_liveness_proc(PredId, ProcId, !.ModuleInfo, !ProcInfo, !IO).
+    detect_liveness_proc(!.ModuleInfo, PredProcId, !ProcInfo).
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/tupling.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/tupling.m,v
retrieving revision 1.51
diff -u -b -r1.51 tupling.m
--- compiler/tupling.m	10 Jun 2010 00:58:17 -0000	1.51
+++ compiler/tupling.m	5 Aug 2010 02:49:19 -0000
@@ -325,7 +325,7 @@
 
 maybe_tuple_scc_2(TraceCounts, TuningParams, PredProcIds, CandidateHeadVars,
         !ModuleInfo, !Counter, !TransformMap, !IO, VeryVerbose) :-
-    list.foldl2(prepare_proc_for_counting, PredProcIds, !ModuleInfo, !IO),
+    list.foldl(prepare_proc_for_counting, PredProcIds, !ModuleInfo),
     % Count the average number of loads/stores without any transformation.
     count_load_stores_for_scc(TraceCounts, TuningParams, !.ModuleInfo,
         map.init, PredProcIds, CostsWithoutTupling),
@@ -848,9 +848,9 @@
     % the count_load_stores_in_proc predicate to work.
     %
 :- pred prepare_proc_for_counting(pred_proc_id::in,
-    module_info::in, module_info::out, io::di, io::uo) is det.
+    module_info::in, module_info::out) is det.
 
-prepare_proc_for_counting(PredProcId, !ModuleInfo, !IO) :-
+prepare_proc_for_counting(PredProcId, !ModuleInfo) :-
     PredProcId = proc(PredId, ProcId),
     some [!ProcInfo] (
         module_info_pred_proc_info(!.ModuleInfo, PredId, ProcId,
@@ -858,7 +858,7 @@
         pred_info_get_arg_types(PredInfo, ArgTypes),
         generate_proc_arg_info(ArgTypes, !.ModuleInfo, !ProcInfo),
 
-        detect_liveness_proc(PredId, ProcId, !.ModuleInfo, !ProcInfo, !IO),
+        detect_liveness_proc(!.ModuleInfo, PredProcId, !ProcInfo),
         initial_liveness(!.ProcInfo, PredId, !.ModuleInfo, Liveness0),
         module_info_get_globals(!.ModuleInfo, Globals),
         body_should_use_typeinfo_liveness(PredInfo, Globals, TypeInfoLiveness),
Index: compiler/unneeded_code.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/unneeded_code.m,v
retrieving revision 1.53
diff -u -b -r1.53 unneeded_code.m
--- compiler/unneeded_code.m	4 Nov 2009 03:44:52 -0000	1.53
+++ compiler/unneeded_code.m	5 Aug 2010 02:49:20 -0000
@@ -62,13 +62,10 @@
 :- import_module hlds.hlds_module.
 :- import_module hlds.hlds_pred.
 
-:- import_module io.
-
 %-----------------------------------------------------------------------------%
 
-:- pred unneeded_process_proc_msg(pred_id::in, proc_id::in,
-    proc_info::in, proc_info::out, module_info::in, module_info::out,
-    io::di, io::uo) is det.
+:- pred unneeded_process_proc_msg(pred_proc_id::in,
+    proc_info::in, proc_info::out, module_info::in, module_info::out) is det.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -84,6 +81,7 @@
 :- import_module hlds.hlds_out.hlds_out_goal.
 :- import_module hlds.hlds_out.hlds_out_util.
 :- import_module hlds.instmap.
+:- import_module hlds.passes_aux.
 :- import_module hlds.quantification.
 :- import_module libs.compiler_util.
 :- import_module libs.globals.
@@ -95,6 +93,7 @@
 :- import_module bool.
 :- import_module cord.
 :- import_module int.
+:- import_module io.
 :- import_module list.
 :- import_module map.
 :- import_module maybe.
@@ -216,7 +215,7 @@
 
 %-----------------------------------------------------------------------------%
 
-unneeded_process_proc_msg(PredId, ProcId, !ProcInfo, !ModuleInfo, !IO) :-
+unneeded_process_proc_msg(PredProcId, !ProcInfo, !ModuleInfo) :-
     % The transformation considers every nonlocal variable of a goal
     % that is bound on entry to be consumed by that goal. If the nonlocal set
     % contains any such variables that are not actually needed by the goal,
@@ -224,26 +223,23 @@
     % Therefore we preprocess the procedure body to ensure that the nonlocals
     % sets are accurate reflections of the true needs of goals.
 
-    module_info_get_globals(!.ModuleInfo, Globals),
-    globals.lookup_bool_option(Globals, very_verbose, VeryVerbose),
-    (
-        VeryVerbose = yes,
-        io.write_string("% Removing dead code in ", !IO),
-        write_pred_proc_id_pair(!.ModuleInfo, PredId, ProcId, !IO),
-        io.write_string(" ...\n", !IO),
+    trace [io(!IO)] (
+        write_proc_progress_message("% Removing dead code in ",
+            PredProcId, !.ModuleInfo, !IO)
+    ),
         unneeded_pre_process_proc(!ProcInfo),
+    PredProcId = proc(PredId, _),
         unneeded_process_proc(!ProcInfo, !ModuleInfo, PredId, 1, Successful),
+    trace [io(!IO)] (
         (
             Successful = yes,
-            io.write_string("% done.\n", !IO)
+            write_proc_progress_message("% done.\n",
+                PredProcId, !.ModuleInfo, !IO)
         ;
             Successful = no,
-            io.write_string("% none found.\n", !IO)
+            write_proc_progress_message("% none found.\n",
+                PredProcId, !.ModuleInfo, !IO)
         )
-    ;
-        VeryVerbose = no,
-        unneeded_pre_process_proc(!ProcInfo),
-        unneeded_process_proc(!ProcInfo, !ModuleInfo, PredId, 1, _)
     ).
 
 :- pred unneeded_pre_process_proc(proc_info::in, proc_info::out) is det.
cvs diff: Diffing compiler/notes
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list