aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/VectorUtils.cpp
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2020-02-05 14:18:13 -0500
committerSanjay Patel <spatel@rotateright.com>2020-02-05 14:55:02 -0500
commit686a038ed8f96e5539c54fea28aedac63145cf71 (patch)
tree4ca290a7ca8de9629664f056227ac636dbc9835c /llvm/lib/Analysis/VectorUtils.cpp
parent043e4787211609e987799050ba2944b77a53ddb5 (diff)
downloadllvm-686a038ed8f96e5539c54fea28aedac63145cf71.zip
llvm-686a038ed8f96e5539c54fea28aedac63145cf71.tar.gz
llvm-686a038ed8f96e5539c54fea28aedac63145cf71.tar.bz2
[Analysis] add query to get splat value from array of ints
I was debug stepping through an x86 shuffle lowering and noticed we were doing an N^2 search for splat index. I didn't find the equivalent functionality anywhere else in LLVM, so here's a helper that takes an array of int and returns a splatted index while ignoring undefs (any negative value). This might also be used inside existing ShuffleVectorInst/ShuffleVectorSDNode functions and/or help with D72467. Differential Revision: https://reviews.llvm.org/D74064
Diffstat (limited to 'llvm/lib/Analysis/VectorUtils.cpp')
-rw-r--r--llvm/lib/Analysis/VectorUtils.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/VectorUtils.cpp b/llvm/lib/Analysis/VectorUtils.cpp
index e4b0010..d2c521a 100644
--- a/llvm/lib/Analysis/VectorUtils.cpp
+++ b/llvm/lib/Analysis/VectorUtils.cpp
@@ -307,6 +307,24 @@ Value *llvm::findScalarElement(Value *V, unsigned EltNo) {
return nullptr;
}
+int llvm::getSplatIndex(ArrayRef<int> Mask) {
+ int SplatIndex = -1;
+ for (int M : Mask) {
+ // Ignore invalid (undefined) mask elements.
+ if (M < 0)
+ continue;
+
+ // There can be only 1 non-negative mask element value if this is a splat.
+ if (SplatIndex != -1 && SplatIndex != M)
+ return -1;
+
+ // Initialize the splat index to the 1st non-negative mask element.
+ SplatIndex = M;
+ }
+ assert((SplatIndex == -1 || SplatIndex >= 0) && "Negative index?");
+ return SplatIndex;
+}
+
/// Get splat value if the input is a splat vector or return nullptr.
/// This function is not fully general. It checks only 2 cases:
/// the input value is (1) a splat constant vector or (2) a sequence