Thursday, August 2, 2007

On the IAR MSP430 C Compiler's Inefficient Register Utilization

Recently, I've been digging into the documentation of Texas Instruments' MSP430 micro-controller family. After covering the CPU itself, I continued into the documentation[1] for the mspgcc project, a port of GCC to the MSP430. After realizing that the ABI used for mspgcc had never been defined in the chip's documentation[3], I dug up the manual[2] for IAR's compiler and compared the two.

I quickly discovered that IAR's compiler wastes registers when passing 16-bit parameters to a C function. By its ABI, the first 16-bit parameter is placed into R12 and the second into R14. R13 and R15 remain unused, as they are reserved for the high words of 32-bit parameters. GCC follows the much more logical route of only assigning a single register to a 16-bit value, such that R15 is used for the first parameter, R14 for the second, R13 for the third, and R12 for the fourth. This allows it to accept four parameters by register, while IAR's compiler will push the third and fourth onto the stack while leaving two clobber registers unused!

To demonstrate this, I have compiled a simple C program containing only a function foo() which returned the sum of its four inputs and a main() method to call foo(). This was compiled to assembly language using mspgcc 3.2.3 and IAR MSP430 C/C++ Compiler V3.42A/W32.

In both compilers, four assembly instructions were used to add the values and return the result in the single register of the first parameter, R12 for IAR and R15 for GCC. The table below lists the assembly generated by each compiler, with instructions converted from the GCC format (lowercase, .W omitted) to the IAR format for clear comparison. GCC, by virtue of its more efficient register usage, avoids both having to PUSH.W two parameters onto the stack and avoids having to use the indexed addressing mode, as X(SP), within the function.

IAR CompilerGCC Compiler

ADD.W R14, R12 //1 cycle, 1 word
ADD.W 0x2(SP), R12 //3c,2w
ADD.W 0x4(SP), R12 //3c,2w

ADD.W R14, R15 //1c,1w
ADD.W R13, R15 //1c,1w
ADD.W R12, R15 //1c,1w

Pages 3-72 and 3-73 of the MSP430 Family Guide[3] detail the full cost of these additions, which increase not only the runtime but also the storage requirements of the function. According to those pages, "ADD.W r14,r15" takes 1 cycle and 1 word of memory while "ADD.W 0x2(SP), R12" takes 3 cycles and 2 words of memory. Additionally, each of the two PUSH.W statements required to call foo() in the IAR compiler takes 3 cycles, which are unnecessary in GCC.

Texas Instruments' Code Composer Essentials does not suffer from IAR's inefficiency; rather, it uses an ABI similar to but incompatible with GCC. TICCE allocates register R12 for the first parameter, then R13, R14, and R15. The result is returned in R12. GCC uses registers in the opposite order and returns in R15. See the Users Guide[4] for more details.

What's the reasoning behind IAR's design? It makes functions of two 32-bit values easily compatible with those of two 16-bit values, but this compatibility breaks as soon as the third parameter comes into play, which is pushed onto the stack as a single word. If such compatibility were essential, the trick could be maintained by using R13 for the third parameter and R15 for the fourth.

[1] mspgcc manual
[2] IAR manuals
[3] Texas Instruments's MSP430 Family Guide
[4] MSP430 Optimizing C/C++ Compiler User's Guide (SLAU132)


Travis Goodspeed said...

IAR's register usage has been fixed as of October 19th, beginning with IAR - EW430 4.10A. See here.

jar said...

Excellent observation. Good job.

Oh geez, I can't post this anonymously.

cv writing service said...

nice post

Mary robinson said...

I appreciate you for finding this problem with IAR's compiler and i think nobody have noticed this types of errors when working with a source code.I f you are searching for Do my excel homework, then you can follow Edubirdie writing service

onlineAssignmenthelp said...

Assignment Help in Sydney Writing Service in Sydney Affordability doesn't mean bad quality. Besides being the most affordable assignment help company, Thetutorshelp is also the most reliable Online Assignment Help Assignment help Sydney- Guiding you towards success with our expert writers. GET INSTANT SUCCESS WITH EVERY PROJECT WE DELIVER Whatsapp Number +61-280062221 See more

onlineAssignmenthelp said...

Essay Writing Help Sydney Assignment Our writers are highly experts and are well-equipped with the research where the work-quality is never compromised. Also, we offer unlimited free revisions until the content meets your requirements. You can get in touch with us at any time as we are available 24*7 to assist you.A student should get prepared for the onset of numerous assignments that will be assigned by the university or the professors. Seemore -

onlineAssignmenthelp said...

Dissertation Writing Assignment Help in Sydney by Expert Writers. Hire our highly talented writers from renowned universities of Australia and learn the skills to prepare quality

onlineAssignmenthelp said...

Assignment Help Sydney Content Writing Service Hire the best assignment help experts of Sydney to write your assignments. We deliver quality that matches the academic requirements of Sydney universities. See more

onlineAssignmenthelp said...

Assignment Help in Sydney
Assignment Help in Sydney The Assignments are the tasks given to the examinee to complete it in a given time. The assignments are of no value without a deadline. At The Tutors Help, we are devoted to delivering work before the deadline day. We do not compromise on quality and on-time deliveries that make us unique

GRSoft Developers said...

Great Research Solutions Pvt. Ltd. ( GRSoft ) is an IT company with no geographical boundaries and provides all that you can think of around IT including consulting, solutions, applications and outsourcing services.

We are a combination of unmatched capabilities, experiences and jobs that serve across the industries. We knit closely with the clients to deliver high quality solutions with latest technologies and every growing business model approach. With the solutions provided by our professional staff our vision is to help our clients always reach new heights.

Software Development Company India | Software Development Company | Software Development Company usa | Software Development Consulting Services India

GRSoft Developers said...

GRSoft Gaming is an honor winning, Live Casino game advancement organization of India. We offer curiosity and inventive game development with an exceptionally talented group of developer. Our bleeding edge gaming innovation creates esteem included gaming arrangements. Quality is our real worry for club game development. We offer tweaked Casino game development benefits over the globe and help you furnish with customized development. With our Casino game, individuals couldn't imagine anything better than to chance their gaining and appreciate the advantages. We have created numerous effective games like the video poker game, Sports wagering game, online club game, lottery games, and numerous other common games.

Casino Game Development Companies in India | Hire Poker Game Developers | Casino Game Developer | Casino Games Development Companies | Hire Poker Developers | Gaming Software Companies in India Rummy Game Software | Rummy Game Development

michael said...


Amy Bella said...

It is really inspiring and informative. I also have some understanding about the subject still I feel this has enhanced my knowledge. Thanks for sharing.
virtual assistant india cost
mobile application development in india

Harry Thomas said...

Wondering “ why is my computer so slow?” Running too many programs altogether can be one of the causes of this issue, making your life troublesome and too slow.

Harry Thomas said...

Yahoo will send you a reset link in your phone number or email address. In case if you are not accessing to the phone number anymore, click on I don’t have access to this phone option. It will then send you the link to your alternative mail address. Check your mail and do the procedure as suggested.

IVR Call Center Solutions said...

Thank you for this post. Good luck.
Call Center Software in dubai

Charles Herve said...

Ambil peluang terbaik anda dengan main OLE777 poker

EthanWilliam said...

Our main concern for club game development is quality. We assist you furnish with specialized development and provide modified Casino game creation services over the globe. People couldn't think of anything better with our casino game than to gamble their money and enjoy the benefits. Accounting Services