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