blob: 4f71db1a4b097cf363e197459b61c707ee5185d0 [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
15#include "InstCombine.h"
Nadav Rotem7df85092013-01-15 23:43:14 +000016#include "llvm/Support/PatternMatch.h"
Chris Lattnerec97a902010-01-05 05:36:20 +000017using namespace llvm;
Nadav Rotem7df85092013-01-15 23:43:14 +000018using namespace PatternMatch;
Chris Lattnerec97a902010-01-05 05:36:20 +000019
20/// CheapToScalarize - Return true if the value is cheaper to scalarize than it
Chris Lattnera0d01ff2012-01-24 14:31:22 +000021/// is to leave as a vector operation. isConstant indicates whether we're
22/// extracting one known element. If false we're extracting a variable index.
Chris Lattnerec97a902010-01-05 05:36:20 +000023static bool CheapToScalarize(Value *V, bool isConstant) {
Chris Lattner8326bd82012-01-26 00:42:34 +000024 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +000025 if (isConstant) return true;
Chris Lattner8326bd82012-01-26 00:42:34 +000026
27 // If all elts are the same, we can extract it and use any of the values.
28 Constant *Op0 = C->getAggregateElement(0U);
29 for (unsigned i = 1, e = V->getType()->getVectorNumElements(); i != e; ++i)
30 if (C->getAggregateElement(i) != Op0)
Chris Lattnerec97a902010-01-05 05:36:20 +000031 return false;
32 return true;
33 }
34 Instruction *I = dyn_cast<Instruction>(V);
35 if (!I) return false;
Bob Wilson8ecf98b2010-10-29 22:20:43 +000036
Chris Lattnerec97a902010-01-05 05:36:20 +000037 // Insert element gets simplified to the inserted element or is deleted if
38 // this is constant idx extract element and its a constant idx insertelt.
39 if (I->getOpcode() == Instruction::InsertElement && isConstant &&
40 isa<ConstantInt>(I->getOperand(2)))
41 return true;
42 if (I->getOpcode() == Instruction::Load && I->hasOneUse())
43 return true;
44 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I))
45 if (BO->hasOneUse() &&
46 (CheapToScalarize(BO->getOperand(0), isConstant) ||
47 CheapToScalarize(BO->getOperand(1), isConstant)))
48 return true;
49 if (CmpInst *CI = dyn_cast<CmpInst>(I))
50 if (CI->hasOneUse() &&
51 (CheapToScalarize(CI->getOperand(0), isConstant) ||
52 CheapToScalarize(CI->getOperand(1), isConstant)))
53 return true;
Bob Wilson8ecf98b2010-10-29 22:20:43 +000054
Chris Lattnerec97a902010-01-05 05:36:20 +000055 return false;
56}
57
Chris Lattnerec97a902010-01-05 05:36:20 +000058/// FindScalarElement - Given a vector and an element number, see if the scalar
59/// value is already around as a register, for example if it were inserted then
60/// extracted from the vector.
61static Value *FindScalarElement(Value *V, unsigned EltNo) {
Duncan Sands19d0b472010-02-16 11:11:14 +000062 assert(V->getType()->isVectorTy() && "Not looking at a vector?");
Chris Lattner8326bd82012-01-26 00:42:34 +000063 VectorType *VTy = cast<VectorType>(V->getType());
64 unsigned Width = VTy->getNumElements();
Chris Lattnerec97a902010-01-05 05:36:20 +000065 if (EltNo >= Width) // Out of range access.
Chris Lattner8326bd82012-01-26 00:42:34 +000066 return UndefValue::get(VTy->getElementType());
Bob Wilson8ecf98b2010-10-29 22:20:43 +000067
Chris Lattner8326bd82012-01-26 00:42:34 +000068 if (Constant *C = dyn_cast<Constant>(V))
69 return C->getAggregateElement(EltNo);
Bob Wilson8ecf98b2010-10-29 22:20:43 +000070
Chris Lattner841af4f2010-01-05 05:42:08 +000071 if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +000072 // If this is an insert to a variable element, we don't know what it is.
Bob Wilson8ecf98b2010-10-29 22:20:43 +000073 if (!isa<ConstantInt>(III->getOperand(2)))
Chris Lattnerec97a902010-01-05 05:36:20 +000074 return 0;
75 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +000076
Chris Lattnerec97a902010-01-05 05:36:20 +000077 // If this is an insert to the element we are looking for, return the
78 // inserted value.
Bob Wilson8ecf98b2010-10-29 22:20:43 +000079 if (EltNo == IIElt)
Chris Lattnerec97a902010-01-05 05:36:20 +000080 return III->getOperand(1);
Bob Wilson8ecf98b2010-10-29 22:20:43 +000081
Chris Lattnerec97a902010-01-05 05:36:20 +000082 // Otherwise, the insertelement doesn't modify the value, recurse on its
83 // vector input.
84 return FindScalarElement(III->getOperand(0), EltNo);
Chris Lattner841af4f2010-01-05 05:42:08 +000085 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +000086
Chris Lattner841af4f2010-01-05 05:42:08 +000087 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
Chris Lattner8326bd82012-01-26 00:42:34 +000088 unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
Eli Friedman303c81c2011-10-21 19:11:34 +000089 int InEl = SVI->getMaskValue(EltNo);
Bob Wilson8ecf98b2010-10-29 22:20:43 +000090 if (InEl < 0)
Chris Lattner8326bd82012-01-26 00:42:34 +000091 return UndefValue::get(VTy->getElementType());
Bob Wilson11ee4562010-10-29 22:03:05 +000092 if (InEl < (int)LHSWidth)
93 return FindScalarElement(SVI->getOperand(0), InEl);
94 return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth);
Chris Lattnerec97a902010-01-05 05:36:20 +000095 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +000096
Nadav Rotem7df85092013-01-15 23:43:14 +000097 // Extract a value from a vector add operation with a constant zero.
98 Value *Val = 0; Constant *Con = 0;
99 if (match(V, m_Add(m_Value(Val), m_Constant(Con)))) {
100 if (Con->getAggregateElement(EltNo)->isNullValue())
101 return FindScalarElement(Val, EltNo);
102 }
103
Chris Lattnerec97a902010-01-05 05:36:20 +0000104 // Otherwise, we don't know.
105 return 0;
106}
107
108Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
Chris Lattner8326bd82012-01-26 00:42:34 +0000109 // If vector val is constant with all elements the same, replace EI with
110 // that element. We handle a known element # below.
111 if (Constant *C = dyn_cast<Constant>(EI.getOperand(0)))
112 if (CheapToScalarize(C, false))
113 return ReplaceInstUsesWith(EI, C->getAggregateElement(0U));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000114
Chris Lattnerec97a902010-01-05 05:36:20 +0000115 // If extracting a specified index from the vector, see if we can recursively
116 // find a previously computed scalar that was inserted into the vector.
117 if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) {
118 unsigned IndexVal = IdxC->getZExtValue();
119 unsigned VectorWidth = EI.getVectorOperandType()->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000120
Chris Lattnerec97a902010-01-05 05:36:20 +0000121 // If this is extracting an invalid index, turn this into undef, to avoid
122 // crashing the code below.
123 if (IndexVal >= VectorWidth)
124 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000125
Chris Lattnerec97a902010-01-05 05:36:20 +0000126 // This instruction only demands the single element from the input vector.
127 // If the input vector has a single use, simplify it based on this use
128 // property.
129 if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
130 APInt UndefElts(VectorWidth, 0);
Chris Lattnerb22423c2010-02-08 23:56:03 +0000131 APInt DemandedMask(VectorWidth, 0);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000132 DemandedMask.setBit(IndexVal);
Chris Lattnerec97a902010-01-05 05:36:20 +0000133 if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0),
134 DemandedMask, UndefElts)) {
135 EI.setOperand(0, V);
136 return &EI;
137 }
138 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000139
Chris Lattnerec97a902010-01-05 05:36:20 +0000140 if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal))
141 return ReplaceInstUsesWith(EI, Elt);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000142
Chris Lattnerec97a902010-01-05 05:36:20 +0000143 // If the this extractelement is directly using a bitcast from a vector of
144 // the same number of elements, see if we can find the source element from
145 // it. In this case, we will end up needing to bitcast the scalars.
146 if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) {
Chris Lattner8326bd82012-01-26 00:42:34 +0000147 if (VectorType *VT = dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
Chris Lattnerec97a902010-01-05 05:36:20 +0000148 if (VT->getNumElements() == VectorWidth)
149 if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal))
150 return new BitCastInst(Elt, EI.getType());
151 }
152 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000153
Chris Lattnerec97a902010-01-05 05:36:20 +0000154 if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) {
155 // Push extractelement into predecessor operation if legal and
156 // profitable to do so
157 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
158 if (I->hasOneUse() &&
159 CheapToScalarize(BO, isa<ConstantInt>(EI.getOperand(1)))) {
160 Value *newEI0 =
Bob Wilson67a6f322010-10-29 22:20:45 +0000161 Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1),
162 EI.getName()+".lhs");
Chris Lattnerec97a902010-01-05 05:36:20 +0000163 Value *newEI1 =
Bob Wilson67a6f322010-10-29 22:20:45 +0000164 Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1),
165 EI.getName()+".rhs");
Chris Lattnerec97a902010-01-05 05:36:20 +0000166 return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1);
167 }
168 } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
169 // Extracting the inserted element?
170 if (IE->getOperand(2) == EI.getOperand(1))
171 return ReplaceInstUsesWith(EI, IE->getOperand(1));
172 // If the inserted and extracted elements are constants, they must not
173 // be the same value, extract from the pre-inserted value instead.
174 if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) {
175 Worklist.AddValue(EI.getOperand(0));
176 EI.setOperand(0, IE->getOperand(0));
177 return &EI;
178 }
179 } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) {
180 // If this is extracting an element from a shufflevector, figure out where
181 // it came from and extract from the appropriate input element instead.
182 if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) {
Eli Friedman303c81c2011-10-21 19:11:34 +0000183 int SrcIdx = SVI->getMaskValue(Elt->getZExtValue());
Chris Lattnerec97a902010-01-05 05:36:20 +0000184 Value *Src;
185 unsigned LHSWidth =
Chris Lattner8326bd82012-01-26 00:42:34 +0000186 SVI->getOperand(0)->getType()->getVectorNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000187
Bob Wilson11ee4562010-10-29 22:03:05 +0000188 if (SrcIdx < 0)
189 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
190 if (SrcIdx < (int)LHSWidth)
Chris Lattnerec97a902010-01-05 05:36:20 +0000191 Src = SVI->getOperand(0);
Bob Wilson11ee4562010-10-29 22:03:05 +0000192 else {
Chris Lattnerec97a902010-01-05 05:36:20 +0000193 SrcIdx -= LHSWidth;
194 Src = SVI->getOperand(1);
Chris Lattnerec97a902010-01-05 05:36:20 +0000195 }
Chris Lattner229907c2011-07-18 04:54:35 +0000196 Type *Int32Ty = Type::getInt32Ty(EI.getContext());
Chris Lattnerec97a902010-01-05 05:36:20 +0000197 return ExtractElementInst::Create(Src,
Bob Wilson9d07f392010-10-29 22:03:07 +0000198 ConstantInt::get(Int32Ty,
Chris Lattnerec97a902010-01-05 05:36:20 +0000199 SrcIdx, false));
200 }
Nadav Rotemd74b72b2011-03-31 22:57:29 +0000201 } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
202 // Canonicalize extractelement(cast) -> cast(extractelement)
203 // bitcasts can change the number of vector elements and they cost nothing
204 if (CI->hasOneUse() && EI.hasOneUse() &&
205 (CI->getOpcode() != Instruction::BitCast)) {
206 Value *EE = Builder->CreateExtractElement(CI->getOperand(0),
207 EI.getIndexOperand());
208 return CastInst::Create(CI->getOpcode(), EE, EI.getType());
209 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000210 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000211 }
212 return 0;
213}
214
215/// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000216/// elements from either LHS or RHS, return the shuffle mask and true.
Chris Lattnerec97a902010-01-05 05:36:20 +0000217/// Otherwise, return false.
218static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
Chris Lattner0256be92012-01-27 03:08:05 +0000219 SmallVectorImpl<Constant*> &Mask) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000220 assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
221 "Invalid CollectSingleShuffleElements");
222 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000223
Chris Lattnerec97a902010-01-05 05:36:20 +0000224 if (isa<UndefValue>(V)) {
225 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
226 return true;
227 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000228
Chris Lattnerec97a902010-01-05 05:36:20 +0000229 if (V == LHS) {
230 for (unsigned i = 0; i != NumElts; ++i)
231 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
232 return true;
233 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000234
Chris Lattnerec97a902010-01-05 05:36:20 +0000235 if (V == RHS) {
236 for (unsigned i = 0; i != NumElts; ++i)
237 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()),
238 i+NumElts));
239 return true;
240 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000241
Chris Lattnerec97a902010-01-05 05:36:20 +0000242 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
243 // If this is an insert of an extract from some other vector, include it.
244 Value *VecOp = IEI->getOperand(0);
245 Value *ScalarOp = IEI->getOperand(1);
246 Value *IdxOp = IEI->getOperand(2);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000247
Chris Lattnerec97a902010-01-05 05:36:20 +0000248 if (!isa<ConstantInt>(IdxOp))
249 return false;
250 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000251
Chris Lattnerec97a902010-01-05 05:36:20 +0000252 if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector.
253 // Okay, we can handle this if the vector we are insertinting into is
254 // transitively ok.
255 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
256 // If so, update the mask to reflect the inserted undef.
257 Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext()));
258 return true;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000259 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000260 } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
261 if (isa<ConstantInt>(EI->getOperand(1)) &&
262 EI->getOperand(0)->getType() == V->getType()) {
263 unsigned ExtractedIdx =
264 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000265
Chris Lattnerec97a902010-01-05 05:36:20 +0000266 // This must be extracting from either LHS or RHS.
267 if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
268 // Okay, we can handle this if the vector we are insertinting into is
269 // transitively ok.
270 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
271 // If so, update the mask to reflect the inserted value.
272 if (EI->getOperand(0) == LHS) {
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000273 Mask[InsertedIdx % NumElts] =
Chris Lattnerec97a902010-01-05 05:36:20 +0000274 ConstantInt::get(Type::getInt32Ty(V->getContext()),
275 ExtractedIdx);
276 } else {
277 assert(EI->getOperand(0) == RHS);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000278 Mask[InsertedIdx % NumElts] =
Chris Lattnerec97a902010-01-05 05:36:20 +0000279 ConstantInt::get(Type::getInt32Ty(V->getContext()),
280 ExtractedIdx+NumElts);
Chris Lattnerec97a902010-01-05 05:36:20 +0000281 }
282 return true;
283 }
284 }
285 }
286 }
287 }
288 // TODO: Handle shufflevector here!
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000289
Chris Lattnerec97a902010-01-05 05:36:20 +0000290 return false;
291}
292
293/// CollectShuffleElements - We are building a shuffle of V, using RHS as the
294/// RHS of the shuffle instruction, if it is not null. Return a shuffle mask
295/// that computes V and the LHS value of the shuffle.
Chris Lattner0256be92012-01-27 03:08:05 +0000296static Value *CollectShuffleElements(Value *V, SmallVectorImpl<Constant*> &Mask,
Chris Lattnerec97a902010-01-05 05:36:20 +0000297 Value *&RHS) {
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000298 assert(V->getType()->isVectorTy() &&
Chris Lattnerec97a902010-01-05 05:36:20 +0000299 (RHS == 0 || V->getType() == RHS->getType()) &&
300 "Invalid shuffle!");
301 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000302
Chris Lattnerec97a902010-01-05 05:36:20 +0000303 if (isa<UndefValue>(V)) {
304 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
305 return V;
Chris Lattnera0d01ff2012-01-24 14:31:22 +0000306 }
Craig Topper2ea22b02013-01-18 05:09:16 +0000307
Chris Lattnera0d01ff2012-01-24 14:31:22 +0000308 if (isa<ConstantAggregateZero>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000309 Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0));
310 return V;
Chris Lattnera0d01ff2012-01-24 14:31:22 +0000311 }
Craig Topper2ea22b02013-01-18 05:09:16 +0000312
Chris Lattnera0d01ff2012-01-24 14:31:22 +0000313 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000314 // If this is an insert of an extract from some other vector, include it.
315 Value *VecOp = IEI->getOperand(0);
316 Value *ScalarOp = IEI->getOperand(1);
317 Value *IdxOp = IEI->getOperand(2);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000318
Chris Lattnerec97a902010-01-05 05:36:20 +0000319 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
320 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
321 EI->getOperand(0)->getType() == V->getType()) {
322 unsigned ExtractedIdx =
Bob Wilson67a6f322010-10-29 22:20:45 +0000323 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Chris Lattnerec97a902010-01-05 05:36:20 +0000324 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000325
Chris Lattnerec97a902010-01-05 05:36:20 +0000326 // Either the extracted from or inserted into vector must be RHSVec,
327 // otherwise we'd end up with a shuffle of three inputs.
328 if (EI->getOperand(0) == RHS || RHS == 0) {
329 RHS = EI->getOperand(0);
330 Value *V = CollectShuffleElements(VecOp, Mask, RHS);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000331 Mask[InsertedIdx % NumElts] =
Bob Wilson67a6f322010-10-29 22:20:45 +0000332 ConstantInt::get(Type::getInt32Ty(V->getContext()),
333 NumElts+ExtractedIdx);
Chris Lattnerec97a902010-01-05 05:36:20 +0000334 return V;
335 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000336
Chris Lattnerec97a902010-01-05 05:36:20 +0000337 if (VecOp == RHS) {
338 Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS);
339 // Everything but the extracted element is replaced with the RHS.
340 for (unsigned i = 0; i != NumElts; ++i) {
341 if (i != InsertedIdx)
342 Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()),
343 NumElts+i);
344 }
345 return V;
346 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000347
Chris Lattnerec97a902010-01-05 05:36:20 +0000348 // If this insertelement is a chain that comes from exactly these two
349 // vectors, return the vector and the effective shuffle.
350 if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask))
351 return EI->getOperand(0);
352 }
353 }
354 }
355 // TODO: Handle shufflevector here!
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000356
Chris Lattnerec97a902010-01-05 05:36:20 +0000357 // Otherwise, can't do anything fancy. Return an identity vector.
358 for (unsigned i = 0; i != NumElts; ++i)
359 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
360 return V;
361}
362
363Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
364 Value *VecOp = IE.getOperand(0);
365 Value *ScalarOp = IE.getOperand(1);
366 Value *IdxOp = IE.getOperand(2);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000367
Chris Lattnerec97a902010-01-05 05:36:20 +0000368 // Inserting an undef or into an undefined place, remove this.
369 if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
370 ReplaceInstUsesWith(IE, VecOp);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000371
372 // If the inserted element was extracted from some other vector, and if the
Chris Lattnerec97a902010-01-05 05:36:20 +0000373 // indexes are constant, try to turn this into a shufflevector operation.
374 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
375 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
376 EI->getOperand(0)->getType() == IE.getType()) {
377 unsigned NumVectorElts = IE.getType()->getNumElements();
378 unsigned ExtractedIdx =
Bob Wilson67a6f322010-10-29 22:20:45 +0000379 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Chris Lattnerec97a902010-01-05 05:36:20 +0000380 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000381
Chris Lattnerec97a902010-01-05 05:36:20 +0000382 if (ExtractedIdx >= NumVectorElts) // Out of range extract.
383 return ReplaceInstUsesWith(IE, VecOp);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000384
Chris Lattnerec97a902010-01-05 05:36:20 +0000385 if (InsertedIdx >= NumVectorElts) // Out of range insert.
386 return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000387
Chris Lattnerec97a902010-01-05 05:36:20 +0000388 // If we are extracting a value from a vector, then inserting it right
389 // back into the same place, just use the input vector.
390 if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000391 return ReplaceInstUsesWith(IE, VecOp);
392
Chris Lattnerec97a902010-01-05 05:36:20 +0000393 // If this insertelement isn't used by some other insertelement, turn it
394 // (and any insertelements it points to), into one big shuffle.
395 if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) {
Chris Lattner0256be92012-01-27 03:08:05 +0000396 SmallVector<Constant*, 16> Mask;
Chris Lattnerec97a902010-01-05 05:36:20 +0000397 Value *RHS = 0;
398 Value *LHS = CollectShuffleElements(&IE, Mask, RHS);
399 if (RHS == 0) RHS = UndefValue::get(LHS->getType());
400 // We now have a shuffle of LHS, RHS, Mask.
Bob Wilson67a6f322010-10-29 22:20:45 +0000401 return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask));
Chris Lattnerec97a902010-01-05 05:36:20 +0000402 }
403 }
404 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000405
Chris Lattnerec97a902010-01-05 05:36:20 +0000406 unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements();
407 APInt UndefElts(VWidth, 0);
408 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
Eli Friedmanef200db2011-02-19 22:42:40 +0000409 if (Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) {
410 if (V != &IE)
411 return ReplaceInstUsesWith(IE, V);
Chris Lattnerec97a902010-01-05 05:36:20 +0000412 return &IE;
Eli Friedmanef200db2011-02-19 22:42:40 +0000413 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000414
Chris Lattnerec97a902010-01-05 05:36:20 +0000415 return 0;
416}
417
418
419Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
420 Value *LHS = SVI.getOperand(0);
421 Value *RHS = SVI.getOperand(1);
Chris Lattner8326bd82012-01-26 00:42:34 +0000422 SmallVector<int, 16> Mask = SVI.getShuffleMask();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000423
Chris Lattnerec97a902010-01-05 05:36:20 +0000424 bool MadeChange = false;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000425
Chris Lattnerec97a902010-01-05 05:36:20 +0000426 // Undefined shuffle mask -> undefined value.
427 if (isa<UndefValue>(SVI.getOperand(2)))
428 return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000429
Eric Christopher51edc7b2010-08-17 22:55:27 +0000430 unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000431
Chris Lattnerec97a902010-01-05 05:36:20 +0000432 APInt UndefElts(VWidth, 0);
433 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
Eli Friedmanef200db2011-02-19 22:42:40 +0000434 if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
435 if (V != &SVI)
436 return ReplaceInstUsesWith(SVI, V);
Chris Lattnerec97a902010-01-05 05:36:20 +0000437 LHS = SVI.getOperand(0);
438 RHS = SVI.getOperand(1);
439 MadeChange = true;
440 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000441
Eli Friedmance818272011-10-21 19:06:29 +0000442 unsigned LHSWidth = cast<VectorType>(LHS->getType())->getNumElements();
443
Chris Lattnerec97a902010-01-05 05:36:20 +0000444 // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask')
445 // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
446 if (LHS == RHS || isa<UndefValue>(LHS)) {
Eric Christopher51edc7b2010-08-17 22:55:27 +0000447 if (isa<UndefValue>(LHS) && LHS == RHS) {
448 // shuffle(undef,undef,mask) -> undef.
Eli Friedmance818272011-10-21 19:06:29 +0000449 Value* result = (VWidth == LHSWidth)
450 ? LHS : UndefValue::get(SVI.getType());
451 return ReplaceInstUsesWith(SVI, result);
Eric Christopher51edc7b2010-08-17 22:55:27 +0000452 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000453
Chris Lattnerec97a902010-01-05 05:36:20 +0000454 // Remap any references to RHS to use LHS.
Chris Lattner0256be92012-01-27 03:08:05 +0000455 SmallVector<Constant*, 16> Elts;
Eli Friedmance818272011-10-21 19:06:29 +0000456 for (unsigned i = 0, e = LHSWidth; i != VWidth; ++i) {
Chris Lattner0256be92012-01-27 03:08:05 +0000457 if (Mask[i] < 0) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000458 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
Chris Lattner0256be92012-01-27 03:08:05 +0000459 continue;
460 }
461
462 if ((Mask[i] >= (int)e && isa<UndefValue>(RHS)) ||
463 (Mask[i] < (int)e && isa<UndefValue>(LHS))) {
464 Mask[i] = -1; // Turn into undef.
465 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
466 } else {
467 Mask[i] = Mask[i] % e; // Force to LHS.
468 Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()),
469 Mask[i]));
Chris Lattnerec97a902010-01-05 05:36:20 +0000470 }
471 }
472 SVI.setOperand(0, SVI.getOperand(1));
473 SVI.setOperand(1, UndefValue::get(RHS->getType()));
474 SVI.setOperand(2, ConstantVector::get(Elts));
475 LHS = SVI.getOperand(0);
476 RHS = SVI.getOperand(1);
477 MadeChange = true;
478 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000479
Eli Friedmance818272011-10-21 19:06:29 +0000480 if (VWidth == LHSWidth) {
481 // Analyze the shuffle, are the LHS or RHS and identity shuffles?
482 bool isLHSID = true, isRHSID = true;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000483
Eli Friedmance818272011-10-21 19:06:29 +0000484 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
485 if (Mask[i] < 0) continue; // Ignore undef values.
486 // Is this an identity shuffle of the LHS value?
487 isLHSID &= (Mask[i] == (int)i);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000488
Eli Friedmance818272011-10-21 19:06:29 +0000489 // Is this an identity shuffle of the RHS value?
490 isRHSID &= (Mask[i]-e == i);
491 }
492
493 // Eliminate identity shuffles.
494 if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
495 if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
Eric Christopher51edc7b2010-08-17 22:55:27 +0000496 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000497
Eric Christopher51edc7b2010-08-17 22:55:27 +0000498 // If the LHS is a shufflevector itself, see if we can combine it with this
Eli Friedmance818272011-10-21 19:06:29 +0000499 // one without producing an unusual shuffle.
500 // Cases that might be simplified:
501 // 1.
502 // x1=shuffle(v1,v2,mask1)
503 // x=shuffle(x1,undef,mask)
504 // ==>
505 // x=shuffle(v1,undef,newMask)
506 // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : -1
507 // 2.
508 // x1=shuffle(v1,undef,mask1)
509 // x=shuffle(x1,x2,mask)
510 // where v1.size() == mask1.size()
511 // ==>
512 // x=shuffle(v1,x2,newMask)
513 // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : mask[i]
514 // 3.
515 // x2=shuffle(v2,undef,mask2)
516 // x=shuffle(x1,x2,mask)
517 // where v2.size() == mask2.size()
518 // ==>
519 // x=shuffle(x1,v2,newMask)
520 // newMask[i] = (mask[i] < x1.size())
521 // ? mask[i] : mask2[mask[i]-x1.size()]+x1.size()
522 // 4.
523 // x1=shuffle(v1,undef,mask1)
524 // x2=shuffle(v2,undef,mask2)
525 // x=shuffle(x1,x2,mask)
526 // where v1.size() == v2.size()
527 // ==>
528 // x=shuffle(v1,v2,newMask)
529 // newMask[i] = (mask[i] < x1.size())
530 // ? mask1[mask[i]] : mask2[mask[i]-x1.size()]+v1.size()
531 //
532 // Here we are really conservative:
Eric Christopher51edc7b2010-08-17 22:55:27 +0000533 // we are absolutely afraid of producing a shuffle mask not in the input
534 // program, because the code gen may not be smart enough to turn a merged
535 // shuffle into two specific shuffles: it may produce worse code. As such,
Bob Wilsoncb11b482010-10-29 22:02:50 +0000536 // we only merge two shuffles if the result is either a splat or one of the
Eli Friedmance818272011-10-21 19:06:29 +0000537 // input shuffle masks. In this case, merging the shuffles just removes
Bob Wilsoncb11b482010-10-29 22:02:50 +0000538 // one instruction, which we know is safe. This is good for things like
Eli Friedmance818272011-10-21 19:06:29 +0000539 // turning: (splat(splat)) -> splat, or
540 // merge(V[0..n], V[n+1..2n]) -> V[0..2n]
541 ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS);
542 ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS);
543 if (LHSShuffle)
544 if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS))
545 LHSShuffle = NULL;
546 if (RHSShuffle)
547 if (!isa<UndefValue>(RHSShuffle->getOperand(1)))
548 RHSShuffle = NULL;
549 if (!LHSShuffle && !RHSShuffle)
550 return MadeChange ? &SVI : 0;
551
552 Value* LHSOp0 = NULL;
553 Value* LHSOp1 = NULL;
554 Value* RHSOp0 = NULL;
555 unsigned LHSOp0Width = 0;
556 unsigned RHSOp0Width = 0;
557 if (LHSShuffle) {
558 LHSOp0 = LHSShuffle->getOperand(0);
559 LHSOp1 = LHSShuffle->getOperand(1);
560 LHSOp0Width = cast<VectorType>(LHSOp0->getType())->getNumElements();
561 }
562 if (RHSShuffle) {
563 RHSOp0 = RHSShuffle->getOperand(0);
564 RHSOp0Width = cast<VectorType>(RHSOp0->getType())->getNumElements();
565 }
566 Value* newLHS = LHS;
567 Value* newRHS = RHS;
568 if (LHSShuffle) {
569 // case 1
Eric Christopher51edc7b2010-08-17 22:55:27 +0000570 if (isa<UndefValue>(RHS)) {
Eli Friedmance818272011-10-21 19:06:29 +0000571 newLHS = LHSOp0;
572 newRHS = LHSOp1;
573 }
574 // case 2 or 4
575 else if (LHSOp0Width == LHSWidth) {
576 newLHS = LHSOp0;
577 }
578 }
579 // case 3 or 4
580 if (RHSShuffle && RHSOp0Width == LHSWidth) {
581 newRHS = RHSOp0;
582 }
583 // case 4
584 if (LHSOp0 == RHSOp0) {
585 newLHS = LHSOp0;
586 newRHS = NULL;
587 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000588
Eli Friedmance818272011-10-21 19:06:29 +0000589 if (newLHS == LHS && newRHS == RHS)
590 return MadeChange ? &SVI : 0;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000591
Eli Friedmance818272011-10-21 19:06:29 +0000592 SmallVector<int, 16> LHSMask;
593 SmallVector<int, 16> RHSMask;
Chris Lattner8326bd82012-01-26 00:42:34 +0000594 if (newLHS != LHS)
595 LHSMask = LHSShuffle->getShuffleMask();
596 if (RHSShuffle && newRHS != RHS)
597 RHSMask = RHSShuffle->getShuffleMask();
598
Eli Friedmance818272011-10-21 19:06:29 +0000599 unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth;
600 SmallVector<int, 16> newMask;
601 bool isSplat = true;
602 int SplatElt = -1;
603 // Create a new mask for the new ShuffleVectorInst so that the new
604 // ShuffleVectorInst is equivalent to the original one.
605 for (unsigned i = 0; i < VWidth; ++i) {
606 int eltMask;
Craig Topper45d9f4b2013-01-18 05:30:07 +0000607 if (Mask[i] < 0) {
Eli Friedmance818272011-10-21 19:06:29 +0000608 // This element is an undef value.
609 eltMask = -1;
610 } else if (Mask[i] < (int)LHSWidth) {
611 // This element is from left hand side vector operand.
Craig Topper2ea22b02013-01-18 05:09:16 +0000612 //
Eli Friedmance818272011-10-21 19:06:29 +0000613 // If LHS is going to be replaced (case 1, 2, or 4), calculate the
614 // new mask value for the element.
615 if (newLHS != LHS) {
616 eltMask = LHSMask[Mask[i]];
617 // If the value selected is an undef value, explicitly specify it
618 // with a -1 mask value.
619 if (eltMask >= (int)LHSOp0Width && isa<UndefValue>(LHSOp1))
620 eltMask = -1;
Craig Topper2ea22b02013-01-18 05:09:16 +0000621 } else
Eli Friedmance818272011-10-21 19:06:29 +0000622 eltMask = Mask[i];
623 } else {
624 // This element is from right hand side vector operand
625 //
626 // If the value selected is an undef value, explicitly specify it
627 // with a -1 mask value. (case 1)
628 if (isa<UndefValue>(RHS))
629 eltMask = -1;
630 // If RHS is going to be replaced (case 3 or 4), calculate the
631 // new mask value for the element.
632 else if (newRHS != RHS) {
633 eltMask = RHSMask[Mask[i]-LHSWidth];
634 // If the value selected is an undef value, explicitly specify it
635 // with a -1 mask value.
636 if (eltMask >= (int)RHSOp0Width) {
637 assert(isa<UndefValue>(RHSShuffle->getOperand(1))
638 && "should have been check above");
639 eltMask = -1;
Nate Begeman2a0ca3e92010-08-13 00:17:53 +0000640 }
Craig Topper2ea22b02013-01-18 05:09:16 +0000641 } else
Eli Friedmance818272011-10-21 19:06:29 +0000642 eltMask = Mask[i]-LHSWidth;
643
644 // If LHS's width is changed, shift the mask value accordingly.
645 // If newRHS == NULL, i.e. LHSOp0 == RHSOp0, we want to remap any
Michael Gottesman02a11412012-10-16 21:29:38 +0000646 // references from RHSOp0 to LHSOp0, so we don't need to shift the mask.
647 // If newRHS == newLHS, we want to remap any references from newRHS to
648 // newLHS so that we can properly identify splats that may occur due to
649 // obfuscation accross the two vectors.
650 if (eltMask >= 0 && newRHS != NULL && newLHS != newRHS)
Eli Friedmance818272011-10-21 19:06:29 +0000651 eltMask += newLHSWidth;
Nate Begeman2a0ca3e92010-08-13 00:17:53 +0000652 }
Eli Friedmance818272011-10-21 19:06:29 +0000653
654 // Check if this could still be a splat.
655 if (eltMask >= 0) {
656 if (SplatElt >= 0 && SplatElt != eltMask)
657 isSplat = false;
658 SplatElt = eltMask;
659 }
660
661 newMask.push_back(eltMask);
662 }
663
664 // If the result mask is equal to one of the original shuffle masks,
665 // or is a splat, do the replacement.
666 if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) {
667 SmallVector<Constant*, 16> Elts;
668 Type *Int32Ty = Type::getInt32Ty(SVI.getContext());
669 for (unsigned i = 0, e = newMask.size(); i != e; ++i) {
670 if (newMask[i] < 0) {
671 Elts.push_back(UndefValue::get(Int32Ty));
672 } else {
673 Elts.push_back(ConstantInt::get(Int32Ty, newMask[i]));
674 }
675 }
676 if (newRHS == NULL)
677 newRHS = UndefValue::get(newLHS->getType());
678 return new ShuffleVectorInst(newLHS, newRHS, ConstantVector::get(Elts));
Nate Begeman2a0ca3e92010-08-13 00:17:53 +0000679 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000680
Chris Lattnerec97a902010-01-05 05:36:20 +0000681 return MadeChange ? &SVI : 0;
682}