← Optimizing PowerPC Code: Programming the PowerPC Chip in Assembly Language

Optimizing PowerPC Code: Programming the PowerPC Chip in Assembly Language

From the originals

13 scans

Machine-transcribed text and image descriptions. Check the linked original for uncertain words or fine print.

These are transcribed pages from the documents, not the full publications. Open the original for the remaining pages.

Image 1

OPTIMIZING PowerPC™ CODE Programming the PowerPC Chip in Assembly Language Gary Kacmarcik

Book cover with a black background, a large dark circular area decorated with faint carved-style glyph and figure illustrations, the title in yellow and red capital letters at top, a white subtitle in the middle, and the author's name in yellow at the bottom.

Reading notes: The trademark symbol after PowerPC is small and rendered as TM; transcribed as ™.

Printed product names
PowerPC
Languages
English
PDF page 1

OPTIMIZING PowerPC™ CODE Programming the PowerPC Chip in Assembly Language Gary Kacmarcik

Book cover with a black background, a large dark circular medallion decorated with faint line-drawn glyph-like carvings, yellow serif title text, a large red bold italic 'PowerPC' with a trademark mark, white sans-serif subtitle, and yellow author name at the bottom.

Reading notes: The trademark symbol after PowerPC is rendered as a small TM in red and is transcribed as ™. Reading order follows title, subtitle, then author name at the bottom.

Printed product names
PowerPC
Languages
English
PDF page 2

Optimizing Power PC™ Code Programming the PowerPC Chip in Assembly Language Gary Kacmarcik .... TT Addison-Wesley Publishing Company Reading, Massachusetts • Menlo Park, California • New York Don Mills, Ontario • Wokingham, England • Amsterdam Bonn • Sydney • Singapore • Tokyo • Madrid • San Juan Paris • Seoul • Milan • Mexico City • Taipei

Printed product names
PowerPC
Languages
English
PDF page 3

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Addison-Wesley was aware of a trademark claim, the designations have been printed in initial capital letters or all capital letters. The authors and publishers have taken care in preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibil- ity for errors or omissions. No liability is assumed for incidental or consequen- tial damages in connection with or arising out of the use of the information or programs contained herein. Library of Congress Cataloging-in-Publication Data Kacmarcik, Gary. Optimizing PowerPC code I Gary Kacmarcik. p.cm. Includes index. ISBN 0-201-40839-2 1. PowerPC microprocessors. I. Title. QA76.8.P67K33 1995 005.265- dc20 Copyright© 1995 by Gary Kacmarcik 94-10489 CIP All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America. Published simultaneously in Canada. Sponsoring Editor: Martha Steffen Project Manager: Eleanor McCarthy Production Coordinator: Deborah McKenna Cover design: Barbara T. Atkinson Cover art© Michael D. Coe Set in 10 point Palatino by Michael D. Wile 12 3 4 5 6 7 8 9-MA-9998979695 First printing, April 1995 Addison-Wesley books are available for bulk purchases by corporations, institu- tions, and other organizations. For more information please contact the Corpo- rate, Government, and Special Sales Department at (800) 238-9682.

Reading notes: Cataloging figure 'dc20' and 'QA76.8.P67K33 1995' spacing may be an OCR artifact

Printed product names
PowerPC
Printed dates
1995 · April 1995 · 94-10489
Languages
English
PDF page 4

Tous mes remerciements a ma femme Sylvie Giral, car sans son assistance et sa presence, ce livre aurait ete bien difficile a accomplir. Aussi, toute ma gratitude a mes parents pour leur soutien et encouragements continuels: 'Tespere qu'ils conserveront ton cerveau au vinaigre apres avoir autant investi dans ton education." Thanks are also in order to Tom Pittman of !tty-Bitty Computers for suffering through an earlier draft of this manuscript and suggesting numerous improvements and identifying areas that needed additional clarification. Special thanks to ff!~) I I °i c' iP· and iif ~ ~-f- for providing much needed distractions and helping me maintain my sanity during the creation of this manuscript.

Reading notes: Garbled characters in the sentence containing the Chinese-style glyphs ' ff!~) I I °i c' iP· and iif ~ ~-f-' are unreadable. OCR-like errors such as 'Tespere' and 'ton cerveau' spacing are uncertain.

Languages
French · English
PDF page 5

Contents Chapter 1 Introduction 1 Purpose of Book 1 Intended Audience 1 Why Assembly Language on a RISC Processor? 2 PowerPC as a RISC ISA 5 Overview of this Book 7 Chapter 2 PowerPC Architecture Overview 9 Data Organization 9 Functional Units 12 Processor Registers 13 32- versus 64-bit Implementations 23 Chapter 3 Instruction Set Overview 27 Instruction Groups 27 Instruction Suffixes 28 Extended Instruction Forms 29 Obsolete Instructions 29 Optional Instructions 29 Notation 30 v

