@@ -53,13 +53,25 @@ Other things to consider
53
53
#. Make sure that a DataLayout is provided (this will likely become required in
54
54
the near future, but is certainly important for optimization).
55
55
56
- #. Add nsw/nuw/fast-math flags as appropriate
56
+ #. Add nsw/nuw flags as appropriate. Reasoning about overflow is
57
+ generally hard for an optimizer so providing these facts from the frontend
58
+ can be very impactful. For languages which need overflow semantics,
59
+ consider using the :ref: `overflow intrinsics <int_overflow >`.
60
+
61
+ #. Use fast-math flags on floating point operations if legal. If you don't
62
+ need strict IEEE floating point semantics, there are a number of additional
63
+ optimizations that can be performed. This can be highly impactful for
64
+ floating point intensive computations.
65
+
66
+ #. Use inbounds on geps. This can help to disambiguate some aliasing queries.
57
67
58
68
#. Add noalias/align/dereferenceable/nonnull to function arguments and return
59
69
values as appropriate
60
70
61
- #. Mark functions as readnone/readonly/nounwind when known (especially for
62
- external functions)
71
+ #. Mark functions as readnone/readonly or noreturn/nounwind when known. The
72
+ optimizer will try to infer these flags, but may not always be able to.
73
+ Manual annotations are particularly important for external functions that
74
+ the optimizer can not analyze.
63
75
64
76
#. Use ptrtoint/inttoptr sparingly (they interfere with pointer aliasing
65
77
analysis), prefer GEPs
@@ -85,9 +97,51 @@ Other things to consider
85
97
and may not be well optimized by the current optimizer. Depending on your
86
98
source language, you may consider using fences instead.
87
99
100
+ #. If calling a function which is known to throw an exception (unwind), use
101
+ an invoke with a normal destination which contains an unreachable
102
+ instruction. This form conveys to the optimizer that the call returns
103
+ abnormally. For an invoke which neither returns normally or requires unwind
104
+ code in the current function, you can use a noreturn call instruction if
105
+ desired. This is generally not required because the optimizer will convert
106
+ an invoke with an unreachable unwind destination to a call instruction.
107
+
88
108
#. If you language uses range checks, consider using the IRCE pass. It is not
89
109
currently part of the standard pass order.
90
110
111
+ #. For languages with numerous rarely executed guard conditions (e.g. null
112
+ checks, type checks, range checks) consider adding an extra execution or
113
+ two of LoopUnswith and LICM to your pass order. The standard pass order,
114
+ which is tuned for C and C++ applications, may not be sufficient to remove
115
+ all dischargeable checks from loops.
116
+
117
+ #. Use profile metadata to indicate statically known cold paths, even if
118
+ dynamic profiling information is not available. This can make a large
119
+ difference in code placement and thus the performance of tight loops.
120
+
121
+ #. When generating code for loops, try to avoid terminating the header block of
122
+ the loop earlier than necessary. If the terminator of the loop header
123
+ block is a loop exiting conditional branch, the effectiveness of LICM will
124
+ be limited for loads not in the header. (This is due to the fact that LLVM
125
+ may not know such a load is safe to speculatively execute and thus can't
126
+ lift an otherwise loop invariant load unless it can prove the exiting
127
+ condition is not taken.) It can be profitable, in some cases, to emit such
128
+ instructions into the header even if they are not used along a rarely
129
+ executed path that exits the loop. This guidance specifically does not
130
+ apply if the condition which terminates the loop header is itself invariant,
131
+ or can be easily discharged by inspecting the loop index variables.
132
+
133
+ #. In hot loops, consider duplicating instructions from small basic blocks
134
+ which end in highly predictable terminators into their successor blocks.
135
+ If a hot successor block contains instructions which can be vectorized
136
+ with the duplicated ones, this can provide a noticeable throughput
137
+ improvement. Note that this is not always profitable and does involve a
138
+ potentially large increase in code size.
139
+
140
+ #. Avoid high in-degree basic blocks (e.g. basic blocks with dozens or hundreds
141
+ of predecessors). Among other issues, the register allocator is known to
142
+ perform badly with confronted with such structures. The only exception to
143
+ this guidance is that a unified return block with high in-degree is fine.
144
+
91
145
p.s. If you want to help improve this document, patches expanding any of the
92
146
above items into standalone sections of their own with a more complete
93
147
discussion would be very welcome.
0 commit comments