-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arm64: Combine if conditions into compare chains (#79283)
Add a new stage optOptimizeCompareChainCondBlock in pass optOptimizeBools. This aims to reduced the number of conditional jumps by joining cases when multiple conditions gate the execution of a block. Example 1: If ( a > b || c == d) { x = y; } Will be represented in IR as: ------------ BB01 -> BB03 (cond), succs={BB02,BB03} * JTRUE (GT a,b) ------------ BB02 -> BB04 (cond), preds={BB01} succs={BB03,BB04} * JTRUE (NE c,d) ------------ BB03, preds={BB01, BB02} succs={BB04} * ASG (x,y) These operands will be combined into a single AND in the first block (with the first condition inverted), wrapped by the test condition (NE(...,0)). Giving: ------------ BB01 -> BB03 (cond), succs={BB03,BB04} * JTRUE (NE (AND (LE a,b), (NE c,d)), 0) ------------ BB03, preds={BB01} succs={BB04} * ASG x,y Example 2: If ( a > b && c == d) { x = y; } else { x = z; } Here the && conditions are connected via an OR. After the pass: ------------ BB01 -> BB03 (cond), succs={BB03,BB04} * JTRUE (NE (OR (LE a,b), (NE c,d)), 0) ------------ BB03, preds={BB01} succs={BB05} * ASG x,y ------------ BB04, preds={BB01} succs={BB05} * ASG x,z Example 3: If ( a > b || c == d || e < f ) { x = y; } The first pass of the optimization will combine two of the conditions. The second pass will then combine remaining condition the earlier chain. ------------ BB01 -> BB03 (cond), succs={BB03,BB04} * JTRUE (NE (OR ((NE (OR (NE c,d), (GE e,f)), 0), (LE a,b))), 0) ------------ BB03, preds={BB01} succs={BB04} * ASG x,y This optimization means that every condition within the IF statement is always evaluated, as opposed to stopping at the first positive match. Theoretically there is no maximum limit on the size of the generated chain. Therefore cost checking is used to limit the maximum number of conditions that can be chained together. Currently the cost checking limits to a maximum of three simple conditions. This is the same behaviour as GCC. Note that LLVM allows chains of much longer length.
- Loading branch information
Showing
6 changed files
with
474 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.