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