Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1 | //===- InstCombineVectorOps.cpp -------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements instcombine for ExtractElement, InsertElement and |
| 11 | // ShuffleVector. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 15 | #include "InstCombineInternal.h" |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/InstructionSimplify.h" |
| 18 | #include "llvm/Analysis/VectorUtils.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 19 | #include "llvm/IR/PatternMatch.h" |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Nadav Rotem | 7df8509 | 2013-01-15 23:43:14 +0000 | [diff] [blame] | 21 | using namespace PatternMatch; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 22 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "instcombine" |
| 24 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 25 | /// Return true if the value is cheaper to scalarize than it is to leave as a |
| 26 | /// vector operation. isConstant indicates whether we're extracting one known |
| 27 | /// element. If false we're extracting a variable index. |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 28 | static bool cheapToScalarize(Value *V, bool isConstant) { |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 29 | if (Constant *C = dyn_cast<Constant>(V)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 30 | if (isConstant) return true; |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 31 | |
| 32 | // If all elts are the same, we can extract it and use any of the values. |
Benjamin Kramer | 09b0f88 | 2014-01-24 19:02:37 +0000 | [diff] [blame] | 33 | if (Constant *Op0 = C->getAggregateElement(0U)) { |
| 34 | for (unsigned i = 1, e = V->getType()->getVectorNumElements(); i != e; |
| 35 | ++i) |
| 36 | if (C->getAggregateElement(i) != Op0) |
| 37 | return false; |
| 38 | return true; |
| 39 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 40 | } |
| 41 | Instruction *I = dyn_cast<Instruction>(V); |
| 42 | if (!I) return false; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 43 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 44 | // Insert element gets simplified to the inserted element or is deleted if |
| 45 | // this is constant idx extract element and its a constant idx insertelt. |
| 46 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 47 | isa<ConstantInt>(I->getOperand(2))) |
| 48 | return true; |
| 49 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 50 | return true; |
| 51 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 52 | if (BO->hasOneUse() && |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 53 | (cheapToScalarize(BO->getOperand(0), isConstant) || |
| 54 | cheapToScalarize(BO->getOperand(1), isConstant))) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 55 | return true; |
| 56 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 57 | if (CI->hasOneUse() && |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 58 | (cheapToScalarize(CI->getOperand(0), isConstant) || |
| 59 | cheapToScalarize(CI->getOperand(1), isConstant))) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 60 | return true; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 61 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
Michael Kuperstein | a0c6ae0 | 2016-06-06 23:38:33 +0000 | [diff] [blame] | 65 | // If we have a PHI node with a vector type that is only used to feed |
Matt Arsenault | 3887473 | 2013-08-28 22:17:26 +0000 | [diff] [blame] | 66 | // itself and be an operand of extractelement at a constant location, |
| 67 | // try to replace the PHI of the vector type with a PHI of a scalar type. |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 68 | Instruction *InstCombiner::scalarizePHI(ExtractElementInst &EI, PHINode *PN) { |
Michael Kuperstein | a0c6ae0 | 2016-06-06 23:38:33 +0000 | [diff] [blame] | 69 | SmallVector<Instruction *, 2> Extracts; |
| 70 | // The users we want the PHI to have are: |
| 71 | // 1) The EI ExtractElement (we already know this) |
| 72 | // 2) Possibly more ExtractElements with the same index. |
| 73 | // 3) Another operand, which will feed back into the PHI. |
| 74 | Instruction *PHIUser = nullptr; |
| 75 | for (auto U : PN->users()) { |
| 76 | if (ExtractElementInst *EU = dyn_cast<ExtractElementInst>(U)) { |
| 77 | if (EI.getIndexOperand() == EU->getIndexOperand()) |
| 78 | Extracts.push_back(EU); |
| 79 | else |
| 80 | return nullptr; |
| 81 | } else if (!PHIUser) { |
| 82 | PHIUser = cast<Instruction>(U); |
| 83 | } else { |
| 84 | return nullptr; |
| 85 | } |
| 86 | } |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 87 | |
Michael Kuperstein | a0c6ae0 | 2016-06-06 23:38:33 +0000 | [diff] [blame] | 88 | if (!PHIUser) |
| 89 | return nullptr; |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 90 | |
| 91 | // Verify that this PHI user has one use, which is the PHI itself, |
| 92 | // and that it is a binary operation which is cheap to scalarize. |
| 93 | // otherwise return NULL. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 94 | if (!PHIUser->hasOneUse() || !(PHIUser->user_back() == PN) || |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 95 | !(isa<BinaryOperator>(PHIUser)) || !cheapToScalarize(PHIUser, true)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 96 | return nullptr; |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 97 | |
| 98 | // Create a scalar PHI node that will replace the vector PHI node |
| 99 | // just before the current PHI node. |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 100 | PHINode *scalarPHI = cast<PHINode>(InsertNewInstWith( |
| 101 | PHINode::Create(EI.getType(), PN->getNumIncomingValues(), ""), *PN)); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 102 | // Scalarize each PHI operand. |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 103 | for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) { |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 104 | Value *PHIInVal = PN->getIncomingValue(i); |
| 105 | BasicBlock *inBB = PN->getIncomingBlock(i); |
| 106 | Value *Elt = EI.getIndexOperand(); |
| 107 | // If the operand is the PHI induction variable: |
| 108 | if (PHIInVal == PHIUser) { |
| 109 | // Scalarize the binary operation. Its first operand is the |
Sanjay Patel | 70af1fd | 2014-07-07 22:13:58 +0000 | [diff] [blame] | 110 | // scalar PHI, and the second operand is extracted from the other |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 111 | // vector operand. |
| 112 | BinaryOperator *B0 = cast<BinaryOperator>(PHIUser); |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 113 | unsigned opId = (B0->getOperand(0) == PN) ? 1 : 0; |
Joey Gouly | 8369928 | 2013-05-24 12:29:54 +0000 | [diff] [blame] | 114 | Value *Op = InsertNewInstWith( |
| 115 | ExtractElementInst::Create(B0->getOperand(opId), Elt, |
| 116 | B0->getOperand(opId)->getName() + ".Elt"), |
| 117 | *B0); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 118 | Value *newPHIUser = InsertNewInstWith( |
Owen Anderson | 7ea02fc | 2016-03-01 19:35:52 +0000 | [diff] [blame] | 119 | BinaryOperator::CreateWithCopiedFlags(B0->getOpcode(), |
| 120 | scalarPHI, Op, B0), *B0); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 121 | scalarPHI->addIncoming(newPHIUser, inBB); |
| 122 | } else { |
| 123 | // Scalarize PHI input: |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 124 | Instruction *newEI = ExtractElementInst::Create(PHIInVal, Elt, ""); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 125 | // Insert the new instruction into the predecessor basic block. |
| 126 | Instruction *pos = dyn_cast<Instruction>(PHIInVal); |
| 127 | BasicBlock::iterator InsertPos; |
| 128 | if (pos && !isa<PHINode>(pos)) { |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 129 | InsertPos = ++pos->getIterator(); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 130 | } else { |
| 131 | InsertPos = inBB->getFirstInsertionPt(); |
| 132 | } |
| 133 | |
| 134 | InsertNewInstWith(newEI, *InsertPos); |
| 135 | |
| 136 | scalarPHI->addIncoming(newEI, inBB); |
| 137 | } |
| 138 | } |
Michael Kuperstein | a0c6ae0 | 2016-06-06 23:38:33 +0000 | [diff] [blame] | 139 | |
| 140 | for (auto E : Extracts) |
| 141 | replaceInstUsesWith(*E, scalarPHI); |
| 142 | |
| 143 | return &EI; |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 146 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 147 | if (Value *V = SimplifyExtractElementInst( |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 148 | EI.getVectorOperand(), EI.getIndexOperand(), DL, &TLI, &DT, &AC)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 149 | return replaceInstUsesWith(EI, V); |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 151 | // If vector val is constant with all elements the same, replace EI with |
| 152 | // that element. We handle a known element # below. |
| 153 | if (Constant *C = dyn_cast<Constant>(EI.getOperand(0))) |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 154 | if (cheapToScalarize(C, false)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 155 | return replaceInstUsesWith(EI, C->getAggregateElement(0U)); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 156 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 157 | // If extracting a specified index from the vector, see if we can recursively |
| 158 | // find a previously computed scalar that was inserted into the vector. |
| 159 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 160 | unsigned IndexVal = IdxC->getZExtValue(); |
| 161 | unsigned VectorWidth = EI.getVectorOperandType()->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 162 | |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 163 | // InstSimplify handles cases where the index is invalid. |
| 164 | assert(IndexVal < VectorWidth); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 165 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 166 | // This instruction only demands the single element from the input vector. |
| 167 | // If the input vector has a single use, simplify it based on this use |
| 168 | // property. |
| 169 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
| 170 | APInt UndefElts(VectorWidth, 0); |
Chris Lattner | b22423c | 2010-02-08 23:56:03 +0000 | [diff] [blame] | 171 | APInt DemandedMask(VectorWidth, 0); |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 172 | DemandedMask.setBit(IndexVal); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 173 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), DemandedMask, |
| 174 | UndefElts)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 175 | EI.setOperand(0, V); |
| 176 | return &EI; |
| 177 | } |
| 178 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 179 | |
Sanjay Patel | b67076c | 2015-11-29 22:09:34 +0000 | [diff] [blame] | 180 | // If this extractelement is directly using a bitcast from a vector of |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 181 | // the same number of elements, see if we can find the source element from |
| 182 | // it. In this case, we will end up needing to bitcast the scalars. |
| 183 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 184 | if (VectorType *VT = dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 185 | if (VT->getNumElements() == VectorWidth) |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 186 | if (Value *Elt = findScalarElement(BCI->getOperand(0), IndexVal)) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 187 | return new BitCastInst(Elt, EI.getType()); |
| 188 | } |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 189 | |
| 190 | // If there's a vector PHI feeding a scalar use through this extractelement |
| 191 | // instruction, try to scalarize the PHI. |
| 192 | if (PHINode *PN = dyn_cast<PHINode>(EI.getOperand(0))) { |
Nick Lewycky | 881e9d6 | 2013-05-04 01:08:15 +0000 | [diff] [blame] | 193 | Instruction *scalarPHI = scalarizePHI(EI, PN); |
| 194 | if (scalarPHI) |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 195 | return scalarPHI; |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 196 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 197 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 198 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 199 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
| 200 | // Push extractelement into predecessor operation if legal and |
Sanjay Patel | b67076c | 2015-11-29 22:09:34 +0000 | [diff] [blame] | 201 | // profitable to do so. |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 202 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
| 203 | if (I->hasOneUse() && |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 204 | cheapToScalarize(BO, isa<ConstantInt>(EI.getOperand(1)))) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 205 | Value *newEI0 = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 206 | Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1), |
| 207 | EI.getName()+".lhs"); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 208 | Value *newEI1 = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 209 | Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1), |
| 210 | EI.getName()+".rhs"); |
Owen Anderson | 7ea02fc | 2016-03-01 19:35:52 +0000 | [diff] [blame] | 211 | return BinaryOperator::CreateWithCopiedFlags(BO->getOpcode(), |
| 212 | newEI0, newEI1, BO); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 213 | } |
| 214 | } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 215 | // Extracting the inserted element? |
| 216 | if (IE->getOperand(2) == EI.getOperand(1)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 217 | return replaceInstUsesWith(EI, IE->getOperand(1)); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 218 | // If the inserted and extracted elements are constants, they must not |
| 219 | // be the same value, extract from the pre-inserted value instead. |
| 220 | if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) { |
| 221 | Worklist.AddValue(EI.getOperand(0)); |
| 222 | EI.setOperand(0, IE->getOperand(0)); |
| 223 | return &EI; |
| 224 | } |
| 225 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 226 | // If this is extracting an element from a shufflevector, figure out where |
| 227 | // it came from and extract from the appropriate input element instead. |
| 228 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Eli Friedman | 303c81c | 2011-10-21 19:11:34 +0000 | [diff] [blame] | 229 | int SrcIdx = SVI->getMaskValue(Elt->getZExtValue()); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 230 | Value *Src; |
| 231 | unsigned LHSWidth = |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 232 | SVI->getOperand(0)->getType()->getVectorNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 233 | |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 234 | if (SrcIdx < 0) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 235 | return replaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 236 | if (SrcIdx < (int)LHSWidth) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 237 | Src = SVI->getOperand(0); |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 238 | else { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 239 | SrcIdx -= LHSWidth; |
| 240 | Src = SVI->getOperand(1); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 241 | } |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 242 | Type *Int32Ty = Type::getInt32Ty(EI.getContext()); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 243 | return ExtractElementInst::Create(Src, |
Bob Wilson | 9d07f39 | 2010-10-29 22:03:07 +0000 | [diff] [blame] | 244 | ConstantInt::get(Int32Ty, |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 245 | SrcIdx, false)); |
| 246 | } |
Nadav Rotem | d74b72b | 2011-03-31 22:57:29 +0000 | [diff] [blame] | 247 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Sanjay Patel | b67076c | 2015-11-29 22:09:34 +0000 | [diff] [blame] | 248 | // Canonicalize extractelement(cast) -> cast(extractelement). |
| 249 | // Bitcasts can change the number of vector elements, and they cost |
| 250 | // nothing. |
Anat Shemer | 5570318 | 2013-04-18 19:56:44 +0000 | [diff] [blame] | 251 | if (CI->hasOneUse() && (CI->getOpcode() != Instruction::BitCast)) { |
Anat Shemer | 10260a7 | 2013-04-22 20:51:10 +0000 | [diff] [blame] | 252 | Value *EE = Builder->CreateExtractElement(CI->getOperand(0), |
| 253 | EI.getIndexOperand()); |
| 254 | Worklist.AddValue(EE); |
Nadav Rotem | d74b72b | 2011-03-31 22:57:29 +0000 | [diff] [blame] | 255 | return CastInst::Create(CI->getOpcode(), EE, EI.getType()); |
| 256 | } |
Matt Arsenault | 243140f | 2013-11-04 20:36:06 +0000 | [diff] [blame] | 257 | } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) { |
| 258 | if (SI->hasOneUse()) { |
| 259 | // TODO: For a select on vectors, it might be useful to do this if it |
| 260 | // has multiple extractelement uses. For vector select, that seems to |
| 261 | // fight the vectorizer. |
| 262 | |
| 263 | // If we are extracting an element from a vector select or a select on |
Sanjay Patel | b67076c | 2015-11-29 22:09:34 +0000 | [diff] [blame] | 264 | // vectors, create a select on the scalars extracted from the vector |
| 265 | // arguments. |
Matt Arsenault | 243140f | 2013-11-04 20:36:06 +0000 | [diff] [blame] | 266 | Value *TrueVal = SI->getTrueValue(); |
| 267 | Value *FalseVal = SI->getFalseValue(); |
| 268 | |
| 269 | Value *Cond = SI->getCondition(); |
| 270 | if (Cond->getType()->isVectorTy()) { |
| 271 | Cond = Builder->CreateExtractElement(Cond, |
| 272 | EI.getIndexOperand(), |
| 273 | Cond->getName() + ".elt"); |
| 274 | } |
| 275 | |
| 276 | Value *V1Elem |
| 277 | = Builder->CreateExtractElement(TrueVal, |
| 278 | EI.getIndexOperand(), |
| 279 | TrueVal->getName() + ".elt"); |
| 280 | |
| 281 | Value *V2Elem |
| 282 | = Builder->CreateExtractElement(FalseVal, |
| 283 | EI.getIndexOperand(), |
| 284 | FalseVal->getName() + ".elt"); |
| 285 | return SelectInst::Create(Cond, |
| 286 | V1Elem, |
| 287 | V2Elem, |
| 288 | SI->getName() + ".elt"); |
| 289 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 290 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 291 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 292 | return nullptr; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 295 | /// If V is a shuffle of values that ONLY returns elements from either LHS or |
| 296 | /// RHS, return the shuffle mask and true. Otherwise, return false. |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 297 | static bool collectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 298 | SmallVectorImpl<Constant*> &Mask) { |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 299 | assert(LHS->getType() == RHS->getType() && |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 300 | "Invalid CollectSingleShuffleElements"); |
Matt Arsenault | 8227b9f | 2013-09-06 00:37:24 +0000 | [diff] [blame] | 301 | unsigned NumElts = V->getType()->getVectorNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 302 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 303 | if (isa<UndefValue>(V)) { |
| 304 | Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); |
| 305 | return true; |
| 306 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 307 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 308 | if (V == LHS) { |
| 309 | for (unsigned i = 0; i != NumElts; ++i) |
| 310 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); |
| 311 | return true; |
| 312 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 313 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 314 | if (V == RHS) { |
| 315 | for (unsigned i = 0; i != NumElts; ++i) |
| 316 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 317 | i+NumElts)); |
| 318 | return true; |
| 319 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 320 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 321 | if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 322 | // If this is an insert of an extract from some other vector, include it. |
| 323 | Value *VecOp = IEI->getOperand(0); |
| 324 | Value *ScalarOp = IEI->getOperand(1); |
| 325 | Value *IdxOp = IEI->getOperand(2); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 326 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 327 | if (!isa<ConstantInt>(IdxOp)) |
| 328 | return false; |
| 329 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 330 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 331 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
Sanjay Patel | 70af1fd | 2014-07-07 22:13:58 +0000 | [diff] [blame] | 332 | // We can handle this if the vector we are inserting into is |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 333 | // transitively ok. |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 334 | if (collectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 335 | // If so, update the mask to reflect the inserted undef. |
| 336 | Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext())); |
| 337 | return true; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 338 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 339 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 340 | if (isa<ConstantInt>(EI->getOperand(1))) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 341 | unsigned ExtractedIdx = |
| 342 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 343 | unsigned NumLHSElts = LHS->getType()->getVectorNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 344 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 345 | // This must be extracting from either LHS or RHS. |
| 346 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
Sanjay Patel | 70af1fd | 2014-07-07 22:13:58 +0000 | [diff] [blame] | 347 | // We can handle this if the vector we are inserting into is |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 348 | // transitively ok. |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 349 | if (collectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 350 | // If so, update the mask to reflect the inserted value. |
| 351 | if (EI->getOperand(0) == LHS) { |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 352 | Mask[InsertedIdx % NumElts] = |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 353 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 354 | ExtractedIdx); |
| 355 | } else { |
| 356 | assert(EI->getOperand(0) == RHS); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 357 | Mask[InsertedIdx % NumElts] = |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 358 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 359 | ExtractedIdx + NumLHSElts); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 360 | } |
| 361 | return true; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 367 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 368 | return false; |
| 369 | } |
| 370 | |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 371 | /// If we have insertion into a vector that is wider than the vector that we |
| 372 | /// are extracting from, try to widen the source vector to allow a single |
| 373 | /// shufflevector to replace one or more insert/extract pairs. |
| 374 | static void replaceExtractElements(InsertElementInst *InsElt, |
| 375 | ExtractElementInst *ExtElt, |
| 376 | InstCombiner &IC) { |
| 377 | VectorType *InsVecType = InsElt->getType(); |
| 378 | VectorType *ExtVecType = ExtElt->getVectorOperandType(); |
| 379 | unsigned NumInsElts = InsVecType->getVectorNumElements(); |
| 380 | unsigned NumExtElts = ExtVecType->getVectorNumElements(); |
| 381 | |
| 382 | // The inserted-to vector must be wider than the extracted-from vector. |
| 383 | if (InsVecType->getElementType() != ExtVecType->getElementType() || |
| 384 | NumExtElts >= NumInsElts) |
| 385 | return; |
| 386 | |
| 387 | // Create a shuffle mask to widen the extended-from vector using undefined |
| 388 | // values. The mask selects all of the values of the original vector followed |
| 389 | // by as many undefined values as needed to create a vector of the same length |
| 390 | // as the inserted-to vector. |
| 391 | SmallVector<Constant *, 16> ExtendMask; |
| 392 | IntegerType *IntType = Type::getInt32Ty(InsElt->getContext()); |
| 393 | for (unsigned i = 0; i < NumExtElts; ++i) |
| 394 | ExtendMask.push_back(ConstantInt::get(IntType, i)); |
| 395 | for (unsigned i = NumExtElts; i < NumInsElts; ++i) |
| 396 | ExtendMask.push_back(UndefValue::get(IntType)); |
| 397 | |
| 398 | Value *ExtVecOp = ExtElt->getVectorOperand(); |
Sanjay Patel | 66fff73 | 2016-01-29 20:21:02 +0000 | [diff] [blame] | 399 | auto *ExtVecOpInst = dyn_cast<Instruction>(ExtVecOp); |
| 400 | BasicBlock *InsertionBlock = (ExtVecOpInst && !isa<PHINode>(ExtVecOpInst)) |
| 401 | ? ExtVecOpInst->getParent() |
| 402 | : ExtElt->getParent(); |
| 403 | |
| 404 | // TODO: This restriction matches the basic block check below when creating |
| 405 | // new extractelement instructions. If that limitation is removed, this one |
| 406 | // could also be removed. But for now, we just bail out to ensure that we |
| 407 | // will replace the extractelement instruction that is feeding our |
| 408 | // insertelement instruction. This allows the insertelement to then be |
| 409 | // replaced by a shufflevector. If the insertelement is not replaced, we can |
| 410 | // induce infinite looping because there's an optimization for extractelement |
| 411 | // that will delete our widening shuffle. This would trigger another attempt |
| 412 | // here to create that shuffle, and we spin forever. |
| 413 | if (InsertionBlock != InsElt->getParent()) |
| 414 | return; |
| 415 | |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 416 | auto *WideVec = new ShuffleVectorInst(ExtVecOp, UndefValue::get(ExtVecType), |
| 417 | ConstantVector::get(ExtendMask)); |
| 418 | |
Sanjay Patel | a1c5347 | 2016-01-05 19:09:47 +0000 | [diff] [blame] | 419 | // Insert the new shuffle after the vector operand of the extract is defined |
Sanjay Patel | d72a458 | 2016-01-08 01:39:16 +0000 | [diff] [blame] | 420 | // (as long as it's not a PHI) or at the start of the basic block of the |
| 421 | // extract, so any subsequent extracts in the same basic block can use it. |
| 422 | // TODO: Insert before the earliest ExtractElementInst that is replaced. |
Sanjay Patel | d72a458 | 2016-01-08 01:39:16 +0000 | [diff] [blame] | 423 | if (ExtVecOpInst && !isa<PHINode>(ExtVecOpInst)) |
Sanjay Patel | a1c5347 | 2016-01-05 19:09:47 +0000 | [diff] [blame] | 424 | WideVec->insertAfter(ExtVecOpInst); |
Sanjay Patel | d72a458 | 2016-01-08 01:39:16 +0000 | [diff] [blame] | 425 | else |
Sanjay Patel | a1c5347 | 2016-01-05 19:09:47 +0000 | [diff] [blame] | 426 | IC.InsertNewInstWith(WideVec, *ExtElt->getParent()->getFirstInsertionPt()); |
Sanjay Patel | a1c5347 | 2016-01-05 19:09:47 +0000 | [diff] [blame] | 427 | |
| 428 | // Replace extracts from the original narrow vector with extracts from the new |
| 429 | // wide vector. |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 430 | for (User *U : ExtVecOp->users()) { |
Sanjay Patel | a1c5347 | 2016-01-05 19:09:47 +0000 | [diff] [blame] | 431 | ExtractElementInst *OldExt = dyn_cast<ExtractElementInst>(U); |
Sanjay Patel | d72a458 | 2016-01-08 01:39:16 +0000 | [diff] [blame] | 432 | if (!OldExt || OldExt->getParent() != WideVec->getParent()) |
Sanjay Patel | a1c5347 | 2016-01-05 19:09:47 +0000 | [diff] [blame] | 433 | continue; |
| 434 | auto *NewExt = ExtractElementInst::Create(WideVec, OldExt->getOperand(1)); |
| 435 | NewExt->insertAfter(WideVec); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 436 | IC.replaceInstUsesWith(*OldExt, NewExt); |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 439 | |
| 440 | /// We are building a shuffle to create V, which is a sequence of insertelement, |
| 441 | /// extractelement pairs. If PermittedRHS is set, then we must either use it or |
Sanjay Patel | 70af1fd | 2014-07-07 22:13:58 +0000 | [diff] [blame] | 442 | /// not rely on the second vector source. Return a std::pair containing the |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 443 | /// left and right vectors of the proposed shuffle (or 0), and set the Mask |
| 444 | /// parameter as required. |
| 445 | /// |
| 446 | /// Note: we intentionally don't try to fold earlier shuffles since they have |
| 447 | /// often been chosen carefully to be efficiently implementable on the target. |
| 448 | typedef std::pair<Value *, Value *> ShuffleOps; |
| 449 | |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 450 | static ShuffleOps collectShuffleElements(Value *V, |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 451 | SmallVectorImpl<Constant *> &Mask, |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 452 | Value *PermittedRHS, |
| 453 | InstCombiner &IC) { |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 454 | assert(V->getType()->isVectorTy() && "Invalid shuffle!"); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 455 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 456 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 457 | if (isa<UndefValue>(V)) { |
| 458 | Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 459 | return std::make_pair( |
| 460 | PermittedRHS ? UndefValue::get(PermittedRHS->getType()) : V, nullptr); |
Chris Lattner | a0d01ff | 2012-01-24 14:31:22 +0000 | [diff] [blame] | 461 | } |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 462 | |
Chris Lattner | a0d01ff | 2012-01-24 14:31:22 +0000 | [diff] [blame] | 463 | if (isa<ConstantAggregateZero>(V)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 464 | Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0)); |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 465 | return std::make_pair(V, nullptr); |
Chris Lattner | a0d01ff | 2012-01-24 14:31:22 +0000 | [diff] [blame] | 466 | } |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 467 | |
Chris Lattner | a0d01ff | 2012-01-24 14:31:22 +0000 | [diff] [blame] | 468 | if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 469 | // If this is an insert of an extract from some other vector, include it. |
| 470 | Value *VecOp = IEI->getOperand(0); |
| 471 | Value *ScalarOp = IEI->getOperand(1); |
| 472 | Value *IdxOp = IEI->getOperand(2); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 473 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 474 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 475 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 476 | unsigned ExtractedIdx = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 477 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 478 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 479 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 480 | // Either the extracted from or inserted into vector must be RHSVec, |
| 481 | // otherwise we'd end up with a shuffle of three inputs. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 482 | if (EI->getOperand(0) == PermittedRHS || PermittedRHS == nullptr) { |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 483 | Value *RHS = EI->getOperand(0); |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 484 | ShuffleOps LR = collectShuffleElements(VecOp, Mask, RHS, IC); |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 485 | assert(LR.second == nullptr || LR.second == RHS); |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 486 | |
| 487 | if (LR.first->getType() != RHS->getType()) { |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 488 | // Although we are giving up for now, see if we can create extracts |
| 489 | // that match the inserts for another round of combining. |
| 490 | replaceExtractElements(IEI, EI, IC); |
| 491 | |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 492 | // We tried our best, but we can't find anything compatible with RHS |
| 493 | // further up the chain. Return a trivial shuffle. |
| 494 | for (unsigned i = 0; i < NumElts; ++i) |
| 495 | Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()), i); |
| 496 | return std::make_pair(V, nullptr); |
| 497 | } |
| 498 | |
| 499 | unsigned NumLHSElts = RHS->getType()->getVectorNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 500 | Mask[InsertedIdx % NumElts] = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 501 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 502 | NumLHSElts+ExtractedIdx); |
| 503 | return std::make_pair(LR.first, RHS); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 504 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 505 | |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 506 | if (VecOp == PermittedRHS) { |
| 507 | // We've gone as far as we can: anything on the other side of the |
| 508 | // extractelement will already have been converted into a shuffle. |
| 509 | unsigned NumLHSElts = |
| 510 | EI->getOperand(0)->getType()->getVectorNumElements(); |
| 511 | for (unsigned i = 0; i != NumElts; ++i) |
| 512 | Mask.push_back(ConstantInt::get( |
| 513 | Type::getInt32Ty(V->getContext()), |
| 514 | i == InsertedIdx ? ExtractedIdx : NumLHSElts + i)); |
| 515 | return std::make_pair(EI->getOperand(0), PermittedRHS); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 516 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 517 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 518 | // If this insertelement is a chain that comes from exactly these two |
| 519 | // vectors, return the vector and the effective shuffle. |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 520 | if (EI->getOperand(0)->getType() == PermittedRHS->getType() && |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 521 | collectSingleShuffleElements(IEI, EI->getOperand(0), PermittedRHS, |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 522 | Mask)) |
| 523 | return std::make_pair(EI->getOperand(0), PermittedRHS); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 527 | |
Sanjay Patel | b67076c | 2015-11-29 22:09:34 +0000 | [diff] [blame] | 528 | // Otherwise, we can't do anything fancy. Return an identity vector. |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 529 | for (unsigned i = 0; i != NumElts; ++i) |
| 530 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 531 | return std::make_pair(V, nullptr); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 534 | /// Try to find redundant insertvalue instructions, like the following ones: |
| 535 | /// %0 = insertvalue { i8, i32 } undef, i8 %x, 0 |
| 536 | /// %1 = insertvalue { i8, i32 } %0, i8 %y, 0 |
| 537 | /// Here the second instruction inserts values at the same indices, as the |
| 538 | /// first one, making the first one redundant. |
| 539 | /// It should be transformed to: |
| 540 | /// %0 = insertvalue { i8, i32 } undef, i8 %y, 0 |
| 541 | Instruction *InstCombiner::visitInsertValueInst(InsertValueInst &I) { |
| 542 | bool IsRedundant = false; |
| 543 | ArrayRef<unsigned int> FirstIndices = I.getIndices(); |
| 544 | |
| 545 | // If there is a chain of insertvalue instructions (each of them except the |
| 546 | // last one has only one use and it's another insertvalue insn from this |
| 547 | // chain), check if any of the 'children' uses the same indices as the first |
| 548 | // instruction. In this case, the first one is redundant. |
| 549 | Value *V = &I; |
Michael Zolotukhin | 292d3ca | 2014-05-08 19:50:24 +0000 | [diff] [blame] | 550 | unsigned Depth = 0; |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 551 | while (V->hasOneUse() && Depth < 10) { |
| 552 | User *U = V->user_back(); |
Michael Zolotukhin | 292d3ca | 2014-05-08 19:50:24 +0000 | [diff] [blame] | 553 | auto UserInsInst = dyn_cast<InsertValueInst>(U); |
| 554 | if (!UserInsInst || U->getOperand(0) != V) |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 555 | break; |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 556 | if (UserInsInst->getIndices() == FirstIndices) { |
| 557 | IsRedundant = true; |
| 558 | break; |
| 559 | } |
| 560 | V = UserInsInst; |
| 561 | Depth++; |
| 562 | } |
| 563 | |
| 564 | if (IsRedundant) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 565 | return replaceInstUsesWith(I, I.getOperand(0)); |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 566 | return nullptr; |
| 567 | } |
| 568 | |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame^] | 569 | static bool isShuffleEquivalentToSelect(ShuffleVectorInst &Shuf) { |
| 570 | int MaskSize = Shuf.getMask()->getType()->getVectorNumElements(); |
| 571 | int VecSize = Shuf.getOperand(0)->getType()->getVectorNumElements(); |
| 572 | |
| 573 | // A vector select does not change the size of the operands. |
| 574 | if (MaskSize != VecSize) |
| 575 | return false; |
| 576 | |
| 577 | // Each mask element must be undefined or choose a vector element from one of |
| 578 | // the source operands without crossing vector lanes. |
| 579 | for (int i = 0; i != MaskSize; ++i) { |
| 580 | int Elt = Shuf.getMaskValue(i); |
| 581 | if (Elt != -1 && Elt != i && Elt != i + VecSize) |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | /// insertelt (shufflevector X, CVec, Mask), C, CIndex --> |
| 589 | /// shufflevector X, CVec', Mask' |
| 590 | static Instruction *foldConstantInsEltIntoShuffle(InsertElementInst &InsElt) { |
| 591 | // Bail out if the shuffle has more than one use. In that case, we'd be |
| 592 | // replacing the insertelt with a shuffle, and that's not a clear win. |
| 593 | auto *Shuf = dyn_cast<ShuffleVectorInst>(InsElt.getOperand(0)); |
| 594 | if (!Shuf || !Shuf->hasOneUse()) |
| 595 | return nullptr; |
| 596 | |
| 597 | // The shuffle must have a constant vector operand. The insertelt must have a |
| 598 | // constant scalar being inserted at a constant position in the vector. |
| 599 | Constant *ShufConstVec, *InsEltScalar; |
| 600 | uint64_t InsEltIndex; |
| 601 | if (!match(Shuf->getOperand(1), m_Constant(ShufConstVec)) || |
| 602 | !match(InsElt.getOperand(1), m_Constant(InsEltScalar)) || |
| 603 | !match(InsElt.getOperand(2), m_ConstantInt(InsEltIndex))) |
| 604 | return nullptr; |
| 605 | |
| 606 | // Adding an element to an arbitrary shuffle could be expensive, but a shuffle |
| 607 | // that selects elements from vectors without crossing lanes is assumed cheap. |
| 608 | // If we're just adding a constant into that shuffle, it will still be cheap. |
| 609 | if (!isShuffleEquivalentToSelect(*Shuf)) |
| 610 | return nullptr; |
| 611 | |
| 612 | // From the above 'select' check, we know that the mask has the same number of |
| 613 | // elements as the vector input operands. We also know that each constant |
| 614 | // input element is used in its lane and can not be used more than once by the |
| 615 | // shuffle. Therefore, replace the constant in the shuffle's constant vector |
| 616 | // with the insertelt constant. Replace the constant in the shuffle's mask |
| 617 | // vector with the insertelt index plus the length of the vector (because the |
| 618 | // constant vector operand of a shuffle is always the 2nd operand). |
| 619 | Constant *Mask = Shuf->getMask(); |
| 620 | unsigned NumElts = Mask->getType()->getVectorNumElements(); |
| 621 | SmallVector<Constant*, 16> NewShufElts(NumElts); |
| 622 | SmallVector<Constant*, 16> NewMaskElts(NumElts); |
| 623 | for (unsigned i = 0; i != NumElts; ++i) { |
| 624 | if (i == InsEltIndex) { |
| 625 | NewShufElts[i] = InsEltScalar; |
| 626 | Type *Int32Ty = Type::getInt32Ty(Shuf->getContext()); |
| 627 | NewMaskElts[i] = ConstantInt::get(Int32Ty, InsEltIndex + NumElts); |
| 628 | } else { |
| 629 | // Copy over the existing values. |
| 630 | NewShufElts[i] = ShufConstVec->getAggregateElement(i); |
| 631 | NewMaskElts[i] = Mask->getAggregateElement(i); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | // Create new operands for a shuffle that includes the constant of the |
| 636 | // original insertelt. The old shuffle will be dead now. |
| 637 | Constant *NewShufVec = ConstantVector::get(NewShufElts); |
| 638 | Constant *NewMask = ConstantVector::get(NewMaskElts); |
| 639 | return new ShuffleVectorInst(Shuf->getOperand(0), NewShufVec, NewMask); |
| 640 | } |
| 641 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 642 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 643 | Value *VecOp = IE.getOperand(0); |
| 644 | Value *ScalarOp = IE.getOperand(1); |
| 645 | Value *IdxOp = IE.getOperand(2); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 646 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 647 | // Inserting an undef or into an undefined place, remove this. |
| 648 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 649 | replaceInstUsesWith(IE, VecOp); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 650 | |
| 651 | // If the inserted element was extracted from some other vector, and if the |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 652 | // indexes are constant, try to turn this into a shufflevector operation. |
| 653 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 654 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp)) { |
| 655 | unsigned NumInsertVectorElts = IE.getType()->getNumElements(); |
| 656 | unsigned NumExtractVectorElts = |
| 657 | EI->getOperand(0)->getType()->getVectorNumElements(); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 658 | unsigned ExtractedIdx = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 659 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 660 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 661 | |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 662 | if (ExtractedIdx >= NumExtractVectorElts) // Out of range extract. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 663 | return replaceInstUsesWith(IE, VecOp); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 664 | |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 665 | if (InsertedIdx >= NumInsertVectorElts) // Out of range insert. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 666 | return replaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 667 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 668 | // If we are extracting a value from a vector, then inserting it right |
| 669 | // back into the same place, just use the input vector. |
| 670 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 671 | return replaceInstUsesWith(IE, VecOp); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 672 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 673 | // If this insertelement isn't used by some other insertelement, turn it |
| 674 | // (and any insertelements it points to), into one big shuffle. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 675 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.user_back())) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 676 | SmallVector<Constant*, 16> Mask; |
Sanjay Patel | ae945e7 | 2015-12-24 21:17:56 +0000 | [diff] [blame] | 677 | ShuffleOps LR = collectShuffleElements(&IE, Mask, nullptr, *this); |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 678 | |
| 679 | // The proposed shuffle may be trivial, in which case we shouldn't |
| 680 | // perform the combine. |
| 681 | if (LR.first != &IE && LR.second != &IE) { |
| 682 | // We now have a shuffle of LHS, RHS, Mask. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 683 | if (LR.second == nullptr) |
| 684 | LR.second = UndefValue::get(LR.first->getType()); |
Tim Northover | fad2761 | 2014-03-07 10:24:44 +0000 | [diff] [blame] | 685 | return new ShuffleVectorInst(LR.first, LR.second, |
| 686 | ConstantVector::get(Mask)); |
| 687 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 691 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 692 | unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements(); |
| 693 | APInt UndefElts(VWidth, 0); |
| 694 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
Eli Friedman | ef200db | 2011-02-19 22:42:40 +0000 | [diff] [blame] | 695 | if (Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) { |
| 696 | if (V != &IE) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 697 | return replaceInstUsesWith(IE, V); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 698 | return &IE; |
Eli Friedman | ef200db | 2011-02-19 22:42:40 +0000 | [diff] [blame] | 699 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 700 | |
Sanjay Patel | 521f19f | 2016-09-02 17:05:43 +0000 | [diff] [blame^] | 701 | if (Instruction *Shuf = foldConstantInsEltIntoShuffle(IE)) |
| 702 | return Shuf; |
| 703 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 704 | return nullptr; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 707 | /// Return true if we can evaluate the specified expression tree if the vector |
| 708 | /// elements were shuffled in a different order. |
| 709 | static bool CanEvaluateShuffled(Value *V, ArrayRef<int> Mask, |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 710 | unsigned Depth = 5) { |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 711 | // We can always reorder the elements of a constant. |
| 712 | if (isa<Constant>(V)) |
| 713 | return true; |
| 714 | |
| 715 | // We won't reorder vector arguments. No IPO here. |
| 716 | Instruction *I = dyn_cast<Instruction>(V); |
| 717 | if (!I) return false; |
| 718 | |
| 719 | // Two users may expect different orders of the elements. Don't try it. |
| 720 | if (!I->hasOneUse()) |
| 721 | return false; |
| 722 | |
| 723 | if (Depth == 0) return false; |
| 724 | |
| 725 | switch (I->getOpcode()) { |
| 726 | case Instruction::Add: |
| 727 | case Instruction::FAdd: |
| 728 | case Instruction::Sub: |
| 729 | case Instruction::FSub: |
| 730 | case Instruction::Mul: |
| 731 | case Instruction::FMul: |
| 732 | case Instruction::UDiv: |
| 733 | case Instruction::SDiv: |
| 734 | case Instruction::FDiv: |
| 735 | case Instruction::URem: |
| 736 | case Instruction::SRem: |
| 737 | case Instruction::FRem: |
| 738 | case Instruction::Shl: |
| 739 | case Instruction::LShr: |
| 740 | case Instruction::AShr: |
| 741 | case Instruction::And: |
| 742 | case Instruction::Or: |
| 743 | case Instruction::Xor: |
| 744 | case Instruction::ICmp: |
| 745 | case Instruction::FCmp: |
| 746 | case Instruction::Trunc: |
| 747 | case Instruction::ZExt: |
| 748 | case Instruction::SExt: |
| 749 | case Instruction::FPToUI: |
| 750 | case Instruction::FPToSI: |
| 751 | case Instruction::UIToFP: |
| 752 | case Instruction::SIToFP: |
| 753 | case Instruction::FPTrunc: |
| 754 | case Instruction::FPExt: |
| 755 | case Instruction::GetElementPtr: { |
Sanjay Patel | 4e2875314 | 2015-11-16 22:16:52 +0000 | [diff] [blame] | 756 | for (Value *Operand : I->operands()) { |
| 757 | if (!CanEvaluateShuffled(Operand, Mask, Depth-1)) |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 758 | return false; |
| 759 | } |
| 760 | return true; |
| 761 | } |
| 762 | case Instruction::InsertElement: { |
| 763 | ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(2)); |
| 764 | if (!CI) return false; |
| 765 | int ElementNumber = CI->getLimitedValue(); |
| 766 | |
| 767 | // Verify that 'CI' does not occur twice in Mask. A single 'insertelement' |
| 768 | // can't put an element into multiple indices. |
| 769 | bool SeenOnce = false; |
| 770 | for (int i = 0, e = Mask.size(); i != e; ++i) { |
| 771 | if (Mask[i] == ElementNumber) { |
| 772 | if (SeenOnce) |
| 773 | return false; |
| 774 | SeenOnce = true; |
| 775 | } |
| 776 | } |
| 777 | return CanEvaluateShuffled(I->getOperand(0), Mask, Depth-1); |
| 778 | } |
| 779 | } |
| 780 | return false; |
| 781 | } |
| 782 | |
| 783 | /// Rebuild a new instruction just like 'I' but with the new operands given. |
| 784 | /// In the event of type mismatch, the type of the operands is correct. |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 785 | static Value *buildNew(Instruction *I, ArrayRef<Value*> NewOps) { |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 786 | // We don't want to use the IRBuilder here because we want the replacement |
| 787 | // instructions to appear next to 'I', not the builder's insertion point. |
| 788 | switch (I->getOpcode()) { |
| 789 | case Instruction::Add: |
| 790 | case Instruction::FAdd: |
| 791 | case Instruction::Sub: |
| 792 | case Instruction::FSub: |
| 793 | case Instruction::Mul: |
| 794 | case Instruction::FMul: |
| 795 | case Instruction::UDiv: |
| 796 | case Instruction::SDiv: |
| 797 | case Instruction::FDiv: |
| 798 | case Instruction::URem: |
| 799 | case Instruction::SRem: |
| 800 | case Instruction::FRem: |
| 801 | case Instruction::Shl: |
| 802 | case Instruction::LShr: |
| 803 | case Instruction::AShr: |
| 804 | case Instruction::And: |
| 805 | case Instruction::Or: |
| 806 | case Instruction::Xor: { |
| 807 | BinaryOperator *BO = cast<BinaryOperator>(I); |
| 808 | assert(NewOps.size() == 2 && "binary operator with #ops != 2"); |
| 809 | BinaryOperator *New = |
| 810 | BinaryOperator::Create(cast<BinaryOperator>(I)->getOpcode(), |
| 811 | NewOps[0], NewOps[1], "", BO); |
| 812 | if (isa<OverflowingBinaryOperator>(BO)) { |
| 813 | New->setHasNoUnsignedWrap(BO->hasNoUnsignedWrap()); |
| 814 | New->setHasNoSignedWrap(BO->hasNoSignedWrap()); |
| 815 | } |
| 816 | if (isa<PossiblyExactOperator>(BO)) { |
| 817 | New->setIsExact(BO->isExact()); |
| 818 | } |
Owen Anderson | 48b842e | 2014-01-18 00:48:14 +0000 | [diff] [blame] | 819 | if (isa<FPMathOperator>(BO)) |
| 820 | New->copyFastMathFlags(I); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 821 | return New; |
| 822 | } |
| 823 | case Instruction::ICmp: |
| 824 | assert(NewOps.size() == 2 && "icmp with #ops != 2"); |
| 825 | return new ICmpInst(I, cast<ICmpInst>(I)->getPredicate(), |
| 826 | NewOps[0], NewOps[1]); |
| 827 | case Instruction::FCmp: |
| 828 | assert(NewOps.size() == 2 && "fcmp with #ops != 2"); |
| 829 | return new FCmpInst(I, cast<FCmpInst>(I)->getPredicate(), |
| 830 | NewOps[0], NewOps[1]); |
| 831 | case Instruction::Trunc: |
| 832 | case Instruction::ZExt: |
| 833 | case Instruction::SExt: |
| 834 | case Instruction::FPToUI: |
| 835 | case Instruction::FPToSI: |
| 836 | case Instruction::UIToFP: |
| 837 | case Instruction::SIToFP: |
| 838 | case Instruction::FPTrunc: |
| 839 | case Instruction::FPExt: { |
| 840 | // It's possible that the mask has a different number of elements from |
| 841 | // the original cast. We recompute the destination type to match the mask. |
| 842 | Type *DestTy = |
| 843 | VectorType::get(I->getType()->getScalarType(), |
| 844 | NewOps[0]->getType()->getVectorNumElements()); |
| 845 | assert(NewOps.size() == 1 && "cast with #ops != 1"); |
| 846 | return CastInst::Create(cast<CastInst>(I)->getOpcode(), NewOps[0], DestTy, |
| 847 | "", I); |
| 848 | } |
| 849 | case Instruction::GetElementPtr: { |
| 850 | Value *Ptr = NewOps[0]; |
| 851 | ArrayRef<Value*> Idx = NewOps.slice(1); |
David Blaikie | 22319eb | 2015-03-14 19:24:04 +0000 | [diff] [blame] | 852 | GetElementPtrInst *GEP = GetElementPtrInst::Create( |
| 853 | cast<GetElementPtrInst>(I)->getSourceElementType(), Ptr, Idx, "", I); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 854 | GEP->setIsInBounds(cast<GetElementPtrInst>(I)->isInBounds()); |
| 855 | return GEP; |
| 856 | } |
| 857 | } |
| 858 | llvm_unreachable("failed to rebuild vector instructions"); |
| 859 | } |
| 860 | |
| 861 | Value * |
| 862 | InstCombiner::EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask) { |
| 863 | // Mask.size() does not need to be equal to the number of vector elements. |
| 864 | |
| 865 | assert(V->getType()->isVectorTy() && "can't reorder non-vector elements"); |
| 866 | if (isa<UndefValue>(V)) { |
| 867 | return UndefValue::get(VectorType::get(V->getType()->getScalarType(), |
| 868 | Mask.size())); |
| 869 | } |
| 870 | if (isa<ConstantAggregateZero>(V)) { |
| 871 | return ConstantAggregateZero::get( |
| 872 | VectorType::get(V->getType()->getScalarType(), |
| 873 | Mask.size())); |
| 874 | } |
| 875 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 876 | SmallVector<Constant *, 16> MaskValues; |
| 877 | for (int i = 0, e = Mask.size(); i != e; ++i) { |
| 878 | if (Mask[i] == -1) |
| 879 | MaskValues.push_back(UndefValue::get(Builder->getInt32Ty())); |
| 880 | else |
| 881 | MaskValues.push_back(Builder->getInt32(Mask[i])); |
| 882 | } |
| 883 | return ConstantExpr::getShuffleVector(C, UndefValue::get(C->getType()), |
| 884 | ConstantVector::get(MaskValues)); |
| 885 | } |
| 886 | |
| 887 | Instruction *I = cast<Instruction>(V); |
| 888 | switch (I->getOpcode()) { |
| 889 | case Instruction::Add: |
| 890 | case Instruction::FAdd: |
| 891 | case Instruction::Sub: |
| 892 | case Instruction::FSub: |
| 893 | case Instruction::Mul: |
| 894 | case Instruction::FMul: |
| 895 | case Instruction::UDiv: |
| 896 | case Instruction::SDiv: |
| 897 | case Instruction::FDiv: |
| 898 | case Instruction::URem: |
| 899 | case Instruction::SRem: |
| 900 | case Instruction::FRem: |
| 901 | case Instruction::Shl: |
| 902 | case Instruction::LShr: |
| 903 | case Instruction::AShr: |
| 904 | case Instruction::And: |
| 905 | case Instruction::Or: |
| 906 | case Instruction::Xor: |
| 907 | case Instruction::ICmp: |
| 908 | case Instruction::FCmp: |
| 909 | case Instruction::Trunc: |
| 910 | case Instruction::ZExt: |
| 911 | case Instruction::SExt: |
| 912 | case Instruction::FPToUI: |
| 913 | case Instruction::FPToSI: |
| 914 | case Instruction::UIToFP: |
| 915 | case Instruction::SIToFP: |
| 916 | case Instruction::FPTrunc: |
| 917 | case Instruction::FPExt: |
| 918 | case Instruction::Select: |
| 919 | case Instruction::GetElementPtr: { |
| 920 | SmallVector<Value*, 8> NewOps; |
| 921 | bool NeedsRebuild = (Mask.size() != I->getType()->getVectorNumElements()); |
| 922 | for (int i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 923 | Value *V = EvaluateInDifferentElementOrder(I->getOperand(i), Mask); |
| 924 | NewOps.push_back(V); |
| 925 | NeedsRebuild |= (V != I->getOperand(i)); |
| 926 | } |
| 927 | if (NeedsRebuild) { |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 928 | return buildNew(I, NewOps); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 929 | } |
| 930 | return I; |
| 931 | } |
| 932 | case Instruction::InsertElement: { |
| 933 | int Element = cast<ConstantInt>(I->getOperand(2))->getLimitedValue(); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 934 | |
| 935 | // The insertelement was inserting at Element. Figure out which element |
| 936 | // that becomes after shuffling. The answer is guaranteed to be unique |
| 937 | // by CanEvaluateShuffled. |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 938 | bool Found = false; |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 939 | int Index = 0; |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 940 | for (int e = Mask.size(); Index != e; ++Index) { |
| 941 | if (Mask[Index] == Element) { |
| 942 | Found = true; |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 943 | break; |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 946 | |
Hao Liu | 26abebb | 2014-01-08 03:06:15 +0000 | [diff] [blame] | 947 | // If element is not in Mask, no need to handle the operand 1 (element to |
| 948 | // be inserted). Just evaluate values in operand 0 according to Mask. |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 949 | if (!Found) |
Hao Liu | 26abebb | 2014-01-08 03:06:15 +0000 | [diff] [blame] | 950 | return EvaluateInDifferentElementOrder(I->getOperand(0), Mask); |
Joey Gouly | a3250f2 | 2013-07-12 23:08:06 +0000 | [diff] [blame] | 951 | |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 952 | Value *V = EvaluateInDifferentElementOrder(I->getOperand(0), Mask); |
| 953 | return InsertElementInst::Create(V, I->getOperand(1), |
| 954 | Builder->getInt32(Index), "", I); |
| 955 | } |
| 956 | } |
| 957 | llvm_unreachable("failed to reorder elements of vector instruction!"); |
| 958 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 959 | |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 960 | static void recognizeIdentityMask(const SmallVectorImpl<int> &Mask, |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 961 | bool &isLHSID, bool &isRHSID) { |
| 962 | isLHSID = isRHSID = true; |
| 963 | |
| 964 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 965 | if (Mask[i] < 0) continue; // Ignore undef values. |
| 966 | // Is this an identity shuffle of the LHS value? |
| 967 | isLHSID &= (Mask[i] == (int)i); |
| 968 | |
| 969 | // Is this an identity shuffle of the RHS value? |
| 970 | isRHSID &= (Mask[i]-e == i); |
| 971 | } |
| 972 | } |
| 973 | |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 974 | // Returns true if the shuffle is extracting a contiguous range of values from |
| 975 | // LHS, for example: |
| 976 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 977 | // Input: |AA|BB|CC|DD|EE|FF|GG|HH|II|JJ|KK|LL|MM|NN|OO|PP| |
| 978 | // Shuffles to: |EE|FF|GG|HH| |
| 979 | // +--+--+--+--+ |
| 980 | static bool isShuffleExtractingFromLHS(ShuffleVectorInst &SVI, |
| 981 | SmallVector<int, 16> &Mask) { |
| 982 | unsigned LHSElems = |
| 983 | cast<VectorType>(SVI.getOperand(0)->getType())->getNumElements(); |
| 984 | unsigned MaskElems = Mask.size(); |
| 985 | unsigned BegIdx = Mask.front(); |
| 986 | unsigned EndIdx = Mask.back(); |
| 987 | if (BegIdx > EndIdx || EndIdx >= LHSElems || EndIdx - BegIdx != MaskElems - 1) |
| 988 | return false; |
| 989 | for (unsigned I = 0; I != MaskElems; ++I) |
| 990 | if (static_cast<unsigned>(Mask[I]) != BegIdx + I) |
| 991 | return false; |
| 992 | return true; |
| 993 | } |
| 994 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 995 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 996 | Value *LHS = SVI.getOperand(0); |
| 997 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 998 | SmallVector<int, 16> Mask = SVI.getShuffleMask(); |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 999 | Type *Int32Ty = Type::getInt32Ty(SVI.getContext()); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1000 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1001 | bool MadeChange = false; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1003 | // Undefined shuffle mask -> undefined value. |
| 1004 | if (isa<UndefValue>(SVI.getOperand(2))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1005 | return replaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1006 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 1007 | unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1008 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1009 | APInt UndefElts(VWidth, 0); |
| 1010 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
Eli Friedman | ef200db | 2011-02-19 22:42:40 +0000 | [diff] [blame] | 1011 | if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) { |
| 1012 | if (V != &SVI) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1013 | return replaceInstUsesWith(SVI, V); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1014 | LHS = SVI.getOperand(0); |
| 1015 | RHS = SVI.getOperand(1); |
| 1016 | MadeChange = true; |
| 1017 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1018 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1019 | unsigned LHSWidth = cast<VectorType>(LHS->getType())->getNumElements(); |
| 1020 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1021 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 1022 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 1023 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 1024 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
| 1025 | // shuffle(undef,undef,mask) -> undef. |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1026 | Value *Result = (VWidth == LHSWidth) |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1027 | ? LHS : UndefValue::get(SVI.getType()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1028 | return replaceInstUsesWith(SVI, Result); |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 1029 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1030 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1031 | // Remap any references to RHS to use LHS. |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1032 | SmallVector<Constant*, 16> Elts; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1033 | for (unsigned i = 0, e = LHSWidth; i != VWidth; ++i) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1034 | if (Mask[i] < 0) { |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 1035 | Elts.push_back(UndefValue::get(Int32Ty)); |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1036 | continue; |
| 1037 | } |
| 1038 | |
| 1039 | if ((Mask[i] >= (int)e && isa<UndefValue>(RHS)) || |
| 1040 | (Mask[i] < (int)e && isa<UndefValue>(LHS))) { |
| 1041 | Mask[i] = -1; // Turn into undef. |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 1042 | Elts.push_back(UndefValue::get(Int32Ty)); |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1043 | } else { |
| 1044 | Mask[i] = Mask[i] % e; // Force to LHS. |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 1045 | Elts.push_back(ConstantInt::get(Int32Ty, Mask[i])); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | SVI.setOperand(0, SVI.getOperand(1)); |
| 1049 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
| 1050 | SVI.setOperand(2, ConstantVector::get(Elts)); |
| 1051 | LHS = SVI.getOperand(0); |
| 1052 | RHS = SVI.getOperand(1); |
| 1053 | MadeChange = true; |
| 1054 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1055 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1056 | if (VWidth == LHSWidth) { |
| 1057 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1058 | bool isLHSID, isRHSID; |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 1059 | recognizeIdentityMask(Mask, isLHSID, isRHSID); |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1060 | |
| 1061 | // Eliminate identity shuffles. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1062 | if (isLHSID) return replaceInstUsesWith(SVI, LHS); |
| 1063 | if (isRHSID) return replaceInstUsesWith(SVI, RHS); |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 1064 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1065 | |
Nick Lewycky | 688d668 | 2013-06-03 23:15:20 +0000 | [diff] [blame] | 1066 | if (isa<UndefValue>(RHS) && CanEvaluateShuffled(LHS, Mask)) { |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1067 | Value *V = EvaluateInDifferentElementOrder(LHS, Mask); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1068 | return replaceInstUsesWith(SVI, V); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 1071 | // SROA generates shuffle+bitcast when the extracted sub-vector is bitcast to |
| 1072 | // a non-vector type. We can instead bitcast the original vector followed by |
| 1073 | // an extract of the desired element: |
| 1074 | // |
| 1075 | // %sroa = shufflevector <16 x i8> %in, <16 x i8> undef, |
| 1076 | // <4 x i32> <i32 0, i32 1, i32 2, i32 3> |
| 1077 | // %1 = bitcast <4 x i8> %sroa to i32 |
| 1078 | // Becomes: |
| 1079 | // %bc = bitcast <16 x i8> %in to <4 x i32> |
| 1080 | // %ext = extractelement <4 x i32> %bc, i32 0 |
| 1081 | // |
| 1082 | // If the shuffle is extracting a contiguous range of values from the input |
| 1083 | // vector then each use which is a bitcast of the extracted size can be |
| 1084 | // replaced. This will work if the vector types are compatible, and the begin |
| 1085 | // index is aligned to a value in the casted vector type. If the begin index |
| 1086 | // isn't aligned then we can shuffle the original vector (keeping the same |
| 1087 | // vector type) before extracting. |
| 1088 | // |
| 1089 | // This code will bail out if the target type is fundamentally incompatible |
| 1090 | // with vectors of the source type. |
| 1091 | // |
| 1092 | // Example of <16 x i8>, target type i32: |
| 1093 | // Index range [4,8): v-----------v Will work. |
| 1094 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 1095 | // <16 x i8>: | | | | | | | | | | | | | | | | | |
| 1096 | // <4 x i32>: | | | | | |
| 1097 | // +-----------+-----------+-----------+-----------+ |
| 1098 | // Index range [6,10): ^-----------^ Needs an extra shuffle. |
| 1099 | // Target type i40: ^--------------^ Won't work, bail. |
| 1100 | if (isShuffleExtractingFromLHS(SVI, Mask)) { |
| 1101 | Value *V = LHS; |
| 1102 | unsigned MaskElems = Mask.size(); |
| 1103 | unsigned BegIdx = Mask.front(); |
| 1104 | VectorType *SrcTy = cast<VectorType>(V->getType()); |
| 1105 | unsigned VecBitWidth = SrcTy->getBitWidth(); |
David Majnemer | 98cfe2b | 2015-04-03 20:18:40 +0000 | [diff] [blame] | 1106 | unsigned SrcElemBitWidth = DL.getTypeSizeInBits(SrcTy->getElementType()); |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 1107 | assert(SrcElemBitWidth && "vector elements must have a bitwidth"); |
| 1108 | unsigned SrcNumElems = SrcTy->getNumElements(); |
| 1109 | SmallVector<BitCastInst *, 8> BCs; |
| 1110 | DenseMap<Type *, Value *> NewBCs; |
| 1111 | for (User *U : SVI.users()) |
| 1112 | if (BitCastInst *BC = dyn_cast<BitCastInst>(U)) |
| 1113 | if (!BC->use_empty()) |
| 1114 | // Only visit bitcasts that weren't previously handled. |
| 1115 | BCs.push_back(BC); |
| 1116 | for (BitCastInst *BC : BCs) { |
| 1117 | Type *TgtTy = BC->getDestTy(); |
David Majnemer | 98cfe2b | 2015-04-03 20:18:40 +0000 | [diff] [blame] | 1118 | unsigned TgtElemBitWidth = DL.getTypeSizeInBits(TgtTy); |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 1119 | if (!TgtElemBitWidth) |
| 1120 | continue; |
| 1121 | unsigned TgtNumElems = VecBitWidth / TgtElemBitWidth; |
| 1122 | bool VecBitWidthsEqual = VecBitWidth == TgtNumElems * TgtElemBitWidth; |
| 1123 | bool BegIsAligned = 0 == ((SrcElemBitWidth * BegIdx) % TgtElemBitWidth); |
| 1124 | if (!VecBitWidthsEqual) |
| 1125 | continue; |
| 1126 | if (!VectorType::isValidElementType(TgtTy)) |
| 1127 | continue; |
| 1128 | VectorType *CastSrcTy = VectorType::get(TgtTy, TgtNumElems); |
| 1129 | if (!BegIsAligned) { |
| 1130 | // Shuffle the input so [0,NumElements) contains the output, and |
| 1131 | // [NumElems,SrcNumElems) is undef. |
| 1132 | SmallVector<Constant *, 16> ShuffleMask(SrcNumElems, |
| 1133 | UndefValue::get(Int32Ty)); |
| 1134 | for (unsigned I = 0, E = MaskElems, Idx = BegIdx; I != E; ++Idx, ++I) |
| 1135 | ShuffleMask[I] = ConstantInt::get(Int32Ty, Idx); |
| 1136 | V = Builder->CreateShuffleVector(V, UndefValue::get(V->getType()), |
| 1137 | ConstantVector::get(ShuffleMask), |
| 1138 | SVI.getName() + ".extract"); |
| 1139 | BegIdx = 0; |
| 1140 | } |
| 1141 | unsigned SrcElemsPerTgtElem = TgtElemBitWidth / SrcElemBitWidth; |
| 1142 | assert(SrcElemsPerTgtElem); |
| 1143 | BegIdx /= SrcElemsPerTgtElem; |
| 1144 | bool BCAlreadyExists = NewBCs.find(CastSrcTy) != NewBCs.end(); |
| 1145 | auto *NewBC = |
| 1146 | BCAlreadyExists |
| 1147 | ? NewBCs[CastSrcTy] |
| 1148 | : Builder->CreateBitCast(V, CastSrcTy, SVI.getName() + ".bc"); |
| 1149 | if (!BCAlreadyExists) |
| 1150 | NewBCs[CastSrcTy] = NewBC; |
| 1151 | auto *Ext = Builder->CreateExtractElement( |
| 1152 | NewBC, ConstantInt::get(Int32Ty, BegIdx), SVI.getName() + ".extract"); |
| 1153 | // The shufflevector isn't being replaced: the bitcast that used it |
| 1154 | // is. InstCombine will visit the newly-created instructions. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1155 | replaceInstUsesWith(*BC, Ext); |
JF Bastien | d52c990 | 2015-02-25 22:30:51 +0000 | [diff] [blame] | 1156 | MadeChange = true; |
| 1157 | } |
| 1158 | } |
| 1159 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 1160 | // If the LHS is a shufflevector itself, see if we can combine it with this |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1161 | // one without producing an unusual shuffle. |
| 1162 | // Cases that might be simplified: |
| 1163 | // 1. |
| 1164 | // x1=shuffle(v1,v2,mask1) |
| 1165 | // x=shuffle(x1,undef,mask) |
| 1166 | // ==> |
| 1167 | // x=shuffle(v1,undef,newMask) |
| 1168 | // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : -1 |
| 1169 | // 2. |
| 1170 | // x1=shuffle(v1,undef,mask1) |
| 1171 | // x=shuffle(x1,x2,mask) |
| 1172 | // where v1.size() == mask1.size() |
| 1173 | // ==> |
| 1174 | // x=shuffle(v1,x2,newMask) |
| 1175 | // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : mask[i] |
| 1176 | // 3. |
| 1177 | // x2=shuffle(v2,undef,mask2) |
| 1178 | // x=shuffle(x1,x2,mask) |
| 1179 | // where v2.size() == mask2.size() |
| 1180 | // ==> |
| 1181 | // x=shuffle(x1,v2,newMask) |
| 1182 | // newMask[i] = (mask[i] < x1.size()) |
| 1183 | // ? mask[i] : mask2[mask[i]-x1.size()]+x1.size() |
| 1184 | // 4. |
| 1185 | // x1=shuffle(v1,undef,mask1) |
| 1186 | // x2=shuffle(v2,undef,mask2) |
| 1187 | // x=shuffle(x1,x2,mask) |
| 1188 | // where v1.size() == v2.size() |
| 1189 | // ==> |
| 1190 | // x=shuffle(v1,v2,newMask) |
| 1191 | // newMask[i] = (mask[i] < x1.size()) |
| 1192 | // ? mask1[mask[i]] : mask2[mask[i]-x1.size()]+v1.size() |
| 1193 | // |
| 1194 | // Here we are really conservative: |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 1195 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 1196 | // program, because the code gen may not be smart enough to turn a merged |
| 1197 | // shuffle into two specific shuffles: it may produce worse code. As such, |
Jim Grosbach | d11584a | 2013-05-01 00:25:27 +0000 | [diff] [blame] | 1198 | // we only merge two shuffles if the result is either a splat or one of the |
| 1199 | // input shuffle masks. In this case, merging the shuffles just removes |
| 1200 | // one instruction, which we know is safe. This is good for things like |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1201 | // turning: (splat(splat)) -> splat, or |
| 1202 | // merge(V[0..n], V[n+1..2n]) -> V[0..2n] |
| 1203 | ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS); |
| 1204 | ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS); |
| 1205 | if (LHSShuffle) |
| 1206 | if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1207 | LHSShuffle = nullptr; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1208 | if (RHSShuffle) |
| 1209 | if (!isa<UndefValue>(RHSShuffle->getOperand(1))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1210 | RHSShuffle = nullptr; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1211 | if (!LHSShuffle && !RHSShuffle) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1212 | return MadeChange ? &SVI : nullptr; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1213 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1214 | Value* LHSOp0 = nullptr; |
| 1215 | Value* LHSOp1 = nullptr; |
| 1216 | Value* RHSOp0 = nullptr; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1217 | unsigned LHSOp0Width = 0; |
| 1218 | unsigned RHSOp0Width = 0; |
| 1219 | if (LHSShuffle) { |
| 1220 | LHSOp0 = LHSShuffle->getOperand(0); |
| 1221 | LHSOp1 = LHSShuffle->getOperand(1); |
| 1222 | LHSOp0Width = cast<VectorType>(LHSOp0->getType())->getNumElements(); |
| 1223 | } |
| 1224 | if (RHSShuffle) { |
| 1225 | RHSOp0 = RHSShuffle->getOperand(0); |
| 1226 | RHSOp0Width = cast<VectorType>(RHSOp0->getType())->getNumElements(); |
| 1227 | } |
| 1228 | Value* newLHS = LHS; |
| 1229 | Value* newRHS = RHS; |
| 1230 | if (LHSShuffle) { |
| 1231 | // case 1 |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 1232 | if (isa<UndefValue>(RHS)) { |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1233 | newLHS = LHSOp0; |
| 1234 | newRHS = LHSOp1; |
| 1235 | } |
| 1236 | // case 2 or 4 |
| 1237 | else if (LHSOp0Width == LHSWidth) { |
| 1238 | newLHS = LHSOp0; |
| 1239 | } |
| 1240 | } |
| 1241 | // case 3 or 4 |
| 1242 | if (RHSShuffle && RHSOp0Width == LHSWidth) { |
| 1243 | newRHS = RHSOp0; |
| 1244 | } |
| 1245 | // case 4 |
| 1246 | if (LHSOp0 == RHSOp0) { |
| 1247 | newLHS = LHSOp0; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1248 | newRHS = nullptr; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1249 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1250 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1251 | if (newLHS == LHS && newRHS == RHS) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1252 | return MadeChange ? &SVI : nullptr; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1253 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1254 | SmallVector<int, 16> LHSMask; |
| 1255 | SmallVector<int, 16> RHSMask; |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1256 | if (newLHS != LHS) |
| 1257 | LHSMask = LHSShuffle->getShuffleMask(); |
| 1258 | if (RHSShuffle && newRHS != RHS) |
| 1259 | RHSMask = RHSShuffle->getShuffleMask(); |
| 1260 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1261 | unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth; |
| 1262 | SmallVector<int, 16> newMask; |
| 1263 | bool isSplat = true; |
| 1264 | int SplatElt = -1; |
| 1265 | // Create a new mask for the new ShuffleVectorInst so that the new |
| 1266 | // ShuffleVectorInst is equivalent to the original one. |
| 1267 | for (unsigned i = 0; i < VWidth; ++i) { |
| 1268 | int eltMask; |
Craig Topper | 45d9f4b | 2013-01-18 05:30:07 +0000 | [diff] [blame] | 1269 | if (Mask[i] < 0) { |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1270 | // This element is an undef value. |
| 1271 | eltMask = -1; |
| 1272 | } else if (Mask[i] < (int)LHSWidth) { |
| 1273 | // This element is from left hand side vector operand. |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 1274 | // |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1275 | // If LHS is going to be replaced (case 1, 2, or 4), calculate the |
| 1276 | // new mask value for the element. |
| 1277 | if (newLHS != LHS) { |
| 1278 | eltMask = LHSMask[Mask[i]]; |
| 1279 | // If the value selected is an undef value, explicitly specify it |
| 1280 | // with a -1 mask value. |
| 1281 | if (eltMask >= (int)LHSOp0Width && isa<UndefValue>(LHSOp1)) |
| 1282 | eltMask = -1; |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 1283 | } else |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1284 | eltMask = Mask[i]; |
| 1285 | } else { |
| 1286 | // This element is from right hand side vector operand |
| 1287 | // |
| 1288 | // If the value selected is an undef value, explicitly specify it |
| 1289 | // with a -1 mask value. (case 1) |
| 1290 | if (isa<UndefValue>(RHS)) |
| 1291 | eltMask = -1; |
| 1292 | // If RHS is going to be replaced (case 3 or 4), calculate the |
| 1293 | // new mask value for the element. |
| 1294 | else if (newRHS != RHS) { |
| 1295 | eltMask = RHSMask[Mask[i]-LHSWidth]; |
| 1296 | // If the value selected is an undef value, explicitly specify it |
| 1297 | // with a -1 mask value. |
| 1298 | if (eltMask >= (int)RHSOp0Width) { |
| 1299 | assert(isa<UndefValue>(RHSShuffle->getOperand(1)) |
| 1300 | && "should have been check above"); |
| 1301 | eltMask = -1; |
Nate Begeman | 2a0ca3e9 | 2010-08-13 00:17:53 +0000 | [diff] [blame] | 1302 | } |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 1303 | } else |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1304 | eltMask = Mask[i]-LHSWidth; |
| 1305 | |
| 1306 | // If LHS's width is changed, shift the mask value accordingly. |
| 1307 | // If newRHS == NULL, i.e. LHSOp0 == RHSOp0, we want to remap any |
Michael Gottesman | 02a1141 | 2012-10-16 21:29:38 +0000 | [diff] [blame] | 1308 | // references from RHSOp0 to LHSOp0, so we don't need to shift the mask. |
| 1309 | // If newRHS == newLHS, we want to remap any references from newRHS to |
| 1310 | // newLHS so that we can properly identify splats that may occur due to |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1311 | // obfuscation across the two vectors. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1312 | if (eltMask >= 0 && newRHS != nullptr && newLHS != newRHS) |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1313 | eltMask += newLHSWidth; |
Nate Begeman | 2a0ca3e9 | 2010-08-13 00:17:53 +0000 | [diff] [blame] | 1314 | } |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1315 | |
| 1316 | // Check if this could still be a splat. |
| 1317 | if (eltMask >= 0) { |
| 1318 | if (SplatElt >= 0 && SplatElt != eltMask) |
| 1319 | isSplat = false; |
| 1320 | SplatElt = eltMask; |
| 1321 | } |
| 1322 | |
| 1323 | newMask.push_back(eltMask); |
| 1324 | } |
| 1325 | |
| 1326 | // If the result mask is equal to one of the original shuffle masks, |
Jim Grosbach | d11584a | 2013-05-01 00:25:27 +0000 | [diff] [blame] | 1327 | // or is a splat, do the replacement. |
| 1328 | if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) { |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1329 | SmallVector<Constant*, 16> Elts; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1330 | for (unsigned i = 0, e = newMask.size(); i != e; ++i) { |
| 1331 | if (newMask[i] < 0) { |
| 1332 | Elts.push_back(UndefValue::get(Int32Ty)); |
| 1333 | } else { |
| 1334 | Elts.push_back(ConstantInt::get(Int32Ty, newMask[i])); |
| 1335 | } |
| 1336 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1337 | if (!newRHS) |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1338 | newRHS = UndefValue::get(newLHS->getType()); |
| 1339 | return new ShuffleVectorInst(newLHS, newRHS, ConstantVector::get(Elts)); |
Nate Begeman | 2a0ca3e9 | 2010-08-13 00:17:53 +0000 | [diff] [blame] | 1340 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1341 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1342 | // If the result mask is an identity, replace uses of this instruction with |
| 1343 | // corresponding argument. |
Serge Pavlov | b575ee8 | 2014-05-13 06:07:21 +0000 | [diff] [blame] | 1344 | bool isLHSID, isRHSID; |
Sanjay Patel | 431e114 | 2015-11-17 17:24:08 +0000 | [diff] [blame] | 1345 | recognizeIdentityMask(newMask, isLHSID, isRHSID); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1346 | if (isLHSID && VWidth == LHSOp0Width) return replaceInstUsesWith(SVI, newLHS); |
| 1347 | if (isRHSID && VWidth == RHSOp0Width) return replaceInstUsesWith(SVI, newRHS); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1348 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1349 | return MadeChange ? &SVI : nullptr; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1350 | } |