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