Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1 | //===- InstCombineVectorOps.cpp -------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements instcombine for ExtractElement, InsertElement and |
| 10 | // ShuffleVector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 14 | #include "InstCombineInternal.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APInt.h" |
| 16 | #include "llvm/ADT/ArrayRef.h" |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Benjamin Kramer | ec228d7 | 2020-04-13 20:36:31 +0200 | [diff] [blame] | 19 | #include "llvm/ADT/SmallBitVector.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/InstructionSimplify.h" |
| 22 | #include "llvm/Analysis/VectorUtils.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 23 | #include "llvm/IR/BasicBlock.h" |
| 24 | #include "llvm/IR/Constant.h" |
| 25 | #include "llvm/IR/Constants.h" |
| 26 | #include "llvm/IR/DerivedTypes.h" |
| 27 | #include "llvm/IR/InstrTypes.h" |
| 28 | #include "llvm/IR/Instruction.h" |
| 29 | #include "llvm/IR/Instructions.h" |
| 30 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 31 | #include "llvm/IR/PatternMatch.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Type.h" |
| 33 | #include "llvm/IR/User.h" |
| 34 | #include "llvm/IR/Value.h" |
| 35 | #include "llvm/Support/Casting.h" |
| 36 | #include "llvm/Support/ErrorHandling.h" |
| 37 | #include "llvm/Transforms/InstCombine/InstCombineWorklist.h" |
| 38 | #include <cassert> |
| 39 | #include <cstdint> |
| 40 | #include <iterator> |
| 41 | #include <utility> |
| 42 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 43 | using namespace llvm; |
Nadav Rotem | 7df8509 | 2013-01-15 23:43:14 +0000 | [diff] [blame] | 44 | using namespace PatternMatch; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 45 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 46 | #define DEBUG_TYPE "instcombine" |
| 47 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 48 | /// Return true if the value is cheaper to scalarize than it is to leave as a |
Sanjay Patel | e51d5bd | 2018-12-18 19:07:38 +0000 | [diff] [blame] | 49 | /// vector operation. IsConstantExtractIndex indicates whether we are extracting |
| 50 | /// one known element from a vector constant. |
| 51 | /// |
| 52 | /// FIXME: It's possible to create more instructions than previously existed. |
| 53 | static bool cheapToScalarize(Value *V, bool IsConstantExtractIndex) { |
| 54 | // If we can pick a scalar constant value out of a vector, that is free. |
| 55 | if (auto *C = dyn_cast<Constant>(V)) |
| 56 | return IsConstantExtractIndex || C->getSplatValue(); |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 57 | |
Sanjay Patel | e51d5bd | 2018-12-18 19:07:38 +0000 | [diff] [blame] | 58 | // An insertelement to the same constant index as our extract will simplify |
| 59 | // to the scalar inserted element. An insertelement to a different constant |
| 60 | // index is irrelevant to our extract. |
| 61 | if (match(V, m_InsertElement(m_Value(), m_Value(), m_ConstantInt()))) |
| 62 | return IsConstantExtractIndex; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 63 | |
Sanjay Patel | e51d5bd | 2018-12-18 19:07:38 +0000 | [diff] [blame] | 64 | if (match(V, m_OneUse(m_Load(m_Value())))) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 65 | return true; |
Sanjay Patel | e51d5bd | 2018-12-18 19:07:38 +0000 | [diff] [blame] | 66 | |
Simon Moll | d871ef4 | 2020-03-10 16:05:31 +0100 | [diff] [blame] | 67 | if (match(V, m_OneUse(m_UnOp()))) |
| 68 | return true; |
| 69 | |
Sanjay Patel | e51d5bd | 2018-12-18 19:07:38 +0000 | [diff] [blame] | 70 | Value *V0, *V1; |
| 71 | if (match(V, m_OneUse(m_BinOp(m_Value(V0), m_Value(V1))))) |
| 72 | if (cheapToScalarize(V0, IsConstantExtractIndex) || |
| 73 | cheapToScalarize(V1, IsConstantExtractIndex)) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 74 | return true; |
Sanjay Patel | e51d5bd | 2018-12-18 19:07:38 +0000 | [diff] [blame] | 75 | |
| 76 | CmpInst::Predicate UnusedPred; |
| 77 | if (match(V, m_OneUse(m_Cmp(UnusedPred, m_Value(V0), m_Value(V1))))) |
| 78 | if (cheapToScalarize(V0, IsConstantExtractIndex) || |
| 79 | cheapToScalarize(V1, IsConstantExtractIndex)) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 80 | return true; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 81 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
Michael Kuperstein | a0c6ae0 | 2016-06-06 23:38:33 +0000 | [diff] [blame] | 85 | // If we have a PHI node with a vector type that is only used to feed |
Matt Arsenault | 3887473 | 2013-08-28 22:17:26 +0000 | [diff] [blame] | 86 | // itself and be an operand of extractelement at a constant location, |
| 87 | // try to replace the PHI of the vector type with a PHI of a scalar type. |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 88 | Instruction *InstCombiner::scalarizePHI(ExtractElementInst &EI, PHINode *PN) { |
Michael Kuperstein | a0c6ae0 | 2016-06-06 23:38:33 +0000 | [diff] [blame] | 89 | SmallVector<Instruction *, 2> Extracts; |
| 90 | // The users we want the PHI to have are: |
| 91 | // 1) The EI ExtractElement (we already know this) |
| 92 | // 2) Possibly more ExtractElements with the same index. |
| 93 | // 3) Another operand, which will feed back into the PHI. |
| 94 | Instruction *PHIUser = nullptr; |
| 95 | for (auto U : PN->users()) { |
| 96 | if (ExtractElementInst *EU = dyn_cast<ExtractElementInst>(U)) { |
| 97 | if (EI.getIndexOperand() == EU->getIndexOperand()) |
| 98 | Extracts.push_back(EU); |
| 99 | else |
| 100 | return nullptr; |
| 101 | } else if (!PHIUser) { |
| 102 | PHIUser = cast<Instruction>(U); |
| 103 | } else { |
| 104 | return nullptr; |
| 105 | } |
| 106 | } |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 107 | |
Michael Kuperstein | a0c6ae0 | 2016-06-06 23:38:33 +0000 | [diff] [blame] | 108 | if (!PHIUser) |
| 109 | return nullptr; |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 110 | |
| 111 | // Verify that this PHI user has one use, which is the PHI itself, |
| 112 | // and that it is a binary operation which is cheap to scalarize. |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 113 | // otherwise return nullptr. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 114 | if (!PHIUser->hasOneUse() || !(PHIUser->user_back() == PN) || |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 115 | !(isa<BinaryOperator>(PHIUser)) || !cheapToScalarize(PHIUser, true)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 116 | return nullptr; |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 117 | |
| 118 | // Create a scalar PHI node that will replace the vector PHI node |
| 119 | // just before the current PHI node. |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 120 | PHINode *scalarPHI = cast<PHINode>(InsertNewInstWith( |
| 121 | PHINode::Create(EI.getType(), PN->getNumIncomingValues(), ""), *PN)); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 122 | // Scalarize each PHI operand. |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 123 | for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) { |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 124 | Value *PHIInVal = PN->getIncomingValue(i); |
| 125 | BasicBlock *inBB = PN->getIncomingBlock(i); |
| 126 | Value *Elt = EI.getIndexOperand(); |
| 127 | // If the operand is the PHI induction variable: |
| 128 | if (PHIInVal == PHIUser) { |
| 129 | // Scalarize the binary operation. Its first operand is the |
Sanjay Patel | 70af1fd | 2014-07-07 22:13:58 +0000 | [diff] [blame] | 130 | // scalar PHI, and the second operand is extracted from the other |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 131 | // vector operand. |
| 132 | BinaryOperator *B0 = cast<BinaryOperator>(PHIUser); |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 133 | unsigned opId = (B0->getOperand(0) == PN) ? 1 : 0; |
Joey Gouly | 8369928 | 2013-05-24 12:29:54 +0000 | [diff] [blame] | 134 | Value *Op = InsertNewInstWith( |
| 135 | ExtractElementInst::Create(B0->getOperand(opId), Elt, |
| 136 | B0->getOperand(opId)->getName() + ".Elt"), |
| 137 | *B0); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 138 | Value *newPHIUser = InsertNewInstWith( |
Owen Anderson | 7ea02fc | 2016-03-01 19:35:52 +0000 | [diff] [blame] | 139 | BinaryOperator::CreateWithCopiedFlags(B0->getOpcode(), |
| 140 | scalarPHI, Op, B0), *B0); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 141 | scalarPHI->addIncoming(newPHIUser, inBB); |
| 142 | } else { |
| 143 | // Scalarize PHI input: |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 144 | Instruction *newEI = ExtractElementInst::Create(PHIInVal, Elt, ""); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 145 | // Insert the new instruction into the predecessor basic block. |
| 146 | Instruction *pos = dyn_cast<Instruction>(PHIInVal); |
| 147 | BasicBlock::iterator InsertPos; |
| 148 | if (pos && !isa<PHINode>(pos)) { |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 149 | InsertPos = ++pos->getIterator(); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 150 | } else { |
| 151 | InsertPos = inBB->getFirstInsertionPt(); |
| 152 | } |
| 153 | |
| 154 | InsertNewInstWith(newEI, *InsertPos); |
| 155 | |
| 156 | scalarPHI->addIncoming(newEI, inBB); |
| 157 | } |
| 158 | } |
Michael Kuperstein | a0c6ae0 | 2016-06-06 23:38:33 +0000 | [diff] [blame] | 159 | |
| 160 | for (auto E : Extracts) |
| 161 | replaceInstUsesWith(*E, scalarPHI); |
| 162 | |
| 163 | return &EI; |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Sanjay Patel | 4674c77 | 2018-09-24 20:41:22 +0000 | [diff] [blame] | 166 | static Instruction *foldBitcastExtElt(ExtractElementInst &Ext, |
Sanjay Patel | 31b0719 | 2018-10-01 14:40:00 +0000 | [diff] [blame] | 167 | InstCombiner::BuilderTy &Builder, |
| 168 | bool IsBigEndian) { |
Sanjay Patel | 4674c77 | 2018-09-24 20:41:22 +0000 | [diff] [blame] | 169 | Value *X; |
| 170 | uint64_t ExtIndexC; |
| 171 | if (!match(Ext.getVectorOperand(), m_BitCast(m_Value(X))) || |
| 172 | !X->getType()->isVectorTy() || |
| 173 | !match(Ext.getIndexOperand(), m_ConstantInt(ExtIndexC))) |
| 174 | return nullptr; |
| 175 | |
| 176 | // If this extractelement is using a bitcast from a vector of the same number |
| 177 | // of elements, see if we can find the source element from the source vector: |
| 178 | // extelt (bitcast VecX), IndexC --> bitcast X[IndexC] |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 179 | auto *SrcTy = cast<VectorType>(X->getType()); |
Sanjay Patel | 4674c77 | 2018-09-24 20:41:22 +0000 | [diff] [blame] | 180 | Type *DestTy = Ext.getType(); |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 181 | unsigned NumSrcElts = SrcTy->getNumElements(); |
Sanjay Patel | 4674c77 | 2018-09-24 20:41:22 +0000 | [diff] [blame] | 182 | unsigned NumElts = Ext.getVectorOperandType()->getNumElements(); |
| 183 | if (NumSrcElts == NumElts) |
| 184 | if (Value *Elt = findScalarElement(X, ExtIndexC)) |
| 185 | return new BitCastInst(Elt, DestTy); |
| 186 | |
Sanjay Patel | 31b0719 | 2018-10-01 14:40:00 +0000 | [diff] [blame] | 187 | // If the source elements are wider than the destination, try to shift and |
| 188 | // truncate a subset of scalar bits of an insert op. |
Sanjay Patel | 3746e11 | 2018-10-04 16:25:05 +0000 | [diff] [blame] | 189 | if (NumSrcElts < NumElts) { |
Sanjay Patel | 31b0719 | 2018-10-01 14:40:00 +0000 | [diff] [blame] | 190 | Value *Scalar; |
| 191 | uint64_t InsIndexC; |
| 192 | if (!match(X, m_InsertElement(m_Value(), m_Value(Scalar), |
| 193 | m_ConstantInt(InsIndexC)))) |
| 194 | return nullptr; |
| 195 | |
| 196 | // The extract must be from the subset of vector elements that we inserted |
| 197 | // into. Example: if we inserted element 1 of a <2 x i64> and we are |
| 198 | // extracting an i16 (narrowing ratio = 4), then this extract must be from 1 |
| 199 | // of elements 4-7 of the bitcasted vector. |
| 200 | unsigned NarrowingRatio = NumElts / NumSrcElts; |
| 201 | if (ExtIndexC / NarrowingRatio != InsIndexC) |
| 202 | return nullptr; |
| 203 | |
| 204 | // We are extracting part of the original scalar. How that scalar is |
| 205 | // inserted into the vector depends on the endian-ness. Example: |
| 206 | // Vector Byte Elt Index: 0 1 2 3 4 5 6 7 |
| 207 | // +--+--+--+--+--+--+--+--+ |
| 208 | // inselt <2 x i32> V, <i32> S, 1: |V0|V1|V2|V3|S0|S1|S2|S3| |
| 209 | // extelt <4 x i16> V', 3: | |S2|S3| |
| 210 | // +--+--+--+--+--+--+--+--+ |
| 211 | // If this is little-endian, S2|S3 are the MSB of the 32-bit 'S' value. |
| 212 | // If this is big-endian, S2|S3 are the LSB of the 32-bit 'S' value. |
| 213 | // In this example, we must right-shift little-endian. Big-endian is just a |
| 214 | // truncate. |
| 215 | unsigned Chunk = ExtIndexC % NarrowingRatio; |
| 216 | if (IsBigEndian) |
| 217 | Chunk = NarrowingRatio - 1 - Chunk; |
Sanjay Patel | 3746e11 | 2018-10-04 16:25:05 +0000 | [diff] [blame] | 218 | |
| 219 | // Bail out if this is an FP vector to FP vector sequence. That would take |
| 220 | // more instructions than we started with unless there is no shift, and it |
| 221 | // may not be handled as well in the backend. |
| 222 | bool NeedSrcBitcast = SrcTy->getScalarType()->isFloatingPointTy(); |
| 223 | bool NeedDestBitcast = DestTy->isFloatingPointTy(); |
| 224 | if (NeedSrcBitcast && NeedDestBitcast) |
| 225 | return nullptr; |
| 226 | |
| 227 | unsigned SrcWidth = SrcTy->getScalarSizeInBits(); |
| 228 | unsigned DestWidth = DestTy->getPrimitiveSizeInBits(); |
| 229 | unsigned ShAmt = Chunk * DestWidth; |
| 230 | |
| 231 | // TODO: This limitation is more strict than necessary. We could sum the |
| 232 | // number of new instructions and subtract the number eliminated to know if |
| 233 | // we can proceed. |
| 234 | if (!X->hasOneUse() || !Ext.getVectorOperand()->hasOneUse()) |
| 235 | if (NeedSrcBitcast || NeedDestBitcast) |
| 236 | return nullptr; |
| 237 | |
| 238 | if (NeedSrcBitcast) { |
| 239 | Type *SrcIntTy = IntegerType::getIntNTy(Scalar->getContext(), SrcWidth); |
| 240 | Scalar = Builder.CreateBitCast(Scalar, SrcIntTy); |
| 241 | } |
| 242 | |
Sanjay Patel | 31b0719 | 2018-10-01 14:40:00 +0000 | [diff] [blame] | 243 | if (ShAmt) { |
| 244 | // Bail out if we could end with more instructions than we started with. |
| 245 | if (!Ext.getVectorOperand()->hasOneUse()) |
| 246 | return nullptr; |
| 247 | Scalar = Builder.CreateLShr(Scalar, ShAmt); |
| 248 | } |
Sanjay Patel | 3746e11 | 2018-10-04 16:25:05 +0000 | [diff] [blame] | 249 | |
| 250 | if (NeedDestBitcast) { |
| 251 | Type *DestIntTy = IntegerType::getIntNTy(Scalar->getContext(), DestWidth); |
| 252 | return new BitCastInst(Builder.CreateTrunc(Scalar, DestIntTy), DestTy); |
| 253 | } |
Sanjay Patel | 31b0719 | 2018-10-01 14:40:00 +0000 | [diff] [blame] | 254 | return new TruncInst(Scalar, DestTy); |
| 255 | } |
| 256 | |
Sanjay Patel | 4674c77 | 2018-09-24 20:41:22 +0000 | [diff] [blame] | 257 | return nullptr; |
| 258 | } |
| 259 | |
Piotr Sobczak | a861c9a | 2019-10-21 08:12:47 +0000 | [diff] [blame] | 260 | /// Find elements of V demanded by UserInstr. |
| 261 | static APInt findDemandedEltsBySingleUser(Value *V, Instruction *UserInstr) { |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 262 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Piotr Sobczak | a861c9a | 2019-10-21 08:12:47 +0000 | [diff] [blame] | 263 | |
| 264 | // Conservatively assume that all elements are needed. |
| 265 | APInt UsedElts(APInt::getAllOnesValue(VWidth)); |
| 266 | |
| 267 | switch (UserInstr->getOpcode()) { |
| 268 | case Instruction::ExtractElement: { |
| 269 | ExtractElementInst *EEI = cast<ExtractElementInst>(UserInstr); |
| 270 | assert(EEI->getVectorOperand() == V); |
| 271 | ConstantInt *EEIIndexC = dyn_cast<ConstantInt>(EEI->getIndexOperand()); |
| 272 | if (EEIIndexC && EEIIndexC->getValue().ult(VWidth)) { |
| 273 | UsedElts = APInt::getOneBitSet(VWidth, EEIIndexC->getZExtValue()); |
| 274 | } |
| 275 | break; |
| 276 | } |
| 277 | case Instruction::ShuffleVector: { |
| 278 | ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(UserInstr); |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 279 | unsigned MaskNumElts = |
| 280 | cast<VectorType>(UserInstr->getType())->getNumElements(); |
Piotr Sobczak | a861c9a | 2019-10-21 08:12:47 +0000 | [diff] [blame] | 281 | |
| 282 | UsedElts = APInt(VWidth, 0); |
| 283 | for (unsigned i = 0; i < MaskNumElts; i++) { |
| 284 | unsigned MaskVal = Shuffle->getMaskValue(i); |
| 285 | if (MaskVal == -1u || MaskVal >= 2 * VWidth) |
| 286 | continue; |
| 287 | if (Shuffle->getOperand(0) == V && (MaskVal < VWidth)) |
| 288 | UsedElts.setBit(MaskVal); |
| 289 | if (Shuffle->getOperand(1) == V && |
| 290 | ((MaskVal >= VWidth) && (MaskVal < 2 * VWidth))) |
| 291 | UsedElts.setBit(MaskVal - VWidth); |
| 292 | } |
| 293 | break; |
| 294 | } |
| 295 | default: |
| 296 | break; |
| 297 | } |
| 298 | return UsedElts; |
| 299 | } |
| 300 | |
| 301 | /// Find union of elements of V demanded by all its users. |
| 302 | /// If it is known by querying findDemandedEltsBySingleUser that |
| 303 | /// no user demands an element of V, then the corresponding bit |
| 304 | /// remains unset in the returned value. |
| 305 | static APInt findDemandedEltsByAllUsers(Value *V) { |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 306 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Piotr Sobczak | a861c9a | 2019-10-21 08:12:47 +0000 | [diff] [blame] | 307 | |
| 308 | APInt UnionUsedElts(VWidth, 0); |
| 309 | for (const Use &U : V->uses()) { |
| 310 | if (Instruction *I = dyn_cast<Instruction>(U.getUser())) { |
| 311 | UnionUsedElts |= findDemandedEltsBySingleUser(V, I); |
| 312 | } else { |
| 313 | UnionUsedElts = APInt::getAllOnesValue(VWidth); |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | if (UnionUsedElts.isAllOnesValue()) |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | return UnionUsedElts; |
| 322 | } |
| 323 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 324 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Sanjay Patel | 47b3b4b | 2018-12-05 21:57:51 +0000 | [diff] [blame] | 325 | Value *SrcVec = EI.getVectorOperand(); |
| 326 | Value *Index = EI.getIndexOperand(); |
| 327 | if (Value *V = SimplifyExtractElementInst(SrcVec, Index, |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 328 | SQ.getWithInstruction(&EI))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 329 | return replaceInstUsesWith(EI, V); |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 330 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 331 | // If extracting a specified index from the vector, see if we can recursively |
| 332 | // find a previously computed scalar that was inserted into the vector. |
Sanjay Patel | 47b3b4b | 2018-12-05 21:57:51 +0000 | [diff] [blame] | 333 | auto *IndexC = dyn_cast<ConstantInt>(Index); |
| 334 | if (IndexC) { |
Sanjay Patel | 7a52626 | 2018-09-24 16:39:03 +0000 | [diff] [blame] | 335 | unsigned NumElts = EI.getVectorOperandType()->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 336 | |
Simon Pilgrim | e7d032f | 2017-12-27 12:00:18 +0000 | [diff] [blame] | 337 | // InstSimplify should handle cases where the index is invalid. |
Sanjay Patel | 47b3b4b | 2018-12-05 21:57:51 +0000 | [diff] [blame] | 338 | if (!IndexC->getValue().ule(NumElts)) |
Simon Pilgrim | e7d032f | 2017-12-27 12:00:18 +0000 | [diff] [blame] | 339 | return nullptr; |
| 340 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 341 | // This instruction only demands the single element from the input vector. |
Piotr Sobczak | a861c9a | 2019-10-21 08:12:47 +0000 | [diff] [blame] | 342 | if (NumElts != 1) { |
| 343 | // If the input vector has a single use, simplify it based on this use |
| 344 | // property. |
| 345 | if (SrcVec->hasOneUse()) { |
| 346 | APInt UndefElts(NumElts, 0); |
| 347 | APInt DemandedElts(NumElts, 0); |
| 348 | DemandedElts.setBit(IndexC->getZExtValue()); |
| 349 | if (Value *V = |
Nikita Popov | 53d2090 | 2020-03-29 20:07:46 +0200 | [diff] [blame] | 350 | SimplifyDemandedVectorElts(SrcVec, DemandedElts, UndefElts)) |
| 351 | return replaceOperand(EI, 0, V); |
Piotr Sobczak | a861c9a | 2019-10-21 08:12:47 +0000 | [diff] [blame] | 352 | } else { |
| 353 | // If the input vector has multiple uses, simplify it based on a union |
| 354 | // of all elements used. |
| 355 | APInt DemandedElts = findDemandedEltsByAllUsers(SrcVec); |
| 356 | if (!DemandedElts.isAllOnesValue()) { |
| 357 | APInt UndefElts(NumElts, 0); |
| 358 | if (Value *V = SimplifyDemandedVectorElts( |
| 359 | SrcVec, DemandedElts, UndefElts, 0 /* Depth */, |
| 360 | true /* AllowMultipleUsers */)) { |
| 361 | if (V != SrcVec) { |
| 362 | SrcVec->replaceAllUsesWith(V); |
| 363 | return &EI; |
| 364 | } |
| 365 | } |
| 366 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
Sanjay Patel | 31b0719 | 2018-10-01 14:40:00 +0000 | [diff] [blame] | 369 | if (Instruction *I = foldBitcastExtElt(EI, Builder, DL.isBigEndian())) |
Sanjay Patel | 4674c77 | 2018-09-24 20:41:22 +0000 | [diff] [blame] | 370 | return I; |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 371 | |
| 372 | // If there's a vector PHI feeding a scalar use through this extractelement |
| 373 | // instruction, try to scalarize the PHI. |
Sanjay Patel | 47b3b4b | 2018-12-05 21:57:51 +0000 | [diff] [blame] | 374 | if (auto *Phi = dyn_cast<PHINode>(SrcVec)) |
| 375 | if (Instruction *ScalarPHI = scalarizePHI(EI, Phi)) |
| 376 | return ScalarPHI; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 377 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 378 | |
Simon Moll | d871ef4 | 2020-03-10 16:05:31 +0100 | [diff] [blame] | 379 | // TODO come up with a n-ary matcher that subsumes both unary and |
| 380 | // binary matchers. |
| 381 | UnaryOperator *UO; |
| 382 | if (match(SrcVec, m_UnOp(UO)) && cheapToScalarize(SrcVec, IndexC)) { |
| 383 | // extelt (unop X), Index --> unop (extelt X, Index) |
| 384 | Value *X = UO->getOperand(0); |
| 385 | Value *E = Builder.CreateExtractElement(X, Index); |
| 386 | return UnaryOperator::CreateWithCopiedFlags(UO->getOpcode(), E, UO); |
| 387 | } |
| 388 | |
Sanjay Patel | 47b3b4b | 2018-12-05 21:57:51 +0000 | [diff] [blame] | 389 | BinaryOperator *BO; |
| 390 | if (match(SrcVec, m_BinOp(BO)) && cheapToScalarize(SrcVec, IndexC)) { |
| 391 | // extelt (binop X, Y), Index --> binop (extelt X, Index), (extelt Y, Index) |
| 392 | Value *X = BO->getOperand(0), *Y = BO->getOperand(1); |
| 393 | Value *E0 = Builder.CreateExtractElement(X, Index); |
| 394 | Value *E1 = Builder.CreateExtractElement(Y, Index); |
| 395 | return BinaryOperator::CreateWithCopiedFlags(BO->getOpcode(), E0, E1, BO); |
| 396 | } |
| 397 | |
Matt Arsenault | 9ccde61 | 2018-12-10 21:50:54 +0000 | [diff] [blame] | 398 | Value *X, *Y; |
| 399 | CmpInst::Predicate Pred; |
| 400 | if (match(SrcVec, m_Cmp(Pred, m_Value(X), m_Value(Y))) && |
| 401 | cheapToScalarize(SrcVec, IndexC)) { |
| 402 | // extelt (cmp X, Y), Index --> cmp (extelt X, Index), (extelt Y, Index) |
| 403 | Value *E0 = Builder.CreateExtractElement(X, Index); |
| 404 | Value *E1 = Builder.CreateExtractElement(Y, Index); |
| 405 | return CmpInst::Create(cast<CmpInst>(SrcVec)->getOpcode(), Pred, E0, E1); |
| 406 | } |
| 407 | |
Sanjay Patel | 47b3b4b | 2018-12-05 21:57:51 +0000 | [diff] [blame] | 408 | if (auto *I = dyn_cast<Instruction>(SrcVec)) { |
| 409 | if (auto *IE = dyn_cast<InsertElementInst>(I)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 410 | // Extracting the inserted element? |
Sanjay Patel | 47b3b4b | 2018-12-05 21:57:51 +0000 | [diff] [blame] | 411 | if (IE->getOperand(2) == Index) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 412 | return replaceInstUsesWith(EI, IE->getOperand(1)); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 413 | // If the inserted and extracted elements are constants, they must not |
| 414 | // be the same value, extract from the pre-inserted value instead. |
Nikita Popov | 878cb38 | 2020-01-31 22:23:33 +0100 | [diff] [blame] | 415 | if (isa<Constant>(IE->getOperand(2)) && IndexC) |
| 416 | return replaceOperand(EI, 0, IE->getOperand(0)); |
Sanjay Patel | 47b3b4b | 2018-12-05 21:57:51 +0000 | [diff] [blame] | 417 | } else if (auto *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 418 | // If this is extracting an element from a shufflevector, figure out where |
| 419 | // it came from and extract from the appropriate input element instead. |
Sanjay Patel | 47b3b4b | 2018-12-05 21:57:51 +0000 | [diff] [blame] | 420 | if (auto *Elt = dyn_cast<ConstantInt>(Index)) { |
Eli Friedman | 303c81c | 2011-10-21 19:11:34 +0000 | [diff] [blame] | 421 | int SrcIdx = SVI->getMaskValue(Elt->getZExtValue()); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 422 | Value *Src; |
| 423 | unsigned LHSWidth = |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 424 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 425 | |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 426 | if (SrcIdx < 0) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 427 | return replaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 428 | if (SrcIdx < (int)LHSWidth) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 429 | Src = SVI->getOperand(0); |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 430 | else { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 431 | SrcIdx -= LHSWidth; |
| 432 | Src = SVI->getOperand(1); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 433 | } |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 434 | Type *Int32Ty = Type::getInt32Ty(EI.getContext()); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 435 | return ExtractElementInst::Create(Src, |
Bob Wilson | 9d07f39 | 2010-10-29 22:03:07 +0000 | [diff] [blame] | 436 | ConstantInt::get(Int32Ty, |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 437 | SrcIdx, false)); |
| 438 | } |
Sanjay Patel | 47b3b4b | 2018-12-05 21:57:51 +0000 | [diff] [blame] | 439 | } else if (auto *CI = dyn_cast<CastInst>(I)) { |
Sanjay Patel | b67076c | 2015-11-29 22:09:34 +0000 | [diff] [blame] | 440 | // Canonicalize extractelement(cast) -> cast(extractelement). |
| 441 | // Bitcasts can change the number of vector elements, and they cost |
| 442 | // nothing. |
Anat Shemer | 5570318 | 2013-04-18 19:56:44 +0000 | [diff] [blame] | 443 | if (CI->hasOneUse() && (CI->getOpcode() != Instruction::BitCast)) { |
Sanjay Patel | 47b3b4b | 2018-12-05 21:57:51 +0000 | [diff] [blame] | 444 | Value *EE = Builder.CreateExtractElement(CI->getOperand(0), Index); |
Nadav Rotem | d74b72b | 2011-03-31 22:57:29 +0000 | [diff] [blame] | 445 | return CastInst::Create(CI->getOpcode(), EE, EI.getType()); |
| 446 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 447 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 448 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 449 | return nullptr; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 452 | /// If V is a shuffle of values that ONLY returns elements from either LHS or |
| 453 | /// RHS, return the shuffle mask and true. Otherwise, return false. |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 454 | static bool collectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 455 | SmallVectorImpl<Constant*> &Mask) { |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 456 | assert(LHS->getType() == RHS->getType() && |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 457 | "Invalid CollectSingleShuffleElements"); |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 458 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 459 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 460 | if (isa<UndefValue>(V)) { |
| 461 | Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); |
| 462 | return true; |
| 463 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 464 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 465 | if (V == LHS) { |
| 466 | for (unsigned i = 0; i != NumElts; ++i) |
| 467 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); |
| 468 | return true; |
| 469 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 470 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 471 | if (V == RHS) { |
| 472 | for (unsigned i = 0; i != NumElts; ++i) |
| 473 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 474 | i+NumElts)); |
| 475 | return true; |
| 476 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 477 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 478 | if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 479 | // If this is an insert of an extract from some other vector, include it. |
| 480 | Value *VecOp = IEI->getOperand(0); |
| 481 | Value *ScalarOp = IEI->getOperand(1); |
| 482 | Value *IdxOp = IEI->getOperand(2); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 483 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 484 | if (!isa<ConstantInt>(IdxOp)) |
| 485 | return false; |
| 486 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 487 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 488 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
Sanjay Patel | 70af1fd | 2014-07-07 22:13:58 +0000 | [diff] [blame] | 489 | // We can handle this if the vector we are inserting into is |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 490 | // transitively ok. |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 491 | if (collectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 492 | // If so, update the mask to reflect the inserted undef. |
| 493 | Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext())); |
| 494 | return true; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 495 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 496 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 497 | if (isa<ConstantInt>(EI->getOperand(1))) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 498 | unsigned ExtractedIdx = |
| 499 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 500 | unsigned NumLHSElts = |
| 501 | cast<VectorType>(LHS->getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 502 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 503 | // This must be extracting from either LHS or RHS. |
| 504 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
Sanjay Patel | 70af1fd | 2014-07-07 22:13:58 +0000 | [diff] [blame] | 505 | // We can handle this if the vector we are inserting into is |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 506 | // transitively ok. |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 507 | if (collectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 508 | // If so, update the mask to reflect the inserted value. |
| 509 | if (EI->getOperand(0) == LHS) { |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 510 | Mask[InsertedIdx % NumElts] = |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 511 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 512 | ExtractedIdx); |
| 513 | } else { |
| 514 | assert(EI->getOperand(0) == RHS); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 515 | Mask[InsertedIdx % NumElts] = |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 516 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 517 | ExtractedIdx + NumLHSElts); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 518 | } |
| 519 | return true; |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 525 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 526 | return false; |
| 527 | } |
| 528 | |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 529 | /// If we have insertion into a vector that is wider than the vector that we |
| 530 | /// are extracting from, try to widen the source vector to allow a single |
| 531 | /// shufflevector to replace one or more insert/extract pairs. |
| 532 | static void replaceExtractElements(InsertElementInst *InsElt, |
| 533 | ExtractElementInst *ExtElt, |
| 534 | InstCombiner &IC) { |
| 535 | VectorType *InsVecType = InsElt->getType(); |
| 536 | VectorType *ExtVecType = ExtElt->getVectorOperandType(); |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 537 | unsigned NumInsElts = InsVecType->getNumElements(); |
| 538 | unsigned NumExtElts = ExtVecType->getNumElements(); |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 539 | |
| 540 | // The inserted-to vector must be wider than the extracted-from vector. |
| 541 | if (InsVecType->getElementType() != ExtVecType->getElementType() || |
| 542 | NumExtElts >= NumInsElts) |
| 543 | return; |
| 544 | |
| 545 | // Create a shuffle mask to widen the extended-from vector using undefined |
| 546 | // values. The mask selects all of the values of the original vector followed |
| 547 | // by as many undefined values as needed to create a vector of the same length |
| 548 | // as the inserted-to vector. |
| 549 | SmallVector<Constant *, 16> ExtendMask; |
| 550 | IntegerType *IntType = Type::getInt32Ty(InsElt->getContext()); |
| 551 | for (unsigned i = 0; i < NumExtElts; ++i) |
| 552 | ExtendMask.push_back(ConstantInt::get(IntType, i)); |
| 553 | for (unsigned i = NumExtElts; i < NumInsElts; ++i) |
| 554 | ExtendMask.push_back(UndefValue::get(IntType)); |
| 555 | |
| 556 | Value *ExtVecOp = ExtElt->getVectorOperand(); |
Sanjay Patel | 66fff73 | 2016-01-29 20:21:02 +0000 | [diff] [blame] | 557 | auto *ExtVecOpInst = dyn_cast<Instruction>(ExtVecOp); |
| 558 | BasicBlock *InsertionBlock = (ExtVecOpInst && !isa<PHINode>(ExtVecOpInst)) |
| 559 | ? ExtVecOpInst->getParent() |
| 560 | : ExtElt->getParent(); |
| 561 | |
| 562 | // TODO: This restriction matches the basic block check below when creating |
| 563 | // new extractelement instructions. If that limitation is removed, this one |
| 564 | // could also be removed. But for now, we just bail out to ensure that we |
| 565 | // will replace the extractelement instruction that is feeding our |
| 566 | // insertelement instruction. This allows the insertelement to then be |
| 567 | // replaced by a shufflevector. If the insertelement is not replaced, we can |
| 568 | // induce infinite looping because there's an optimization for extractelement |
| 569 | // that will delete our widening shuffle. This would trigger another attempt |
| 570 | // here to create that shuffle, and we spin forever. |
| 571 | if (InsertionBlock != InsElt->getParent()) |
| 572 | return; |
| 573 | |
Sanjay Patel | 4e1b5a5 | 2016-11-10 00:15:14 +0000 | [diff] [blame] | 574 | // TODO: This restriction matches the check in visitInsertElementInst() and |
| 575 | // prevents an infinite loop caused by not turning the extract/insert pair |
| 576 | // into a shuffle. We really should not need either check, but we're lacking |
| 577 | // folds for shufflevectors because we're afraid to generate shuffle masks |
| 578 | // that the backend can't handle. |
| 579 | if (InsElt->hasOneUse() && isa<InsertElementInst>(InsElt->user_back())) |
| 580 | return; |
| 581 | |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 582 | auto *WideVec = new ShuffleVectorInst(ExtVecOp, UndefValue::get(ExtVecType), |
| 583 | ConstantVector::get(ExtendMask)); |
| 584 | |
Sanjay Patel | a1c5347 | 2016-01-05 19:09:47 +0000 | [diff] [blame] | 585 | // Insert the new shuffle after the vector operand of the extract is defined |
Sanjay Patel | d72a458 | 2016-01-08 01:39:16 +0000 | [diff] [blame] | 586 | // (as long as it's not a PHI) or at the start of the basic block of the |
| 587 | // extract, so any subsequent extracts in the same basic block can use it. |
| 588 | // TODO: Insert before the earliest ExtractElementInst that is replaced. |
Sanjay Patel | d72a458 | 2016-01-08 01:39:16 +0000 | [diff] [blame] | 589 | if (ExtVecOpInst && !isa<PHINode>(ExtVecOpInst)) |
Sanjay Patel | a1c5347 | 2016-01-05 19:09:47 +0000 | [diff] [blame] | 590 | WideVec->insertAfter(ExtVecOpInst); |
Sanjay Patel | d72a458 | 2016-01-08 01:39:16 +0000 | [diff] [blame] | 591 | else |
Sanjay Patel | a1c5347 | 2016-01-05 19:09:47 +0000 | [diff] [blame] | 592 | IC.InsertNewInstWith(WideVec, *ExtElt->getParent()->getFirstInsertionPt()); |
Sanjay Patel | a1c5347 | 2016-01-05 19:09:47 +0000 | [diff] [blame] | 593 | |
| 594 | // Replace extracts from the original narrow vector with extracts from the new |
| 595 | // wide vector. |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 596 | for (User *U : ExtVecOp->users()) { |
Sanjay Patel | a1c5347 | 2016-01-05 19:09:47 +0000 | [diff] [blame] | 597 | ExtractElementInst *OldExt = dyn_cast<ExtractElementInst>(U); |
Sanjay Patel | d72a458 | 2016-01-08 01:39:16 +0000 | [diff] [blame] | 598 | if (!OldExt || OldExt->getParent() != WideVec->getParent()) |
Sanjay Patel | a1c5347 | 2016-01-05 19:09:47 +0000 | [diff] [blame] | 599 | continue; |
| 600 | auto *NewExt = ExtractElementInst::Create(WideVec, OldExt->getOperand(1)); |
Sven van Haastregt | 78819e0 | 2017-06-05 09:18:10 +0000 | [diff] [blame] | 601 | NewExt->insertAfter(OldExt); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 602 | IC.replaceInstUsesWith(*OldExt, NewExt); |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 605 | |
| 606 | /// We are building a shuffle to create V, which is a sequence of insertelement, |
| 607 | /// extractelement pairs. If PermittedRHS is set, then we must either use it or |
Sanjay Patel | 70af1fd | 2014-07-07 22:13:58 +0000 | [diff] [blame] | 608 | /// not rely on the second vector source. Return a std::pair containing the |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 609 | /// left and right vectors of the proposed shuffle (or 0), and set the Mask |
| 610 | /// parameter as required. |
| 611 | /// |
| 612 | /// Note: we intentionally don't try to fold earlier shuffles since they have |
| 613 | /// often been chosen carefully to be efficiently implementable on the target. |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 614 | using ShuffleOps = std::pair<Value *, Value *>; |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 615 | |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 616 | static ShuffleOps collectShuffleElements(Value *V, |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 617 | SmallVectorImpl<Constant *> &Mask, |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 618 | Value *PermittedRHS, |
| 619 | InstCombiner &IC) { |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 620 | assert(V->getType()->isVectorTy() && "Invalid shuffle!"); |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 621 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 622 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 623 | if (isa<UndefValue>(V)) { |
| 624 | Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 625 | return std::make_pair( |
| 626 | PermittedRHS ? UndefValue::get(PermittedRHS->getType()) : V, nullptr); |
Chris Lattner | a0d01ff | 2012-01-24 14:31:22 +0000 | [diff] [blame] | 627 | } |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 628 | |
Chris Lattner | a0d01ff | 2012-01-24 14:31:22 +0000 | [diff] [blame] | 629 | if (isa<ConstantAggregateZero>(V)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 630 | Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0)); |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 631 | return std::make_pair(V, nullptr); |
Chris Lattner | a0d01ff | 2012-01-24 14:31:22 +0000 | [diff] [blame] | 632 | } |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 633 | |
Chris Lattner | a0d01ff | 2012-01-24 14:31:22 +0000 | [diff] [blame] | 634 | if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 635 | // If this is an insert of an extract from some other vector, include it. |
| 636 | Value *VecOp = IEI->getOperand(0); |
| 637 | Value *ScalarOp = IEI->getOperand(1); |
| 638 | Value *IdxOp = IEI->getOperand(2); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 639 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 640 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 641 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 642 | unsigned ExtractedIdx = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 643 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 644 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 645 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 646 | // Either the extracted from or inserted into vector must be RHSVec, |
| 647 | // otherwise we'd end up with a shuffle of three inputs. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 648 | if (EI->getOperand(0) == PermittedRHS || PermittedRHS == nullptr) { |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 649 | Value *RHS = EI->getOperand(0); |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 650 | ShuffleOps LR = collectShuffleElements(VecOp, Mask, RHS, IC); |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 651 | assert(LR.second == nullptr || LR.second == RHS); |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 652 | |
| 653 | if (LR.first->getType() != RHS->getType()) { |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 654 | // Although we are giving up for now, see if we can create extracts |
| 655 | // that match the inserts for another round of combining. |
| 656 | replaceExtractElements(IEI, EI, IC); |
| 657 | |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 658 | // We tried our best, but we can't find anything compatible with RHS |
| 659 | // further up the chain. Return a trivial shuffle. |
| 660 | for (unsigned i = 0; i < NumElts; ++i) |
| 661 | Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()), i); |
| 662 | return std::make_pair(V, nullptr); |
| 663 | } |
| 664 | |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 665 | unsigned NumLHSElts = |
| 666 | cast<VectorType>(RHS->getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 667 | Mask[InsertedIdx % NumElts] = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 668 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 669 | NumLHSElts+ExtractedIdx); |
| 670 | return std::make_pair(LR.first, RHS); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 671 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 672 | |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 673 | if (VecOp == PermittedRHS) { |
| 674 | // We've gone as far as we can: anything on the other side of the |
| 675 | // extractelement will already have been converted into a shuffle. |
| 676 | unsigned NumLHSElts = |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 677 | cast<VectorType>(EI->getOperand(0)->getType())->getNumElements(); |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 678 | for (unsigned i = 0; i != NumElts; ++i) |
| 679 | Mask.push_back(ConstantInt::get( |
| 680 | Type::getInt32Ty(V->getContext()), |
| 681 | i == InsertedIdx ? ExtractedIdx : NumLHSElts + i)); |
| 682 | return std::make_pair(EI->getOperand(0), PermittedRHS); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 683 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 684 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 685 | // If this insertelement is a chain that comes from exactly these two |
| 686 | // vectors, return the vector and the effective shuffle. |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 687 | if (EI->getOperand(0)->getType() == PermittedRHS->getType() && |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 688 | collectSingleShuffleElements(IEI, EI->getOperand(0), PermittedRHS, |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 689 | Mask)) |
| 690 | return std::make_pair(EI->getOperand(0), PermittedRHS); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 694 | |
Sanjay Patel | b67076c | 2015-11-29 22:09:34 +0000 | [diff] [blame] | 695 | // Otherwise, we can't do anything fancy. Return an identity vector. |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 696 | for (unsigned i = 0; i != NumElts; ++i) |
| 697 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 698 | return std::make_pair(V, nullptr); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 701 | /// Try to find redundant insertvalue instructions, like the following ones: |
| 702 | /// %0 = insertvalue { i8, i32 } undef, i8 %x, 0 |
| 703 | /// %1 = insertvalue { i8, i32 } %0, i8 %y, 0 |
| 704 | /// Here the second instruction inserts values at the same indices, as the |
| 705 | /// first one, making the first one redundant. |
| 706 | /// It should be transformed to: |
| 707 | /// %0 = insertvalue { i8, i32 } undef, i8 %y, 0 |
| 708 | Instruction *InstCombiner::visitInsertValueInst(InsertValueInst &I) { |
| 709 | bool IsRedundant = false; |
| 710 | ArrayRef<unsigned int> FirstIndices = I.getIndices(); |
| 711 | |
| 712 | // If there is a chain of insertvalue instructions (each of them except the |
| 713 | // last one has only one use and it's another insertvalue insn from this |
| 714 | // chain), check if any of the 'children' uses the same indices as the first |
| 715 | // instruction. In this case, the first one is redundant. |
| 716 | Value *V = &I; |
Michael Zolotukhin | 292d3ca | 2014-05-08 19:50:24 +0000 | [diff] [blame] | 717 | unsigned Depth = 0; |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 718 | while (V->hasOneUse() && Depth < 10) { |
| 719 | User *U = V->user_back(); |
Michael Zolotukhin | 292d3ca | 2014-05-08 19:50:24 +0000 | [diff] [blame] | 720 | auto UserInsInst = dyn_cast<InsertValueInst>(U); |
| 721 | if (!UserInsInst || U->getOperand(0) != V) |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 722 | break; |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 723 | if (UserInsInst->getIndices() == FirstIndices) { |
| 724 | IsRedundant = true; |
| 725 | break; |
| 726 | } |
| 727 | V = UserInsInst; |
| 728 | Depth++; |
| 729 | } |
| 730 | |
| 731 | if (IsRedundant) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 732 | return replaceInstUsesWith(I, I.getOperand(0)); |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 733 | return nullptr; |
| 734 | } |
| 735 | |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame] | 736 | static bool isShuffleEquivalentToSelect(ShuffleVectorInst &Shuf) { |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 737 | int MaskSize = Shuf.getShuffleMask().size(); |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 738 | int VecSize = |
| 739 | cast<VectorType>(Shuf.getOperand(0)->getType())->getNumElements(); |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame] | 740 | |
| 741 | // A vector select does not change the size of the operands. |
| 742 | if (MaskSize != VecSize) |
| 743 | return false; |
| 744 | |
| 745 | // Each mask element must be undefined or choose a vector element from one of |
| 746 | // the source operands without crossing vector lanes. |
| 747 | for (int i = 0; i != MaskSize; ++i) { |
| 748 | int Elt = Shuf.getMaskValue(i); |
| 749 | if (Elt != -1 && Elt != i && Elt != i + VecSize) |
| 750 | return false; |
| 751 | } |
| 752 | |
| 753 | return true; |
| 754 | } |
| 755 | |
Sanjay Patel | 71ad227 | 2019-06-26 15:52:59 +0000 | [diff] [blame] | 756 | /// Turn a chain of inserts that splats a value into an insert + shuffle: |
| 757 | /// insertelt(insertelt(insertelt(insertelt X, %k, 0), %k, 1), %k, 2) ... -> |
| 758 | /// shufflevector(insertelt(X, %k, 0), undef, zero) |
| 759 | static Instruction *foldInsSequenceIntoSplat(InsertElementInst &InsElt) { |
| 760 | // We are interested in the last insert in a chain. So if this insert has a |
| 761 | // single user and that user is an insert, bail. |
Michael Kuperstein | cd7ad71 | 2016-12-28 00:18:08 +0000 | [diff] [blame] | 762 | if (InsElt.hasOneUse() && isa<InsertElementInst>(InsElt.user_back())) |
| 763 | return nullptr; |
| 764 | |
Sanjay Patel | 71ad227 | 2019-06-26 15:52:59 +0000 | [diff] [blame] | 765 | auto *VecTy = cast<VectorType>(InsElt.getType()); |
| 766 | unsigned NumElements = VecTy->getNumElements(); |
Michael Kuperstein | cd7ad71 | 2016-12-28 00:18:08 +0000 | [diff] [blame] | 767 | |
| 768 | // Do not try to do this for a one-element vector, since that's a nop, |
| 769 | // and will cause an inf-loop. |
| 770 | if (NumElements == 1) |
| 771 | return nullptr; |
| 772 | |
| 773 | Value *SplatVal = InsElt.getOperand(1); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 774 | InsertElementInst *CurrIE = &InsElt; |
Benjamin Kramer | ec228d7 | 2020-04-13 20:36:31 +0200 | [diff] [blame] | 775 | SmallBitVector ElementPresent(NumElements, false); |
Florian Hahn | b992fee | 2017-08-30 10:54:21 +0000 | [diff] [blame] | 776 | InsertElementInst *FirstIE = nullptr; |
Michael Kuperstein | cd7ad71 | 2016-12-28 00:18:08 +0000 | [diff] [blame] | 777 | |
| 778 | // Walk the chain backwards, keeping track of which indices we inserted into, |
| 779 | // until we hit something that isn't an insert of the splatted value. |
| 780 | while (CurrIE) { |
Sanjay Patel | 863d494 | 2017-11-27 18:19:32 +0000 | [diff] [blame] | 781 | auto *Idx = dyn_cast<ConstantInt>(CurrIE->getOperand(2)); |
Michael Kuperstein | cd7ad71 | 2016-12-28 00:18:08 +0000 | [diff] [blame] | 782 | if (!Idx || CurrIE->getOperand(1) != SplatVal) |
| 783 | return nullptr; |
| 784 | |
Sanjay Patel | 863d494 | 2017-11-27 18:19:32 +0000 | [diff] [blame] | 785 | auto *NextIE = dyn_cast<InsertElementInst>(CurrIE->getOperand(0)); |
Florian Hahn | b992fee | 2017-08-30 10:54:21 +0000 | [diff] [blame] | 786 | // Check none of the intermediate steps have any additional uses, except |
| 787 | // for the root insertelement instruction, which can be re-used, if it |
| 788 | // inserts at position 0. |
| 789 | if (CurrIE != &InsElt && |
| 790 | (!CurrIE->hasOneUse() && (NextIE != nullptr || !Idx->isZero()))) |
Michael Kuperstein | cd7ad71 | 2016-12-28 00:18:08 +0000 | [diff] [blame] | 791 | return nullptr; |
| 792 | |
| 793 | ElementPresent[Idx->getZExtValue()] = true; |
Florian Hahn | b992fee | 2017-08-30 10:54:21 +0000 | [diff] [blame] | 794 | FirstIE = CurrIE; |
| 795 | CurrIE = NextIE; |
Michael Kuperstein | cd7ad71 | 2016-12-28 00:18:08 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Sanjay Patel | 75b5edf | 2019-07-04 16:45:34 +0000 | [diff] [blame] | 798 | // If this is just a single insertelement (not a sequence), we are done. |
| 799 | if (FirstIE == &InsElt) |
Michael Kuperstein | cd7ad71 | 2016-12-28 00:18:08 +0000 | [diff] [blame] | 800 | return nullptr; |
| 801 | |
Sanjay Patel | 75b5edf | 2019-07-04 16:45:34 +0000 | [diff] [blame] | 802 | // If we are not inserting into an undef vector, make sure we've seen an |
| 803 | // insert into every element. |
| 804 | // TODO: If the base vector is not undef, it might be better to create a splat |
| 805 | // and then a select-shuffle (blend) with the base vector. |
| 806 | if (!isa<UndefValue>(FirstIE->getOperand(0))) |
Benjamin Kramer | ec228d7 | 2020-04-13 20:36:31 +0200 | [diff] [blame] | 807 | if (!ElementPresent.all()) |
Sanjay Patel | 75b5edf | 2019-07-04 16:45:34 +0000 | [diff] [blame] | 808 | return nullptr; |
| 809 | |
Sanjay Patel | 71ad227 | 2019-06-26 15:52:59 +0000 | [diff] [blame] | 810 | // Create the insert + shuffle. |
| 811 | Type *Int32Ty = Type::getInt32Ty(InsElt.getContext()); |
| 812 | UndefValue *UndefVec = UndefValue::get(VecTy); |
| 813 | Constant *Zero = ConstantInt::get(Int32Ty, 0); |
| 814 | if (!cast<ConstantInt>(FirstIE->getOperand(2))->isZero()) |
| 815 | FirstIE = InsertElementInst::Create(UndefVec, SplatVal, Zero, "", &InsElt); |
Michael Kuperstein | cd7ad71 | 2016-12-28 00:18:08 +0000 | [diff] [blame] | 816 | |
Sanjay Patel | 75b5edf | 2019-07-04 16:45:34 +0000 | [diff] [blame] | 817 | // Splat from element 0, but replace absent elements with undef in the mask. |
| 818 | SmallVector<Constant *, 16> Mask(NumElements, Zero); |
| 819 | for (unsigned i = 0; i != NumElements; ++i) |
| 820 | if (!ElementPresent[i]) |
| 821 | Mask[i] = UndefValue::get(Int32Ty); |
| 822 | |
| 823 | return new ShuffleVectorInst(FirstIE, UndefVec, ConstantVector::get(Mask)); |
Michael Kuperstein | cd7ad71 | 2016-12-28 00:18:08 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Sanjay Patel | 3dee113 | 2019-07-08 19:48:52 +0000 | [diff] [blame] | 826 | /// Try to fold an insert element into an existing splat shuffle by changing |
| 827 | /// the shuffle's mask to include the index of this insert element. |
| 828 | static Instruction *foldInsEltIntoSplat(InsertElementInst &InsElt) { |
| 829 | // Check if the vector operand of this insert is a canonical splat shuffle. |
| 830 | auto *Shuf = dyn_cast<ShuffleVectorInst>(InsElt.getOperand(0)); |
| 831 | if (!Shuf || !Shuf->isZeroEltSplat()) |
| 832 | return nullptr; |
| 833 | |
| 834 | // Check for a constant insertion index. |
| 835 | uint64_t IdxC; |
| 836 | if (!match(InsElt.getOperand(2), m_ConstantInt(IdxC))) |
| 837 | return nullptr; |
| 838 | |
| 839 | // Check if the splat shuffle's input is the same as this insert's scalar op. |
| 840 | Value *X = InsElt.getOperand(1); |
| 841 | Value *Op0 = Shuf->getOperand(0); |
| 842 | if (!match(Op0, m_InsertElement(m_Undef(), m_Specific(X), m_ZeroInt()))) |
| 843 | return nullptr; |
| 844 | |
| 845 | // Replace the shuffle mask element at the index of this insert with a zero. |
| 846 | // For example: |
| 847 | // inselt (shuf (inselt undef, X, 0), undef, <0,undef,0,undef>), X, 1 |
| 848 | // --> shuf (inselt undef, X, 0), undef, <0,0,0,undef> |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 849 | unsigned NumMaskElts = Shuf->getType()->getNumElements(); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 850 | SmallVector<int, 16> NewMask(NumMaskElts); |
Sanjay Patel | 3dee113 | 2019-07-08 19:48:52 +0000 | [diff] [blame] | 851 | for (unsigned i = 0; i != NumMaskElts; ++i) |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 852 | NewMask[i] = i == IdxC ? 0 : Shuf->getMaskValue(i); |
Sanjay Patel | 3dee113 | 2019-07-08 19:48:52 +0000 | [diff] [blame] | 853 | |
Sanjay Patel | 3dee113 | 2019-07-08 19:48:52 +0000 | [diff] [blame] | 854 | return new ShuffleVectorInst(Op0, UndefValue::get(Op0->getType()), NewMask); |
| 855 | } |
| 856 | |
Sanjay Patel | aff5bee | 2019-09-08 19:03:01 +0000 | [diff] [blame] | 857 | /// Try to fold an extract+insert element into an existing identity shuffle by |
| 858 | /// changing the shuffle's mask to include the index of this insert element. |
| 859 | static Instruction *foldInsEltIntoIdentityShuffle(InsertElementInst &InsElt) { |
| 860 | // Check if the vector operand of this insert is an identity shuffle. |
| 861 | auto *Shuf = dyn_cast<ShuffleVectorInst>(InsElt.getOperand(0)); |
| 862 | if (!Shuf || !isa<UndefValue>(Shuf->getOperand(1)) || |
| 863 | !(Shuf->isIdentityWithExtract() || Shuf->isIdentityWithPadding())) |
| 864 | return nullptr; |
| 865 | |
| 866 | // Check for a constant insertion index. |
| 867 | uint64_t IdxC; |
| 868 | if (!match(InsElt.getOperand(2), m_ConstantInt(IdxC))) |
| 869 | return nullptr; |
| 870 | |
| 871 | // Check if this insert's scalar op is extracted from the identity shuffle's |
| 872 | // input vector. |
| 873 | Value *Scalar = InsElt.getOperand(1); |
| 874 | Value *X = Shuf->getOperand(0); |
| 875 | if (!match(Scalar, m_ExtractElement(m_Specific(X), m_SpecificInt(IdxC)))) |
| 876 | return nullptr; |
| 877 | |
| 878 | // Replace the shuffle mask element at the index of this extract+insert with |
| 879 | // that same index value. |
| 880 | // For example: |
| 881 | // inselt (shuf X, IdMask), (extelt X, IdxC), IdxC --> shuf X, IdMask' |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 882 | unsigned NumMaskElts = Shuf->getType()->getNumElements(); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 883 | SmallVector<int, 16> NewMask(NumMaskElts); |
| 884 | ArrayRef<int> OldMask = Shuf->getShuffleMask(); |
Sanjay Patel | aff5bee | 2019-09-08 19:03:01 +0000 | [diff] [blame] | 885 | for (unsigned i = 0; i != NumMaskElts; ++i) { |
| 886 | if (i != IdxC) { |
| 887 | // All mask elements besides the inserted element remain the same. |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 888 | NewMask[i] = OldMask[i]; |
| 889 | } else if (OldMask[i] == (int)IdxC) { |
Sanjay Patel | aff5bee | 2019-09-08 19:03:01 +0000 | [diff] [blame] | 890 | // If the mask element was already set, there's nothing to do |
| 891 | // (demanded elements analysis may unset it later). |
| 892 | return nullptr; |
| 893 | } else { |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 894 | assert(OldMask[i] == UndefMaskElem && |
Sanjay Patel | aff5bee | 2019-09-08 19:03:01 +0000 | [diff] [blame] | 895 | "Unexpected shuffle mask element for identity shuffle"); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 896 | NewMask[i] = IdxC; |
Sanjay Patel | aff5bee | 2019-09-08 19:03:01 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
Sanjay Patel | aff5bee | 2019-09-08 19:03:01 +0000 | [diff] [blame] | 900 | return new ShuffleVectorInst(X, Shuf->getOperand(1), NewMask); |
| 901 | } |
| 902 | |
Sanjay Patel | 2f602ce | 2017-03-22 17:10:44 +0000 | [diff] [blame] | 903 | /// If we have an insertelement instruction feeding into another insertelement |
| 904 | /// and the 2nd is inserting a constant into the vector, canonicalize that |
| 905 | /// constant insertion before the insertion of a variable: |
| 906 | /// |
| 907 | /// insertelement (insertelement X, Y, IdxC1), ScalarC, IdxC2 --> |
| 908 | /// insertelement (insertelement X, ScalarC, IdxC2), Y, IdxC1 |
| 909 | /// |
| 910 | /// This has the potential of eliminating the 2nd insertelement instruction |
| 911 | /// via constant folding of the scalar constant into a vector constant. |
| 912 | static Instruction *hoistInsEltConst(InsertElementInst &InsElt2, |
| 913 | InstCombiner::BuilderTy &Builder) { |
| 914 | auto *InsElt1 = dyn_cast<InsertElementInst>(InsElt2.getOperand(0)); |
| 915 | if (!InsElt1 || !InsElt1->hasOneUse()) |
| 916 | return nullptr; |
| 917 | |
| 918 | Value *X, *Y; |
| 919 | Constant *ScalarC; |
| 920 | ConstantInt *IdxC1, *IdxC2; |
| 921 | if (match(InsElt1->getOperand(0), m_Value(X)) && |
| 922 | match(InsElt1->getOperand(1), m_Value(Y)) && !isa<Constant>(Y) && |
| 923 | match(InsElt1->getOperand(2), m_ConstantInt(IdxC1)) && |
| 924 | match(InsElt2.getOperand(1), m_Constant(ScalarC)) && |
| 925 | match(InsElt2.getOperand(2), m_ConstantInt(IdxC2)) && IdxC1 != IdxC2) { |
| 926 | Value *NewInsElt1 = Builder.CreateInsertElement(X, ScalarC, IdxC2); |
| 927 | return InsertElementInst::Create(NewInsElt1, Y, IdxC1); |
| 928 | } |
| 929 | |
| 930 | return nullptr; |
| 931 | } |
| 932 | |
Alexey Bataev | fee9078 | 2016-09-23 09:14:08 +0000 | [diff] [blame] | 933 | /// insertelt (shufflevector X, CVec, Mask|insertelt X, C1, CIndex1), C, CIndex |
| 934 | /// --> shufflevector X, CVec', Mask' |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame] | 935 | static Instruction *foldConstantInsEltIntoShuffle(InsertElementInst &InsElt) { |
Alexey Bataev | fee9078 | 2016-09-23 09:14:08 +0000 | [diff] [blame] | 936 | auto *Inst = dyn_cast<Instruction>(InsElt.getOperand(0)); |
| 937 | // Bail out if the parent has more than one use. In that case, we'd be |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame] | 938 | // replacing the insertelt with a shuffle, and that's not a clear win. |
Alexey Bataev | fee9078 | 2016-09-23 09:14:08 +0000 | [diff] [blame] | 939 | if (!Inst || !Inst->hasOneUse()) |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame] | 940 | return nullptr; |
Alexey Bataev | fee9078 | 2016-09-23 09:14:08 +0000 | [diff] [blame] | 941 | if (auto *Shuf = dyn_cast<ShuffleVectorInst>(InsElt.getOperand(0))) { |
| 942 | // The shuffle must have a constant vector operand. The insertelt must have |
| 943 | // a constant scalar being inserted at a constant position in the vector. |
| 944 | Constant *ShufConstVec, *InsEltScalar; |
| 945 | uint64_t InsEltIndex; |
| 946 | if (!match(Shuf->getOperand(1), m_Constant(ShufConstVec)) || |
| 947 | !match(InsElt.getOperand(1), m_Constant(InsEltScalar)) || |
| 948 | !match(InsElt.getOperand(2), m_ConstantInt(InsEltIndex))) |
| 949 | return nullptr; |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame] | 950 | |
Alexey Bataev | fee9078 | 2016-09-23 09:14:08 +0000 | [diff] [blame] | 951 | // Adding an element to an arbitrary shuffle could be expensive, but a |
| 952 | // shuffle that selects elements from vectors without crossing lanes is |
| 953 | // assumed cheap. |
| 954 | // If we're just adding a constant into that shuffle, it will still be |
| 955 | // cheap. |
| 956 | if (!isShuffleEquivalentToSelect(*Shuf)) |
| 957 | return nullptr; |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame] | 958 | |
Alexey Bataev | fee9078 | 2016-09-23 09:14:08 +0000 | [diff] [blame] | 959 | // From the above 'select' check, we know that the mask has the same number |
| 960 | // of elements as the vector input operands. We also know that each constant |
| 961 | // input element is used in its lane and can not be used more than once by |
| 962 | // the shuffle. Therefore, replace the constant in the shuffle's constant |
| 963 | // vector with the insertelt constant. Replace the constant in the shuffle's |
| 964 | // mask vector with the insertelt index plus the length of the vector |
| 965 | // (because the constant vector operand of a shuffle is always the 2nd |
| 966 | // operand). |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 967 | ArrayRef<int> Mask = Shuf->getShuffleMask(); |
| 968 | unsigned NumElts = Mask.size(); |
Alexey Bataev | fee9078 | 2016-09-23 09:14:08 +0000 | [diff] [blame] | 969 | SmallVector<Constant *, 16> NewShufElts(NumElts); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 970 | SmallVector<int, 16> NewMaskElts(NumElts); |
Alexey Bataev | fee9078 | 2016-09-23 09:14:08 +0000 | [diff] [blame] | 971 | for (unsigned I = 0; I != NumElts; ++I) { |
| 972 | if (I == InsEltIndex) { |
| 973 | NewShufElts[I] = InsEltScalar; |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 974 | NewMaskElts[I] = InsEltIndex + NumElts; |
Alexey Bataev | fee9078 | 2016-09-23 09:14:08 +0000 | [diff] [blame] | 975 | } else { |
| 976 | // Copy over the existing values. |
| 977 | NewShufElts[I] = ShufConstVec->getAggregateElement(I); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 978 | NewMaskElts[I] = Mask[I]; |
Alexey Bataev | fee9078 | 2016-09-23 09:14:08 +0000 | [diff] [blame] | 979 | } |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame] | 980 | } |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame] | 981 | |
Alexey Bataev | fee9078 | 2016-09-23 09:14:08 +0000 | [diff] [blame] | 982 | // Create new operands for a shuffle that includes the constant of the |
| 983 | // original insertelt. The old shuffle will be dead now. |
| 984 | return new ShuffleVectorInst(Shuf->getOperand(0), |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 985 | ConstantVector::get(NewShufElts), NewMaskElts); |
Alexey Bataev | fee9078 | 2016-09-23 09:14:08 +0000 | [diff] [blame] | 986 | } else if (auto *IEI = dyn_cast<InsertElementInst>(Inst)) { |
| 987 | // Transform sequences of insertelements ops with constant data/indexes into |
| 988 | // a single shuffle op. |
| 989 | unsigned NumElts = InsElt.getType()->getNumElements(); |
| 990 | |
| 991 | uint64_t InsertIdx[2]; |
| 992 | Constant *Val[2]; |
| 993 | if (!match(InsElt.getOperand(2), m_ConstantInt(InsertIdx[0])) || |
| 994 | !match(InsElt.getOperand(1), m_Constant(Val[0])) || |
| 995 | !match(IEI->getOperand(2), m_ConstantInt(InsertIdx[1])) || |
| 996 | !match(IEI->getOperand(1), m_Constant(Val[1]))) |
| 997 | return nullptr; |
| 998 | SmallVector<Constant *, 16> Values(NumElts); |
| 999 | SmallVector<Constant *, 16> Mask(NumElts); |
| 1000 | auto ValI = std::begin(Val); |
| 1001 | // Generate new constant vector and mask. |
| 1002 | // We have 2 values/masks from the insertelements instructions. Insert them |
| 1003 | // into new value/mask vectors. |
| 1004 | for (uint64_t I : InsertIdx) { |
| 1005 | if (!Values[I]) { |
| 1006 | assert(!Mask[I]); |
| 1007 | Values[I] = *ValI; |
| 1008 | Mask[I] = ConstantInt::get(Type::getInt32Ty(InsElt.getContext()), |
| 1009 | NumElts + I); |
| 1010 | } |
| 1011 | ++ValI; |
| 1012 | } |
| 1013 | // Remaining values are filled with 'undef' values. |
| 1014 | for (unsigned I = 0; I < NumElts; ++I) { |
| 1015 | if (!Values[I]) { |
| 1016 | assert(!Mask[I]); |
| 1017 | Values[I] = UndefValue::get(InsElt.getType()->getElementType()); |
| 1018 | Mask[I] = ConstantInt::get(Type::getInt32Ty(InsElt.getContext()), I); |
| 1019 | } |
| 1020 | } |
| 1021 | // Create new operands for a shuffle that includes the constant of the |
| 1022 | // original insertelt. |
| 1023 | return new ShuffleVectorInst(IEI->getOperand(0), |
| 1024 | ConstantVector::get(Values), |
| 1025 | ConstantVector::get(Mask)); |
| 1026 | } |
| 1027 | return nullptr; |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1030 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 1031 | Value *VecOp = IE.getOperand(0); |
| 1032 | Value *ScalarOp = IE.getOperand(1); |
| 1033 | Value *IdxOp = IE.getOperand(2); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1034 | |
Igor Laevsky | e0edb66 | 2017-12-13 11:21:18 +0000 | [diff] [blame] | 1035 | if (auto *V = SimplifyInsertElementInst( |
| 1036 | VecOp, ScalarOp, IdxOp, SQ.getWithInstruction(&IE))) |
| 1037 | return replaceInstUsesWith(IE, V); |
| 1038 | |
Sanjay Patel | 926e477 | 2019-05-17 18:06:12 +0000 | [diff] [blame] | 1039 | // If the vector and scalar are both bitcast from the same element type, do |
| 1040 | // the insert in that source type followed by bitcast. |
| 1041 | Value *VecSrc, *ScalarSrc; |
| 1042 | if (match(VecOp, m_BitCast(m_Value(VecSrc))) && |
| 1043 | match(ScalarOp, m_BitCast(m_Value(ScalarSrc))) && |
| 1044 | (VecOp->hasOneUse() || ScalarOp->hasOneUse()) && |
| 1045 | VecSrc->getType()->isVectorTy() && !ScalarSrc->getType()->isVectorTy() && |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1046 | cast<VectorType>(VecSrc->getType())->getElementType() == |
| 1047 | ScalarSrc->getType()) { |
Sanjay Patel | 926e477 | 2019-05-17 18:06:12 +0000 | [diff] [blame] | 1048 | // inselt (bitcast VecSrc), (bitcast ScalarSrc), IdxOp --> |
| 1049 | // bitcast (inselt VecSrc, ScalarSrc, IdxOp) |
| 1050 | Value *NewInsElt = Builder.CreateInsertElement(VecSrc, ScalarSrc, IdxOp); |
| 1051 | return new BitCastInst(NewInsElt, IE.getType()); |
| 1052 | } |
| 1053 | |
Sanjay Patel | 0522b0d | 2018-10-20 17:15:57 +0000 | [diff] [blame] | 1054 | // If the inserted element was extracted from some other vector and both |
Sanjay Patel | 9317963 | 2019-05-26 14:03:50 +0000 | [diff] [blame] | 1055 | // indexes are valid constants, try to turn this into a shuffle. |
Sanjay Patel | 0522b0d | 2018-10-20 17:15:57 +0000 | [diff] [blame] | 1056 | uint64_t InsertedIdx, ExtractedIdx; |
| 1057 | Value *ExtVecOp; |
| 1058 | if (match(IdxOp, m_ConstantInt(InsertedIdx)) && |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1059 | match(ScalarOp, |
| 1060 | m_ExtractElement(m_Value(ExtVecOp), m_ConstantInt(ExtractedIdx))) && |
| 1061 | ExtractedIdx < cast<VectorType>(ExtVecOp->getType())->getNumElements()) { |
Sanjay Patel | 0522b0d | 2018-10-20 17:15:57 +0000 | [diff] [blame] | 1062 | // TODO: Looking at the user(s) to determine if this insert is a |
| 1063 | // fold-to-shuffle opportunity does not match the usual instcombine |
| 1064 | // constraints. We should decide if the transform is worthy based only |
| 1065 | // on this instruction and its operands, but that may not work currently. |
| 1066 | // |
| 1067 | // Here, we are trying to avoid creating shuffles before reaching |
| 1068 | // the end of a chain of extract-insert pairs. This is complicated because |
| 1069 | // we do not generally form arbitrary shuffle masks in instcombine |
| 1070 | // (because those may codegen poorly), but collectShuffleElements() does |
| 1071 | // exactly that. |
| 1072 | // |
| 1073 | // The rules for determining what is an acceptable target-independent |
| 1074 | // shuffle mask are fuzzy because they evolve based on the backend's |
| 1075 | // capabilities and real-world impact. |
| 1076 | auto isShuffleRootCandidate = [](InsertElementInst &Insert) { |
| 1077 | if (!Insert.hasOneUse()) |
| 1078 | return true; |
| 1079 | auto *InsertUser = dyn_cast<InsertElementInst>(Insert.user_back()); |
| 1080 | if (!InsertUser) |
| 1081 | return true; |
| 1082 | return false; |
| 1083 | }; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1084 | |
Sanjay Patel | 0522b0d | 2018-10-20 17:15:57 +0000 | [diff] [blame] | 1085 | // Try to form a shuffle from a chain of extract-insert ops. |
| 1086 | if (isShuffleRootCandidate(IE)) { |
| 1087 | SmallVector<Constant*, 16> Mask; |
| 1088 | ShuffleOps LR = collectShuffleElements(&IE, Mask, nullptr, *this); |
Sanjay Patel | 729c436 | 2018-10-20 16:25:55 +0000 | [diff] [blame] | 1089 | |
Sanjay Patel | 0522b0d | 2018-10-20 17:15:57 +0000 | [diff] [blame] | 1090 | // The proposed shuffle may be trivial, in which case we shouldn't |
| 1091 | // perform the combine. |
| 1092 | if (LR.first != &IE && LR.second != &IE) { |
| 1093 | // We now have a shuffle of LHS, RHS, Mask. |
| 1094 | if (LR.second == nullptr) |
| 1095 | LR.second = UndefValue::get(LR.first->getType()); |
| 1096 | return new ShuffleVectorInst(LR.first, LR.second, |
| 1097 | ConstantVector::get(Mask)); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1101 | |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1102 | unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements(); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1103 | APInt UndefElts(VWidth, 0); |
| 1104 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
Eli Friedman | ef200db | 2011-02-19 22:42:40 +0000 | [diff] [blame] | 1105 | if (Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) { |
| 1106 | if (V != &IE) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1107 | return replaceInstUsesWith(IE, V); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1108 | return &IE; |
Eli Friedman | ef200db | 2011-02-19 22:42:40 +0000 | [diff] [blame] | 1109 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1110 | |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame] | 1111 | if (Instruction *Shuf = foldConstantInsEltIntoShuffle(IE)) |
| 1112 | return Shuf; |
| 1113 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1114 | if (Instruction *NewInsElt = hoistInsEltConst(IE, Builder)) |
Sanjay Patel | 2f602ce | 2017-03-22 17:10:44 +0000 | [diff] [blame] | 1115 | return NewInsElt; |
| 1116 | |
Sanjay Patel | 71ad227 | 2019-06-26 15:52:59 +0000 | [diff] [blame] | 1117 | if (Instruction *Broadcast = foldInsSequenceIntoSplat(IE)) |
Michael Kuperstein | cd7ad71 | 2016-12-28 00:18:08 +0000 | [diff] [blame] | 1118 | return Broadcast; |
| 1119 | |
Sanjay Patel | 3dee113 | 2019-07-08 19:48:52 +0000 | [diff] [blame] | 1120 | if (Instruction *Splat = foldInsEltIntoSplat(IE)) |
| 1121 | return Splat; |
| 1122 | |
Sanjay Patel | aff5bee | 2019-09-08 19:03:01 +0000 | [diff] [blame] | 1123 | if (Instruction *IdentityShuf = foldInsEltIntoIdentityShuffle(IE)) |
| 1124 | return IdentityShuf; |
| 1125 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1126 | return nullptr; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1129 | /// Return true if we can evaluate the specified expression tree if the vector |
| 1130 | /// elements were shuffled in a different order. |
Sanjay Patel | 54d31ef | 2018-09-29 15:05:24 +0000 | [diff] [blame] | 1131 | static bool canEvaluateShuffled(Value *V, ArrayRef<int> Mask, |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 1132 | unsigned Depth = 5) { |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1133 | // We can always reorder the elements of a constant. |
| 1134 | if (isa<Constant>(V)) |
| 1135 | return true; |
| 1136 | |
| 1137 | // We won't reorder vector arguments. No IPO here. |
| 1138 | Instruction *I = dyn_cast<Instruction>(V); |
| 1139 | if (!I) return false; |
| 1140 | |
| 1141 | // Two users may expect different orders of the elements. Don't try it. |
| 1142 | if (!I->hasOneUse()) |
| 1143 | return false; |
| 1144 | |
| 1145 | if (Depth == 0) return false; |
| 1146 | |
| 1147 | switch (I->getOpcode()) { |
Bjorn Pettersson | 6456252 | 2019-10-18 07:42:02 +0000 | [diff] [blame] | 1148 | case Instruction::UDiv: |
| 1149 | case Instruction::SDiv: |
| 1150 | case Instruction::URem: |
| 1151 | case Instruction::SRem: |
| 1152 | // Propagating an undefined shuffle mask element to integer div/rem is not |
| 1153 | // allowed because those opcodes can create immediate undefined behavior |
| 1154 | // from an undefined element in an operand. |
| 1155 | if (llvm::any_of(Mask, [](int M){ return M == -1; })) |
| 1156 | return false; |
| 1157 | LLVM_FALLTHROUGH; |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1158 | case Instruction::Add: |
| 1159 | case Instruction::FAdd: |
| 1160 | case Instruction::Sub: |
| 1161 | case Instruction::FSub: |
| 1162 | case Instruction::Mul: |
| 1163 | case Instruction::FMul: |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1164 | case Instruction::FDiv: |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1165 | case Instruction::FRem: |
| 1166 | case Instruction::Shl: |
| 1167 | case Instruction::LShr: |
| 1168 | case Instruction::AShr: |
| 1169 | case Instruction::And: |
| 1170 | case Instruction::Or: |
| 1171 | case Instruction::Xor: |
| 1172 | case Instruction::ICmp: |
| 1173 | case Instruction::FCmp: |
| 1174 | case Instruction::Trunc: |
| 1175 | case Instruction::ZExt: |
| 1176 | case Instruction::SExt: |
| 1177 | case Instruction::FPToUI: |
| 1178 | case Instruction::FPToSI: |
| 1179 | case Instruction::UIToFP: |
| 1180 | case Instruction::SIToFP: |
| 1181 | case Instruction::FPTrunc: |
| 1182 | case Instruction::FPExt: |
| 1183 | case Instruction::GetElementPtr: { |
Sanjay Patel | 26c119a | 2018-09-30 13:50:42 +0000 | [diff] [blame] | 1184 | // Bail out if we would create longer vector ops. We could allow creating |
Bjorn Pettersson | 6456252 | 2019-10-18 07:42:02 +0000 | [diff] [blame] | 1185 | // longer vector ops, but that may result in more expensive codegen. |
Sanjay Patel | 26c119a | 2018-09-30 13:50:42 +0000 | [diff] [blame] | 1186 | Type *ITy = I->getType(); |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1187 | if (ITy->isVectorTy() && |
| 1188 | Mask.size() > cast<VectorType>(ITy)->getNumElements()) |
Sanjay Patel | 26c119a | 2018-09-30 13:50:42 +0000 | [diff] [blame] | 1189 | return false; |
Sanjay Patel | 4e2875314 | 2015-11-16 22:16:52 +0000 | [diff] [blame] | 1190 | for (Value *Operand : I->operands()) { |
Sanjay Patel | 54d31ef | 2018-09-29 15:05:24 +0000 | [diff] [blame] | 1191 | if (!canEvaluateShuffled(Operand, Mask, Depth - 1)) |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1192 | return false; |
| 1193 | } |
| 1194 | return true; |
| 1195 | } |
| 1196 | case Instruction::InsertElement: { |
| 1197 | ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(2)); |
| 1198 | if (!CI) return false; |
| 1199 | int ElementNumber = CI->getLimitedValue(); |
| 1200 | |
| 1201 | // Verify that 'CI' does not occur twice in Mask. A single 'insertelement' |
| 1202 | // can't put an element into multiple indices. |
| 1203 | bool SeenOnce = false; |
| 1204 | for (int i = 0, e = Mask.size(); i != e; ++i) { |
| 1205 | if (Mask[i] == ElementNumber) { |
| 1206 | if (SeenOnce) |
| 1207 | return false; |
| 1208 | SeenOnce = true; |
| 1209 | } |
| 1210 | } |
Sanjay Patel | 54d31ef | 2018-09-29 15:05:24 +0000 | [diff] [blame] | 1211 | return canEvaluateShuffled(I->getOperand(0), Mask, Depth - 1); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | return false; |
| 1215 | } |
| 1216 | |
| 1217 | /// Rebuild a new instruction just like 'I' but with the new operands given. |
| 1218 | /// In the event of type mismatch, the type of the operands is correct. |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 1219 | static Value *buildNew(Instruction *I, ArrayRef<Value*> NewOps) { |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1220 | // We don't want to use the IRBuilder here because we want the replacement |
| 1221 | // instructions to appear next to 'I', not the builder's insertion point. |
| 1222 | switch (I->getOpcode()) { |
| 1223 | case Instruction::Add: |
| 1224 | case Instruction::FAdd: |
| 1225 | case Instruction::Sub: |
| 1226 | case Instruction::FSub: |
| 1227 | case Instruction::Mul: |
| 1228 | case Instruction::FMul: |
| 1229 | case Instruction::UDiv: |
| 1230 | case Instruction::SDiv: |
| 1231 | case Instruction::FDiv: |
| 1232 | case Instruction::URem: |
| 1233 | case Instruction::SRem: |
| 1234 | case Instruction::FRem: |
| 1235 | case Instruction::Shl: |
| 1236 | case Instruction::LShr: |
| 1237 | case Instruction::AShr: |
| 1238 | case Instruction::And: |
| 1239 | case Instruction::Or: |
| 1240 | case Instruction::Xor: { |
| 1241 | BinaryOperator *BO = cast<BinaryOperator>(I); |
| 1242 | assert(NewOps.size() == 2 && "binary operator with #ops != 2"); |
| 1243 | BinaryOperator *New = |
| 1244 | BinaryOperator::Create(cast<BinaryOperator>(I)->getOpcode(), |
| 1245 | NewOps[0], NewOps[1], "", BO); |
| 1246 | if (isa<OverflowingBinaryOperator>(BO)) { |
| 1247 | New->setHasNoUnsignedWrap(BO->hasNoUnsignedWrap()); |
| 1248 | New->setHasNoSignedWrap(BO->hasNoSignedWrap()); |
| 1249 | } |
| 1250 | if (isa<PossiblyExactOperator>(BO)) { |
| 1251 | New->setIsExact(BO->isExact()); |
| 1252 | } |
Owen Anderson | 48b842e | 2014-01-18 00:48:14 +0000 | [diff] [blame] | 1253 | if (isa<FPMathOperator>(BO)) |
| 1254 | New->copyFastMathFlags(I); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1255 | return New; |
| 1256 | } |
| 1257 | case Instruction::ICmp: |
| 1258 | assert(NewOps.size() == 2 && "icmp with #ops != 2"); |
| 1259 | return new ICmpInst(I, cast<ICmpInst>(I)->getPredicate(), |
| 1260 | NewOps[0], NewOps[1]); |
| 1261 | case Instruction::FCmp: |
| 1262 | assert(NewOps.size() == 2 && "fcmp with #ops != 2"); |
| 1263 | return new FCmpInst(I, cast<FCmpInst>(I)->getPredicate(), |
| 1264 | NewOps[0], NewOps[1]); |
| 1265 | case Instruction::Trunc: |
| 1266 | case Instruction::ZExt: |
| 1267 | case Instruction::SExt: |
| 1268 | case Instruction::FPToUI: |
| 1269 | case Instruction::FPToSI: |
| 1270 | case Instruction::UIToFP: |
| 1271 | case Instruction::SIToFP: |
| 1272 | case Instruction::FPTrunc: |
| 1273 | case Instruction::FPExt: { |
| 1274 | // It's possible that the mask has a different number of elements from |
| 1275 | // the original cast. We recompute the destination type to match the mask. |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1276 | Type *DestTy = VectorType::get( |
| 1277 | I->getType()->getScalarType(), |
| 1278 | cast<VectorType>(NewOps[0]->getType())->getElementCount()); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1279 | assert(NewOps.size() == 1 && "cast with #ops != 1"); |
| 1280 | return CastInst::Create(cast<CastInst>(I)->getOpcode(), NewOps[0], DestTy, |
| 1281 | "", I); |
| 1282 | } |
| 1283 | case Instruction::GetElementPtr: { |
| 1284 | Value *Ptr = NewOps[0]; |
| 1285 | ArrayRef<Value*> Idx = NewOps.slice(1); |
David Blaikie | 22319eb | 2015-03-14 19:24:04 +0000 | [diff] [blame] | 1286 | GetElementPtrInst *GEP = GetElementPtrInst::Create( |
| 1287 | cast<GetElementPtrInst>(I)->getSourceElementType(), Ptr, Idx, "", I); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1288 | GEP->setIsInBounds(cast<GetElementPtrInst>(I)->isInBounds()); |
| 1289 | return GEP; |
| 1290 | } |
| 1291 | } |
| 1292 | llvm_unreachable("failed to rebuild vector instructions"); |
| 1293 | } |
| 1294 | |
Sanjay Patel | 54d31ef | 2018-09-29 15:05:24 +0000 | [diff] [blame] | 1295 | static Value *evaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask) { |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1296 | // Mask.size() does not need to be equal to the number of vector elements. |
| 1297 | |
| 1298 | assert(V->getType()->isVectorTy() && "can't reorder non-vector elements"); |
Sanjay Patel | ce36b03 | 2017-10-09 17:54:46 +0000 | [diff] [blame] | 1299 | Type *EltTy = V->getType()->getScalarType(); |
Sanjay Patel | 54d31ef | 2018-09-29 15:05:24 +0000 | [diff] [blame] | 1300 | Type *I32Ty = IntegerType::getInt32Ty(V->getContext()); |
Sanjay Patel | ce36b03 | 2017-10-09 17:54:46 +0000 | [diff] [blame] | 1301 | if (isa<UndefValue>(V)) |
| 1302 | return UndefValue::get(VectorType::get(EltTy, Mask.size())); |
| 1303 | |
| 1304 | if (isa<ConstantAggregateZero>(V)) |
| 1305 | return ConstantAggregateZero::get(VectorType::get(EltTy, Mask.size())); |
| 1306 | |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1307 | if (Constant *C = dyn_cast<Constant>(V)) |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1308 | return ConstantExpr::getShuffleVector(C, UndefValue::get(C->getType()), |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1309 | Mask); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1310 | |
| 1311 | Instruction *I = cast<Instruction>(V); |
| 1312 | switch (I->getOpcode()) { |
| 1313 | case Instruction::Add: |
| 1314 | case Instruction::FAdd: |
| 1315 | case Instruction::Sub: |
| 1316 | case Instruction::FSub: |
| 1317 | case Instruction::Mul: |
| 1318 | case Instruction::FMul: |
| 1319 | case Instruction::UDiv: |
| 1320 | case Instruction::SDiv: |
| 1321 | case Instruction::FDiv: |
| 1322 | case Instruction::URem: |
| 1323 | case Instruction::SRem: |
| 1324 | case Instruction::FRem: |
| 1325 | case Instruction::Shl: |
| 1326 | case Instruction::LShr: |
| 1327 | case Instruction::AShr: |
| 1328 | case Instruction::And: |
| 1329 | case Instruction::Or: |
| 1330 | case Instruction::Xor: |
| 1331 | case Instruction::ICmp: |
| 1332 | case Instruction::FCmp: |
| 1333 | case Instruction::Trunc: |
| 1334 | case Instruction::ZExt: |
| 1335 | case Instruction::SExt: |
| 1336 | case Instruction::FPToUI: |
| 1337 | case Instruction::FPToSI: |
| 1338 | case Instruction::UIToFP: |
| 1339 | case Instruction::SIToFP: |
| 1340 | case Instruction::FPTrunc: |
| 1341 | case Instruction::FPExt: |
| 1342 | case Instruction::Select: |
| 1343 | case Instruction::GetElementPtr: { |
| 1344 | SmallVector<Value*, 8> NewOps; |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1345 | bool NeedsRebuild = |
| 1346 | (Mask.size() != cast<VectorType>(I->getType())->getNumElements()); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1347 | for (int i = 0, e = I->getNumOperands(); i != e; ++i) { |
Mikael Holmen | 150a7ec | 2019-04-01 14:10:10 +0000 | [diff] [blame] | 1348 | Value *V; |
| 1349 | // Recursively call evaluateInDifferentElementOrder on vector arguments |
| 1350 | // as well. E.g. GetElementPtr may have scalar operands even if the |
| 1351 | // return value is a vector, so we need to examine the operand type. |
| 1352 | if (I->getOperand(i)->getType()->isVectorTy()) |
| 1353 | V = evaluateInDifferentElementOrder(I->getOperand(i), Mask); |
| 1354 | else |
| 1355 | V = I->getOperand(i); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1356 | NewOps.push_back(V); |
| 1357 | NeedsRebuild |= (V != I->getOperand(i)); |
| 1358 | } |
| 1359 | if (NeedsRebuild) { |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 1360 | return buildNew(I, NewOps); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1361 | } |
| 1362 | return I; |
| 1363 | } |
| 1364 | case Instruction::InsertElement: { |
| 1365 | int Element = cast<ConstantInt>(I->getOperand(2))->getLimitedValue(); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1366 | |
| 1367 | // The insertelement was inserting at Element. Figure out which element |
| 1368 | // that becomes after shuffling. The answer is guaranteed to be unique |
| 1369 | // by CanEvaluateShuffled. |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 1370 | bool Found = false; |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1371 | int Index = 0; |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 1372 | for (int e = Mask.size(); Index != e; ++Index) { |
| 1373 | if (Mask[Index] == Element) { |
| 1374 | Found = true; |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1375 | break; |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 1376 | } |
| 1377 | } |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1378 | |
Hao Liu | 26abebb | 2014-01-08 03:06:15 +0000 | [diff] [blame] | 1379 | // If element is not in Mask, no need to handle the operand 1 (element to |
| 1380 | // be inserted). Just evaluate values in operand 0 according to Mask. |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 1381 | if (!Found) |
Sanjay Patel | 54d31ef | 2018-09-29 15:05:24 +0000 | [diff] [blame] | 1382 | return evaluateInDifferentElementOrder(I->getOperand(0), Mask); |
Joey Gouly | a3250f2 | 2013-07-12 23:08:06 +0000 | [diff] [blame] | 1383 | |
Sanjay Patel | 54d31ef | 2018-09-29 15:05:24 +0000 | [diff] [blame] | 1384 | Value *V = evaluateInDifferentElementOrder(I->getOperand(0), Mask); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1385 | return InsertElementInst::Create(V, I->getOperand(1), |
Sanjay Patel | 54d31ef | 2018-09-29 15:05:24 +0000 | [diff] [blame] | 1386 | ConstantInt::get(I32Ty, Index), "", I); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | llvm_unreachable("failed to reorder elements of vector instruction!"); |
| 1390 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1391 | |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 1392 | // Returns true if the shuffle is extracting a contiguous range of values from |
| 1393 | // LHS, for example: |
| 1394 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 1395 | // Input: |AA|BB|CC|DD|EE|FF|GG|HH|II|JJ|KK|LL|MM|NN|OO|PP| |
| 1396 | // Shuffles to: |EE|FF|GG|HH| |
| 1397 | // +--+--+--+--+ |
| 1398 | static bool isShuffleExtractingFromLHS(ShuffleVectorInst &SVI, |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1399 | ArrayRef<int> Mask) { |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1400 | unsigned LHSElems = |
| 1401 | cast<VectorType>(SVI.getOperand(0)->getType())->getNumElements(); |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 1402 | unsigned MaskElems = Mask.size(); |
| 1403 | unsigned BegIdx = Mask.front(); |
| 1404 | unsigned EndIdx = Mask.back(); |
| 1405 | if (BegIdx > EndIdx || EndIdx >= LHSElems || EndIdx - BegIdx != MaskElems - 1) |
| 1406 | return false; |
| 1407 | for (unsigned I = 0; I != MaskElems; ++I) |
| 1408 | if (static_cast<unsigned>(Mask[I]) != BegIdx + I) |
| 1409 | return false; |
| 1410 | return true; |
| 1411 | } |
| 1412 | |
Sanjay Patel | b999d74 | 2018-07-02 17:42:29 +0000 | [diff] [blame] | 1413 | /// These are the ingredients in an alternate form binary operator as described |
| 1414 | /// below. |
| 1415 | struct BinopElts { |
| 1416 | BinaryOperator::BinaryOps Opcode; |
| 1417 | Value *Op0; |
| 1418 | Value *Op1; |
| 1419 | BinopElts(BinaryOperator::BinaryOps Opc = (BinaryOperator::BinaryOps)0, |
| 1420 | Value *V0 = nullptr, Value *V1 = nullptr) : |
| 1421 | Opcode(Opc), Op0(V0), Op1(V1) {} |
| 1422 | operator bool() const { return Opcode != 0; } |
| 1423 | }; |
| 1424 | |
| 1425 | /// Binops may be transformed into binops with different opcodes and operands. |
| 1426 | /// Reverse the usual canonicalization to enable folds with the non-canonical |
| 1427 | /// form of the binop. If a transform is possible, return the elements of the |
| 1428 | /// new binop. If not, return invalid elements. |
| 1429 | static BinopElts getAlternateBinop(BinaryOperator *BO, const DataLayout &DL) { |
| 1430 | Value *BO0 = BO->getOperand(0), *BO1 = BO->getOperand(1); |
| 1431 | Type *Ty = BO->getType(); |
| 1432 | switch (BO->getOpcode()) { |
| 1433 | case Instruction::Shl: { |
| 1434 | // shl X, C --> mul X, (1 << C) |
| 1435 | Constant *C; |
| 1436 | if (match(BO1, m_Constant(C))) { |
| 1437 | Constant *ShlOne = ConstantExpr::getShl(ConstantInt::get(Ty, 1), C); |
| 1438 | return { Instruction::Mul, BO0, ShlOne }; |
| 1439 | } |
| 1440 | break; |
| 1441 | } |
| 1442 | case Instruction::Or: { |
| 1443 | // or X, C --> add X, C (when X and C have no common bits set) |
| 1444 | const APInt *C; |
| 1445 | if (match(BO1, m_APInt(C)) && MaskedValueIsZero(BO0, *C, DL)) |
| 1446 | return { Instruction::Add, BO0, BO1 }; |
| 1447 | break; |
| 1448 | } |
| 1449 | default: |
| 1450 | break; |
| 1451 | } |
| 1452 | return {}; |
| 1453 | } |
| 1454 | |
Sanjay Patel | 3074b9e | 2018-07-03 13:44:22 +0000 | [diff] [blame] | 1455 | static Instruction *foldSelectShuffleWith1Binop(ShuffleVectorInst &Shuf) { |
| 1456 | assert(Shuf.isSelect() && "Must have select-equivalent shuffle"); |
| 1457 | |
| 1458 | // Are we shuffling together some value and that same value after it has been |
| 1459 | // modified by a binop with a constant? |
| 1460 | Value *Op0 = Shuf.getOperand(0), *Op1 = Shuf.getOperand(1); |
| 1461 | Constant *C; |
| 1462 | bool Op0IsBinop; |
| 1463 | if (match(Op0, m_BinOp(m_Specific(Op1), m_Constant(C)))) |
| 1464 | Op0IsBinop = true; |
| 1465 | else if (match(Op1, m_BinOp(m_Specific(Op0), m_Constant(C)))) |
| 1466 | Op0IsBinop = false; |
| 1467 | else |
| 1468 | return nullptr; |
| 1469 | |
Sanjay Patel | 3074b9e | 2018-07-03 13:44:22 +0000 | [diff] [blame] | 1470 | // The identity constant for a binop leaves a variable operand unchanged. For |
| 1471 | // a vector, this is a splat of something like 0, -1, or 1. |
| 1472 | // If there's no identity constant for this binop, we're done. |
Sanjay Patel | 509a1e7 | 2018-07-10 15:12:31 +0000 | [diff] [blame] | 1473 | auto *BO = cast<BinaryOperator>(Op0IsBinop ? Op0 : Op1); |
Sanjay Patel | 3074b9e | 2018-07-03 13:44:22 +0000 | [diff] [blame] | 1474 | BinaryOperator::BinaryOps BOpcode = BO->getOpcode(); |
Sanjay Patel | 509a1e7 | 2018-07-10 15:12:31 +0000 | [diff] [blame] | 1475 | Constant *IdC = ConstantExpr::getBinOpIdentity(BOpcode, Shuf.getType(), true); |
Sanjay Patel | 3074b9e | 2018-07-03 13:44:22 +0000 | [diff] [blame] | 1476 | if (!IdC) |
| 1477 | return nullptr; |
| 1478 | |
| 1479 | // Shuffle identity constants into the lanes that return the original value. |
| 1480 | // Example: shuf (mul X, {-1,-2,-3,-4}), X, {0,5,6,3} --> mul X, {-1,1,1,-4} |
| 1481 | // Example: shuf X, (add X, {-1,-2,-3,-4}), {0,1,6,7} --> add X, {0,0,-3,-4} |
| 1482 | // The existing binop constant vector remains in the same operand position. |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1483 | ArrayRef<int> Mask = Shuf.getShuffleMask(); |
Sanjay Patel | 3074b9e | 2018-07-03 13:44:22 +0000 | [diff] [blame] | 1484 | Constant *NewC = Op0IsBinop ? ConstantExpr::getShuffleVector(C, IdC, Mask) : |
| 1485 | ConstantExpr::getShuffleVector(IdC, C, Mask); |
| 1486 | |
Sanjay Patel | 509a1e7 | 2018-07-10 15:12:31 +0000 | [diff] [blame] | 1487 | bool MightCreatePoisonOrUB = |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1488 | is_contained(Mask, UndefMaskElem) && |
Sanjay Patel | 509a1e7 | 2018-07-10 15:12:31 +0000 | [diff] [blame] | 1489 | (Instruction::isIntDivRem(BOpcode) || Instruction::isShift(BOpcode)); |
| 1490 | if (MightCreatePoisonOrUB) |
| 1491 | NewC = getSafeVectorConstantForBinop(BOpcode, NewC, true); |
| 1492 | |
Sanjay Patel | 3074b9e | 2018-07-03 13:44:22 +0000 | [diff] [blame] | 1493 | // shuf (bop X, C), X, M --> bop X, C' |
| 1494 | // shuf X, (bop X, C), M --> bop X, C' |
Sanjay Patel | 509a1e7 | 2018-07-10 15:12:31 +0000 | [diff] [blame] | 1495 | Value *X = Op0IsBinop ? Op1 : Op0; |
Sanjay Patel | 3074b9e | 2018-07-03 13:44:22 +0000 | [diff] [blame] | 1496 | Instruction *NewBO = BinaryOperator::Create(BOpcode, X, NewC); |
| 1497 | NewBO->copyIRFlags(BO); |
Sanjay Patel | 3333106 | 2018-07-10 14:27:55 +0000 | [diff] [blame] | 1498 | |
| 1499 | // An undef shuffle mask element may propagate as an undef constant element in |
| 1500 | // the new binop. That would produce poison where the original code might not. |
Sanjay Patel | 509a1e7 | 2018-07-10 15:12:31 +0000 | [diff] [blame] | 1501 | // If we already made a safe constant, then there's no danger. |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1502 | if (is_contained(Mask, UndefMaskElem) && !MightCreatePoisonOrUB) |
Sanjay Patel | 3333106 | 2018-07-10 14:27:55 +0000 | [diff] [blame] | 1503 | NewBO->dropPoisonGeneratingFlags(); |
Sanjay Patel | 3074b9e | 2018-07-03 13:44:22 +0000 | [diff] [blame] | 1504 | return NewBO; |
| 1505 | } |
| 1506 | |
Sanjay Patel | 0b59103 | 2019-07-08 16:26:48 +0000 | [diff] [blame] | 1507 | /// If we have an insert of a scalar to a non-zero element of an undefined |
| 1508 | /// vector and then shuffle that value, that's the same as inserting to the zero |
| 1509 | /// element and shuffling. Splatting from the zero element is recognized as the |
| 1510 | /// canonical form of splat. |
| 1511 | static Instruction *canonicalizeInsertSplat(ShuffleVectorInst &Shuf, |
| 1512 | InstCombiner::BuilderTy &Builder) { |
| 1513 | Value *Op0 = Shuf.getOperand(0), *Op1 = Shuf.getOperand(1); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1514 | ArrayRef<int> Mask = Shuf.getShuffleMask(); |
Sanjay Patel | 0b59103 | 2019-07-08 16:26:48 +0000 | [diff] [blame] | 1515 | Value *X; |
| 1516 | uint64_t IndexC; |
| 1517 | |
| 1518 | // Match a shuffle that is a splat to a non-zero element. |
| 1519 | if (!match(Op0, m_OneUse(m_InsertElement(m_Undef(), m_Value(X), |
| 1520 | m_ConstantInt(IndexC)))) || |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1521 | !match(Op1, m_Undef()) || match(Mask, m_ZeroMask()) || IndexC == 0) |
Sanjay Patel | 0b59103 | 2019-07-08 16:26:48 +0000 | [diff] [blame] | 1522 | return nullptr; |
| 1523 | |
| 1524 | // Insert into element 0 of an undef vector. |
| 1525 | UndefValue *UndefVec = UndefValue::get(Shuf.getType()); |
| 1526 | Constant *Zero = Builder.getInt32(0); |
| 1527 | Value *NewIns = Builder.CreateInsertElement(UndefVec, X, Zero); |
| 1528 | |
| 1529 | // Splat from element 0. Any mask element that is undefined remains undefined. |
| 1530 | // For example: |
| 1531 | // shuf (inselt undef, X, 2), undef, <2,2,undef> |
| 1532 | // --> shuf (inselt undef, X, 0), undef, <0,0,undef> |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1533 | unsigned NumMaskElts = Shuf.getType()->getNumElements(); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1534 | SmallVector<int, 16> NewMask(NumMaskElts, 0); |
Sanjay Patel | 0b59103 | 2019-07-08 16:26:48 +0000 | [diff] [blame] | 1535 | for (unsigned i = 0; i != NumMaskElts; ++i) |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1536 | if (Mask[i] == UndefMaskElem) |
| 1537 | NewMask[i] = Mask[i]; |
Sanjay Patel | 0b59103 | 2019-07-08 16:26:48 +0000 | [diff] [blame] | 1538 | |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1539 | return new ShuffleVectorInst(NewIns, UndefVec, NewMask); |
Sanjay Patel | 0b59103 | 2019-07-08 16:26:48 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Sanjay Patel | b999d74 | 2018-07-02 17:42:29 +0000 | [diff] [blame] | 1542 | /// Try to fold shuffles that are the equivalent of a vector select. |
Sanjay Patel | da66753 | 2018-06-29 13:44:06 +0000 | [diff] [blame] | 1543 | static Instruction *foldSelectShuffle(ShuffleVectorInst &Shuf, |
Sanjay Patel | b999d74 | 2018-07-02 17:42:29 +0000 | [diff] [blame] | 1544 | InstCombiner::BuilderTy &Builder, |
| 1545 | const DataLayout &DL) { |
Sanjay Patel | a76b700 | 2018-06-21 20:15:09 +0000 | [diff] [blame] | 1546 | if (!Shuf.isSelect()) |
| 1547 | return nullptr; |
| 1548 | |
Sanjay Patel | e85d2e4 | 2019-11-25 11:55:57 -0500 | [diff] [blame] | 1549 | // Canonicalize to choose from operand 0 first unless operand 1 is undefined. |
| 1550 | // Commuting undef to operand 0 conflicts with another canonicalization. |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1551 | unsigned NumElts = Shuf.getType()->getNumElements(); |
Sanjay Patel | e85d2e4 | 2019-11-25 11:55:57 -0500 | [diff] [blame] | 1552 | if (!isa<UndefValue>(Shuf.getOperand(1)) && |
| 1553 | Shuf.getMaskValue(0) >= (int)NumElts) { |
Sanjay Patel | b33938d | 2019-04-08 13:28:29 +0000 | [diff] [blame] | 1554 | // TODO: Can we assert that both operands of a shuffle-select are not undef |
| 1555 | // (otherwise, it would have been folded by instsimplify? |
Sanjay Patel | b276dd1 | 2019-03-31 15:01:30 +0000 | [diff] [blame] | 1556 | Shuf.commute(); |
| 1557 | return &Shuf; |
| 1558 | } |
| 1559 | |
Sanjay Patel | 3074b9e | 2018-07-03 13:44:22 +0000 | [diff] [blame] | 1560 | if (Instruction *I = foldSelectShuffleWith1Binop(Shuf)) |
| 1561 | return I; |
| 1562 | |
Sanjay Patel | a76b700 | 2018-06-21 20:15:09 +0000 | [diff] [blame] | 1563 | BinaryOperator *B0, *B1; |
| 1564 | if (!match(Shuf.getOperand(0), m_BinOp(B0)) || |
| 1565 | !match(Shuf.getOperand(1), m_BinOp(B1))) |
| 1566 | return nullptr; |
| 1567 | |
Sanjay Patel | da66753 | 2018-06-29 13:44:06 +0000 | [diff] [blame] | 1568 | Value *X, *Y; |
Sanjay Patel | a76b700 | 2018-06-21 20:15:09 +0000 | [diff] [blame] | 1569 | Constant *C0, *C1; |
Sanjay Patel | a52963b | 2018-06-22 12:46:16 +0000 | [diff] [blame] | 1570 | bool ConstantsAreOp1; |
| 1571 | if (match(B0, m_BinOp(m_Value(X), m_Constant(C0))) && |
Sanjay Patel | da66753 | 2018-06-29 13:44:06 +0000 | [diff] [blame] | 1572 | match(B1, m_BinOp(m_Value(Y), m_Constant(C1)))) |
Sanjay Patel | a52963b | 2018-06-22 12:46:16 +0000 | [diff] [blame] | 1573 | ConstantsAreOp1 = true; |
| 1574 | else if (match(B0, m_BinOp(m_Constant(C0), m_Value(X))) && |
Sanjay Patel | da66753 | 2018-06-29 13:44:06 +0000 | [diff] [blame] | 1575 | match(B1, m_BinOp(m_Constant(C1), m_Value(Y)))) |
Sanjay Patel | a52963b | 2018-06-22 12:46:16 +0000 | [diff] [blame] | 1576 | ConstantsAreOp1 = false; |
| 1577 | else |
Sanjay Patel | a76b700 | 2018-06-21 20:15:09 +0000 | [diff] [blame] | 1578 | return nullptr; |
| 1579 | |
Sanjay Patel | 57bda36 | 2018-06-28 17:48:04 +0000 | [diff] [blame] | 1580 | // We need matching binops to fold the lanes together. |
| 1581 | BinaryOperator::BinaryOps Opc0 = B0->getOpcode(); |
| 1582 | BinaryOperator::BinaryOps Opc1 = B1->getOpcode(); |
| 1583 | bool DropNSW = false; |
| 1584 | if (ConstantsAreOp1 && Opc0 != Opc1) { |
Sanjay Patel | 57bda36 | 2018-06-28 17:48:04 +0000 | [diff] [blame] | 1585 | // TODO: We drop "nsw" if shift is converted into multiply because it may |
| 1586 | // not be correct when the shift amount is BitWidth - 1. We could examine |
| 1587 | // each vector element to determine if it is safe to keep that flag. |
Sanjay Patel | b999d74 | 2018-07-02 17:42:29 +0000 | [diff] [blame] | 1588 | if (Opc0 == Instruction::Shl || Opc1 == Instruction::Shl) |
Sanjay Patel | 57bda36 | 2018-06-28 17:48:04 +0000 | [diff] [blame] | 1589 | DropNSW = true; |
Sanjay Patel | b999d74 | 2018-07-02 17:42:29 +0000 | [diff] [blame] | 1590 | if (BinopElts AltB0 = getAlternateBinop(B0, DL)) { |
| 1591 | assert(isa<Constant>(AltB0.Op1) && "Expecting constant with alt binop"); |
| 1592 | Opc0 = AltB0.Opcode; |
| 1593 | C0 = cast<Constant>(AltB0.Op1); |
| 1594 | } else if (BinopElts AltB1 = getAlternateBinop(B1, DL)) { |
| 1595 | assert(isa<Constant>(AltB1.Op1) && "Expecting constant with alt binop"); |
| 1596 | Opc1 = AltB1.Opcode; |
| 1597 | C1 = cast<Constant>(AltB1.Op1); |
Sanjay Patel | 57bda36 | 2018-06-28 17:48:04 +0000 | [diff] [blame] | 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | if (Opc0 != Opc1) |
Sanjay Patel | 4784e15 | 2018-06-21 23:56:59 +0000 | [diff] [blame] | 1602 | return nullptr; |
| 1603 | |
Sanjay Patel | 57bda36 | 2018-06-28 17:48:04 +0000 | [diff] [blame] | 1604 | // The opcodes must be the same. Use a new name to make that clear. |
| 1605 | BinaryOperator::BinaryOps BOpc = Opc0; |
| 1606 | |
Sanjay Patel | 06ea420 | 2018-07-10 13:33:26 +0000 | [diff] [blame] | 1607 | // Select the constant elements needed for the single binop. |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1608 | ArrayRef<int> Mask = Shuf.getShuffleMask(); |
Sanjay Patel | 06ea420 | 2018-07-10 13:33:26 +0000 | [diff] [blame] | 1609 | Constant *NewC = ConstantExpr::getShuffleVector(C0, C1, Mask); |
| 1610 | |
Sanjay Patel | 5bd3664 | 2018-07-09 13:21:46 +0000 | [diff] [blame] | 1611 | // We are moving a binop after a shuffle. When a shuffle has an undefined |
| 1612 | // mask element, the result is undefined, but it is not poison or undefined |
| 1613 | // behavior. That is not necessarily true for div/rem/shift. |
Sanjay Patel | 5bd3664 | 2018-07-09 13:21:46 +0000 | [diff] [blame] | 1614 | bool MightCreatePoisonOrUB = |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1615 | is_contained(Mask, UndefMaskElem) && |
Sanjay Patel | 5bd3664 | 2018-07-09 13:21:46 +0000 | [diff] [blame] | 1616 | (Instruction::isIntDivRem(BOpc) || Instruction::isShift(BOpc)); |
Sanjay Patel | 06ea420 | 2018-07-10 13:33:26 +0000 | [diff] [blame] | 1617 | if (MightCreatePoisonOrUB) |
| 1618 | NewC = getSafeVectorConstantForBinop(BOpc, NewC, ConstantsAreOp1); |
Sanjay Patel | 5bd3664 | 2018-07-09 13:21:46 +0000 | [diff] [blame] | 1619 | |
Sanjay Patel | da66753 | 2018-06-29 13:44:06 +0000 | [diff] [blame] | 1620 | Value *V; |
| 1621 | if (X == Y) { |
| 1622 | // Remove a binop and the shuffle by rearranging the constant: |
| 1623 | // shuffle (op V, C0), (op V, C1), M --> op V, C' |
| 1624 | // shuffle (op C0, V), (op C1, V), M --> op C', V |
| 1625 | V = X; |
Sanjay Patel | 5bd3664 | 2018-07-09 13:21:46 +0000 | [diff] [blame] | 1626 | } else { |
Sanjay Patel | da66753 | 2018-06-29 13:44:06 +0000 | [diff] [blame] | 1627 | // If there are 2 different variable operands, we must create a new shuffle |
| 1628 | // (select) first, so check uses to ensure that we don't end up with more |
| 1629 | // instructions than we started with. |
Sanjay Patel | 5bd3664 | 2018-07-09 13:21:46 +0000 | [diff] [blame] | 1630 | if (!B0->hasOneUse() && !B1->hasOneUse()) |
| 1631 | return nullptr; |
| 1632 | |
Sanjay Patel | 06ea420 | 2018-07-10 13:33:26 +0000 | [diff] [blame] | 1633 | // If we use the original shuffle mask and op1 is *variable*, we would be |
| 1634 | // putting an undef into operand 1 of div/rem/shift. This is either UB or |
| 1635 | // poison. We do not have to guard against UB when *constants* are op1 |
| 1636 | // because safe constants guarantee that we do not overflow sdiv/srem (and |
| 1637 | // there's no danger for other opcodes). |
| 1638 | // TODO: To allow this case, create a new shuffle mask with no undefs. |
| 1639 | if (MightCreatePoisonOrUB && !ConstantsAreOp1) |
Sanjay Patel | 5bd3664 | 2018-07-09 13:21:46 +0000 | [diff] [blame] | 1640 | return nullptr; |
| 1641 | |
Sanjay Patel | da66753 | 2018-06-29 13:44:06 +0000 | [diff] [blame] | 1642 | // Note: In general, we do not create new shuffles in InstCombine because we |
| 1643 | // do not know if a target can lower an arbitrary shuffle optimally. In this |
| 1644 | // case, the shuffle uses the existing mask, so there is no additional risk. |
Sanjay Patel | da66753 | 2018-06-29 13:44:06 +0000 | [diff] [blame] | 1645 | |
| 1646 | // Select the variable vectors first, then perform the binop: |
| 1647 | // shuffle (op X, C0), (op Y, C1), M --> op (shuffle X, Y, M), C' |
| 1648 | // shuffle (op C0, X), (op C1, Y), M --> op C', (shuffle X, Y, M) |
Sanjay Patel | 5bd3664 | 2018-07-09 13:21:46 +0000 | [diff] [blame] | 1649 | V = Builder.CreateShuffleVector(X, Y, Mask); |
Sanjay Patel | da66753 | 2018-06-29 13:44:06 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Sanjay Patel | da66753 | 2018-06-29 13:44:06 +0000 | [diff] [blame] | 1652 | Instruction *NewBO = ConstantsAreOp1 ? BinaryOperator::Create(BOpc, V, NewC) : |
| 1653 | BinaryOperator::Create(BOpc, NewC, V); |
Sanjay Patel | a76b700 | 2018-06-21 20:15:09 +0000 | [diff] [blame] | 1654 | |
Sanjay Patel | 5bd3664 | 2018-07-09 13:21:46 +0000 | [diff] [blame] | 1655 | // Flags are intersected from the 2 source binops. But there are 2 exceptions: |
| 1656 | // 1. If we changed an opcode, poison conditions might have changed. |
| 1657 | // 2. If the shuffle had undef mask elements, the new binop might have undefs |
Sanjay Patel | c8d9d81 | 2018-07-10 16:09:49 +0000 | [diff] [blame] | 1658 | // where the original code did not. But if we already made a safe constant, |
| 1659 | // then there's no danger. |
Sanjay Patel | a76b700 | 2018-06-21 20:15:09 +0000 | [diff] [blame] | 1660 | NewBO->copyIRFlags(B0); |
| 1661 | NewBO->andIRFlags(B1); |
Sanjay Patel | 57bda36 | 2018-06-28 17:48:04 +0000 | [diff] [blame] | 1662 | if (DropNSW) |
| 1663 | NewBO->setHasNoSignedWrap(false); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1664 | if (is_contained(Mask, UndefMaskElem) && !MightCreatePoisonOrUB) |
Sanjay Patel | 5bd3664 | 2018-07-09 13:21:46 +0000 | [diff] [blame] | 1665 | NewBO->dropPoisonGeneratingFlags(); |
Sanjay Patel | a76b700 | 2018-06-21 20:15:09 +0000 | [diff] [blame] | 1666 | return NewBO; |
| 1667 | } |
| 1668 | |
Sanjay Patel | 538a8f0 | 2020-04-05 09:46:22 -0400 | [diff] [blame] | 1669 | /// Convert a narrowing shuffle of a bitcasted vector into a vector truncate. |
| 1670 | /// Example (little endian): |
| 1671 | /// shuf (bitcast <4 x i16> X to <8 x i8>), <0, 2, 4, 6> --> trunc X to <4 x i8> |
| 1672 | static Instruction *foldTruncShuffle(ShuffleVectorInst &Shuf, |
| 1673 | bool IsBigEndian) { |
| 1674 | // This must be a bitcasted shuffle of 1 vector integer operand. |
| 1675 | Type *DestType = Shuf.getType(); |
| 1676 | Value *X; |
| 1677 | if (!match(Shuf.getOperand(0), m_BitCast(m_Value(X))) || |
| 1678 | !match(Shuf.getOperand(1), m_Undef()) || !DestType->isIntOrIntVectorTy()) |
| 1679 | return nullptr; |
| 1680 | |
| 1681 | // The source type must have the same number of elements as the shuffle, |
| 1682 | // and the source element type must be larger than the shuffle element type. |
| 1683 | Type *SrcType = X->getType(); |
| 1684 | if (!SrcType->isVectorTy() || !SrcType->isIntOrIntVectorTy() || |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1685 | cast<VectorType>(SrcType)->getNumElements() != |
| 1686 | cast<VectorType>(DestType)->getNumElements() || |
Sanjay Patel | 538a8f0 | 2020-04-05 09:46:22 -0400 | [diff] [blame] | 1687 | SrcType->getScalarSizeInBits() % DestType->getScalarSizeInBits() != 0) |
| 1688 | return nullptr; |
| 1689 | |
| 1690 | assert(Shuf.changesLength() && !Shuf.increasesLength() && |
| 1691 | "Expected a shuffle that decreases length"); |
| 1692 | |
| 1693 | // Last, check that the mask chooses the correct low bits for each narrow |
| 1694 | // element in the result. |
| 1695 | uint64_t TruncRatio = |
| 1696 | SrcType->getScalarSizeInBits() / DestType->getScalarSizeInBits(); |
| 1697 | ArrayRef<int> Mask = Shuf.getShuffleMask(); |
| 1698 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 1699 | if (Mask[i] == UndefMaskElem) |
| 1700 | continue; |
| 1701 | uint64_t LSBIndex = IsBigEndian ? (i + 1) * TruncRatio - 1 : i * TruncRatio; |
| 1702 | assert(LSBIndex <= std::numeric_limits<int32_t>::max() && |
| 1703 | "Overflowed 32-bits"); |
| 1704 | if (Mask[i] != (int)LSBIndex) |
| 1705 | return nullptr; |
| 1706 | } |
| 1707 | |
| 1708 | return new TruncInst(X, DestType); |
| 1709 | } |
| 1710 | |
Sanjay Patel | c1416b6 | 2018-09-07 21:03:34 +0000 | [diff] [blame] | 1711 | /// Match a shuffle-select-shuffle pattern where the shuffles are widening and |
| 1712 | /// narrowing (concatenating with undef and extracting back to the original |
| 1713 | /// length). This allows replacing the wide select with a narrow select. |
Sanjay Patel | 88194df | 2018-10-09 15:29:26 +0000 | [diff] [blame] | 1714 | static Instruction *narrowVectorSelect(ShuffleVectorInst &Shuf, |
| 1715 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | c1416b6 | 2018-09-07 21:03:34 +0000 | [diff] [blame] | 1716 | // This must be a narrowing identity shuffle. It extracts the 1st N elements |
| 1717 | // of the 1st vector operand of a shuffle. |
| 1718 | if (!match(Shuf.getOperand(1), m_Undef()) || !Shuf.isIdentityWithExtract()) |
| 1719 | return nullptr; |
| 1720 | |
| 1721 | // The vector being shuffled must be a vector select that we can eliminate. |
| 1722 | // TODO: The one-use requirement could be eased if X and/or Y are constants. |
| 1723 | Value *Cond, *X, *Y; |
| 1724 | if (!match(Shuf.getOperand(0), |
| 1725 | m_OneUse(m_Select(m_Value(Cond), m_Value(X), m_Value(Y))))) |
| 1726 | return nullptr; |
| 1727 | |
| 1728 | // We need a narrow condition value. It must be extended with undef elements |
| 1729 | // and have the same number of elements as this shuffle. |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1730 | unsigned NarrowNumElts = Shuf.getType()->getNumElements(); |
Sanjay Patel | c1416b6 | 2018-09-07 21:03:34 +0000 | [diff] [blame] | 1731 | Value *NarrowCond; |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1732 | if (!match(Cond, m_OneUse(m_ShuffleVector(m_Value(NarrowCond), m_Undef()))) || |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1733 | cast<VectorType>(NarrowCond->getType())->getNumElements() != |
| 1734 | NarrowNumElts || |
Sanjay Patel | c1416b6 | 2018-09-07 21:03:34 +0000 | [diff] [blame] | 1735 | !cast<ShuffleVectorInst>(Cond)->isIdentityWithPadding()) |
| 1736 | return nullptr; |
| 1737 | |
| 1738 | // shuf (sel (shuf NarrowCond, undef, WideMask), X, Y), undef, NarrowMask) --> |
| 1739 | // sel NarrowCond, (shuf X, undef, NarrowMask), (shuf Y, undef, NarrowMask) |
| 1740 | Value *Undef = UndefValue::get(X->getType()); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1741 | Value *NarrowX = Builder.CreateShuffleVector(X, Undef, Shuf.getShuffleMask()); |
| 1742 | Value *NarrowY = Builder.CreateShuffleVector(Y, Undef, Shuf.getShuffleMask()); |
Sanjay Patel | c1416b6 | 2018-09-07 21:03:34 +0000 | [diff] [blame] | 1743 | return SelectInst::Create(NarrowCond, NarrowX, NarrowY); |
| 1744 | } |
| 1745 | |
Sanjay Patel | 7181146 | 2018-10-14 15:25:06 +0000 | [diff] [blame] | 1746 | /// Try to combine 2 shuffles into 1 shuffle by concatenating a shuffle mask. |
| 1747 | static Instruction *foldIdentityExtractShuffle(ShuffleVectorInst &Shuf) { |
| 1748 | Value *Op0 = Shuf.getOperand(0), *Op1 = Shuf.getOperand(1); |
| 1749 | if (!Shuf.isIdentityWithExtract() || !isa<UndefValue>(Op1)) |
| 1750 | return nullptr; |
| 1751 | |
| 1752 | Value *X, *Y; |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1753 | ArrayRef<int> Mask; |
| 1754 | if (!match(Op0, m_ShuffleVector(m_Value(X), m_Value(Y), m_Mask(Mask)))) |
Sanjay Patel | 7181146 | 2018-10-14 15:25:06 +0000 | [diff] [blame] | 1755 | return nullptr; |
| 1756 | |
Sanjay Patel | cddb1e5 | 2019-02-05 22:58:45 +0000 | [diff] [blame] | 1757 | // Be conservative with shuffle transforms. If we can't kill the 1st shuffle, |
| 1758 | // then combining may result in worse codegen. |
| 1759 | if (!Op0->hasOneUse()) |
| 1760 | return nullptr; |
| 1761 | |
Sanjay Patel | 7181146 | 2018-10-14 15:25:06 +0000 | [diff] [blame] | 1762 | // We are extracting a subvector from a shuffle. Remove excess elements from |
| 1763 | // the 1st shuffle mask to eliminate the extract. |
| 1764 | // |
| 1765 | // This transform is conservatively limited to identity extracts because we do |
| 1766 | // not allow arbitrary shuffle mask creation as a target-independent transform |
| 1767 | // (because we can't guarantee that will lower efficiently). |
| 1768 | // |
| 1769 | // If the extracting shuffle has an undef mask element, it transfers to the |
| 1770 | // new shuffle mask. Otherwise, copy the original mask element. Example: |
| 1771 | // shuf (shuf X, Y, <C0, C1, C2, undef, C4>), undef, <0, undef, 2, 3> --> |
| 1772 | // shuf X, Y, <C0, undef, C2, undef> |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1773 | unsigned NumElts = Shuf.getType()->getNumElements(); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1774 | SmallVector<int, 16> NewMask(NumElts); |
| 1775 | assert(NumElts < Mask.size() && |
Sanjay Patel | 7181146 | 2018-10-14 15:25:06 +0000 | [diff] [blame] | 1776 | "Identity with extract must have less elements than its inputs"); |
| 1777 | |
| 1778 | for (unsigned i = 0; i != NumElts; ++i) { |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1779 | int ExtractMaskElt = Shuf.getMaskValue(i); |
| 1780 | int MaskElt = Mask[i]; |
| 1781 | NewMask[i] = ExtractMaskElt == UndefMaskElem ? ExtractMaskElt : MaskElt; |
Sanjay Patel | 7181146 | 2018-10-14 15:25:06 +0000 | [diff] [blame] | 1782 | } |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1783 | return new ShuffleVectorInst(X, Y, NewMask); |
Sanjay Patel | 7181146 | 2018-10-14 15:25:06 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Sanjay Patel | 396d18a | 2019-12-10 10:10:05 -0500 | [diff] [blame] | 1786 | /// Try to replace a shuffle with an insertelement or try to replace a shuffle |
| 1787 | /// operand with the operand of an insertelement. |
Nikita Popov | 5a8819b | 2020-02-03 21:17:36 +0100 | [diff] [blame] | 1788 | static Instruction *foldShuffleWithInsert(ShuffleVectorInst &Shuf, |
| 1789 | InstCombiner &IC) { |
Sanjay Patel | b12e410 | 2018-10-30 15:26:39 +0000 | [diff] [blame] | 1790 | Value *V0 = Shuf.getOperand(0), *V1 = Shuf.getOperand(1); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1791 | SmallVector<int, 16> Mask; |
| 1792 | Shuf.getShuffleMask(Mask); |
Sanjay Patel | b12e410 | 2018-10-30 15:26:39 +0000 | [diff] [blame] | 1793 | |
| 1794 | // The shuffle must not change vector sizes. |
| 1795 | // TODO: This restriction could be removed if the insert has only one use |
| 1796 | // (because the transform would require a new length-changing shuffle). |
| 1797 | int NumElts = Mask.size(); |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1798 | if (NumElts != (int)(cast<VectorType>(V0->getType())->getNumElements())) |
Sanjay Patel | b12e410 | 2018-10-30 15:26:39 +0000 | [diff] [blame] | 1799 | return nullptr; |
| 1800 | |
Sanjay Patel | 396d18a | 2019-12-10 10:10:05 -0500 | [diff] [blame] | 1801 | // This is a specialization of a fold in SimplifyDemandedVectorElts. We may |
| 1802 | // not be able to handle it there if the insertelement has >1 use. |
| 1803 | // If the shuffle has an insertelement operand but does not choose the |
| 1804 | // inserted scalar element from that value, then we can replace that shuffle |
| 1805 | // operand with the source vector of the insertelement. |
| 1806 | Value *X; |
| 1807 | uint64_t IdxC; |
| 1808 | if (match(V0, m_InsertElement(m_Value(X), m_Value(), m_ConstantInt(IdxC)))) { |
| 1809 | // shuf (inselt X, ?, IdxC), ?, Mask --> shuf X, ?, Mask |
Nikita Popov | 5a8819b | 2020-02-03 21:17:36 +0100 | [diff] [blame] | 1810 | if (none_of(Mask, [IdxC](int MaskElt) { return MaskElt == (int)IdxC; })) |
| 1811 | return IC.replaceOperand(Shuf, 0, X); |
Sanjay Patel | 396d18a | 2019-12-10 10:10:05 -0500 | [diff] [blame] | 1812 | } |
| 1813 | if (match(V1, m_InsertElement(m_Value(X), m_Value(), m_ConstantInt(IdxC)))) { |
| 1814 | // Offset the index constant by the vector width because we are checking for |
| 1815 | // accesses to the 2nd vector input of the shuffle. |
| 1816 | IdxC += NumElts; |
| 1817 | // shuf ?, (inselt X, ?, IdxC), Mask --> shuf ?, X, Mask |
Nikita Popov | 5a8819b | 2020-02-03 21:17:36 +0100 | [diff] [blame] | 1818 | if (none_of(Mask, [IdxC](int MaskElt) { return MaskElt == (int)IdxC; })) |
| 1819 | return IC.replaceOperand(Shuf, 1, X); |
Sanjay Patel | 396d18a | 2019-12-10 10:10:05 -0500 | [diff] [blame] | 1820 | } |
| 1821 | |
Sanjay Patel | b12e410 | 2018-10-30 15:26:39 +0000 | [diff] [blame] | 1822 | // shuffle (insert ?, Scalar, IndexC), V1, Mask --> insert V1, Scalar, IndexC' |
| 1823 | auto isShufflingScalarIntoOp1 = [&](Value *&Scalar, ConstantInt *&IndexC) { |
| 1824 | // We need an insertelement with a constant index. |
| 1825 | if (!match(V0, m_InsertElement(m_Value(), m_Value(Scalar), |
| 1826 | m_ConstantInt(IndexC)))) |
| 1827 | return false; |
| 1828 | |
| 1829 | // Test the shuffle mask to see if it splices the inserted scalar into the |
| 1830 | // operand 1 vector of the shuffle. |
| 1831 | int NewInsIndex = -1; |
| 1832 | for (int i = 0; i != NumElts; ++i) { |
| 1833 | // Ignore undef mask elements. |
| 1834 | if (Mask[i] == -1) |
| 1835 | continue; |
| 1836 | |
| 1837 | // The shuffle takes elements of operand 1 without lane changes. |
| 1838 | if (Mask[i] == NumElts + i) |
| 1839 | continue; |
| 1840 | |
| 1841 | // The shuffle must choose the inserted scalar exactly once. |
| 1842 | if (NewInsIndex != -1 || Mask[i] != IndexC->getSExtValue()) |
| 1843 | return false; |
| 1844 | |
| 1845 | // The shuffle is placing the inserted scalar into element i. |
| 1846 | NewInsIndex = i; |
| 1847 | } |
| 1848 | |
| 1849 | assert(NewInsIndex != -1 && "Did not fold shuffle with unused operand?"); |
| 1850 | |
| 1851 | // Index is updated to the potentially translated insertion lane. |
| 1852 | IndexC = ConstantInt::get(IndexC->getType(), NewInsIndex); |
| 1853 | return true; |
| 1854 | }; |
| 1855 | |
| 1856 | // If the shuffle is unnecessary, insert the scalar operand directly into |
| 1857 | // operand 1 of the shuffle. Example: |
| 1858 | // shuffle (insert ?, S, 1), V1, <1, 5, 6, 7> --> insert V1, S, 0 |
| 1859 | Value *Scalar; |
| 1860 | ConstantInt *IndexC; |
| 1861 | if (isShufflingScalarIntoOp1(Scalar, IndexC)) |
| 1862 | return InsertElementInst::Create(V1, Scalar, IndexC); |
| 1863 | |
| 1864 | // Try again after commuting shuffle. Example: |
| 1865 | // shuffle V0, (insert ?, S, 0), <0, 1, 2, 4> --> |
| 1866 | // shuffle (insert ?, S, 0), V0, <4, 5, 6, 0> --> insert V0, S, 3 |
| 1867 | std::swap(V0, V1); |
| 1868 | ShuffleVectorInst::commuteShuffleMask(Mask, NumElts); |
| 1869 | if (isShufflingScalarIntoOp1(Scalar, IndexC)) |
| 1870 | return InsertElementInst::Create(V1, Scalar, IndexC); |
| 1871 | |
| 1872 | return nullptr; |
| 1873 | } |
| 1874 | |
Sanjay Patel | 6a55418 | 2019-05-22 00:32:25 +0000 | [diff] [blame] | 1875 | static Instruction *foldIdentityPaddedShuffles(ShuffleVectorInst &Shuf) { |
| 1876 | // Match the operands as identity with padding (also known as concatenation |
| 1877 | // with undef) shuffles of the same source type. The backend is expected to |
| 1878 | // recreate these concatenations from a shuffle of narrow operands. |
| 1879 | auto *Shuffle0 = dyn_cast<ShuffleVectorInst>(Shuf.getOperand(0)); |
| 1880 | auto *Shuffle1 = dyn_cast<ShuffleVectorInst>(Shuf.getOperand(1)); |
| 1881 | if (!Shuffle0 || !Shuffle0->isIdentityWithPadding() || |
| 1882 | !Shuffle1 || !Shuffle1->isIdentityWithPadding()) |
| 1883 | return nullptr; |
| 1884 | |
| 1885 | // We limit this transform to power-of-2 types because we expect that the |
| 1886 | // backend can convert the simplified IR patterns to identical nodes as the |
| 1887 | // original IR. |
Sanjay Patel | 3249be1 | 2019-05-23 18:46:03 +0000 | [diff] [blame] | 1888 | // TODO: If we can verify the same behavior for arbitrary types, the |
| 1889 | // power-of-2 checks can be removed. |
Sanjay Patel | 6a55418 | 2019-05-22 00:32:25 +0000 | [diff] [blame] | 1890 | Value *X = Shuffle0->getOperand(0); |
| 1891 | Value *Y = Shuffle1->getOperand(0); |
| 1892 | if (X->getType() != Y->getType() || |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1893 | !isPowerOf2_32(Shuf.getType()->getNumElements()) || |
| 1894 | !isPowerOf2_32(Shuffle0->getType()->getNumElements()) || |
| 1895 | !isPowerOf2_32(cast<VectorType>(X->getType())->getNumElements()) || |
Sanjay Patel | 6a55418 | 2019-05-22 00:32:25 +0000 | [diff] [blame] | 1896 | isa<UndefValue>(X) || isa<UndefValue>(Y)) |
| 1897 | return nullptr; |
| 1898 | assert(isa<UndefValue>(Shuffle0->getOperand(1)) && |
| 1899 | isa<UndefValue>(Shuffle1->getOperand(1)) && |
| 1900 | "Unexpected operand for identity shuffle"); |
| 1901 | |
| 1902 | // This is a shuffle of 2 widening shuffles. We can shuffle the narrow source |
| 1903 | // operands directly by adjusting the shuffle mask to account for the narrower |
| 1904 | // types: |
| 1905 | // shuf (widen X), (widen Y), Mask --> shuf X, Y, Mask' |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1906 | int NarrowElts = cast<VectorType>(X->getType())->getNumElements(); |
| 1907 | int WideElts = Shuffle0->getType()->getNumElements(); |
Sanjay Patel | 6a55418 | 2019-05-22 00:32:25 +0000 | [diff] [blame] | 1908 | assert(WideElts > NarrowElts && "Unexpected types for identity with padding"); |
| 1909 | |
| 1910 | Type *I32Ty = IntegerType::getInt32Ty(Shuf.getContext()); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1911 | ArrayRef<int> Mask = Shuf.getShuffleMask(); |
Sanjay Patel | 6a55418 | 2019-05-22 00:32:25 +0000 | [diff] [blame] | 1912 | SmallVector<Constant *, 16> NewMask(Mask.size(), UndefValue::get(I32Ty)); |
| 1913 | for (int i = 0, e = Mask.size(); i != e; ++i) { |
| 1914 | if (Mask[i] == -1) |
| 1915 | continue; |
Sanjay Patel | 3249be1 | 2019-05-23 18:46:03 +0000 | [diff] [blame] | 1916 | |
| 1917 | // If this shuffle is choosing an undef element from 1 of the sources, that |
| 1918 | // element is undef. |
| 1919 | if (Mask[i] < WideElts) { |
| 1920 | if (Shuffle0->getMaskValue(Mask[i]) == -1) |
| 1921 | continue; |
| 1922 | } else { |
| 1923 | if (Shuffle1->getMaskValue(Mask[i] - WideElts) == -1) |
| 1924 | continue; |
| 1925 | } |
| 1926 | |
| 1927 | // If this shuffle is choosing from the 1st narrow op, the mask element is |
| 1928 | // the same. If this shuffle is choosing from the 2nd narrow op, the mask |
| 1929 | // element is offset down to adjust for the narrow vector widths. |
| 1930 | if (Mask[i] < WideElts) { |
| 1931 | assert(Mask[i] < NarrowElts && "Unexpected shuffle mask"); |
Sanjay Patel | 6a55418 | 2019-05-22 00:32:25 +0000 | [diff] [blame] | 1932 | NewMask[i] = ConstantInt::get(I32Ty, Mask[i]); |
Sanjay Patel | 3249be1 | 2019-05-23 18:46:03 +0000 | [diff] [blame] | 1933 | } else { |
| 1934 | assert(Mask[i] < (WideElts + NarrowElts) && "Unexpected shuffle mask"); |
Sanjay Patel | 6a55418 | 2019-05-22 00:32:25 +0000 | [diff] [blame] | 1935 | NewMask[i] = ConstantInt::get(I32Ty, Mask[i] - (WideElts - NarrowElts)); |
Sanjay Patel | 3249be1 | 2019-05-23 18:46:03 +0000 | [diff] [blame] | 1936 | } |
Sanjay Patel | 6a55418 | 2019-05-22 00:32:25 +0000 | [diff] [blame] | 1937 | } |
| 1938 | return new ShuffleVectorInst(X, Y, ConstantVector::get(NewMask)); |
| 1939 | } |
| 1940 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1941 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 1942 | Value *LHS = SVI.getOperand(0); |
| 1943 | Value *RHS = SVI.getOperand(1); |
Sanjay Patel | f444806 | 2020-04-02 13:44:50 -0400 | [diff] [blame] | 1944 | SimplifyQuery ShufQuery = SQ.getWithInstruction(&SVI); |
| 1945 | if (auto *V = SimplifyShuffleVectorInst(LHS, RHS, SVI.getShuffleMask(), |
| 1946 | SVI.getType(), ShufQuery)) |
Zvi Rackover | 82bf48d | 2017-04-04 04:47:57 +0000 | [diff] [blame] | 1947 | return replaceInstUsesWith(SVI, V); |
| 1948 | |
Sanjay Patel | 3582716 | 2019-11-25 13:30:45 -0500 | [diff] [blame] | 1949 | // shuffle x, x, mask --> shuffle x, undef, mask' |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1950 | unsigned VWidth = SVI.getType()->getNumElements(); |
| 1951 | unsigned LHSWidth = cast<VectorType>(LHS->getType())->getNumElements(); |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1952 | ArrayRef<int> Mask = SVI.getShuffleMask(); |
Sanjay Patel | 3f4d1b4 | 2019-03-29 16:49:38 +0000 | [diff] [blame] | 1953 | Type *Int32Ty = Type::getInt32Ty(SVI.getContext()); |
Sanjay Patel | f444806 | 2020-04-02 13:44:50 -0400 | [diff] [blame] | 1954 | |
| 1955 | // Peek through a bitcasted shuffle operand by scaling the mask. If the |
| 1956 | // simulated shuffle can simplify, then this shuffle is unnecessary: |
| 1957 | // shuf (bitcast X), undef, Mask --> bitcast X' |
Sanjay Patel | 6a7e958 | 2020-04-14 14:41:35 -0400 | [diff] [blame] | 1958 | // TODO: This could be extended to allow length-changing shuffles. |
| 1959 | // The transform might also be obsoleted if we allowed canonicalization |
| 1960 | // of bitcasted shuffles. |
Sanjay Patel | f444806 | 2020-04-02 13:44:50 -0400 | [diff] [blame] | 1961 | Value *X; |
| 1962 | if (match(LHS, m_BitCast(m_Value(X))) && match(RHS, m_Undef()) && |
Sanjay Patel | 6a7e958 | 2020-04-14 14:41:35 -0400 | [diff] [blame] | 1963 | X->getType()->isVectorTy() && VWidth == LHSWidth) { |
| 1964 | // Try to create a scaled mask constant. |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 1965 | auto *XType = cast<VectorType>(X->getType()); |
| 1966 | unsigned XNumElts = XType->getNumElements(); |
Sanjay Patel | f444806 | 2020-04-02 13:44:50 -0400 | [diff] [blame] | 1967 | SmallVector<int, 16> ScaledMask; |
Sanjay Patel | 6a7e958 | 2020-04-14 14:41:35 -0400 | [diff] [blame] | 1968 | if (XNumElts >= VWidth) { |
| 1969 | assert(XNumElts % VWidth == 0 && "Unexpected vector bitcast"); |
| 1970 | narrowShuffleMaskElts(XNumElts / VWidth, Mask, ScaledMask); |
| 1971 | } else { |
| 1972 | assert(VWidth % XNumElts == 0 && "Unexpected vector bitcast"); |
| 1973 | if (!widenShuffleMaskElts(VWidth / XNumElts, Mask, ScaledMask)) |
| 1974 | ScaledMask.clear(); |
| 1975 | } |
| 1976 | if (!ScaledMask.empty()) { |
| 1977 | // If the shuffled source vector simplifies, cast that value to this |
| 1978 | // shuffle's type. |
| 1979 | if (auto *V = SimplifyShuffleVectorInst(X, UndefValue::get(XType), |
| 1980 | ScaledMask, XType, ShufQuery)) |
| 1981 | return BitCastInst::Create(Instruction::BitCast, V, SVI.getType()); |
| 1982 | } |
Sanjay Patel | f444806 | 2020-04-02 13:44:50 -0400 | [diff] [blame] | 1983 | } |
| 1984 | |
Sanjay Patel | 3582716 | 2019-11-25 13:30:45 -0500 | [diff] [blame] | 1985 | if (LHS == RHS) { |
Sanjay Patel | 847aabf | 2019-11-25 10:54:18 -0500 | [diff] [blame] | 1986 | assert(!isa<UndefValue>(RHS) && "Shuffle with 2 undef ops not simplified?"); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1987 | // Remap any references to RHS to use LHS. |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1988 | SmallVector<int, 16> Elts; |
Sanjay Patel | 2068409 | 2019-11-25 10:40:21 -0500 | [diff] [blame] | 1989 | for (unsigned i = 0; i != VWidth; ++i) { |
Sanjay Patel | 3582716 | 2019-11-25 13:30:45 -0500 | [diff] [blame] | 1990 | // Propagate undef elements or force mask to LHS. |
| 1991 | if (Mask[i] < 0) |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1992 | Elts.push_back(UndefMaskElem); |
Sanjay Patel | fc31b58 | 2019-11-25 11:11:12 -0500 | [diff] [blame] | 1993 | else |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1994 | Elts.push_back(Mask[i] % LHSWidth); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1995 | } |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 1996 | return new ShuffleVectorInst(LHS, UndefValue::get(RHS->getType()), Elts); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1997 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1998 | |
Sanjay Patel | 3582716 | 2019-11-25 13:30:45 -0500 | [diff] [blame] | 1999 | // shuffle undef, x, mask --> shuffle x, undef, mask' |
| 2000 | if (isa<UndefValue>(LHS)) { |
| 2001 | SVI.commute(); |
| 2002 | return &SVI; |
| 2003 | } |
| 2004 | |
Sanjay Patel | 0b59103 | 2019-07-08 16:26:48 +0000 | [diff] [blame] | 2005 | if (Instruction *I = canonicalizeInsertSplat(SVI, Builder)) |
| 2006 | return I; |
| 2007 | |
Sanjay Patel | 3f4d1b4 | 2019-03-29 16:49:38 +0000 | [diff] [blame] | 2008 | if (Instruction *I = foldSelectShuffle(SVI, Builder, DL)) |
| 2009 | return I; |
| 2010 | |
Sanjay Patel | 538a8f0 | 2020-04-05 09:46:22 -0400 | [diff] [blame] | 2011 | if (Instruction *I = foldTruncShuffle(SVI, DL.isBigEndian())) |
| 2012 | return I; |
| 2013 | |
Sanjay Patel | 3f4d1b4 | 2019-03-29 16:49:38 +0000 | [diff] [blame] | 2014 | if (Instruction *I = narrowVectorSelect(SVI, Builder)) |
| 2015 | return I; |
| 2016 | |
| 2017 | APInt UndefElts(VWidth, 0); |
| 2018 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
| 2019 | if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) { |
| 2020 | if (V != &SVI) |
| 2021 | return replaceInstUsesWith(SVI, V); |
| 2022 | return &SVI; |
| 2023 | } |
| 2024 | |
| 2025 | if (Instruction *I = foldIdentityExtractShuffle(SVI)) |
| 2026 | return I; |
| 2027 | |
Sanjay Patel | 6a55418 | 2019-05-22 00:32:25 +0000 | [diff] [blame] | 2028 | // These transforms have the potential to lose undef knowledge, so they are |
Sanjay Patel | 3f4d1b4 | 2019-03-29 16:49:38 +0000 | [diff] [blame] | 2029 | // intentionally placed after SimplifyDemandedVectorElts(). |
Nikita Popov | 5a8819b | 2020-02-03 21:17:36 +0100 | [diff] [blame] | 2030 | if (Instruction *I = foldShuffleWithInsert(SVI, *this)) |
Sanjay Patel | 3f4d1b4 | 2019-03-29 16:49:38 +0000 | [diff] [blame] | 2031 | return I; |
Sanjay Patel | 6a55418 | 2019-05-22 00:32:25 +0000 | [diff] [blame] | 2032 | if (Instruction *I = foldIdentityPaddedShuffles(SVI)) |
| 2033 | return I; |
Sanjay Patel | 3f4d1b4 | 2019-03-29 16:49:38 +0000 | [diff] [blame] | 2034 | |
Sanjay Patel | 26c119a | 2018-09-30 13:50:42 +0000 | [diff] [blame] | 2035 | if (isa<UndefValue>(RHS) && canEvaluateShuffled(LHS, Mask)) { |
Sanjay Patel | 54d31ef | 2018-09-29 15:05:24 +0000 | [diff] [blame] | 2036 | Value *V = evaluateInDifferentElementOrder(LHS, Mask); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2037 | return replaceInstUsesWith(SVI, V); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 2040 | // SROA generates shuffle+bitcast when the extracted sub-vector is bitcast to |
| 2041 | // a non-vector type. We can instead bitcast the original vector followed by |
| 2042 | // an extract of the desired element: |
| 2043 | // |
| 2044 | // %sroa = shufflevector <16 x i8> %in, <16 x i8> undef, |
| 2045 | // <4 x i32> <i32 0, i32 1, i32 2, i32 3> |
| 2046 | // %1 = bitcast <4 x i8> %sroa to i32 |
| 2047 | // Becomes: |
| 2048 | // %bc = bitcast <16 x i8> %in to <4 x i32> |
| 2049 | // %ext = extractelement <4 x i32> %bc, i32 0 |
| 2050 | // |
| 2051 | // If the shuffle is extracting a contiguous range of values from the input |
| 2052 | // vector then each use which is a bitcast of the extracted size can be |
| 2053 | // replaced. This will work if the vector types are compatible, and the begin |
| 2054 | // index is aligned to a value in the casted vector type. If the begin index |
| 2055 | // isn't aligned then we can shuffle the original vector (keeping the same |
| 2056 | // vector type) before extracting. |
| 2057 | // |
| 2058 | // This code will bail out if the target type is fundamentally incompatible |
| 2059 | // with vectors of the source type. |
| 2060 | // |
| 2061 | // Example of <16 x i8>, target type i32: |
| 2062 | // Index range [4,8): v-----------v Will work. |
| 2063 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 2064 | // <16 x i8>: | | | | | | | | | | | | | | | | | |
| 2065 | // <4 x i32>: | | | | | |
| 2066 | // +-----------+-----------+-----------+-----------+ |
| 2067 | // Index range [6,10): ^-----------^ Needs an extra shuffle. |
| 2068 | // Target type i40: ^--------------^ Won't work, bail. |
Sanjay Patel | 3f4d1b4 | 2019-03-29 16:49:38 +0000 | [diff] [blame] | 2069 | bool MadeChange = false; |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 2070 | if (isShuffleExtractingFromLHS(SVI, Mask)) { |
| 2071 | Value *V = LHS; |
| 2072 | unsigned MaskElems = Mask.size(); |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 2073 | VectorType *SrcTy = cast<VectorType>(V->getType()); |
Christopher Tetreault | 8226d59 | 2020-04-14 13:46:27 -0700 | [diff] [blame] | 2074 | unsigned VecBitWidth = SrcTy->getPrimitiveSizeInBits().getFixedSize(); |
David Majnemer | 98cfe2b | 2015-04-03 20:18:40 +0000 | [diff] [blame] | 2075 | unsigned SrcElemBitWidth = DL.getTypeSizeInBits(SrcTy->getElementType()); |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 2076 | assert(SrcElemBitWidth && "vector elements must have a bitwidth"); |
| 2077 | unsigned SrcNumElems = SrcTy->getNumElements(); |
| 2078 | SmallVector<BitCastInst *, 8> BCs; |
| 2079 | DenseMap<Type *, Value *> NewBCs; |
| 2080 | for (User *U : SVI.users()) |
| 2081 | if (BitCastInst *BC = dyn_cast<BitCastInst>(U)) |
| 2082 | if (!BC->use_empty()) |
| 2083 | // Only visit bitcasts that weren't previously handled. |
| 2084 | BCs.push_back(BC); |
| 2085 | for (BitCastInst *BC : BCs) { |
Eugene Leviant | 958fcd7 | 2017-02-17 07:36:03 +0000 | [diff] [blame] | 2086 | unsigned BegIdx = Mask.front(); |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 2087 | Type *TgtTy = BC->getDestTy(); |
David Majnemer | 98cfe2b | 2015-04-03 20:18:40 +0000 | [diff] [blame] | 2088 | unsigned TgtElemBitWidth = DL.getTypeSizeInBits(TgtTy); |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 2089 | if (!TgtElemBitWidth) |
| 2090 | continue; |
| 2091 | unsigned TgtNumElems = VecBitWidth / TgtElemBitWidth; |
| 2092 | bool VecBitWidthsEqual = VecBitWidth == TgtNumElems * TgtElemBitWidth; |
| 2093 | bool BegIsAligned = 0 == ((SrcElemBitWidth * BegIdx) % TgtElemBitWidth); |
| 2094 | if (!VecBitWidthsEqual) |
| 2095 | continue; |
| 2096 | if (!VectorType::isValidElementType(TgtTy)) |
| 2097 | continue; |
| 2098 | VectorType *CastSrcTy = VectorType::get(TgtTy, TgtNumElems); |
| 2099 | if (!BegIsAligned) { |
| 2100 | // Shuffle the input so [0,NumElements) contains the output, and |
| 2101 | // [NumElems,SrcNumElems) is undef. |
| 2102 | SmallVector<Constant *, 16> ShuffleMask(SrcNumElems, |
| 2103 | UndefValue::get(Int32Ty)); |
| 2104 | for (unsigned I = 0, E = MaskElems, Idx = BegIdx; I != E; ++Idx, ++I) |
| 2105 | ShuffleMask[I] = ConstantInt::get(Int32Ty, Idx); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2106 | V = Builder.CreateShuffleVector(V, UndefValue::get(V->getType()), |
| 2107 | ConstantVector::get(ShuffleMask), |
| 2108 | SVI.getName() + ".extract"); |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 2109 | BegIdx = 0; |
| 2110 | } |
| 2111 | unsigned SrcElemsPerTgtElem = TgtElemBitWidth / SrcElemBitWidth; |
| 2112 | assert(SrcElemsPerTgtElem); |
| 2113 | BegIdx /= SrcElemsPerTgtElem; |
| 2114 | bool BCAlreadyExists = NewBCs.find(CastSrcTy) != NewBCs.end(); |
| 2115 | auto *NewBC = |
| 2116 | BCAlreadyExists |
| 2117 | ? NewBCs[CastSrcTy] |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2118 | : Builder.CreateBitCast(V, CastSrcTy, SVI.getName() + ".bc"); |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 2119 | if (!BCAlreadyExists) |
| 2120 | NewBCs[CastSrcTy] = NewBC; |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2121 | auto *Ext = Builder.CreateExtractElement( |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 2122 | NewBC, ConstantInt::get(Int32Ty, BegIdx), SVI.getName() + ".extract"); |
| 2123 | // The shufflevector isn't being replaced: the bitcast that used it |
| 2124 | // is. InstCombine will visit the newly-created instructions. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2125 | replaceInstUsesWith(*BC, Ext); |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 2126 | MadeChange = true; |
| 2127 | } |
| 2128 | } |
| 2129 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 2130 | // If the LHS is a shufflevector itself, see if we can combine it with this |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2131 | // one without producing an unusual shuffle. |
| 2132 | // Cases that might be simplified: |
| 2133 | // 1. |
| 2134 | // x1=shuffle(v1,v2,mask1) |
| 2135 | // x=shuffle(x1,undef,mask) |
| 2136 | // ==> |
| 2137 | // x=shuffle(v1,undef,newMask) |
| 2138 | // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : -1 |
| 2139 | // 2. |
| 2140 | // x1=shuffle(v1,undef,mask1) |
| 2141 | // x=shuffle(x1,x2,mask) |
| 2142 | // where v1.size() == mask1.size() |
| 2143 | // ==> |
| 2144 | // x=shuffle(v1,x2,newMask) |
| 2145 | // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : mask[i] |
| 2146 | // 3. |
| 2147 | // x2=shuffle(v2,undef,mask2) |
| 2148 | // x=shuffle(x1,x2,mask) |
| 2149 | // where v2.size() == mask2.size() |
| 2150 | // ==> |
| 2151 | // x=shuffle(x1,v2,newMask) |
| 2152 | // newMask[i] = (mask[i] < x1.size()) |
| 2153 | // ? mask[i] : mask2[mask[i]-x1.size()]+x1.size() |
| 2154 | // 4. |
| 2155 | // x1=shuffle(v1,undef,mask1) |
| 2156 | // x2=shuffle(v2,undef,mask2) |
| 2157 | // x=shuffle(x1,x2,mask) |
| 2158 | // where v1.size() == v2.size() |
| 2159 | // ==> |
| 2160 | // x=shuffle(v1,v2,newMask) |
| 2161 | // newMask[i] = (mask[i] < x1.size()) |
| 2162 | // ? mask1[mask[i]] : mask2[mask[i]-x1.size()]+v1.size() |
| 2163 | // |
| 2164 | // Here we are really conservative: |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 2165 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 2166 | // program, because the code gen may not be smart enough to turn a merged |
| 2167 | // shuffle into two specific shuffles: it may produce worse code. As such, |
Jim Grosbach | d11584a | 2013-05-01 00:25:27 +0000 | [diff] [blame] | 2168 | // we only merge two shuffles if the result is either a splat or one of the |
| 2169 | // input shuffle masks. In this case, merging the shuffles just removes |
| 2170 | // one instruction, which we know is safe. This is good for things like |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2171 | // turning: (splat(splat)) -> splat, or |
| 2172 | // merge(V[0..n], V[n+1..2n]) -> V[0..2n] |
| 2173 | ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS); |
| 2174 | ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS); |
| 2175 | if (LHSShuffle) |
| 2176 | if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2177 | LHSShuffle = nullptr; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2178 | if (RHSShuffle) |
| 2179 | if (!isa<UndefValue>(RHSShuffle->getOperand(1))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2180 | RHSShuffle = nullptr; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2181 | if (!LHSShuffle && !RHSShuffle) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2182 | return MadeChange ? &SVI : nullptr; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2183 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2184 | Value* LHSOp0 = nullptr; |
| 2185 | Value* LHSOp1 = nullptr; |
| 2186 | Value* RHSOp0 = nullptr; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2187 | unsigned LHSOp0Width = 0; |
| 2188 | unsigned RHSOp0Width = 0; |
| 2189 | if (LHSShuffle) { |
| 2190 | LHSOp0 = LHSShuffle->getOperand(0); |
| 2191 | LHSOp1 = LHSShuffle->getOperand(1); |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 2192 | LHSOp0Width = cast<VectorType>(LHSOp0->getType())->getNumElements(); |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2193 | } |
| 2194 | if (RHSShuffle) { |
| 2195 | RHSOp0 = RHSShuffle->getOperand(0); |
Christopher Tetreault | 155740c | 2020-04-08 10:42:22 -0700 | [diff] [blame] | 2196 | RHSOp0Width = cast<VectorType>(RHSOp0->getType())->getNumElements(); |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2197 | } |
| 2198 | Value* newLHS = LHS; |
| 2199 | Value* newRHS = RHS; |
| 2200 | if (LHSShuffle) { |
| 2201 | // case 1 |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 2202 | if (isa<UndefValue>(RHS)) { |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2203 | newLHS = LHSOp0; |
| 2204 | newRHS = LHSOp1; |
| 2205 | } |
| 2206 | // case 2 or 4 |
| 2207 | else if (LHSOp0Width == LHSWidth) { |
| 2208 | newLHS = LHSOp0; |
| 2209 | } |
| 2210 | } |
| 2211 | // case 3 or 4 |
| 2212 | if (RHSShuffle && RHSOp0Width == LHSWidth) { |
| 2213 | newRHS = RHSOp0; |
| 2214 | } |
| 2215 | // case 4 |
| 2216 | if (LHSOp0 == RHSOp0) { |
| 2217 | newLHS = LHSOp0; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2218 | newRHS = nullptr; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2219 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 2220 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2221 | if (newLHS == LHS && newRHS == RHS) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2222 | return MadeChange ? &SVI : nullptr; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 2223 | |
Eli Friedman | 1ee6ec2 | 2020-03-31 13:08:59 -0700 | [diff] [blame] | 2224 | ArrayRef<int> LHSMask; |
| 2225 | ArrayRef<int> RHSMask; |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 2226 | if (newLHS != LHS) |
| 2227 | LHSMask = LHSShuffle->getShuffleMask(); |
| 2228 | if (RHSShuffle && newRHS != RHS) |
| 2229 | RHSMask = RHSShuffle->getShuffleMask(); |
| 2230 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2231 | unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth; |
| 2232 | SmallVector<int, 16> newMask; |
| 2233 | bool isSplat = true; |
| 2234 | int SplatElt = -1; |
| 2235 | // Create a new mask for the new ShuffleVectorInst so that the new |
| 2236 | // ShuffleVectorInst is equivalent to the original one. |
| 2237 | for (unsigned i = 0; i < VWidth; ++i) { |
| 2238 | int eltMask; |
Craig Topper | 45d9f4b | 2013-01-18 05:30:07 +0000 | [diff] [blame] | 2239 | if (Mask[i] < 0) { |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2240 | // This element is an undef value. |
| 2241 | eltMask = -1; |
| 2242 | } else if (Mask[i] < (int)LHSWidth) { |
| 2243 | // This element is from left hand side vector operand. |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 2244 | // |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2245 | // If LHS is going to be replaced (case 1, 2, or 4), calculate the |
| 2246 | // new mask value for the element. |
| 2247 | if (newLHS != LHS) { |
| 2248 | eltMask = LHSMask[Mask[i]]; |
| 2249 | // If the value selected is an undef value, explicitly specify it |
| 2250 | // with a -1 mask value. |
| 2251 | if (eltMask >= (int)LHSOp0Width && isa<UndefValue>(LHSOp1)) |
| 2252 | eltMask = -1; |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 2253 | } else |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2254 | eltMask = Mask[i]; |
| 2255 | } else { |
| 2256 | // This element is from right hand side vector operand |
| 2257 | // |
| 2258 | // If the value selected is an undef value, explicitly specify it |
| 2259 | // with a -1 mask value. (case 1) |
| 2260 | if (isa<UndefValue>(RHS)) |
| 2261 | eltMask = -1; |
| 2262 | // If RHS is going to be replaced (case 3 or 4), calculate the |
| 2263 | // new mask value for the element. |
| 2264 | else if (newRHS != RHS) { |
| 2265 | eltMask = RHSMask[Mask[i]-LHSWidth]; |
| 2266 | // If the value selected is an undef value, explicitly specify it |
| 2267 | // with a -1 mask value. |
| 2268 | if (eltMask >= (int)RHSOp0Width) { |
| 2269 | assert(isa<UndefValue>(RHSShuffle->getOperand(1)) |
| 2270 | && "should have been check above"); |
| 2271 | eltMask = -1; |
Nate Begeman | 2a0ca3e9 | 2010-08-13 00:17:53 +0000 | [diff] [blame] | 2272 | } |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 2273 | } else |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2274 | eltMask = Mask[i]-LHSWidth; |
| 2275 | |
| 2276 | // If LHS's width is changed, shift the mask value accordingly. |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 2277 | // If newRHS == nullptr, i.e. LHSOp0 == RHSOp0, we want to remap any |
Michael Gottesman | 02a1141 | 2012-10-16 21:29:38 +0000 | [diff] [blame] | 2278 | // references from RHSOp0 to LHSOp0, so we don't need to shift the mask. |
| 2279 | // If newRHS == newLHS, we want to remap any references from newRHS to |
| 2280 | // newLHS so that we can properly identify splats that may occur due to |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 2281 | // obfuscation across the two vectors. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2282 | if (eltMask >= 0 && newRHS != nullptr && newLHS != newRHS) |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2283 | eltMask += newLHSWidth; |
Nate Begeman | 2a0ca3e9 | 2010-08-13 00:17:53 +0000 | [diff] [blame] | 2284 | } |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2285 | |
| 2286 | // Check if this could still be a splat. |
| 2287 | if (eltMask >= 0) { |
| 2288 | if (SplatElt >= 0 && SplatElt != eltMask) |
| 2289 | isSplat = false; |
| 2290 | SplatElt = eltMask; |
| 2291 | } |
| 2292 | |
| 2293 | newMask.push_back(eltMask); |
| 2294 | } |
| 2295 | |
| 2296 | // If the result mask is equal to one of the original shuffle masks, |
Jim Grosbach | d11584a | 2013-05-01 00:25:27 +0000 | [diff] [blame] | 2297 | // or is a splat, do the replacement. |
| 2298 | if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) { |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2299 | SmallVector<Constant*, 16> Elts; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2300 | for (unsigned i = 0, e = newMask.size(); i != e; ++i) { |
| 2301 | if (newMask[i] < 0) { |
| 2302 | Elts.push_back(UndefValue::get(Int32Ty)); |
| 2303 | } else { |
| 2304 | Elts.push_back(ConstantInt::get(Int32Ty, newMask[i])); |
| 2305 | } |
| 2306 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2307 | if (!newRHS) |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 2308 | newRHS = UndefValue::get(newLHS->getType()); |
| 2309 | return new ShuffleVectorInst(newLHS, newRHS, ConstantVector::get(Elts)); |
Nate Begeman | 2a0ca3e9 | 2010-08-13 00:17:53 +0000 | [diff] [blame] | 2310 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 2311 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2312 | return MadeChange ? &SVI : nullptr; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 2313 | } |