This file is indexed.

/usr/share/fpcsrc/2.6.2/compiler/aoptcs.pas is in fpc-source-2.6.2 2.6.2-8.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
{
    Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
    Development Team

    This unit contains the common subexpression elimination object of the
    assembler optimizer.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************
}
unit aoptcs;

interface

uses aasm, aoptcpu, aoptobj;

{ ************************************************************************* }
{ info about the equivalence of registers when comparing two code sequences }
{ ************************************************************************* }

  TRegInfo = Object(TAoptBaseCpu)
    { registers encountered in the new and old sequence }
    NewRegsEncountered, OldRegsEncountered,
    { registers which only have been loaded for use as base or index in a }
    { reference later on                                                  }
    RegsLoadedForRef: TRegSet;
    { to which register in the old sequence corresponds every register in }
    { the new sequence                                                    }
    New2OldReg: TRegArray;

    Constructor init;
    { clear all information store in the object }
    Procedure Clear;
    { the contents of OldReg in the old sequence are now being loaded into }
    { NewReg in the new sequence                                           }
    Procedure AddReg(OldReg, NewReg: TRegister); Virtual;
    { the contents of OldOp in the old sequence are now being loaded into }
    { NewOp in the new sequence. It is assumed that OldOp and NewOp are   }
    { equivalent                                                          }
    Procedure AddOp(const OldOp, NewOp:Toper);
    { check if a register in the old sequence (OldReg) can be equivalent to }
    { a register in the new sequence (NewReg) if the operation OpAct is     }
    { performed on it. The RegInfo is updated (not necessary to call AddReg }
    { afterwards)                                                           }
    Function RegsEquivalent(OldReg, NewReg: TRegister; OpAct: TopAction):
      Boolean;
    { check if a reference in the old sequence (OldRef) can be equivalent   }
    { to a reference in the new sequence (NewRef) if the operation OpAct is }
    { performed on it. The RegInfo is updated (not necessary to call AddOp  }
    { afterwards)                                                           }
    Function RefsEquivalent(Const OldRef, NewRef: TReference; OpAct:
      TOpAction): Boolean;
    { check if an operand in the old sequence (OldOp) can be equivalent to }
    { an operand in the new sequence (NewOp) if the operation OpAct is     }
    { performed on it. The RegInfo is updated (not necessary to call AddOp }
    { afterwards)                                                          }
    Function OpsEquivalent(const OldOp, NewOp: toper; OpAct: TopAction):
      Boolean;
    { check if an instruction in the old sequence (OldP) can be equivalent  }
    { to an instruction in the new sequence (Newp). The RegInfo is updated  }
    Function InstructionsEquivalent(OldP, NewP: Pai): Boolean;
  End;


{ ************************************************************************* }
{ *************** The common subexpression elimination object ************* }
{ ************************************************************************* }

Type TAoptCSE = Object(TAoptObj)
       { returns true if the instruction p1 modifies the register Reg }
       Function RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
     End;

Implementation

{ ************************************************************************* }
{ ******************************* TReginfo ******************************** }
{ ************************************************************************* }

Constructor TRegInfo.Init;
Begin
  Clear;
End;

Procedure TRegInfo.Clear;
Begin
  RegsLoadedForRef   := [];
  NewRegsEncountered := [FRAME_POINTER_REG, STACK_POINTER_REG];
  OldRegsEncountered := [FRAME_POINTER_REG, STACK_POINTER_REG];
  New2OldReg[FRAME_POINTER_REG] := FRAME_POINTER_REG;
  New2OldReg[STACK_POINTER_REG] := STACK_POINTER_REG;
End;

Procedure TRegInfo.AddReg(OldReg, NewReg: TRegister);
{ updates the ???RegsEncountered and ???2???Reg fields of RegInfo. Assumes  }
{ that OldReg and NewReg have the same size (has to be chcked in advance    }
{ with RegsSameSize) and that neither equals R_NO                           }
{ has to be overridden for architectures like the 80x86 when not all GP     }
{ regs are of the same size                                                 }
Begin
  NewRegsEncountered := NewRegsEncountered + [NewReg];
  OldRegsEncountered := OldRegsEncountered + [OldReg];
  New2OldReg[NewReg] := OldReg;
End;

