1 / 26

Improvements to the Compiler

Improvements to the Compiler. Lecture 27 Mon, Apr 26, 2004. Overflowing the Stack. Each expression leaves a value on the stack. That is because expressions are typically subexpressions of larger expressions. Their values are used in the larger expression. Example: n = n + 1;

jimbo
Download Presentation

Improvements to the Compiler

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Improvements to the Compiler Lecture 27 Mon, Apr 26, 2004

  2. Overflowing the Stack • Each expression leaves a value on the stack. • That is because expressions are typically subexpressions of larger expressions. • Their values are used in the larger expression. • Example: n = n + 1; • However, at some point, the value is no longer needed. • If we leave it on the stack, then eventually the stack will overflow.

  3. Example • This loop will fail after about 500,000 iterations. read n; d = 2; while (d < n) { if (n % d == 0) { n = n / d; print d; } else d = d + 1; }

  4. Excessive Pushing and Popping • Most expressions • Begin with a pop operation, and • End with a push operation. • In most cases, the value being pushed by one expression is popped immediately by the next expression, making the two operations unnecessary.

  5. Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax n = n + 1;

  6. Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

  7. Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

  8. Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

  9. Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

  10. Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

  11. Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

  12. Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax

  13. Example lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax

  14. Example lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax

  15. Example lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax

  16. Example lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax

  17. Example lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax mov (%eax),%edx mov $1,%eax add %edx,%eax pop %edx mov %eax,(%edx) push %eax

  18. Example lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax mov (%eax),%edx mov $1,%eax add %edx,%eax pop %edx mov %eax,(%edx) push %eax

  19. Eliminating Dead Code • Code that cannot be reached by any logical path is called dead code. • Dead code can be detected by looking for unlabeled statements following unconditional jumps. • Such statements are unreachable.

  20. Eliminating Dead Code • Make one pass of the assembly language program, compiling a list of all labels that are referenced. • Make a second pass, removing all unreferenced labels. • Make a third pass removing all code occurring between unconditional jumps and the next label.

  21. Example L1: # Code for ID lea 8(%ebp),%eax push %eax # Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret # Code for LABEL L2: mov %ebp,%esp pop %ebp ret int copy(int a) { return a; }

  22. Remove Unreferenced Labels L1: # Code for ID lea 8(%ebp),%eax push %eax # Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret # Code for LABEL L2: mov %ebp,%esp pop %ebp ret int copy(int a) { return a; }

  23. Remove Unreferenced Labels L1: # Code for ID lea 8(%ebp),%eax push %eax # Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret # Code for LABEL mov %ebp,%esp pop %ebp ret int copy(int a) { return a; }

  24. Find Unconditional Branches L1: # Code for ID lea 8(%ebp),%eax push %eax # Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret # Code for LABEL mov %ebp,%esp pop %ebp ret int copy(int a) { return a; }

  25. Remove Unreachable Code L1: # Code for ID lea 8(%ebp),%eax push %eax # Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret # Code for LABEL mov %ebp,%esp pop %ebp ret int copy(int a) { return a; }

  26. Remove Unreachable Code L1: # Code for ID lea 8(%ebp),%eax push %eax # Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret int copy(int a) { return a; }

More Related