[PATCH] [SLSR] handle candidate form &B[i * S]

Jingyue Wu jingyue at google.com
Mon Mar 9 15:51:24 PDT 2015


Hi Andrew and Hal,

Sorry about the long delay in reworking this patch. I hope it is not too cache-cold :)

Two major changes against the previous patch are:

- Replacing BaseExpr with SCEV. As we discussed before, we shouldn't reinvent another abstraction of arithmetic expressions. Reusing SCEV turns out to save lots of code!
- The discovery phase of the current patch tries to match GEPs in the form of `&B[..][i * S][..]` and make it a candidate `(char *)&B[..][0][..] + (i * element_size) * S`. The old patch only works when `i * S` is the last index.

I wished I could use SCEV more extensively in this patch, e.g., to discover candidates and compute bumps. However, I didn't do that for two reasons:

- Given an LLVM value, SCEV recursively evaluates all subexpressions of this value. The current SLSR algorithm does very shallow pattern matching and rewrites candidates based on these simple patterns.  Recursively evaluating subexpressions makes SLSR harder to rewrite candidates into desired forms unless we can easily map SCEVs back to LLVM Values. For example, if S in candidate `(B + i) * S` can be further SCEV-evaluated, we would rewrite the candidate into some form like `Y + (i' - i) * expr_of(S)` instead of `Y + (i' - i) * S`.
- ScalarEvolution is designed to be control-flow oblivious, and tends to strip nsw/nuw flags. This makes SLSR harder to factor the multiply of a sext'ed expression GEPs which happens very frequently for array indexing. For example, a GEP `&B[i * S]` is translated to `B + sext(i * S)` with the nsw flag on `i * S` stripped out. Without this flag, SLSR can hardly accept `B + sext(i * S)` as a valid GEP candidate.


================
Comment at: lib/Transforms/Scalar/StraightLineStrengthReduce.cpp:242
@@ +241,3 @@
+        TTI->isLegalAddressingMode(
+            GEP->getType(), /*BaseGV=*/nullptr, /*BaseOffset=*/0,
+            /*HasBaseReg=*/true,
----------------
hfinkel wrote:
> Shouldn't you test whether or not B isa GlobalVariable? Seems like, if it is, you should set BaseGV to that, and HasBaseReg = false, and otherwise keep the current set of arguments.
Done. Please check out function `isCompletelyFoldable`.

http://reviews.llvm.org/D7459

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the llvm-commits mailing list