Procedure TRegInfo.AddOp(const OldOp, NewOp:Toper);
Begin
  Case OldOp.typ Of
    Top_Reg:
      If (OldOp.reg <> R_NO) Then
        AddReg(OldOp.reg, NewOp.reg);
    Top_Ref:
      Begin
        If OldOp.ref^.base <> R_NO Then
          AddReg(OldOp.ref^.base, NewOp.ref^.base);
{$ifdef RefsHaveIndexReg}
        If OldOp.ref^.index <> R_NO Then
          AddReg(OldOp.ref^.index, NewOp.ref^.index);
{$endif RefsHaveIndexReg}
      End;
  End;
End;

Function TRegInfo.RegsEquivalent(OldReg, NewReg: TRegister;
           OPAct: TOpAction): Boolean;
Begin
  If Not((OldReg = R_NO) Or (NewReg = R_NO)) Then
    If RegsSameSize(OldReg, NewReg) Then
{ here we always check for the 32 bit component, because it is possible    }
{ that the 8 bit component has not been set, event though NewReg already   }
{ has been processed. This happens if it has been compared with a register }
{ that doesn't have an 8 bit component (such as EDI). In that case the 8   }
{ bit component is still set to R_NO and the comparison in the Else-part   }
{ will fail                                                                }
      If (RegMaxSize(OldReg) in OldRegsEncountered) Then
        If (RegMaxSize(NewReg) in NewRegsEncountered) Then
          RegsEquivalent := (OldReg = New2OldReg[NewReg])
{ If we haven't encountered the new register yet, but we have encountered }
{ the old one already, the new one can only be correct if it's being      }
{ written to (and consequently the old one is also being written to),     }
{ otherwise                                                               }
{                                                                         }
{  movl -8(%ebp), %eax        and         movl -8(%ebp), %eax             }
{  movl (%eax), %eax                      movl (%edx), %edx               }
{                                                                         }
{  are considered equivalent                                              }
        Else
          If (OpAct = OpAct_Write) Then
            Begin
              AddReg(OldReg, NewReg);
              RegsEquivalent := True
            End
          Else Regsequivalent := False
      Else
        If Not(RegMaxSize(NewReg) in NewRegsEncountered) Then
          Begin
            AddReg(OldReg, NewReg);
            RegsEquivalent := True
          End
        Else RegsEquivalent := False
    Else RegsEquivalent := False
  Else RegsEquivalent := OldReg = NewReg
End;

Function TRegInfo.RefsEquivalent(Const OldRef, NewRef: TReference;
           OpAct: TOpAction): Boolean;
Begin
  If OldRef.is_immediate Then
    RefsEquivalent := NewRef.is_immediate and (OldRef.Offset = NewRef.Offset)
  Else
    RefsEquivalent := (OldRef.Offset+OldRef.OffsetFixup =
                         NewRef.Offset+NewRef.OffsetFixup) And
                      RegsEquivalent(OldRef.Base, NewRef.Base, OpAct)
{$ifdef RefsHaveindexReg}
                      And RegsEquivalent(OldRef.Index, NewRef.Index, OpAct)
{$endif RefsHaveIndexReg}
{$ifdef RefsHaveScale}
                      And (OldRef.ScaleFactor = NewRef.ScaleFactor)
{$endif RefsHaveScale}
                      And (OldRef.Symbol = NewRef.Symbol)
{$ifdef RefsHaveSegment}
                      And (OldRef.Segment = NewRef.Segment)
{$endif RefsHaveSegment}
                      ;
End;

Function TRegInfo.OpsEquivalent(const OldOp, NewOp: toper; OpAct: TopAction):
           Boolean;
Begin
  OpsEquivalent := False;
  if OldOp.typ=NewOp.typ then
    Case OldOp.typ Of
      Top_Const: OpsEquivalent := OldOp.val = NewOp.val;
      Top_Reg: OpsEquivalent := RegsEquivalent(OldOp.reg,NewOp.reg, OpAct);
      Top_Ref: OpsEquivalent := RefsEquivalent(OldOp.ref^, NewOp.ref^, OpAct);
      Top_None: OpsEquivalent := True
    End;
End;

Function TRegInfo.InstructionsEquivalent(OldP, NewP: Pai): Boolean;

  Function OperandTypesEqual: Boolean;
  Var Count: AWord;
  Begin
    OperandTypesEqual := False;
    For Count := 0 to max_operands-1 Do
      If (PInstr(OldP)^.oper[Count].typ <> PInstr(NewP)^.oper[Count].typ) Then
        Exit;
    OperandTypesEqual := True
  End;

Var Count: AWord;
    TmpResult: Boolean;
Begin
  If Assigned(OldP) And Assigned(NewP) And
     (Pai(OldP)^.typ = ait_instruction) And
     (Pai(NewP)^.typ = ait_instruction) And
     (PInstr(OldP)^.opcode = PInstr(NewP)^.opcode) And
     OperandTypesEqual
    Then
{ both instructions have the same structure:                }
{ "<operator> <operand of type1>, <operand of type 2>, ..." }
      If IsLoadMemReg(OldP) Then
{ then also NewP = loadmemreg because of the previous check }
        If Not(RegInRef(PInstr(OldP)^.oper[LoadDst].reg,
                 PInstr(OldP)^.oper[LoadSrc].ref^)) Then
{ the "old" instruction is a load of a register with a new value, not with }
{ a value based on the contents of this register (so no "mov (reg), reg")  }
          If Not(RegInRef(PInstr(NewP)^.oper[LoadDst].reg,
                          PInstr(NewP)^.oper[LoadSrc].ref^)) And
             RefsEqual(PInstr(OldP)^.oper[LoadSrc].ref^,
                       PInstr(NewP)^.oper[LoadSrc].ref^)
            Then
{ the "new" instruction is also a load of a register with a new value, and }
{ this value is fetched from the same memory location                      }
              Begin
                With PInstr(NewP)^.oper[LoadSrc].ref^ Do
                  Begin
                    If Not(Base in [ProcInfo.FramePointer, R_NO, STACK_POINTER_REG])
{ it won't do any harm if the register is already in RegsLoadedForRef }
                      Then RegsLoadedForRef := RegsLoadedForRef + [Base];
{$ifdef RefsHaveIndexReg}
                    If Not(Index in [ProcInfo.FramePointer, R_NO, STACK_POINTER_REG])
                      Then RegsLoadedForRef := RegsLoadedForRef + [Index];
{$endif RefsHaveIndexReg}
                  End;
{ add the registers from the reference (.oper[Src]) to the RegInfo, all }
{ registers from the reference are the same in the old and in the new   }
{ instruction sequence (refsequal returned true)                        }
                AddOp(PInstr(OldP)^.oper[LoadSrc], PInstr(OldP)^.oper[LoadSrc]);
{ the registers from .oper[Dest] have to be equivalent, but not necessarily }
{ equal                                                                     }
                InstructionsEquivalent :=
                  RegsEquivalent(PInstr(OldP)^.oper[LoadDst].reg,
                                 PInstr(NewP)^.oper[LoadDst].reg, OpAct_Write);
              End
{ the registers are loaded with values from different memory locations. If }
{ this were allowed, the instructions "mov -4(%esi),%eax" and              }
{  "mov -4(%ebp),%eax" would be considered equivalent                      }
            Else InstructionsEquivalent := False
        Else
{ load register with a value based on the current value of this register }
          Begin
            With PInstr(NewP)^.oper[0].ref^ Do
{ Assume the registers occurring in the reference have only been loaded with }
{ the value they contain now to calculate an address (so the value they have }
{ now, won't be stored to memory later on)                                   }
              Begin
                If Not(Base in [ProcInfo.FramePointer,
                                RegMaxSize(PInstr(NewP)^.oper[LoadDst].reg),
                                R_NO,STACK_POINTER_REG])
{ It won't do any harm if the register is already in RegsLoadedForRef }
                  Then
                    Begin
                      RegsLoadedForRef := RegsLoadedForRef + [Base];
{$ifdef csdebug}
                      Writeln(std_reg2str[base], ' added');
{$endif csdebug}
                    end;
{$Ifdef RefsHaveIndexReg}
                If Not(Index in [ProcInfo.FramePointer,
                                 RegMaxSize(PInstr(NewP)^.oper[LoadDst].reg),
                                 R_NO,StackPtr])
                  Then
                    Begin
                      RegsLoadedForRef := RegsLoadedForRef + [Index];
{$ifdef csdebug}
                      Writeln(std_reg2str[index], ' added');
{$endif csdebug}
                    end;
{$endif RefsHaveIndexReg}
              End;

{ now, remove the destination register of the load from the                 }
{ RegsLoadedForReg, since if it's loaded with a new value, it certainly     }
{ will still be used later on                                               }
            If Not(RegMaxSize(PInstr(NewP)^.oper[LoadDst].reg) In
                [ProcInfo.FramePointer,R_NO,STACK_POINTER_REG])
              Then
                Begin
                  RegsLoadedForRef := RegsLoadedForRef -
                    [RegMaxSize(PInstr(NewP)^.oper[LoadDst].reg)];
{$ifdef csdebug}
                  Writeln(std_reg2str[RegMaxSize(PInstr(NewP)^.oper[1].reg)], ' removed');
{$endif csdebug}
                end;
            InstructionsEquivalent :=
               OpsEquivalent(PInstr(OldP)^.oper[LoadSrc],
                             PInstr(NewP)^.oper[LoadSrc], OpAct_Read) And
               OpsEquivalent(PInstr(OldP)^.oper[LoadDst],
                             PInstr(NewP)^.oper[LoadDst], OpAct_Write)
          End
      Else
{ OldP and NewP are not a load instruction, but have the same structure }
{ (opcode, operand types), so they're equivalent if all operands are    }
{ equivalent                                                            }
       Begin
         Count := 0;
         TmpResult := true;
         Repeat
           TmpResult :=
             OpsEquivalent(PInstr(OldP)^.oper[Count], PInstr(NewP)^.oper[Count],
                           OpAct_Unknown);
           Inc(Count)
         Until (Count = MaxOps) or not(TmpResult);
         InstructionsEquivalent := TmpResult
       End
{ the instructions haven't even got the same structure, so they're certainly }
{ not equivalent                                                             }
    Else InstructionsEquivalent := False;
End;


Function TRegInfo.CheckSequence(p: Pai; Reg: TRegister; Var Found: Longint):
           Boolean;
{checks whether the current instruction sequence (starting with p) and the
 one between StartMod and EndMod of Reg are the same. If so, the number of
 instructions that match is stored in Found and true is returned, otherwise
 Found holds the number of instructions between StartMod and EndMod and false
 is returned}

{ note: the NrOfMods field can hold two deifferent values depending on      }
{ which instruction it belongs to:                                          }
{   * if it is the first instruction of a sequence that describes the       }
{     contents of a register, NrOfMods contains how many instructions are   }
{      in the sequence                                                      }
{   * otherwise, NrOfMods contains how many instructions are in the         }
{     describing the contents of the register after the current instruction }
{     has been executed                                                     }

Var oldp, newp: Pai;
    PrevNonRemovablePai: Pai;
    OrgRegInfo, HighRegInfo: PRegInfo;
    HighFound, OrgRegFound: Byte;
    RegCounter: TRegister;
    OrgRegResult: Boolean;
    TmpResult: Boolean;
    OldNrOfMods: Byte;
Begin {CheckSequence}
  Reg := RegMaxSize(Reg);
{ have we found a sequence of instructions equivalent to the new one? }
  TmpResult := False;
{ HighRegInfo will contain the RegInfo for the longest sequence of matching }
{ instructions found                                                        }
  New(HighRegInfo, Init);
{ how many instructions are in the sequence describing the content of Reg }
{ (the parameter) in the old sequence                                     }
  OrgRegFound := 0;
{ how many instructions are in the longest sequence of matching }
{ instructions found until now?                                 }
  HighFound := 0;
{ does the content of Reg in the old equence match the content of Reg in }
{ the new sequence                                                       }
  OrgRegResult := False;
  RegCounter := LoGPReg;
{ PrevNonRemovablePai's OptInfo contains the contents of the registers   }
{ before the current instruction is executed. It will be used to compare }
{ the new contents with and to see whether the new instructions can be   }
{ removed                                                                }
  GetLastInstruction(p, PrevNonRemovablePai);
{ don't check registers that only contain a constant or something unknown }
  While (RegCounter <= HiGPReg And
        (PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].Typ <> Con_Ref) Do
    Inc(RegCounter);
  While (RegCounter <= HiGPReg) Do
    Begin
      { reinitialize the reginfo fields }
      Init;
      { no matching instructions found yet }
      Found := 0;
      With PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter] Do
        Begin
          { get the first instruction that describes the content of the }
          { the register we're going to check the way it was before the }
          { current instruction got executed                            }
          oldp := StartMod;
          { how many instructions describe the content of the register }
          { before the current instructions got executed?              }
          OldNrOfMods := NrOfMods
        End;
      { p is the first instruction that describes the content of Reg }
      { after p (= the current instruction) got executed             }
      newp := p;
      { it's possible that the old contents of the current register are   }
      { described by a sequence of instructions that also contains the    }
      { one in parameter p. In that case, we have to compare until we     }
      { encounter p. Otherwise, compare as much instructions as there are }
      { in the old sequence or until there's a mismatch                   }
      While  (p <> oldp) And
             (Found < OldNrOfMods) And
                                  { old  new }
             InstructionsEquivalent(oldp, newp, RegInfo) Do
        Begin
          GetNextInstruction(oldp, oldp);
          GetNextInstruction(newp, newp);
          Inc(Found)
        End;
      If (Found < OldNrOfMods) Then
        Begin
          { the old sequence was longer than than the new one, so no match }
          TmpResult := False;
          { If there is no match, we have to set the CanBeRemoved flag of   }
          { all pai objects part of the new sequence to false, because it's }
          { possible that some of them have already been scheduled for      }
          { removal after checking another sequence (an instruction can be  }
          { of more than one sequence). If we return false, the number      }
          { returned in found denotes how many instructions have to have    }
          { their CanBeRemoved flag set to false                            }
          { We only have to set those flags to false if their was a partial }
          { match of instructions (found > 0), because otherwise they can't }
          { have been set to true in a previous comparison                  }
          If (found > 0) Then
            Found := PPaiProp(Pai(p)^.OptInfo)^.Regs[Reg].NrOfMods
        End
      Else TmpResult := True;
      If (RegCounter = Reg) Then
        Begin
          OrgRegFound := Found;
          OrgRegResult := TmpResult;
          New(OrgRegInfo, InitWithValue(RegInfo));
        End
      Else
        If TmpResult And
           (Found > HighFound) Then
          Begin
            HighFound := Found;
            HighRegInfo^.InitWithValue(RegInfo);
          End;
      RegInfo.Done;
      Repeat
        Inc(RegCounter);
      Until (RegCounter > HiGPReg) or
            (PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].Typ =
              Con_Ref);
    End;
  If (HighFound > 0) And
     (Not(OrgRegResult) Or
      (HighFound > OrgRegFound)) Then
    Begin
      CheckSequence := True;
      Found := HighFound
      InitWithValue(HighRegInfo);
    End
  Else
    Begin
      CheckSequence := OrgRegResult;
      Found := OrgRegFound;
      InitWithValue(OrgRegInfo);
    End;
    Dispose(HighRegInfo, Done);
    Dispose(OrgRegInfo, Done)
End; {CheckSequence}


{ ************************************************************************* }
{ ******************************* TAOptCSE ******************************** }
{ ************************************************************************* }


Function TAOptCSE.RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
Var hp: Pai;
Begin
  If GetLastInstruction(p1, hp)
    Then
      RegModifiedByInstruction :=
        PPAiProp(p1^.OptInfo)^.GetWState <>
          PPAiProp(hp^.OptInfo)^.GetWState
    Else RegModifiedByInstruction := True;
End;

Procedure TAoptCSE.RestoreContents(Current: Pai; Reg: TRegister);
Var Prev, hp3, hp5: Pai;
    TmpState: TStateInt;
    Cnt, Cnt2: Byte;
Begin
{ load Cnt2 with the total number of instructions of this sequence }
  Cnt2 := PPaiProp(Prev^.OptInfo)^.Regs[RegInfo.New2OldReg[reg]].
    NrOfMods;
{ sometimes, a register can not be removed from a sequence, because it's }
{ still used afterwards:                                                 }
{                                                                        }
{ movl    -8(%ebp), %eax                        movl    -8(%ebp), %eax   }
{ movl    70(%eax), %eax                        movl    70(%eax), %eax   }
{ cmpl    74(%eax), %eax                        cmpl    74(%eax), %eax   }
{ jne     l1               can't be changed to  jne     l1               }
{ movl    -8(%ebp), %eax                                                 }
{ movl    70(%eax), %edi                        movl    %eax, %edi       }
{ boundl  R_282, %edi                           boundl  R_282, %edi      }
{ pushl   70(%eax)                              pushl   70(%eax)         }
{                                                                        }
{ because eax now contains the wrong value when 70(%eax) is pushed       }

{ start at the first instruction of the sequence }
  hp3 := Current;
  For Cnt := 1 to Pred(Cnt2) Do
    GetNextInstruction(hp3, hp3);
{ hp3 now containts the last instruction of the sequence }
{ get the writestate at this point of the register in TmpState }
  TmpState := PPaiProp(hp3^.OptInfo)^.GetWState(reg);
{ hp3 := first instruction after the sequence }
  GetNextInstruction(hp3, hp3);

{ now, even though reg is in RegsLoadedForRef, sometimes it's still used  }
{ afterwards. It is not if either it is not in usedregs anymore after the }
{ sequence, or if it is loaded with a new value right after the sequence  }
  If (TmpState <> PPaiProp(hp3^.OptInfo)^.Regs[reg].WState) Or
     Not(reg in PPaiProp(hp3^.OptInfo)^.UsedRegs) Then
{ the register is not used anymore after the sequence! }
    Begin
{$ifdef csdebug}
      Writeln('Cnt2: ',Cnt2);
      hp5 := new(pai_asm_comment,init(strpnew('starting here...')));
      InsertLLItem(Pai(Current^.previous), Current, hp5);
{$endif csdebug}
      hp3 := Current;
{ first change the contents of the register inside the sequence }
      For Cnt := 1 to Cnt2 Do
        Begin
 {save the WState of the last pai object of the sequence for later use}
          TmpState := PPaiProp(hp3^.OptInfo)^.Regs[reg].WState;
{$ifdef csdebug}
          hp5 := new(pai_asm_comment,init(strpnew('WState for '+
            std_reg2str[reg]+': '+tostr(tmpstate))));
          InsertLLItem(hp3, pai(hp3^.next), hp5);
{$endif csdebug}
          PPaiProp(hp3^.OptInfo)^.Regs[reg] :=
            PPaiProp(Prev^.OptInfo)^.Regs[reg];
          GetNextInstruction(hp3, hp3);
        End;
{ here, hp3 = p = Pai object right after the sequence, TmpState = WState of }
{ reg at the last Pai object of the sequence                                }
      GetLastInstruction(hp3, hp3);
{ now, as long as the register isn't modified after the sequence, set its }
{ contents to what they were before the sequence                          }
      While GetNextInstruction(hp3, hp3) And
            (PPaiProp(hp3^.OptInfo)^.GetWState(Reg) = TmpState) Do
{$ifdef csdebug}
        begin
          hp5 := new(pai_asm_comment,init(strpnew('WState for '+std_reg2str[reg]+': '+
                 tostr(PPaiProp(hp3^.OptInfo)^.GetWState(reg)))));
             InsertLLItem(hp3, pai(hp3^.next), hp5);
{$endif csdebug}
          PPaiProp(hp3^.OptInfo)^.Regs[reg] :=
            PPaiProp(Prev^.OptInfo)^.Regs[reg];
{$ifdef csdebug}
        end;
{$endif csdebug}
    End
  Else
{ the register is still used after the sequence, so undelete all }
{ instructions in the sequence that modify reg                   }
    Begin
{$ifdef csdebug}
      Writeln('Got there for ',std_reg2str[reg]);
{$endif csdebug}
      hp3 := Current;
      For Cnt := 1 to Cnt2 Do
        Begin
          If RegModifiedByInstruction(reg, hp3) Then
            PPaiProp(hp3^.OptInfo)^.CanBeRemoved := False;
          GetNextInstruction(hp3, hp3);
        End;
    End;
{$ifdef csdebug}
  hp5 := new(pai_asm_comment,init(strpnew('stopping here...')));
  InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
{$endif csdebug}
End;

Procedure TAoptCSE.DoCSE;
{marks the instructions that can be removed by RemoveInstructs. They're not
 removed immediately because sometimes an instruction needs to be checked in
 two different sequences}
Var Cnt, Cnt2: Longint;
    p, hp1, Current: Pai;
    hp3, Prev: Pai;
{$ifdef csdebug}
    hp5: pai;
{$endif csdebug}
    RegInfo: TRegInfo;
    RegCounter: TRegister;
    TmpState: Byte;
Begin
  p := SkipHead(BlockStart);
  While (p <> BlockEnd) Do
    Begin
      Case p^.typ Of
        ait_instruction:
          Begin
{            Case PInstr(p)^.opcode Of
              A_CLD: If GetLastInstruction(p, hp1) And
                        (PPaiProp(hp1^.OptInfo)^.DirFlag = F_NotSet) Then
                       PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;}
              If IsLoadMemReg(p) Then
                Begin
                  If (p = PPaiProp(p^.OptInfo)^.Regs[RegMaxSize(
                       PInstr(p)^.oper[LoadDst].reg)].StartMod) And
                     GetLastInstruction (p, hp1) And
                     (hp1^.typ <> ait_marker) Then
{so we don't try to check a sequence when p is the first instruction of the block}
                    If CheckSequence(p, PInstr(p)^.oper[LoadDst].reg, Cnt) And
                       (Cnt > 0) Then
                      Begin
                        hp1 := nil;
{ although it's perfectly ok to remove an instruction which doesn't contain }
{ the register that we've just checked (CheckSequence takes care of that),  }
{   the sequence containing this other register should also be completely   }
{   checked (and either removed or marked as non-removable), otherwise we   }
{ may get situations like this:                                             }
{                                                                           }
{     movl 12(%ebp), %edx                       movl 12(%ebp), %edx         }
{     movl 16(%ebp), %eax                       movl 16(%ebp), %eax         }
{     movl 8(%edx), %edx                        movl 8(%edx), %edx          }
{     movl (%eax), eax                          movl (%eax), eax            }
{     cmpl %eax, %edx                           cmpl %eax, %edx             }
{     jnz  l123           getting converted to  jnz  l123                   }
{     movl 12(%ebp), %edx                       movl 4(%eax), eax           }
{     movl 16(%ebp), %eax                                                   }
{     movl 8(%edx), %edx                                                    }
{     movl 4(%eax), eax                                                     }
                        Current := p;
                        Cnt2 := 1;
{ after this while loop, if hp1 <> nil it will contain the pai object }
{ that's the start of a sequence that's not completely checked yet    }
                        While Cnt2 <= Cnt Do
                          Begin
                            If (hp1 = nil) And
                               Not(RegInInstruction(
                                     PInstr(Current)^.oper[LoadDst].reg,p) Or
                                   RegInInstruction(RegMaxSize(PInstr(
                                     Current)^.oper[LoadDst].reg), p)) And
{ do not recheck a sequence if it's completely part of the one we just }
{ checked                                                              }
                               Not(IsLoadMemReg(p) And
                                   (PPaiProp(p^.OptInfo)^.Regs[RegMaxSize(
                                      PInstr(p)^.Oper[LoadDst].reg)]
                                      .NrOfMods <= (Cnt - Cnt2 + 1))) Then
                              hp1 := p;
{$ifndef noremove}
                            PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
{$endif noremove}
                            Inc(Cnt2);
                            GetNextInstruction(p, p);
                          End;
{ insert a marker noting that for the following instructions no PPaiProp's }
{ (containing optimizer info) have been generated, so GetNext/             }
{ LastInstruction will ignore them (it will use the original instructions) }
                        hp3 := New(Pai_Marker,Init(mark_NoPropInfoStart));
                        InsertLLItem(Pai(Current^.Previous), Current, hp3);
{ Prev is used to get the contents of the registers before the sequence }
                        GetLastInstruction(Current, Prev);
{ If some registers were different in the old and the new sequence, move }
{  the contents of those old registers to the new ones, e.g.             }
{                                                                        }
{   mov mem1, reg1                        mov mem1, reg1                 }
{   ...               can be changed to   ...                            }
{   mov mem1, reg2                        mov reg1, reg2                 }

{$IfDef CSDebug}
                        For RegCounter := LoGPReg To HiGPReg Do
                          If (RegCounter in RegInfo.RegsLoadedForRef) Then
                            Begin
                              hp5 := new(pai_asm_comment,init(strpnew(
                                'New: '+std_reg2str[RegCounter]+', Old: '+
                                std_reg2str[RegInfo.New2OldReg[RegCounter]])));
                              InsertLLItem(AsmL, Pai(Current^.previous), Current, hp5);
                            End;
{$EndIf CSDebug}
                        For RegCounter := LoGPReg to HiGPReg Do
                          Begin
{ if New2OldReg[RegCounter] = R_NO, it means this register doesn't appear }
{ the new nor the old sequence                                            }
                            If (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
{ if a register is in RegsLoadedForRef, it means this register was loaded }
{ with a value only to function as a base or index in a reference. The    }
{ practical upshot of this is that this value won't be used anymore later }
{ on, so even if another register was used in the new sequence for this,  }
{ we don't have to load it. E.g.                                          }
{                                                                         }
{ movl 8(%ebp), %eax                        "                             }
{ movl 4(%eax), %eax                        "                             }
{ movl (%eax), %edi                         "                             }
{ movl %edi, 12(%ebp)                       "                             }
{ ...                   can be changed to   "                             }
{ movl 8(%ebp), %edx                                                      }
{ movl 4(%edx), %edx                                                      }
{ movl (%edx), %ebx                         movl %edi, %ebx               }
{                                                                         }
{ There is no need to also add a "movl %eax, %edx"                        }
                              If Not(RegCounter In RegInfo.RegsLoadedForRef) And
                                             {old reg              new reg}
{ no need to reload the register if it's the same in the old and new }
{ sequence                                                           }
                                 (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then

                                Begin
                                  hp3 := a_load_reg_reg(
                                                 {old reg          new reg}
                                    RegInfo.New2OldReg[RegCounter], RegCounter));
                                  InsertLLItem(Pai(Current^.previous), Current, hp3);
                                End
                              Else
{ As noted before, if a register is in RegsLoadedForRef, it doesn't have  }
{ to be loaded. However, when data flow analyzer processed this code, the }
{ was loaded, so we need to change that. This is done by setting the      }
{ contents of the register to its contents before the new sequence, for   }
{ every instruction until the first load of the register with a new value }
                                If (RegCounter In RegInfo.RegsLoadedForRef) Then
                                  RestoreOrigContents(Current, RegCounter);

                          End;
{ the end of the area where instructions without optimizer info can occur }
                        hp3 := New(Pai_Marker,Init(mark_NoPropInfoEnd));
                        InsertLLItem(AsmL, Pai(Current^.Previous), Current, hp3);
{ if we found an instruction sequence that needs complete re-evaluation, }
{ process it                                                             }
                        If hp1 <> nil Then p := hp1;
                        Continue;
                      End
                    Else
{ checksequence returned false. In that case, if the current instruction }
{ was already deleted (as part of another sequence), we have to undelete }
{ all instructions pertaining to the register whose sequence we just     }
{ checked                                                                }
                      If (Cnt > 0) And
                         (PPaiProp(p^.OptInfo)^. Regs[RegMaxSize(PInstr(p)^.
                            oper[LoadDst].reg)].Typ = Con_Ref) And
                         (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
                        Begin
                          Current := p;
                          Cnt2 := 1;
                          While Cnt2 <= Cnt Do
                            Begin
                              If RegInInstruction(PInstr(Current)^.
                                   oper[LoadDst].reg, p) Or
                                 RegInInstruction(RegMaxSize(PInstr(Current)^.
                                   oper[LoadDst].reg), p) Then
                                PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
                              Inc(Cnt2);
                              GetNextInstruction(p, p);
                            End;
                          Continue;
                        End;
                End
              Else if IsLoadConstReg(p) Then
                Begin
                  If GetLastInstruction(p, hp1) Then
                    With PPaiProp(hp1^.OptInfo)^.Regs[
                           RegMaxSize(PInstr(p)^.oper[LoadDst].reg)] Do
                      If (Typ = Con_Const) And
                         (StartMod = p) Then
                        PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
                End
              Else
                CpuCSE(p);
{              A_STD: If GetLastInstruction(p, hp1) And
                        (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
                        PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
              A_XOR:
                Begin
                  If (Paicpu(p)^.oper[0].typ = top_reg) And
                     (Paicpu(p)^.oper[0].typ = top_reg) And
                     (Paicpu(p)^.oper[1].reg = Paicpu(p)^.oper[1].reg) And
                     GetLastInstruction(p, hp1) And
                     (PPaiProp(hp1^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)].typ = con_const) And
                     (PPaiProp(hp1^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)].StartMod = nil)
                    Then PPaiProp(p^.OptInfo)^.CanBeRemoved := True
                End
          End;
      End;
      GetNextInstruction(p, p);
    End;
End;

Procedure RemoveInstructs;
{Removes the marked instructions and disposes the PPaiProps of the other
 instructions, restoring their line number}
Var p, hp1: Pai;
    InstrCnt: Longint;
Begin
 p := SkipHead(BlockStart);
  InstrCnt := 1;
  While (p <> BlockEnd) Do
    Begin
{$ifndef noinstremove}
      If PPaiProp(p^.OptInfo)^.CanBeRemoved
        Then
          Begin
            Dispose(PPaiProp(p^.OptInfo));
            GetNextInstruction(p, hp1);
            AsmL^.Remove(p);
            Dispose(p, Done);
            p := hp1;
            Inc(InstrCnt);
          End
        Else
{$endif noinstremove}
          Begin
            Dispose(PPaiProp(p^.OptInfo));
            p^.OptInfo := nil;
            GetNextInstruction(p, p);
            Inc(InstrCnt);
          End;
    End;
End;

Procedure TAoptCSE.CSE;
Begin
  DoCSE;
  RemoveInstructs;
End;



End.