Languages
English
PDF page 6

Chapter 4 Branch and Trap Instructions 35 Branch Instructions 35 Branch Prediction 47 Trap Instructions 50 System Linkage Instructions 54 Chapter 5 Load and Store Instructions 57 Loads and Stores 57 Load and Store Byte 59 Load and Store Halfword 60 Load and Store Word 61 Load and Store Doubleword 63 Load and Store Byte-Reversed Data 64 Load and Store Floating-Point Double-Precision 65 Load and Store Floating-Point Single-Precision 65 Load and Store Multiple 66 Load and Store String 68 Load and Store Synchronization 71 Obsolete Load String 72 Chapter 6 Integer Instructions 75 Addition 75 Subtraction 78 Multiplication 80 Division 81 Miscellaneous Arithmetic Instructions 83 Comparison Instructions 84 Logical (Boolean) Instructions 86 Obsolete Arithmetic Instructions 88 Chapter 7 Rotate and Shift Instructions 93 Rotation Masks 93 How Rotates and Shifts Update the CR 96 Rotate Instructions 97 Shift Instructions 102 Extended Rotate Instruction Forms 107 Multiple-Precision Shifts 115 Obsolete Rotate and Shift Instructions 129 Chapter 8 Floating-Point Instructions 135 Floating-Point Data Representation 135 Floating-Point Operation 142 Floating-Point Instructions 148 Floating-Point Conversions 155 vi Contents

Reading notes: Source is a table of contents with page numbers; no advertised products, prices, or dates appear. Headings with line-wrapped words (e.g., 'Loads and Stores') may reflect layout breaks rather than the original spelling.

Languages
English
PDF page 7

Chapter 9 System Register Instructions 159 CR Instructions 159 FPSCR Instructions 162 MSR Instructions 163 SPR Instructions 164 User-Level SPR Extended Forms 164 Supervisor-Level SPR Extended Forms 166 Time Base Register Instructions 170 Segment Register Instructions 171 Chapter 10 Memory and Caches 173 Introduction 173 Memory and Cache Overview 174 Cache Architecture 176 Power PC Cache Geometry 184 PowerPC Cache Coherency 185 Power PC Storage Control Instructions 185 Virtual Memory 188 PowerPC Memory Management 192 PowerPC Memory Access Modes 196 Power PC Lookaside Buffer Instructions 198 Chapter 11 Pipelining 201 What is a Pipeline? 201 Basic Pipeline Functions 202 PowerPC 601 Pipeline Description 205 Chapter 12 PowerPC 601 Instruction Timing 215 Reading the Timing Tables 215 Instruction Dispatch Timing 216 Fixed-Point Instruction Timings 219 Floating-Point Instruction Timings 225 Branch Instruction Timings 228 Cache Access Timings 238 Pipeline Synchronization 240 Abnormal Integer Conditions 243 Abnormal Floating-Point Conditions 245 Timing Pitfalls 248 Chapter 13 Programming Model 251 Register Usage Conventions 251 Table of Contents (TOC) 254 The Stack Pointer 255 Contents vii

Reading notes: Table of contents lists chapter titles and page numbers in reading order; some inter-word spacing is irregular

Languages
English
PDF page 8

Brief Interlude: Naming Conventions 261 Subroutine Calling Conventions 261 A Simple Subroutine Call 262 An Even Simpler Subroutine Call 268 Saving Registers on the Stack 270 Stack Frames 278 Passing Arguments to Routines 281 Retrieving Results from Routines 286 Stack Frames and alloca() 287 Linking with Global Routines 288 Chapter 14 Introduction to Optimizing 295 When to Optimize 295 Examining Compiled Code 297 Standard Optimizations 300 Chapter 15 Resource Scheduling 305 Types of Processor Resources 305 Pipeline Conflicts 306 Register Usage Dependencies 314 Memory Dependencies 334 Chapter 16 More Optimization Techniques 341 Keeping the Processor Busy 341 Increasing Scheduling Opportunities 344 Strength Reduction 346 Load I Store Ordering 350 Software Pipelining 351 Appendix A Instruction Set Summary 353 AppendixB Complete List of Mnemonics AppendixC Register Bit Definitions 669 AppendixD Optimization Summary 677 AppendixE References & Further Reading Index 681 viii Contents 643 679

Reading notes: Table of contents layout with page numbers; reading order is list-like rather than prose.

Languages
English
PDF page 9

