blob: cb83d3d053bf1c1f358e2bb3c1cc0e631dc76a49 [file] [log] [blame]
Philip Reamesf8bf9dd2015-02-27 23:14:50 +00001=====================================
2Performance Tips for Frontend Authors
3=====================================
4
5.. contents::
6 :local:
7 :depth: 2
8
9Abstract
10========
11
12The intended audience of this document is developers of language frontends
13targeting LLVM IR. This document is home to a collection of tips on how to
14generate IR that optimizes well. As with any optimizer, LLVM has its strengths
15and weaknesses. In some cases, surprisingly small changes in the source IR
16can have a large effect on the generated code.
17
18Avoid loads and stores of large aggregate type
19================================================
20
21LLVM currently does not optimize well loads and stores of large :ref:`aggregate
22types <t_aggregate>` (i.e. structs and arrays). As an alternative, consider
23loading individual fields from memory.
24
25Aggregates that are smaller than the largest (performant) load or store
26instruction supported by the targeted hardware are well supported. These can
27be an effective way to represent collections of small packed fields.
28
29Prefer zext over sext when legal
30==================================
31
32On some architectures (X86_64 is one), sign extension can involve an extra
33instruction whereas zero extension can be folded into a load. LLVM will try to
34replace a sext with a zext when it can be proven safe, but if you have
35information in your source language about the range of a integer value, it can
36be profitable to use a zext rather than a sext.
37
38Alternatively, you can :ref:`specify the range of the value using metadata
39<range-metadata>` and LLVM can do the sext to zext conversion for you.
40
41Zext GEP indices to machine register width
42============================================
43
44Internally, LLVM often promotes the width of GEP indices to machine register
45width. When it does so, it will default to using sign extension (sext)
46operations for safety. If your source language provides information about
47the range of the index, you may wish to manually extend indices to machine
48register width using a zext instruction.
49
50
51Adding to this document
52=======================
53
54If you run across a case that you feel deserves to be covered here, please send
55a patch to `llvm-commits
56<http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits>`_ for review.
57
58If you have questions on these items, please direct them to `llvmdev
59<http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev>`_. The more relevant
60context you are able to give to your question, the more likely it is to be
61answered.
62