blob: dd579a6632544dfdc06dfe3e0e5696c775b8300c [file] [log] [blame]
Chris Lattnerec97a902010-01-05 05:36:20 +00001//===- 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 Carrutha9174582015-01-22 05:25:13 +000015#include "InstCombineInternal.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000016#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
JF Bastiend52c9902015-02-25 22:30:51 +000018#include "llvm/ADT/DenseMap.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000019#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallVector.h"
David Majnemer599ca442015-07-13 01:15:53 +000021#include "llvm/Analysis/InstructionSimplify.h"
22#include "llvm/Analysis/VectorUtils.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000023#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 Carruth820a9082014-03-04 11:08:18 +000031#include "llvm/IR/PatternMatch.h"
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +000032#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 Lattnerec97a902010-01-05 05:36:20 +000043using namespace llvm;
Nadav Rotem7df85092013-01-15 23:43:14 +000044using namespace PatternMatch;
Chris Lattnerec97a902010-01-05 05:36:20 +000045
Chandler Carruth964daaa2014-04-22 02:55:47 +000046#define DEBUG_TYPE "instcombine"
47
Sanjay Patel6eccf482015-09-09 15:24:36 +000048/// 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 Patel431e1142015-11-17 17:24:08 +000051static bool cheapToScalarize(Value *V, bool isConstant) {
Chris Lattner8326bd82012-01-26 00:42:34 +000052 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +000053 if (isConstant) return true;
Chris Lattner8326bd82012-01-26 00:42:34 +000054
55 // If all elts are the same, we can extract it and use any of the values.
Benjamin Kramer09b0f882014-01-24 19:02:37 +000056 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 Lattnerec97a902010-01-05 05:36:20 +000063 }
64 Instruction *I = dyn_cast<Instruction>(V);
65 if (!I) return false;
Bob Wilson8ecf98b2010-10-29 22:20:43 +000066
Chris Lattnerec97a902010-01-05 05:36:20 +000067 // 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 Patel431e1142015-11-17 17:24:08 +000076 (cheapToScalarize(BO->getOperand(0), isConstant) ||
77 cheapToScalarize(BO->getOperand(1), isConstant)))
Chris Lattnerec97a902010-01-05 05:36:20 +000078 return true;
79 if (CmpInst *CI = dyn_cast<CmpInst>(I))
80 if (CI->hasOneUse() &&
Sanjay Patel431e1142015-11-17 17:24:08 +000081 (cheapToScalarize(CI->getOperand(0), isConstant) ||
82 cheapToScalarize(CI->getOperand(1), isConstant)))
Chris Lattnerec97a902010-01-05 05:36:20 +000083 return true;
Bob Wilson8ecf98b2010-10-29 22:20:43 +000084
Chris Lattnerec97a902010-01-05 05:36:20 +000085 return false;
86}
87
Michael Kupersteina0c6ae02016-06-06 23:38:33 +000088// If we have a PHI node with a vector type that is only used to feed
Matt Arsenault38874732013-08-28 22:17:26 +000089// 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 Shemer0c95efa2013-04-18 19:35:39 +000091Instruction *InstCombiner::scalarizePHI(ExtractElementInst &EI, PHINode *PN) {
Michael Kupersteina0c6ae02016-06-06 23:38:33 +000092 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 Shemer0c95efa2013-04-18 19:35:39 +0000110
Michael Kupersteina0c6ae02016-06-06 23:38:33 +0000111 if (!PHIUser)
112 return nullptr;
Anat Shemer0c95efa2013-04-18 19:35:39 +0000113
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 Zelenko7f0f9bc2017-10-24 21:24:53 +0000116 // otherwise return nullptr.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000117 if (!PHIUser->hasOneUse() || !(PHIUser->user_back() == PN) ||
Sanjay Patel431e1142015-11-17 17:24:08 +0000118 !(isa<BinaryOperator>(PHIUser)) || !cheapToScalarize(PHIUser, true))
Craig Topperf40110f2014-04-25 05:29:35 +0000119 return nullptr;
Anat Shemer0c95efa2013-04-18 19:35:39 +0000120
121 // Create a scalar PHI node that will replace the vector PHI node
122 // just before the current PHI node.
Joey Goulyb34294d2013-05-24 12:33:28 +0000123 PHINode *scalarPHI = cast<PHINode>(InsertNewInstWith(
124 PHINode::Create(EI.getType(), PN->getNumIncomingValues(), ""), *PN));
Anat Shemer0c95efa2013-04-18 19:35:39 +0000125 // Scalarize each PHI operand.
Joey Goulyb34294d2013-05-24 12:33:28 +0000126 for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
Anat Shemer0c95efa2013-04-18 19:35:39 +0000127 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 Patel70af1fd2014-07-07 22:13:58 +0000133 // scalar PHI, and the second operand is extracted from the other
Anat Shemer0c95efa2013-04-18 19:35:39 +0000134 // vector operand.
135 BinaryOperator *B0 = cast<BinaryOperator>(PHIUser);
Joey Goulyb34294d2013-05-24 12:33:28 +0000136 unsigned opId = (B0->getOperand(0) == PN) ? 1 : 0;
Joey Gouly83699282013-05-24 12:29:54 +0000137 Value *Op = InsertNewInstWith(
138 ExtractElementInst::Create(B0->getOperand(opId), Elt,
139 B0->getOperand(opId)->getName() + ".Elt"),
140 *B0);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000141 Value *newPHIUser = InsertNewInstWith(
Owen Anderson7ea02fc2016-03-01 19:35:52 +0000142 BinaryOperator::CreateWithCopiedFlags(B0->getOpcode(),
143 scalarPHI, Op, B0), *B0);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000144 scalarPHI->addIncoming(newPHIUser, inBB);
145 } else {
146 // Scalarize PHI input:
Joey Goulyb34294d2013-05-24 12:33:28 +0000147 Instruction *newEI = ExtractElementInst::Create(PHIInVal, Elt, "");
Anat Shemer0c95efa2013-04-18 19:35:39 +0000148 // 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 Smith9f8aaf22015-10-13 16:59:33 +0000152 InsertPos = ++pos->getIterator();
Anat Shemer0c95efa2013-04-18 19:35:39 +0000153 } else {
154 InsertPos = inBB->getFirstInsertionPt();
155 }
156
157 InsertNewInstWith(newEI, *InsertPos);
158
159 scalarPHI->addIncoming(newEI, inBB);
160 }
161 }
Michael Kupersteina0c6ae02016-06-06 23:38:33 +0000162
163 for (auto E : Extracts)
164 replaceInstUsesWith(*E, scalarPHI);
165
166 return &EI;
Anat Shemer0c95efa2013-04-18 19:35:39 +0000167}
168
Chris Lattnerec97a902010-01-05 05:36:20 +0000169Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
Daniel Berlin2c75c632017-04-26 20:56:07 +0000170 if (Value *V = SimplifyExtractElementInst(EI.getVectorOperand(),
Craig Toppera4205622017-06-09 03:21:29 +0000171 EI.getIndexOperand(),
172 SQ.getWithInstruction(&EI)))
Sanjay Patel4b198802016-02-01 22:23:39 +0000173 return replaceInstUsesWith(EI, V);
David Majnemer599ca442015-07-13 01:15:53 +0000174
Chris Lattner8326bd82012-01-26 00:42:34 +0000175 // If vector val is constant with all elements the same, replace EI with
176 // that element. We handle a known element # below.
177 if (Constant *C = dyn_cast<Constant>(EI.getOperand(0)))
Sanjay Patel431e1142015-11-17 17:24:08 +0000178 if (cheapToScalarize(C, false))
Sanjay Patel4b198802016-02-01 22:23:39 +0000179 return replaceInstUsesWith(EI, C->getAggregateElement(0U));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000180
Chris Lattnerec97a902010-01-05 05:36:20 +0000181 // If extracting a specified index from the vector, see if we can recursively
182 // find a previously computed scalar that was inserted into the vector.
183 if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000184 unsigned VectorWidth = EI.getVectorOperandType()->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000185
Simon Pilgrime7d032f2017-12-27 12:00:18 +0000186 // InstSimplify should handle cases where the index is invalid.
187 if (!IdxC->getValue().ule(VectorWidth))
188 return nullptr;
189
190 unsigned IndexVal = IdxC->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000191
Chris Lattnerec97a902010-01-05 05:36:20 +0000192 // This instruction only demands the single element from the input vector.
193 // If the input vector has a single use, simplify it based on this use
194 // property.
195 if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
196 APInt UndefElts(VectorWidth, 0);
Chris Lattnerb22423c2010-02-08 23:56:03 +0000197 APInt DemandedMask(VectorWidth, 0);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000198 DemandedMask.setBit(IndexVal);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000199 if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), DemandedMask,
200 UndefElts)) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000201 EI.setOperand(0, V);
202 return &EI;
203 }
204 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000205
Sanjay Patelb67076c2015-11-29 22:09:34 +0000206 // If this extractelement is directly using a bitcast from a vector of
Chris Lattnerec97a902010-01-05 05:36:20 +0000207 // the same number of elements, see if we can find the source element from
208 // it. In this case, we will end up needing to bitcast the scalars.
209 if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) {
Chris Lattner8326bd82012-01-26 00:42:34 +0000210 if (VectorType *VT = dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
Chris Lattnerec97a902010-01-05 05:36:20 +0000211 if (VT->getNumElements() == VectorWidth)
David Majnemer599ca442015-07-13 01:15:53 +0000212 if (Value *Elt = findScalarElement(BCI->getOperand(0), IndexVal))
Chris Lattnerec97a902010-01-05 05:36:20 +0000213 return new BitCastInst(Elt, EI.getType());
214 }
Anat Shemer0c95efa2013-04-18 19:35:39 +0000215
216 // If there's a vector PHI feeding a scalar use through this extractelement
217 // instruction, try to scalarize the PHI.
218 if (PHINode *PN = dyn_cast<PHINode>(EI.getOperand(0))) {
Nick Lewycky881e9d62013-05-04 01:08:15 +0000219 Instruction *scalarPHI = scalarizePHI(EI, PN);
220 if (scalarPHI)
Joey Goulyb34294d2013-05-24 12:33:28 +0000221 return scalarPHI;
Anat Shemer0c95efa2013-04-18 19:35:39 +0000222 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000223 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000224
Chris Lattnerec97a902010-01-05 05:36:20 +0000225 if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) {
226 // Push extractelement into predecessor operation if legal and
Sanjay Patelb67076c2015-11-29 22:09:34 +0000227 // profitable to do so.
Chris Lattnerec97a902010-01-05 05:36:20 +0000228 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
229 if (I->hasOneUse() &&
Sanjay Patel431e1142015-11-17 17:24:08 +0000230 cheapToScalarize(BO, isa<ConstantInt>(EI.getOperand(1)))) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000231 Value *newEI0 =
Craig Topperbb4069e2017-07-07 23:16:26 +0000232 Builder.CreateExtractElement(BO->getOperand(0), EI.getOperand(1),
233 EI.getName()+".lhs");
Chris Lattnerec97a902010-01-05 05:36:20 +0000234 Value *newEI1 =
Craig Topperbb4069e2017-07-07 23:16:26 +0000235 Builder.CreateExtractElement(BO->getOperand(1), EI.getOperand(1),
236 EI.getName()+".rhs");
Owen Anderson7ea02fc2016-03-01 19:35:52 +0000237 return BinaryOperator::CreateWithCopiedFlags(BO->getOpcode(),
238 newEI0, newEI1, BO);
Chris Lattnerec97a902010-01-05 05:36:20 +0000239 }
240 } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
241 // Extracting the inserted element?
242 if (IE->getOperand(2) == EI.getOperand(1))
Sanjay Patel4b198802016-02-01 22:23:39 +0000243 return replaceInstUsesWith(EI, IE->getOperand(1));
Chris Lattnerec97a902010-01-05 05:36:20 +0000244 // If the inserted and extracted elements are constants, they must not
245 // be the same value, extract from the pre-inserted value instead.
246 if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) {
247 Worklist.AddValue(EI.getOperand(0));
248 EI.setOperand(0, IE->getOperand(0));
249 return &EI;
250 }
251 } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) {
252 // If this is extracting an element from a shufflevector, figure out where
253 // it came from and extract from the appropriate input element instead.
254 if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) {
Eli Friedman303c81c2011-10-21 19:11:34 +0000255 int SrcIdx = SVI->getMaskValue(Elt->getZExtValue());
Chris Lattnerec97a902010-01-05 05:36:20 +0000256 Value *Src;
257 unsigned LHSWidth =
Chris Lattner8326bd82012-01-26 00:42:34 +0000258 SVI->getOperand(0)->getType()->getVectorNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000259
Bob Wilson11ee4562010-10-29 22:03:05 +0000260 if (SrcIdx < 0)
Sanjay Patel4b198802016-02-01 22:23:39 +0000261 return replaceInstUsesWith(EI, UndefValue::get(EI.getType()));
Bob Wilson11ee4562010-10-29 22:03:05 +0000262 if (SrcIdx < (int)LHSWidth)
Chris Lattnerec97a902010-01-05 05:36:20 +0000263 Src = SVI->getOperand(0);
Bob Wilson11ee4562010-10-29 22:03:05 +0000264 else {
Chris Lattnerec97a902010-01-05 05:36:20 +0000265 SrcIdx -= LHSWidth;
266 Src = SVI->getOperand(1);
Chris Lattnerec97a902010-01-05 05:36:20 +0000267 }
Chris Lattner229907c2011-07-18 04:54:35 +0000268 Type *Int32Ty = Type::getInt32Ty(EI.getContext());
Chris Lattnerec97a902010-01-05 05:36:20 +0000269 return ExtractElementInst::Create(Src,
Bob Wilson9d07f392010-10-29 22:03:07 +0000270 ConstantInt::get(Int32Ty,
Chris Lattnerec97a902010-01-05 05:36:20 +0000271 SrcIdx, false));
272 }
Nadav Rotemd74b72b2011-03-31 22:57:29 +0000273 } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
Sanjay Patelb67076c2015-11-29 22:09:34 +0000274 // Canonicalize extractelement(cast) -> cast(extractelement).
275 // Bitcasts can change the number of vector elements, and they cost
276 // nothing.
Anat Shemer55703182013-04-18 19:56:44 +0000277 if (CI->hasOneUse() && (CI->getOpcode() != Instruction::BitCast)) {
Craig Topperbb4069e2017-07-07 23:16:26 +0000278 Value *EE = Builder.CreateExtractElement(CI->getOperand(0),
279 EI.getIndexOperand());
Anat Shemer10260a72013-04-22 20:51:10 +0000280 Worklist.AddValue(EE);
Nadav Rotemd74b72b2011-03-31 22:57:29 +0000281 return CastInst::Create(CI->getOpcode(), EE, EI.getType());
282 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000283 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000284 }
Craig Topperf40110f2014-04-25 05:29:35 +0000285 return nullptr;
Chris Lattnerec97a902010-01-05 05:36:20 +0000286}
287
Sanjay Patel6eccf482015-09-09 15:24:36 +0000288/// If V is a shuffle of values that ONLY returns elements from either LHS or
289/// RHS, return the shuffle mask and true. Otherwise, return false.
Sanjay Patel431e1142015-11-17 17:24:08 +0000290static bool collectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
Chris Lattner0256be92012-01-27 03:08:05 +0000291 SmallVectorImpl<Constant*> &Mask) {
Tim Northoverfad27612014-03-07 10:24:44 +0000292 assert(LHS->getType() == RHS->getType() &&
Chris Lattnerec97a902010-01-05 05:36:20 +0000293 "Invalid CollectSingleShuffleElements");
Matt Arsenault8227b9f2013-09-06 00:37:24 +0000294 unsigned NumElts = V->getType()->getVectorNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000295
Chris Lattnerec97a902010-01-05 05:36:20 +0000296 if (isa<UndefValue>(V)) {
297 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
298 return true;
299 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000300
Chris Lattnerec97a902010-01-05 05:36:20 +0000301 if (V == LHS) {
302 for (unsigned i = 0; i != NumElts; ++i)
303 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
304 return true;
305 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000306
Chris Lattnerec97a902010-01-05 05:36:20 +0000307 if (V == RHS) {
308 for (unsigned i = 0; i != NumElts; ++i)
309 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()),
310 i+NumElts));
311 return true;
312 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000313
Chris Lattnerec97a902010-01-05 05:36:20 +0000314 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
315 // If this is an insert of an extract from some other vector, include it.
316 Value *VecOp = IEI->getOperand(0);
317 Value *ScalarOp = IEI->getOperand(1);
318 Value *IdxOp = IEI->getOperand(2);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000319
Chris Lattnerec97a902010-01-05 05:36:20 +0000320 if (!isa<ConstantInt>(IdxOp))
321 return false;
322 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000323
Chris Lattnerec97a902010-01-05 05:36:20 +0000324 if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector.
Sanjay Patel70af1fd2014-07-07 22:13:58 +0000325 // We can handle this if the vector we are inserting into is
Chris Lattnerec97a902010-01-05 05:36:20 +0000326 // transitively ok.
Sanjay Patel431e1142015-11-17 17:24:08 +0000327 if (collectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000328 // If so, update the mask to reflect the inserted undef.
329 Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext()));
330 return true;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000331 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000332 } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
Tim Northoverfad27612014-03-07 10:24:44 +0000333 if (isa<ConstantInt>(EI->getOperand(1))) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000334 unsigned ExtractedIdx =
335 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Tim Northoverfad27612014-03-07 10:24:44 +0000336 unsigned NumLHSElts = LHS->getType()->getVectorNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000337
Chris Lattnerec97a902010-01-05 05:36:20 +0000338 // This must be extracting from either LHS or RHS.
339 if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
Sanjay Patel70af1fd2014-07-07 22:13:58 +0000340 // We can handle this if the vector we are inserting into is
Chris Lattnerec97a902010-01-05 05:36:20 +0000341 // transitively ok.
Sanjay Patel431e1142015-11-17 17:24:08 +0000342 if (collectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000343 // If so, update the mask to reflect the inserted value.
344 if (EI->getOperand(0) == LHS) {
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000345 Mask[InsertedIdx % NumElts] =
Chris Lattnerec97a902010-01-05 05:36:20 +0000346 ConstantInt::get(Type::getInt32Ty(V->getContext()),
347 ExtractedIdx);
348 } else {
349 assert(EI->getOperand(0) == RHS);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000350 Mask[InsertedIdx % NumElts] =
Chris Lattnerec97a902010-01-05 05:36:20 +0000351 ConstantInt::get(Type::getInt32Ty(V->getContext()),
Tim Northoverfad27612014-03-07 10:24:44 +0000352 ExtractedIdx + NumLHSElts);
Chris Lattnerec97a902010-01-05 05:36:20 +0000353 }
354 return true;
355 }
356 }
357 }
358 }
359 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000360
Chris Lattnerec97a902010-01-05 05:36:20 +0000361 return false;
362}
363
Sanjay Patelae945e72015-12-24 21:17:56 +0000364/// If we have insertion into a vector that is wider than the vector that we
365/// are extracting from, try to widen the source vector to allow a single
366/// shufflevector to replace one or more insert/extract pairs.
367static void replaceExtractElements(InsertElementInst *InsElt,
368 ExtractElementInst *ExtElt,
369 InstCombiner &IC) {
370 VectorType *InsVecType = InsElt->getType();
371 VectorType *ExtVecType = ExtElt->getVectorOperandType();
372 unsigned NumInsElts = InsVecType->getVectorNumElements();
373 unsigned NumExtElts = ExtVecType->getVectorNumElements();
374
375 // The inserted-to vector must be wider than the extracted-from vector.
376 if (InsVecType->getElementType() != ExtVecType->getElementType() ||
377 NumExtElts >= NumInsElts)
378 return;
379
380 // Create a shuffle mask to widen the extended-from vector using undefined
381 // values. The mask selects all of the values of the original vector followed
382 // by as many undefined values as needed to create a vector of the same length
383 // as the inserted-to vector.
384 SmallVector<Constant *, 16> ExtendMask;
385 IntegerType *IntType = Type::getInt32Ty(InsElt->getContext());
386 for (unsigned i = 0; i < NumExtElts; ++i)
387 ExtendMask.push_back(ConstantInt::get(IntType, i));
388 for (unsigned i = NumExtElts; i < NumInsElts; ++i)
389 ExtendMask.push_back(UndefValue::get(IntType));
390
391 Value *ExtVecOp = ExtElt->getVectorOperand();
Sanjay Patel66fff732016-01-29 20:21:02 +0000392 auto *ExtVecOpInst = dyn_cast<Instruction>(ExtVecOp);
393 BasicBlock *InsertionBlock = (ExtVecOpInst && !isa<PHINode>(ExtVecOpInst))
394 ? ExtVecOpInst->getParent()
395 : ExtElt->getParent();
396
397 // TODO: This restriction matches the basic block check below when creating
398 // new extractelement instructions. If that limitation is removed, this one
399 // could also be removed. But for now, we just bail out to ensure that we
400 // will replace the extractelement instruction that is feeding our
401 // insertelement instruction. This allows the insertelement to then be
402 // replaced by a shufflevector. If the insertelement is not replaced, we can
403 // induce infinite looping because there's an optimization for extractelement
404 // that will delete our widening shuffle. This would trigger another attempt
405 // here to create that shuffle, and we spin forever.
406 if (InsertionBlock != InsElt->getParent())
407 return;
408
Sanjay Patel4e1b5a52016-11-10 00:15:14 +0000409 // TODO: This restriction matches the check in visitInsertElementInst() and
410 // prevents an infinite loop caused by not turning the extract/insert pair
411 // into a shuffle. We really should not need either check, but we're lacking
412 // folds for shufflevectors because we're afraid to generate shuffle masks
413 // that the backend can't handle.
414 if (InsElt->hasOneUse() && isa<InsertElementInst>(InsElt->user_back()))
415 return;
416
Sanjay Patelae945e72015-12-24 21:17:56 +0000417 auto *WideVec = new ShuffleVectorInst(ExtVecOp, UndefValue::get(ExtVecType),
418 ConstantVector::get(ExtendMask));
419
Sanjay Patela1c53472016-01-05 19:09:47 +0000420 // Insert the new shuffle after the vector operand of the extract is defined
Sanjay Pateld72a4582016-01-08 01:39:16 +0000421 // (as long as it's not a PHI) or at the start of the basic block of the
422 // extract, so any subsequent extracts in the same basic block can use it.
423 // TODO: Insert before the earliest ExtractElementInst that is replaced.
Sanjay Pateld72a4582016-01-08 01:39:16 +0000424 if (ExtVecOpInst && !isa<PHINode>(ExtVecOpInst))
Sanjay Patela1c53472016-01-05 19:09:47 +0000425 WideVec->insertAfter(ExtVecOpInst);
Sanjay Pateld72a4582016-01-08 01:39:16 +0000426 else
Sanjay Patela1c53472016-01-05 19:09:47 +0000427 IC.InsertNewInstWith(WideVec, *ExtElt->getParent()->getFirstInsertionPt());
Sanjay Patela1c53472016-01-05 19:09:47 +0000428
429 // Replace extracts from the original narrow vector with extracts from the new
430 // wide vector.
Sanjay Patelae945e72015-12-24 21:17:56 +0000431 for (User *U : ExtVecOp->users()) {
Sanjay Patela1c53472016-01-05 19:09:47 +0000432 ExtractElementInst *OldExt = dyn_cast<ExtractElementInst>(U);
Sanjay Pateld72a4582016-01-08 01:39:16 +0000433 if (!OldExt || OldExt->getParent() != WideVec->getParent())
Sanjay Patela1c53472016-01-05 19:09:47 +0000434 continue;
435 auto *NewExt = ExtractElementInst::Create(WideVec, OldExt->getOperand(1));
Sven van Haastregt78819e02017-06-05 09:18:10 +0000436 NewExt->insertAfter(OldExt);
Sanjay Patel4b198802016-02-01 22:23:39 +0000437 IC.replaceInstUsesWith(*OldExt, NewExt);
Sanjay Patelae945e72015-12-24 21:17:56 +0000438 }
439}
Tim Northoverfad27612014-03-07 10:24:44 +0000440
441/// We are building a shuffle to create V, which is a sequence of insertelement,
442/// extractelement pairs. If PermittedRHS is set, then we must either use it or
Sanjay Patel70af1fd2014-07-07 22:13:58 +0000443/// not rely on the second vector source. Return a std::pair containing the
Tim Northoverfad27612014-03-07 10:24:44 +0000444/// left and right vectors of the proposed shuffle (or 0), and set the Mask
445/// parameter as required.
446///
447/// Note: we intentionally don't try to fold earlier shuffles since they have
448/// often been chosen carefully to be efficiently implementable on the target.
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +0000449using ShuffleOps = std::pair<Value *, Value *>;
Tim Northoverfad27612014-03-07 10:24:44 +0000450
Sanjay Patel431e1142015-11-17 17:24:08 +0000451static ShuffleOps collectShuffleElements(Value *V,
Tim Northoverfad27612014-03-07 10:24:44 +0000452 SmallVectorImpl<Constant *> &Mask,
Sanjay Patelae945e72015-12-24 21:17:56 +0000453 Value *PermittedRHS,
454 InstCombiner &IC) {
Tim Northoverfad27612014-03-07 10:24:44 +0000455 assert(V->getType()->isVectorTy() && "Invalid shuffle!");
Craig Topper17b55682016-12-29 07:03:18 +0000456 unsigned NumElts = V->getType()->getVectorNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000457
Chris Lattnerec97a902010-01-05 05:36:20 +0000458 if (isa<UndefValue>(V)) {
459 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
Tim Northoverfad27612014-03-07 10:24:44 +0000460 return std::make_pair(
461 PermittedRHS ? UndefValue::get(PermittedRHS->getType()) : V, nullptr);
Chris Lattnera0d01ff2012-01-24 14:31:22 +0000462 }
Craig Topper2ea22b02013-01-18 05:09:16 +0000463
Chris Lattnera0d01ff2012-01-24 14:31:22 +0000464 if (isa<ConstantAggregateZero>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000465 Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0));
Tim Northoverfad27612014-03-07 10:24:44 +0000466 return std::make_pair(V, nullptr);
Chris Lattnera0d01ff2012-01-24 14:31:22 +0000467 }
Craig Topper2ea22b02013-01-18 05:09:16 +0000468
Chris Lattnera0d01ff2012-01-24 14:31:22 +0000469 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000470 // If this is an insert of an extract from some other vector, include it.
471 Value *VecOp = IEI->getOperand(0);
472 Value *ScalarOp = IEI->getOperand(1);
473 Value *IdxOp = IEI->getOperand(2);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000474
Chris Lattnerec97a902010-01-05 05:36:20 +0000475 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
Tim Northoverfad27612014-03-07 10:24:44 +0000476 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp)) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000477 unsigned ExtractedIdx =
Bob Wilson67a6f322010-10-29 22:20:45 +0000478 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Chris Lattnerec97a902010-01-05 05:36:20 +0000479 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000480
Chris Lattnerec97a902010-01-05 05:36:20 +0000481 // Either the extracted from or inserted into vector must be RHSVec,
482 // otherwise we'd end up with a shuffle of three inputs.
Craig Topperf40110f2014-04-25 05:29:35 +0000483 if (EI->getOperand(0) == PermittedRHS || PermittedRHS == nullptr) {
Tim Northoverfad27612014-03-07 10:24:44 +0000484 Value *RHS = EI->getOperand(0);
Sanjay Patelae945e72015-12-24 21:17:56 +0000485 ShuffleOps LR = collectShuffleElements(VecOp, Mask, RHS, IC);
Craig Toppere73658d2014-04-28 04:05:08 +0000486 assert(LR.second == nullptr || LR.second == RHS);
Tim Northoverfad27612014-03-07 10:24:44 +0000487
488 if (LR.first->getType() != RHS->getType()) {
Sanjay Patelae945e72015-12-24 21:17:56 +0000489 // Although we are giving up for now, see if we can create extracts
490 // that match the inserts for another round of combining.
491 replaceExtractElements(IEI, EI, IC);
492
Tim Northoverfad27612014-03-07 10:24:44 +0000493 // We tried our best, but we can't find anything compatible with RHS
494 // further up the chain. Return a trivial shuffle.
495 for (unsigned i = 0; i < NumElts; ++i)
496 Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()), i);
497 return std::make_pair(V, nullptr);
498 }
499
500 unsigned NumLHSElts = RHS->getType()->getVectorNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000501 Mask[InsertedIdx % NumElts] =
Bob Wilson67a6f322010-10-29 22:20:45 +0000502 ConstantInt::get(Type::getInt32Ty(V->getContext()),
Tim Northoverfad27612014-03-07 10:24:44 +0000503 NumLHSElts+ExtractedIdx);
504 return std::make_pair(LR.first, RHS);
Chris Lattnerec97a902010-01-05 05:36:20 +0000505 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000506
Tim Northoverfad27612014-03-07 10:24:44 +0000507 if (VecOp == PermittedRHS) {
508 // We've gone as far as we can: anything on the other side of the
509 // extractelement will already have been converted into a shuffle.
510 unsigned NumLHSElts =
511 EI->getOperand(0)->getType()->getVectorNumElements();
512 for (unsigned i = 0; i != NumElts; ++i)
513 Mask.push_back(ConstantInt::get(
514 Type::getInt32Ty(V->getContext()),
515 i == InsertedIdx ? ExtractedIdx : NumLHSElts + i));
516 return std::make_pair(EI->getOperand(0), PermittedRHS);
Chris Lattnerec97a902010-01-05 05:36:20 +0000517 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000518
Chris Lattnerec97a902010-01-05 05:36:20 +0000519 // If this insertelement is a chain that comes from exactly these two
520 // vectors, return the vector and the effective shuffle.
Tim Northoverfad27612014-03-07 10:24:44 +0000521 if (EI->getOperand(0)->getType() == PermittedRHS->getType() &&
Sanjay Patel431e1142015-11-17 17:24:08 +0000522 collectSingleShuffleElements(IEI, EI->getOperand(0), PermittedRHS,
Tim Northoverfad27612014-03-07 10:24:44 +0000523 Mask))
524 return std::make_pair(EI->getOperand(0), PermittedRHS);
Chris Lattnerec97a902010-01-05 05:36:20 +0000525 }
526 }
527 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000528
Sanjay Patelb67076c2015-11-29 22:09:34 +0000529 // Otherwise, we can't do anything fancy. Return an identity vector.
Chris Lattnerec97a902010-01-05 05:36:20 +0000530 for (unsigned i = 0; i != NumElts; ++i)
531 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
Tim Northoverfad27612014-03-07 10:24:44 +0000532 return std::make_pair(V, nullptr);
Chris Lattnerec97a902010-01-05 05:36:20 +0000533}
534
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000535/// Try to find redundant insertvalue instructions, like the following ones:
536/// %0 = insertvalue { i8, i32 } undef, i8 %x, 0
537/// %1 = insertvalue { i8, i32 } %0, i8 %y, 0
538/// Here the second instruction inserts values at the same indices, as the
539/// first one, making the first one redundant.
540/// It should be transformed to:
541/// %0 = insertvalue { i8, i32 } undef, i8 %y, 0
542Instruction *InstCombiner::visitInsertValueInst(InsertValueInst &I) {
543 bool IsRedundant = false;
544 ArrayRef<unsigned int> FirstIndices = I.getIndices();
545
546 // If there is a chain of insertvalue instructions (each of them except the
547 // last one has only one use and it's another insertvalue insn from this
548 // chain), check if any of the 'children' uses the same indices as the first
549 // instruction. In this case, the first one is redundant.
550 Value *V = &I;
Michael Zolotukhin292d3ca2014-05-08 19:50:24 +0000551 unsigned Depth = 0;
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000552 while (V->hasOneUse() && Depth < 10) {
553 User *U = V->user_back();
Michael Zolotukhin292d3ca2014-05-08 19:50:24 +0000554 auto UserInsInst = dyn_cast<InsertValueInst>(U);
555 if (!UserInsInst || U->getOperand(0) != V)
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000556 break;
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000557 if (UserInsInst->getIndices() == FirstIndices) {
558 IsRedundant = true;
559 break;
560 }
561 V = UserInsInst;
562 Depth++;
563 }
564
565 if (IsRedundant)
Sanjay Patel4b198802016-02-01 22:23:39 +0000566 return replaceInstUsesWith(I, I.getOperand(0));
Michael Zolotukhin7d6293a2014-05-07 14:30:18 +0000567 return nullptr;
568}
569
Sanjay Patel521f19f2016-09-02 17:05:43 +0000570static bool isShuffleEquivalentToSelect(ShuffleVectorInst &Shuf) {
571 int MaskSize = Shuf.getMask()->getType()->getVectorNumElements();
572 int VecSize = Shuf.getOperand(0)->getType()->getVectorNumElements();
573
574 // A vector select does not change the size of the operands.
575 if (MaskSize != VecSize)
576 return false;
577
578 // Each mask element must be undefined or choose a vector element from one of
579 // the source operands without crossing vector lanes.
580 for (int i = 0; i != MaskSize; ++i) {
581 int Elt = Shuf.getMaskValue(i);
582 if (Elt != -1 && Elt != i && Elt != i + VecSize)
583 return false;
584 }
585
586 return true;
587}
588
Michael Kupersteincd7ad712016-12-28 00:18:08 +0000589// Turn a chain of inserts that splats a value into a canonical insert + shuffle
590// splat. That is:
591// insertelt(insertelt(insertelt(insertelt X, %k, 0), %k, 1), %k, 2) ... ->
592// shufflevector(insertelt(X, %k, 0), undef, zero)
593static Instruction *foldInsSequenceIntoBroadcast(InsertElementInst &InsElt) {
594 // We are interested in the last insert in a chain. So, if this insert
595 // has a single user, and that user is an insert, bail.
596 if (InsElt.hasOneUse() && isa<InsertElementInst>(InsElt.user_back()))
597 return nullptr;
598
599 VectorType *VT = cast<VectorType>(InsElt.getType());
600 int NumElements = VT->getNumElements();
601
602 // Do not try to do this for a one-element vector, since that's a nop,
603 // and will cause an inf-loop.
604 if (NumElements == 1)
605 return nullptr;
606
607 Value *SplatVal = InsElt.getOperand(1);
608 InsertElementInst *CurrIE = &InsElt;
609 SmallVector<bool, 16> ElementPresent(NumElements, false);
Florian Hahnb992fee2017-08-30 10:54:21 +0000610 InsertElementInst *FirstIE = nullptr;
Michael Kupersteincd7ad712016-12-28 00:18:08 +0000611
612 // Walk the chain backwards, keeping track of which indices we inserted into,
613 // until we hit something that isn't an insert of the splatted value.
614 while (CurrIE) {
Sanjay Patel863d4942017-11-27 18:19:32 +0000615 auto *Idx = dyn_cast<ConstantInt>(CurrIE->getOperand(2));
Michael Kupersteincd7ad712016-12-28 00:18:08 +0000616 if (!Idx || CurrIE->getOperand(1) != SplatVal)
617 return nullptr;
618
Sanjay Patel863d4942017-11-27 18:19:32 +0000619 auto *NextIE = dyn_cast<InsertElementInst>(CurrIE->getOperand(0));
Florian Hahnb992fee2017-08-30 10:54:21 +0000620 // Check none of the intermediate steps have any additional uses, except
621 // for the root insertelement instruction, which can be re-used, if it
622 // inserts at position 0.
623 if (CurrIE != &InsElt &&
624 (!CurrIE->hasOneUse() && (NextIE != nullptr || !Idx->isZero())))
Michael Kupersteincd7ad712016-12-28 00:18:08 +0000625 return nullptr;
626
627 ElementPresent[Idx->getZExtValue()] = true;
Florian Hahnb992fee2017-08-30 10:54:21 +0000628 FirstIE = CurrIE;
629 CurrIE = NextIE;
Michael Kupersteincd7ad712016-12-28 00:18:08 +0000630 }
631
632 // Make sure we've seen an insert into every element.
633 if (llvm::any_of(ElementPresent, [](bool Present) { return !Present; }))
634 return nullptr;
635
636 // All right, create the insert + shuffle.
Florian Hahnb992fee2017-08-30 10:54:21 +0000637 Instruction *InsertFirst;
638 if (cast<ConstantInt>(FirstIE->getOperand(2))->isZero())
639 InsertFirst = FirstIE;
640 else
641 InsertFirst = InsertElementInst::Create(
642 UndefValue::get(VT), SplatVal,
643 ConstantInt::get(Type::getInt32Ty(InsElt.getContext()), 0),
644 "", &InsElt);
Michael Kupersteincd7ad712016-12-28 00:18:08 +0000645
646 Constant *ZeroMask = ConstantAggregateZero::get(
647 VectorType::get(Type::getInt32Ty(InsElt.getContext()), NumElements));
648
649 return new ShuffleVectorInst(InsertFirst, UndefValue::get(VT), ZeroMask);
650}
651
Sanjay Patel2f602ce2017-03-22 17:10:44 +0000652/// If we have an insertelement instruction feeding into another insertelement
653/// and the 2nd is inserting a constant into the vector, canonicalize that
654/// constant insertion before the insertion of a variable:
655///
656/// insertelement (insertelement X, Y, IdxC1), ScalarC, IdxC2 -->
657/// insertelement (insertelement X, ScalarC, IdxC2), Y, IdxC1
658///
659/// This has the potential of eliminating the 2nd insertelement instruction
660/// via constant folding of the scalar constant into a vector constant.
661static Instruction *hoistInsEltConst(InsertElementInst &InsElt2,
662 InstCombiner::BuilderTy &Builder) {
663 auto *InsElt1 = dyn_cast<InsertElementInst>(InsElt2.getOperand(0));
664 if (!InsElt1 || !InsElt1->hasOneUse())
665 return nullptr;
666
667 Value *X, *Y;
668 Constant *ScalarC;
669 ConstantInt *IdxC1, *IdxC2;
670 if (match(InsElt1->getOperand(0), m_Value(X)) &&
671 match(InsElt1->getOperand(1), m_Value(Y)) && !isa<Constant>(Y) &&
672 match(InsElt1->getOperand(2), m_ConstantInt(IdxC1)) &&
673 match(InsElt2.getOperand(1), m_Constant(ScalarC)) &&
674 match(InsElt2.getOperand(2), m_ConstantInt(IdxC2)) && IdxC1 != IdxC2) {
675 Value *NewInsElt1 = Builder.CreateInsertElement(X, ScalarC, IdxC2);
676 return InsertElementInst::Create(NewInsElt1, Y, IdxC1);
677 }
678
679 return nullptr;
680}
681
Alexey Bataevfee90782016-09-23 09:14:08 +0000682/// insertelt (shufflevector X, CVec, Mask|insertelt X, C1, CIndex1), C, CIndex
683/// --> shufflevector X, CVec', Mask'
Sanjay Patel521f19f2016-09-02 17:05:43 +0000684static Instruction *foldConstantInsEltIntoShuffle(InsertElementInst &InsElt) {
Alexey Bataevfee90782016-09-23 09:14:08 +0000685 auto *Inst = dyn_cast<Instruction>(InsElt.getOperand(0));
686 // Bail out if the parent has more than one use. In that case, we'd be
Sanjay Patel521f19f2016-09-02 17:05:43 +0000687 // replacing the insertelt with a shuffle, and that's not a clear win.
Alexey Bataevfee90782016-09-23 09:14:08 +0000688 if (!Inst || !Inst->hasOneUse())
Sanjay Patel521f19f2016-09-02 17:05:43 +0000689 return nullptr;
Alexey Bataevfee90782016-09-23 09:14:08 +0000690 if (auto *Shuf = dyn_cast<ShuffleVectorInst>(InsElt.getOperand(0))) {
691 // The shuffle must have a constant vector operand. The insertelt must have
692 // a constant scalar being inserted at a constant position in the vector.
693 Constant *ShufConstVec, *InsEltScalar;
694 uint64_t InsEltIndex;
695 if (!match(Shuf->getOperand(1), m_Constant(ShufConstVec)) ||
696 !match(InsElt.getOperand(1), m_Constant(InsEltScalar)) ||
697 !match(InsElt.getOperand(2), m_ConstantInt(InsEltIndex)))
698 return nullptr;
Sanjay Patel521f19f2016-09-02 17:05:43 +0000699
Alexey Bataevfee90782016-09-23 09:14:08 +0000700 // Adding an element to an arbitrary shuffle could be expensive, but a
701 // shuffle that selects elements from vectors without crossing lanes is
702 // assumed cheap.
703 // If we're just adding a constant into that shuffle, it will still be
704 // cheap.
705 if (!isShuffleEquivalentToSelect(*Shuf))
706 return nullptr;
Sanjay Patel521f19f2016-09-02 17:05:43 +0000707
Alexey Bataevfee90782016-09-23 09:14:08 +0000708 // From the above 'select' check, we know that the mask has the same number
709 // of elements as the vector input operands. We also know that each constant
710 // input element is used in its lane and can not be used more than once by
711 // the shuffle. Therefore, replace the constant in the shuffle's constant
712 // vector with the insertelt constant. Replace the constant in the shuffle's
713 // mask vector with the insertelt index plus the length of the vector
714 // (because the constant vector operand of a shuffle is always the 2nd
715 // operand).
716 Constant *Mask = Shuf->getMask();
717 unsigned NumElts = Mask->getType()->getVectorNumElements();
718 SmallVector<Constant *, 16> NewShufElts(NumElts);
719 SmallVector<Constant *, 16> NewMaskElts(NumElts);
720 for (unsigned I = 0; I != NumElts; ++I) {
721 if (I == InsEltIndex) {
722 NewShufElts[I] = InsEltScalar;
723 Type *Int32Ty = Type::getInt32Ty(Shuf->getContext());
724 NewMaskElts[I] = ConstantInt::get(Int32Ty, InsEltIndex + NumElts);
725 } else {
726 // Copy over the existing values.
727 NewShufElts[I] = ShufConstVec->getAggregateElement(I);
728 NewMaskElts[I] = Mask->getAggregateElement(I);
729 }
Sanjay Patel521f19f2016-09-02 17:05:43 +0000730 }
Sanjay Patel521f19f2016-09-02 17:05:43 +0000731
Alexey Bataevfee90782016-09-23 09:14:08 +0000732 // Create new operands for a shuffle that includes the constant of the
733 // original insertelt. The old shuffle will be dead now.
734 return new ShuffleVectorInst(Shuf->getOperand(0),
735 ConstantVector::get(NewShufElts),
736 ConstantVector::get(NewMaskElts));
737 } else if (auto *IEI = dyn_cast<InsertElementInst>(Inst)) {
738 // Transform sequences of insertelements ops with constant data/indexes into
739 // a single shuffle op.
740 unsigned NumElts = InsElt.getType()->getNumElements();
741
742 uint64_t InsertIdx[2];
743 Constant *Val[2];
744 if (!match(InsElt.getOperand(2), m_ConstantInt(InsertIdx[0])) ||
745 !match(InsElt.getOperand(1), m_Constant(Val[0])) ||
746 !match(IEI->getOperand(2), m_ConstantInt(InsertIdx[1])) ||
747 !match(IEI->getOperand(1), m_Constant(Val[1])))
748 return nullptr;
749 SmallVector<Constant *, 16> Values(NumElts);
750 SmallVector<Constant *, 16> Mask(NumElts);
751 auto ValI = std::begin(Val);
752 // Generate new constant vector and mask.
753 // We have 2 values/masks from the insertelements instructions. Insert them
754 // into new value/mask vectors.
755 for (uint64_t I : InsertIdx) {
756 if (!Values[I]) {
757 assert(!Mask[I]);
758 Values[I] = *ValI;
759 Mask[I] = ConstantInt::get(Type::getInt32Ty(InsElt.getContext()),
760 NumElts + I);
761 }
762 ++ValI;
763 }
764 // Remaining values are filled with 'undef' values.
765 for (unsigned I = 0; I < NumElts; ++I) {
766 if (!Values[I]) {
767 assert(!Mask[I]);
768 Values[I] = UndefValue::get(InsElt.getType()->getElementType());
769 Mask[I] = ConstantInt::get(Type::getInt32Ty(InsElt.getContext()), I);
770 }
771 }
772 // Create new operands for a shuffle that includes the constant of the
773 // original insertelt.
774 return new ShuffleVectorInst(IEI->getOperand(0),
775 ConstantVector::get(Values),
776 ConstantVector::get(Mask));
777 }
778 return nullptr;
Sanjay Patel521f19f2016-09-02 17:05:43 +0000779}
780
Chris Lattnerec97a902010-01-05 05:36:20 +0000781Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
782 Value *VecOp = IE.getOperand(0);
783 Value *ScalarOp = IE.getOperand(1);
784 Value *IdxOp = IE.getOperand(2);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000785
Igor Laevskye0edb662017-12-13 11:21:18 +0000786 if (auto *V = SimplifyInsertElementInst(
787 VecOp, ScalarOp, IdxOp, SQ.getWithInstruction(&IE)))
788 return replaceInstUsesWith(IE, V);
789
Chris Lattnerec97a902010-01-05 05:36:20 +0000790 // Inserting an undef or into an undefined place, remove this.
791 if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
Sanjay Patel4b198802016-02-01 22:23:39 +0000792 replaceInstUsesWith(IE, VecOp);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000793
794 // If the inserted element was extracted from some other vector, and if the
Chris Lattnerec97a902010-01-05 05:36:20 +0000795 // indexes are constant, try to turn this into a shufflevector operation.
796 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
Tim Northoverfad27612014-03-07 10:24:44 +0000797 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp)) {
798 unsigned NumInsertVectorElts = IE.getType()->getNumElements();
799 unsigned NumExtractVectorElts =
800 EI->getOperand(0)->getType()->getVectorNumElements();
Chris Lattnerec97a902010-01-05 05:36:20 +0000801 unsigned ExtractedIdx =
Bob Wilson67a6f322010-10-29 22:20:45 +0000802 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Chris Lattnerec97a902010-01-05 05:36:20 +0000803 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000804
Tim Northoverfad27612014-03-07 10:24:44 +0000805 if (ExtractedIdx >= NumExtractVectorElts) // Out of range extract.
Sanjay Patel4b198802016-02-01 22:23:39 +0000806 return replaceInstUsesWith(IE, VecOp);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000807
Tim Northoverfad27612014-03-07 10:24:44 +0000808 if (InsertedIdx >= NumInsertVectorElts) // Out of range insert.
Sanjay Patel4b198802016-02-01 22:23:39 +0000809 return replaceInstUsesWith(IE, UndefValue::get(IE.getType()));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000810
Chris Lattnerec97a902010-01-05 05:36:20 +0000811 // If we are extracting a value from a vector, then inserting it right
812 // back into the same place, just use the input vector.
813 if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
Sanjay Patel4b198802016-02-01 22:23:39 +0000814 return replaceInstUsesWith(IE, VecOp);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000815
Chris Lattnerec97a902010-01-05 05:36:20 +0000816 // If this insertelement isn't used by some other insertelement, turn it
817 // (and any insertelements it points to), into one big shuffle.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000818 if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.user_back())) {
Chris Lattner0256be92012-01-27 03:08:05 +0000819 SmallVector<Constant*, 16> Mask;
Sanjay Patelae945e72015-12-24 21:17:56 +0000820 ShuffleOps LR = collectShuffleElements(&IE, Mask, nullptr, *this);
Tim Northoverfad27612014-03-07 10:24:44 +0000821
822 // The proposed shuffle may be trivial, in which case we shouldn't
823 // perform the combine.
824 if (LR.first != &IE && LR.second != &IE) {
825 // We now have a shuffle of LHS, RHS, Mask.
Craig Topperf40110f2014-04-25 05:29:35 +0000826 if (LR.second == nullptr)
827 LR.second = UndefValue::get(LR.first->getType());
Tim Northoverfad27612014-03-07 10:24:44 +0000828 return new ShuffleVectorInst(LR.first, LR.second,
829 ConstantVector::get(Mask));
830 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000831 }
832 }
833 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000834
Craig Topper17b55682016-12-29 07:03:18 +0000835 unsigned VWidth = VecOp->getType()->getVectorNumElements();
Chris Lattnerec97a902010-01-05 05:36:20 +0000836 APInt UndefElts(VWidth, 0);
837 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
Eli Friedmanef200db2011-02-19 22:42:40 +0000838 if (Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) {
839 if (V != &IE)
Sanjay Patel4b198802016-02-01 22:23:39 +0000840 return replaceInstUsesWith(IE, V);
Chris Lattnerec97a902010-01-05 05:36:20 +0000841 return &IE;
Eli Friedmanef200db2011-02-19 22:42:40 +0000842 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000843
Sanjay Patel521f19f2016-09-02 17:05:43 +0000844 if (Instruction *Shuf = foldConstantInsEltIntoShuffle(IE))
845 return Shuf;
846
Craig Topperbb4069e2017-07-07 23:16:26 +0000847 if (Instruction *NewInsElt = hoistInsEltConst(IE, Builder))
Sanjay Patel2f602ce2017-03-22 17:10:44 +0000848 return NewInsElt;
849
Michael Kupersteincd7ad712016-12-28 00:18:08 +0000850 // Turn a sequence of inserts that broadcasts a scalar into a single
851 // insert + shufflevector.
852 if (Instruction *Broadcast = foldInsSequenceIntoBroadcast(IE))
853 return Broadcast;
854
Craig Topperf40110f2014-04-25 05:29:35 +0000855 return nullptr;
Chris Lattnerec97a902010-01-05 05:36:20 +0000856}
857
Nick Lewyckya2b77202013-05-31 00:59:42 +0000858/// Return true if we can evaluate the specified expression tree if the vector
859/// elements were shuffled in a different order.
860static bool CanEvaluateShuffled(Value *V, ArrayRef<int> Mask,
Nick Lewycky3f715e22013-06-01 20:51:31 +0000861 unsigned Depth = 5) {
Nick Lewyckya2b77202013-05-31 00:59:42 +0000862 // We can always reorder the elements of a constant.
863 if (isa<Constant>(V))
864 return true;
865
866 // We won't reorder vector arguments. No IPO here.
867 Instruction *I = dyn_cast<Instruction>(V);
868 if (!I) return false;
869
870 // Two users may expect different orders of the elements. Don't try it.
871 if (!I->hasOneUse())
872 return false;
873
874 if (Depth == 0) return false;
875
876 switch (I->getOpcode()) {
877 case Instruction::Add:
878 case Instruction::FAdd:
879 case Instruction::Sub:
880 case Instruction::FSub:
881 case Instruction::Mul:
882 case Instruction::FMul:
883 case Instruction::UDiv:
884 case Instruction::SDiv:
885 case Instruction::FDiv:
886 case Instruction::URem:
887 case Instruction::SRem:
888 case Instruction::FRem:
889 case Instruction::Shl:
890 case Instruction::LShr:
891 case Instruction::AShr:
892 case Instruction::And:
893 case Instruction::Or:
894 case Instruction::Xor:
895 case Instruction::ICmp:
896 case Instruction::FCmp:
897 case Instruction::Trunc:
898 case Instruction::ZExt:
899 case Instruction::SExt:
900 case Instruction::FPToUI:
901 case Instruction::FPToSI:
902 case Instruction::UIToFP:
903 case Instruction::SIToFP:
904 case Instruction::FPTrunc:
905 case Instruction::FPExt:
906 case Instruction::GetElementPtr: {
Sanjay Patel4e28753142015-11-16 22:16:52 +0000907 for (Value *Operand : I->operands()) {
908 if (!CanEvaluateShuffled(Operand, Mask, Depth-1))
Nick Lewyckya2b77202013-05-31 00:59:42 +0000909 return false;
910 }
911 return true;
912 }
913 case Instruction::InsertElement: {
914 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(2));
915 if (!CI) return false;
916 int ElementNumber = CI->getLimitedValue();
917
918 // Verify that 'CI' does not occur twice in Mask. A single 'insertelement'
919 // can't put an element into multiple indices.
920 bool SeenOnce = false;
921 for (int i = 0, e = Mask.size(); i != e; ++i) {
922 if (Mask[i] == ElementNumber) {
923 if (SeenOnce)
924 return false;
925 SeenOnce = true;
926 }
927 }
928 return CanEvaluateShuffled(I->getOperand(0), Mask, Depth-1);
929 }
930 }
931 return false;
932}
933
934/// Rebuild a new instruction just like 'I' but with the new operands given.
935/// In the event of type mismatch, the type of the operands is correct.
Sanjay Patel431e1142015-11-17 17:24:08 +0000936static Value *buildNew(Instruction *I, ArrayRef<Value*> NewOps) {
Nick Lewyckya2b77202013-05-31 00:59:42 +0000937 // We don't want to use the IRBuilder here because we want the replacement
938 // instructions to appear next to 'I', not the builder's insertion point.
939 switch (I->getOpcode()) {
940 case Instruction::Add:
941 case Instruction::FAdd:
942 case Instruction::Sub:
943 case Instruction::FSub:
944 case Instruction::Mul:
945 case Instruction::FMul:
946 case Instruction::UDiv:
947 case Instruction::SDiv:
948 case Instruction::FDiv:
949 case Instruction::URem:
950 case Instruction::SRem:
951 case Instruction::FRem:
952 case Instruction::Shl:
953 case Instruction::LShr:
954 case Instruction::AShr:
955 case Instruction::And:
956 case Instruction::Or:
957 case Instruction::Xor: {
958 BinaryOperator *BO = cast<BinaryOperator>(I);
959 assert(NewOps.size() == 2 && "binary operator with #ops != 2");
960 BinaryOperator *New =
961 BinaryOperator::Create(cast<BinaryOperator>(I)->getOpcode(),
962 NewOps[0], NewOps[1], "", BO);
963 if (isa<OverflowingBinaryOperator>(BO)) {
964 New->setHasNoUnsignedWrap(BO->hasNoUnsignedWrap());
965 New->setHasNoSignedWrap(BO->hasNoSignedWrap());
966 }
967 if (isa<PossiblyExactOperator>(BO)) {
968 New->setIsExact(BO->isExact());
969 }
Owen Anderson48b842e2014-01-18 00:48:14 +0000970 if (isa<FPMathOperator>(BO))
971 New->copyFastMathFlags(I);
Nick Lewyckya2b77202013-05-31 00:59:42 +0000972 return New;
973 }
974 case Instruction::ICmp:
975 assert(NewOps.size() == 2 && "icmp with #ops != 2");
976 return new ICmpInst(I, cast<ICmpInst>(I)->getPredicate(),
977 NewOps[0], NewOps[1]);
978 case Instruction::FCmp:
979 assert(NewOps.size() == 2 && "fcmp with #ops != 2");
980 return new FCmpInst(I, cast<FCmpInst>(I)->getPredicate(),
981 NewOps[0], NewOps[1]);
982 case Instruction::Trunc:
983 case Instruction::ZExt:
984 case Instruction::SExt:
985 case Instruction::FPToUI:
986 case Instruction::FPToSI:
987 case Instruction::UIToFP:
988 case Instruction::SIToFP:
989 case Instruction::FPTrunc:
990 case Instruction::FPExt: {
991 // It's possible that the mask has a different number of elements from
992 // the original cast. We recompute the destination type to match the mask.
993 Type *DestTy =
994 VectorType::get(I->getType()->getScalarType(),
995 NewOps[0]->getType()->getVectorNumElements());
996 assert(NewOps.size() == 1 && "cast with #ops != 1");
997 return CastInst::Create(cast<CastInst>(I)->getOpcode(), NewOps[0], DestTy,
998 "", I);
999 }
1000 case Instruction::GetElementPtr: {
1001 Value *Ptr = NewOps[0];
1002 ArrayRef<Value*> Idx = NewOps.slice(1);
David Blaikie22319eb2015-03-14 19:24:04 +00001003 GetElementPtrInst *GEP = GetElementPtrInst::Create(
1004 cast<GetElementPtrInst>(I)->getSourceElementType(), Ptr, Idx, "", I);
Nick Lewyckya2b77202013-05-31 00:59:42 +00001005 GEP->setIsInBounds(cast<GetElementPtrInst>(I)->isInBounds());
1006 return GEP;
1007 }
1008 }
1009 llvm_unreachable("failed to rebuild vector instructions");
1010}
1011
1012Value *
1013InstCombiner::EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask) {
1014 // Mask.size() does not need to be equal to the number of vector elements.
1015
1016 assert(V->getType()->isVectorTy() && "can't reorder non-vector elements");
Sanjay Patelce36b032017-10-09 17:54:46 +00001017 Type *EltTy = V->getType()->getScalarType();
1018 if (isa<UndefValue>(V))
1019 return UndefValue::get(VectorType::get(EltTy, Mask.size()));
1020
1021 if (isa<ConstantAggregateZero>(V))
1022 return ConstantAggregateZero::get(VectorType::get(EltTy, Mask.size()));
1023
Nick Lewyckya2b77202013-05-31 00:59:42 +00001024 if (Constant *C = dyn_cast<Constant>(V)) {
1025 SmallVector<Constant *, 16> MaskValues;
1026 for (int i = 0, e = Mask.size(); i != e; ++i) {
1027 if (Mask[i] == -1)
Craig Topperbb4069e2017-07-07 23:16:26 +00001028 MaskValues.push_back(UndefValue::get(Builder.getInt32Ty()));
Nick Lewyckya2b77202013-05-31 00:59:42 +00001029 else
Craig Topperbb4069e2017-07-07 23:16:26 +00001030 MaskValues.push_back(Builder.getInt32(Mask[i]));
Nick Lewyckya2b77202013-05-31 00:59:42 +00001031 }
1032 return ConstantExpr::getShuffleVector(C, UndefValue::get(C->getType()),
1033 ConstantVector::get(MaskValues));
1034 }
1035
1036 Instruction *I = cast<Instruction>(V);
1037 switch (I->getOpcode()) {
1038 case Instruction::Add:
1039 case Instruction::FAdd:
1040 case Instruction::Sub:
1041 case Instruction::FSub:
1042 case Instruction::Mul:
1043 case Instruction::FMul:
1044 case Instruction::UDiv:
1045 case Instruction::SDiv:
1046 case Instruction::FDiv:
1047 case Instruction::URem:
1048 case Instruction::SRem:
1049 case Instruction::FRem:
1050 case Instruction::Shl:
1051 case Instruction::LShr:
1052 case Instruction::AShr:
1053 case Instruction::And:
1054 case Instruction::Or:
1055 case Instruction::Xor:
1056 case Instruction::ICmp:
1057 case Instruction::FCmp:
1058 case Instruction::Trunc:
1059 case Instruction::ZExt:
1060 case Instruction::SExt:
1061 case Instruction::FPToUI:
1062 case Instruction::FPToSI:
1063 case Instruction::UIToFP:
1064 case Instruction::SIToFP:
1065 case Instruction::FPTrunc:
1066 case Instruction::FPExt:
1067 case Instruction::Select:
1068 case Instruction::GetElementPtr: {
1069 SmallVector<Value*, 8> NewOps;
1070 bool NeedsRebuild = (Mask.size() != I->getType()->getVectorNumElements());
1071 for (int i = 0, e = I->getNumOperands(); i != e; ++i) {
1072 Value *V = EvaluateInDifferentElementOrder(I->getOperand(i), Mask);
1073 NewOps.push_back(V);
1074 NeedsRebuild |= (V != I->getOperand(i));
1075 }
1076 if (NeedsRebuild) {
Sanjay Patel431e1142015-11-17 17:24:08 +00001077 return buildNew(I, NewOps);
Nick Lewyckya2b77202013-05-31 00:59:42 +00001078 }
1079 return I;
1080 }
1081 case Instruction::InsertElement: {
1082 int Element = cast<ConstantInt>(I->getOperand(2))->getLimitedValue();
Nick Lewyckya2b77202013-05-31 00:59:42 +00001083
1084 // The insertelement was inserting at Element. Figure out which element
1085 // that becomes after shuffling. The answer is guaranteed to be unique
1086 // by CanEvaluateShuffled.
Nick Lewycky3f715e22013-06-01 20:51:31 +00001087 bool Found = false;
Nick Lewyckya2b77202013-05-31 00:59:42 +00001088 int Index = 0;
Nick Lewycky3f715e22013-06-01 20:51:31 +00001089 for (int e = Mask.size(); Index != e; ++Index) {
1090 if (Mask[Index] == Element) {
1091 Found = true;
Nick Lewyckya2b77202013-05-31 00:59:42 +00001092 break;
Nick Lewycky3f715e22013-06-01 20:51:31 +00001093 }
1094 }
Nick Lewyckya2b77202013-05-31 00:59:42 +00001095
Hao Liu26abebb2014-01-08 03:06:15 +00001096 // If element is not in Mask, no need to handle the operand 1 (element to
1097 // be inserted). Just evaluate values in operand 0 according to Mask.
Nick Lewycky3f715e22013-06-01 20:51:31 +00001098 if (!Found)
Hao Liu26abebb2014-01-08 03:06:15 +00001099 return EvaluateInDifferentElementOrder(I->getOperand(0), Mask);
Joey Goulya3250f22013-07-12 23:08:06 +00001100
Nick Lewyckya2b77202013-05-31 00:59:42 +00001101 Value *V = EvaluateInDifferentElementOrder(I->getOperand(0), Mask);
1102 return InsertElementInst::Create(V, I->getOperand(1),
Craig Topperbb4069e2017-07-07 23:16:26 +00001103 Builder.getInt32(Index), "", I);
Nick Lewyckya2b77202013-05-31 00:59:42 +00001104 }
1105 }
1106 llvm_unreachable("failed to reorder elements of vector instruction!");
1107}
Chris Lattnerec97a902010-01-05 05:36:20 +00001108
Sanjay Patel431e1142015-11-17 17:24:08 +00001109static void recognizeIdentityMask(const SmallVectorImpl<int> &Mask,
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001110 bool &isLHSID, bool &isRHSID) {
1111 isLHSID = isRHSID = true;
1112
1113 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
1114 if (Mask[i] < 0) continue; // Ignore undef values.
1115 // Is this an identity shuffle of the LHS value?
1116 isLHSID &= (Mask[i] == (int)i);
1117
1118 // Is this an identity shuffle of the RHS value?
1119 isRHSID &= (Mask[i]-e == i);
1120 }
1121}
1122
JF Bastiend52c9902015-02-25 22:30:51 +00001123// Returns true if the shuffle is extracting a contiguous range of values from
1124// LHS, for example:
1125// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
1126// Input: |AA|BB|CC|DD|EE|FF|GG|HH|II|JJ|KK|LL|MM|NN|OO|PP|
1127// Shuffles to: |EE|FF|GG|HH|
1128// +--+--+--+--+
1129static bool isShuffleExtractingFromLHS(ShuffleVectorInst &SVI,
1130 SmallVector<int, 16> &Mask) {
Craig Topper17b55682016-12-29 07:03:18 +00001131 unsigned LHSElems = SVI.getOperand(0)->getType()->getVectorNumElements();
JF Bastiend52c9902015-02-25 22:30:51 +00001132 unsigned MaskElems = Mask.size();
1133 unsigned BegIdx = Mask.front();
1134 unsigned EndIdx = Mask.back();
1135 if (BegIdx > EndIdx || EndIdx >= LHSElems || EndIdx - BegIdx != MaskElems - 1)
1136 return false;
1137 for (unsigned I = 0; I != MaskElems; ++I)
1138 if (static_cast<unsigned>(Mask[I]) != BegIdx + I)
1139 return false;
1140 return true;
1141}
1142
Sanjay Patelda667532018-06-29 13:44:06 +00001143static Instruction *foldSelectShuffle(ShuffleVectorInst &Shuf,
1144 InstCombiner::BuilderTy &Builder) {
Sanjay Patela76b7002018-06-21 20:15:09 +00001145 // Folds under here require the equivalent of a vector select.
1146 if (!Shuf.isSelect())
1147 return nullptr;
1148
1149 BinaryOperator *B0, *B1;
1150 if (!match(Shuf.getOperand(0), m_BinOp(B0)) ||
1151 !match(Shuf.getOperand(1), m_BinOp(B1)))
1152 return nullptr;
1153
Sanjay Patelda667532018-06-29 13:44:06 +00001154 Value *X, *Y;
Sanjay Patela76b7002018-06-21 20:15:09 +00001155 Constant *C0, *C1;
Sanjay Patela52963b2018-06-22 12:46:16 +00001156 bool ConstantsAreOp1;
1157 if (match(B0, m_BinOp(m_Value(X), m_Constant(C0))) &&
Sanjay Patelda667532018-06-29 13:44:06 +00001158 match(B1, m_BinOp(m_Value(Y), m_Constant(C1))))
Sanjay Patela52963b2018-06-22 12:46:16 +00001159 ConstantsAreOp1 = true;
1160 else if (match(B0, m_BinOp(m_Constant(C0), m_Value(X))) &&
Sanjay Patelda667532018-06-29 13:44:06 +00001161 match(B1, m_BinOp(m_Constant(C1), m_Value(Y))))
Sanjay Patela52963b2018-06-22 12:46:16 +00001162 ConstantsAreOp1 = false;
1163 else
Sanjay Patela76b7002018-06-21 20:15:09 +00001164 return nullptr;
1165
Sanjay Patel57bda362018-06-28 17:48:04 +00001166 // We need matching binops to fold the lanes together.
1167 BinaryOperator::BinaryOps Opc0 = B0->getOpcode();
1168 BinaryOperator::BinaryOps Opc1 = B1->getOpcode();
1169 bool DropNSW = false;
1170 if (ConstantsAreOp1 && Opc0 != Opc1) {
1171 // If we have multiply and shift-left-by-constant, convert the shift:
1172 // shl X, C --> mul X, 1 << C
1173 // TODO: We drop "nsw" if shift is converted into multiply because it may
1174 // not be correct when the shift amount is BitWidth - 1. We could examine
1175 // each vector element to determine if it is safe to keep that flag.
1176 if (Opc0 == Instruction::Mul && Opc1 == Instruction::Shl) {
1177 C1 = ConstantExpr::getShl(ConstantInt::get(C1->getType(), 1), C1);
1178 Opc1 = Instruction::Mul;
1179 DropNSW = true;
1180 } else if (Opc0 == Instruction::Shl && Opc1 == Instruction::Mul) {
1181 C0 = ConstantExpr::getShl(ConstantInt::get(C0->getType(), 1), C0);
1182 Opc0 = Instruction::Mul;
1183 DropNSW = true;
1184 }
1185 }
1186
1187 if (Opc0 != Opc1)
Sanjay Patel4784e152018-06-21 23:56:59 +00001188 return nullptr;
1189
Sanjay Patel57bda362018-06-28 17:48:04 +00001190 // The opcodes must be the same. Use a new name to make that clear.
1191 BinaryOperator::BinaryOps BOpc = Opc0;
1192
Sanjay Patelda667532018-06-29 13:44:06 +00001193 Value *V;
1194 if (X == Y) {
1195 // Remove a binop and the shuffle by rearranging the constant:
1196 // shuffle (op V, C0), (op V, C1), M --> op V, C'
1197 // shuffle (op C0, V), (op C1, V), M --> op C', V
1198 V = X;
1199 } else if (!Instruction::isIntDivRem(BOpc) &&
1200 (B0->hasOneUse() || B1->hasOneUse())) {
1201 // If there are 2 different variable operands, we must create a new shuffle
1202 // (select) first, so check uses to ensure that we don't end up with more
1203 // instructions than we started with.
1204 //
1205 // Note: In general, we do not create new shuffles in InstCombine because we
1206 // do not know if a target can lower an arbitrary shuffle optimally. In this
1207 // case, the shuffle uses the existing mask, so there is no additional risk.
1208 //
1209 // TODO: We are disallowing div/rem because a shuffle with an undef mask
1210 // element would propagate an undef value to the div/rem. That's not
1211 // safe in general because div/rem allow for undefined behavior. We can
1212 // loosen this restriction (eg, check if the mask has no undefs or replace
1213 // undef elements).
1214
1215 // Select the variable vectors first, then perform the binop:
1216 // shuffle (op X, C0), (op Y, C1), M --> op (shuffle X, Y, M), C'
1217 // shuffle (op C0, X), (op C1, Y), M --> op C', (shuffle X, Y, M)
1218 V = Builder.CreateShuffleVector(X, Y, Shuf.getMask());
1219 } else {
1220 return nullptr;
1221 }
1222
Sanjay Patela76b7002018-06-21 20:15:09 +00001223 Constant *NewC = ConstantExpr::getShuffleVector(C0, C1, Shuf.getMask());
1224
1225 // If the shuffle mask contains undef elements, then the new constant
1226 // vector will have undefs in those lanes. This could cause the entire
1227 // binop to be undef.
Sanjay Pateld5128532018-06-28 20:52:43 +00001228 if (Instruction::isIntDivRem(BOpc))
Sanjay Patela76b7002018-06-21 20:15:09 +00001229 NewC = getSafeVectorConstantForIntDivRem(NewC);
1230
Sanjay Patelda667532018-06-29 13:44:06 +00001231 Instruction *NewBO = ConstantsAreOp1 ? BinaryOperator::Create(BOpc, V, NewC) :
1232 BinaryOperator::Create(BOpc, NewC, V);
Sanjay Patela76b7002018-06-21 20:15:09 +00001233
1234 // Flags are intersected from the 2 source binops.
1235 NewBO->copyIRFlags(B0);
1236 NewBO->andIRFlags(B1);
Sanjay Patel57bda362018-06-28 17:48:04 +00001237 if (DropNSW)
1238 NewBO->setHasNoSignedWrap(false);
Sanjay Patela76b7002018-06-21 20:15:09 +00001239 return NewBO;
1240}
1241
Chris Lattnerec97a902010-01-05 05:36:20 +00001242Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
1243 Value *LHS = SVI.getOperand(0);
1244 Value *RHS = SVI.getOperand(1);
Chris Lattner8326bd82012-01-26 00:42:34 +00001245 SmallVector<int, 16> Mask = SVI.getShuffleMask();
JF Bastiend52c9902015-02-25 22:30:51 +00001246 Type *Int32Ty = Type::getInt32Ty(SVI.getContext());
Bob Wilson8ecf98b2010-10-29 22:20:43 +00001247
Craig Toppera4205622017-06-09 03:21:29 +00001248 if (auto *V = SimplifyShuffleVectorInst(
1249 LHS, RHS, SVI.getMask(), SVI.getType(), SQ.getWithInstruction(&SVI)))
Zvi Rackover82bf48d2017-04-04 04:47:57 +00001250 return replaceInstUsesWith(SVI, V);
1251
Sanjay Patelda667532018-06-29 13:44:06 +00001252 if (Instruction *I = foldSelectShuffle(SVI, Builder))
Sanjay Patela76b7002018-06-21 20:15:09 +00001253 return I;
1254
Chris Lattnerec97a902010-01-05 05:36:20 +00001255 bool MadeChange = false;
Craig Topper17b55682016-12-29 07:03:18 +00001256 unsigned VWidth = SVI.getType()->getVectorNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +00001257
Chris Lattnerec97a902010-01-05 05:36:20 +00001258 APInt UndefElts(VWidth, 0);
1259 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
Eli Friedmanef200db2011-02-19 22:42:40 +00001260 if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
1261 if (V != &SVI)
Sanjay Patel4b198802016-02-01 22:23:39 +00001262 return replaceInstUsesWith(SVI, V);
Sanjay Patele6b48a12017-08-31 15:57:17 +00001263 return &SVI;
Chris Lattnerec97a902010-01-05 05:36:20 +00001264 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +00001265
Craig Topper17b55682016-12-29 07:03:18 +00001266 unsigned LHSWidth = LHS->getType()->getVectorNumElements();
Eli Friedmance818272011-10-21 19:06:29 +00001267
Chris Lattnerec97a902010-01-05 05:36:20 +00001268 // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask')
1269 // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
1270 if (LHS == RHS || isa<UndefValue>(LHS)) {
Eric Christopher51edc7b2010-08-17 22:55:27 +00001271 if (isa<UndefValue>(LHS) && LHS == RHS) {
1272 // shuffle(undef,undef,mask) -> undef.
Nick Lewyckya2b77202013-05-31 00:59:42 +00001273 Value *Result = (VWidth == LHSWidth)
Eli Friedmance818272011-10-21 19:06:29 +00001274 ? LHS : UndefValue::get(SVI.getType());
Sanjay Patel4b198802016-02-01 22:23:39 +00001275 return replaceInstUsesWith(SVI, Result);
Eric Christopher51edc7b2010-08-17 22:55:27 +00001276 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +00001277
Chris Lattnerec97a902010-01-05 05:36:20 +00001278 // Remap any references to RHS to use LHS.
Chris Lattner0256be92012-01-27 03:08:05 +00001279 SmallVector<Constant*, 16> Elts;
Eli Friedmance818272011-10-21 19:06:29 +00001280 for (unsigned i = 0, e = LHSWidth; i != VWidth; ++i) {
Chris Lattner0256be92012-01-27 03:08:05 +00001281 if (Mask[i] < 0) {
JF Bastiend52c9902015-02-25 22:30:51 +00001282 Elts.push_back(UndefValue::get(Int32Ty));
Chris Lattner0256be92012-01-27 03:08:05 +00001283 continue;
1284 }
1285
1286 if ((Mask[i] >= (int)e && isa<UndefValue>(RHS)) ||
1287 (Mask[i] < (int)e && isa<UndefValue>(LHS))) {
1288 Mask[i] = -1; // Turn into undef.
JF Bastiend52c9902015-02-25 22:30:51 +00001289 Elts.push_back(UndefValue::get(Int32Ty));
Chris Lattner0256be92012-01-27 03:08:05 +00001290 } else {
1291 Mask[i] = Mask[i] % e; // Force to LHS.
JF Bastiend52c9902015-02-25 22:30:51 +00001292 Elts.push_back(ConstantInt::get(Int32Ty, Mask[i]));
Chris Lattnerec97a902010-01-05 05:36:20 +00001293 }
1294 }
1295 SVI.setOperand(0, SVI.getOperand(1));
1296 SVI.setOperand(1, UndefValue::get(RHS->getType()));
1297 SVI.setOperand(2, ConstantVector::get(Elts));
1298 LHS = SVI.getOperand(0);
1299 RHS = SVI.getOperand(1);
1300 MadeChange = true;
1301 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +00001302
Eli Friedmance818272011-10-21 19:06:29 +00001303 if (VWidth == LHSWidth) {
1304 // Analyze the shuffle, are the LHS or RHS and identity shuffles?
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001305 bool isLHSID, isRHSID;
Sanjay Patel431e1142015-11-17 17:24:08 +00001306 recognizeIdentityMask(Mask, isLHSID, isRHSID);
Eli Friedmance818272011-10-21 19:06:29 +00001307
1308 // Eliminate identity shuffles.
Sanjay Patel4b198802016-02-01 22:23:39 +00001309 if (isLHSID) return replaceInstUsesWith(SVI, LHS);
1310 if (isRHSID) return replaceInstUsesWith(SVI, RHS);
Eric Christopher51edc7b2010-08-17 22:55:27 +00001311 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +00001312
Nick Lewycky688d6682013-06-03 23:15:20 +00001313 if (isa<UndefValue>(RHS) && CanEvaluateShuffled(LHS, Mask)) {
Nick Lewyckya2b77202013-05-31 00:59:42 +00001314 Value *V = EvaluateInDifferentElementOrder(LHS, Mask);
Sanjay Patel4b198802016-02-01 22:23:39 +00001315 return replaceInstUsesWith(SVI, V);
Nick Lewyckya2b77202013-05-31 00:59:42 +00001316 }
1317
JF Bastiend52c9902015-02-25 22:30:51 +00001318 // SROA generates shuffle+bitcast when the extracted sub-vector is bitcast to
1319 // a non-vector type. We can instead bitcast the original vector followed by
1320 // an extract of the desired element:
1321 //
1322 // %sroa = shufflevector <16 x i8> %in, <16 x i8> undef,
1323 // <4 x i32> <i32 0, i32 1, i32 2, i32 3>
1324 // %1 = bitcast <4 x i8> %sroa to i32
1325 // Becomes:
1326 // %bc = bitcast <16 x i8> %in to <4 x i32>
1327 // %ext = extractelement <4 x i32> %bc, i32 0
1328 //
1329 // If the shuffle is extracting a contiguous range of values from the input
1330 // vector then each use which is a bitcast of the extracted size can be
1331 // replaced. This will work if the vector types are compatible, and the begin
1332 // index is aligned to a value in the casted vector type. If the begin index
1333 // isn't aligned then we can shuffle the original vector (keeping the same
1334 // vector type) before extracting.
1335 //
1336 // This code will bail out if the target type is fundamentally incompatible
1337 // with vectors of the source type.
1338 //
1339 // Example of <16 x i8>, target type i32:
1340 // Index range [4,8): v-----------v Will work.
1341 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
1342 // <16 x i8>: | | | | | | | | | | | | | | | | |
1343 // <4 x i32>: | | | | |
1344 // +-----------+-----------+-----------+-----------+
1345 // Index range [6,10): ^-----------^ Needs an extra shuffle.
1346 // Target type i40: ^--------------^ Won't work, bail.
1347 if (isShuffleExtractingFromLHS(SVI, Mask)) {
1348 Value *V = LHS;
1349 unsigned MaskElems = Mask.size();
JF Bastiend52c9902015-02-25 22:30:51 +00001350 VectorType *SrcTy = cast<VectorType>(V->getType());
1351 unsigned VecBitWidth = SrcTy->getBitWidth();
David Majnemer98cfe2b2015-04-03 20:18:40 +00001352 unsigned SrcElemBitWidth = DL.getTypeSizeInBits(SrcTy->getElementType());
JF Bastiend52c9902015-02-25 22:30:51 +00001353 assert(SrcElemBitWidth && "vector elements must have a bitwidth");
1354 unsigned SrcNumElems = SrcTy->getNumElements();
1355 SmallVector<BitCastInst *, 8> BCs;
1356 DenseMap<Type *, Value *> NewBCs;
1357 for (User *U : SVI.users())
1358 if (BitCastInst *BC = dyn_cast<BitCastInst>(U))
1359 if (!BC->use_empty())
1360 // Only visit bitcasts that weren't previously handled.
1361 BCs.push_back(BC);
1362 for (BitCastInst *BC : BCs) {
Eugene Leviant958fcd72017-02-17 07:36:03 +00001363 unsigned BegIdx = Mask.front();
JF Bastiend52c9902015-02-25 22:30:51 +00001364 Type *TgtTy = BC->getDestTy();
David Majnemer98cfe2b2015-04-03 20:18:40 +00001365 unsigned TgtElemBitWidth = DL.getTypeSizeInBits(TgtTy);
JF Bastiend52c9902015-02-25 22:30:51 +00001366 if (!TgtElemBitWidth)
1367 continue;
1368 unsigned TgtNumElems = VecBitWidth / TgtElemBitWidth;
1369 bool VecBitWidthsEqual = VecBitWidth == TgtNumElems * TgtElemBitWidth;
1370 bool BegIsAligned = 0 == ((SrcElemBitWidth * BegIdx) % TgtElemBitWidth);
1371 if (!VecBitWidthsEqual)
1372 continue;
1373 if (!VectorType::isValidElementType(TgtTy))
1374 continue;
1375 VectorType *CastSrcTy = VectorType::get(TgtTy, TgtNumElems);
1376 if (!BegIsAligned) {
1377 // Shuffle the input so [0,NumElements) contains the output, and
1378 // [NumElems,SrcNumElems) is undef.
1379 SmallVector<Constant *, 16> ShuffleMask(SrcNumElems,
1380 UndefValue::get(Int32Ty));
1381 for (unsigned I = 0, E = MaskElems, Idx = BegIdx; I != E; ++Idx, ++I)
1382 ShuffleMask[I] = ConstantInt::get(Int32Ty, Idx);
Craig Topperbb4069e2017-07-07 23:16:26 +00001383 V = Builder.CreateShuffleVector(V, UndefValue::get(V->getType()),
1384 ConstantVector::get(ShuffleMask),
1385 SVI.getName() + ".extract");
JF Bastiend52c9902015-02-25 22:30:51 +00001386 BegIdx = 0;
1387 }
1388 unsigned SrcElemsPerTgtElem = TgtElemBitWidth / SrcElemBitWidth;
1389 assert(SrcElemsPerTgtElem);
1390 BegIdx /= SrcElemsPerTgtElem;
1391 bool BCAlreadyExists = NewBCs.find(CastSrcTy) != NewBCs.end();
1392 auto *NewBC =
1393 BCAlreadyExists
1394 ? NewBCs[CastSrcTy]
Craig Topperbb4069e2017-07-07 23:16:26 +00001395 : Builder.CreateBitCast(V, CastSrcTy, SVI.getName() + ".bc");
JF Bastiend52c9902015-02-25 22:30:51 +00001396 if (!BCAlreadyExists)
1397 NewBCs[CastSrcTy] = NewBC;
Craig Topperbb4069e2017-07-07 23:16:26 +00001398 auto *Ext = Builder.CreateExtractElement(
JF Bastiend52c9902015-02-25 22:30:51 +00001399 NewBC, ConstantInt::get(Int32Ty, BegIdx), SVI.getName() + ".extract");
1400 // The shufflevector isn't being replaced: the bitcast that used it
1401 // is. InstCombine will visit the newly-created instructions.
Sanjay Patel4b198802016-02-01 22:23:39 +00001402 replaceInstUsesWith(*BC, Ext);
JF Bastiend52c9902015-02-25 22:30:51 +00001403 MadeChange = true;
1404 }
1405 }
1406
Eric Christopher51edc7b2010-08-17 22:55:27 +00001407 // If the LHS is a shufflevector itself, see if we can combine it with this
Eli Friedmance818272011-10-21 19:06:29 +00001408 // one without producing an unusual shuffle.
1409 // Cases that might be simplified:
1410 // 1.
1411 // x1=shuffle(v1,v2,mask1)
1412 // x=shuffle(x1,undef,mask)
1413 // ==>
1414 // x=shuffle(v1,undef,newMask)
1415 // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : -1
1416 // 2.
1417 // x1=shuffle(v1,undef,mask1)
1418 // x=shuffle(x1,x2,mask)
1419 // where v1.size() == mask1.size()
1420 // ==>
1421 // x=shuffle(v1,x2,newMask)
1422 // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : mask[i]
1423 // 3.
1424 // x2=shuffle(v2,undef,mask2)
1425 // x=shuffle(x1,x2,mask)
1426 // where v2.size() == mask2.size()
1427 // ==>
1428 // x=shuffle(x1,v2,newMask)
1429 // newMask[i] = (mask[i] < x1.size())
1430 // ? mask[i] : mask2[mask[i]-x1.size()]+x1.size()
1431 // 4.
1432 // x1=shuffle(v1,undef,mask1)
1433 // x2=shuffle(v2,undef,mask2)
1434 // x=shuffle(x1,x2,mask)
1435 // where v1.size() == v2.size()
1436 // ==>
1437 // x=shuffle(v1,v2,newMask)
1438 // newMask[i] = (mask[i] < x1.size())
1439 // ? mask1[mask[i]] : mask2[mask[i]-x1.size()]+v1.size()
1440 //
1441 // Here we are really conservative:
Eric Christopher51edc7b2010-08-17 22:55:27 +00001442 // we are absolutely afraid of producing a shuffle mask not in the input
1443 // program, because the code gen may not be smart enough to turn a merged
1444 // shuffle into two specific shuffles: it may produce worse code. As such,
Jim Grosbachd11584a2013-05-01 00:25:27 +00001445 // we only merge two shuffles if the result is either a splat or one of the
1446 // input shuffle masks. In this case, merging the shuffles just removes
1447 // one instruction, which we know is safe. This is good for things like
Eli Friedmance818272011-10-21 19:06:29 +00001448 // turning: (splat(splat)) -> splat, or
1449 // merge(V[0..n], V[n+1..2n]) -> V[0..2n]
1450 ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS);
1451 ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS);
1452 if (LHSShuffle)
1453 if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS))
Craig Topperf40110f2014-04-25 05:29:35 +00001454 LHSShuffle = nullptr;
Eli Friedmance818272011-10-21 19:06:29 +00001455 if (RHSShuffle)
1456 if (!isa<UndefValue>(RHSShuffle->getOperand(1)))
Craig Topperf40110f2014-04-25 05:29:35 +00001457 RHSShuffle = nullptr;
Eli Friedmance818272011-10-21 19:06:29 +00001458 if (!LHSShuffle && !RHSShuffle)
Craig Topperf40110f2014-04-25 05:29:35 +00001459 return MadeChange ? &SVI : nullptr;
Eli Friedmance818272011-10-21 19:06:29 +00001460
Craig Topperf40110f2014-04-25 05:29:35 +00001461 Value* LHSOp0 = nullptr;
1462 Value* LHSOp1 = nullptr;
1463 Value* RHSOp0 = nullptr;
Eli Friedmance818272011-10-21 19:06:29 +00001464 unsigned LHSOp0Width = 0;
1465 unsigned RHSOp0Width = 0;
1466 if (LHSShuffle) {
1467 LHSOp0 = LHSShuffle->getOperand(0);
1468 LHSOp1 = LHSShuffle->getOperand(1);
Craig Topper17b55682016-12-29 07:03:18 +00001469 LHSOp0Width = LHSOp0->getType()->getVectorNumElements();
Eli Friedmance818272011-10-21 19:06:29 +00001470 }
1471 if (RHSShuffle) {
1472 RHSOp0 = RHSShuffle->getOperand(0);
Craig Topper17b55682016-12-29 07:03:18 +00001473 RHSOp0Width = RHSOp0->getType()->getVectorNumElements();
Eli Friedmance818272011-10-21 19:06:29 +00001474 }
1475 Value* newLHS = LHS;
1476 Value* newRHS = RHS;
1477 if (LHSShuffle) {
1478 // case 1
Eric Christopher51edc7b2010-08-17 22:55:27 +00001479 if (isa<UndefValue>(RHS)) {
Eli Friedmance818272011-10-21 19:06:29 +00001480 newLHS = LHSOp0;
1481 newRHS = LHSOp1;
1482 }
1483 // case 2 or 4
1484 else if (LHSOp0Width == LHSWidth) {
1485 newLHS = LHSOp0;
1486 }
1487 }
1488 // case 3 or 4
1489 if (RHSShuffle && RHSOp0Width == LHSWidth) {
1490 newRHS = RHSOp0;
1491 }
1492 // case 4
1493 if (LHSOp0 == RHSOp0) {
1494 newLHS = LHSOp0;
Craig Topperf40110f2014-04-25 05:29:35 +00001495 newRHS = nullptr;
Eli Friedmance818272011-10-21 19:06:29 +00001496 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +00001497
Eli Friedmance818272011-10-21 19:06:29 +00001498 if (newLHS == LHS && newRHS == RHS)
Craig Topperf40110f2014-04-25 05:29:35 +00001499 return MadeChange ? &SVI : nullptr;
Bob Wilson8ecf98b2010-10-29 22:20:43 +00001500
Eli Friedmance818272011-10-21 19:06:29 +00001501 SmallVector<int, 16> LHSMask;
1502 SmallVector<int, 16> RHSMask;
Chris Lattner8326bd82012-01-26 00:42:34 +00001503 if (newLHS != LHS)
1504 LHSMask = LHSShuffle->getShuffleMask();
1505 if (RHSShuffle && newRHS != RHS)
1506 RHSMask = RHSShuffle->getShuffleMask();
1507
Eli Friedmance818272011-10-21 19:06:29 +00001508 unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth;
1509 SmallVector<int, 16> newMask;
1510 bool isSplat = true;
1511 int SplatElt = -1;
1512 // Create a new mask for the new ShuffleVectorInst so that the new
1513 // ShuffleVectorInst is equivalent to the original one.
1514 for (unsigned i = 0; i < VWidth; ++i) {
1515 int eltMask;
Craig Topper45d9f4b2013-01-18 05:30:07 +00001516 if (Mask[i] < 0) {
Eli Friedmance818272011-10-21 19:06:29 +00001517 // This element is an undef value.
1518 eltMask = -1;
1519 } else if (Mask[i] < (int)LHSWidth) {
1520 // This element is from left hand side vector operand.
Craig Topper2ea22b02013-01-18 05:09:16 +00001521 //
Eli Friedmance818272011-10-21 19:06:29 +00001522 // If LHS is going to be replaced (case 1, 2, or 4), calculate the
1523 // new mask value for the element.
1524 if (newLHS != LHS) {
1525 eltMask = LHSMask[Mask[i]];
1526 // If the value selected is an undef value, explicitly specify it
1527 // with a -1 mask value.
1528 if (eltMask >= (int)LHSOp0Width && isa<UndefValue>(LHSOp1))
1529 eltMask = -1;
Craig Topper2ea22b02013-01-18 05:09:16 +00001530 } else
Eli Friedmance818272011-10-21 19:06:29 +00001531 eltMask = Mask[i];
1532 } else {
1533 // This element is from right hand side vector operand
1534 //
1535 // If the value selected is an undef value, explicitly specify it
1536 // with a -1 mask value. (case 1)
1537 if (isa<UndefValue>(RHS))
1538 eltMask = -1;
1539 // If RHS is going to be replaced (case 3 or 4), calculate the
1540 // new mask value for the element.
1541 else if (newRHS != RHS) {
1542 eltMask = RHSMask[Mask[i]-LHSWidth];
1543 // If the value selected is an undef value, explicitly specify it
1544 // with a -1 mask value.
1545 if (eltMask >= (int)RHSOp0Width) {
1546 assert(isa<UndefValue>(RHSShuffle->getOperand(1))
1547 && "should have been check above");
1548 eltMask = -1;
Nate Begeman2a0ca3e92010-08-13 00:17:53 +00001549 }
Craig Topper2ea22b02013-01-18 05:09:16 +00001550 } else
Eli Friedmance818272011-10-21 19:06:29 +00001551 eltMask = Mask[i]-LHSWidth;
1552
1553 // If LHS's width is changed, shift the mask value accordingly.
Eugene Zelenko7f0f9bc2017-10-24 21:24:53 +00001554 // If newRHS == nullptr, i.e. LHSOp0 == RHSOp0, we want to remap any
Michael Gottesman02a11412012-10-16 21:29:38 +00001555 // references from RHSOp0 to LHSOp0, so we don't need to shift the mask.
1556 // If newRHS == newLHS, we want to remap any references from newRHS to
1557 // newLHS so that we can properly identify splats that may occur due to
Alp Tokercb402912014-01-24 17:20:08 +00001558 // obfuscation across the two vectors.
Craig Topperf40110f2014-04-25 05:29:35 +00001559 if (eltMask >= 0 && newRHS != nullptr && newLHS != newRHS)
Eli Friedmance818272011-10-21 19:06:29 +00001560 eltMask += newLHSWidth;
Nate Begeman2a0ca3e92010-08-13 00:17:53 +00001561 }
Eli Friedmance818272011-10-21 19:06:29 +00001562
1563 // Check if this could still be a splat.
1564 if (eltMask >= 0) {
1565 if (SplatElt >= 0 && SplatElt != eltMask)
1566 isSplat = false;
1567 SplatElt = eltMask;
1568 }
1569
1570 newMask.push_back(eltMask);
1571 }
1572
1573 // If the result mask is equal to one of the original shuffle masks,
Jim Grosbachd11584a2013-05-01 00:25:27 +00001574 // or is a splat, do the replacement.
1575 if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) {
Eli Friedmance818272011-10-21 19:06:29 +00001576 SmallVector<Constant*, 16> Elts;
Eli Friedmance818272011-10-21 19:06:29 +00001577 for (unsigned i = 0, e = newMask.size(); i != e; ++i) {
1578 if (newMask[i] < 0) {
1579 Elts.push_back(UndefValue::get(Int32Ty));
1580 } else {
1581 Elts.push_back(ConstantInt::get(Int32Ty, newMask[i]));
1582 }
1583 }
Craig Topperf40110f2014-04-25 05:29:35 +00001584 if (!newRHS)
Eli Friedmance818272011-10-21 19:06:29 +00001585 newRHS = UndefValue::get(newLHS->getType());
1586 return new ShuffleVectorInst(newLHS, newRHS, ConstantVector::get(Elts));
Nate Begeman2a0ca3e92010-08-13 00:17:53 +00001587 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +00001588
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001589 // If the result mask is an identity, replace uses of this instruction with
1590 // corresponding argument.
Serge Pavlovb575ee82014-05-13 06:07:21 +00001591 bool isLHSID, isRHSID;
Sanjay Patel431e1142015-11-17 17:24:08 +00001592 recognizeIdentityMask(newMask, isLHSID, isRHSID);
Sanjay Patel4b198802016-02-01 22:23:39 +00001593 if (isLHSID && VWidth == LHSOp0Width) return replaceInstUsesWith(SVI, newLHS);
1594 if (isRHSID && VWidth == RHSOp0Width) return replaceInstUsesWith(SVI, newRHS);
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001595
Craig Topperf40110f2014-04-25 05:29:35 +00001596 return MadeChange ? &SVI : nullptr;
Chris Lattnerec97a902010-01-05 05:36:20 +00001597}