blob: af072b94e359480758cc0764f3a8430034d3fc21 [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"
16using namespace llvm;
17
18/// CheapToScalarize - Return true if the value is cheaper to scalarize than it
19/// is to leave as a vector operation.
20static bool CheapToScalarize(Value *V, bool isConstant) {
Bob Wilson8ecf98b2010-10-29 22:20:43 +000021 if (isa<ConstantAggregateZero>(V))
Chris Lattnerec97a902010-01-05 05:36:20 +000022 return true;
23 if (ConstantVector *C = dyn_cast<ConstantVector>(V)) {
24 if (isConstant) return true;
25 // If all elts are the same, we can extract.
26 Constant *Op0 = C->getOperand(0);
27 for (unsigned i = 1; i < C->getNumOperands(); ++i)
28 if (C->getOperand(i) != Op0)
29 return false;
30 return true;
31 }
32 Instruction *I = dyn_cast<Instruction>(V);
33 if (!I) return false;
Bob Wilson8ecf98b2010-10-29 22:20:43 +000034
Chris Lattnerec97a902010-01-05 05:36:20 +000035 // Insert element gets simplified to the inserted element or is deleted if
36 // this is constant idx extract element and its a constant idx insertelt.
37 if (I->getOpcode() == Instruction::InsertElement && isConstant &&
38 isa<ConstantInt>(I->getOperand(2)))
39 return true;
40 if (I->getOpcode() == Instruction::Load && I->hasOneUse())
41 return true;
42 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I))
43 if (BO->hasOneUse() &&
44 (CheapToScalarize(BO->getOperand(0), isConstant) ||
45 CheapToScalarize(BO->getOperand(1), isConstant)))
46 return true;
47 if (CmpInst *CI = dyn_cast<CmpInst>(I))
48 if (CI->hasOneUse() &&
49 (CheapToScalarize(CI->getOperand(0), isConstant) ||
50 CheapToScalarize(CI->getOperand(1), isConstant)))
51 return true;
Bob Wilson8ecf98b2010-10-29 22:20:43 +000052
Chris Lattnerec97a902010-01-05 05:36:20 +000053 return false;
54}
55
Bob Wilson11ee4562010-10-29 22:03:05 +000056/// getShuffleMask - Read and decode a shufflevector mask.
57/// Turn undef elements into negative values.
58static std::vector<int> getShuffleMask(const ShuffleVectorInst *SVI) {
Chris Lattnerec97a902010-01-05 05:36:20 +000059 unsigned NElts = SVI->getType()->getNumElements();
60 if (isa<ConstantAggregateZero>(SVI->getOperand(2)))
Bob Wilson11ee4562010-10-29 22:03:05 +000061 return std::vector<int>(NElts, 0);
Chris Lattnerec97a902010-01-05 05:36:20 +000062 if (isa<UndefValue>(SVI->getOperand(2)))
Bob Wilson11ee4562010-10-29 22:03:05 +000063 return std::vector<int>(NElts, -1);
Bob Wilson8ecf98b2010-10-29 22:20:43 +000064
Bob Wilson11ee4562010-10-29 22:03:05 +000065 std::vector<int> Result;
Chris Lattnerec97a902010-01-05 05:36:20 +000066 const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2));
67 for (User::const_op_iterator i = CP->op_begin(), e = CP->op_end(); i!=e; ++i)
68 if (isa<UndefValue>(*i))
Bob Wilson11ee4562010-10-29 22:03:05 +000069 Result.push_back(-1); // undef
Chris Lattnerec97a902010-01-05 05:36:20 +000070 else
71 Result.push_back(cast<ConstantInt>(*i)->getZExtValue());
72 return Result;
73}
74
75/// FindScalarElement - Given a vector and an element number, see if the scalar
76/// value is already around as a register, for example if it were inserted then
77/// extracted from the vector.
78static Value *FindScalarElement(Value *V, unsigned EltNo) {
Duncan Sands19d0b472010-02-16 11:11:14 +000079 assert(V->getType()->isVectorTy() && "Not looking at a vector?");
Chris Lattnerec97a902010-01-05 05:36:20 +000080 const VectorType *PTy = cast<VectorType>(V->getType());
81 unsigned Width = PTy->getNumElements();
82 if (EltNo >= Width) // Out of range access.
83 return UndefValue::get(PTy->getElementType());
Bob Wilson8ecf98b2010-10-29 22:20:43 +000084
Chris Lattnerec97a902010-01-05 05:36:20 +000085 if (isa<UndefValue>(V))
86 return UndefValue::get(PTy->getElementType());
Chris Lattner841af4f2010-01-05 05:42:08 +000087 if (isa<ConstantAggregateZero>(V))
Chris Lattnerec97a902010-01-05 05:36:20 +000088 return Constant::getNullValue(PTy->getElementType());
Chris Lattner841af4f2010-01-05 05:42:08 +000089 if (ConstantVector *CP = dyn_cast<ConstantVector>(V))
Chris Lattnerec97a902010-01-05 05:36:20 +000090 return CP->getOperand(EltNo);
Bob Wilson8ecf98b2010-10-29 22:20:43 +000091
Chris Lattner841af4f2010-01-05 05:42:08 +000092 if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +000093 // If this is an insert to a variable element, we don't know what it is.
Bob Wilson8ecf98b2010-10-29 22:20:43 +000094 if (!isa<ConstantInt>(III->getOperand(2)))
Chris Lattnerec97a902010-01-05 05:36:20 +000095 return 0;
96 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +000097
Chris Lattnerec97a902010-01-05 05:36:20 +000098 // If this is an insert to the element we are looking for, return the
99 // inserted value.
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000100 if (EltNo == IIElt)
Chris Lattnerec97a902010-01-05 05:36:20 +0000101 return III->getOperand(1);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000102
Chris Lattnerec97a902010-01-05 05:36:20 +0000103 // Otherwise, the insertelement doesn't modify the value, recurse on its
104 // vector input.
105 return FindScalarElement(III->getOperand(0), EltNo);
Chris Lattner841af4f2010-01-05 05:42:08 +0000106 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000107
Chris Lattner841af4f2010-01-05 05:42:08 +0000108 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000109 unsigned LHSWidth =
Bob Wilson11ee4562010-10-29 22:03:05 +0000110 cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements();
111 int InEl = getShuffleMask(SVI)[EltNo];
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000112 if (InEl < 0)
Chris Lattnerec97a902010-01-05 05:36:20 +0000113 return UndefValue::get(PTy->getElementType());
Bob Wilson11ee4562010-10-29 22:03:05 +0000114 if (InEl < (int)LHSWidth)
115 return FindScalarElement(SVI->getOperand(0), InEl);
116 return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth);
Chris Lattnerec97a902010-01-05 05:36:20 +0000117 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000118
Chris Lattnerec97a902010-01-05 05:36:20 +0000119 // Otherwise, we don't know.
120 return 0;
121}
122
123Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
124 // If vector val is undef, replace extract with scalar undef.
125 if (isa<UndefValue>(EI.getOperand(0)))
126 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000127
Chris Lattnerec97a902010-01-05 05:36:20 +0000128 // If vector val is constant 0, replace extract with scalar 0.
129 if (isa<ConstantAggregateZero>(EI.getOperand(0)))
130 return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType()));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000131
Chris Lattnerec97a902010-01-05 05:36:20 +0000132 if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) {
133 // If vector val is constant with all elements the same, replace EI with
134 // that element. When the elements are not identical, we cannot replace yet
135 // (we do that below, but only when the index is constant).
136 Constant *op0 = C->getOperand(0);
137 for (unsigned i = 1; i != C->getNumOperands(); ++i)
138 if (C->getOperand(i) != op0) {
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000139 op0 = 0;
Chris Lattnerec97a902010-01-05 05:36:20 +0000140 break;
141 }
142 if (op0)
143 return ReplaceInstUsesWith(EI, op0);
144 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000145
Chris Lattnerec97a902010-01-05 05:36:20 +0000146 // If extracting a specified index from the vector, see if we can recursively
147 // find a previously computed scalar that was inserted into the vector.
148 if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) {
149 unsigned IndexVal = IdxC->getZExtValue();
150 unsigned VectorWidth = EI.getVectorOperandType()->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000151
Chris Lattnerec97a902010-01-05 05:36:20 +0000152 // If this is extracting an invalid index, turn this into undef, to avoid
153 // crashing the code below.
154 if (IndexVal >= VectorWidth)
155 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000156
Chris Lattnerec97a902010-01-05 05:36:20 +0000157 // This instruction only demands the single element from the input vector.
158 // If the input vector has a single use, simplify it based on this use
159 // property.
160 if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
161 APInt UndefElts(VectorWidth, 0);
Chris Lattnerb22423c2010-02-08 23:56:03 +0000162 APInt DemandedMask(VectorWidth, 0);
163 DemandedMask.set(IndexVal);
Chris Lattnerec97a902010-01-05 05:36:20 +0000164 if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0),
165 DemandedMask, UndefElts)) {
166 EI.setOperand(0, V);
167 return &EI;
168 }
169 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000170
Chris Lattnerec97a902010-01-05 05:36:20 +0000171 if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal))
172 return ReplaceInstUsesWith(EI, Elt);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000173
Chris Lattnerec97a902010-01-05 05:36:20 +0000174 // If the this extractelement is directly using a bitcast from a vector of
175 // the same number of elements, see if we can find the source element from
176 // it. In this case, we will end up needing to bitcast the scalars.
177 if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) {
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000178 if (const VectorType *VT =
Chris Lattnerec97a902010-01-05 05:36:20 +0000179 dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
180 if (VT->getNumElements() == VectorWidth)
181 if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal))
182 return new BitCastInst(Elt, EI.getType());
183 }
184 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000185
Chris Lattnerec97a902010-01-05 05:36:20 +0000186 if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) {
187 // Push extractelement into predecessor operation if legal and
188 // profitable to do so
189 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
190 if (I->hasOneUse() &&
191 CheapToScalarize(BO, isa<ConstantInt>(EI.getOperand(1)))) {
192 Value *newEI0 =
Bob Wilson67a6f322010-10-29 22:20:45 +0000193 Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1),
194 EI.getName()+".lhs");
Chris Lattnerec97a902010-01-05 05:36:20 +0000195 Value *newEI1 =
Bob Wilson67a6f322010-10-29 22:20:45 +0000196 Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1),
197 EI.getName()+".rhs");
Chris Lattnerec97a902010-01-05 05:36:20 +0000198 return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1);
199 }
200 } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
201 // Extracting the inserted element?
202 if (IE->getOperand(2) == EI.getOperand(1))
203 return ReplaceInstUsesWith(EI, IE->getOperand(1));
204 // If the inserted and extracted elements are constants, they must not
205 // be the same value, extract from the pre-inserted value instead.
206 if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) {
207 Worklist.AddValue(EI.getOperand(0));
208 EI.setOperand(0, IE->getOperand(0));
209 return &EI;
210 }
211 } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) {
212 // If this is extracting an element from a shufflevector, figure out where
213 // it came from and extract from the appropriate input element instead.
214 if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) {
Bob Wilson11ee4562010-10-29 22:03:05 +0000215 int SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()];
Chris Lattnerec97a902010-01-05 05:36:20 +0000216 Value *Src;
217 unsigned LHSWidth =
Bob Wilson11ee4562010-10-29 22:03:05 +0000218 cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000219
Bob Wilson11ee4562010-10-29 22:03:05 +0000220 if (SrcIdx < 0)
221 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
222 if (SrcIdx < (int)LHSWidth)
Chris Lattnerec97a902010-01-05 05:36:20 +0000223 Src = SVI->getOperand(0);
Bob Wilson11ee4562010-10-29 22:03:05 +0000224 else {
Chris Lattnerec97a902010-01-05 05:36:20 +0000225 SrcIdx -= LHSWidth;
226 Src = SVI->getOperand(1);
Chris Lattnerec97a902010-01-05 05:36:20 +0000227 }
Bob Wilson9d07f392010-10-29 22:03:07 +0000228 const Type *Int32Ty = Type::getInt32Ty(EI.getContext());
Chris Lattnerec97a902010-01-05 05:36:20 +0000229 return ExtractElementInst::Create(Src,
Bob Wilson9d07f392010-10-29 22:03:07 +0000230 ConstantInt::get(Int32Ty,
Chris Lattnerec97a902010-01-05 05:36:20 +0000231 SrcIdx, false));
232 }
233 }
234 // FIXME: Canonicalize extractelement(bitcast) -> bitcast(extractelement)
235 }
236 return 0;
237}
238
239/// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000240/// elements from either LHS or RHS, return the shuffle mask and true.
Chris Lattnerec97a902010-01-05 05:36:20 +0000241/// Otherwise, return false.
242static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
243 std::vector<Constant*> &Mask) {
244 assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
245 "Invalid CollectSingleShuffleElements");
246 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000247
Chris Lattnerec97a902010-01-05 05:36:20 +0000248 if (isa<UndefValue>(V)) {
249 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
250 return true;
251 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000252
Chris Lattnerec97a902010-01-05 05:36:20 +0000253 if (V == LHS) {
254 for (unsigned i = 0; i != NumElts; ++i)
255 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
256 return true;
257 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000258
Chris Lattnerec97a902010-01-05 05:36:20 +0000259 if (V == RHS) {
260 for (unsigned i = 0; i != NumElts; ++i)
261 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()),
262 i+NumElts));
263 return true;
264 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000265
Chris Lattnerec97a902010-01-05 05:36:20 +0000266 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
267 // If this is an insert of an extract from some other vector, include it.
268 Value *VecOp = IEI->getOperand(0);
269 Value *ScalarOp = IEI->getOperand(1);
270 Value *IdxOp = IEI->getOperand(2);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000271
Chris Lattnerec97a902010-01-05 05:36:20 +0000272 if (!isa<ConstantInt>(IdxOp))
273 return false;
274 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000275
Chris Lattnerec97a902010-01-05 05:36:20 +0000276 if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector.
277 // Okay, we can handle this if the vector we are insertinting into is
278 // transitively ok.
279 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
280 // If so, update the mask to reflect the inserted undef.
281 Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext()));
282 return true;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000283 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000284 } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
285 if (isa<ConstantInt>(EI->getOperand(1)) &&
286 EI->getOperand(0)->getType() == V->getType()) {
287 unsigned ExtractedIdx =
288 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000289
Chris Lattnerec97a902010-01-05 05:36:20 +0000290 // This must be extracting from either LHS or RHS.
291 if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
292 // Okay, we can handle this if the vector we are insertinting into is
293 // transitively ok.
294 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
295 // If so, update the mask to reflect the inserted value.
296 if (EI->getOperand(0) == LHS) {
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000297 Mask[InsertedIdx % NumElts] =
Chris Lattnerec97a902010-01-05 05:36:20 +0000298 ConstantInt::get(Type::getInt32Ty(V->getContext()),
299 ExtractedIdx);
300 } else {
301 assert(EI->getOperand(0) == RHS);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000302 Mask[InsertedIdx % NumElts] =
Chris Lattnerec97a902010-01-05 05:36:20 +0000303 ConstantInt::get(Type::getInt32Ty(V->getContext()),
304 ExtractedIdx+NumElts);
Chris Lattnerec97a902010-01-05 05:36:20 +0000305 }
306 return true;
307 }
308 }
309 }
310 }
311 }
312 // TODO: Handle shufflevector here!
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000313
Chris Lattnerec97a902010-01-05 05:36:20 +0000314 return false;
315}
316
317/// CollectShuffleElements - We are building a shuffle of V, using RHS as the
318/// RHS of the shuffle instruction, if it is not null. Return a shuffle mask
319/// that computes V and the LHS value of the shuffle.
320static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask,
321 Value *&RHS) {
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000322 assert(V->getType()->isVectorTy() &&
Chris Lattnerec97a902010-01-05 05:36:20 +0000323 (RHS == 0 || V->getType() == RHS->getType()) &&
324 "Invalid shuffle!");
325 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000326
Chris Lattnerec97a902010-01-05 05:36:20 +0000327 if (isa<UndefValue>(V)) {
328 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
329 return V;
330 } else if (isa<ConstantAggregateZero>(V)) {
331 Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0));
332 return V;
333 } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
334 // If this is an insert of an extract from some other vector, include it.
335 Value *VecOp = IEI->getOperand(0);
336 Value *ScalarOp = IEI->getOperand(1);
337 Value *IdxOp = IEI->getOperand(2);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000338
Chris Lattnerec97a902010-01-05 05:36:20 +0000339 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
340 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
341 EI->getOperand(0)->getType() == V->getType()) {
342 unsigned ExtractedIdx =
Bob Wilson67a6f322010-10-29 22:20:45 +0000343 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Chris Lattnerec97a902010-01-05 05:36:20 +0000344 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000345
Chris Lattnerec97a902010-01-05 05:36:20 +0000346 // Either the extracted from or inserted into vector must be RHSVec,
347 // otherwise we'd end up with a shuffle of three inputs.
348 if (EI->getOperand(0) == RHS || RHS == 0) {
349 RHS = EI->getOperand(0);
350 Value *V = CollectShuffleElements(VecOp, Mask, RHS);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000351 Mask[InsertedIdx % NumElts] =
Bob Wilson67a6f322010-10-29 22:20:45 +0000352 ConstantInt::get(Type::getInt32Ty(V->getContext()),
353 NumElts+ExtractedIdx);
Chris Lattnerec97a902010-01-05 05:36:20 +0000354 return V;
355 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000356
Chris Lattnerec97a902010-01-05 05:36:20 +0000357 if (VecOp == RHS) {
358 Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS);
359 // Everything but the extracted element is replaced with the RHS.
360 for (unsigned i = 0; i != NumElts; ++i) {
361 if (i != InsertedIdx)
362 Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()),
363 NumElts+i);
364 }
365 return V;
366 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000367
Chris Lattnerec97a902010-01-05 05:36:20 +0000368 // If this insertelement is a chain that comes from exactly these two
369 // vectors, return the vector and the effective shuffle.
370 if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask))
371 return EI->getOperand(0);
372 }
373 }
374 }
375 // TODO: Handle shufflevector here!
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000376
Chris Lattnerec97a902010-01-05 05:36:20 +0000377 // Otherwise, can't do anything fancy. Return an identity vector.
378 for (unsigned i = 0; i != NumElts; ++i)
379 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
380 return V;
381}
382
383Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
384 Value *VecOp = IE.getOperand(0);
385 Value *ScalarOp = IE.getOperand(1);
386 Value *IdxOp = IE.getOperand(2);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000387
Chris Lattnerec97a902010-01-05 05:36:20 +0000388 // Inserting an undef or into an undefined place, remove this.
389 if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
390 ReplaceInstUsesWith(IE, VecOp);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000391
392 // If the inserted element was extracted from some other vector, and if the
Chris Lattnerec97a902010-01-05 05:36:20 +0000393 // indexes are constant, try to turn this into a shufflevector operation.
394 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
395 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
396 EI->getOperand(0)->getType() == IE.getType()) {
397 unsigned NumVectorElts = IE.getType()->getNumElements();
398 unsigned ExtractedIdx =
Bob Wilson67a6f322010-10-29 22:20:45 +0000399 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Chris Lattnerec97a902010-01-05 05:36:20 +0000400 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000401
Chris Lattnerec97a902010-01-05 05:36:20 +0000402 if (ExtractedIdx >= NumVectorElts) // Out of range extract.
403 return ReplaceInstUsesWith(IE, VecOp);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000404
Chris Lattnerec97a902010-01-05 05:36:20 +0000405 if (InsertedIdx >= NumVectorElts) // Out of range insert.
406 return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000407
Chris Lattnerec97a902010-01-05 05:36:20 +0000408 // If we are extracting a value from a vector, then inserting it right
409 // back into the same place, just use the input vector.
410 if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000411 return ReplaceInstUsesWith(IE, VecOp);
412
Chris Lattnerec97a902010-01-05 05:36:20 +0000413 // If this insertelement isn't used by some other insertelement, turn it
414 // (and any insertelements it points to), into one big shuffle.
415 if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) {
416 std::vector<Constant*> Mask;
417 Value *RHS = 0;
418 Value *LHS = CollectShuffleElements(&IE, Mask, RHS);
419 if (RHS == 0) RHS = UndefValue::get(LHS->getType());
420 // We now have a shuffle of LHS, RHS, Mask.
Bob Wilson67a6f322010-10-29 22:20:45 +0000421 return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask));
Chris Lattnerec97a902010-01-05 05:36:20 +0000422 }
423 }
424 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000425
Chris Lattnerec97a902010-01-05 05:36:20 +0000426 unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements();
427 APInt UndefElts(VWidth, 0);
428 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
429 if (SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts))
430 return &IE;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000431
Chris Lattnerec97a902010-01-05 05:36:20 +0000432 return 0;
433}
434
435
436Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
437 Value *LHS = SVI.getOperand(0);
438 Value *RHS = SVI.getOperand(1);
Bob Wilson11ee4562010-10-29 22:03:05 +0000439 std::vector<int> Mask = getShuffleMask(&SVI);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000440
Chris Lattnerec97a902010-01-05 05:36:20 +0000441 bool MadeChange = false;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000442
Chris Lattnerec97a902010-01-05 05:36:20 +0000443 // Undefined shuffle mask -> undefined value.
444 if (isa<UndefValue>(SVI.getOperand(2)))
445 return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000446
Eric Christopher51edc7b2010-08-17 22:55:27 +0000447 unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000448
Eric Christopher51edc7b2010-08-17 22:55:27 +0000449 if (VWidth != cast<VectorType>(LHS->getType())->getNumElements())
450 return 0;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000451
Chris Lattnerec97a902010-01-05 05:36:20 +0000452 APInt UndefElts(VWidth, 0);
453 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
454 if (SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
455 LHS = SVI.getOperand(0);
456 RHS = SVI.getOperand(1);
457 MadeChange = true;
458 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000459
Chris Lattnerec97a902010-01-05 05:36:20 +0000460 // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask')
461 // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
462 if (LHS == RHS || isa<UndefValue>(LHS)) {
Eric Christopher51edc7b2010-08-17 22:55:27 +0000463 if (isa<UndefValue>(LHS) && LHS == RHS) {
464 // shuffle(undef,undef,mask) -> undef.
465 return ReplaceInstUsesWith(SVI, LHS);
466 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000467
Chris Lattnerec97a902010-01-05 05:36:20 +0000468 // Remap any references to RHS to use LHS.
469 std::vector<Constant*> Elts;
Eric Christopher51edc7b2010-08-17 22:55:27 +0000470 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
Bob Wilson11ee4562010-10-29 22:03:05 +0000471 if (Mask[i] < 0)
Chris Lattnerec97a902010-01-05 05:36:20 +0000472 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
473 else {
Bob Wilson11ee4562010-10-29 22:03:05 +0000474 if ((Mask[i] >= (int)e && isa<UndefValue>(RHS)) ||
475 (Mask[i] < (int)e && isa<UndefValue>(LHS))) {
476 Mask[i] = -1; // Turn into undef.
Chris Lattnerec97a902010-01-05 05:36:20 +0000477 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
478 } else {
479 Mask[i] = Mask[i] % e; // Force to LHS.
480 Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()),
481 Mask[i]));
482 }
483 }
484 }
485 SVI.setOperand(0, SVI.getOperand(1));
486 SVI.setOperand(1, UndefValue::get(RHS->getType()));
487 SVI.setOperand(2, ConstantVector::get(Elts));
488 LHS = SVI.getOperand(0);
489 RHS = SVI.getOperand(1);
490 MadeChange = true;
491 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000492
Chris Lattnerec97a902010-01-05 05:36:20 +0000493 // Analyze the shuffle, are the LHS or RHS and identity shuffles?
Eric Christopher51edc7b2010-08-17 22:55:27 +0000494 bool isLHSID = true, isRHSID = true;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000495
Eric Christopher51edc7b2010-08-17 22:55:27 +0000496 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
Bob Wilson11ee4562010-10-29 22:03:05 +0000497 if (Mask[i] < 0) continue; // Ignore undef values.
Eric Christopher51edc7b2010-08-17 22:55:27 +0000498 // Is this an identity shuffle of the LHS value?
Bob Wilson11ee4562010-10-29 22:03:05 +0000499 isLHSID &= (Mask[i] == (int)i);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000500
Eric Christopher51edc7b2010-08-17 22:55:27 +0000501 // Is this an identity shuffle of the RHS value?
502 isRHSID &= (Mask[i]-e == i);
503 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000504
Eric Christopher51edc7b2010-08-17 22:55:27 +0000505 // Eliminate identity shuffles.
506 if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
507 if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000508
Eric Christopher51edc7b2010-08-17 22:55:27 +0000509 // If the LHS is a shufflevector itself, see if we can combine it with this
510 // one without producing an unusual shuffle. Here we are really conservative:
511 // we are absolutely afraid of producing a shuffle mask not in the input
512 // program, because the code gen may not be smart enough to turn a merged
513 // shuffle into two specific shuffles: it may produce worse code. As such,
Bob Wilsoncb11b482010-10-29 22:02:50 +0000514 // we only merge two shuffles if the result is either a splat or one of the
515 // two input shuffle masks. In this case, merging the shuffles just removes
516 // one instruction, which we know is safe. This is good for things like
517 // turning: (splat(splat)) -> splat.
Eric Christopher51edc7b2010-08-17 22:55:27 +0000518 if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) {
519 if (isa<UndefValue>(RHS)) {
Bob Wilson11ee4562010-10-29 22:03:05 +0000520 std::vector<int> LHSMask = getShuffleMask(LHSSVI);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000521
Eric Christopher51edc7b2010-08-17 22:55:27 +0000522 if (LHSMask.size() == Mask.size()) {
Bob Wilson11ee4562010-10-29 22:03:05 +0000523 std::vector<int> NewMask;
Bob Wilsoncb11b482010-10-29 22:02:50 +0000524 bool isSplat = true;
Bob Wilson11ee4562010-10-29 22:03:05 +0000525 int SplatElt = -1; // undef
Bob Wilsoncb11b482010-10-29 22:02:50 +0000526 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
Bob Wilson11ee4562010-10-29 22:03:05 +0000527 int MaskElt;
528 if (Mask[i] < 0 || Mask[i] >= (int)e)
529 MaskElt = -1; // undef
530 else
Bob Wilsoncb11b482010-10-29 22:02:50 +0000531 MaskElt = LHSMask[Mask[i]];
532 // Check if this could still be a splat.
Bob Wilson11ee4562010-10-29 22:03:05 +0000533 if (MaskElt >= 0) {
534 if (SplatElt >=0 && SplatElt != MaskElt)
Bob Wilsoncb11b482010-10-29 22:02:50 +0000535 isSplat = false;
536 SplatElt = MaskElt;
537 }
538 NewMask.push_back(MaskElt);
539 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000540
Eric Christopher51edc7b2010-08-17 22:55:27 +0000541 // If the result mask is equal to the src shuffle or this
542 // shuffle mask, do the replacement.
Bob Wilsoncb11b482010-10-29 22:02:50 +0000543 if (isSplat || NewMask == LHSMask || NewMask == Mask) {
Eric Christopher51edc7b2010-08-17 22:55:27 +0000544 std::vector<Constant*> Elts;
Bob Wilsoncb11b482010-10-29 22:02:50 +0000545 const Type *Int32Ty = Type::getInt32Ty(SVI.getContext());
Eric Christopher51edc7b2010-08-17 22:55:27 +0000546 for (unsigned i = 0, e = NewMask.size(); i != e; ++i) {
Bob Wilson11ee4562010-10-29 22:03:05 +0000547 if (NewMask[i] < 0) {
Bob Wilsoncb11b482010-10-29 22:02:50 +0000548 Elts.push_back(UndefValue::get(Int32Ty));
Eric Christopher51edc7b2010-08-17 22:55:27 +0000549 } else {
Bob Wilsoncb11b482010-10-29 22:02:50 +0000550 Elts.push_back(ConstantInt::get(Int32Ty, NewMask[i]));
Eric Christopher51edc7b2010-08-17 22:55:27 +0000551 }
552 }
553 return new ShuffleVectorInst(LHSSVI->getOperand(0),
554 LHSSVI->getOperand(1),
555 ConstantVector::get(Elts));
Nate Begeman2a0ca3e92010-08-13 00:17:53 +0000556 }
557 }
Nate Begeman2a0ca3e92010-08-13 00:17:53 +0000558 }
559 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000560
Chris Lattnerec97a902010-01-05 05:36:20 +0000561 return MadeChange ? &SVI : 0;
562}