comment fix
[ghc-hetmet.git] / rts / sm / Evac.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team 1998-2008
4  *
5  * Generational garbage collector: evacuation functions
6  *
7  * Documentation on the architecture of the Garbage Collector can be
8  * found in the online commentary:
9  * 
10  *   http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
11  *
12  * ---------------------------------------------------------------------------*/
13
14 #include "PosixSource.h"
15 #include "Rts.h"
16
17 #include "Evac.h"
18 #include "Storage.h"
19 #include "GC.h"
20 #include "GCThread.h"
21 #include "GCUtils.h"
22 #include "Compact.h"
23 #include "Prelude.h"
24 #include "Trace.h"
25 #include "LdvProfile.h"
26
27 #if defined(PROF_SPIN) && defined(THREADED_RTS) && defined(PARALLEL_GC)
28 StgWord64 whitehole_spin = 0;
29 #endif
30
31 #if defined(THREADED_RTS) && !defined(PARALLEL_GC)
32 #define evacuate(p) evacuate1(p)
33 #define HEAP_ALLOCED_GC(p) HEAP_ALLOCED(p)
34 #endif
35
36 #if !defined(PARALLEL_GC)
37 #define copy_tag_nolock(p, info, src, size, stp, tag) \
38         copy_tag(p, info, src, size, stp, tag)
39 #endif
40
41 /* Used to avoid long recursion due to selector thunks
42  */
43 #define MAX_THUNK_SELECTOR_DEPTH 16
44
45 static void eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool);
46 STATIC_INLINE void evacuate_large(StgPtr p);
47
48 /* -----------------------------------------------------------------------------
49    Allocate some space in which to copy an object.
50    -------------------------------------------------------------------------- */
51
52 STATIC_INLINE StgPtr
53 alloc_for_copy (nat size, step *stp)
54 {
55     StgPtr to;
56     step_workspace *ws;
57
58     /* Find out where we're going, using the handy "to" pointer in 
59      * the step of the source object.  If it turns out we need to
60      * evacuate to an older generation, adjust it here (see comment
61      * by evacuate()).
62      */
63     if (stp < gct->evac_step) {
64         if (gct->eager_promotion) {
65             stp = gct->evac_step;
66         } else {
67             gct->failed_to_evac = rtsTrue;
68         }
69     }
70     
71     ws = &gct->steps[stp->abs_no];
72     // this compiles to a single mem access to stp->abs_no only
73     
74     /* chain a new block onto the to-space for the destination step if
75      * necessary.
76      */
77     to = ws->todo_free;
78     ws->todo_free += size;
79     if (ws->todo_free > ws->todo_lim) {
80         to = todo_block_full(size, ws);
81     }
82     ASSERT(ws->todo_free >= ws->todo_bd->free && ws->todo_free <= ws->todo_lim);
83
84     return to;
85 }
86
87 /* -----------------------------------------------------------------------------
88    The evacuate() code
89    -------------------------------------------------------------------------- */
90
91 STATIC_INLINE GNUC_ATTR_HOT void
92 copy_tag(StgClosure **p, const StgInfoTable *info, 
93          StgClosure *src, nat size, step *stp, StgWord tag)
94 {
95     StgPtr to, from;
96     nat i;
97
98     to = alloc_for_copy(size,stp);
99     
100     TICK_GC_WORDS_COPIED(size);
101
102     from = (StgPtr)src;
103     to[0] = (W_)info;
104     for (i = 1; i < size; i++) { // unroll for small i
105         to[i] = from[i];
106     }
107
108 //  if (to+size+2 < bd->start + BLOCK_SIZE_W) {
109 //      __builtin_prefetch(to + size + 2, 1);
110 //  }
111
112 #if defined(PARALLEL_GC)
113     {
114         const StgInfoTable *new_info;
115         new_info = (const StgInfoTable *)cas((StgPtr)&src->header.info, (W_)info, MK_FORWARDING_PTR(to));
116         if (new_info != info) {
117             return evacuate(p); // does the failed_to_evac stuff
118         } else {
119             *p = TAG_CLOSURE(tag,(StgClosure*)to);
120         }
121     }
122 #else
123     src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
124     *p = TAG_CLOSURE(tag,(StgClosure*)to);
125 #endif
126
127 #ifdef PROFILING
128     // We store the size of the just evacuated object in the LDV word so that
129     // the profiler can guess the position of the next object later.
130     SET_EVACUAEE_FOR_LDV(from, size);
131 #endif
132 }
133
134 #if defined(PARALLEL_GC)
135 STATIC_INLINE void
136 copy_tag_nolock(StgClosure **p, const StgInfoTable *info, 
137          StgClosure *src, nat size, step *stp, StgWord tag)
138 {
139     StgPtr to, from;
140     nat i;
141
142     to = alloc_for_copy(size,stp);
143     *p = TAG_CLOSURE(tag,(StgClosure*)to);
144     src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
145     
146     TICK_GC_WORDS_COPIED(size);
147
148     from = (StgPtr)src;
149     to[0] = (W_)info;
150     for (i = 1; i < size; i++) { // unroll for small i
151         to[i] = from[i];
152     }
153
154 //  if (to+size+2 < bd->start + BLOCK_SIZE_W) {
155 //      __builtin_prefetch(to + size + 2, 1);
156 //  }
157
158 #ifdef PROFILING
159     // We store the size of the just evacuated object in the LDV word so that
160     // the profiler can guess the position of the next object later.
161     SET_EVACUAEE_FOR_LDV(from, size);
162 #endif
163 }
164 #endif
165
166 /* Special version of copy() for when we only want to copy the info
167  * pointer of an object, but reserve some padding after it.  This is
168  * used to optimise evacuation of BLACKHOLEs.
169  */
170 static rtsBool
171 copyPart(StgClosure **p, StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
172 {
173     StgPtr to, from;
174     nat i;
175     StgWord info;
176     
177 #if defined(PARALLEL_GC)
178 spin:
179         info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
180         if (info == (W_)&stg_WHITEHOLE_info) {
181 #ifdef PROF_SPIN
182             whitehole_spin++;
183 #endif
184             goto spin;
185         }
186     if (IS_FORWARDING_PTR(info)) {
187         src->header.info = (const StgInfoTable *)info;
188         evacuate(p); // does the failed_to_evac stuff
189         return rtsFalse;
190     }
191 #else
192     info = (W_)src->header.info;
193 #endif
194
195     to = alloc_for_copy(size_to_reserve, stp);
196     *p = (StgClosure *)to;
197
198     TICK_GC_WORDS_COPIED(size_to_copy);
199
200     from = (StgPtr)src;
201     to[0] = info;
202     for (i = 1; i < size_to_copy; i++) { // unroll for small i
203         to[i] = from[i];
204     }
205     
206 #if defined(PARALLEL_GC)
207     write_barrier();
208 #endif
209     src->header.info = (const StgInfoTable*)MK_FORWARDING_PTR(to);
210     
211 #ifdef PROFILING
212     // We store the size of the just evacuated object in the LDV word so that
213     // the profiler can guess the position of the next object later.
214     SET_EVACUAEE_FOR_LDV(from, size_to_reserve);
215     // fill the slop
216     if (size_to_reserve - size_to_copy > 0)
217         LDV_FILL_SLOP(to + size_to_copy, (int)(size_to_reserve - size_to_copy));
218 #endif
219
220     return rtsTrue;
221 }
222
223
224 /* Copy wrappers that don't tag the closure after copying */
225 STATIC_INLINE GNUC_ATTR_HOT void
226 copy(StgClosure **p, const StgInfoTable *info, 
227      StgClosure *src, nat size, step *stp)
228 {
229     copy_tag(p,info,src,size,stp,0);
230 }
231
232 /* -----------------------------------------------------------------------------
233    Evacuate a large object
234
235    This just consists of removing the object from the (doubly-linked)
236    step->large_objects list, and linking it on to the (singly-linked)
237    step->new_large_objects list, from where it will be scavenged later.
238
239    Convention: bd->flags has BF_EVACUATED set for a large object
240    that has been evacuated, or unset otherwise.
241    -------------------------------------------------------------------------- */
242
243 STATIC_INLINE void
244 evacuate_large(StgPtr p)
245 {
246   bdescr *bd = Bdescr(p);
247   step *stp, *new_stp;
248   step_workspace *ws;
249     
250   stp = bd->step;
251   ACQUIRE_SPIN_LOCK(&stp->sync_large_objects);
252
253   // already evacuated? 
254   if (bd->flags & BF_EVACUATED) { 
255     /* Don't forget to set the gct->failed_to_evac flag if we didn't get
256      * the desired destination (see comments in evacuate()).
257      */
258     if (stp < gct->evac_step) {
259         gct->failed_to_evac = rtsTrue;
260         TICK_GC_FAILED_PROMOTION();
261     }
262     RELEASE_SPIN_LOCK(&stp->sync_large_objects);
263     return;
264   }
265
266   // remove from large_object list 
267   if (bd->u.back) {
268     bd->u.back->link = bd->link;
269   } else { // first object in the list 
270     stp->large_objects = bd->link;
271   }
272   if (bd->link) {
273     bd->link->u.back = bd->u.back;
274   }
275   
276   /* link it on to the evacuated large object list of the destination step
277    */
278   new_stp = stp->to;
279   if (new_stp < gct->evac_step) {
280       if (gct->eager_promotion) {
281           new_stp = gct->evac_step;
282       } else {
283           gct->failed_to_evac = rtsTrue;
284       }
285   }
286
287   ws = &gct->steps[new_stp->abs_no];
288
289   bd->flags |= BF_EVACUATED;
290   bd->step = new_stp;
291   bd->gen_no = new_stp->gen_no;
292
293   // If this is a block of pinned objects, we don't have to scan
294   // these objects, because they aren't allowed to contain any
295   // pointers.  For these blocks, we skip the scavenge stage and put
296   // them straight on the scavenged_large_objects list.
297   if (bd->flags & BF_PINNED) {
298       ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS);
299       if (new_stp != stp) { ACQUIRE_SPIN_LOCK(&new_stp->sync_large_objects); }
300       dbl_link_onto(bd, &new_stp->scavenged_large_objects);
301       new_stp->n_scavenged_large_blocks += bd->blocks;
302       if (new_stp != stp) { RELEASE_SPIN_LOCK(&new_stp->sync_large_objects); }
303   } else {
304       bd->link = ws->todo_large_objects;
305       ws->todo_large_objects = bd;
306   }
307
308   RELEASE_SPIN_LOCK(&stp->sync_large_objects);
309 }
310
311 /* ----------------------------------------------------------------------------
312    Evacuate
313
314    This is called (eventually) for every live object in the system.
315
316    The caller to evacuate specifies a desired generation in the
317    gct->evac_step thread-local variable.  The following conditions apply to
318    evacuating an object which resides in generation M when we're
319    collecting up to generation N
320
321    if  M >= gct->evac_step 
322            if  M > N     do nothing
323            else          evac to step->to
324
325    if  M < gct->evac_step      evac to gct->evac_step, step 0
326
327    if the object is already evacuated, then we check which generation
328    it now resides in.
329
330    if  M >= gct->evac_step     do nothing
331    if  M <  gct->evac_step     set gct->failed_to_evac flag to indicate that we
332                          didn't manage to evacuate this object into gct->evac_step.
333
334
335    OPTIMISATION NOTES:
336
337    evacuate() is the single most important function performance-wise
338    in the GC.  Various things have been tried to speed it up, but as
339    far as I can tell the code generated by gcc 3.2 with -O2 is about
340    as good as it's going to get.  We pass the argument to evacuate()
341    in a register using the 'regparm' attribute (see the prototype for
342    evacuate() near the top of this file).
343
344    Changing evacuate() to take an (StgClosure **) rather than
345    returning the new pointer seems attractive, because we can avoid
346    writing back the pointer when it hasn't changed (eg. for a static
347    object, or an object in a generation > N).  However, I tried it and
348    it doesn't help.  One reason is that the (StgClosure **) pointer
349    gets spilled to the stack inside evacuate(), resulting in far more
350    extra reads/writes than we save.
351    ------------------------------------------------------------------------- */
352
353 REGPARM1 GNUC_ATTR_HOT void 
354 evacuate(StgClosure **p)
355 {
356   bdescr *bd = NULL;
357   step *stp;
358   StgClosure *q;
359   const StgInfoTable *info;
360   StgWord tag;
361
362   q = *p;
363
364 loop:
365   /* The tag and the pointer are split, to be merged after evacing */
366   tag = GET_CLOSURE_TAG(q);
367   q = UNTAG_CLOSURE(q);
368
369   ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
370
371   if (!HEAP_ALLOCED_GC(q)) {
372
373       if (!major_gc) return;
374
375       info = get_itbl(q);
376       switch (info->type) {
377
378       case THUNK_STATIC:
379           if (info->srt_bitmap != 0) {
380               if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
381 #ifndef THREADED_RTS
382                   *THUNK_STATIC_LINK((StgClosure *)q) = gct->static_objects;
383                   gct->static_objects = (StgClosure *)q;
384 #else
385                   StgPtr link;
386                   link = (StgPtr)cas((StgPtr)THUNK_STATIC_LINK((StgClosure *)q),
387                                      (StgWord)NULL,
388                                      (StgWord)gct->static_objects);
389                   if (link == NULL) {
390                       gct->static_objects = (StgClosure *)q;
391                   }
392 #endif
393               }
394           }
395           return;
396
397       case FUN_STATIC:
398           if (info->srt_bitmap != 0 &&
399               *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
400 #ifndef THREADED_RTS
401               *FUN_STATIC_LINK((StgClosure *)q) = gct->static_objects;
402               gct->static_objects = (StgClosure *)q;
403 #else
404               StgPtr link;
405               link = (StgPtr)cas((StgPtr)FUN_STATIC_LINK((StgClosure *)q),
406                                  (StgWord)NULL,
407                                  (StgWord)gct->static_objects);
408               if (link == NULL) {
409                   gct->static_objects = (StgClosure *)q;
410               }
411 #endif
412           }
413           return;
414           
415       case IND_STATIC:
416           /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
417            * on the CAF list, so don't do anything with it here (we'll
418            * scavenge it later).
419            */
420           if (((StgIndStatic *)q)->saved_info == NULL) {
421               if (*IND_STATIC_LINK((StgClosure *)q) == NULL) {
422 #ifndef THREADED_RTS
423                   *IND_STATIC_LINK((StgClosure *)q) = gct->static_objects;
424                   gct->static_objects = (StgClosure *)q;
425 #else
426                   StgPtr link;
427                   link = (StgPtr)cas((StgPtr)IND_STATIC_LINK((StgClosure *)q),
428                                      (StgWord)NULL,
429                                      (StgWord)gct->static_objects);
430                   if (link == NULL) {
431                       gct->static_objects = (StgClosure *)q;
432                   }
433 #endif
434               }
435           }
436           return;
437           
438       case CONSTR_STATIC:
439           if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
440 #ifndef THREADED_RTS
441               *STATIC_LINK(info,(StgClosure *)q) = gct->static_objects;
442               gct->static_objects = (StgClosure *)q;
443 #else
444               StgPtr link;
445               link = (StgPtr)cas((StgPtr)STATIC_LINK(info,(StgClosure *)q),
446                                  (StgWord)NULL,
447                                  (StgWord)gct->static_objects);
448               if (link == NULL) {
449                   gct->static_objects = (StgClosure *)q;
450               }
451 #endif
452           }
453           /* I am assuming that static_objects pointers are not
454            * written to other objects, and thus, no need to retag. */
455           return;
456           
457       case CONSTR_NOCAF_STATIC:
458           /* no need to put these on the static linked list, they don't need
459            * to be scavenged.
460            */
461           return;
462           
463       default:
464           barf("evacuate(static): strange closure type %d", (int)(info->type));
465       }
466   }
467
468   bd = Bdescr((P_)q);
469
470   if ((bd->flags & (BF_LARGE | BF_MARKED | BF_EVACUATED)) != 0) {
471
472       // pointer into to-space: just return it.  It might be a pointer
473       // into a generation that we aren't collecting (> N), or it
474       // might just be a pointer into to-space.  The latter doesn't
475       // happen often, but allowing it makes certain things a bit
476       // easier; e.g. scavenging an object is idempotent, so it's OK to
477       // have an object on the mutable list multiple times.
478       if (bd->flags & BF_EVACUATED) {
479           // We aren't copying this object, so we have to check
480           // whether it is already in the target generation.  (this is
481           // the write barrier).
482           if (bd->step < gct->evac_step) {
483               gct->failed_to_evac = rtsTrue;
484               TICK_GC_FAILED_PROMOTION();
485           }
486           return;
487       }
488
489       /* evacuate large objects by re-linking them onto a different list.
490        */
491       if (bd->flags & BF_LARGE) {
492           info = get_itbl(q);
493           if (info->type == TSO && 
494               ((StgTSO *)q)->what_next == ThreadRelocated) {
495               q = (StgClosure *)((StgTSO *)q)->_link;
496               *p = q;
497               goto loop;
498           }
499           evacuate_large((P_)q);
500           return;
501       }
502       
503       /* If the object is in a step that we're compacting, then we
504        * need to use an alternative evacuate procedure.
505        */
506       if (!is_marked((P_)q,bd)) {
507           mark((P_)q,bd);
508           if (mark_stack_full()) {
509               debugTrace(DEBUG_gc,"mark stack overflowed");
510               mark_stack_overflowed = rtsTrue;
511               reset_mark_stack();
512           }
513           push_mark_stack((P_)q);
514       }
515       return;
516   }
517       
518   stp = bd->step->to;
519
520   info = q->header.info;
521   if (IS_FORWARDING_PTR(info))
522   {
523     /* Already evacuated, just return the forwarding address.
524      * HOWEVER: if the requested destination generation (gct->evac_step) is
525      * older than the actual generation (because the object was
526      * already evacuated to a younger generation) then we have to
527      * set the gct->failed_to_evac flag to indicate that we couldn't 
528      * manage to promote the object to the desired generation.
529      */
530     /* 
531      * Optimisation: the check is fairly expensive, but we can often
532      * shortcut it if either the required generation is 0, or the
533      * current object (the EVACUATED) is in a high enough generation.
534      * We know that an EVACUATED always points to an object in the
535      * same or an older generation.  stp is the lowest step that the
536      * current object would be evacuated to, so we only do the full
537      * check if stp is too low.
538      */
539       StgClosure *e = (StgClosure*)UN_FORWARDING_PTR(info);
540       *p = TAG_CLOSURE(tag,e);
541       if (stp < gct->evac_step) {  // optimisation 
542           if (Bdescr((P_)e)->step < gct->evac_step) {
543               gct->failed_to_evac = rtsTrue;
544               TICK_GC_FAILED_PROMOTION();
545           }
546       }
547       return;
548   }
549
550   switch (INFO_PTR_TO_STRUCT(info)->type) {
551
552   case WHITEHOLE:
553       goto loop;
554
555   case MUT_VAR_CLEAN:
556   case MUT_VAR_DIRTY:
557   case MVAR_CLEAN:
558   case MVAR_DIRTY:
559       copy(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp);
560       return;
561
562   case CONSTR_0_1:
563   { 
564       StgWord w = (StgWord)q->payload[0];
565       if (info == Czh_con_info &&
566           // unsigned, so always true:  (StgChar)w >= MIN_CHARLIKE &&  
567           (StgChar)w <= MAX_CHARLIKE) {
568           *p =  TAG_CLOSURE(tag,
569                             (StgClosure *)CHARLIKE_CLOSURE((StgChar)w)
570                            );
571       }
572       else if (info == Izh_con_info &&
573           (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
574           *p = TAG_CLOSURE(tag,
575                              (StgClosure *)INTLIKE_CLOSURE((StgInt)w)
576                              );
577       }
578       else {
579           copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,stp,tag);
580       }
581       return;
582   }
583
584   case FUN_0_1:
585   case FUN_1_0:
586   case CONSTR_1_0:
587       copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,stp,tag);
588       return;
589
590   case THUNK_1_0:
591   case THUNK_0_1:
592       copy(p,info,q,sizeofW(StgThunk)+1,stp);
593       return;
594
595   case THUNK_1_1:
596   case THUNK_2_0:
597   case THUNK_0_2:
598 #ifdef NO_PROMOTE_THUNKS
599     if (bd->gen_no == 0 && 
600         bd->step->no != 0 &&
601         bd->step->no == generations[bd->gen_no].n_steps-1) {
602       stp = bd->step;
603     }
604 #endif
605     copy(p,info,q,sizeofW(StgThunk)+2,stp);
606     return;
607
608   case FUN_1_1:
609   case FUN_2_0:
610   case FUN_0_2:
611   case CONSTR_1_1:
612   case CONSTR_2_0:
613       copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,stp,tag);
614       return;
615
616   case CONSTR_0_2:
617       copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,stp,tag);
618       return;
619
620   case THUNK:
621       copy(p,info,q,thunk_sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp);
622       return;
623
624   case FUN:
625   case IND_PERM:
626   case IND_OLDGEN_PERM:
627   case CONSTR:
628       copy_tag_nolock(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp,tag);
629       return;
630
631   case WEAK:
632   case STABLE_NAME:
633       copy_tag(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp,tag);
634       return;
635
636   case BCO:
637       copy(p,info,q,bco_sizeW((StgBCO *)q),stp);
638       return;
639
640   case CAF_BLACKHOLE:
641   case BLACKHOLE:
642       copyPart(p,q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
643       return;
644
645   case THUNK_SELECTOR:
646       eval_thunk_selector(p, (StgSelector *)q, rtsTrue);
647       return;
648
649   case IND:
650   case IND_OLDGEN:
651     // follow chains of indirections, don't evacuate them 
652     q = ((StgInd*)q)->indirectee;
653     *p = q;
654     goto loop;
655
656   case RET_BCO:
657   case RET_SMALL:
658   case RET_BIG:
659   case RET_DYN:
660   case UPDATE_FRAME:
661   case STOP_FRAME:
662   case CATCH_FRAME:
663   case CATCH_STM_FRAME:
664   case CATCH_RETRY_FRAME:
665   case ATOMICALLY_FRAME:
666     // shouldn't see these 
667     barf("evacuate: stack frame at %p\n", q);
668
669   case PAP:
670       copy(p,info,q,pap_sizeW((StgPAP*)q),stp);
671       return;
672
673   case AP:
674       copy(p,info,q,ap_sizeW((StgAP*)q),stp);
675       return;
676
677   case AP_STACK:
678       copy(p,info,q,ap_stack_sizeW((StgAP_STACK*)q),stp);
679       return;
680
681   case ARR_WORDS:
682       // just copy the block 
683       copy(p,info,q,arr_words_sizeW((StgArrWords *)q),stp);
684       return;
685
686   case MUT_ARR_PTRS_CLEAN:
687   case MUT_ARR_PTRS_DIRTY:
688   case MUT_ARR_PTRS_FROZEN:
689   case MUT_ARR_PTRS_FROZEN0:
690       // just copy the block 
691       copy(p,info,q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
692       return;
693
694   case TSO:
695     {
696       StgTSO *tso = (StgTSO *)q;
697
698       /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
699        */
700       if (tso->what_next == ThreadRelocated) {
701         q = (StgClosure *)tso->_link;
702         *p = q;
703         goto loop;
704       }
705
706       /* To evacuate a small TSO, we need to adjust the stack pointer
707        */
708       {
709           StgTSO *new_tso;
710           StgPtr r, s;
711           rtsBool mine;
712
713           mine = copyPart(p,(StgClosure *)tso, tso_sizeW(tso), 
714                           sizeofW(StgTSO), stp);
715           if (mine) {
716               new_tso = (StgTSO *)*p;
717               move_TSO(tso, new_tso);
718               for (r = tso->sp, s = new_tso->sp;
719                    r < tso->stack+tso->stack_size;) {
720                   *s++ = *r++;
721               }
722           }
723           return;
724       }
725     }
726
727   case TREC_HEADER: 
728       copy(p,info,q,sizeofW(StgTRecHeader),stp);
729       return;
730
731   case TVAR_WATCH_QUEUE:
732       copy(p,info,q,sizeofW(StgTVarWatchQueue),stp);
733       return;
734
735   case TVAR:
736       copy(p,info,q,sizeofW(StgTVar),stp);
737       return;
738     
739   case TREC_CHUNK:
740       copy(p,info,q,sizeofW(StgTRecChunk),stp);
741       return;
742
743   case ATOMIC_INVARIANT:
744       copy(p,info,q,sizeofW(StgAtomicInvariant),stp);
745       return;
746
747   case INVARIANT_CHECK_QUEUE:
748       copy(p,info,q,sizeofW(StgInvariantCheckQueue),stp);
749       return;
750
751   default:
752     barf("evacuate: strange closure type %d", (int)(INFO_PTR_TO_STRUCT(info)->type));
753   }
754
755   barf("evacuate");
756 }
757
758 /* -----------------------------------------------------------------------------
759    Evaluate a THUNK_SELECTOR if possible.
760
761    p points to a THUNK_SELECTOR that we want to evaluate.  The
762    result of "evaluating" it will be evacuated and a pointer to the
763    to-space closure will be returned.
764
765    If the THUNK_SELECTOR could not be evaluated (its selectee is still
766    a THUNK, for example), then the THUNK_SELECTOR itself will be
767    evacuated.
768    -------------------------------------------------------------------------- */
769 static void
770 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
771 {
772     StgSelector *prev;
773
774     prev = NULL;
775     while (p)
776     {
777 #ifdef THREADED_RTS
778         ASSERT(p->header.info == &stg_WHITEHOLE_info);
779 #else
780         ASSERT(p->header.info == &stg_BLACKHOLE_info);
781 #endif
782         // val must be in to-space.  Not always: when we recursively
783         // invoke eval_thunk_selector(), the recursive calls will not 
784         // evacuate the value (because we want to select on the value,
785         // not evacuate it), so in this case val is in from-space.
786         // ASSERT(!HEAP_ALLOCED_GC(val) || Bdescr((P_)val)->gen_no > N || (Bdescr((P_)val)->flags & BF_EVACUATED));
787
788         prev = (StgSelector*)((StgClosure *)p)->payload[0];
789
790         // Update the THUNK_SELECTOR with an indirection to the
791         // value.  The value is still in from-space at this stage.
792         //
793         // (old note: Why not do upd_evacuee(q,p)?  Because we have an
794         // invariant that an EVACUATED closure always points to an
795         // object in the same or an older generation (required by
796         // the short-cut test in the EVACUATED case, below).
797         if ((StgClosure *)p == val) {
798             // must be a loop; just leave a BLACKHOLE in place.  This
799             // can happen when we have a chain of selectors that
800             // eventually loops back on itself.  We can't leave an
801             // indirection pointing to itself, and we want the program
802             // to deadlock if it ever enters this closure, so
803             // BLACKHOLE is correct.
804             SET_INFO(p, &stg_BLACKHOLE_info);
805         } else {
806             ((StgInd *)p)->indirectee = val;
807             write_barrier();
808             SET_INFO(p, &stg_IND_info);
809         }
810
811         // For the purposes of LDV profiling, we have created an
812         // indirection.
813         LDV_RECORD_CREATE(p);
814
815         p = prev;
816     }
817 }
818
819 static void
820 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
821                  // NB. for legacy reasons, p & q are swapped around :(
822 {
823     nat field;
824     StgInfoTable *info;
825     StgWord info_ptr;
826     StgClosure *selectee;
827     StgSelector *prev_thunk_selector;
828     bdescr *bd;
829     StgClosure *val;
830     
831     prev_thunk_selector = NULL;
832     // this is a chain of THUNK_SELECTORs that we are going to update
833     // to point to the value of the current THUNK_SELECTOR.  Each
834     // closure on the chain is a BLACKHOLE, and points to the next in the
835     // chain with payload[0].
836
837 selector_chain:
838
839     bd = Bdescr((StgPtr)p);
840     if (HEAP_ALLOCED_GC(p)) {
841         // If the THUNK_SELECTOR is in to-space or in a generation that we
842         // are not collecting, then bale out early.  We won't be able to
843         // save any space in any case, and updating with an indirection is
844         // trickier in a non-collected gen: we would have to update the
845         // mutable list.
846         if (bd->flags & BF_EVACUATED) {
847             unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
848             *q = (StgClosure *)p;
849             // shortcut, behave as for:  if (evac) evacuate(q);
850             if (evac && bd->step < gct->evac_step) {
851                 gct->failed_to_evac = rtsTrue;
852                 TICK_GC_FAILED_PROMOTION();
853             }
854             return;
855         }
856         // we don't update THUNK_SELECTORS in the compacted
857         // generation, because compaction does not remove the INDs
858         // that result, this causes confusion later
859         // (scavenge_mark_stack doesn't deal with IND).  BEWARE!  This
860         // bit is very tricky to get right.  If you make changes
861         // around here, test by compiling stage 3 with +RTS -c -RTS.
862         if (bd->flags & BF_MARKED) {
863             // must call evacuate() to mark this closure if evac==rtsTrue
864             *q = (StgClosure *)p;
865             if (evac) evacuate(q);
866             unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
867             return;
868         }
869     }
870
871
872     // BLACKHOLE the selector thunk, since it is now under evaluation.
873     // This is important to stop us going into an infinite loop if
874     // this selector thunk eventually refers to itself.
875 #if defined(THREADED_RTS)
876     // In threaded mode, we'll use WHITEHOLE to lock the selector
877     // thunk while we evaluate it.
878     {
879         do {
880             info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
881         } while (info_ptr == (W_)&stg_WHITEHOLE_info);
882
883         // make sure someone else didn't get here first...
884         if (IS_FORWARDING_PTR(p) || 
885             INFO_PTR_TO_STRUCT(info_ptr)->type != THUNK_SELECTOR) {
886             // v. tricky now.  The THUNK_SELECTOR has been evacuated
887             // by another thread, and is now either a forwarding ptr or IND.
888             // We need to extract ourselves from the current situation
889             // as cleanly as possible.
890             //   - unlock the closure
891             //   - update *q, we may have done *some* evaluation
892             //   - if evac, we need to call evacuate(), because we
893             //     need the write-barrier stuff.
894             //   - undo the chain we've built to point to p.
895             SET_INFO(p, (const StgInfoTable *)info_ptr);
896             *q = (StgClosure *)p;
897             if (evac) evacuate(q);
898             unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
899             return;
900         }
901     }
902 #else
903     // Save the real info pointer (NOTE: not the same as get_itbl()).
904     info_ptr = (StgWord)p->header.info;
905     SET_INFO(p,&stg_BLACKHOLE_info);
906 #endif
907
908     field = INFO_PTR_TO_STRUCT(info_ptr)->layout.selector_offset;
909
910     // The selectee might be a constructor closure,
911     // so we untag the pointer.
912     selectee = UNTAG_CLOSURE(p->selectee);
913
914 selector_loop:
915     // selectee now points to the closure that we're trying to select
916     // a field from.  It may or may not be in to-space: we try not to
917     // end up in to-space, but it's impractical to avoid it in
918     // general.  The compacting GC scatters to-space pointers in
919     // from-space during marking, for example.  We rely on the property
920     // that evacuate() doesn't mind if it gets passed a to-space pointer.
921
922     info = (StgInfoTable*)selectee->header.info;
923
924     if (IS_FORWARDING_PTR(info)) {
925         // We don't follow pointers into to-space; the constructor
926         // has already been evacuated, so we won't save any space
927         // leaks by evaluating this selector thunk anyhow.
928         goto bale_out;
929     }
930
931     info = INFO_PTR_TO_STRUCT(info);
932     switch (info->type) {
933       case WHITEHOLE:
934           goto bale_out; // about to be evacuated by another thread (or a loop).
935         
936       case CONSTR:
937       case CONSTR_1_0:
938       case CONSTR_0_1:
939       case CONSTR_2_0:
940       case CONSTR_1_1:
941       case CONSTR_0_2:
942       case CONSTR_STATIC:
943       case CONSTR_NOCAF_STATIC:
944           {
945               // check that the size is in range 
946               ASSERT(field <  (StgWord32)(info->layout.payload.ptrs + 
947                                           info->layout.payload.nptrs));
948           
949               // Select the right field from the constructor
950               val = selectee->payload[field];
951               
952 #ifdef PROFILING
953               // For the purposes of LDV profiling, we have destroyed
954               // the original selector thunk, p.
955               SET_INFO(p, (StgInfoTable *)info_ptr);
956               LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC((StgClosure *)p);
957 #if defined(THREADED_RTS)
958               SET_INFO(p, &stg_WHITEHOLE_info);
959 #else
960               SET_INFO(p, &stg_BLACKHOLE_info);
961 #endif
962 #endif
963
964               // the closure in val is now the "value" of the
965               // THUNK_SELECTOR in p.  However, val may itself be a
966               // THUNK_SELECTOR, in which case we want to continue
967               // evaluating until we find the real value, and then
968               // update the whole chain to point to the value.
969           val_loop:
970               info_ptr = (StgWord)UNTAG_CLOSURE(val)->header.info;
971               if (!IS_FORWARDING_PTR(info_ptr))
972               {
973                   info = INFO_PTR_TO_STRUCT(info_ptr);
974                   switch (info->type) {
975                   case IND:
976                   case IND_PERM:
977                   case IND_OLDGEN:
978                   case IND_OLDGEN_PERM:
979                   case IND_STATIC:
980                       val = ((StgInd *)val)->indirectee;
981                       goto val_loop;
982                   case THUNK_SELECTOR:
983                       ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
984                       prev_thunk_selector = p;
985                       p = (StgSelector*)val;
986                       goto selector_chain;
987                   default:
988                       break;
989                   }
990               }
991               ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
992               prev_thunk_selector = p;
993
994               *q = val;
995
996               // update the other selectors in the chain *before*
997               // evacuating the value.  This is necessary in the case
998               // where the value turns out to be one of the selectors
999               // in the chain (i.e. we have a loop), and evacuating it
1000               // would corrupt the chain.
1001               unchain_thunk_selectors(prev_thunk_selector, val);
1002
1003               // evacuate() cannot recurse through
1004               // eval_thunk_selector(), because we know val is not
1005               // a THUNK_SELECTOR.
1006               if (evac) evacuate(q);
1007               return;
1008           }
1009
1010       case IND:
1011       case IND_PERM:
1012       case IND_OLDGEN:
1013       case IND_OLDGEN_PERM:
1014       case IND_STATIC:
1015           // Again, we might need to untag a constructor.
1016           selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
1017           goto selector_loop;
1018
1019       case THUNK_SELECTOR:
1020       {
1021           StgClosure *val;
1022
1023           // recursively evaluate this selector.  We don't want to
1024           // recurse indefinitely, so we impose a depth bound.
1025           if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
1026               goto bale_out;
1027           }
1028
1029           gct->thunk_selector_depth++;
1030           // rtsFalse says "don't evacuate the result".  It will,
1031           // however, update any THUNK_SELECTORs that are evaluated
1032           // along the way.
1033           eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
1034           gct->thunk_selector_depth--;
1035
1036           // did we actually manage to evaluate it?
1037           if (val == selectee) goto bale_out;
1038
1039           // Of course this pointer might be tagged...
1040           selectee = UNTAG_CLOSURE(val);
1041           goto selector_loop;
1042       }
1043
1044       case AP:
1045       case AP_STACK:
1046       case THUNK:
1047       case THUNK_1_0:
1048       case THUNK_0_1:
1049       case THUNK_2_0:
1050       case THUNK_1_1:
1051       case THUNK_0_2:
1052       case THUNK_STATIC:
1053       case CAF_BLACKHOLE:
1054       case BLACKHOLE:
1055           // not evaluated yet 
1056           goto bale_out;
1057     
1058       default:
1059         barf("eval_thunk_selector: strange selectee %d",
1060              (int)(info->type));
1061     }
1062
1063 bale_out:
1064     // We didn't manage to evaluate this thunk; restore the old info
1065     // pointer.  But don't forget: we still need to evacuate the thunk itself.
1066     SET_INFO(p, (const StgInfoTable *)info_ptr);
1067     // THREADED_RTS: we just unlocked the thunk, so another thread
1068     // might get in and update it.  copy() will lock it again and
1069     // check whether it was updated in the meantime.
1070     *q = (StgClosure *)p;
1071     if (evac) {
1072         copy(q,(const StgInfoTable *)info_ptr,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->step->to);
1073     }
1074     unchain_thunk_selectors(prev_thunk_selector, *q);
1075     return;
1076 }