LLVM API Documentation
00001 //===- InstCombineVectorOps.cpp -------------------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements instcombine for ExtractElement, InsertElement and 00011 // ShuffleVector. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "InstCombine.h" 00016 #include "llvm/Support/PatternMatch.h" 00017 using namespace llvm; 00018 using namespace PatternMatch; 00019 00020 /// CheapToScalarize - Return true if the value is cheaper to scalarize than it 00021 /// is to leave as a vector operation. isConstant indicates whether we're 00022 /// extracting one known element. If false we're extracting a variable index. 00023 static bool CheapToScalarize(Value *V, bool isConstant) { 00024 if (Constant *C = dyn_cast<Constant>(V)) { 00025 if (isConstant) return true; 00026 00027 // If all elts are the same, we can extract it and use any of the values. 00028 Constant *Op0 = C->getAggregateElement(0U); 00029 for (unsigned i = 1, e = V->getType()->getVectorNumElements(); i != e; ++i) 00030 if (C->getAggregateElement(i) != Op0) 00031 return false; 00032 return true; 00033 } 00034 Instruction *I = dyn_cast<Instruction>(V); 00035 if (!I) return false; 00036 00037 // Insert element gets simplified to the inserted element or is deleted if 00038 // this is constant idx extract element and its a constant idx insertelt. 00039 if (I->getOpcode() == Instruction::InsertElement && isConstant && 00040 isa<ConstantInt>(I->getOperand(2))) 00041 return true; 00042 if (I->getOpcode() == Instruction::Load && I->hasOneUse()) 00043 return true; 00044 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) 00045 if (BO->hasOneUse() && 00046 (CheapToScalarize(BO->getOperand(0), isConstant) || 00047 CheapToScalarize(BO->getOperand(1), isConstant))) 00048 return true; 00049 if (CmpInst *CI = dyn_cast<CmpInst>(I)) 00050 if (CI->hasOneUse() && 00051 (CheapToScalarize(CI->getOperand(0), isConstant) || 00052 CheapToScalarize(CI->getOperand(1), isConstant))) 00053 return true; 00054 00055 return false; 00056 } 00057 00058 /// FindScalarElement - Given a vector and an element number, see if the scalar 00059 /// value is already around as a register, for example if it were inserted then 00060 /// extracted from the vector. 00061 static Value *FindScalarElement(Value *V, unsigned EltNo) { 00062 assert(V->getType()->isVectorTy() && "Not looking at a vector?"); 00063 VectorType *VTy = cast<VectorType>(V->getType()); 00064 unsigned Width = VTy->getNumElements(); 00065 if (EltNo >= Width) // Out of range access. 00066 return UndefValue::get(VTy->getElementType()); 00067 00068 if (Constant *C = dyn_cast<Constant>(V)) 00069 return C->getAggregateElement(EltNo); 00070 00071 if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { 00072 // If this is an insert to a variable element, we don't know what it is. 00073 if (!isa<ConstantInt>(III->getOperand(2))) 00074 return 0; 00075 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); 00076 00077 // If this is an insert to the element we are looking for, return the 00078 // inserted value. 00079 if (EltNo == IIElt) 00080 return III->getOperand(1); 00081 00082 // Otherwise, the insertelement doesn't modify the value, recurse on its 00083 // vector input. 00084 return FindScalarElement(III->getOperand(0), EltNo); 00085 } 00086 00087 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { 00088 unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements(); 00089 int InEl = SVI->getMaskValue(EltNo); 00090 if (InEl < 0) 00091 return UndefValue::get(VTy->getElementType()); 00092 if (InEl < (int)LHSWidth) 00093 return FindScalarElement(SVI->getOperand(0), InEl); 00094 return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth); 00095 } 00096 00097 // Extract a value from a vector add operation with a constant zero. 00098 Value *Val = 0; Constant *Con = 0; 00099 if (match(V, m_Add(m_Value(Val), m_Constant(Con)))) { 00100 if (Con->getAggregateElement(EltNo)->isNullValue()) 00101 return FindScalarElement(Val, EltNo); 00102 } 00103 00104 // Otherwise, we don't know. 00105 return 0; 00106 } 00107 00108 // If we have a PHI node with a vector type that has only 2 uses: feed 00109 // itself and be an operand of extractelemnt at a constant location, 00110 // try to replace the PHI of the vector type with a PHI of a scalar type 00111 Instruction *InstCombiner::scalarizePHI(ExtractElementInst &EI, PHINode *PN) { 00112 // Verify that the PHI node has exactly 2 uses. Otherwise return NULL. 00113 if (!PN->hasNUses(2)) 00114 return NULL; 00115 00116 // If so, it's known at this point that one operand is PHI and the other is 00117 // an extractelement node. Find the PHI user that is not the extractelement 00118 // node. 00119 Value::use_iterator iu = PN->use_begin(); 00120 Instruction *PHIUser = dyn_cast<Instruction>(*iu); 00121 if (PHIUser == cast<Instruction>(&EI)) 00122 PHIUser = cast<Instruction>(*(++iu)); 00123 00124 // Verify that this PHI user has one use, which is the PHI itself, 00125 // and that it is a binary operation which is cheap to scalarize. 00126 // otherwise return NULL. 00127 if (!PHIUser->hasOneUse() || !(PHIUser->use_back() == PN) || 00128 !(isa<BinaryOperator>(PHIUser)) || 00129 !CheapToScalarize(PHIUser, true)) 00130 return NULL; 00131 00132 // Create a scalar PHI node that will replace the vector PHI node 00133 // just before the current PHI node. 00134 PHINode * scalarPHI = cast<PHINode>( 00135 InsertNewInstWith(PHINode::Create(EI.getType(), 00136 PN->getNumIncomingValues(), ""), *PN)); 00137 // Scalarize each PHI operand. 00138 for (unsigned i=0; i < PN->getNumIncomingValues(); i++) { 00139 Value *PHIInVal = PN->getIncomingValue(i); 00140 BasicBlock *inBB = PN->getIncomingBlock(i); 00141 Value *Elt = EI.getIndexOperand(); 00142 // If the operand is the PHI induction variable: 00143 if (PHIInVal == PHIUser) { 00144 // Scalarize the binary operation. Its first operand is the 00145 // scalar PHI and the second operand is extracted from the other 00146 // vector operand. 00147 BinaryOperator *B0 = cast<BinaryOperator>(PHIUser); 00148 unsigned opId = (B0->getOperand(0) == PN) ? 1: 0; 00149 Value *Op = Builder->CreateExtractElement( 00150 B0->getOperand(opId), Elt, B0->getOperand(opId)->getName()+".Elt"); 00151 Value *newPHIUser = InsertNewInstWith( 00152 BinaryOperator::Create(B0->getOpcode(), scalarPHI,Op), 00153 *B0); 00154 scalarPHI->addIncoming(newPHIUser, inBB); 00155 } else { 00156 // Scalarize PHI input: 00157 Instruction *newEI = 00158 ExtractElementInst::Create(PHIInVal, Elt, ""); 00159 // Insert the new instruction into the predecessor basic block. 00160 Instruction *pos = dyn_cast<Instruction>(PHIInVal); 00161 BasicBlock::iterator InsertPos; 00162 if (pos && !isa<PHINode>(pos)) { 00163 InsertPos = pos; 00164 ++InsertPos; 00165 } else { 00166 InsertPos = inBB->getFirstInsertionPt(); 00167 } 00168 00169 InsertNewInstWith(newEI, *InsertPos); 00170 00171 scalarPHI->addIncoming(newEI, inBB); 00172 } 00173 } 00174 return ReplaceInstUsesWith(EI, scalarPHI); 00175 } 00176 00177 Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { 00178 // If vector val is constant with all elements the same, replace EI with 00179 // that element. We handle a known element # below. 00180 if (Constant *C = dyn_cast<Constant>(EI.getOperand(0))) 00181 if (CheapToScalarize(C, false)) 00182 return ReplaceInstUsesWith(EI, C->getAggregateElement(0U)); 00183 00184 // If extracting a specified index from the vector, see if we can recursively 00185 // find a previously computed scalar that was inserted into the vector. 00186 if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { 00187 unsigned IndexVal = IdxC->getZExtValue(); 00188 unsigned VectorWidth = EI.getVectorOperandType()->getNumElements(); 00189 00190 // If this is extracting an invalid index, turn this into undef, to avoid 00191 // crashing the code below. 00192 if (IndexVal >= VectorWidth) 00193 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); 00194 00195 // This instruction only demands the single element from the input vector. 00196 // If the input vector has a single use, simplify it based on this use 00197 // property. 00198 if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { 00199 APInt UndefElts(VectorWidth, 0); 00200 APInt DemandedMask(VectorWidth, 0); 00201 DemandedMask.setBit(IndexVal); 00202 if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), 00203 DemandedMask, UndefElts)) { 00204 EI.setOperand(0, V); 00205 return &EI; 00206 } 00207 } 00208 00209 if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) 00210 return ReplaceInstUsesWith(EI, Elt); 00211 00212 // If the this extractelement is directly using a bitcast from a vector of 00213 // the same number of elements, see if we can find the source element from 00214 // it. In this case, we will end up needing to bitcast the scalars. 00215 if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { 00216 if (VectorType *VT = dyn_cast<VectorType>(BCI->getOperand(0)->getType())) 00217 if (VT->getNumElements() == VectorWidth) 00218 if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) 00219 return new BitCastInst(Elt, EI.getType()); 00220 } 00221 00222 // If there's a vector PHI feeding a scalar use through this extractelement 00223 // instruction, try to scalarize the PHI. 00224 if (PHINode *PN = dyn_cast<PHINode>(EI.getOperand(0))) { 00225 Instruction *scalarPHI = scalarizePHI(EI, PN); 00226 if (scalarPHI) 00227 return (scalarPHI); 00228 } 00229 } 00230 00231 if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { 00232 // Push extractelement into predecessor operation if legal and 00233 // profitable to do so 00234 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { 00235 if (I->hasOneUse() && 00236 CheapToScalarize(BO, isa<ConstantInt>(EI.getOperand(1)))) { 00237 Value *newEI0 = 00238 Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1), 00239 EI.getName()+".lhs"); 00240 Value *newEI1 = 00241 Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1), 00242 EI.getName()+".rhs"); 00243 return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1); 00244 } 00245 } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { 00246 // Extracting the inserted element? 00247 if (IE->getOperand(2) == EI.getOperand(1)) 00248 return ReplaceInstUsesWith(EI, IE->getOperand(1)); 00249 // If the inserted and extracted elements are constants, they must not 00250 // be the same value, extract from the pre-inserted value instead. 00251 if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) { 00252 Worklist.AddValue(EI.getOperand(0)); 00253 EI.setOperand(0, IE->getOperand(0)); 00254 return &EI; 00255 } 00256 } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { 00257 // If this is extracting an element from a shufflevector, figure out where 00258 // it came from and extract from the appropriate input element instead. 00259 if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { 00260 int SrcIdx = SVI->getMaskValue(Elt->getZExtValue()); 00261 Value *Src; 00262 unsigned LHSWidth = 00263 SVI->getOperand(0)->getType()->getVectorNumElements(); 00264 00265 if (SrcIdx < 0) 00266 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); 00267 if (SrcIdx < (int)LHSWidth) 00268 Src = SVI->getOperand(0); 00269 else { 00270 SrcIdx -= LHSWidth; 00271 Src = SVI->getOperand(1); 00272 } 00273 Type *Int32Ty = Type::getInt32Ty(EI.getContext()); 00274 return ExtractElementInst::Create(Src, 00275 ConstantInt::get(Int32Ty, 00276 SrcIdx, false)); 00277 } 00278 } else if (CastInst *CI = dyn_cast<CastInst>(I)) { 00279 // Canonicalize extractelement(cast) -> cast(extractelement) 00280 // bitcasts can change the number of vector elements and they cost nothing 00281 if (CI->hasOneUse() && (CI->getOpcode() != Instruction::BitCast)) { 00282 Value *EE = Builder->CreateExtractElement(CI->getOperand(0), 00283 EI.getIndexOperand()); 00284 Worklist.AddValue(EE); 00285 return CastInst::Create(CI->getOpcode(), EE, EI.getType()); 00286 } 00287 } 00288 } 00289 return 0; 00290 } 00291 00292 /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns 00293 /// elements from either LHS or RHS, return the shuffle mask and true. 00294 /// Otherwise, return false. 00295 static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, 00296 SmallVectorImpl<Constant*> &Mask) { 00297 assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && 00298 "Invalid CollectSingleShuffleElements"); 00299 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); 00300 00301 if (isa<UndefValue>(V)) { 00302 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); 00303 return true; 00304 } 00305 00306 if (V == LHS) { 00307 for (unsigned i = 0; i != NumElts; ++i) 00308 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); 00309 return true; 00310 } 00311 00312 if (V == RHS) { 00313 for (unsigned i = 0; i != NumElts; ++i) 00314 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), 00315 i+NumElts)); 00316 return true; 00317 } 00318 00319 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { 00320 // If this is an insert of an extract from some other vector, include it. 00321 Value *VecOp = IEI->getOperand(0); 00322 Value *ScalarOp = IEI->getOperand(1); 00323 Value *IdxOp = IEI->getOperand(2); 00324 00325 if (!isa<ConstantInt>(IdxOp)) 00326 return false; 00327 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); 00328 00329 if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. 00330 // Okay, we can handle this if the vector we are insertinting into is 00331 // transitively ok. 00332 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { 00333 // If so, update the mask to reflect the inserted undef. 00334 Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext())); 00335 return true; 00336 } 00337 } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ 00338 if (isa<ConstantInt>(EI->getOperand(1)) && 00339 EI->getOperand(0)->getType() == V->getType()) { 00340 unsigned ExtractedIdx = 00341 cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); 00342 00343 // This must be extracting from either LHS or RHS. 00344 if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { 00345 // Okay, we can handle this if the vector we are insertinting into is 00346 // transitively ok. 00347 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { 00348 // If so, update the mask to reflect the inserted value. 00349 if (EI->getOperand(0) == LHS) { 00350 Mask[InsertedIdx % NumElts] = 00351 ConstantInt::get(Type::getInt32Ty(V->getContext()), 00352 ExtractedIdx); 00353 } else { 00354 assert(EI->getOperand(0) == RHS); 00355 Mask[InsertedIdx % NumElts] = 00356 ConstantInt::get(Type::getInt32Ty(V->getContext()), 00357 ExtractedIdx+NumElts); 00358 } 00359 return true; 00360 } 00361 } 00362 } 00363 } 00364 } 00365 // TODO: Handle shufflevector here! 00366 00367 return false; 00368 } 00369 00370 /// CollectShuffleElements - We are building a shuffle of V, using RHS as the 00371 /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask 00372 /// that computes V and the LHS value of the shuffle. 00373 static Value *CollectShuffleElements(Value *V, SmallVectorImpl<Constant*> &Mask, 00374 Value *&RHS) { 00375 assert(V->getType()->isVectorTy() && 00376 (RHS == 0 || V->getType() == RHS->getType()) && 00377 "Invalid shuffle!"); 00378 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); 00379 00380 if (isa<UndefValue>(V)) { 00381 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); 00382 return V; 00383 } 00384 00385 if (isa<ConstantAggregateZero>(V)) { 00386 Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0)); 00387 return V; 00388 } 00389 00390 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { 00391 // If this is an insert of an extract from some other vector, include it. 00392 Value *VecOp = IEI->getOperand(0); 00393 Value *ScalarOp = IEI->getOperand(1); 00394 Value *IdxOp = IEI->getOperand(2); 00395 00396 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { 00397 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && 00398 EI->getOperand(0)->getType() == V->getType()) { 00399 unsigned ExtractedIdx = 00400 cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); 00401 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); 00402 00403 // Either the extracted from or inserted into vector must be RHSVec, 00404 // otherwise we'd end up with a shuffle of three inputs. 00405 if (EI->getOperand(0) == RHS || RHS == 0) { 00406 RHS = EI->getOperand(0); 00407 Value *V = CollectShuffleElements(VecOp, Mask, RHS); 00408 Mask[InsertedIdx % NumElts] = 00409 ConstantInt::get(Type::getInt32Ty(V->getContext()), 00410 NumElts+ExtractedIdx); 00411 return V; 00412 } 00413 00414 if (VecOp == RHS) { 00415 Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); 00416 // Update Mask to reflect that `ScalarOp' has been inserted at 00417 // position `InsertedIdx' within the vector returned by IEI. 00418 Mask[InsertedIdx % NumElts] = Mask[ExtractedIdx]; 00419 00420 // Everything but the extracted element is replaced with the RHS. 00421 for (unsigned i = 0; i != NumElts; ++i) { 00422 if (i != InsertedIdx) 00423 Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()), 00424 NumElts+i); 00425 } 00426 return V; 00427 } 00428 00429 // If this insertelement is a chain that comes from exactly these two 00430 // vectors, return the vector and the effective shuffle. 00431 if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) 00432 return EI->getOperand(0); 00433 } 00434 } 00435 } 00436 // TODO: Handle shufflevector here! 00437 00438 // Otherwise, can't do anything fancy. Return an identity vector. 00439 for (unsigned i = 0; i != NumElts; ++i) 00440 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); 00441 return V; 00442 } 00443 00444 Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { 00445 Value *VecOp = IE.getOperand(0); 00446 Value *ScalarOp = IE.getOperand(1); 00447 Value *IdxOp = IE.getOperand(2); 00448 00449 // Inserting an undef or into an undefined place, remove this. 00450 if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) 00451 ReplaceInstUsesWith(IE, VecOp); 00452 00453 // If the inserted element was extracted from some other vector, and if the 00454 // indexes are constant, try to turn this into a shufflevector operation. 00455 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { 00456 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && 00457 EI->getOperand(0)->getType() == IE.getType()) { 00458 unsigned NumVectorElts = IE.getType()->getNumElements(); 00459 unsigned ExtractedIdx = 00460 cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); 00461 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); 00462 00463 if (ExtractedIdx >= NumVectorElts) // Out of range extract. 00464 return ReplaceInstUsesWith(IE, VecOp); 00465 00466 if (InsertedIdx >= NumVectorElts) // Out of range insert. 00467 return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); 00468 00469 // If we are extracting a value from a vector, then inserting it right 00470 // back into the same place, just use the input vector. 00471 if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) 00472 return ReplaceInstUsesWith(IE, VecOp); 00473 00474 // If this insertelement isn't used by some other insertelement, turn it 00475 // (and any insertelements it points to), into one big shuffle. 00476 if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { 00477 SmallVector<Constant*, 16> Mask; 00478 Value *RHS = 0; 00479 Value *LHS = CollectShuffleElements(&IE, Mask, RHS); 00480 if (RHS == 0) RHS = UndefValue::get(LHS->getType()); 00481 // We now have a shuffle of LHS, RHS, Mask. 00482 return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); 00483 } 00484 } 00485 } 00486 00487 unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements(); 00488 APInt UndefElts(VWidth, 0); 00489 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); 00490 if (Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) { 00491 if (V != &IE) 00492 return ReplaceInstUsesWith(IE, V); 00493 return &IE; 00494 } 00495 00496 return 0; 00497 } 00498 00499 00500 Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { 00501 Value *LHS = SVI.getOperand(0); 00502 Value *RHS = SVI.getOperand(1); 00503 SmallVector<int, 16> Mask = SVI.getShuffleMask(); 00504 00505 bool MadeChange = false; 00506 00507 // Undefined shuffle mask -> undefined value. 00508 if (isa<UndefValue>(SVI.getOperand(2))) 00509 return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); 00510 00511 unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements(); 00512 00513 APInt UndefElts(VWidth, 0); 00514 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); 00515 if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) { 00516 if (V != &SVI) 00517 return ReplaceInstUsesWith(SVI, V); 00518 LHS = SVI.getOperand(0); 00519 RHS = SVI.getOperand(1); 00520 MadeChange = true; 00521 } 00522 00523 unsigned LHSWidth = cast<VectorType>(LHS->getType())->getNumElements(); 00524 00525 // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') 00526 // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). 00527 if (LHS == RHS || isa<UndefValue>(LHS)) { 00528 if (isa<UndefValue>(LHS) && LHS == RHS) { 00529 // shuffle(undef,undef,mask) -> undef. 00530 Value* result = (VWidth == LHSWidth) 00531 ? LHS : UndefValue::get(SVI.getType()); 00532 return ReplaceInstUsesWith(SVI, result); 00533 } 00534 00535 // Remap any references to RHS to use LHS. 00536 SmallVector<Constant*, 16> Elts; 00537 for (unsigned i = 0, e = LHSWidth; i != VWidth; ++i) { 00538 if (Mask[i] < 0) { 00539 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext()))); 00540 continue; 00541 } 00542 00543 if ((Mask[i] >= (int)e && isa<UndefValue>(RHS)) || 00544 (Mask[i] < (int)e && isa<UndefValue>(LHS))) { 00545 Mask[i] = -1; // Turn into undef. 00546 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext()))); 00547 } else { 00548 Mask[i] = Mask[i] % e; // Force to LHS. 00549 Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()), 00550 Mask[i])); 00551 } 00552 } 00553 SVI.setOperand(0, SVI.getOperand(1)); 00554 SVI.setOperand(1, UndefValue::get(RHS->getType())); 00555 SVI.setOperand(2, ConstantVector::get(Elts)); 00556 LHS = SVI.getOperand(0); 00557 RHS = SVI.getOperand(1); 00558 MadeChange = true; 00559 } 00560 00561 if (VWidth == LHSWidth) { 00562 // Analyze the shuffle, are the LHS or RHS and identity shuffles? 00563 bool isLHSID = true, isRHSID = true; 00564 00565 for (unsigned i = 0, e = Mask.size(); i != e; ++i) { 00566 if (Mask[i] < 0) continue; // Ignore undef values. 00567 // Is this an identity shuffle of the LHS value? 00568 isLHSID &= (Mask[i] == (int)i); 00569 00570 // Is this an identity shuffle of the RHS value? 00571 isRHSID &= (Mask[i]-e == i); 00572 } 00573 00574 // Eliminate identity shuffles. 00575 if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); 00576 if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); 00577 } 00578 00579 // If the LHS is a shufflevector itself, see if we can combine it with this 00580 // one without producing an unusual shuffle. 00581 // Cases that might be simplified: 00582 // 1. 00583 // x1=shuffle(v1,v2,mask1) 00584 // x=shuffle(x1,undef,mask) 00585 // ==> 00586 // x=shuffle(v1,undef,newMask) 00587 // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : -1 00588 // 2. 00589 // x1=shuffle(v1,undef,mask1) 00590 // x=shuffle(x1,x2,mask) 00591 // where v1.size() == mask1.size() 00592 // ==> 00593 // x=shuffle(v1,x2,newMask) 00594 // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : mask[i] 00595 // 3. 00596 // x2=shuffle(v2,undef,mask2) 00597 // x=shuffle(x1,x2,mask) 00598 // where v2.size() == mask2.size() 00599 // ==> 00600 // x=shuffle(x1,v2,newMask) 00601 // newMask[i] = (mask[i] < x1.size()) 00602 // ? mask[i] : mask2[mask[i]-x1.size()]+x1.size() 00603 // 4. 00604 // x1=shuffle(v1,undef,mask1) 00605 // x2=shuffle(v2,undef,mask2) 00606 // x=shuffle(x1,x2,mask) 00607 // where v1.size() == v2.size() 00608 // ==> 00609 // x=shuffle(v1,v2,newMask) 00610 // newMask[i] = (mask[i] < x1.size()) 00611 // ? mask1[mask[i]] : mask2[mask[i]-x1.size()]+v1.size() 00612 // 00613 // Here we are really conservative: 00614 // we are absolutely afraid of producing a shuffle mask not in the input 00615 // program, because the code gen may not be smart enough to turn a merged 00616 // shuffle into two specific shuffles: it may produce worse code. As such, 00617 // we only merge two shuffles if the result is either a splat or one of the 00618 // input shuffle masks. In this case, merging the shuffles just removes 00619 // one instruction, which we know is safe. This is good for things like 00620 // turning: (splat(splat)) -> splat, or 00621 // merge(V[0..n], V[n+1..2n]) -> V[0..2n] 00622 ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS); 00623 ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS); 00624 if (LHSShuffle) 00625 if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS)) 00626 LHSShuffle = NULL; 00627 if (RHSShuffle) 00628 if (!isa<UndefValue>(RHSShuffle->getOperand(1))) 00629 RHSShuffle = NULL; 00630 if (!LHSShuffle && !RHSShuffle) 00631 return MadeChange ? &SVI : 0; 00632 00633 Value* LHSOp0 = NULL; 00634 Value* LHSOp1 = NULL; 00635 Value* RHSOp0 = NULL; 00636 unsigned LHSOp0Width = 0; 00637 unsigned RHSOp0Width = 0; 00638 if (LHSShuffle) { 00639 LHSOp0 = LHSShuffle->getOperand(0); 00640 LHSOp1 = LHSShuffle->getOperand(1); 00641 LHSOp0Width = cast<VectorType>(LHSOp0->getType())->getNumElements(); 00642 } 00643 if (RHSShuffle) { 00644 RHSOp0 = RHSShuffle->getOperand(0); 00645 RHSOp0Width = cast<VectorType>(RHSOp0->getType())->getNumElements(); 00646 } 00647 Value* newLHS = LHS; 00648 Value* newRHS = RHS; 00649 if (LHSShuffle) { 00650 // case 1 00651 if (isa<UndefValue>(RHS)) { 00652 newLHS = LHSOp0; 00653 newRHS = LHSOp1; 00654 } 00655 // case 2 or 4 00656 else if (LHSOp0Width == LHSWidth) { 00657 newLHS = LHSOp0; 00658 } 00659 } 00660 // case 3 or 4 00661 if (RHSShuffle && RHSOp0Width == LHSWidth) { 00662 newRHS = RHSOp0; 00663 } 00664 // case 4 00665 if (LHSOp0 == RHSOp0) { 00666 newLHS = LHSOp0; 00667 newRHS = NULL; 00668 } 00669 00670 if (newLHS == LHS && newRHS == RHS) 00671 return MadeChange ? &SVI : 0; 00672 00673 SmallVector<int, 16> LHSMask; 00674 SmallVector<int, 16> RHSMask; 00675 if (newLHS != LHS) 00676 LHSMask = LHSShuffle->getShuffleMask(); 00677 if (RHSShuffle && newRHS != RHS) 00678 RHSMask = RHSShuffle->getShuffleMask(); 00679 00680 unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth; 00681 SmallVector<int, 16> newMask; 00682 bool isSplat = true; 00683 int SplatElt = -1; 00684 // Create a new mask for the new ShuffleVectorInst so that the new 00685 // ShuffleVectorInst is equivalent to the original one. 00686 for (unsigned i = 0; i < VWidth; ++i) { 00687 int eltMask; 00688 if (Mask[i] < 0) { 00689 // This element is an undef value. 00690 eltMask = -1; 00691 } else if (Mask[i] < (int)LHSWidth) { 00692 // This element is from left hand side vector operand. 00693 // 00694 // If LHS is going to be replaced (case 1, 2, or 4), calculate the 00695 // new mask value for the element. 00696 if (newLHS != LHS) { 00697 eltMask = LHSMask[Mask[i]]; 00698 // If the value selected is an undef value, explicitly specify it 00699 // with a -1 mask value. 00700 if (eltMask >= (int)LHSOp0Width && isa<UndefValue>(LHSOp1)) 00701 eltMask = -1; 00702 } else 00703 eltMask = Mask[i]; 00704 } else { 00705 // This element is from right hand side vector operand 00706 // 00707 // If the value selected is an undef value, explicitly specify it 00708 // with a -1 mask value. (case 1) 00709 if (isa<UndefValue>(RHS)) 00710 eltMask = -1; 00711 // If RHS is going to be replaced (case 3 or 4), calculate the 00712 // new mask value for the element. 00713 else if (newRHS != RHS) { 00714 eltMask = RHSMask[Mask[i]-LHSWidth]; 00715 // If the value selected is an undef value, explicitly specify it 00716 // with a -1 mask value. 00717 if (eltMask >= (int)RHSOp0Width) { 00718 assert(isa<UndefValue>(RHSShuffle->getOperand(1)) 00719 && "should have been check above"); 00720 eltMask = -1; 00721 } 00722 } else 00723 eltMask = Mask[i]-LHSWidth; 00724 00725 // If LHS's width is changed, shift the mask value accordingly. 00726 // If newRHS == NULL, i.e. LHSOp0 == RHSOp0, we want to remap any 00727 // references from RHSOp0 to LHSOp0, so we don't need to shift the mask. 00728 // If newRHS == newLHS, we want to remap any references from newRHS to 00729 // newLHS so that we can properly identify splats that may occur due to 00730 // obfuscation accross the two vectors. 00731 if (eltMask >= 0 && newRHS != NULL && newLHS != newRHS) 00732 eltMask += newLHSWidth; 00733 } 00734 00735 // Check if this could still be a splat. 00736 if (eltMask >= 0) { 00737 if (SplatElt >= 0 && SplatElt != eltMask) 00738 isSplat = false; 00739 SplatElt = eltMask; 00740 } 00741 00742 newMask.push_back(eltMask); 00743 } 00744 00745 // If the result mask is equal to one of the original shuffle masks, 00746 // or is a splat, do the replacement. 00747 if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) { 00748 SmallVector<Constant*, 16> Elts; 00749 Type *Int32Ty = Type::getInt32Ty(SVI.getContext()); 00750 for (unsigned i = 0, e = newMask.size(); i != e; ++i) { 00751 if (newMask[i] < 0) { 00752 Elts.push_back(UndefValue::get(Int32Ty)); 00753 } else { 00754 Elts.push_back(ConstantInt::get(Int32Ty, newMask[i])); 00755 } 00756 } 00757 if (newRHS == NULL) 00758 newRHS = UndefValue::get(newLHS->getType()); 00759 return new ShuffleVectorInst(newLHS, newRHS, ConstantVector::get(Elts)); 00760 } 00761 00762 return MadeChange ? &SVI : 0; 00763 }