blob: 3d14e59ea0d0b24685a6e1f7c9975781baaa2a90 [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());
Craig Toppera8129a12017-04-20 16:17:13 +000041 if (C->isSubsetOf(Demanded))
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 Topper17f37ba2017-04-20 20:47:35 +0000179 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000180 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 Topper17f37ba2017-04-20 20:47:35 +0000184 if (DemandedMask.isSubsetOf(LHSKnownZero | RHSKnownOne))
Chris Lattner7e044912010-01-04 07:17:19 +0000185 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000186 if (DemandedMask.isSubsetOf(RHSKnownZero | LHSKnownOne))
Chris Lattner7e044912010-01-04 07:17:19 +0000187 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000188
Chris Lattner7e044912010-01-04 07:17:19 +0000189 // If the RHS is a constant, see if we can simplify it.
190 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
191 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000192
Craig Topper9a458cd2017-04-14 22:34:14 +0000193 KnownZero = std::move(IKnownZero);
194 KnownOne = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000195 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000196 }
197 case Instruction::Or: {
Chris Lattner7e044912010-01-04 07:17:19 +0000198 // If either the LHS or the RHS are One, the result is One.
Craig Topper47596dd2017-03-25 06:52:52 +0000199 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
200 Depth + 1) ||
201 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnownOne, LHSKnownZero,
202 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000203 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000204 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
205 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
206
Craig Topper9a458cd2017-04-14 22:34:14 +0000207 // Output known-0 bits are only known if clear in both the LHS & RHS.
208 APInt IKnownZero = RHSKnownZero & LHSKnownZero;
209 // Output known-1 are known to be set if set in either the LHS | RHS.
210 APInt IKnownOne = RHSKnownOne | LHSKnownOne;
211
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000212 // If the client is only demanding bits that we know, return the known
213 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000214 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000215 return Constant::getIntegerValue(VTy, IKnownOne);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000216
Chris Lattner7e044912010-01-04 07:17:19 +0000217 // If all of the demanded bits are known zero on one side, return the other.
218 // These bits cannot contribute to the result of the 'or'.
Craig Topper17f37ba2017-04-20 20:47:35 +0000219 if (DemandedMask.isSubsetOf(LHSKnownOne | RHSKnownZero))
Chris Lattner7e044912010-01-04 07:17:19 +0000220 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000221 if (DemandedMask.isSubsetOf(RHSKnownOne | LHSKnownZero))
Chris Lattner7e044912010-01-04 07:17:19 +0000222 return I->getOperand(1);
223
Chris Lattner7e044912010-01-04 07:17:19 +0000224 // If the RHS is a constant, see if we can simplify it.
225 if (ShrinkDemandedConstant(I, 1, DemandedMask))
226 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000227
Craig Topper9a458cd2017-04-14 22:34:14 +0000228 KnownZero = std::move(IKnownZero);
229 KnownOne = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000230 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000231 }
Chris Lattner7e044912010-01-04 07:17:19 +0000232 case Instruction::Xor: {
Craig Topper47596dd2017-03-25 06:52:52 +0000233 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
234 Depth + 1) ||
235 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnownZero, LHSKnownOne,
236 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000237 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000238 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
239 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
240
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000241 // Output known-0 bits are known if clear or set in both the LHS & RHS.
242 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
243 (RHSKnownOne & LHSKnownOne);
244 // Output known-1 are known to be set if set in only one of the LHS, RHS.
245 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
246 (RHSKnownOne & LHSKnownZero);
247
248 // If the client is only demanding bits that we know, return the known
249 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000250 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000251 return Constant::getIntegerValue(VTy, IKnownOne);
252
Chris Lattner7e044912010-01-04 07:17:19 +0000253 // If all of the demanded bits are known zero on one side, return the other.
254 // These bits cannot contribute to the result of the 'xor'.
Craig Topper17f37ba2017-04-20 20:47:35 +0000255 if (DemandedMask.isSubsetOf(RHSKnownZero))
Chris Lattner7e044912010-01-04 07:17:19 +0000256 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000257 if (DemandedMask.isSubsetOf(LHSKnownZero))
Chris Lattner7e044912010-01-04 07:17:19 +0000258 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000259
Chris Lattner7e044912010-01-04 07:17:19 +0000260 // If all of the demanded bits are known to be zero on one side or the
261 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000262 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Craig Topper17f37ba2017-04-20 20:47:35 +0000263 if (DemandedMask.isSubsetOf(RHSKnownZero | LHSKnownZero)) {
Craig Topper4c947752012-12-22 18:09:02 +0000264 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000265 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
266 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000267 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000268 }
Craig Topper4c947752012-12-22 18:09:02 +0000269
Chris Lattner7e044912010-01-04 07:17:19 +0000270 // If all of the demanded bits on one side are known, and all of the set
271 // bits on that side are also known to be set on the other side, turn this
272 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000273 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper17f37ba2017-04-20 20:47:35 +0000274 if (DemandedMask.isSubsetOf(RHSKnownZero|RHSKnownOne) &&
275 RHSKnownOne.isSubsetOf(LHSKnownOne)) {
276 Constant *AndC = Constant::getIntegerValue(VTy,
277 ~RHSKnownOne & DemandedMask);
278 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
279 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000280 }
Craig Topper4c947752012-12-22 18:09:02 +0000281
Chris Lattner7e044912010-01-04 07:17:19 +0000282 // If the RHS is a constant, see if we can simplify it.
283 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
284 if (ShrinkDemandedConstant(I, 1, DemandedMask))
285 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000286
Chris Lattner7e044912010-01-04 07:17:19 +0000287 // If our LHS is an 'and' and if it has one use, and if any of the bits we
288 // are flipping are known to be set, then the xor is just resetting those
289 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
290 // simplifying both of them.
291 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
292 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
293 isa<ConstantInt>(I->getOperand(1)) &&
294 isa<ConstantInt>(LHSInst->getOperand(1)) &&
295 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
296 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
297 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
298 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000299
Chris Lattner7e044912010-01-04 07:17:19 +0000300 Constant *AndC =
301 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000302 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000303 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000304
Chris Lattner7e044912010-01-04 07:17:19 +0000305 Constant *XorC =
306 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000307 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000308 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000309 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000310
311 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topper9a458cd2017-04-14 22:34:14 +0000312 KnownZero = std::move(IKnownZero);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000313 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topper9a458cd2017-04-14 22:34:14 +0000314 KnownOne = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000315 break;
316 }
317 case Instruction::Select:
James Molloy2b21a7c2015-05-20 18:41:25 +0000318 // If this is a select as part of a min/max pattern, don't simplify any
319 // further in case we break the structure.
320 Value *LHS, *RHS;
James Molloy134bec22015-08-11 09:12:57 +0000321 if (matchSelectPattern(I, LHS, RHS).Flavor != SPF_UNKNOWN)
James Molloy2b21a7c2015-05-20 18:41:25 +0000322 return nullptr;
Simon Pilgrim61116dd2015-09-17 20:32:45 +0000323
Craig Topper47596dd2017-03-25 06:52:52 +0000324 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnownZero, RHSKnownOne,
325 Depth + 1) ||
326 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnownZero, LHSKnownOne,
327 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000328 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000329 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
330 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
331
Chris Lattner7e044912010-01-04 07:17:19 +0000332 // If the operands are constants, see if we can simplify them.
333 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
334 ShrinkDemandedConstant(I, 2, DemandedMask))
335 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000336
Chris Lattner7e044912010-01-04 07:17:19 +0000337 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000338 KnownOne = RHSKnownOne & LHSKnownOne;
339 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000340 break;
341 case Instruction::Trunc: {
342 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000343 DemandedMask = DemandedMask.zext(truncBf);
344 KnownZero = KnownZero.zext(truncBf);
345 KnownOne = KnownOne.zext(truncBf);
Craig Topper47596dd2017-03-25 06:52:52 +0000346 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
347 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000348 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000349 DemandedMask = DemandedMask.trunc(BitWidth);
350 KnownZero = KnownZero.trunc(BitWidth);
351 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000352 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000353 break;
354 }
355 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000356 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000357 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000358
Chris Lattner229907c2011-07-18 04:54:35 +0000359 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
360 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000361 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
362 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
363 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000364 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000365 } else
366 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000367 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000368 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000369 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000370 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000371
Craig Topper47596dd2017-03-25 06:52:52 +0000372 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
373 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000374 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000375 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000376 break;
377 case Instruction::ZExt: {
378 // Compute the bits in the result that are not present in the input.
379 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000380
Jay Foad583abbc2010-12-07 08:25:19 +0000381 DemandedMask = DemandedMask.trunc(SrcBitWidth);
382 KnownZero = KnownZero.trunc(SrcBitWidth);
383 KnownOne = KnownOne.trunc(SrcBitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000384 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
385 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000386 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000387 DemandedMask = DemandedMask.zext(BitWidth);
388 KnownZero = KnownZero.zext(BitWidth);
389 KnownOne = KnownOne.zext(BitWidth);
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 // The top bits are known to be zero.
Craig Topper3a86a042017-03-19 05:49:16 +0000392 KnownZero.setBitsFrom(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000393 break;
394 }
395 case Instruction::SExt: {
396 // Compute the bits in the result that are not present in the input.
397 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000398
399 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000400 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
401
Craig Topper3a86a042017-03-19 05:49:16 +0000402 APInt NewBits(APInt::getBitsSetFrom(BitWidth, SrcBitWidth));
Chris Lattner7e044912010-01-04 07:17:19 +0000403 // If any of the sign extended bits are demanded, we know that the sign
404 // bit is demanded.
405 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000406 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000407
Jay Foad583abbc2010-12-07 08:25:19 +0000408 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
409 KnownZero = KnownZero.trunc(SrcBitWidth);
410 KnownOne = KnownOne.trunc(SrcBitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000411 if (SimplifyDemandedBits(I, 0, InputDemandedBits, KnownZero, KnownOne,
412 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000413 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000414 InputDemandedBits = InputDemandedBits.zext(BitWidth);
415 KnownZero = KnownZero.zext(BitWidth);
416 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000417 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
418
Chris Lattner7e044912010-01-04 07:17:19 +0000419 // If the sign bit of the input is known set or clear, then we know the
420 // top bits of the result.
421
422 // If the input sign bit is known zero, or if the NewBits are not demanded
423 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000424 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000425 // Convert to ZExt cast
426 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000427 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000428 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
429 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000430 }
431 break;
432 }
Matthias Braune48484c2015-04-30 22:05:30 +0000433 case Instruction::Add:
434 case Instruction::Sub: {
435 /// If the high-bits of an ADD/SUB are not demanded, then we do not care
436 /// about the high bits of the operands.
Chris Lattner7e044912010-01-04 07:17:19 +0000437 unsigned NLZ = DemandedMask.countLeadingZeros();
Matthias Braune48484c2015-04-30 22:05:30 +0000438 if (NLZ > 0) {
439 // Right fill the mask of bits for this ADD/SUB to demand the most
Chris Lattner7e044912010-01-04 07:17:19 +0000440 // significant bit and all those below it.
Chris Lattner7e044912010-01-04 07:17:19 +0000441 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
Craig Topper07f29152017-03-22 04:03:53 +0000442 if (ShrinkDemandedConstant(I, 0, DemandedFromOps) ||
Craig Topper47596dd2017-03-25 06:52:52 +0000443 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnownZero, LHSKnownOne,
444 Depth + 1) ||
Matthias Braune48484c2015-04-30 22:05:30 +0000445 ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
Craig Topper845033a2017-04-12 16:49:59 +0000446 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnownZero, RHSKnownOne,
Craig Topper47596dd2017-03-25 06:52:52 +0000447 Depth + 1)) {
Matthias Braune48484c2015-04-30 22:05:30 +0000448 // Disable the nsw and nuw flags here: We can no longer guarantee that
449 // we won't wrap after simplification. Removing the nsw/nuw flags is
450 // legal here because the top bit is not demanded.
451 BinaryOperator &BinOP = *cast<BinaryOperator>(I);
452 BinOP.setHasNoSignedWrap(false);
453 BinOP.setHasNoUnsignedWrap(false);
Chris Lattner7e044912010-01-04 07:17:19 +0000454 return I;
David Majnemer7d0e99c2015-04-22 22:42:05 +0000455 }
Craig Topper845033a2017-04-12 16:49:59 +0000456
457 // If we are known to be adding/subtracting zeros to every bit below
458 // the highest demanded bit, we just return the other side.
459 if ((DemandedFromOps & RHSKnownZero) == DemandedFromOps)
460 return I->getOperand(0);
461 // We can't do this with the LHS for subtraction.
462 if (I->getOpcode() == Instruction::Add &&
463 (DemandedFromOps & LHSKnownZero) == DemandedFromOps)
464 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000465 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000466
Craig Topper8fbb74b2017-03-24 22:12:10 +0000467 // Otherwise just hand the add/sub off to computeKnownBits to fill in
468 // the known zeros and ones.
469 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000470 break;
Matthias Braune48484c2015-04-30 22:05:30 +0000471 }
Sanjay Patel3e1ae722017-04-20 21:33:02 +0000472 case Instruction::Shl: {
473 const APInt *SA;
474 if (match(I->getOperand(1), m_APInt(SA))) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000475 {
476 Value *VarX; ConstantInt *C1;
477 if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
478 Instruction *Shr = cast<Instruction>(I->getOperand(0));
479 Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
480 KnownZero, KnownOne);
481 if (R)
482 return R;
483 }
484 }
485
Chris Lattner768003c2011-02-10 05:09:34 +0000486 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000487 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000488
Chris Lattner768003c2011-02-10 05:09:34 +0000489 // If the shift is NUW/NSW, then it does demand the high bits.
490 ShlOperator *IOp = cast<ShlOperator>(I);
491 if (IOp->hasNoSignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000492 DemandedMaskIn.setHighBits(ShiftAmt+1);
Chris Lattner768003c2011-02-10 05:09:34 +0000493 else if (IOp->hasNoUnsignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000494 DemandedMaskIn.setHighBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000495
Craig Topper47596dd2017-03-25 06:52:52 +0000496 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
497 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000498 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000499 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
500 KnownZero <<= ShiftAmt;
501 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000502 // low bits known zero.
503 if (ShiftAmt)
Craig Topper3a86a042017-03-19 05:49:16 +0000504 KnownZero.setLowBits(ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000505 }
506 break;
Sanjay Patel3e1ae722017-04-20 21:33:02 +0000507 }
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000508 case Instruction::LShr: {
509 const APInt *SA;
510 if (match(I->getOperand(1), m_APInt(SA))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000511 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000512
Chris Lattner7e044912010-01-04 07:17:19 +0000513 // Unsigned shift right.
514 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000515
Chris Lattner768003c2011-02-10 05:09:34 +0000516 // If the shift is exact, then it does demand the low bits (and knows that
517 // they are zero).
518 if (cast<LShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000519 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000520
Craig Topper47596dd2017-03-25 06:52:52 +0000521 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
522 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000523 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000524 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Craig Topperfc947bc2017-04-18 17:14:21 +0000525 KnownZero.lshrInPlace(ShiftAmt);
526 KnownOne.lshrInPlace(ShiftAmt);
Craig Topper3a86a042017-03-19 05:49:16 +0000527 if (ShiftAmt)
528 KnownZero.setHighBits(ShiftAmt); // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000529 }
530 break;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000531 }
532 case Instruction::AShr: {
Chris Lattner7e044912010-01-04 07:17:19 +0000533 // If this is an arithmetic shift right and only the low-bit is set, we can
534 // always convert this into a logical shr, even if the shift amount is
535 // variable. The low bit of the shift cannot be an input sign bit unless
536 // the shift amount is >= the size of the datatype, which is undefined.
537 if (DemandedMask == 1) {
538 // Perform the logical shift right.
539 Instruction *NewVal = BinaryOperator::CreateLShr(
540 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000541 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000542 }
Chris Lattner7e044912010-01-04 07:17:19 +0000543
544 // If the sign bit is the only bit demanded by this ashr, then there is no
545 // need to do it, the shift doesn't change the high bit.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000546 if (DemandedMask.isSignMask())
Chris Lattner7e044912010-01-04 07:17:19 +0000547 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000548
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000549 const APInt *SA;
550 if (match(I->getOperand(1), m_APInt(SA))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000551 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000552
Chris Lattner7e044912010-01-04 07:17:19 +0000553 // Signed shift right.
554 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000555 // If any of the high bits are demanded, we should set the sign bit as
Chris Lattner7e044912010-01-04 07:17:19 +0000556 // demanded.
557 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Craig Topperc9a4fc02017-04-14 05:09:04 +0000558 DemandedMaskIn.setSignBit();
Craig Topper4c947752012-12-22 18:09:02 +0000559
Chris Lattner768003c2011-02-10 05:09:34 +0000560 // If the shift is exact, then it does demand the low bits (and knows that
561 // they are zero).
562 if (cast<AShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000563 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000564
Craig Topper47596dd2017-03-25 06:52:52 +0000565 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
566 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000567 return I;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000568
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000569 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000570 // Compute the new bits that are at the top now.
571 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Craig Topperfc947bc2017-04-18 17:14:21 +0000572 KnownZero.lshrInPlace(ShiftAmt);
573 KnownOne.lshrInPlace(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000574
Chris Lattner7e044912010-01-04 07:17:19 +0000575 // Handle the sign bits.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000576 APInt SignMask(APInt::getSignMask(BitWidth));
Chris Lattner7e044912010-01-04 07:17:19 +0000577 // Adjust to where it is now in the mask.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000578 SignMask.lshrInPlace(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000579
Chris Lattner7e044912010-01-04 07:17:19 +0000580 // If the input sign bit is known to be zero, or if none of the top bits
581 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000582 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Craig Topperff238892017-04-20 21:24:37 +0000583 !DemandedMask.intersects(HighBits)) {
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000584 BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0),
585 I->getOperand(1));
586 LShr->setIsExact(cast<BinaryOperator>(I)->isExact());
587 return InsertNewInstWith(LShr, *I);
Craig Topperff238892017-04-20 21:24:37 +0000588 } else if (KnownOne.intersects(SignMask)) { // New bits are known one.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000589 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000590 }
591 }
592 break;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000593 }
Chris Lattner7e044912010-01-04 07:17:19 +0000594 case Instruction::SRem:
595 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000596 // X % -1 demands all the bits because we don't want to introduce
597 // INT_MIN % -1 (== undef) by accident.
598 if (Rem->isAllOnesValue())
599 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000600 APInt RA = Rem->getValue().abs();
601 if (RA.isPowerOf2()) {
602 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
603 return I->getOperand(0);
604
605 APInt LowBits = RA - 1;
Craig Topperbcfd2d12017-04-20 16:56:25 +0000606 APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000607 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnownZero, LHSKnownOne,
608 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000609 return I;
610
Duncan Sands3a48b872010-01-28 17:22:42 +0000611 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000612 KnownZero = LHSKnownZero & LowBits;
613 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000614
Duncan Sands3a48b872010-01-28 17:22:42 +0000615 // If LHS is non-negative or has all low bits zero, then the upper bits
616 // are all zero.
Craig Topperff238892017-04-20 21:24:37 +0000617 if (LHSKnownZero.isSignBitSet() || LowBits.isSubsetOf(LHSKnownZero))
Duncan Sands3a48b872010-01-28 17:22:42 +0000618 KnownZero |= ~LowBits;
619
620 // If LHS is negative and not all low bits are zero, then the upper bits
621 // are all one.
Craig Topperff238892017-04-20 21:24:37 +0000622 if (LHSKnownOne.isSignBitSet() && LowBits.intersects(LHSKnownOne))
Duncan Sands3a48b872010-01-28 17:22:42 +0000623 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000624
Craig Topper4c947752012-12-22 18:09:02 +0000625 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Craig Topperda886c62017-04-16 21:46:12 +0000626 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000627 }
628 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000629
630 // The sign bit is the LHS's sign bit, except when the result of the
631 // remainder is zero.
Craig Topperd23004c2017-04-17 16:38:20 +0000632 if (DemandedMask.isSignBitSet()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000633 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000634 CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000635 // If it's known zero, our sign bit is also zero.
Craig Topperd23004c2017-04-17 16:38:20 +0000636 if (LHSKnownZero.isSignBitSet())
Craig Topper3a86a042017-03-19 05:49:16 +0000637 KnownZero.setSignBit();
Nick Lewyckye4679792011-03-07 01:50:10 +0000638 }
Chris Lattner7e044912010-01-04 07:17:19 +0000639 break;
640 case Instruction::URem: {
641 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
642 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000643 if (SimplifyDemandedBits(I, 0, AllOnes, KnownZero2, KnownOne2, Depth + 1) ||
644 SimplifyDemandedBits(I, 1, AllOnes, KnownZero2, KnownOne2, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000645 return I;
646
647 unsigned Leaders = KnownZero2.countLeadingOnes();
Chris Lattner7e044912010-01-04 07:17:19 +0000648 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
649 break;
650 }
651 case Instruction::Call:
652 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
653 switch (II->getIntrinsicID()) {
654 default: break;
655 case Intrinsic::bswap: {
656 // If the only bits demanded come from one byte of the bswap result,
657 // just shift the input byte into position to eliminate the bswap.
658 unsigned NLZ = DemandedMask.countLeadingZeros();
659 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000660
Chris Lattner7e044912010-01-04 07:17:19 +0000661 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
662 // we need all the bits down to bit 8. Likewise, round NLZ. If we
663 // have 14 leading zeros, round to 8.
664 NLZ &= ~7;
665 NTZ &= ~7;
666 // If we need exactly one byte, we can do this transformation.
667 if (BitWidth-NLZ-NTZ == 8) {
668 unsigned ResultBit = NTZ;
669 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000670
Chris Lattner7e044912010-01-04 07:17:19 +0000671 // Replace this with either a left or right shift to get the byte into
672 // the right place.
673 Instruction *NewVal;
674 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000675 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000676 ConstantInt::get(I->getType(), InputBit-ResultBit));
677 else
Gabor Greif79430172010-06-24 12:35:13 +0000678 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000679 ConstantInt::get(I->getType(), ResultBit-InputBit));
680 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000681 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000682 }
Craig Topper4c947752012-12-22 18:09:02 +0000683
Chris Lattner7e044912010-01-04 07:17:19 +0000684 // TODO: Could compute known zero/one bits based on the input.
685 break;
686 }
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000687 case Intrinsic::x86_mmx_pmovmskb:
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000688 case Intrinsic::x86_sse_movmsk_ps:
689 case Intrinsic::x86_sse2_movmsk_pd:
690 case Intrinsic::x86_sse2_pmovmskb_128:
691 case Intrinsic::x86_avx_movmsk_ps_256:
692 case Intrinsic::x86_avx_movmsk_pd_256:
693 case Intrinsic::x86_avx2_pmovmskb: {
694 // MOVMSK copies the vector elements' sign bits to the low bits
695 // and zeros the high bits.
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000696 unsigned ArgWidth;
697 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
698 ArgWidth = 8; // Arg is x86_mmx, but treated as <8 x i8>.
699 } else {
700 auto Arg = II->getArgOperand(0);
701 auto ArgType = cast<VectorType>(Arg->getType());
702 ArgWidth = ArgType->getNumElements();
703 }
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000704
705 // If we don't need any of low bits then return zero,
706 // we know that DemandedMask is non-zero already.
707 APInt DemandedElts = DemandedMask.zextOrTrunc(ArgWidth);
708 if (DemandedElts == 0)
709 return ConstantInt::getNullValue(VTy);
710
Ahmed Bougacha17482a52016-04-28 14:36:07 +0000711 // We know that the upper bits are set to zero.
Craig Topper3a86a042017-03-19 05:49:16 +0000712 KnownZero.setBitsFrom(ArgWidth);
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000713 return nullptr;
714 }
Chad Rosierb3628842011-05-26 23:13:19 +0000715 case Intrinsic::x86_sse42_crc32_64_64:
Craig Topper3a86a042017-03-19 05:49:16 +0000716 KnownZero.setBitsFrom(32);
Craig Topperf40110f2014-04-25 05:29:35 +0000717 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000718 }
719 }
Hal Finkel60db0582014-09-07 18:57:58 +0000720 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000721 break;
722 }
Craig Topper4c947752012-12-22 18:09:02 +0000723
Chris Lattner7e044912010-01-04 07:17:19 +0000724 // If the client is only demanding bits that we know, return the known
725 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000726 if (DemandedMask.isSubsetOf(KnownZero|KnownOne))
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000727 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000728 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000729}
730
Craig Topperb0076fe2017-04-12 18:05:21 +0000731/// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
732/// bits. It also tries to handle simplifications that can be done based on
733/// DemandedMask, but without modifying the Instruction.
734Value *InstCombiner::SimplifyMultipleUseDemandedBits(Instruction *I,
735 const APInt &DemandedMask,
736 APInt &KnownZero,
737 APInt &KnownOne,
738 unsigned Depth,
739 Instruction *CxtI) {
740 unsigned BitWidth = DemandedMask.getBitWidth();
741 Type *ITy = I->getType();
742
743 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
744 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
745
746 // Despite the fact that we can't simplify this instruction in all User's
747 // context, we can at least compute the knownzero/knownone bits, and we can
748 // do simplifications that apply to *just* the one user if we know that
749 // this instruction has a simpler value in that context.
Craig Topperf35a7f72017-04-12 18:25:25 +0000750 switch (I->getOpcode()) {
Craig Topper9a458cd2017-04-14 22:34:14 +0000751 case Instruction::And: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000752 // If either the LHS or the RHS are Zero, the result is zero.
753 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
754 CxtI);
755 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
756 CxtI);
757
Craig Topper9a458cd2017-04-14 22:34:14 +0000758 // Output known-0 are known to be clear if zero in either the LHS | RHS.
759 APInt IKnownZero = RHSKnownZero | LHSKnownZero;
760 // Output known-1 bits are only known if set in both the LHS & RHS.
761 APInt IKnownOne = RHSKnownOne & LHSKnownOne;
762
Craig Topperc75f94b2017-04-12 19:32:47 +0000763 // If the client is only demanding bits that we know, return the known
764 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000765 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000766 return Constant::getIntegerValue(ITy, IKnownOne);
Craig Topperc75f94b2017-04-12 19:32:47 +0000767
Craig Topperb0076fe2017-04-12 18:05:21 +0000768 // If all of the demanded bits are known 1 on one side, return the other.
769 // These bits cannot contribute to the result of the 'and' in this
770 // context.
Craig Topper17f37ba2017-04-20 20:47:35 +0000771 if (DemandedMask.isSubsetOf(LHSKnownZero | RHSKnownOne))
Craig Topperb0076fe2017-04-12 18:05:21 +0000772 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000773 if (DemandedMask.isSubsetOf(RHSKnownZero | LHSKnownOne))
Craig Topperb0076fe2017-04-12 18:05:21 +0000774 return I->getOperand(1);
775
Craig Topper9a458cd2017-04-14 22:34:14 +0000776 KnownZero = std::move(IKnownZero);
777 KnownOne = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000778 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000779 }
780 case Instruction::Or: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000781 // We can simplify (X|Y) -> X or Y in the user's context if we know that
782 // only bits from X or Y are demanded.
783
784 // If either the LHS or the RHS are One, the result is One.
785 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
786 CxtI);
787 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
788 CxtI);
789
Craig Topper9a458cd2017-04-14 22:34:14 +0000790 // Output known-0 bits are only known if clear in both the LHS & RHS.
791 APInt IKnownZero = RHSKnownZero & LHSKnownZero;
792 // Output known-1 are known to be set if set in either the LHS | RHS.
793 APInt IKnownOne = RHSKnownOne | LHSKnownOne;
794
Craig Topperc75f94b2017-04-12 19:32:47 +0000795 // If the client is only demanding bits that we know, return the known
796 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000797 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000798 return Constant::getIntegerValue(ITy, IKnownOne);
Craig Topperc75f94b2017-04-12 19:32:47 +0000799
Craig Topperb0076fe2017-04-12 18:05:21 +0000800 // If all of the demanded bits are known zero on one side, return the
801 // other. These bits cannot contribute to the result of the 'or' in this
802 // context.
Craig Topper17f37ba2017-04-20 20:47:35 +0000803 if (DemandedMask.isSubsetOf(LHSKnownOne | RHSKnownZero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000804 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000805 if (DemandedMask.isSubsetOf(RHSKnownOne | LHSKnownZero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000806 return I->getOperand(1);
807
Craig Topper9a458cd2017-04-14 22:34:14 +0000808 KnownZero = std::move(IKnownZero);
809 KnownOne = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000810 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000811 }
Craig Topperc75f94b2017-04-12 19:32:47 +0000812 case Instruction::Xor: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000813 // We can simplify (X^Y) -> X or Y in the user's context if we know that
814 // only bits from X or Y are demanded.
815
816 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
817 CxtI);
818 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
819 CxtI);
820
Craig Topperc75f94b2017-04-12 19:32:47 +0000821 // Output known-0 bits are known if clear or set in both the LHS & RHS.
822 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
823 (RHSKnownOne & LHSKnownOne);
824 // Output known-1 are known to be set if set in only one of the LHS, RHS.
825 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
826 (RHSKnownOne & LHSKnownZero);
827
828 // If the client is only demanding bits that we know, return the known
829 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000830 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topperc75f94b2017-04-12 19:32:47 +0000831 return Constant::getIntegerValue(ITy, IKnownOne);
832
Craig Topperb0076fe2017-04-12 18:05:21 +0000833 // If all of the demanded bits are known zero on one side, return the
834 // other.
Craig Topper17f37ba2017-04-20 20:47:35 +0000835 if (DemandedMask.isSubsetOf(RHSKnownZero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000836 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000837 if (DemandedMask.isSubsetOf(LHSKnownZero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000838 return I->getOperand(1);
Craig Topperf35a7f72017-04-12 18:25:25 +0000839
Craig Topperc75f94b2017-04-12 19:32:47 +0000840 // Output known-0 bits are known if clear or set in both the LHS & RHS.
841 KnownZero = std::move(IKnownZero);
842 // Output known-1 are known to be set if set in only one of the LHS, RHS.
843 KnownOne = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000844 break;
Craig Topperb0076fe2017-04-12 18:05:21 +0000845 }
Craig Topperc75f94b2017-04-12 19:32:47 +0000846 default:
847 // Compute the KnownZero/KnownOne bits to simplify things downstream.
848 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Craig Topperb0076fe2017-04-12 18:05:21 +0000849
Craig Topperc75f94b2017-04-12 19:32:47 +0000850 // If this user is only demanding bits that we know, return the known
851 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000852 if (DemandedMask.isSubsetOf(KnownZero|KnownOne))
Craig Topperc75f94b2017-04-12 19:32:47 +0000853 return Constant::getIntegerValue(ITy, KnownOne);
Craig Topper9a51c7f2017-04-12 18:17:46 +0000854
Craig Topperc75f94b2017-04-12 19:32:47 +0000855 break;
856 }
Craig Topper9a51c7f2017-04-12 18:17:46 +0000857
Craig Topperb0076fe2017-04-12 18:05:21 +0000858 return nullptr;
859}
860
861
Shuxin Yang63e999e2012-12-04 00:04:54 +0000862/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
863/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
864/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
865/// of "C2-C1".
866///
867/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
868/// ..., bn}, without considering the specific value X is holding.
869/// This transformation is legal iff one of following conditions is hold:
870/// 1) All the bit in S are 0, in this case E1 == E2.
871/// 2) We don't care those bits in S, per the input DemandedMask.
872/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
873/// rest bits.
874///
875/// Currently we only test condition 2).
876///
877/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
878/// not successful.
879Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
Benjamin Kramerc321e532016-06-08 19:09:22 +0000880 Instruction *Shl,
881 const APInt &DemandedMask,
882 APInt &KnownZero,
883 APInt &KnownOne) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000884
Benjamin Kramer010f1082013-08-30 14:35:35 +0000885 const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
886 const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
887 if (!ShlOp1 || !ShrOp1)
Craig Topperf40110f2014-04-25 05:29:35 +0000888 return nullptr; // Noop.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000889
890 Value *VarX = Shr->getOperand(0);
891 Type *Ty = VarX->getType();
892 unsigned BitWidth = Ty->getIntegerBitWidth();
893 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000894 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000895
896 unsigned ShlAmt = ShlOp1.getZExtValue();
897 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000898
899 KnownOne.clearAllBits();
Craig Topper3a86a042017-03-19 05:49:16 +0000900 KnownZero.setLowBits(ShlAmt - 1);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000901 KnownZero &= DemandedMask;
902
Benjamin Kramer010f1082013-08-30 14:35:35 +0000903 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
904 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000905
906 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
907 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
908 (BitMask1.ashr(ShrAmt) << ShlAmt);
909
910 if (ShrAmt <= ShlAmt) {
911 BitMask2 <<= (ShlAmt - ShrAmt);
912 } else {
913 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
914 BitMask2.ashr(ShrAmt - ShlAmt);
915 }
916
917 // Check if condition-2 (see the comment to this function) is satified.
918 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
919 if (ShrAmt == ShlAmt)
920 return VarX;
921
922 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000923 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000924
925 BinaryOperator *New;
926 if (ShrAmt < ShlAmt) {
927 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
928 New = BinaryOperator::CreateShl(VarX, Amt);
929 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
930 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
931 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
932 } else {
933 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000934 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
935 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000936 if (cast<BinaryOperator>(Shr)->isExact())
937 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000938 }
939
940 return InsertNewInstWith(New, *Shl);
941 }
942
Craig Topperf40110f2014-04-25 05:29:35 +0000943 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000944}
Chris Lattner7e044912010-01-04 07:17:19 +0000945
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +0000946/// The specified value produces a vector with any number of elements.
947/// DemandedElts contains the set of elements that are actually used by the
948/// caller. This method analyzes which elements of the operand are undef and
949/// returns that information in UndefElts.
Chris Lattner7e044912010-01-04 07:17:19 +0000950///
951/// If the information about demanded elements can be used to simplify the
952/// operation, the operation is simplified, then the resultant value is
953/// returned. This returns null if no change was made.
954Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000955 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000956 unsigned Depth) {
Sanjay Patel9190b4a2016-04-29 20:54:56 +0000957 unsigned VWidth = V->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +0000958 APInt EltMask(APInt::getAllOnesValue(VWidth));
959 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
960
961 if (isa<UndefValue>(V)) {
962 // If the entire vector is undefined, just return this info.
963 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000964 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000965 }
Craig Topper4c947752012-12-22 18:09:02 +0000966
Chris Lattnerb22423c2010-02-08 23:56:03 +0000967 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000968 UndefElts = EltMask;
969 return UndefValue::get(V->getType());
970 }
971
972 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000973
Chris Lattner67058832012-01-25 06:48:06 +0000974 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
975 if (Constant *C = dyn_cast<Constant>(V)) {
976 // Check if this is identity. If so, return 0 since we are not simplifying
977 // anything.
978 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000979 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000980
Chris Lattner229907c2011-07-18 04:54:35 +0000981 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000982 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000983
Chris Lattner67058832012-01-25 06:48:06 +0000984 SmallVector<Constant*, 16> Elts;
985 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000986 if (!DemandedElts[i]) { // If not demanded, set to undef.
987 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000988 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +0000989 continue;
990 }
Craig Topper4c947752012-12-22 18:09:02 +0000991
Chris Lattner67058832012-01-25 06:48:06 +0000992 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000993 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000994
Chris Lattner67058832012-01-25 06:48:06 +0000995 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000996 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000997 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +0000998 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +0000999 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +00001000 }
Chris Lattner67058832012-01-25 06:48:06 +00001001 }
Craig Topper4c947752012-12-22 18:09:02 +00001002
Chris Lattner7e044912010-01-04 07:17:19 +00001003 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +00001004 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +00001005 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001006 }
Craig Topper4c947752012-12-22 18:09:02 +00001007
Chris Lattner7e044912010-01-04 07:17:19 +00001008 // Limit search depth.
1009 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +00001010 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001011
Stuart Hastings5bd18b62011-05-17 22:13:31 +00001012 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +00001013 // simplification conservatively assuming that all elements
1014 // are needed.
1015 if (!V->hasOneUse()) {
1016 // Quit if we find multiple users of a non-root value though.
1017 // They'll be handled when it's their turn to be visited by
1018 // the main instcombine process.
1019 if (Depth != 0)
1020 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +00001021 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001022
1023 // Conservatively assume that all elements are needed.
1024 DemandedElts = EltMask;
1025 }
Craig Topper4c947752012-12-22 18:09:02 +00001026
Chris Lattner7e044912010-01-04 07:17:19 +00001027 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +00001028 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +00001029
Chris Lattner7e044912010-01-04 07:17:19 +00001030 bool MadeChange = false;
1031 APInt UndefElts2(VWidth, 0);
Craig Topper23ebd952016-12-11 08:54:52 +00001032 APInt UndefElts3(VWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001033 Value *TmpV;
1034 switch (I->getOpcode()) {
1035 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001036
Chris Lattner7e044912010-01-04 07:17:19 +00001037 case Instruction::InsertElement: {
1038 // If this is a variable index, we don't know which element it overwrites.
1039 // demand exactly the same input as we produce.
1040 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +00001041 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +00001042 // Note that we can't propagate undef elt info, because we don't know
1043 // which elt is getting updated.
1044 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001045 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001046 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1047 break;
1048 }
Craig Topper4c947752012-12-22 18:09:02 +00001049
Chris Lattner7e044912010-01-04 07:17:19 +00001050 // If this is inserting an element that isn't demanded, remove this
1051 // insertelement.
1052 unsigned IdxNo = Idx->getZExtValue();
1053 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1054 Worklist.Add(I);
1055 return I->getOperand(0);
1056 }
Craig Topper4c947752012-12-22 18:09:02 +00001057
Chris Lattner7e044912010-01-04 07:17:19 +00001058 // Otherwise, the element inserted overwrites whatever was there, so the
1059 // input demanded set is simpler than the output set.
1060 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001061 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001062 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001063 UndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001064 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1065
1066 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001067 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001068 break;
1069 }
1070 case Instruction::ShuffleVector: {
1071 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Craig Topper2e18bcf2016-12-29 04:24:32 +00001072 unsigned LHSVWidth =
1073 Shuffle->getOperand(0)->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +00001074 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1075 for (unsigned i = 0; i < VWidth; i++) {
1076 if (DemandedElts[i]) {
1077 unsigned MaskVal = Shuffle->getMaskValue(i);
1078 if (MaskVal != -1u) {
1079 assert(MaskVal < LHSVWidth * 2 &&
1080 "shufflevector mask index out of range!");
1081 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001082 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001083 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001084 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001085 }
1086 }
1087 }
1088
Alexey Bataevfee90782016-09-23 09:14:08 +00001089 APInt LHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001090 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001091 LHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001092 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1093
Alexey Bataevfee90782016-09-23 09:14:08 +00001094 APInt RHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001095 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001096 RHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001097 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1098
1099 bool NewUndefElts = false;
Alexey Bataev793c9462016-09-26 13:18:59 +00001100 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1101 unsigned RHSIdx = -1u, RHSValIdx = -1u;
Alexey Bataevfee90782016-09-23 09:14:08 +00001102 bool LHSUniform = true;
1103 bool RHSUniform = true;
Chris Lattner7e044912010-01-04 07:17:19 +00001104 for (unsigned i = 0; i < VWidth; i++) {
1105 unsigned MaskVal = Shuffle->getMaskValue(i);
1106 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001107 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001108 } else if (!DemandedElts[i]) {
1109 NewUndefElts = true;
1110 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001111 } else if (MaskVal < LHSVWidth) {
Alexey Bataevfee90782016-09-23 09:14:08 +00001112 if (LHSUndefElts[MaskVal]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001113 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001114 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001115 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001116 LHSIdx = LHSIdx == -1u ? i : LHSVWidth;
1117 LHSValIdx = LHSValIdx == -1u ? MaskVal : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001118 LHSUniform = LHSUniform && (MaskVal == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001119 }
1120 } else {
Alexey Bataevfee90782016-09-23 09:14:08 +00001121 if (RHSUndefElts[MaskVal - LHSVWidth]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001122 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001123 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001124 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001125 RHSIdx = RHSIdx == -1u ? i : LHSVWidth;
1126 RHSValIdx = RHSValIdx == -1u ? MaskVal - LHSVWidth : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001127 RHSUniform = RHSUniform && (MaskVal - LHSVWidth == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001128 }
1129 }
1130 }
1131
Alexey Bataevfee90782016-09-23 09:14:08 +00001132 // Try to transform shuffle with constant vector and single element from
1133 // this constant vector to single insertelement instruction.
1134 // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
1135 // insertelement V, C[ci], ci-n
1136 if (LHSVWidth == Shuffle->getType()->getNumElements()) {
1137 Value *Op = nullptr;
1138 Constant *Value = nullptr;
1139 unsigned Idx = -1u;
1140
Craig Topper62f06e22016-12-29 05:38:31 +00001141 // Find constant vector with the single element in shuffle (LHS or RHS).
Alexey Bataevfee90782016-09-23 09:14:08 +00001142 if (LHSIdx < LHSVWidth && RHSUniform) {
1143 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1144 Op = Shuffle->getOperand(1);
Alexey Bataev793c9462016-09-26 13:18:59 +00001145 Value = CV->getOperand(LHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001146 Idx = LHSIdx;
1147 }
1148 }
1149 if (RHSIdx < LHSVWidth && LHSUniform) {
1150 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1151 Op = Shuffle->getOperand(0);
Alexey Bataev793c9462016-09-26 13:18:59 +00001152 Value = CV->getOperand(RHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001153 Idx = RHSIdx;
1154 }
1155 }
1156 // Found constant vector with single element - convert to insertelement.
1157 if (Op && Value) {
1158 Instruction *New = InsertElementInst::Create(
1159 Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx),
1160 Shuffle->getName());
1161 InsertNewInstWith(New, *Shuffle);
1162 return New;
1163 }
1164 }
Chris Lattner7e044912010-01-04 07:17:19 +00001165 if (NewUndefElts) {
1166 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001167 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001168 for (unsigned i = 0; i < VWidth; ++i) {
1169 if (UndefElts[i])
1170 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1171 else
1172 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1173 Shuffle->getMaskValue(i)));
1174 }
1175 I->setOperand(2, ConstantVector::get(Elts));
1176 MadeChange = true;
1177 }
1178 break;
1179 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001180 case Instruction::Select: {
1181 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1182 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1183 for (unsigned i = 0; i < VWidth; i++) {
Andrea Di Biagio40f59e42015-10-06 10:34:53 +00001184 Constant *CElt = CV->getAggregateElement(i);
1185 // Method isNullValue always returns false when called on a
1186 // ConstantExpr. If CElt is a ConstantExpr then skip it in order to
1187 // to avoid propagating incorrect information.
1188 if (isa<ConstantExpr>(CElt))
1189 continue;
1190 if (CElt->isNullValue())
Pete Cooperabc13af2012-07-26 23:10:24 +00001191 LeftDemanded.clearBit(i);
1192 else
1193 RightDemanded.clearBit(i);
1194 }
1195 }
1196
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001197 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded, UndefElts,
1198 Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001199 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1200
1201 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001202 UndefElts2, Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001203 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001204
Pete Cooperabc13af2012-07-26 23:10:24 +00001205 // Output elements are undefined if both are undefined.
1206 UndefElts &= UndefElts2;
1207 break;
1208 }
Chris Lattner7e044912010-01-04 07:17:19 +00001209 case Instruction::BitCast: {
1210 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001211 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001212 if (!VTy) break;
1213 unsigned InVWidth = VTy->getNumElements();
1214 APInt InputDemandedElts(InVWidth, 0);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001215 UndefElts2 = APInt(InVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001216 unsigned Ratio;
1217
1218 if (VWidth == InVWidth) {
1219 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1220 // elements as are demanded of us.
1221 Ratio = 1;
1222 InputDemandedElts = DemandedElts;
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001223 } else if ((VWidth % InVWidth) == 0) {
1224 // If the number of elements in the output is a multiple of the number of
1225 // elements in the input then an input element is live if any of the
1226 // corresponding output elements are live.
1227 Ratio = VWidth / InVWidth;
1228 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Chris Lattner7e044912010-01-04 07:17:19 +00001229 if (DemandedElts[OutIdx])
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001230 InputDemandedElts.setBit(OutIdx / Ratio);
1231 } else if ((InVWidth % VWidth) == 0) {
1232 // If the number of elements in the input is a multiple of the number of
1233 // elements in the output then an input element is live if the
1234 // corresponding output element is live.
1235 Ratio = InVWidth / VWidth;
Chris Lattner7e044912010-01-04 07:17:19 +00001236 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001237 if (DemandedElts[InIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001238 InputDemandedElts.setBit(InIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001239 } else {
1240 // Unsupported so far.
1241 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001242 }
Craig Topper4c947752012-12-22 18:09:02 +00001243
Chris Lattner7e044912010-01-04 07:17:19 +00001244 // div/rem demand all inputs, because they don't want divide by zero.
1245 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001246 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001247 if (TmpV) {
1248 I->setOperand(0, TmpV);
1249 MadeChange = true;
1250 }
Craig Topper4c947752012-12-22 18:09:02 +00001251
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001252 if (VWidth == InVWidth) {
1253 UndefElts = UndefElts2;
1254 } else if ((VWidth % InVWidth) == 0) {
1255 // If the number of elements in the output is a multiple of the number of
1256 // elements in the input then an output element is undef if the
1257 // corresponding input element is undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001258 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001259 if (UndefElts2[OutIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001260 UndefElts.setBit(OutIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001261 } else if ((InVWidth % VWidth) == 0) {
1262 // If the number of elements in the input is a multiple of the number of
1263 // elements in the output then an output element is undef if all of the
1264 // corresponding input elements are undef.
1265 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1266 APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
1267 if (SubUndef.countPopulation() == Ratio)
1268 UndefElts.setBit(OutIdx);
1269 }
1270 } else {
Chris Lattner7e044912010-01-04 07:17:19 +00001271 llvm_unreachable("Unimp");
Chris Lattner7e044912010-01-04 07:17:19 +00001272 }
1273 break;
1274 }
1275 case Instruction::And:
1276 case Instruction::Or:
1277 case Instruction::Xor:
1278 case Instruction::Add:
1279 case Instruction::Sub:
1280 case Instruction::Mul:
1281 // div/rem demand all inputs, because they don't want divide by zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001282 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1283 Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001284 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1285 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001286 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001287 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001288
Chris Lattner7e044912010-01-04 07:17:19 +00001289 // Output elements are undefined if both are undefined. Consider things
1290 // like undef&0. The result is known zero, not undef.
1291 UndefElts &= UndefElts2;
1292 break;
Pete Coopere807e452012-07-26 22:37:04 +00001293 case Instruction::FPTrunc:
1294 case Instruction::FPExt:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001295 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1296 Depth + 1);
Pete Coopere807e452012-07-26 22:37:04 +00001297 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1298 break;
Craig Topper4c947752012-12-22 18:09:02 +00001299
Chris Lattner7e044912010-01-04 07:17:19 +00001300 case Instruction::Call: {
1301 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1302 if (!II) break;
1303 switch (II->getIntrinsicID()) {
1304 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001305
Craig Topper7fc6d342016-12-11 22:32:38 +00001306 case Intrinsic::x86_xop_vfrcz_ss:
1307 case Intrinsic::x86_xop_vfrcz_sd:
1308 // The instructions for these intrinsics are speced to zero upper bits not
1309 // pass them through like other scalar intrinsics. So we shouldn't just
1310 // use Arg0 if DemandedElts[0] is clear like we do for other intrinsics.
1311 // Instead we should return a zero vector.
Craig Topper1a8a3372016-12-29 03:30:17 +00001312 if (!DemandedElts[0]) {
1313 Worklist.Add(II);
Craig Topper7fc6d342016-12-11 22:32:38 +00001314 return ConstantAggregateZero::get(II->getType());
Craig Topper1a8a3372016-12-29 03:30:17 +00001315 }
Craig Topper7fc6d342016-12-11 22:32:38 +00001316
Craig Topperac75bca2016-12-13 07:45:45 +00001317 // Only the lower element is used.
1318 DemandedElts = 1;
Craig Topper7fc6d342016-12-11 22:32:38 +00001319 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1320 UndefElts, Depth + 1);
1321 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperac75bca2016-12-13 07:45:45 +00001322
1323 // Only the lower element is undefined. The high elements are zero.
1324 UndefElts = UndefElts[0];
Craig Topper7fc6d342016-12-11 22:32:38 +00001325 break;
1326
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001327 // Unary scalar-as-vector operations that work column-wise.
Simon Pilgrim83020942016-04-24 18:23:14 +00001328 case Intrinsic::x86_sse_rcp_ss:
1329 case Intrinsic::x86_sse_rsqrt_ss:
1330 case Intrinsic::x86_sse_sqrt_ss:
1331 case Intrinsic::x86_sse2_sqrt_sd:
Simon Pilgrim83020942016-04-24 18:23:14 +00001332 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1333 UndefElts, Depth + 1);
1334 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1335
1336 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001337 if (!DemandedElts[0]) {
1338 Worklist.Add(II);
Simon Pilgrim83020942016-04-24 18:23:14 +00001339 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001340 }
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001341 // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions
1342 // checks).
Simon Pilgrim83020942016-04-24 18:23:14 +00001343 break;
1344
Craig Toppera0372de2016-12-14 03:17:27 +00001345 // Binary scalar-as-vector operations that work column-wise. The high
1346 // elements come from operand 0. The low element is a function of both
1347 // operands.
Chris Lattner7e044912010-01-04 07:17:19 +00001348 case Intrinsic::x86_sse_min_ss:
1349 case Intrinsic::x86_sse_max_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001350 case Intrinsic::x86_sse_cmp_ss:
Chris Lattner7e044912010-01-04 07:17:19 +00001351 case Intrinsic::x86_sse2_min_sd:
1352 case Intrinsic::x86_sse2_max_sd:
Craig Toppera0372de2016-12-14 03:17:27 +00001353 case Intrinsic::x86_sse2_cmp_sd: {
1354 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1355 UndefElts, Depth + 1);
1356 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1357
1358 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001359 if (!DemandedElts[0]) {
1360 Worklist.Add(II);
Craig Toppera0372de2016-12-14 03:17:27 +00001361 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001362 }
Craig Toppera0372de2016-12-14 03:17:27 +00001363
1364 // Only lower element is used for operand 1.
1365 DemandedElts = 1;
1366 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1367 UndefElts2, Depth + 1);
1368 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1369
1370 // Lower element is undefined if both lower elements are undefined.
1371 // Consider things like undef&0. The result is known zero, not undef.
1372 if (!UndefElts2[0])
1373 UndefElts.clearBit(0);
1374
1375 break;
1376 }
1377
Craig Toppereb6a20e2016-12-14 03:17:30 +00001378 // Binary scalar-as-vector operations that work column-wise. The high
1379 // elements come from operand 0 and the low element comes from operand 1.
Simon Pilgrim83020942016-04-24 18:23:14 +00001380 case Intrinsic::x86_sse41_round_ss:
Craig Toppereb6a20e2016-12-14 03:17:30 +00001381 case Intrinsic::x86_sse41_round_sd: {
1382 // Don't use the low element of operand 0.
1383 APInt DemandedElts2 = DemandedElts;
1384 DemandedElts2.clearBit(0);
1385 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001386 UndefElts, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001387 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001388
1389 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001390 if (!DemandedElts[0]) {
1391 Worklist.Add(II);
Craig Toppereb6a20e2016-12-14 03:17:30 +00001392 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001393 }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001394
1395 // Only lower element is used for operand 1.
1396 DemandedElts = 1;
Gabor Greife23efee2010-06-28 16:45:00 +00001397 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001398 UndefElts2, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001399 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001400
Craig Toppereb6a20e2016-12-14 03:17:30 +00001401 // Take the high undef elements from operand 0 and take the lower element
1402 // from operand 1.
1403 UndefElts.clearBit(0);
1404 UndefElts |= UndefElts2[0];
Chris Lattner7e044912010-01-04 07:17:19 +00001405 break;
Craig Toppereb6a20e2016-12-14 03:17:30 +00001406 }
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001407
Craig Topperdfd268d2016-12-14 05:43:05 +00001408 // Three input scalar-as-vector operations that work column-wise. The high
1409 // elements come from operand 0 and the low element is a function of all
1410 // three inputs.
Craig Topper268b3ab2016-12-14 06:06:58 +00001411 case Intrinsic::x86_avx512_mask_add_ss_round:
1412 case Intrinsic::x86_avx512_mask_div_ss_round:
1413 case Intrinsic::x86_avx512_mask_mul_ss_round:
1414 case Intrinsic::x86_avx512_mask_sub_ss_round:
1415 case Intrinsic::x86_avx512_mask_max_ss_round:
1416 case Intrinsic::x86_avx512_mask_min_ss_round:
1417 case Intrinsic::x86_avx512_mask_add_sd_round:
1418 case Intrinsic::x86_avx512_mask_div_sd_round:
1419 case Intrinsic::x86_avx512_mask_mul_sd_round:
1420 case Intrinsic::x86_avx512_mask_sub_sd_round:
1421 case Intrinsic::x86_avx512_mask_max_sd_round:
1422 case Intrinsic::x86_avx512_mask_min_sd_round:
Craig Topper23ebd952016-12-11 08:54:52 +00001423 case Intrinsic::x86_fma_vfmadd_ss:
1424 case Intrinsic::x86_fma_vfmsub_ss:
1425 case Intrinsic::x86_fma_vfnmadd_ss:
1426 case Intrinsic::x86_fma_vfnmsub_ss:
1427 case Intrinsic::x86_fma_vfmadd_sd:
1428 case Intrinsic::x86_fma_vfmsub_sd:
1429 case Intrinsic::x86_fma_vfnmadd_sd:
1430 case Intrinsic::x86_fma_vfnmsub_sd:
Craig Topperab5f3552016-12-15 03:49:45 +00001431 case Intrinsic::x86_avx512_mask_vfmadd_ss:
1432 case Intrinsic::x86_avx512_mask_vfmadd_sd:
1433 case Intrinsic::x86_avx512_maskz_vfmadd_ss:
1434 case Intrinsic::x86_avx512_maskz_vfmadd_sd:
Craig Topper23ebd952016-12-11 08:54:52 +00001435 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1436 UndefElts, Depth + 1);
1437 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperdfd268d2016-12-14 05:43:05 +00001438
1439 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001440 if (!DemandedElts[0]) {
1441 Worklist.Add(II);
Craig Topperdfd268d2016-12-14 05:43:05 +00001442 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001443 }
Craig Topperdfd268d2016-12-14 05:43:05 +00001444
1445 // Only lower element is used for operand 1 and 2.
1446 DemandedElts = 1;
Craig Topper23ebd952016-12-11 08:54:52 +00001447 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1448 UndefElts2, Depth + 1);
1449 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1450 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1451 UndefElts3, Depth + 1);
1452 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1453
Craig Topperdfd268d2016-12-14 05:43:05 +00001454 // Lower element is undefined if all three lower elements are undefined.
1455 // Consider things like undef&0. The result is known zero, not undef.
1456 if (!UndefElts2[0] || !UndefElts3[0])
1457 UndefElts.clearBit(0);
Craig Topper23ebd952016-12-11 08:54:52 +00001458
Craig Topper23ebd952016-12-11 08:54:52 +00001459 break;
1460
Craig Topperab5f3552016-12-15 03:49:45 +00001461 case Intrinsic::x86_avx512_mask3_vfmadd_ss:
1462 case Intrinsic::x86_avx512_mask3_vfmadd_sd:
1463 case Intrinsic::x86_avx512_mask3_vfmsub_ss:
1464 case Intrinsic::x86_avx512_mask3_vfmsub_sd:
1465 case Intrinsic::x86_avx512_mask3_vfnmsub_ss:
1466 case Intrinsic::x86_avx512_mask3_vfnmsub_sd:
1467 // These intrinsics get the passthru bits from operand 2.
1468 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1469 UndefElts, Depth + 1);
1470 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1471
1472 // If lowest element of a scalar op isn't used then use Arg2.
Craig Topper1a8a3372016-12-29 03:30:17 +00001473 if (!DemandedElts[0]) {
1474 Worklist.Add(II);
Craig Topperab5f3552016-12-15 03:49:45 +00001475 return II->getArgOperand(2);
Craig Topper1a8a3372016-12-29 03:30:17 +00001476 }
Craig Topperab5f3552016-12-15 03:49:45 +00001477
1478 // Only lower element is used for operand 0 and 1.
1479 DemandedElts = 1;
1480 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1481 UndefElts2, Depth + 1);
1482 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1483 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1484 UndefElts3, Depth + 1);
1485 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1486
1487 // Lower element is undefined if all three lower elements are undefined.
1488 // Consider things like undef&0. The result is known zero, not undef.
1489 if (!UndefElts2[0] || !UndefElts3[0])
1490 UndefElts.clearBit(0);
1491
1492 break;
1493
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001494 case Intrinsic::x86_sse2_pmulu_dq:
1495 case Intrinsic::x86_sse41_pmuldq:
1496 case Intrinsic::x86_avx2_pmul_dq:
Craig Topper72f2d4e2016-12-27 05:30:09 +00001497 case Intrinsic::x86_avx2_pmulu_dq:
1498 case Intrinsic::x86_avx512_pmul_dq_512:
1499 case Intrinsic::x86_avx512_pmulu_dq_512: {
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001500 Value *Op0 = II->getArgOperand(0);
1501 Value *Op1 = II->getArgOperand(1);
1502 unsigned InnerVWidth = Op0->getType()->getVectorNumElements();
1503 assert((VWidth * 2) == InnerVWidth && "Unexpected input size");
1504
1505 APInt InnerDemandedElts(InnerVWidth, 0);
1506 for (unsigned i = 0; i != VWidth; ++i)
1507 if (DemandedElts[i])
1508 InnerDemandedElts.setBit(i * 2);
1509
1510 UndefElts2 = APInt(InnerVWidth, 0);
1511 TmpV = SimplifyDemandedVectorElts(Op0, InnerDemandedElts, UndefElts2,
1512 Depth + 1);
1513 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1514
1515 UndefElts3 = APInt(InnerVWidth, 0);
1516 TmpV = SimplifyDemandedVectorElts(Op1, InnerDemandedElts, UndefElts3,
1517 Depth + 1);
1518 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1519
1520 break;
1521 }
1522
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001523 case Intrinsic::x86_sse2_packssdw_128:
1524 case Intrinsic::x86_sse2_packsswb_128:
1525 case Intrinsic::x86_sse2_packuswb_128:
1526 case Intrinsic::x86_sse41_packusdw:
1527 case Intrinsic::x86_avx2_packssdw:
1528 case Intrinsic::x86_avx2_packsswb:
1529 case Intrinsic::x86_avx2_packusdw:
Craig Topper3731f4d2017-02-16 07:35:23 +00001530 case Intrinsic::x86_avx2_packuswb:
1531 case Intrinsic::x86_avx512_packssdw_512:
1532 case Intrinsic::x86_avx512_packsswb_512:
1533 case Intrinsic::x86_avx512_packusdw_512:
1534 case Intrinsic::x86_avx512_packuswb_512: {
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001535 auto *Ty0 = II->getArgOperand(0)->getType();
1536 unsigned InnerVWidth = Ty0->getVectorNumElements();
1537 assert(VWidth == (InnerVWidth * 2) && "Unexpected input size");
1538
1539 unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128;
1540 unsigned VWidthPerLane = VWidth / NumLanes;
1541 unsigned InnerVWidthPerLane = InnerVWidth / NumLanes;
1542
1543 // Per lane, pack the elements of the first input and then the second.
1544 // e.g.
1545 // v8i16 PACK(v4i32 X, v4i32 Y) - (X[0..3],Y[0..3])
1546 // v32i8 PACK(v16i16 X, v16i16 Y) - (X[0..7],Y[0..7]),(X[8..15],Y[8..15])
1547 for (int OpNum = 0; OpNum != 2; ++OpNum) {
1548 APInt OpDemandedElts(InnerVWidth, 0);
1549 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1550 unsigned LaneIdx = Lane * VWidthPerLane;
1551 for (unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) {
1552 unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum;
1553 if (DemandedElts[Idx])
1554 OpDemandedElts.setBit((Lane * InnerVWidthPerLane) + Elt);
1555 }
1556 }
1557
1558 // Demand elements from the operand.
1559 auto *Op = II->getArgOperand(OpNum);
1560 APInt OpUndefElts(InnerVWidth, 0);
1561 TmpV = SimplifyDemandedVectorElts(Op, OpDemandedElts, OpUndefElts,
1562 Depth + 1);
1563 if (TmpV) {
1564 II->setArgOperand(OpNum, TmpV);
1565 MadeChange = true;
1566 }
1567
1568 // Pack the operand's UNDEF elements, one lane at a time.
1569 OpUndefElts = OpUndefElts.zext(VWidth);
1570 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1571 APInt LaneElts = OpUndefElts.lshr(InnerVWidthPerLane * Lane);
1572 LaneElts = LaneElts.getLoBits(InnerVWidthPerLane);
1573 LaneElts = LaneElts.shl(InnerVWidthPerLane * (2 * Lane + OpNum));
1574 UndefElts |= LaneElts;
1575 }
1576 }
1577 break;
1578 }
1579
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001580 // PSHUFB
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001581 case Intrinsic::x86_ssse3_pshuf_b_128:
1582 case Intrinsic::x86_avx2_pshuf_b:
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001583 case Intrinsic::x86_avx512_pshuf_b_512:
1584 // PERMILVAR
1585 case Intrinsic::x86_avx_vpermilvar_ps:
1586 case Intrinsic::x86_avx_vpermilvar_ps_256:
1587 case Intrinsic::x86_avx512_vpermilvar_ps_512:
1588 case Intrinsic::x86_avx_vpermilvar_pd:
1589 case Intrinsic::x86_avx_vpermilvar_pd_256:
Simon Pilgrimfe2c0ed2017-01-18 14:47:49 +00001590 case Intrinsic::x86_avx512_vpermilvar_pd_512:
1591 // PERMV
1592 case Intrinsic::x86_avx2_permd:
1593 case Intrinsic::x86_avx2_permps: {
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001594 Value *Op1 = II->getArgOperand(1);
1595 TmpV = SimplifyDemandedVectorElts(Op1, DemandedElts, UndefElts,
1596 Depth + 1);
1597 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1598 break;
1599 }
1600
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001601 // SSE4A instructions leave the upper 64-bits of the 128-bit result
1602 // in an undefined state.
1603 case Intrinsic::x86_sse4a_extrq:
1604 case Intrinsic::x86_sse4a_extrqi:
1605 case Intrinsic::x86_sse4a_insertq:
1606 case Intrinsic::x86_sse4a_insertqi:
Craig Topper3a86a042017-03-19 05:49:16 +00001607 UndefElts.setHighBits(VWidth / 2);
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001608 break;
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001609 case Intrinsic::amdgcn_buffer_load:
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001610 case Intrinsic::amdgcn_buffer_load_format:
1611 case Intrinsic::amdgcn_image_sample:
1612 case Intrinsic::amdgcn_image_sample_cl:
1613 case Intrinsic::amdgcn_image_sample_d:
1614 case Intrinsic::amdgcn_image_sample_d_cl:
1615 case Intrinsic::amdgcn_image_sample_l:
1616 case Intrinsic::amdgcn_image_sample_b:
1617 case Intrinsic::amdgcn_image_sample_b_cl:
1618 case Intrinsic::amdgcn_image_sample_lz:
1619 case Intrinsic::amdgcn_image_sample_cd:
1620 case Intrinsic::amdgcn_image_sample_cd_cl:
1621
1622 case Intrinsic::amdgcn_image_sample_c:
1623 case Intrinsic::amdgcn_image_sample_c_cl:
1624 case Intrinsic::amdgcn_image_sample_c_d:
1625 case Intrinsic::amdgcn_image_sample_c_d_cl:
1626 case Intrinsic::amdgcn_image_sample_c_l:
1627 case Intrinsic::amdgcn_image_sample_c_b:
1628 case Intrinsic::amdgcn_image_sample_c_b_cl:
1629 case Intrinsic::amdgcn_image_sample_c_lz:
1630 case Intrinsic::amdgcn_image_sample_c_cd:
1631 case Intrinsic::amdgcn_image_sample_c_cd_cl:
1632
1633 case Intrinsic::amdgcn_image_sample_o:
1634 case Intrinsic::amdgcn_image_sample_cl_o:
1635 case Intrinsic::amdgcn_image_sample_d_o:
1636 case Intrinsic::amdgcn_image_sample_d_cl_o:
1637 case Intrinsic::amdgcn_image_sample_l_o:
1638 case Intrinsic::amdgcn_image_sample_b_o:
1639 case Intrinsic::amdgcn_image_sample_b_cl_o:
1640 case Intrinsic::amdgcn_image_sample_lz_o:
1641 case Intrinsic::amdgcn_image_sample_cd_o:
1642 case Intrinsic::amdgcn_image_sample_cd_cl_o:
1643
1644 case Intrinsic::amdgcn_image_sample_c_o:
1645 case Intrinsic::amdgcn_image_sample_c_cl_o:
1646 case Intrinsic::amdgcn_image_sample_c_d_o:
1647 case Intrinsic::amdgcn_image_sample_c_d_cl_o:
1648 case Intrinsic::amdgcn_image_sample_c_l_o:
1649 case Intrinsic::amdgcn_image_sample_c_b_o:
1650 case Intrinsic::amdgcn_image_sample_c_b_cl_o:
1651 case Intrinsic::amdgcn_image_sample_c_lz_o:
1652 case Intrinsic::amdgcn_image_sample_c_cd_o:
1653 case Intrinsic::amdgcn_image_sample_c_cd_cl_o:
1654
1655 case Intrinsic::amdgcn_image_getlod: {
Craig Topperd33ee1b2017-04-03 16:34:59 +00001656 if (VWidth == 1 || !DemandedElts.isMask())
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001657 return nullptr;
1658
1659 // TODO: Handle 3 vectors when supported in code gen.
1660 unsigned NewNumElts = PowerOf2Ceil(DemandedElts.countTrailingOnes());
1661 if (NewNumElts == VWidth)
1662 return nullptr;
1663
1664 Module *M = II->getParent()->getParent()->getParent();
1665 Type *EltTy = V->getType()->getVectorElementType();
1666
1667 Type *NewTy = (NewNumElts == 1) ? EltTy :
1668 VectorType::get(EltTy, NewNumElts);
1669
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001670 auto IID = II->getIntrinsicID();
1671
1672 bool IsBuffer = IID == Intrinsic::amdgcn_buffer_load ||
1673 IID == Intrinsic::amdgcn_buffer_load_format;
1674
1675 Function *NewIntrin = IsBuffer ?
1676 Intrinsic::getDeclaration(M, IID, NewTy) :
1677 // Samplers have 3 mangled types.
1678 Intrinsic::getDeclaration(M, IID,
1679 { NewTy, II->getArgOperand(0)->getType(),
1680 II->getArgOperand(1)->getType()});
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001681
1682 SmallVector<Value *, 5> Args;
1683 for (unsigned I = 0, E = II->getNumArgOperands(); I != E; ++I)
1684 Args.push_back(II->getArgOperand(I));
1685
Matt Arsenaulta3bdd8f2017-03-10 05:25:49 +00001686 IRBuilderBase::InsertPointGuard Guard(*Builder);
1687 Builder->SetInsertPoint(II);
1688
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001689 CallInst *NewCall = Builder->CreateCall(NewIntrin, Args);
1690 NewCall->takeName(II);
1691 NewCall->copyMetadata(*II);
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001692
1693 if (!IsBuffer) {
1694 ConstantInt *DMask = dyn_cast<ConstantInt>(NewCall->getArgOperand(3));
1695 if (DMask) {
1696 unsigned DMaskVal = DMask->getZExtValue() & 0xf;
1697
1698 unsigned PopCnt = 0;
1699 unsigned NewDMask = 0;
1700 for (unsigned I = 0; I < 4; ++I) {
1701 const unsigned Bit = 1 << I;
1702 if (!!(DMaskVal & Bit)) {
1703 if (++PopCnt > NewNumElts)
1704 break;
1705
1706 NewDMask |= Bit;
1707 }
1708 }
1709
1710 NewCall->setArgOperand(3, ConstantInt::get(DMask->getType(), NewDMask));
1711 }
1712 }
1713
1714
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001715 if (NewNumElts == 1) {
1716 return Builder->CreateInsertElement(UndefValue::get(V->getType()),
1717 NewCall, static_cast<uint64_t>(0));
1718 }
1719
1720 SmallVector<uint32_t, 8> EltMask;
1721 for (unsigned I = 0; I < VWidth; ++I)
1722 EltMask.push_back(I);
1723
1724 Value *Shuffle = Builder->CreateShuffleVector(
1725 NewCall, UndefValue::get(NewTy), EltMask);
1726
1727 MadeChange = true;
1728 return Shuffle;
1729 }
Chris Lattner7e044912010-01-04 07:17:19 +00001730 }
1731 break;
1732 }
1733 }
Craig Topperf40110f2014-04-25 05:29:35 +00001734 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001735}