blob: c5328c77abb7eb688189c4f0fb3c867bdcbbbc33 [file] [log] [blame]
Chris Lattner7e044912010-01-04 07:17:19 +00001//===- InstCombineSimplifyDemanded.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 contains logic for simplifying instructions based on information
11// about how they are used.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carrutha9174582015-01-22 05:25:13 +000015#include "InstCombineInternal.h"
James Molloy2b21a7c2015-05-20 18:41:25 +000016#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000018#include "llvm/IR/PatternMatch.h"
Chris Lattner7e044912010-01-04 07:17:19 +000019
20using namespace llvm;
Shuxin Yang63e999e2012-12-04 00:04:54 +000021using namespace llvm::PatternMatch;
Chris Lattner7e044912010-01-04 07:17:19 +000022
Chandler Carruth964daaa2014-04-22 02:55:47 +000023#define DEBUG_TYPE "instcombine"
24
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000025/// Check to see if the specified operand of the specified instruction is a
26/// constant integer. If so, check to see if there are any bits set in the
27/// constant that are not demanded. If so, shrink the constant and return true.
Craig Topper4c947752012-12-22 18:09:02 +000028static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
Chris Lattner7e044912010-01-04 07:17:19 +000029 APInt Demanded) {
30 assert(I && "No instruction?");
31 assert(OpNo < I->getNumOperands() && "Operand index too large");
32
Sanjay Patelae3b43e2017-02-09 21:43:06 +000033 // The operand must be a constant integer or splat integer.
34 Value *Op = I->getOperand(OpNo);
35 const APInt *C;
36 if (!match(Op, m_APInt(C)))
37 return false;
Chris Lattner7e044912010-01-04 07:17:19 +000038
39 // If there are no bits set that aren't demanded, nothing to do.
Sanjay Patelae3b43e2017-02-09 21:43:06 +000040 Demanded = Demanded.zextOrTrunc(C->getBitWidth());
41 if ((~Demanded & *C) == 0)
Chris Lattner7e044912010-01-04 07:17:19 +000042 return false;
43
44 // This instruction is producing bits that are not demanded. Shrink the RHS.
Sanjay Patelae3b43e2017-02-09 21:43:06 +000045 Demanded &= *C;
46 I->setOperand(OpNo, ConstantInt::get(Op->getType(), Demanded));
David Majnemer42b83a52014-08-22 07:56:32 +000047
Chris Lattner7e044912010-01-04 07:17:19 +000048 return true;
49}
50
51
52
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000053/// Inst is an integer instruction that SimplifyDemandedBits knows about. See if
54/// the instruction has any properties that allow us to simplify its operands.
Chris Lattner7e044912010-01-04 07:17:19 +000055bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
56 unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
57 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
58 APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
Craig Topper4c947752012-12-22 18:09:02 +000059
Mehdi Aminia28d91d2015-03-10 02:37:25 +000060 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, KnownZero, KnownOne,
61 0, &Inst);
Craig Topperf40110f2014-04-25 05:29:35 +000062 if (!V) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000063 if (V == &Inst) return true;
Sanjay Patel4b198802016-02-01 22:23:39 +000064 replaceInstUsesWith(Inst, V);
Chris Lattner7e044912010-01-04 07:17:19 +000065 return true;
66}
67
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000068/// This form of SimplifyDemandedBits simplifies the specified instruction
69/// operand if possible, updating it in place. It returns true if it made any
70/// change and false otherwise.
Craig Topper47596dd2017-03-25 06:52:52 +000071bool InstCombiner::SimplifyDemandedBits(Instruction *I, unsigned OpNo,
72 const APInt &DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +000073 APInt &KnownZero, APInt &KnownOne,
74 unsigned Depth) {
Craig Topper47596dd2017-03-25 06:52:52 +000075 Use &U = I->getOperandUse(OpNo);
David Majnemerfe58d132015-04-22 20:59:28 +000076 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, KnownZero,
Craig Topper47596dd2017-03-25 06:52:52 +000077 KnownOne, Depth, I);
Craig Topperf40110f2014-04-25 05:29:35 +000078 if (!NewVal) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000079 U = NewVal;
80 return true;
81}
82
83
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000084/// This function attempts to replace V with a simpler value based on the
85/// demanded bits. When this function is called, it is known that only the bits
86/// set in DemandedMask of the result of V are ever used downstream.
87/// Consequently, depending on the mask and V, it may be possible to replace V
88/// with a constant or one of its operands. In such cases, this function does
89/// the replacement and returns true. In all other cases, it returns false after
90/// analyzing the expression and setting KnownOne and known to be one in the
91/// expression. KnownZero contains all the bits that are known to be zero in the
92/// expression. These are provided to potentially allow the caller (which might
93/// recursively be SimplifyDemandedBits itself) to simplify the expression.
94/// KnownOne and KnownZero always follow the invariant that:
95/// KnownOne & KnownZero == 0.
96/// That is, a bit can't be both 1 and 0. Note that the bits in KnownOne and
97/// KnownZero may only be accurate for those bits set in DemandedMask. Note also
98/// that the bitwidth of V, DemandedMask, KnownZero and KnownOne must all be the
99/// same.
Chris Lattner7e044912010-01-04 07:17:19 +0000100///
101/// This returns null if it did not change anything and it permits no
102/// simplification. This returns V itself if it did some simplification of V's
103/// operands based on the information about what bits are demanded. This returns
104/// some other non-null value if it found out that V is equal to another value
105/// in the context where the specified bits are demanded, but not for all users.
106Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
107 APInt &KnownZero, APInt &KnownOne,
Hal Finkel60db0582014-09-07 18:57:58 +0000108 unsigned Depth,
109 Instruction *CxtI) {
Craig Toppere73658d2014-04-28 04:05:08 +0000110 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000111 assert(Depth <= 6 && "Limit Search Depth");
112 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000113 Type *VTy = V->getType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000114 assert(
115 (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
116 KnownZero.getBitWidth() == BitWidth &&
117 KnownOne.getBitWidth() == BitWidth &&
118 "Value *V, DemandedMask, KnownZero and KnownOne "
119 "must have same BitWidth");
Craig Topper83dc1c62017-04-20 16:14:58 +0000120
121 if (isa<Constant>(V)) {
122 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000123 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000124 }
125
Jay Foad25a5e4c2010-12-01 08:53:58 +0000126 KnownZero.clearAllBits();
127 KnownOne.clearAllBits();
Craig Topper83dc1c62017-04-20 16:14:58 +0000128 if (DemandedMask == 0) // Not demanding any bits from V.
Chris Lattner7e044912010-01-04 07:17:19 +0000129 return UndefValue::get(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000130
Chris Lattner7e044912010-01-04 07:17:19 +0000131 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000132 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000133
Chris Lattner7e044912010-01-04 07:17:19 +0000134 Instruction *I = dyn_cast<Instruction>(V);
135 if (!I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000136 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000137 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000138 }
139
140 // If there are multiple uses of this value and we aren't at the root, then
141 // we can't do any simplifications of the operands, because DemandedMask
142 // only reflects the bits demanded by *one* of the users.
143 if (Depth != 0 && !I->hasOneUse()) {
Craig Topperb0076fe2017-04-12 18:05:21 +0000144 return SimplifyMultipleUseDemandedBits(I, DemandedMask, KnownZero, KnownOne,
145 Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000146 }
Craig Topper4c947752012-12-22 18:09:02 +0000147
Craig Topperb0076fe2017-04-12 18:05:21 +0000148 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
149 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
150
Chris Lattner7e044912010-01-04 07:17:19 +0000151 // If this is the root being simplified, allow it to have multiple uses,
152 // just set the DemandedMask to all bits so that we can try to simplify the
153 // operands. This allows visitTruncInst (for example) to simplify the
154 // operand of a trunc without duplicating all the logic below.
155 if (Depth == 0 && !V->hasOneUse())
Craig Toppere06b6bc2017-04-04 05:03:02 +0000156 DemandedMask.setAllBits();
Craig Topper4c947752012-12-22 18:09:02 +0000157
Chris Lattner7e044912010-01-04 07:17:19 +0000158 switch (I->getOpcode()) {
159 default:
Hal Finkel60db0582014-09-07 18:57:58 +0000160 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000161 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000162 case Instruction::And: {
Chris Lattner7e044912010-01-04 07:17:19 +0000163 // If either the LHS or the RHS are Zero, the result is zero.
Craig Topper47596dd2017-03-25 06:52:52 +0000164 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
165 Depth + 1) ||
166 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnownZero, LHSKnownZero,
167 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000168 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000169 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
170 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000171
Craig Topper9a458cd2017-04-14 22:34:14 +0000172 // Output known-0 are known to be clear if zero in either the LHS | RHS.
173 APInt IKnownZero = RHSKnownZero | LHSKnownZero;
174 // Output known-1 bits are only known if set in both the LHS & RHS.
175 APInt IKnownOne = RHSKnownOne & LHSKnownOne;
176
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000177 // If the client is only demanding bits that we know, return the known
178 // constant.
Craig Topper9a458cd2017-04-14 22:34:14 +0000179 if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
180 return Constant::getIntegerValue(VTy, IKnownOne);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000181
Chris Lattner7e044912010-01-04 07:17:19 +0000182 // If all of the demanded bits are known 1 on one side, return the other.
183 // These bits cannot contribute to the result of the 'and'.
Craig Topper4c947752012-12-22 18:09:02 +0000184 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000185 (DemandedMask & ~LHSKnownZero))
186 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000187 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000188 (DemandedMask & ~RHSKnownZero))
189 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000190
Chris Lattner7e044912010-01-04 07:17:19 +0000191 // If the RHS is a constant, see if we can simplify it.
192 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
193 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000194
Craig Topper9a458cd2017-04-14 22:34:14 +0000195 KnownZero = std::move(IKnownZero);
196 KnownOne = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000197 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000198 }
199 case Instruction::Or: {
Chris Lattner7e044912010-01-04 07:17:19 +0000200 // If either the LHS or the RHS are One, the result is One.
Craig Topper47596dd2017-03-25 06:52:52 +0000201 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
202 Depth + 1) ||
203 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnownOne, LHSKnownZero,
204 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000205 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000206 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
207 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
208
Craig Topper9a458cd2017-04-14 22:34:14 +0000209 // Output known-0 bits are only known if clear in both the LHS & RHS.
210 APInt IKnownZero = RHSKnownZero & LHSKnownZero;
211 // Output known-1 are known to be set if set in either the LHS | RHS.
212 APInt IKnownOne = RHSKnownOne | LHSKnownOne;
213
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000214 // If the client is only demanding bits that we know, return the known
215 // constant.
Craig Topper9a458cd2017-04-14 22:34:14 +0000216 if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
217 return Constant::getIntegerValue(VTy, IKnownOne);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000218
Chris Lattner7e044912010-01-04 07:17:19 +0000219 // If all of the demanded bits are known zero on one side, return the other.
220 // These bits cannot contribute to the result of the 'or'.
Craig Topper4c947752012-12-22 18:09:02 +0000221 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000222 (DemandedMask & ~LHSKnownOne))
223 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000224 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000225 (DemandedMask & ~RHSKnownOne))
226 return I->getOperand(1);
227
228 // If all of the potentially set bits on one side are known to be set on
229 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000230 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000231 (DemandedMask & (~RHSKnownZero)))
232 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000233 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000234 (DemandedMask & (~LHSKnownZero)))
235 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000236
Chris Lattner7e044912010-01-04 07:17:19 +0000237 // If the RHS is a constant, see if we can simplify it.
238 if (ShrinkDemandedConstant(I, 1, DemandedMask))
239 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000240
Craig Topper9a458cd2017-04-14 22:34:14 +0000241 KnownZero = std::move(IKnownZero);
242 KnownOne = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000243 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000244 }
Chris Lattner7e044912010-01-04 07:17:19 +0000245 case Instruction::Xor: {
Craig Topper47596dd2017-03-25 06:52:52 +0000246 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
247 Depth + 1) ||
248 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnownZero, LHSKnownOne,
249 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000250 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000251 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
252 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
253
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000254 // Output known-0 bits are known if clear or set in both the LHS & RHS.
255 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
256 (RHSKnownOne & LHSKnownOne);
257 // Output known-1 are known to be set if set in only one of the LHS, RHS.
258 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
259 (RHSKnownOne & LHSKnownZero);
260
261 // If the client is only demanding bits that we know, return the known
262 // constant.
263 if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
264 return Constant::getIntegerValue(VTy, IKnownOne);
265
Chris Lattner7e044912010-01-04 07:17:19 +0000266 // If all of the demanded bits are known zero on one side, return the other.
267 // These bits cannot contribute to the result of the 'xor'.
268 if ((DemandedMask & RHSKnownZero) == DemandedMask)
269 return I->getOperand(0);
270 if ((DemandedMask & LHSKnownZero) == DemandedMask)
271 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000272
Chris Lattner7e044912010-01-04 07:17:19 +0000273 // If all of the demanded bits are known to be zero on one side or the
274 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000275 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Chris Lattner7e044912010-01-04 07:17:19 +0000276 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
Craig Topper4c947752012-12-22 18:09:02 +0000277 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000278 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
279 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000280 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000281 }
Craig Topper4c947752012-12-22 18:09:02 +0000282
Chris Lattner7e044912010-01-04 07:17:19 +0000283 // If all of the demanded bits on one side are known, and all of the set
284 // bits on that side are also known to be set on the other side, turn this
285 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000286 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper4c947752012-12-22 18:09:02 +0000287 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
Chris Lattner7e044912010-01-04 07:17:19 +0000288 // all known
289 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
290 Constant *AndC = Constant::getIntegerValue(VTy,
291 ~RHSKnownOne & DemandedMask);
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000292 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000293 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000294 }
295 }
Craig Topper4c947752012-12-22 18:09:02 +0000296
Chris Lattner7e044912010-01-04 07:17:19 +0000297 // If the RHS is a constant, see if we can simplify it.
298 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
299 if (ShrinkDemandedConstant(I, 1, DemandedMask))
300 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000301
Chris Lattner7e044912010-01-04 07:17:19 +0000302 // If our LHS is an 'and' and if it has one use, and if any of the bits we
303 // are flipping are known to be set, then the xor is just resetting those
304 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
305 // simplifying both of them.
306 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
307 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
308 isa<ConstantInt>(I->getOperand(1)) &&
309 isa<ConstantInt>(LHSInst->getOperand(1)) &&
310 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
311 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
312 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
313 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000314
Chris Lattner7e044912010-01-04 07:17:19 +0000315 Constant *AndC =
316 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000317 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000318 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000319
Chris Lattner7e044912010-01-04 07:17:19 +0000320 Constant *XorC =
321 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000322 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000323 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000324 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000325
326 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topper9a458cd2017-04-14 22:34:14 +0000327 KnownZero = std::move(IKnownZero);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000328 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topper9a458cd2017-04-14 22:34:14 +0000329 KnownOne = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000330 break;
331 }
332 case Instruction::Select:
James Molloy2b21a7c2015-05-20 18:41:25 +0000333 // If this is a select as part of a min/max pattern, don't simplify any
334 // further in case we break the structure.
335 Value *LHS, *RHS;
James Molloy134bec22015-08-11 09:12:57 +0000336 if (matchSelectPattern(I, LHS, RHS).Flavor != SPF_UNKNOWN)
James Molloy2b21a7c2015-05-20 18:41:25 +0000337 return nullptr;
Simon Pilgrim61116dd2015-09-17 20:32:45 +0000338
Craig Topper47596dd2017-03-25 06:52:52 +0000339 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnownZero, RHSKnownOne,
340 Depth + 1) ||
341 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnownZero, LHSKnownOne,
342 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000343 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000344 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
345 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
346
Chris Lattner7e044912010-01-04 07:17:19 +0000347 // If the operands are constants, see if we can simplify them.
348 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
349 ShrinkDemandedConstant(I, 2, DemandedMask))
350 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000351
Chris Lattner7e044912010-01-04 07:17:19 +0000352 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000353 KnownOne = RHSKnownOne & LHSKnownOne;
354 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000355 break;
356 case Instruction::Trunc: {
357 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000358 DemandedMask = DemandedMask.zext(truncBf);
359 KnownZero = KnownZero.zext(truncBf);
360 KnownOne = KnownOne.zext(truncBf);
Craig Topper47596dd2017-03-25 06:52:52 +0000361 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
362 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000363 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000364 DemandedMask = DemandedMask.trunc(BitWidth);
365 KnownZero = KnownZero.trunc(BitWidth);
366 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000367 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000368 break;
369 }
370 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000371 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000372 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000373
Chris Lattner229907c2011-07-18 04:54:35 +0000374 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
375 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000376 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
377 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
378 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000379 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000380 } else
381 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000382 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000383 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000384 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000385 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000386
Craig Topper47596dd2017-03-25 06:52:52 +0000387 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
388 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000389 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000390 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000391 break;
392 case Instruction::ZExt: {
393 // Compute the bits in the result that are not present in the input.
394 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000395
Jay Foad583abbc2010-12-07 08:25:19 +0000396 DemandedMask = DemandedMask.trunc(SrcBitWidth);
397 KnownZero = KnownZero.trunc(SrcBitWidth);
398 KnownOne = KnownOne.trunc(SrcBitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000399 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
400 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000401 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000402 DemandedMask = DemandedMask.zext(BitWidth);
403 KnownZero = KnownZero.zext(BitWidth);
404 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000405 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000406 // The top bits are known to be zero.
Craig Topper3a86a042017-03-19 05:49:16 +0000407 KnownZero.setBitsFrom(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000408 break;
409 }
410 case Instruction::SExt: {
411 // Compute the bits in the result that are not present in the input.
412 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000413
414 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000415 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
416
Craig Topper3a86a042017-03-19 05:49:16 +0000417 APInt NewBits(APInt::getBitsSetFrom(BitWidth, SrcBitWidth));
Chris Lattner7e044912010-01-04 07:17:19 +0000418 // If any of the sign extended bits are demanded, we know that the sign
419 // bit is demanded.
420 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000421 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000422
Jay Foad583abbc2010-12-07 08:25:19 +0000423 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
424 KnownZero = KnownZero.trunc(SrcBitWidth);
425 KnownOne = KnownOne.trunc(SrcBitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000426 if (SimplifyDemandedBits(I, 0, InputDemandedBits, KnownZero, KnownOne,
427 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000428 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000429 InputDemandedBits = InputDemandedBits.zext(BitWidth);
430 KnownZero = KnownZero.zext(BitWidth);
431 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000432 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
433
Chris Lattner7e044912010-01-04 07:17:19 +0000434 // If the sign bit of the input is known set or clear, then we know the
435 // top bits of the result.
436
437 // If the input sign bit is known zero, or if the NewBits are not demanded
438 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000439 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000440 // Convert to ZExt cast
441 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000442 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000443 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
444 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000445 }
446 break;
447 }
Matthias Braune48484c2015-04-30 22:05:30 +0000448 case Instruction::Add:
449 case Instruction::Sub: {
450 /// If the high-bits of an ADD/SUB are not demanded, then we do not care
451 /// about the high bits of the operands.
Chris Lattner7e044912010-01-04 07:17:19 +0000452 unsigned NLZ = DemandedMask.countLeadingZeros();
Matthias Braune48484c2015-04-30 22:05:30 +0000453 if (NLZ > 0) {
454 // Right fill the mask of bits for this ADD/SUB to demand the most
Chris Lattner7e044912010-01-04 07:17:19 +0000455 // significant bit and all those below it.
Chris Lattner7e044912010-01-04 07:17:19 +0000456 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
Craig Topper07f29152017-03-22 04:03:53 +0000457 if (ShrinkDemandedConstant(I, 0, DemandedFromOps) ||
Craig Topper47596dd2017-03-25 06:52:52 +0000458 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnownZero, LHSKnownOne,
459 Depth + 1) ||
Matthias Braune48484c2015-04-30 22:05:30 +0000460 ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
Craig Topper845033a2017-04-12 16:49:59 +0000461 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnownZero, RHSKnownOne,
Craig Topper47596dd2017-03-25 06:52:52 +0000462 Depth + 1)) {
Matthias Braune48484c2015-04-30 22:05:30 +0000463 // Disable the nsw and nuw flags here: We can no longer guarantee that
464 // we won't wrap after simplification. Removing the nsw/nuw flags is
465 // legal here because the top bit is not demanded.
466 BinaryOperator &BinOP = *cast<BinaryOperator>(I);
467 BinOP.setHasNoSignedWrap(false);
468 BinOP.setHasNoUnsignedWrap(false);
Chris Lattner7e044912010-01-04 07:17:19 +0000469 return I;
David Majnemer7d0e99c2015-04-22 22:42:05 +0000470 }
Craig Topper845033a2017-04-12 16:49:59 +0000471
472 // If we are known to be adding/subtracting zeros to every bit below
473 // the highest demanded bit, we just return the other side.
474 if ((DemandedFromOps & RHSKnownZero) == DemandedFromOps)
475 return I->getOperand(0);
476 // We can't do this with the LHS for subtraction.
477 if (I->getOpcode() == Instruction::Add &&
478 (DemandedFromOps & LHSKnownZero) == DemandedFromOps)
479 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000480 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000481
Craig Topper8fbb74b2017-03-24 22:12:10 +0000482 // Otherwise just hand the add/sub off to computeKnownBits to fill in
483 // the known zeros and ones.
484 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000485 break;
Matthias Braune48484c2015-04-30 22:05:30 +0000486 }
Chris Lattner7e044912010-01-04 07:17:19 +0000487 case Instruction::Shl:
488 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000489 {
490 Value *VarX; ConstantInt *C1;
491 if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
492 Instruction *Shr = cast<Instruction>(I->getOperand(0));
493 Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
494 KnownZero, KnownOne);
495 if (R)
496 return R;
497 }
498 }
499
Chris Lattner768003c2011-02-10 05:09:34 +0000500 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000501 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000502
Chris Lattner768003c2011-02-10 05:09:34 +0000503 // If the shift is NUW/NSW, then it does demand the high bits.
504 ShlOperator *IOp = cast<ShlOperator>(I);
505 if (IOp->hasNoSignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000506 DemandedMaskIn.setHighBits(ShiftAmt+1);
Chris Lattner768003c2011-02-10 05:09:34 +0000507 else if (IOp->hasNoUnsignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000508 DemandedMaskIn.setHighBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000509
Craig Topper47596dd2017-03-25 06:52:52 +0000510 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
511 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000512 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000513 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
514 KnownZero <<= ShiftAmt;
515 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000516 // low bits known zero.
517 if (ShiftAmt)
Craig Topper3a86a042017-03-19 05:49:16 +0000518 KnownZero.setLowBits(ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000519 }
520 break;
521 case Instruction::LShr:
522 // For a logical shift right
523 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000524 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000525
Chris Lattner7e044912010-01-04 07:17:19 +0000526 // Unsigned shift right.
527 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000528
Chris Lattner768003c2011-02-10 05:09:34 +0000529 // If the shift is exact, then it does demand the low bits (and knows that
530 // they are zero).
531 if (cast<LShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000532 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000533
Craig Topper47596dd2017-03-25 06:52:52 +0000534 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
535 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000536 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000537 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Craig Topperfc947bc2017-04-18 17:14:21 +0000538 KnownZero.lshrInPlace(ShiftAmt);
539 KnownOne.lshrInPlace(ShiftAmt);
Craig Topper3a86a042017-03-19 05:49:16 +0000540 if (ShiftAmt)
541 KnownZero.setHighBits(ShiftAmt); // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000542 }
543 break;
544 case Instruction::AShr:
545 // If this is an arithmetic shift right and only the low-bit is set, we can
546 // always convert this into a logical shr, even if the shift amount is
547 // variable. The low bit of the shift cannot be an input sign bit unless
548 // the shift amount is >= the size of the datatype, which is undefined.
549 if (DemandedMask == 1) {
550 // Perform the logical shift right.
551 Instruction *NewVal = BinaryOperator::CreateLShr(
552 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000553 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000554 }
Chris Lattner7e044912010-01-04 07:17:19 +0000555
556 // If the sign bit is the only bit demanded by this ashr, then there is no
557 // need to do it, the shift doesn't change the high bit.
558 if (DemandedMask.isSignBit())
559 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000560
Chris Lattner7e044912010-01-04 07:17:19 +0000561 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000562 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000563
Chris Lattner7e044912010-01-04 07:17:19 +0000564 // Signed shift right.
565 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
566 // If any of the "high bits" are demanded, we should set the sign bit as
567 // demanded.
568 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Craig Topperc9a4fc02017-04-14 05:09:04 +0000569 DemandedMaskIn.setSignBit();
Craig Topper4c947752012-12-22 18:09:02 +0000570
Chris Lattner768003c2011-02-10 05:09:34 +0000571 // If the shift is exact, then it does demand the low bits (and knows that
572 // they are zero).
573 if (cast<AShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000574 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000575
Craig Topper47596dd2017-03-25 06:52:52 +0000576 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
577 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000578 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000579 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000580 // Compute the new bits that are at the top now.
581 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Craig Topperfc947bc2017-04-18 17:14:21 +0000582 KnownZero.lshrInPlace(ShiftAmt);
583 KnownOne.lshrInPlace(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000584
Chris Lattner7e044912010-01-04 07:17:19 +0000585 // Handle the sign bits.
586 APInt SignBit(APInt::getSignBit(BitWidth));
587 // Adjust to where it is now in the mask.
Craig Topperfc947bc2017-04-18 17:14:21 +0000588 SignBit.lshrInPlace(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000589
Chris Lattner7e044912010-01-04 07:17:19 +0000590 // If the input sign bit is known to be zero, or if none of the top bits
591 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000592 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Chris Lattner7e044912010-01-04 07:17:19 +0000593 (HighBits & ~DemandedMask) == HighBits) {
594 // Perform the logical shift right.
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000595 BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
596 SA, I->getName());
597 NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000598 return InsertNewInstWith(NewVal, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000599 } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
600 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000601 }
602 }
603 break;
604 case Instruction::SRem:
605 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000606 // X % -1 demands all the bits because we don't want to introduce
607 // INT_MIN % -1 (== undef) by accident.
608 if (Rem->isAllOnesValue())
609 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000610 APInt RA = Rem->getValue().abs();
611 if (RA.isPowerOf2()) {
612 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
613 return I->getOperand(0);
614
615 APInt LowBits = RA - 1;
616 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000617 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnownZero, LHSKnownOne,
618 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000619 return I;
620
Duncan Sands3a48b872010-01-28 17:22:42 +0000621 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000622 KnownZero = LHSKnownZero & LowBits;
623 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000624
Duncan Sands3a48b872010-01-28 17:22:42 +0000625 // If LHS is non-negative or has all low bits zero, then the upper bits
626 // are all zero.
Craig Topperd23004c2017-04-17 16:38:20 +0000627 if (LHSKnownZero.isSignBitSet() || ((LHSKnownZero & LowBits) == LowBits))
Duncan Sands3a48b872010-01-28 17:22:42 +0000628 KnownZero |= ~LowBits;
629
630 // If LHS is negative and not all low bits are zero, then the upper bits
631 // are all one.
Craig Topperd23004c2017-04-17 16:38:20 +0000632 if (LHSKnownOne.isSignBitSet() && ((LHSKnownOne & LowBits) != 0))
Duncan Sands3a48b872010-01-28 17:22:42 +0000633 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000634
Craig Topper4c947752012-12-22 18:09:02 +0000635 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Craig Topperda886c62017-04-16 21:46:12 +0000636 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000637 }
638 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000639
640 // The sign bit is the LHS's sign bit, except when the result of the
641 // remainder is zero.
Craig Topperd23004c2017-04-17 16:38:20 +0000642 if (DemandedMask.isSignBitSet()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000643 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000644 CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000645 // If it's known zero, our sign bit is also zero.
Craig Topperd23004c2017-04-17 16:38:20 +0000646 if (LHSKnownZero.isSignBitSet())
Craig Topper3a86a042017-03-19 05:49:16 +0000647 KnownZero.setSignBit();
Nick Lewyckye4679792011-03-07 01:50:10 +0000648 }
Chris Lattner7e044912010-01-04 07:17:19 +0000649 break;
650 case Instruction::URem: {
651 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
652 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000653 if (SimplifyDemandedBits(I, 0, AllOnes, KnownZero2, KnownOne2, Depth + 1) ||
654 SimplifyDemandedBits(I, 1, AllOnes, KnownZero2, KnownOne2, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000655 return I;
656
657 unsigned Leaders = KnownZero2.countLeadingOnes();
Chris Lattner7e044912010-01-04 07:17:19 +0000658 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
659 break;
660 }
661 case Instruction::Call:
662 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
663 switch (II->getIntrinsicID()) {
664 default: break;
665 case Intrinsic::bswap: {
666 // If the only bits demanded come from one byte of the bswap result,
667 // just shift the input byte into position to eliminate the bswap.
668 unsigned NLZ = DemandedMask.countLeadingZeros();
669 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000670
Chris Lattner7e044912010-01-04 07:17:19 +0000671 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
672 // we need all the bits down to bit 8. Likewise, round NLZ. If we
673 // have 14 leading zeros, round to 8.
674 NLZ &= ~7;
675 NTZ &= ~7;
676 // If we need exactly one byte, we can do this transformation.
677 if (BitWidth-NLZ-NTZ == 8) {
678 unsigned ResultBit = NTZ;
679 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000680
Chris Lattner7e044912010-01-04 07:17:19 +0000681 // Replace this with either a left or right shift to get the byte into
682 // the right place.
683 Instruction *NewVal;
684 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000685 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000686 ConstantInt::get(I->getType(), InputBit-ResultBit));
687 else
Gabor Greif79430172010-06-24 12:35:13 +0000688 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000689 ConstantInt::get(I->getType(), ResultBit-InputBit));
690 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000691 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000692 }
Craig Topper4c947752012-12-22 18:09:02 +0000693
Chris Lattner7e044912010-01-04 07:17:19 +0000694 // TODO: Could compute known zero/one bits based on the input.
695 break;
696 }
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000697 case Intrinsic::x86_mmx_pmovmskb:
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000698 case Intrinsic::x86_sse_movmsk_ps:
699 case Intrinsic::x86_sse2_movmsk_pd:
700 case Intrinsic::x86_sse2_pmovmskb_128:
701 case Intrinsic::x86_avx_movmsk_ps_256:
702 case Intrinsic::x86_avx_movmsk_pd_256:
703 case Intrinsic::x86_avx2_pmovmskb: {
704 // MOVMSK copies the vector elements' sign bits to the low bits
705 // and zeros the high bits.
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000706 unsigned ArgWidth;
707 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
708 ArgWidth = 8; // Arg is x86_mmx, but treated as <8 x i8>.
709 } else {
710 auto Arg = II->getArgOperand(0);
711 auto ArgType = cast<VectorType>(Arg->getType());
712 ArgWidth = ArgType->getNumElements();
713 }
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000714
715 // If we don't need any of low bits then return zero,
716 // we know that DemandedMask is non-zero already.
717 APInt DemandedElts = DemandedMask.zextOrTrunc(ArgWidth);
718 if (DemandedElts == 0)
719 return ConstantInt::getNullValue(VTy);
720
Ahmed Bougacha17482a52016-04-28 14:36:07 +0000721 // We know that the upper bits are set to zero.
Craig Topper3a86a042017-03-19 05:49:16 +0000722 KnownZero.setBitsFrom(ArgWidth);
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000723 return nullptr;
724 }
Chad Rosierb3628842011-05-26 23:13:19 +0000725 case Intrinsic::x86_sse42_crc32_64_64:
Craig Topper3a86a042017-03-19 05:49:16 +0000726 KnownZero.setBitsFrom(32);
Craig Topperf40110f2014-04-25 05:29:35 +0000727 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000728 }
729 }
Hal Finkel60db0582014-09-07 18:57:58 +0000730 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000731 break;
732 }
Craig Topper4c947752012-12-22 18:09:02 +0000733
Chris Lattner7e044912010-01-04 07:17:19 +0000734 // If the client is only demanding bits that we know, return the known
735 // constant.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000736 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
737 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000738 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000739}
740
Craig Topperb0076fe2017-04-12 18:05:21 +0000741/// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
742/// bits. It also tries to handle simplifications that can be done based on
743/// DemandedMask, but without modifying the Instruction.
744Value *InstCombiner::SimplifyMultipleUseDemandedBits(Instruction *I,
745 const APInt &DemandedMask,
746 APInt &KnownZero,
747 APInt &KnownOne,
748 unsigned Depth,
749 Instruction *CxtI) {
750 unsigned BitWidth = DemandedMask.getBitWidth();
751 Type *ITy = I->getType();
752
753 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
754 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
755
756 // Despite the fact that we can't simplify this instruction in all User's
757 // context, we can at least compute the knownzero/knownone bits, and we can
758 // do simplifications that apply to *just* the one user if we know that
759 // this instruction has a simpler value in that context.
Craig Topperf35a7f72017-04-12 18:25:25 +0000760 switch (I->getOpcode()) {
Craig Topper9a458cd2017-04-14 22:34:14 +0000761 case Instruction::And: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000762 // If either the LHS or the RHS are Zero, the result is zero.
763 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
764 CxtI);
765 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
766 CxtI);
767
Craig Topper9a458cd2017-04-14 22:34:14 +0000768 // Output known-0 are known to be clear if zero in either the LHS | RHS.
769 APInt IKnownZero = RHSKnownZero | LHSKnownZero;
770 // Output known-1 bits are only known if set in both the LHS & RHS.
771 APInt IKnownOne = RHSKnownOne & LHSKnownOne;
772
Craig Topperc75f94b2017-04-12 19:32:47 +0000773 // If the client is only demanding bits that we know, return the known
774 // constant.
Craig Topper9a458cd2017-04-14 22:34:14 +0000775 if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
776 return Constant::getIntegerValue(ITy, IKnownOne);
Craig Topperc75f94b2017-04-12 19:32:47 +0000777
Craig Topperb0076fe2017-04-12 18:05:21 +0000778 // If all of the demanded bits are known 1 on one side, return the other.
779 // These bits cannot contribute to the result of the 'and' in this
780 // context.
781 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
782 (DemandedMask & ~LHSKnownZero))
783 return I->getOperand(0);
784 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
785 (DemandedMask & ~RHSKnownZero))
786 return I->getOperand(1);
787
Craig Topper9a458cd2017-04-14 22:34:14 +0000788 KnownZero = std::move(IKnownZero);
789 KnownOne = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000790 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000791 }
792 case Instruction::Or: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000793 // We can simplify (X|Y) -> X or Y in the user's context if we know that
794 // only bits from X or Y are demanded.
795
796 // If either the LHS or the RHS are One, the result is One.
797 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
798 CxtI);
799 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
800 CxtI);
801
Craig Topper9a458cd2017-04-14 22:34:14 +0000802 // Output known-0 bits are only known if clear in both the LHS & RHS.
803 APInt IKnownZero = RHSKnownZero & LHSKnownZero;
804 // Output known-1 are known to be set if set in either the LHS | RHS.
805 APInt IKnownOne = RHSKnownOne | LHSKnownOne;
806
Craig Topperc75f94b2017-04-12 19:32:47 +0000807 // If the client is only demanding bits that we know, return the known
808 // constant.
Craig Topper9a458cd2017-04-14 22:34:14 +0000809 if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
810 return Constant::getIntegerValue(ITy, IKnownOne);
Craig Topperc75f94b2017-04-12 19:32:47 +0000811
Craig Topperb0076fe2017-04-12 18:05:21 +0000812 // If all of the demanded bits are known zero on one side, return the
813 // other. These bits cannot contribute to the result of the 'or' in this
814 // context.
815 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
816 (DemandedMask & ~LHSKnownOne))
817 return I->getOperand(0);
818 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
819 (DemandedMask & ~RHSKnownOne))
820 return I->getOperand(1);
821
822 // If all of the potentially set bits on one side are known to be set on
823 // the other side, just use the 'other' side.
824 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
825 (DemandedMask & (~RHSKnownZero)))
826 return I->getOperand(0);
827 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
828 (DemandedMask & (~LHSKnownZero)))
829 return I->getOperand(1);
Craig Topperf35a7f72017-04-12 18:25:25 +0000830
Craig Topper9a458cd2017-04-14 22:34:14 +0000831 KnownZero = std::move(IKnownZero);
832 KnownOne = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000833 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000834 }
Craig Topperc75f94b2017-04-12 19:32:47 +0000835 case Instruction::Xor: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000836 // We can simplify (X^Y) -> X or Y in the user's context if we know that
837 // only bits from X or Y are demanded.
838
839 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
840 CxtI);
841 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
842 CxtI);
843
Craig Topperc75f94b2017-04-12 19:32:47 +0000844 // Output known-0 bits are known if clear or set in both the LHS & RHS.
845 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
846 (RHSKnownOne & LHSKnownOne);
847 // Output known-1 are known to be set if set in only one of the LHS, RHS.
848 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
849 (RHSKnownOne & LHSKnownZero);
850
851 // If the client is only demanding bits that we know, return the known
852 // constant.
853 if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
854 return Constant::getIntegerValue(ITy, IKnownOne);
855
Craig Topperb0076fe2017-04-12 18:05:21 +0000856 // If all of the demanded bits are known zero on one side, return the
857 // other.
858 if ((DemandedMask & RHSKnownZero) == DemandedMask)
859 return I->getOperand(0);
860 if ((DemandedMask & LHSKnownZero) == DemandedMask)
861 return I->getOperand(1);
Craig Topperf35a7f72017-04-12 18:25:25 +0000862
Craig Topperc75f94b2017-04-12 19:32:47 +0000863 // Output known-0 bits are known if clear or set in both the LHS & RHS.
864 KnownZero = std::move(IKnownZero);
865 // Output known-1 are known to be set if set in only one of the LHS, RHS.
866 KnownOne = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000867 break;
Craig Topperb0076fe2017-04-12 18:05:21 +0000868 }
Craig Topperc75f94b2017-04-12 19:32:47 +0000869 default:
870 // Compute the KnownZero/KnownOne bits to simplify things downstream.
871 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Craig Topperb0076fe2017-04-12 18:05:21 +0000872
Craig Topperc75f94b2017-04-12 19:32:47 +0000873 // If this user is only demanding bits that we know, return the known
874 // constant.
875 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
876 return Constant::getIntegerValue(ITy, KnownOne);
Craig Topper9a51c7f2017-04-12 18:17:46 +0000877
Craig Topperc75f94b2017-04-12 19:32:47 +0000878 break;
879 }
Craig Topper9a51c7f2017-04-12 18:17:46 +0000880
Craig Topperb0076fe2017-04-12 18:05:21 +0000881 return nullptr;
882}
883
884
Shuxin Yang63e999e2012-12-04 00:04:54 +0000885/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
886/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
887/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
888/// of "C2-C1".
889///
890/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
891/// ..., bn}, without considering the specific value X is holding.
892/// This transformation is legal iff one of following conditions is hold:
893/// 1) All the bit in S are 0, in this case E1 == E2.
894/// 2) We don't care those bits in S, per the input DemandedMask.
895/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
896/// rest bits.
897///
898/// Currently we only test condition 2).
899///
900/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
901/// not successful.
902Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
Benjamin Kramerc321e532016-06-08 19:09:22 +0000903 Instruction *Shl,
904 const APInt &DemandedMask,
905 APInt &KnownZero,
906 APInt &KnownOne) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000907
Benjamin Kramer010f1082013-08-30 14:35:35 +0000908 const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
909 const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
910 if (!ShlOp1 || !ShrOp1)
Craig Topperf40110f2014-04-25 05:29:35 +0000911 return nullptr; // Noop.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000912
913 Value *VarX = Shr->getOperand(0);
914 Type *Ty = VarX->getType();
915 unsigned BitWidth = Ty->getIntegerBitWidth();
916 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000917 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000918
919 unsigned ShlAmt = ShlOp1.getZExtValue();
920 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000921
922 KnownOne.clearAllBits();
Craig Topper3a86a042017-03-19 05:49:16 +0000923 KnownZero.setLowBits(ShlAmt - 1);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000924 KnownZero &= DemandedMask;
925
Benjamin Kramer010f1082013-08-30 14:35:35 +0000926 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
927 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000928
929 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
930 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
931 (BitMask1.ashr(ShrAmt) << ShlAmt);
932
933 if (ShrAmt <= ShlAmt) {
934 BitMask2 <<= (ShlAmt - ShrAmt);
935 } else {
936 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
937 BitMask2.ashr(ShrAmt - ShlAmt);
938 }
939
940 // Check if condition-2 (see the comment to this function) is satified.
941 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
942 if (ShrAmt == ShlAmt)
943 return VarX;
944
945 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000946 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000947
948 BinaryOperator *New;
949 if (ShrAmt < ShlAmt) {
950 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
951 New = BinaryOperator::CreateShl(VarX, Amt);
952 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
953 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
954 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
955 } else {
956 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000957 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
958 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000959 if (cast<BinaryOperator>(Shr)->isExact())
960 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000961 }
962
963 return InsertNewInstWith(New, *Shl);
964 }
965
Craig Topperf40110f2014-04-25 05:29:35 +0000966 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000967}
Chris Lattner7e044912010-01-04 07:17:19 +0000968
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +0000969/// The specified value produces a vector with any number of elements.
970/// DemandedElts contains the set of elements that are actually used by the
971/// caller. This method analyzes which elements of the operand are undef and
972/// returns that information in UndefElts.
Chris Lattner7e044912010-01-04 07:17:19 +0000973///
974/// If the information about demanded elements can be used to simplify the
975/// operation, the operation is simplified, then the resultant value is
976/// returned. This returns null if no change was made.
977Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000978 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000979 unsigned Depth) {
Sanjay Patel9190b4a2016-04-29 20:54:56 +0000980 unsigned VWidth = V->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +0000981 APInt EltMask(APInt::getAllOnesValue(VWidth));
982 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
983
984 if (isa<UndefValue>(V)) {
985 // If the entire vector is undefined, just return this info.
986 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000987 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000988 }
Craig Topper4c947752012-12-22 18:09:02 +0000989
Chris Lattnerb22423c2010-02-08 23:56:03 +0000990 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000991 UndefElts = EltMask;
992 return UndefValue::get(V->getType());
993 }
994
995 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000996
Chris Lattner67058832012-01-25 06:48:06 +0000997 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
998 if (Constant *C = dyn_cast<Constant>(V)) {
999 // Check if this is identity. If so, return 0 since we are not simplifying
1000 // anything.
1001 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +00001002 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +00001003
Chris Lattner229907c2011-07-18 04:54:35 +00001004 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +00001005 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +00001006
Chris Lattner67058832012-01-25 06:48:06 +00001007 SmallVector<Constant*, 16> Elts;
1008 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +00001009 if (!DemandedElts[i]) { // If not demanded, set to undef.
1010 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +00001011 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +00001012 continue;
1013 }
Craig Topper4c947752012-12-22 18:09:02 +00001014
Chris Lattner67058832012-01-25 06:48:06 +00001015 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +00001016 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +00001017
Chris Lattner67058832012-01-25 06:48:06 +00001018 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001019 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +00001020 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001021 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +00001022 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +00001023 }
Chris Lattner67058832012-01-25 06:48:06 +00001024 }
Craig Topper4c947752012-12-22 18:09:02 +00001025
Chris Lattner7e044912010-01-04 07:17:19 +00001026 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +00001027 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +00001028 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001029 }
Craig Topper4c947752012-12-22 18:09:02 +00001030
Chris Lattner7e044912010-01-04 07:17:19 +00001031 // Limit search depth.
1032 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +00001033 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001034
Stuart Hastings5bd18b62011-05-17 22:13:31 +00001035 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +00001036 // simplification conservatively assuming that all elements
1037 // are needed.
1038 if (!V->hasOneUse()) {
1039 // Quit if we find multiple users of a non-root value though.
1040 // They'll be handled when it's their turn to be visited by
1041 // the main instcombine process.
1042 if (Depth != 0)
1043 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +00001044 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001045
1046 // Conservatively assume that all elements are needed.
1047 DemandedElts = EltMask;
1048 }
Craig Topper4c947752012-12-22 18:09:02 +00001049
Chris Lattner7e044912010-01-04 07:17:19 +00001050 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +00001051 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +00001052
Chris Lattner7e044912010-01-04 07:17:19 +00001053 bool MadeChange = false;
1054 APInt UndefElts2(VWidth, 0);
Craig Topper23ebd952016-12-11 08:54:52 +00001055 APInt UndefElts3(VWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001056 Value *TmpV;
1057 switch (I->getOpcode()) {
1058 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001059
Chris Lattner7e044912010-01-04 07:17:19 +00001060 case Instruction::InsertElement: {
1061 // If this is a variable index, we don't know which element it overwrites.
1062 // demand exactly the same input as we produce.
1063 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +00001064 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +00001065 // Note that we can't propagate undef elt info, because we don't know
1066 // which elt is getting updated.
1067 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001068 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001069 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1070 break;
1071 }
Craig Topper4c947752012-12-22 18:09:02 +00001072
Chris Lattner7e044912010-01-04 07:17:19 +00001073 // If this is inserting an element that isn't demanded, remove this
1074 // insertelement.
1075 unsigned IdxNo = Idx->getZExtValue();
1076 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1077 Worklist.Add(I);
1078 return I->getOperand(0);
1079 }
Craig Topper4c947752012-12-22 18:09:02 +00001080
Chris Lattner7e044912010-01-04 07:17:19 +00001081 // Otherwise, the element inserted overwrites whatever was there, so the
1082 // input demanded set is simpler than the output set.
1083 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001084 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001085 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001086 UndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001087 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1088
1089 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001090 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001091 break;
1092 }
1093 case Instruction::ShuffleVector: {
1094 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Craig Topper2e18bcf2016-12-29 04:24:32 +00001095 unsigned LHSVWidth =
1096 Shuffle->getOperand(0)->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +00001097 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1098 for (unsigned i = 0; i < VWidth; i++) {
1099 if (DemandedElts[i]) {
1100 unsigned MaskVal = Shuffle->getMaskValue(i);
1101 if (MaskVal != -1u) {
1102 assert(MaskVal < LHSVWidth * 2 &&
1103 "shufflevector mask index out of range!");
1104 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001105 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001106 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001107 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001108 }
1109 }
1110 }
1111
Alexey Bataevfee90782016-09-23 09:14:08 +00001112 APInt LHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001113 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001114 LHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001115 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1116
Alexey Bataevfee90782016-09-23 09:14:08 +00001117 APInt RHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001118 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001119 RHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001120 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1121
1122 bool NewUndefElts = false;
Alexey Bataev793c9462016-09-26 13:18:59 +00001123 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1124 unsigned RHSIdx = -1u, RHSValIdx = -1u;
Alexey Bataevfee90782016-09-23 09:14:08 +00001125 bool LHSUniform = true;
1126 bool RHSUniform = true;
Chris Lattner7e044912010-01-04 07:17:19 +00001127 for (unsigned i = 0; i < VWidth; i++) {
1128 unsigned MaskVal = Shuffle->getMaskValue(i);
1129 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001130 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001131 } else if (!DemandedElts[i]) {
1132 NewUndefElts = true;
1133 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001134 } else if (MaskVal < LHSVWidth) {
Alexey Bataevfee90782016-09-23 09:14:08 +00001135 if (LHSUndefElts[MaskVal]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001136 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001137 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001138 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001139 LHSIdx = LHSIdx == -1u ? i : LHSVWidth;
1140 LHSValIdx = LHSValIdx == -1u ? MaskVal : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001141 LHSUniform = LHSUniform && (MaskVal == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001142 }
1143 } else {
Alexey Bataevfee90782016-09-23 09:14:08 +00001144 if (RHSUndefElts[MaskVal - LHSVWidth]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001145 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001146 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001147 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001148 RHSIdx = RHSIdx == -1u ? i : LHSVWidth;
1149 RHSValIdx = RHSValIdx == -1u ? MaskVal - LHSVWidth : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001150 RHSUniform = RHSUniform && (MaskVal - LHSVWidth == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001151 }
1152 }
1153 }
1154
Alexey Bataevfee90782016-09-23 09:14:08 +00001155 // Try to transform shuffle with constant vector and single element from
1156 // this constant vector to single insertelement instruction.
1157 // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
1158 // insertelement V, C[ci], ci-n
1159 if (LHSVWidth == Shuffle->getType()->getNumElements()) {
1160 Value *Op = nullptr;
1161 Constant *Value = nullptr;
1162 unsigned Idx = -1u;
1163
Craig Topper62f06e22016-12-29 05:38:31 +00001164 // Find constant vector with the single element in shuffle (LHS or RHS).
Alexey Bataevfee90782016-09-23 09:14:08 +00001165 if (LHSIdx < LHSVWidth && RHSUniform) {
1166 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1167 Op = Shuffle->getOperand(1);
Alexey Bataev793c9462016-09-26 13:18:59 +00001168 Value = CV->getOperand(LHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001169 Idx = LHSIdx;
1170 }
1171 }
1172 if (RHSIdx < LHSVWidth && LHSUniform) {
1173 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1174 Op = Shuffle->getOperand(0);
Alexey Bataev793c9462016-09-26 13:18:59 +00001175 Value = CV->getOperand(RHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001176 Idx = RHSIdx;
1177 }
1178 }
1179 // Found constant vector with single element - convert to insertelement.
1180 if (Op && Value) {
1181 Instruction *New = InsertElementInst::Create(
1182 Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx),
1183 Shuffle->getName());
1184 InsertNewInstWith(New, *Shuffle);
1185 return New;
1186 }
1187 }
Chris Lattner7e044912010-01-04 07:17:19 +00001188 if (NewUndefElts) {
1189 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001190 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001191 for (unsigned i = 0; i < VWidth; ++i) {
1192 if (UndefElts[i])
1193 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1194 else
1195 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1196 Shuffle->getMaskValue(i)));
1197 }
1198 I->setOperand(2, ConstantVector::get(Elts));
1199 MadeChange = true;
1200 }
1201 break;
1202 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001203 case Instruction::Select: {
1204 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1205 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1206 for (unsigned i = 0; i < VWidth; i++) {
Andrea Di Biagio40f59e42015-10-06 10:34:53 +00001207 Constant *CElt = CV->getAggregateElement(i);
1208 // Method isNullValue always returns false when called on a
1209 // ConstantExpr. If CElt is a ConstantExpr then skip it in order to
1210 // to avoid propagating incorrect information.
1211 if (isa<ConstantExpr>(CElt))
1212 continue;
1213 if (CElt->isNullValue())
Pete Cooperabc13af2012-07-26 23:10:24 +00001214 LeftDemanded.clearBit(i);
1215 else
1216 RightDemanded.clearBit(i);
1217 }
1218 }
1219
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001220 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded, UndefElts,
1221 Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001222 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1223
1224 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001225 UndefElts2, Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001226 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001227
Pete Cooperabc13af2012-07-26 23:10:24 +00001228 // Output elements are undefined if both are undefined.
1229 UndefElts &= UndefElts2;
1230 break;
1231 }
Chris Lattner7e044912010-01-04 07:17:19 +00001232 case Instruction::BitCast: {
1233 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001234 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001235 if (!VTy) break;
1236 unsigned InVWidth = VTy->getNumElements();
1237 APInt InputDemandedElts(InVWidth, 0);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001238 UndefElts2 = APInt(InVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001239 unsigned Ratio;
1240
1241 if (VWidth == InVWidth) {
1242 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1243 // elements as are demanded of us.
1244 Ratio = 1;
1245 InputDemandedElts = DemandedElts;
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001246 } else if ((VWidth % InVWidth) == 0) {
1247 // If the number of elements in the output is a multiple of the number of
1248 // elements in the input then an input element is live if any of the
1249 // corresponding output elements are live.
1250 Ratio = VWidth / InVWidth;
1251 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Chris Lattner7e044912010-01-04 07:17:19 +00001252 if (DemandedElts[OutIdx])
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001253 InputDemandedElts.setBit(OutIdx / Ratio);
1254 } else if ((InVWidth % VWidth) == 0) {
1255 // If the number of elements in the input is a multiple of the number of
1256 // elements in the output then an input element is live if the
1257 // corresponding output element is live.
1258 Ratio = InVWidth / VWidth;
Chris Lattner7e044912010-01-04 07:17:19 +00001259 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001260 if (DemandedElts[InIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001261 InputDemandedElts.setBit(InIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001262 } else {
1263 // Unsupported so far.
1264 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001265 }
Craig Topper4c947752012-12-22 18:09:02 +00001266
Chris Lattner7e044912010-01-04 07:17:19 +00001267 // div/rem demand all inputs, because they don't want divide by zero.
1268 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001269 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001270 if (TmpV) {
1271 I->setOperand(0, TmpV);
1272 MadeChange = true;
1273 }
Craig Topper4c947752012-12-22 18:09:02 +00001274
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001275 if (VWidth == InVWidth) {
1276 UndefElts = UndefElts2;
1277 } else if ((VWidth % InVWidth) == 0) {
1278 // If the number of elements in the output is a multiple of the number of
1279 // elements in the input then an output element is undef if the
1280 // corresponding input element is undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001281 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001282 if (UndefElts2[OutIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001283 UndefElts.setBit(OutIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001284 } else if ((InVWidth % VWidth) == 0) {
1285 // If the number of elements in the input is a multiple of the number of
1286 // elements in the output then an output element is undef if all of the
1287 // corresponding input elements are undef.
1288 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1289 APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
1290 if (SubUndef.countPopulation() == Ratio)
1291 UndefElts.setBit(OutIdx);
1292 }
1293 } else {
Chris Lattner7e044912010-01-04 07:17:19 +00001294 llvm_unreachable("Unimp");
Chris Lattner7e044912010-01-04 07:17:19 +00001295 }
1296 break;
1297 }
1298 case Instruction::And:
1299 case Instruction::Or:
1300 case Instruction::Xor:
1301 case Instruction::Add:
1302 case Instruction::Sub:
1303 case Instruction::Mul:
1304 // div/rem demand all inputs, because they don't want divide by zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001305 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1306 Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001307 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1308 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001309 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001310 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001311
Chris Lattner7e044912010-01-04 07:17:19 +00001312 // Output elements are undefined if both are undefined. Consider things
1313 // like undef&0. The result is known zero, not undef.
1314 UndefElts &= UndefElts2;
1315 break;
Pete Coopere807e452012-07-26 22:37:04 +00001316 case Instruction::FPTrunc:
1317 case Instruction::FPExt:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001318 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1319 Depth + 1);
Pete Coopere807e452012-07-26 22:37:04 +00001320 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1321 break;
Craig Topper4c947752012-12-22 18:09:02 +00001322
Chris Lattner7e044912010-01-04 07:17:19 +00001323 case Instruction::Call: {
1324 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1325 if (!II) break;
1326 switch (II->getIntrinsicID()) {
1327 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001328
Craig Topper7fc6d342016-12-11 22:32:38 +00001329 case Intrinsic::x86_xop_vfrcz_ss:
1330 case Intrinsic::x86_xop_vfrcz_sd:
1331 // The instructions for these intrinsics are speced to zero upper bits not
1332 // pass them through like other scalar intrinsics. So we shouldn't just
1333 // use Arg0 if DemandedElts[0] is clear like we do for other intrinsics.
1334 // Instead we should return a zero vector.
Craig Topper1a8a3372016-12-29 03:30:17 +00001335 if (!DemandedElts[0]) {
1336 Worklist.Add(II);
Craig Topper7fc6d342016-12-11 22:32:38 +00001337 return ConstantAggregateZero::get(II->getType());
Craig Topper1a8a3372016-12-29 03:30:17 +00001338 }
Craig Topper7fc6d342016-12-11 22:32:38 +00001339
Craig Topperac75bca2016-12-13 07:45:45 +00001340 // Only the lower element is used.
1341 DemandedElts = 1;
Craig Topper7fc6d342016-12-11 22:32:38 +00001342 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1343 UndefElts, Depth + 1);
1344 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperac75bca2016-12-13 07:45:45 +00001345
1346 // Only the lower element is undefined. The high elements are zero.
1347 UndefElts = UndefElts[0];
Craig Topper7fc6d342016-12-11 22:32:38 +00001348 break;
1349
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001350 // Unary scalar-as-vector operations that work column-wise.
Simon Pilgrim83020942016-04-24 18:23:14 +00001351 case Intrinsic::x86_sse_rcp_ss:
1352 case Intrinsic::x86_sse_rsqrt_ss:
1353 case Intrinsic::x86_sse_sqrt_ss:
1354 case Intrinsic::x86_sse2_sqrt_sd:
Simon Pilgrim83020942016-04-24 18:23:14 +00001355 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1356 UndefElts, Depth + 1);
1357 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1358
1359 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001360 if (!DemandedElts[0]) {
1361 Worklist.Add(II);
Simon Pilgrim83020942016-04-24 18:23:14 +00001362 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001363 }
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001364 // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions
1365 // checks).
Simon Pilgrim83020942016-04-24 18:23:14 +00001366 break;
1367
Craig Toppera0372de2016-12-14 03:17:27 +00001368 // Binary scalar-as-vector operations that work column-wise. The high
1369 // elements come from operand 0. The low element is a function of both
1370 // operands.
Chris Lattner7e044912010-01-04 07:17:19 +00001371 case Intrinsic::x86_sse_min_ss:
1372 case Intrinsic::x86_sse_max_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001373 case Intrinsic::x86_sse_cmp_ss:
Chris Lattner7e044912010-01-04 07:17:19 +00001374 case Intrinsic::x86_sse2_min_sd:
1375 case Intrinsic::x86_sse2_max_sd:
Craig Toppera0372de2016-12-14 03:17:27 +00001376 case Intrinsic::x86_sse2_cmp_sd: {
1377 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1378 UndefElts, Depth + 1);
1379 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1380
1381 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001382 if (!DemandedElts[0]) {
1383 Worklist.Add(II);
Craig Toppera0372de2016-12-14 03:17:27 +00001384 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001385 }
Craig Toppera0372de2016-12-14 03:17:27 +00001386
1387 // Only lower element is used for operand 1.
1388 DemandedElts = 1;
1389 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1390 UndefElts2, Depth + 1);
1391 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1392
1393 // Lower element is undefined if both lower elements are undefined.
1394 // Consider things like undef&0. The result is known zero, not undef.
1395 if (!UndefElts2[0])
1396 UndefElts.clearBit(0);
1397
1398 break;
1399 }
1400
Craig Toppereb6a20e2016-12-14 03:17:30 +00001401 // Binary scalar-as-vector operations that work column-wise. The high
1402 // elements come from operand 0 and the low element comes from operand 1.
Simon Pilgrim83020942016-04-24 18:23:14 +00001403 case Intrinsic::x86_sse41_round_ss:
Craig Toppereb6a20e2016-12-14 03:17:30 +00001404 case Intrinsic::x86_sse41_round_sd: {
1405 // Don't use the low element of operand 0.
1406 APInt DemandedElts2 = DemandedElts;
1407 DemandedElts2.clearBit(0);
1408 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001409 UndefElts, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001410 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001411
1412 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001413 if (!DemandedElts[0]) {
1414 Worklist.Add(II);
Craig Toppereb6a20e2016-12-14 03:17:30 +00001415 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001416 }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001417
1418 // Only lower element is used for operand 1.
1419 DemandedElts = 1;
Gabor Greife23efee2010-06-28 16:45:00 +00001420 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001421 UndefElts2, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001422 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001423
Craig Toppereb6a20e2016-12-14 03:17:30 +00001424 // Take the high undef elements from operand 0 and take the lower element
1425 // from operand 1.
1426 UndefElts.clearBit(0);
1427 UndefElts |= UndefElts2[0];
Chris Lattner7e044912010-01-04 07:17:19 +00001428 break;
Craig Toppereb6a20e2016-12-14 03:17:30 +00001429 }
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001430
Craig Topperdfd268d2016-12-14 05:43:05 +00001431 // Three input scalar-as-vector operations that work column-wise. The high
1432 // elements come from operand 0 and the low element is a function of all
1433 // three inputs.
Craig Topper268b3ab2016-12-14 06:06:58 +00001434 case Intrinsic::x86_avx512_mask_add_ss_round:
1435 case Intrinsic::x86_avx512_mask_div_ss_round:
1436 case Intrinsic::x86_avx512_mask_mul_ss_round:
1437 case Intrinsic::x86_avx512_mask_sub_ss_round:
1438 case Intrinsic::x86_avx512_mask_max_ss_round:
1439 case Intrinsic::x86_avx512_mask_min_ss_round:
1440 case Intrinsic::x86_avx512_mask_add_sd_round:
1441 case Intrinsic::x86_avx512_mask_div_sd_round:
1442 case Intrinsic::x86_avx512_mask_mul_sd_round:
1443 case Intrinsic::x86_avx512_mask_sub_sd_round:
1444 case Intrinsic::x86_avx512_mask_max_sd_round:
1445 case Intrinsic::x86_avx512_mask_min_sd_round:
Craig Topper23ebd952016-12-11 08:54:52 +00001446 case Intrinsic::x86_fma_vfmadd_ss:
1447 case Intrinsic::x86_fma_vfmsub_ss:
1448 case Intrinsic::x86_fma_vfnmadd_ss:
1449 case Intrinsic::x86_fma_vfnmsub_ss:
1450 case Intrinsic::x86_fma_vfmadd_sd:
1451 case Intrinsic::x86_fma_vfmsub_sd:
1452 case Intrinsic::x86_fma_vfnmadd_sd:
1453 case Intrinsic::x86_fma_vfnmsub_sd:
Craig Topperab5f3552016-12-15 03:49:45 +00001454 case Intrinsic::x86_avx512_mask_vfmadd_ss:
1455 case Intrinsic::x86_avx512_mask_vfmadd_sd:
1456 case Intrinsic::x86_avx512_maskz_vfmadd_ss:
1457 case Intrinsic::x86_avx512_maskz_vfmadd_sd:
Craig Topper23ebd952016-12-11 08:54:52 +00001458 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1459 UndefElts, Depth + 1);
1460 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperdfd268d2016-12-14 05:43:05 +00001461
1462 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001463 if (!DemandedElts[0]) {
1464 Worklist.Add(II);
Craig Topperdfd268d2016-12-14 05:43:05 +00001465 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001466 }
Craig Topperdfd268d2016-12-14 05:43:05 +00001467
1468 // Only lower element is used for operand 1 and 2.
1469 DemandedElts = 1;
Craig Topper23ebd952016-12-11 08:54:52 +00001470 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1471 UndefElts2, Depth + 1);
1472 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1473 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1474 UndefElts3, Depth + 1);
1475 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1476
Craig Topperdfd268d2016-12-14 05:43:05 +00001477 // Lower element is undefined if all three lower elements are undefined.
1478 // Consider things like undef&0. The result is known zero, not undef.
1479 if (!UndefElts2[0] || !UndefElts3[0])
1480 UndefElts.clearBit(0);
Craig Topper23ebd952016-12-11 08:54:52 +00001481
Craig Topper23ebd952016-12-11 08:54:52 +00001482 break;
1483
Craig Topperab5f3552016-12-15 03:49:45 +00001484 case Intrinsic::x86_avx512_mask3_vfmadd_ss:
1485 case Intrinsic::x86_avx512_mask3_vfmadd_sd:
1486 case Intrinsic::x86_avx512_mask3_vfmsub_ss:
1487 case Intrinsic::x86_avx512_mask3_vfmsub_sd:
1488 case Intrinsic::x86_avx512_mask3_vfnmsub_ss:
1489 case Intrinsic::x86_avx512_mask3_vfnmsub_sd:
1490 // These intrinsics get the passthru bits from operand 2.
1491 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1492 UndefElts, Depth + 1);
1493 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1494
1495 // If lowest element of a scalar op isn't used then use Arg2.
Craig Topper1a8a3372016-12-29 03:30:17 +00001496 if (!DemandedElts[0]) {
1497 Worklist.Add(II);
Craig Topperab5f3552016-12-15 03:49:45 +00001498 return II->getArgOperand(2);
Craig Topper1a8a3372016-12-29 03:30:17 +00001499 }
Craig Topperab5f3552016-12-15 03:49:45 +00001500
1501 // Only lower element is used for operand 0 and 1.
1502 DemandedElts = 1;
1503 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1504 UndefElts2, Depth + 1);
1505 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1506 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1507 UndefElts3, Depth + 1);
1508 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1509
1510 // Lower element is undefined if all three lower elements are undefined.
1511 // Consider things like undef&0. The result is known zero, not undef.
1512 if (!UndefElts2[0] || !UndefElts3[0])
1513 UndefElts.clearBit(0);
1514
1515 break;
1516
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001517 case Intrinsic::x86_sse2_pmulu_dq:
1518 case Intrinsic::x86_sse41_pmuldq:
1519 case Intrinsic::x86_avx2_pmul_dq:
Craig Topper72f2d4e2016-12-27 05:30:09 +00001520 case Intrinsic::x86_avx2_pmulu_dq:
1521 case Intrinsic::x86_avx512_pmul_dq_512:
1522 case Intrinsic::x86_avx512_pmulu_dq_512: {
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001523 Value *Op0 = II->getArgOperand(0);
1524 Value *Op1 = II->getArgOperand(1);
1525 unsigned InnerVWidth = Op0->getType()->getVectorNumElements();
1526 assert((VWidth * 2) == InnerVWidth && "Unexpected input size");
1527
1528 APInt InnerDemandedElts(InnerVWidth, 0);
1529 for (unsigned i = 0; i != VWidth; ++i)
1530 if (DemandedElts[i])
1531 InnerDemandedElts.setBit(i * 2);
1532
1533 UndefElts2 = APInt(InnerVWidth, 0);
1534 TmpV = SimplifyDemandedVectorElts(Op0, InnerDemandedElts, UndefElts2,
1535 Depth + 1);
1536 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1537
1538 UndefElts3 = APInt(InnerVWidth, 0);
1539 TmpV = SimplifyDemandedVectorElts(Op1, InnerDemandedElts, UndefElts3,
1540 Depth + 1);
1541 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1542
1543 break;
1544 }
1545
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001546 case Intrinsic::x86_sse2_packssdw_128:
1547 case Intrinsic::x86_sse2_packsswb_128:
1548 case Intrinsic::x86_sse2_packuswb_128:
1549 case Intrinsic::x86_sse41_packusdw:
1550 case Intrinsic::x86_avx2_packssdw:
1551 case Intrinsic::x86_avx2_packsswb:
1552 case Intrinsic::x86_avx2_packusdw:
Craig Topper3731f4d2017-02-16 07:35:23 +00001553 case Intrinsic::x86_avx2_packuswb:
1554 case Intrinsic::x86_avx512_packssdw_512:
1555 case Intrinsic::x86_avx512_packsswb_512:
1556 case Intrinsic::x86_avx512_packusdw_512:
1557 case Intrinsic::x86_avx512_packuswb_512: {
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001558 auto *Ty0 = II->getArgOperand(0)->getType();
1559 unsigned InnerVWidth = Ty0->getVectorNumElements();
1560 assert(VWidth == (InnerVWidth * 2) && "Unexpected input size");
1561
1562 unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128;
1563 unsigned VWidthPerLane = VWidth / NumLanes;
1564 unsigned InnerVWidthPerLane = InnerVWidth / NumLanes;
1565
1566 // Per lane, pack the elements of the first input and then the second.
1567 // e.g.
1568 // v8i16 PACK(v4i32 X, v4i32 Y) - (X[0..3],Y[0..3])
1569 // v32i8 PACK(v16i16 X, v16i16 Y) - (X[0..7],Y[0..7]),(X[8..15],Y[8..15])
1570 for (int OpNum = 0; OpNum != 2; ++OpNum) {
1571 APInt OpDemandedElts(InnerVWidth, 0);
1572 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1573 unsigned LaneIdx = Lane * VWidthPerLane;
1574 for (unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) {
1575 unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum;
1576 if (DemandedElts[Idx])
1577 OpDemandedElts.setBit((Lane * InnerVWidthPerLane) + Elt);
1578 }
1579 }
1580
1581 // Demand elements from the operand.
1582 auto *Op = II->getArgOperand(OpNum);
1583 APInt OpUndefElts(InnerVWidth, 0);
1584 TmpV = SimplifyDemandedVectorElts(Op, OpDemandedElts, OpUndefElts,
1585 Depth + 1);
1586 if (TmpV) {
1587 II->setArgOperand(OpNum, TmpV);
1588 MadeChange = true;
1589 }
1590
1591 // Pack the operand's UNDEF elements, one lane at a time.
1592 OpUndefElts = OpUndefElts.zext(VWidth);
1593 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1594 APInt LaneElts = OpUndefElts.lshr(InnerVWidthPerLane * Lane);
1595 LaneElts = LaneElts.getLoBits(InnerVWidthPerLane);
1596 LaneElts = LaneElts.shl(InnerVWidthPerLane * (2 * Lane + OpNum));
1597 UndefElts |= LaneElts;
1598 }
1599 }
1600 break;
1601 }
1602
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001603 // PSHUFB
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001604 case Intrinsic::x86_ssse3_pshuf_b_128:
1605 case Intrinsic::x86_avx2_pshuf_b:
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001606 case Intrinsic::x86_avx512_pshuf_b_512:
1607 // PERMILVAR
1608 case Intrinsic::x86_avx_vpermilvar_ps:
1609 case Intrinsic::x86_avx_vpermilvar_ps_256:
1610 case Intrinsic::x86_avx512_vpermilvar_ps_512:
1611 case Intrinsic::x86_avx_vpermilvar_pd:
1612 case Intrinsic::x86_avx_vpermilvar_pd_256:
Simon Pilgrimfe2c0ed2017-01-18 14:47:49 +00001613 case Intrinsic::x86_avx512_vpermilvar_pd_512:
1614 // PERMV
1615 case Intrinsic::x86_avx2_permd:
1616 case Intrinsic::x86_avx2_permps: {
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001617 Value *Op1 = II->getArgOperand(1);
1618 TmpV = SimplifyDemandedVectorElts(Op1, DemandedElts, UndefElts,
1619 Depth + 1);
1620 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1621 break;
1622 }
1623
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001624 // SSE4A instructions leave the upper 64-bits of the 128-bit result
1625 // in an undefined state.
1626 case Intrinsic::x86_sse4a_extrq:
1627 case Intrinsic::x86_sse4a_extrqi:
1628 case Intrinsic::x86_sse4a_insertq:
1629 case Intrinsic::x86_sse4a_insertqi:
Craig Topper3a86a042017-03-19 05:49:16 +00001630 UndefElts.setHighBits(VWidth / 2);
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001631 break;
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001632 case Intrinsic::amdgcn_buffer_load:
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001633 case Intrinsic::amdgcn_buffer_load_format:
1634 case Intrinsic::amdgcn_image_sample:
1635 case Intrinsic::amdgcn_image_sample_cl:
1636 case Intrinsic::amdgcn_image_sample_d:
1637 case Intrinsic::amdgcn_image_sample_d_cl:
1638 case Intrinsic::amdgcn_image_sample_l:
1639 case Intrinsic::amdgcn_image_sample_b:
1640 case Intrinsic::amdgcn_image_sample_b_cl:
1641 case Intrinsic::amdgcn_image_sample_lz:
1642 case Intrinsic::amdgcn_image_sample_cd:
1643 case Intrinsic::amdgcn_image_sample_cd_cl:
1644
1645 case Intrinsic::amdgcn_image_sample_c:
1646 case Intrinsic::amdgcn_image_sample_c_cl:
1647 case Intrinsic::amdgcn_image_sample_c_d:
1648 case Intrinsic::amdgcn_image_sample_c_d_cl:
1649 case Intrinsic::amdgcn_image_sample_c_l:
1650 case Intrinsic::amdgcn_image_sample_c_b:
1651 case Intrinsic::amdgcn_image_sample_c_b_cl:
1652 case Intrinsic::amdgcn_image_sample_c_lz:
1653 case Intrinsic::amdgcn_image_sample_c_cd:
1654 case Intrinsic::amdgcn_image_sample_c_cd_cl:
1655
1656 case Intrinsic::amdgcn_image_sample_o:
1657 case Intrinsic::amdgcn_image_sample_cl_o:
1658 case Intrinsic::amdgcn_image_sample_d_o:
1659 case Intrinsic::amdgcn_image_sample_d_cl_o:
1660 case Intrinsic::amdgcn_image_sample_l_o:
1661 case Intrinsic::amdgcn_image_sample_b_o:
1662 case Intrinsic::amdgcn_image_sample_b_cl_o:
1663 case Intrinsic::amdgcn_image_sample_lz_o:
1664 case Intrinsic::amdgcn_image_sample_cd_o:
1665 case Intrinsic::amdgcn_image_sample_cd_cl_o:
1666
1667 case Intrinsic::amdgcn_image_sample_c_o:
1668 case Intrinsic::amdgcn_image_sample_c_cl_o:
1669 case Intrinsic::amdgcn_image_sample_c_d_o:
1670 case Intrinsic::amdgcn_image_sample_c_d_cl_o:
1671 case Intrinsic::amdgcn_image_sample_c_l_o:
1672 case Intrinsic::amdgcn_image_sample_c_b_o:
1673 case Intrinsic::amdgcn_image_sample_c_b_cl_o:
1674 case Intrinsic::amdgcn_image_sample_c_lz_o:
1675 case Intrinsic::amdgcn_image_sample_c_cd_o:
1676 case Intrinsic::amdgcn_image_sample_c_cd_cl_o:
1677
1678 case Intrinsic::amdgcn_image_getlod: {
Craig Topperd33ee1b2017-04-03 16:34:59 +00001679 if (VWidth == 1 || !DemandedElts.isMask())
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001680 return nullptr;
1681
1682 // TODO: Handle 3 vectors when supported in code gen.
1683 unsigned NewNumElts = PowerOf2Ceil(DemandedElts.countTrailingOnes());
1684 if (NewNumElts == VWidth)
1685 return nullptr;
1686
1687 Module *M = II->getParent()->getParent()->getParent();
1688 Type *EltTy = V->getType()->getVectorElementType();
1689
1690 Type *NewTy = (NewNumElts == 1) ? EltTy :
1691 VectorType::get(EltTy, NewNumElts);
1692
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001693 auto IID = II->getIntrinsicID();
1694
1695 bool IsBuffer = IID == Intrinsic::amdgcn_buffer_load ||
1696 IID == Intrinsic::amdgcn_buffer_load_format;
1697
1698 Function *NewIntrin = IsBuffer ?
1699 Intrinsic::getDeclaration(M, IID, NewTy) :
1700 // Samplers have 3 mangled types.
1701 Intrinsic::getDeclaration(M, IID,
1702 { NewTy, II->getArgOperand(0)->getType(),
1703 II->getArgOperand(1)->getType()});
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001704
1705 SmallVector<Value *, 5> Args;
1706 for (unsigned I = 0, E = II->getNumArgOperands(); I != E; ++I)
1707 Args.push_back(II->getArgOperand(I));
1708
Matt Arsenaulta3bdd8f2017-03-10 05:25:49 +00001709 IRBuilderBase::InsertPointGuard Guard(*Builder);
1710 Builder->SetInsertPoint(II);
1711
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001712 CallInst *NewCall = Builder->CreateCall(NewIntrin, Args);
1713 NewCall->takeName(II);
1714 NewCall->copyMetadata(*II);
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001715
1716 if (!IsBuffer) {
1717 ConstantInt *DMask = dyn_cast<ConstantInt>(NewCall->getArgOperand(3));
1718 if (DMask) {
1719 unsigned DMaskVal = DMask->getZExtValue() & 0xf;
1720
1721 unsigned PopCnt = 0;
1722 unsigned NewDMask = 0;
1723 for (unsigned I = 0; I < 4; ++I) {
1724 const unsigned Bit = 1 << I;
1725 if (!!(DMaskVal & Bit)) {
1726 if (++PopCnt > NewNumElts)
1727 break;
1728
1729 NewDMask |= Bit;
1730 }
1731 }
1732
1733 NewCall->setArgOperand(3, ConstantInt::get(DMask->getType(), NewDMask));
1734 }
1735 }
1736
1737
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001738 if (NewNumElts == 1) {
1739 return Builder->CreateInsertElement(UndefValue::get(V->getType()),
1740 NewCall, static_cast<uint64_t>(0));
1741 }
1742
1743 SmallVector<uint32_t, 8> EltMask;
1744 for (unsigned I = 0; I < VWidth; ++I)
1745 EltMask.push_back(I);
1746
1747 Value *Shuffle = Builder->CreateShuffleVector(
1748 NewCall, UndefValue::get(NewTy), EltMask);
1749
1750 MadeChange = true;
1751 return Shuffle;
1752 }
Chris Lattner7e044912010-01-04 07:17:19 +00001753 }
1754 break;
1755 }
1756 }
Craig Topperf40110f2014-04-25 05:29:35 +00001757 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001758}