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