Introduction 1.1 Purpose of Book The purpose of this book is simple: to introduce programmers to the concepts of assembly language programming for PowerPC processors. Beyond simply pre- senting the information required to write functional code, this book presents the information necessary to write efficient code, and it also discusses the tricks that compilers use to produce fast code. The vast majority of people reading this book may have little need to write a significant amount of code in assembly language. But the techniques presented here will be useful to the high-level programmer who needs to read compiler- generated code as part of the debugging process. 1.2 Intended Audience This book is intended for programmers who have had some sort of experience with high-level languages such as C or Pascal, or with assembly language for another processor. This book will not spend time defining basic concepts such as hexadecimal nota- tion nor expounding the virtues of null-terminated over length-encoded strings. It will, however, present definitions for any term that is used. For example, con- cepts like latency and throughput will be defined before being used. Introduction 1

Languages
English
PDF page 10

§ 1.3 Why Assembly Language on a RISC Processor? 1.3 Why Assembly Language on a RISC Processor? The first question that many people ask is something like: "Why would any sane person be interested in learning assembly language for a RISC processor?" The reasoning behind this question is the belief that properly written RISC assembly language routines are significantly more difficult to write than similar CISC assembly routines, and that today's compilers produce far better code than code generated by hand, so why bother? The question is also valid because the computer industry has been moving away from programming in low-level languages and emphasizing languages that promise robustness, easy verification of correctness, and so on. Assembly lan- guage can promise none of those things. So, again, why bother? The truth of the matter is that there are two major reasons for wanting to be familiar with assembly language for any processor (RISC or not). The first is that you need complete control of all processor resources to make the code as fast or small as possible. The second reason is that you need to debug your code (or someone else's) and see what's really going on. A source-level debugger may not be sufficient (or even available). The first reason may sound like it's only for anal-retentive speed freaks, but there are reasons for normal human beings to want to hand-code assembly. It is a sad fact that many of the commercially available compilers do not generate highly optimized code. The best-selling compilers are typically those with the fastest edit-compile-run times, and a proper optimizer takes a bit of time to run. Thus, while it's true that a properly written optimizing compiler can generate code that is better than the code an average assembly language programmer would produce, many compilers are not properly written and thus, do not pro- duce properly optimized code. In addition, a compiler is limited by the source language. As a programmer in assembly language, you know exactly what you need and when you need it (and when you no longer need it). The compiler must try to figure out as much about the program as it can from the source, and when it's in doubt, it must play it safe and produce less efficient code. In the C programming language, the pro- grammer is given such flexibility with addresses and pointers that it becomes extremely difficult for the compiler to determine if a range of memory can be modified by a section of code. Because of the compiler's inability to guarantee that the memory won't be modified, it may not be able to risk applying certain optimizations. Debugging is another situation that causes programmers to drop down into assembly. Sometimes (one hopes rarely), compilers do not produce correct code. More commonly, you may find that your program works fine in some circum- stances, but dies a horrible death when it interacts with other programs. Lacking 2 Chapter 1

Reading notes: Page number '2 Chapter 1' appears at the end of the excerpt, so the reading order near the page break may be incomplete.

Languages
English
PDF page 11

CISC versus RISC the source code to the other program, assembly may be the only course of action available. Of course, there's a reason high-level languages exist. The vast majority of your coding and debugging will be done in some sort of high-level language. You should need to use assembly only when you have a routine that needs to be as fast as possible. To correctly respond to whether RISC assembly is more difficult than CISC assembly, the terms RISC and CISC must first be discussed in more detail. CISC versus RISC There is a lot of confusion surrounding the differences between a RISC and a CISC architecture arising from misuse of the terms and from various marketing departments propagating the "RISC is always faster /better than CISC" myth. Unfortunately, part of the confusion also comes from the fact that there is no clear line between the two design philosophies: some architectures are clearly RISC, others are clearly CISC, while still others fall somewhere in between. The most important thing to note is that RISC ("Reduced Instruction Set Com- puter") and CISC ("Complex Instruction Set Computer") are terms that are properly applied to an Instruction Set Architecture (ISA) and not to a particular implementation of an ISA. Common usage allows a processor to be called a "RISC processor," but what is really meant is that the processor is an implemen- tation of a RISC architecture. This difference between an ISA and a hardware implemention of that ISA is fun- damental to understanding RISC and CISC. What RISC Is The following is a list of features that are commonly associated with RISC ISA's: • A large uniform register set • A load I store architecture • A minimal number of addressing modes • A simple fixed-length instruction encoding • No I minimal support for misaligned accesses This set of "rules" is designed to make fast processors easier to implement. Notice that the list contains no requirements as to the number of instructions, nor does it indicate how the processor should be implemented. These are issues that are commonly mistaken as features required for a RISC processor. Introduction 3

Reading notes: Text begins mid-sentence and page header 'CISC versus RISC' appears mid-passage; reading order may be partial. Printed headers and running text are interleaved with hyphenation artifacts such as 'Com-puter' and 'fun-damental'.

Languages
English