blob: 5222cd498f369d1d4e899528e58aa8ebde38dfb5 [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,
Craig Topper358cd9a2017-04-20 23:58:27 +000029 const APInt &Demanded) {
Chris Lattner7e044912010-01-04 07:17:19 +000030 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.
Craig Toppera8129a12017-04-20 16:17:13 +000040 if (C->isSubsetOf(Demanded))
Chris Lattner7e044912010-01-04 07:17:19 +000041 return false;
42
43 // This instruction is producing bits that are not demanded. Shrink the RHS.
Craig Topper358cd9a2017-04-20 23:58:27 +000044 I->setOperand(OpNo, ConstantInt::get(Op->getType(), *C & Demanded));
David Majnemer42b83a52014-08-22 07:56:32 +000045
Chris Lattner7e044912010-01-04 07:17:19 +000046 return true;
47}
48
49
50
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000051/// Inst is an integer instruction that SimplifyDemandedBits knows about. See if
52/// the instruction has any properties that allow us to simplify its operands.
Chris Lattner7e044912010-01-04 07:17:19 +000053bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
54 unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
55 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
56 APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
Craig Topper4c947752012-12-22 18:09:02 +000057
Mehdi Aminia28d91d2015-03-10 02:37:25 +000058 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, KnownZero, KnownOne,
59 0, &Inst);
Craig Topperf40110f2014-04-25 05:29:35 +000060 if (!V) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000061 if (V == &Inst) return true;
Sanjay Patel4b198802016-02-01 22:23:39 +000062 replaceInstUsesWith(Inst, V);
Chris Lattner7e044912010-01-04 07:17:19 +000063 return true;
64}
65
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000066/// This form of SimplifyDemandedBits simplifies the specified instruction
67/// operand if possible, updating it in place. It returns true if it made any
68/// change and false otherwise.
Craig Topper47596dd2017-03-25 06:52:52 +000069bool InstCombiner::SimplifyDemandedBits(Instruction *I, unsigned OpNo,
70 const APInt &DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +000071 APInt &KnownZero, APInt &KnownOne,
72 unsigned Depth) {
Craig Topper47596dd2017-03-25 06:52:52 +000073 Use &U = I->getOperandUse(OpNo);
David Majnemerfe58d132015-04-22 20:59:28 +000074 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, KnownZero,
Craig Topper47596dd2017-03-25 06:52:52 +000075 KnownOne, Depth, I);
Craig Topperf40110f2014-04-25 05:29:35 +000076 if (!NewVal) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000077 U = NewVal;
78 return true;
79}
80
81
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000082/// This function attempts to replace V with a simpler value based on the
83/// demanded bits. When this function is called, it is known that only the bits
84/// set in DemandedMask of the result of V are ever used downstream.
85/// Consequently, depending on the mask and V, it may be possible to replace V
86/// with a constant or one of its operands. In such cases, this function does
87/// the replacement and returns true. In all other cases, it returns false after
88/// analyzing the expression and setting KnownOne and known to be one in the
89/// expression. KnownZero contains all the bits that are known to be zero in the
90/// expression. These are provided to potentially allow the caller (which might
91/// recursively be SimplifyDemandedBits itself) to simplify the expression.
92/// KnownOne and KnownZero always follow the invariant that:
93/// KnownOne & KnownZero == 0.
94/// That is, a bit can't be both 1 and 0. Note that the bits in KnownOne and
95/// KnownZero may only be accurate for those bits set in DemandedMask. Note also
96/// that the bitwidth of V, DemandedMask, KnownZero and KnownOne must all be the
97/// same.
Chris Lattner7e044912010-01-04 07:17:19 +000098///
99/// This returns null if it did not change anything and it permits no
100/// simplification. This returns V itself if it did some simplification of V's
101/// operands based on the information about what bits are demanded. This returns
102/// some other non-null value if it found out that V is equal to another value
103/// in the context where the specified bits are demanded, but not for all users.
104Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
105 APInt &KnownZero, APInt &KnownOne,
Hal Finkel60db0582014-09-07 18:57:58 +0000106 unsigned Depth,
107 Instruction *CxtI) {
Craig Toppere73658d2014-04-28 04:05:08 +0000108 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000109 assert(Depth <= 6 && "Limit Search Depth");
110 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000111 Type *VTy = V->getType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000112 assert(
113 (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
114 KnownZero.getBitWidth() == BitWidth &&
115 KnownOne.getBitWidth() == BitWidth &&
116 "Value *V, DemandedMask, KnownZero and KnownOne "
117 "must have same BitWidth");
Craig Topper83dc1c62017-04-20 16:14:58 +0000118
119 if (isa<Constant>(V)) {
120 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000121 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000122 }
123
Jay Foad25a5e4c2010-12-01 08:53:58 +0000124 KnownZero.clearAllBits();
125 KnownOne.clearAllBits();
Craig Topper83dc1c62017-04-20 16:14:58 +0000126 if (DemandedMask == 0) // Not demanding any bits from V.
Chris Lattner7e044912010-01-04 07:17:19 +0000127 return UndefValue::get(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000128
Chris Lattner7e044912010-01-04 07:17:19 +0000129 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000130 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000131
Chris Lattner7e044912010-01-04 07:17:19 +0000132 Instruction *I = dyn_cast<Instruction>(V);
133 if (!I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000134 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000135 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000136 }
137
138 // If there are multiple uses of this value and we aren't at the root, then
139 // we can't do any simplifications of the operands, because DemandedMask
140 // only reflects the bits demanded by *one* of the users.
Craig Topper7603dce2017-04-25 16:48:19 +0000141 if (Depth != 0 && !I->hasOneUse())
Craig Topperb0076fe2017-04-12 18:05:21 +0000142 return SimplifyMultipleUseDemandedBits(I, DemandedMask, KnownZero, KnownOne,
143 Depth, CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000144
Craig Topperb0076fe2017-04-12 18:05:21 +0000145 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
146 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
147
Chris Lattner7e044912010-01-04 07:17:19 +0000148 // If this is the root being simplified, allow it to have multiple uses,
149 // just set the DemandedMask to all bits so that we can try to simplify the
150 // operands. This allows visitTruncInst (for example) to simplify the
151 // operand of a trunc without duplicating all the logic below.
152 if (Depth == 0 && !V->hasOneUse())
Craig Toppere06b6bc2017-04-04 05:03:02 +0000153 DemandedMask.setAllBits();
Craig Topper4c947752012-12-22 18:09:02 +0000154
Chris Lattner7e044912010-01-04 07:17:19 +0000155 switch (I->getOpcode()) {
156 default:
Hal Finkel60db0582014-09-07 18:57:58 +0000157 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000158 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000159 case Instruction::And: {
Chris Lattner7e044912010-01-04 07:17:19 +0000160 // If either the LHS or the RHS are Zero, the result is zero.
Craig Topper47596dd2017-03-25 06:52:52 +0000161 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
162 Depth + 1) ||
163 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnownZero, LHSKnownZero,
164 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000165 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000166 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
167 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000168
Craig Topper9a458cd2017-04-14 22:34:14 +0000169 // Output known-0 are known to be clear if zero in either the LHS | RHS.
170 APInt IKnownZero = RHSKnownZero | LHSKnownZero;
171 // Output known-1 bits are only known if set in both the LHS & RHS.
172 APInt IKnownOne = RHSKnownOne & LHSKnownOne;
173
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000174 // If the client is only demanding bits that we know, return the known
175 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000176 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000177 return Constant::getIntegerValue(VTy, IKnownOne);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000178
Chris Lattner7e044912010-01-04 07:17:19 +0000179 // If all of the demanded bits are known 1 on one side, return the other.
180 // These bits cannot contribute to the result of the 'and'.
Craig Topper17f37ba2017-04-20 20:47:35 +0000181 if (DemandedMask.isSubsetOf(LHSKnownZero | RHSKnownOne))
Chris Lattner7e044912010-01-04 07:17:19 +0000182 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000183 if (DemandedMask.isSubsetOf(RHSKnownZero | LHSKnownOne))
Chris Lattner7e044912010-01-04 07:17:19 +0000184 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000185
Chris Lattner7e044912010-01-04 07:17:19 +0000186 // If the RHS is a constant, see if we can simplify it.
187 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
188 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000189
Craig Topper9a458cd2017-04-14 22:34:14 +0000190 KnownZero = std::move(IKnownZero);
191 KnownOne = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000192 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000193 }
194 case Instruction::Or: {
Chris Lattner7e044912010-01-04 07:17:19 +0000195 // If either the LHS or the RHS are One, the result is One.
Craig Topper47596dd2017-03-25 06:52:52 +0000196 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
197 Depth + 1) ||
198 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnownOne, LHSKnownZero,
199 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000200 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000201 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
202 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
203
Craig Topper9a458cd2017-04-14 22:34:14 +0000204 // Output known-0 bits are only known if clear in both the LHS & RHS.
205 APInt IKnownZero = RHSKnownZero & LHSKnownZero;
206 // Output known-1 are known to be set if set in either the LHS | RHS.
207 APInt IKnownOne = RHSKnownOne | LHSKnownOne;
208
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000209 // If the client is only demanding bits that we know, return the known
210 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000211 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000212 return Constant::getIntegerValue(VTy, IKnownOne);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000213
Chris Lattner7e044912010-01-04 07:17:19 +0000214 // If all of the demanded bits are known zero on one side, return the other.
215 // These bits cannot contribute to the result of the 'or'.
Craig Topper17f37ba2017-04-20 20:47:35 +0000216 if (DemandedMask.isSubsetOf(LHSKnownOne | RHSKnownZero))
Chris Lattner7e044912010-01-04 07:17:19 +0000217 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000218 if (DemandedMask.isSubsetOf(RHSKnownOne | LHSKnownZero))
Chris Lattner7e044912010-01-04 07:17:19 +0000219 return I->getOperand(1);
220
Chris Lattner7e044912010-01-04 07:17:19 +0000221 // If the RHS is a constant, see if we can simplify it.
222 if (ShrinkDemandedConstant(I, 1, DemandedMask))
223 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000224
Craig Topper9a458cd2017-04-14 22:34:14 +0000225 KnownZero = std::move(IKnownZero);
226 KnownOne = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000227 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000228 }
Chris Lattner7e044912010-01-04 07:17:19 +0000229 case Instruction::Xor: {
Craig Topper47596dd2017-03-25 06:52:52 +0000230 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
231 Depth + 1) ||
232 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnownZero, LHSKnownOne,
233 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000234 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000235 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
236 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
237
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000238 // Output known-0 bits are known if clear or set in both the LHS & RHS.
239 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
240 (RHSKnownOne & LHSKnownOne);
241 // Output known-1 are known to be set if set in only one of the LHS, RHS.
242 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
243 (RHSKnownOne & LHSKnownZero);
244
245 // If the client is only demanding bits that we know, return the known
246 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000247 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000248 return Constant::getIntegerValue(VTy, IKnownOne);
249
Chris Lattner7e044912010-01-04 07:17:19 +0000250 // If all of the demanded bits are known zero on one side, return the other.
251 // These bits cannot contribute to the result of the 'xor'.
Craig Topper17f37ba2017-04-20 20:47:35 +0000252 if (DemandedMask.isSubsetOf(RHSKnownZero))
Chris Lattner7e044912010-01-04 07:17:19 +0000253 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000254 if (DemandedMask.isSubsetOf(LHSKnownZero))
Chris Lattner7e044912010-01-04 07:17:19 +0000255 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000256
Chris Lattner7e044912010-01-04 07:17:19 +0000257 // If all of the demanded bits are known to be zero on one side or the
258 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000259 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Craig Topper17f37ba2017-04-20 20:47:35 +0000260 if (DemandedMask.isSubsetOf(RHSKnownZero | LHSKnownZero)) {
Craig Topper4c947752012-12-22 18:09:02 +0000261 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000262 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
263 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000264 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000265 }
Craig Topper4c947752012-12-22 18:09:02 +0000266
Chris Lattner7e044912010-01-04 07:17:19 +0000267 // If all of the demanded bits on one side are known, and all of the set
268 // bits on that side are also known to be set on the other side, turn this
269 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000270 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper17f37ba2017-04-20 20:47:35 +0000271 if (DemandedMask.isSubsetOf(RHSKnownZero|RHSKnownOne) &&
272 RHSKnownOne.isSubsetOf(LHSKnownOne)) {
273 Constant *AndC = Constant::getIntegerValue(VTy,
274 ~RHSKnownOne & DemandedMask);
275 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
276 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000277 }
Craig Topper4c947752012-12-22 18:09:02 +0000278
Sanjay Patel8ce1d4c2017-04-21 20:29:17 +0000279 // If the RHS is a constant, see if we can simplify it.
280 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
281 if (ShrinkDemandedConstant(I, 1, DemandedMask))
282 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000283
Chris Lattner7e044912010-01-04 07:17:19 +0000284 // If our LHS is an 'and' and if it has one use, and if any of the bits we
285 // are flipping are known to be set, then the xor is just resetting those
286 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
287 // simplifying both of them.
288 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
289 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
290 isa<ConstantInt>(I->getOperand(1)) &&
291 isa<ConstantInt>(LHSInst->getOperand(1)) &&
292 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
293 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
294 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
295 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000296
Chris Lattner7e044912010-01-04 07:17:19 +0000297 Constant *AndC =
298 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000299 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000300 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000301
Chris Lattner7e044912010-01-04 07:17:19 +0000302 Constant *XorC =
303 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000304 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000305 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000306 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000307
308 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topper9a458cd2017-04-14 22:34:14 +0000309 KnownZero = std::move(IKnownZero);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000310 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topper9a458cd2017-04-14 22:34:14 +0000311 KnownOne = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000312 break;
313 }
314 case Instruction::Select:
James Molloy2b21a7c2015-05-20 18:41:25 +0000315 // If this is a select as part of a min/max pattern, don't simplify any
316 // further in case we break the structure.
317 Value *LHS, *RHS;
James Molloy134bec22015-08-11 09:12:57 +0000318 if (matchSelectPattern(I, LHS, RHS).Flavor != SPF_UNKNOWN)
James Molloy2b21a7c2015-05-20 18:41:25 +0000319 return nullptr;
Simon Pilgrim61116dd2015-09-17 20:32:45 +0000320
Craig Topper47596dd2017-03-25 06:52:52 +0000321 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnownZero, RHSKnownOne,
322 Depth + 1) ||
323 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnownZero, LHSKnownOne,
324 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000325 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000326 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
327 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
328
Chris Lattner7e044912010-01-04 07:17:19 +0000329 // If the operands are constants, see if we can simplify them.
330 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
331 ShrinkDemandedConstant(I, 2, DemandedMask))
332 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000333
Chris Lattner7e044912010-01-04 07:17:19 +0000334 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000335 KnownOne = RHSKnownOne & LHSKnownOne;
336 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000337 break;
338 case Instruction::Trunc: {
339 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000340 DemandedMask = DemandedMask.zext(truncBf);
341 KnownZero = KnownZero.zext(truncBf);
342 KnownOne = KnownOne.zext(truncBf);
Craig Topper47596dd2017-03-25 06:52:52 +0000343 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
344 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000345 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000346 DemandedMask = DemandedMask.trunc(BitWidth);
347 KnownZero = KnownZero.trunc(BitWidth);
348 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000349 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000350 break;
351 }
352 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000353 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000354 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000355
Chris Lattner229907c2011-07-18 04:54:35 +0000356 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
357 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000358 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
359 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
360 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000361 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000362 } else
363 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000364 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000365 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000366 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000367 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000368
Craig Topper47596dd2017-03-25 06:52:52 +0000369 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
370 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000371 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000372 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000373 break;
374 case Instruction::ZExt: {
375 // Compute the bits in the result that are not present in the input.
376 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000377
Jay Foad583abbc2010-12-07 08:25:19 +0000378 DemandedMask = DemandedMask.trunc(SrcBitWidth);
379 KnownZero = KnownZero.trunc(SrcBitWidth);
380 KnownOne = KnownOne.trunc(SrcBitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000381 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
382 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000383 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000384 DemandedMask = DemandedMask.zext(BitWidth);
385 KnownZero = KnownZero.zext(BitWidth);
386 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000387 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000388 // The top bits are known to be zero.
Craig Topper3a86a042017-03-19 05:49:16 +0000389 KnownZero.setBitsFrom(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000390 break;
391 }
392 case Instruction::SExt: {
393 // Compute the bits in the result that are not present in the input.
394 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000395
396 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000397 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
398
Craig Topper3a86a042017-03-19 05:49:16 +0000399 APInt NewBits(APInt::getBitsSetFrom(BitWidth, SrcBitWidth));
Chris Lattner7e044912010-01-04 07:17:19 +0000400 // If any of the sign extended bits are demanded, we know that the sign
401 // bit is demanded.
402 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000403 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000404
Jay Foad583abbc2010-12-07 08:25:19 +0000405 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
406 KnownZero = KnownZero.trunc(SrcBitWidth);
407 KnownOne = KnownOne.trunc(SrcBitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000408 if (SimplifyDemandedBits(I, 0, InputDemandedBits, KnownZero, KnownOne,
409 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000410 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000411 InputDemandedBits = InputDemandedBits.zext(BitWidth);
412 KnownZero = KnownZero.zext(BitWidth);
413 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000414 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
415
Chris Lattner7e044912010-01-04 07:17:19 +0000416 // If the sign bit of the input is known set or clear, then we know the
417 // top bits of the result.
418
419 // If the input sign bit is known zero, or if the NewBits are not demanded
420 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000421 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000422 // Convert to ZExt cast
423 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000424 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000425 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
426 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000427 }
428 break;
429 }
Matthias Braune48484c2015-04-30 22:05:30 +0000430 case Instruction::Add:
431 case Instruction::Sub: {
432 /// If the high-bits of an ADD/SUB are not demanded, then we do not care
433 /// about the high bits of the operands.
Chris Lattner7e044912010-01-04 07:17:19 +0000434 unsigned NLZ = DemandedMask.countLeadingZeros();
Matthias Braune48484c2015-04-30 22:05:30 +0000435 if (NLZ > 0) {
436 // Right fill the mask of bits for this ADD/SUB to demand the most
Chris Lattner7e044912010-01-04 07:17:19 +0000437 // significant bit and all those below it.
Chris Lattner7e044912010-01-04 07:17:19 +0000438 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
Craig Topper07f29152017-03-22 04:03:53 +0000439 if (ShrinkDemandedConstant(I, 0, DemandedFromOps) ||
Craig Topper47596dd2017-03-25 06:52:52 +0000440 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnownZero, LHSKnownOne,
441 Depth + 1) ||
Matthias Braune48484c2015-04-30 22:05:30 +0000442 ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
Craig Topper845033a2017-04-12 16:49:59 +0000443 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnownZero, RHSKnownOne,
Craig Topper47596dd2017-03-25 06:52:52 +0000444 Depth + 1)) {
Matthias Braune48484c2015-04-30 22:05:30 +0000445 // Disable the nsw and nuw flags here: We can no longer guarantee that
446 // we won't wrap after simplification. Removing the nsw/nuw flags is
447 // legal here because the top bit is not demanded.
448 BinaryOperator &BinOP = *cast<BinaryOperator>(I);
449 BinOP.setHasNoSignedWrap(false);
450 BinOP.setHasNoUnsignedWrap(false);
Chris Lattner7e044912010-01-04 07:17:19 +0000451 return I;
David Majnemer7d0e99c2015-04-22 22:42:05 +0000452 }
Craig Topper845033a2017-04-12 16:49:59 +0000453
454 // If we are known to be adding/subtracting zeros to every bit below
455 // the highest demanded bit, we just return the other side.
456 if ((DemandedFromOps & RHSKnownZero) == DemandedFromOps)
457 return I->getOperand(0);
458 // We can't do this with the LHS for subtraction.
459 if (I->getOpcode() == Instruction::Add &&
460 (DemandedFromOps & LHSKnownZero) == DemandedFromOps)
461 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000462 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000463
Craig Topper8fbb74b2017-03-24 22:12:10 +0000464 // Otherwise just hand the add/sub off to computeKnownBits to fill in
465 // the known zeros and ones.
466 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000467 break;
Matthias Braune48484c2015-04-30 22:05:30 +0000468 }
Sanjay Patel3e1ae722017-04-20 21:33:02 +0000469 case Instruction::Shl: {
470 const APInt *SA;
471 if (match(I->getOperand(1), m_APInt(SA))) {
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000472 const APInt *ShrAmt;
473 if (match(I->getOperand(0), m_Shr(m_Value(), m_APInt(ShrAmt)))) {
474 Instruction *Shr = cast<Instruction>(I->getOperand(0));
Sanjay Patelcc663b82017-04-20 22:37:01 +0000475 if (Value *R = simplifyShrShlDemandedBits(
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000476 Shr, *ShrAmt, I, *SA, DemandedMask, KnownZero, KnownOne))
477 return R;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000478 }
479
Chris Lattner768003c2011-02-10 05:09:34 +0000480 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000481 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000482
Chris Lattner768003c2011-02-10 05:09:34 +0000483 // If the shift is NUW/NSW, then it does demand the high bits.
484 ShlOperator *IOp = cast<ShlOperator>(I);
485 if (IOp->hasNoSignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000486 DemandedMaskIn.setHighBits(ShiftAmt+1);
Chris Lattner768003c2011-02-10 05:09:34 +0000487 else if (IOp->hasNoUnsignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000488 DemandedMaskIn.setHighBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000489
Craig Topper47596dd2017-03-25 06:52:52 +0000490 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
491 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000492 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000493 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
494 KnownZero <<= ShiftAmt;
495 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000496 // low bits known zero.
497 if (ShiftAmt)
Craig Topper3a86a042017-03-19 05:49:16 +0000498 KnownZero.setLowBits(ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000499 }
500 break;
Sanjay Patel3e1ae722017-04-20 21:33:02 +0000501 }
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000502 case Instruction::LShr: {
503 const APInt *SA;
504 if (match(I->getOperand(1), m_APInt(SA))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000505 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000506
Chris Lattner7e044912010-01-04 07:17:19 +0000507 // Unsigned shift right.
508 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000509
Chris Lattner768003c2011-02-10 05:09:34 +0000510 // If the shift is exact, then it does demand the low bits (and knows that
511 // they are zero).
512 if (cast<LShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000513 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000514
Craig Topper47596dd2017-03-25 06:52:52 +0000515 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
516 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000517 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000518 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Craig Topperfc947bc2017-04-18 17:14:21 +0000519 KnownZero.lshrInPlace(ShiftAmt);
520 KnownOne.lshrInPlace(ShiftAmt);
Craig Topper3a86a042017-03-19 05:49:16 +0000521 if (ShiftAmt)
522 KnownZero.setHighBits(ShiftAmt); // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000523 }
524 break;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000525 }
526 case Instruction::AShr: {
Chris Lattner7e044912010-01-04 07:17:19 +0000527 // If this is an arithmetic shift right and only the low-bit is set, we can
528 // always convert this into a logical shr, even if the shift amount is
529 // variable. The low bit of the shift cannot be an input sign bit unless
530 // the shift amount is >= the size of the datatype, which is undefined.
531 if (DemandedMask == 1) {
532 // Perform the logical shift right.
533 Instruction *NewVal = BinaryOperator::CreateLShr(
534 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000535 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000536 }
Chris Lattner7e044912010-01-04 07:17:19 +0000537
538 // If the sign bit is the only bit demanded by this ashr, then there is no
539 // need to do it, the shift doesn't change the high bit.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000540 if (DemandedMask.isSignMask())
Chris Lattner7e044912010-01-04 07:17:19 +0000541 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000542
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000543 const APInt *SA;
544 if (match(I->getOperand(1), m_APInt(SA))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000545 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000546
Chris Lattner7e044912010-01-04 07:17:19 +0000547 // Signed shift right.
548 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000549 // If any of the high bits are demanded, we should set the sign bit as
Chris Lattner7e044912010-01-04 07:17:19 +0000550 // demanded.
551 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Craig Topperc9a4fc02017-04-14 05:09:04 +0000552 DemandedMaskIn.setSignBit();
Craig Topper4c947752012-12-22 18:09:02 +0000553
Chris Lattner768003c2011-02-10 05:09:34 +0000554 // If the shift is exact, then it does demand the low bits (and knows that
555 // they are zero).
556 if (cast<AShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000557 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000558
Craig Topper47596dd2017-03-25 06:52:52 +0000559 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
560 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000561 return I;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000562
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000563 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000564 // Compute the new bits that are at the top now.
565 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Craig Topperfc947bc2017-04-18 17:14:21 +0000566 KnownZero.lshrInPlace(ShiftAmt);
567 KnownOne.lshrInPlace(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000568
Chris Lattner7e044912010-01-04 07:17:19 +0000569 // Handle the sign bits.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000570 APInt SignMask(APInt::getSignMask(BitWidth));
Chris Lattner7e044912010-01-04 07:17:19 +0000571 // Adjust to where it is now in the mask.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000572 SignMask.lshrInPlace(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000573
Chris Lattner7e044912010-01-04 07:17:19 +0000574 // If the input sign bit is known to be zero, or if none of the top bits
575 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000576 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Craig Topperff238892017-04-20 21:24:37 +0000577 !DemandedMask.intersects(HighBits)) {
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000578 BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0),
579 I->getOperand(1));
580 LShr->setIsExact(cast<BinaryOperator>(I)->isExact());
581 return InsertNewInstWith(LShr, *I);
Craig Topperff238892017-04-20 21:24:37 +0000582 } else if (KnownOne.intersects(SignMask)) { // New bits are known one.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000583 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000584 }
585 }
586 break;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000587 }
Chris Lattner7e044912010-01-04 07:17:19 +0000588 case Instruction::SRem:
589 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000590 // X % -1 demands all the bits because we don't want to introduce
591 // INT_MIN % -1 (== undef) by accident.
592 if (Rem->isAllOnesValue())
593 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000594 APInt RA = Rem->getValue().abs();
595 if (RA.isPowerOf2()) {
596 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
597 return I->getOperand(0);
598
599 APInt LowBits = RA - 1;
Craig Topperbcfd2d12017-04-20 16:56:25 +0000600 APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000601 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnownZero, LHSKnownOne,
602 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000603 return I;
604
Duncan Sands3a48b872010-01-28 17:22:42 +0000605 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000606 KnownZero = LHSKnownZero & LowBits;
607 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000608
Duncan Sands3a48b872010-01-28 17:22:42 +0000609 // If LHS is non-negative or has all low bits zero, then the upper bits
610 // are all zero.
Craig Topperff238892017-04-20 21:24:37 +0000611 if (LHSKnownZero.isSignBitSet() || LowBits.isSubsetOf(LHSKnownZero))
Duncan Sands3a48b872010-01-28 17:22:42 +0000612 KnownZero |= ~LowBits;
613
614 // If LHS is negative and not all low bits are zero, then the upper bits
615 // are all one.
Craig Topperff238892017-04-20 21:24:37 +0000616 if (LHSKnownOne.isSignBitSet() && LowBits.intersects(LHSKnownOne))
Duncan Sands3a48b872010-01-28 17:22:42 +0000617 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000618
Craig Topper4c947752012-12-22 18:09:02 +0000619 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Craig Topperda886c62017-04-16 21:46:12 +0000620 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000621 }
622 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000623
624 // The sign bit is the LHS's sign bit, except when the result of the
625 // remainder is zero.
Craig Topperd23004c2017-04-17 16:38:20 +0000626 if (DemandedMask.isSignBitSet()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000627 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000628 CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000629 // If it's known zero, our sign bit is also zero.
Craig Topperd23004c2017-04-17 16:38:20 +0000630 if (LHSKnownZero.isSignBitSet())
Craig Topper3a86a042017-03-19 05:49:16 +0000631 KnownZero.setSignBit();
Nick Lewyckye4679792011-03-07 01:50:10 +0000632 }
Chris Lattner7e044912010-01-04 07:17:19 +0000633 break;
634 case Instruction::URem: {
635 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
636 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000637 if (SimplifyDemandedBits(I, 0, AllOnes, KnownZero2, KnownOne2, Depth + 1) ||
638 SimplifyDemandedBits(I, 1, AllOnes, KnownZero2, KnownOne2, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000639 return I;
640
641 unsigned Leaders = KnownZero2.countLeadingOnes();
Chris Lattner7e044912010-01-04 07:17:19 +0000642 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
643 break;
644 }
645 case Instruction::Call:
646 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
647 switch (II->getIntrinsicID()) {
648 default: break;
649 case Intrinsic::bswap: {
650 // If the only bits demanded come from one byte of the bswap result,
651 // just shift the input byte into position to eliminate the bswap.
652 unsigned NLZ = DemandedMask.countLeadingZeros();
653 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000654
Chris Lattner7e044912010-01-04 07:17:19 +0000655 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
656 // we need all the bits down to bit 8. Likewise, round NLZ. If we
657 // have 14 leading zeros, round to 8.
658 NLZ &= ~7;
659 NTZ &= ~7;
660 // If we need exactly one byte, we can do this transformation.
661 if (BitWidth-NLZ-NTZ == 8) {
662 unsigned ResultBit = NTZ;
663 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000664
Chris Lattner7e044912010-01-04 07:17:19 +0000665 // Replace this with either a left or right shift to get the byte into
666 // the right place.
667 Instruction *NewVal;
668 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000669 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000670 ConstantInt::get(I->getType(), InputBit-ResultBit));
671 else
Gabor Greif79430172010-06-24 12:35:13 +0000672 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000673 ConstantInt::get(I->getType(), ResultBit-InputBit));
674 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000675 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000676 }
Craig Topper4c947752012-12-22 18:09:02 +0000677
Chris Lattner7e044912010-01-04 07:17:19 +0000678 // TODO: Could compute known zero/one bits based on the input.
679 break;
680 }
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000681 case Intrinsic::x86_mmx_pmovmskb:
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000682 case Intrinsic::x86_sse_movmsk_ps:
683 case Intrinsic::x86_sse2_movmsk_pd:
684 case Intrinsic::x86_sse2_pmovmskb_128:
685 case Intrinsic::x86_avx_movmsk_ps_256:
686 case Intrinsic::x86_avx_movmsk_pd_256:
687 case Intrinsic::x86_avx2_pmovmskb: {
688 // MOVMSK copies the vector elements' sign bits to the low bits
689 // and zeros the high bits.
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000690 unsigned ArgWidth;
691 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
692 ArgWidth = 8; // Arg is x86_mmx, but treated as <8 x i8>.
693 } else {
694 auto Arg = II->getArgOperand(0);
695 auto ArgType = cast<VectorType>(Arg->getType());
696 ArgWidth = ArgType->getNumElements();
697 }
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000698
699 // If we don't need any of low bits then return zero,
700 // we know that DemandedMask is non-zero already.
701 APInt DemandedElts = DemandedMask.zextOrTrunc(ArgWidth);
702 if (DemandedElts == 0)
703 return ConstantInt::getNullValue(VTy);
704
Ahmed Bougacha17482a52016-04-28 14:36:07 +0000705 // We know that the upper bits are set to zero.
Craig Topper3a86a042017-03-19 05:49:16 +0000706 KnownZero.setBitsFrom(ArgWidth);
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000707 return nullptr;
708 }
Chad Rosierb3628842011-05-26 23:13:19 +0000709 case Intrinsic::x86_sse42_crc32_64_64:
Craig Topper3a86a042017-03-19 05:49:16 +0000710 KnownZero.setBitsFrom(32);
Craig Topperf40110f2014-04-25 05:29:35 +0000711 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000712 }
713 }
Hal Finkel60db0582014-09-07 18:57:58 +0000714 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000715 break;
716 }
Craig Topper4c947752012-12-22 18:09:02 +0000717
Chris Lattner7e044912010-01-04 07:17:19 +0000718 // If the client is only demanding bits that we know, return the known
719 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000720 if (DemandedMask.isSubsetOf(KnownZero|KnownOne))
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000721 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000722 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000723}
724
Craig Topperb0076fe2017-04-12 18:05:21 +0000725/// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
726/// bits. It also tries to handle simplifications that can be done based on
727/// DemandedMask, but without modifying the Instruction.
728Value *InstCombiner::SimplifyMultipleUseDemandedBits(Instruction *I,
729 const APInt &DemandedMask,
730 APInt &KnownZero,
731 APInt &KnownOne,
732 unsigned Depth,
733 Instruction *CxtI) {
734 unsigned BitWidth = DemandedMask.getBitWidth();
735 Type *ITy = I->getType();
736
737 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
738 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
739
740 // Despite the fact that we can't simplify this instruction in all User's
741 // context, we can at least compute the knownzero/knownone bits, and we can
742 // do simplifications that apply to *just* the one user if we know that
743 // this instruction has a simpler value in that context.
Craig Topperf35a7f72017-04-12 18:25:25 +0000744 switch (I->getOpcode()) {
Craig Topper9a458cd2017-04-14 22:34:14 +0000745 case Instruction::And: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000746 // If either the LHS or the RHS are Zero, the result is zero.
747 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
748 CxtI);
749 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
750 CxtI);
751
Craig Topper9a458cd2017-04-14 22:34:14 +0000752 // Output known-0 are known to be clear if zero in either the LHS | RHS.
753 APInt IKnownZero = RHSKnownZero | LHSKnownZero;
754 // Output known-1 bits are only known if set in both the LHS & RHS.
755 APInt IKnownOne = RHSKnownOne & LHSKnownOne;
756
Craig Topperc75f94b2017-04-12 19:32:47 +0000757 // If the client is only demanding bits that we know, return the known
758 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000759 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000760 return Constant::getIntegerValue(ITy, IKnownOne);
Craig Topperc75f94b2017-04-12 19:32:47 +0000761
Craig Topperb0076fe2017-04-12 18:05:21 +0000762 // If all of the demanded bits are known 1 on one side, return the other.
763 // These bits cannot contribute to the result of the 'and' in this
764 // context.
Craig Topper17f37ba2017-04-20 20:47:35 +0000765 if (DemandedMask.isSubsetOf(LHSKnownZero | RHSKnownOne))
Craig Topperb0076fe2017-04-12 18:05:21 +0000766 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000767 if (DemandedMask.isSubsetOf(RHSKnownZero | LHSKnownOne))
Craig Topperb0076fe2017-04-12 18:05:21 +0000768 return I->getOperand(1);
769
Craig Topper9a458cd2017-04-14 22:34:14 +0000770 KnownZero = std::move(IKnownZero);
771 KnownOne = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000772 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000773 }
774 case Instruction::Or: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000775 // We can simplify (X|Y) -> X or Y in the user's context if we know that
776 // only bits from X or Y are demanded.
777
778 // If either the LHS or the RHS are One, the result is One.
779 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
780 CxtI);
781 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
782 CxtI);
783
Craig Topper9a458cd2017-04-14 22:34:14 +0000784 // Output known-0 bits are only known if clear in both the LHS & RHS.
785 APInt IKnownZero = RHSKnownZero & LHSKnownZero;
786 // Output known-1 are known to be set if set in either the LHS | RHS.
787 APInt IKnownOne = RHSKnownOne | LHSKnownOne;
788
Craig Topperc75f94b2017-04-12 19:32:47 +0000789 // If the client is only demanding bits that we know, return the known
790 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000791 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000792 return Constant::getIntegerValue(ITy, IKnownOne);
Craig Topperc75f94b2017-04-12 19:32:47 +0000793
Craig Topperb0076fe2017-04-12 18:05:21 +0000794 // If all of the demanded bits are known zero on one side, return the
795 // other. These bits cannot contribute to the result of the 'or' in this
796 // context.
Craig Topper17f37ba2017-04-20 20:47:35 +0000797 if (DemandedMask.isSubsetOf(LHSKnownOne | RHSKnownZero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000798 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000799 if (DemandedMask.isSubsetOf(RHSKnownOne | LHSKnownZero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000800 return I->getOperand(1);
801
Craig Topper9a458cd2017-04-14 22:34:14 +0000802 KnownZero = std::move(IKnownZero);
803 KnownOne = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000804 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000805 }
Craig Topperc75f94b2017-04-12 19:32:47 +0000806 case Instruction::Xor: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000807 // We can simplify (X^Y) -> X or Y in the user's context if we know that
808 // only bits from X or Y are demanded.
809
810 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
811 CxtI);
812 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
813 CxtI);
814
Craig Topperc75f94b2017-04-12 19:32:47 +0000815 // Output known-0 bits are known if clear or set in both the LHS & RHS.
816 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
817 (RHSKnownOne & LHSKnownOne);
818 // Output known-1 are known to be set if set in only one of the LHS, RHS.
819 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
820 (RHSKnownOne & LHSKnownZero);
821
822 // If the client is only demanding bits that we know, return the known
823 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000824 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topperc75f94b2017-04-12 19:32:47 +0000825 return Constant::getIntegerValue(ITy, IKnownOne);
826
Craig Topperb0076fe2017-04-12 18:05:21 +0000827 // If all of the demanded bits are known zero on one side, return the
828 // other.
Craig Topper17f37ba2017-04-20 20:47:35 +0000829 if (DemandedMask.isSubsetOf(RHSKnownZero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000830 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000831 if (DemandedMask.isSubsetOf(LHSKnownZero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000832 return I->getOperand(1);
Craig Topperf35a7f72017-04-12 18:25:25 +0000833
Craig Topperc75f94b2017-04-12 19:32:47 +0000834 // Output known-0 bits are known if clear or set in both the LHS & RHS.
835 KnownZero = std::move(IKnownZero);
836 // Output known-1 are known to be set if set in only one of the LHS, RHS.
837 KnownOne = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000838 break;
Craig Topperb0076fe2017-04-12 18:05:21 +0000839 }
Craig Topperc75f94b2017-04-12 19:32:47 +0000840 default:
841 // Compute the KnownZero/KnownOne bits to simplify things downstream.
842 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Craig Topperb0076fe2017-04-12 18:05:21 +0000843
Craig Topperc75f94b2017-04-12 19:32:47 +0000844 // If this user is only demanding bits that we know, return the known
845 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000846 if (DemandedMask.isSubsetOf(KnownZero|KnownOne))
Craig Topperc75f94b2017-04-12 19:32:47 +0000847 return Constant::getIntegerValue(ITy, KnownOne);
Craig Topper9a51c7f2017-04-12 18:17:46 +0000848
Craig Topperc75f94b2017-04-12 19:32:47 +0000849 break;
850 }
Craig Topper9a51c7f2017-04-12 18:17:46 +0000851
Craig Topperb0076fe2017-04-12 18:05:21 +0000852 return nullptr;
853}
854
855
Shuxin Yang63e999e2012-12-04 00:04:54 +0000856/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
857/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
858/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
859/// of "C2-C1".
860///
861/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
862/// ..., bn}, without considering the specific value X is holding.
863/// This transformation is legal iff one of following conditions is hold:
864/// 1) All the bit in S are 0, in this case E1 == E2.
865/// 2) We don't care those bits in S, per the input DemandedMask.
866/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
867/// rest bits.
868///
869/// Currently we only test condition 2).
870///
871/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
872/// not successful.
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000873Value *
Sanjay Patelcc663b82017-04-20 22:37:01 +0000874InstCombiner::simplifyShrShlDemandedBits(Instruction *Shr, const APInt &ShrOp1,
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000875 Instruction *Shl, const APInt &ShlOp1,
876 const APInt &DemandedMask,
877 APInt &KnownZero, APInt &KnownOne) {
Benjamin Kramer010f1082013-08-30 14:35:35 +0000878 if (!ShlOp1 || !ShrOp1)
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000879 return nullptr; // No-op.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000880
881 Value *VarX = Shr->getOperand(0);
882 Type *Ty = VarX->getType();
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000883 unsigned BitWidth = Ty->getScalarSizeInBits();
Benjamin Kramer010f1082013-08-30 14:35:35 +0000884 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000885 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000886
887 unsigned ShlAmt = ShlOp1.getZExtValue();
888 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000889
890 KnownOne.clearAllBits();
Craig Topper3a86a042017-03-19 05:49:16 +0000891 KnownZero.setLowBits(ShlAmt - 1);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000892 KnownZero &= DemandedMask;
893
Benjamin Kramer010f1082013-08-30 14:35:35 +0000894 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
895 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000896
897 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
898 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
899 (BitMask1.ashr(ShrAmt) << ShlAmt);
900
901 if (ShrAmt <= ShlAmt) {
902 BitMask2 <<= (ShlAmt - ShrAmt);
903 } else {
904 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
905 BitMask2.ashr(ShrAmt - ShlAmt);
906 }
907
908 // Check if condition-2 (see the comment to this function) is satified.
909 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
910 if (ShrAmt == ShlAmt)
911 return VarX;
912
913 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000914 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000915
916 BinaryOperator *New;
917 if (ShrAmt < ShlAmt) {
918 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
919 New = BinaryOperator::CreateShl(VarX, Amt);
920 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
921 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
922 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
923 } else {
924 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000925 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
926 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000927 if (cast<BinaryOperator>(Shr)->isExact())
928 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000929 }
930
931 return InsertNewInstWith(New, *Shl);
932 }
933
Craig Topperf40110f2014-04-25 05:29:35 +0000934 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000935}
Chris Lattner7e044912010-01-04 07:17:19 +0000936
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +0000937/// The specified value produces a vector with any number of elements.
938/// DemandedElts contains the set of elements that are actually used by the
939/// caller. This method analyzes which elements of the operand are undef and
940/// returns that information in UndefElts.
Chris Lattner7e044912010-01-04 07:17:19 +0000941///
942/// If the information about demanded elements can be used to simplify the
943/// operation, the operation is simplified, then the resultant value is
944/// returned. This returns null if no change was made.
945Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000946 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000947 unsigned Depth) {
Sanjay Patel9190b4a2016-04-29 20:54:56 +0000948 unsigned VWidth = V->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +0000949 APInt EltMask(APInt::getAllOnesValue(VWidth));
950 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
951
952 if (isa<UndefValue>(V)) {
953 // If the entire vector is undefined, just return this info.
954 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000955 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000956 }
Craig Topper4c947752012-12-22 18:09:02 +0000957
Chris Lattnerb22423c2010-02-08 23:56:03 +0000958 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000959 UndefElts = EltMask;
960 return UndefValue::get(V->getType());
961 }
962
963 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000964
Chris Lattner67058832012-01-25 06:48:06 +0000965 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
966 if (Constant *C = dyn_cast<Constant>(V)) {
967 // Check if this is identity. If so, return 0 since we are not simplifying
968 // anything.
969 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000970 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000971
Chris Lattner229907c2011-07-18 04:54:35 +0000972 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000973 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000974
Chris Lattner67058832012-01-25 06:48:06 +0000975 SmallVector<Constant*, 16> Elts;
976 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000977 if (!DemandedElts[i]) { // If not demanded, set to undef.
978 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000979 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +0000980 continue;
981 }
Craig Topper4c947752012-12-22 18:09:02 +0000982
Chris Lattner67058832012-01-25 06:48:06 +0000983 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000984 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000985
Chris Lattner67058832012-01-25 06:48:06 +0000986 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000987 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000988 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +0000989 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +0000990 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +0000991 }
Chris Lattner67058832012-01-25 06:48:06 +0000992 }
Craig Topper4c947752012-12-22 18:09:02 +0000993
Chris Lattner7e044912010-01-04 07:17:19 +0000994 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +0000995 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +0000996 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000997 }
Craig Topper4c947752012-12-22 18:09:02 +0000998
Chris Lattner7e044912010-01-04 07:17:19 +0000999 // Limit search depth.
1000 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +00001001 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001002
Stuart Hastings5bd18b62011-05-17 22:13:31 +00001003 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +00001004 // simplification conservatively assuming that all elements
1005 // are needed.
1006 if (!V->hasOneUse()) {
1007 // Quit if we find multiple users of a non-root value though.
1008 // They'll be handled when it's their turn to be visited by
1009 // the main instcombine process.
1010 if (Depth != 0)
1011 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +00001012 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001013
1014 // Conservatively assume that all elements are needed.
1015 DemandedElts = EltMask;
1016 }
Craig Topper4c947752012-12-22 18:09:02 +00001017
Chris Lattner7e044912010-01-04 07:17:19 +00001018 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +00001019 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +00001020
Chris Lattner7e044912010-01-04 07:17:19 +00001021 bool MadeChange = false;
1022 APInt UndefElts2(VWidth, 0);
Craig Topper23ebd952016-12-11 08:54:52 +00001023 APInt UndefElts3(VWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001024 Value *TmpV;
1025 switch (I->getOpcode()) {
1026 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001027
Chris Lattner7e044912010-01-04 07:17:19 +00001028 case Instruction::InsertElement: {
1029 // If this is a variable index, we don't know which element it overwrites.
1030 // demand exactly the same input as we produce.
1031 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +00001032 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +00001033 // Note that we can't propagate undef elt info, because we don't know
1034 // which elt is getting updated.
1035 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001036 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001037 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1038 break;
1039 }
Craig Topper4c947752012-12-22 18:09:02 +00001040
Chris Lattner7e044912010-01-04 07:17:19 +00001041 // If this is inserting an element that isn't demanded, remove this
1042 // insertelement.
1043 unsigned IdxNo = Idx->getZExtValue();
1044 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1045 Worklist.Add(I);
1046 return I->getOperand(0);
1047 }
Craig Topper4c947752012-12-22 18:09:02 +00001048
Chris Lattner7e044912010-01-04 07:17:19 +00001049 // Otherwise, the element inserted overwrites whatever was there, so the
1050 // input demanded set is simpler than the output set.
1051 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001052 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001053 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001054 UndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001055 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1056
1057 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001058 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001059 break;
1060 }
1061 case Instruction::ShuffleVector: {
1062 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Craig Topper2e18bcf2016-12-29 04:24:32 +00001063 unsigned LHSVWidth =
1064 Shuffle->getOperand(0)->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +00001065 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1066 for (unsigned i = 0; i < VWidth; i++) {
1067 if (DemandedElts[i]) {
1068 unsigned MaskVal = Shuffle->getMaskValue(i);
1069 if (MaskVal != -1u) {
1070 assert(MaskVal < LHSVWidth * 2 &&
1071 "shufflevector mask index out of range!");
1072 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001073 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001074 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001075 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001076 }
1077 }
1078 }
1079
Alexey Bataevfee90782016-09-23 09:14:08 +00001080 APInt LHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001081 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001082 LHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001083 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1084
Alexey Bataevfee90782016-09-23 09:14:08 +00001085 APInt RHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001086 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001087 RHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001088 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1089
1090 bool NewUndefElts = false;
Alexey Bataev793c9462016-09-26 13:18:59 +00001091 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1092 unsigned RHSIdx = -1u, RHSValIdx = -1u;
Alexey Bataevfee90782016-09-23 09:14:08 +00001093 bool LHSUniform = true;
1094 bool RHSUniform = true;
Chris Lattner7e044912010-01-04 07:17:19 +00001095 for (unsigned i = 0; i < VWidth; i++) {
1096 unsigned MaskVal = Shuffle->getMaskValue(i);
1097 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001098 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001099 } else if (!DemandedElts[i]) {
1100 NewUndefElts = true;
1101 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001102 } else if (MaskVal < LHSVWidth) {
Alexey Bataevfee90782016-09-23 09:14:08 +00001103 if (LHSUndefElts[MaskVal]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001104 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001105 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001106 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001107 LHSIdx = LHSIdx == -1u ? i : LHSVWidth;
1108 LHSValIdx = LHSValIdx == -1u ? MaskVal : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001109 LHSUniform = LHSUniform && (MaskVal == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001110 }
1111 } else {
Alexey Bataevfee90782016-09-23 09:14:08 +00001112 if (RHSUndefElts[MaskVal - LHSVWidth]) {
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 RHSIdx = RHSIdx == -1u ? i : LHSVWidth;
1117 RHSValIdx = RHSValIdx == -1u ? MaskVal - LHSVWidth : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001118 RHSUniform = RHSUniform && (MaskVal - LHSVWidth == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001119 }
1120 }
1121 }
1122
Alexey Bataevfee90782016-09-23 09:14:08 +00001123 // Try to transform shuffle with constant vector and single element from
1124 // this constant vector to single insertelement instruction.
1125 // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
1126 // insertelement V, C[ci], ci-n
1127 if (LHSVWidth == Shuffle->getType()->getNumElements()) {
1128 Value *Op = nullptr;
1129 Constant *Value = nullptr;
1130 unsigned Idx = -1u;
1131
Craig Topper62f06e22016-12-29 05:38:31 +00001132 // Find constant vector with the single element in shuffle (LHS or RHS).
Alexey Bataevfee90782016-09-23 09:14:08 +00001133 if (LHSIdx < LHSVWidth && RHSUniform) {
1134 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1135 Op = Shuffle->getOperand(1);
Alexey Bataev793c9462016-09-26 13:18:59 +00001136 Value = CV->getOperand(LHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001137 Idx = LHSIdx;
1138 }
1139 }
1140 if (RHSIdx < LHSVWidth && LHSUniform) {
1141 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1142 Op = Shuffle->getOperand(0);
Alexey Bataev793c9462016-09-26 13:18:59 +00001143 Value = CV->getOperand(RHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001144 Idx = RHSIdx;
1145 }
1146 }
1147 // Found constant vector with single element - convert to insertelement.
1148 if (Op && Value) {
1149 Instruction *New = InsertElementInst::Create(
1150 Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx),
1151 Shuffle->getName());
1152 InsertNewInstWith(New, *Shuffle);
1153 return New;
1154 }
1155 }
Chris Lattner7e044912010-01-04 07:17:19 +00001156 if (NewUndefElts) {
1157 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001158 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001159 for (unsigned i = 0; i < VWidth; ++i) {
1160 if (UndefElts[i])
1161 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1162 else
1163 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1164 Shuffle->getMaskValue(i)));
1165 }
1166 I->setOperand(2, ConstantVector::get(Elts));
1167 MadeChange = true;
1168 }
1169 break;
1170 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001171 case Instruction::Select: {
1172 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1173 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1174 for (unsigned i = 0; i < VWidth; i++) {
Andrea Di Biagio40f59e42015-10-06 10:34:53 +00001175 Constant *CElt = CV->getAggregateElement(i);
1176 // Method isNullValue always returns false when called on a
1177 // ConstantExpr. If CElt is a ConstantExpr then skip it in order to
1178 // to avoid propagating incorrect information.
1179 if (isa<ConstantExpr>(CElt))
1180 continue;
1181 if (CElt->isNullValue())
Pete Cooperabc13af2012-07-26 23:10:24 +00001182 LeftDemanded.clearBit(i);
1183 else
1184 RightDemanded.clearBit(i);
1185 }
1186 }
1187
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001188 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded, UndefElts,
1189 Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001190 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1191
1192 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001193 UndefElts2, Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001194 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001195
Pete Cooperabc13af2012-07-26 23:10:24 +00001196 // Output elements are undefined if both are undefined.
1197 UndefElts &= UndefElts2;
1198 break;
1199 }
Chris Lattner7e044912010-01-04 07:17:19 +00001200 case Instruction::BitCast: {
1201 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001202 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001203 if (!VTy) break;
1204 unsigned InVWidth = VTy->getNumElements();
1205 APInt InputDemandedElts(InVWidth, 0);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001206 UndefElts2 = APInt(InVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001207 unsigned Ratio;
1208
1209 if (VWidth == InVWidth) {
1210 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1211 // elements as are demanded of us.
1212 Ratio = 1;
1213 InputDemandedElts = DemandedElts;
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001214 } else if ((VWidth % InVWidth) == 0) {
1215 // If the number of elements in the output is a multiple of the number of
1216 // elements in the input then an input element is live if any of the
1217 // corresponding output elements are live.
1218 Ratio = VWidth / InVWidth;
1219 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Chris Lattner7e044912010-01-04 07:17:19 +00001220 if (DemandedElts[OutIdx])
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001221 InputDemandedElts.setBit(OutIdx / Ratio);
1222 } else if ((InVWidth % VWidth) == 0) {
1223 // If the number of elements in the input is a multiple of the number of
1224 // elements in the output then an input element is live if the
1225 // corresponding output element is live.
1226 Ratio = InVWidth / VWidth;
Chris Lattner7e044912010-01-04 07:17:19 +00001227 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001228 if (DemandedElts[InIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001229 InputDemandedElts.setBit(InIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001230 } else {
1231 // Unsupported so far.
1232 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001233 }
Craig Topper4c947752012-12-22 18:09:02 +00001234
Chris Lattner7e044912010-01-04 07:17:19 +00001235 // div/rem demand all inputs, because they don't want divide by zero.
1236 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001237 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001238 if (TmpV) {
1239 I->setOperand(0, TmpV);
1240 MadeChange = true;
1241 }
Craig Topper4c947752012-12-22 18:09:02 +00001242
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001243 if (VWidth == InVWidth) {
1244 UndefElts = UndefElts2;
1245 } else if ((VWidth % InVWidth) == 0) {
1246 // If the number of elements in the output is a multiple of the number of
1247 // elements in the input then an output element is undef if the
1248 // corresponding input element is undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001249 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001250 if (UndefElts2[OutIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001251 UndefElts.setBit(OutIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001252 } else if ((InVWidth % VWidth) == 0) {
1253 // If the number of elements in the input is a multiple of the number of
1254 // elements in the output then an output element is undef if all of the
1255 // corresponding input elements are undef.
1256 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1257 APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
1258 if (SubUndef.countPopulation() == Ratio)
1259 UndefElts.setBit(OutIdx);
1260 }
1261 } else {
Chris Lattner7e044912010-01-04 07:17:19 +00001262 llvm_unreachable("Unimp");
Chris Lattner7e044912010-01-04 07:17:19 +00001263 }
1264 break;
1265 }
1266 case Instruction::And:
1267 case Instruction::Or:
1268 case Instruction::Xor:
1269 case Instruction::Add:
1270 case Instruction::Sub:
1271 case Instruction::Mul:
1272 // div/rem demand all inputs, because they don't want divide by zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001273 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1274 Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001275 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1276 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001277 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001278 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001279
Chris Lattner7e044912010-01-04 07:17:19 +00001280 // Output elements are undefined if both are undefined. Consider things
1281 // like undef&0. The result is known zero, not undef.
1282 UndefElts &= UndefElts2;
1283 break;
Pete Coopere807e452012-07-26 22:37:04 +00001284 case Instruction::FPTrunc:
1285 case Instruction::FPExt:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001286 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1287 Depth + 1);
Pete Coopere807e452012-07-26 22:37:04 +00001288 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1289 break;
Craig Topper4c947752012-12-22 18:09:02 +00001290
Chris Lattner7e044912010-01-04 07:17:19 +00001291 case Instruction::Call: {
1292 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1293 if (!II) break;
1294 switch (II->getIntrinsicID()) {
1295 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001296
Craig Topper7fc6d342016-12-11 22:32:38 +00001297 case Intrinsic::x86_xop_vfrcz_ss:
1298 case Intrinsic::x86_xop_vfrcz_sd:
1299 // The instructions for these intrinsics are speced to zero upper bits not
1300 // pass them through like other scalar intrinsics. So we shouldn't just
1301 // use Arg0 if DemandedElts[0] is clear like we do for other intrinsics.
1302 // Instead we should return a zero vector.
Craig Topper1a8a3372016-12-29 03:30:17 +00001303 if (!DemandedElts[0]) {
1304 Worklist.Add(II);
Craig Topper7fc6d342016-12-11 22:32:38 +00001305 return ConstantAggregateZero::get(II->getType());
Craig Topper1a8a3372016-12-29 03:30:17 +00001306 }
Craig Topper7fc6d342016-12-11 22:32:38 +00001307
Craig Topperac75bca2016-12-13 07:45:45 +00001308 // Only the lower element is used.
1309 DemandedElts = 1;
Craig Topper7fc6d342016-12-11 22:32:38 +00001310 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1311 UndefElts, Depth + 1);
1312 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperac75bca2016-12-13 07:45:45 +00001313
1314 // Only the lower element is undefined. The high elements are zero.
1315 UndefElts = UndefElts[0];
Craig Topper7fc6d342016-12-11 22:32:38 +00001316 break;
1317
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001318 // Unary scalar-as-vector operations that work column-wise.
Simon Pilgrim83020942016-04-24 18:23:14 +00001319 case Intrinsic::x86_sse_rcp_ss:
1320 case Intrinsic::x86_sse_rsqrt_ss:
1321 case Intrinsic::x86_sse_sqrt_ss:
1322 case Intrinsic::x86_sse2_sqrt_sd:
Simon Pilgrim83020942016-04-24 18:23:14 +00001323 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1324 UndefElts, Depth + 1);
1325 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1326
1327 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001328 if (!DemandedElts[0]) {
1329 Worklist.Add(II);
Simon Pilgrim83020942016-04-24 18:23:14 +00001330 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001331 }
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001332 // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions
1333 // checks).
Simon Pilgrim83020942016-04-24 18:23:14 +00001334 break;
1335
Craig Toppera0372de2016-12-14 03:17:27 +00001336 // Binary scalar-as-vector operations that work column-wise. The high
1337 // elements come from operand 0. The low element is a function of both
1338 // operands.
Chris Lattner7e044912010-01-04 07:17:19 +00001339 case Intrinsic::x86_sse_min_ss:
1340 case Intrinsic::x86_sse_max_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001341 case Intrinsic::x86_sse_cmp_ss:
Chris Lattner7e044912010-01-04 07:17:19 +00001342 case Intrinsic::x86_sse2_min_sd:
1343 case Intrinsic::x86_sse2_max_sd:
Craig Toppera0372de2016-12-14 03:17:27 +00001344 case Intrinsic::x86_sse2_cmp_sd: {
1345 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1346 UndefElts, Depth + 1);
1347 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1348
1349 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001350 if (!DemandedElts[0]) {
1351 Worklist.Add(II);
Craig Toppera0372de2016-12-14 03:17:27 +00001352 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001353 }
Craig Toppera0372de2016-12-14 03:17:27 +00001354
1355 // Only lower element is used for operand 1.
1356 DemandedElts = 1;
1357 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1358 UndefElts2, Depth + 1);
1359 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1360
1361 // Lower element is undefined if both lower elements are undefined.
1362 // Consider things like undef&0. The result is known zero, not undef.
1363 if (!UndefElts2[0])
1364 UndefElts.clearBit(0);
1365
1366 break;
1367 }
1368
Craig Toppereb6a20e2016-12-14 03:17:30 +00001369 // Binary scalar-as-vector operations that work column-wise. The high
1370 // elements come from operand 0 and the low element comes from operand 1.
Simon Pilgrim83020942016-04-24 18:23:14 +00001371 case Intrinsic::x86_sse41_round_ss:
Craig Toppereb6a20e2016-12-14 03:17:30 +00001372 case Intrinsic::x86_sse41_round_sd: {
1373 // Don't use the low element of operand 0.
1374 APInt DemandedElts2 = DemandedElts;
1375 DemandedElts2.clearBit(0);
1376 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001377 UndefElts, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001378 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001379
1380 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001381 if (!DemandedElts[0]) {
1382 Worklist.Add(II);
Craig Toppereb6a20e2016-12-14 03:17:30 +00001383 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001384 }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001385
1386 // Only lower element is used for operand 1.
1387 DemandedElts = 1;
Gabor Greife23efee2010-06-28 16:45:00 +00001388 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001389 UndefElts2, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001390 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001391
Craig Toppereb6a20e2016-12-14 03:17:30 +00001392 // Take the high undef elements from operand 0 and take the lower element
1393 // from operand 1.
1394 UndefElts.clearBit(0);
1395 UndefElts |= UndefElts2[0];
Chris Lattner7e044912010-01-04 07:17:19 +00001396 break;
Craig Toppereb6a20e2016-12-14 03:17:30 +00001397 }
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001398
Craig Topperdfd268d2016-12-14 05:43:05 +00001399 // Three input scalar-as-vector operations that work column-wise. The high
1400 // elements come from operand 0 and the low element is a function of all
1401 // three inputs.
Craig Topper268b3ab2016-12-14 06:06:58 +00001402 case Intrinsic::x86_avx512_mask_add_ss_round:
1403 case Intrinsic::x86_avx512_mask_div_ss_round:
1404 case Intrinsic::x86_avx512_mask_mul_ss_round:
1405 case Intrinsic::x86_avx512_mask_sub_ss_round:
1406 case Intrinsic::x86_avx512_mask_max_ss_round:
1407 case Intrinsic::x86_avx512_mask_min_ss_round:
1408 case Intrinsic::x86_avx512_mask_add_sd_round:
1409 case Intrinsic::x86_avx512_mask_div_sd_round:
1410 case Intrinsic::x86_avx512_mask_mul_sd_round:
1411 case Intrinsic::x86_avx512_mask_sub_sd_round:
1412 case Intrinsic::x86_avx512_mask_max_sd_round:
1413 case Intrinsic::x86_avx512_mask_min_sd_round:
Craig Topper23ebd952016-12-11 08:54:52 +00001414 case Intrinsic::x86_fma_vfmadd_ss:
1415 case Intrinsic::x86_fma_vfmsub_ss:
1416 case Intrinsic::x86_fma_vfnmadd_ss:
1417 case Intrinsic::x86_fma_vfnmsub_ss:
1418 case Intrinsic::x86_fma_vfmadd_sd:
1419 case Intrinsic::x86_fma_vfmsub_sd:
1420 case Intrinsic::x86_fma_vfnmadd_sd:
1421 case Intrinsic::x86_fma_vfnmsub_sd:
Craig Topperab5f3552016-12-15 03:49:45 +00001422 case Intrinsic::x86_avx512_mask_vfmadd_ss:
1423 case Intrinsic::x86_avx512_mask_vfmadd_sd:
1424 case Intrinsic::x86_avx512_maskz_vfmadd_ss:
1425 case Intrinsic::x86_avx512_maskz_vfmadd_sd:
Craig Topper23ebd952016-12-11 08:54:52 +00001426 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1427 UndefElts, Depth + 1);
1428 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperdfd268d2016-12-14 05:43:05 +00001429
1430 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001431 if (!DemandedElts[0]) {
1432 Worklist.Add(II);
Craig Topperdfd268d2016-12-14 05:43:05 +00001433 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001434 }
Craig Topperdfd268d2016-12-14 05:43:05 +00001435
1436 // Only lower element is used for operand 1 and 2.
1437 DemandedElts = 1;
Craig Topper23ebd952016-12-11 08:54:52 +00001438 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1439 UndefElts2, Depth + 1);
1440 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1441 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1442 UndefElts3, Depth + 1);
1443 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1444
Craig Topperdfd268d2016-12-14 05:43:05 +00001445 // Lower element is undefined if all three lower elements are undefined.
1446 // Consider things like undef&0. The result is known zero, not undef.
1447 if (!UndefElts2[0] || !UndefElts3[0])
1448 UndefElts.clearBit(0);
Craig Topper23ebd952016-12-11 08:54:52 +00001449
Craig Topper23ebd952016-12-11 08:54:52 +00001450 break;
1451
Craig Topperab5f3552016-12-15 03:49:45 +00001452 case Intrinsic::x86_avx512_mask3_vfmadd_ss:
1453 case Intrinsic::x86_avx512_mask3_vfmadd_sd:
1454 case Intrinsic::x86_avx512_mask3_vfmsub_ss:
1455 case Intrinsic::x86_avx512_mask3_vfmsub_sd:
1456 case Intrinsic::x86_avx512_mask3_vfnmsub_ss:
1457 case Intrinsic::x86_avx512_mask3_vfnmsub_sd:
1458 // These intrinsics get the passthru bits from operand 2.
1459 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1460 UndefElts, Depth + 1);
1461 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1462
1463 // If lowest element of a scalar op isn't used then use Arg2.
Craig Topper1a8a3372016-12-29 03:30:17 +00001464 if (!DemandedElts[0]) {
1465 Worklist.Add(II);
Craig Topperab5f3552016-12-15 03:49:45 +00001466 return II->getArgOperand(2);
Craig Topper1a8a3372016-12-29 03:30:17 +00001467 }
Craig Topperab5f3552016-12-15 03:49:45 +00001468
1469 // Only lower element is used for operand 0 and 1.
1470 DemandedElts = 1;
1471 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1472 UndefElts2, Depth + 1);
1473 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1474 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1475 UndefElts3, Depth + 1);
1476 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1477
1478 // Lower element is undefined if all three lower elements are undefined.
1479 // Consider things like undef&0. The result is known zero, not undef.
1480 if (!UndefElts2[0] || !UndefElts3[0])
1481 UndefElts.clearBit(0);
1482
1483 break;
1484
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001485 case Intrinsic::x86_sse2_pmulu_dq:
1486 case Intrinsic::x86_sse41_pmuldq:
1487 case Intrinsic::x86_avx2_pmul_dq:
Craig Topper72f2d4e2016-12-27 05:30:09 +00001488 case Intrinsic::x86_avx2_pmulu_dq:
1489 case Intrinsic::x86_avx512_pmul_dq_512:
1490 case Intrinsic::x86_avx512_pmulu_dq_512: {
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001491 Value *Op0 = II->getArgOperand(0);
1492 Value *Op1 = II->getArgOperand(1);
1493 unsigned InnerVWidth = Op0->getType()->getVectorNumElements();
1494 assert((VWidth * 2) == InnerVWidth && "Unexpected input size");
1495
1496 APInt InnerDemandedElts(InnerVWidth, 0);
1497 for (unsigned i = 0; i != VWidth; ++i)
1498 if (DemandedElts[i])
1499 InnerDemandedElts.setBit(i * 2);
1500
1501 UndefElts2 = APInt(InnerVWidth, 0);
1502 TmpV = SimplifyDemandedVectorElts(Op0, InnerDemandedElts, UndefElts2,
1503 Depth + 1);
1504 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1505
1506 UndefElts3 = APInt(InnerVWidth, 0);
1507 TmpV = SimplifyDemandedVectorElts(Op1, InnerDemandedElts, UndefElts3,
1508 Depth + 1);
1509 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1510
1511 break;
1512 }
1513
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001514 case Intrinsic::x86_sse2_packssdw_128:
1515 case Intrinsic::x86_sse2_packsswb_128:
1516 case Intrinsic::x86_sse2_packuswb_128:
1517 case Intrinsic::x86_sse41_packusdw:
1518 case Intrinsic::x86_avx2_packssdw:
1519 case Intrinsic::x86_avx2_packsswb:
1520 case Intrinsic::x86_avx2_packusdw:
Craig Topper3731f4d2017-02-16 07:35:23 +00001521 case Intrinsic::x86_avx2_packuswb:
1522 case Intrinsic::x86_avx512_packssdw_512:
1523 case Intrinsic::x86_avx512_packsswb_512:
1524 case Intrinsic::x86_avx512_packusdw_512:
1525 case Intrinsic::x86_avx512_packuswb_512: {
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001526 auto *Ty0 = II->getArgOperand(0)->getType();
1527 unsigned InnerVWidth = Ty0->getVectorNumElements();
1528 assert(VWidth == (InnerVWidth * 2) && "Unexpected input size");
1529
1530 unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128;
1531 unsigned VWidthPerLane = VWidth / NumLanes;
1532 unsigned InnerVWidthPerLane = InnerVWidth / NumLanes;
1533
1534 // Per lane, pack the elements of the first input and then the second.
1535 // e.g.
1536 // v8i16 PACK(v4i32 X, v4i32 Y) - (X[0..3],Y[0..3])
1537 // v32i8 PACK(v16i16 X, v16i16 Y) - (X[0..7],Y[0..7]),(X[8..15],Y[8..15])
1538 for (int OpNum = 0; OpNum != 2; ++OpNum) {
1539 APInt OpDemandedElts(InnerVWidth, 0);
1540 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1541 unsigned LaneIdx = Lane * VWidthPerLane;
1542 for (unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) {
1543 unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum;
1544 if (DemandedElts[Idx])
1545 OpDemandedElts.setBit((Lane * InnerVWidthPerLane) + Elt);
1546 }
1547 }
1548
1549 // Demand elements from the operand.
1550 auto *Op = II->getArgOperand(OpNum);
1551 APInt OpUndefElts(InnerVWidth, 0);
1552 TmpV = SimplifyDemandedVectorElts(Op, OpDemandedElts, OpUndefElts,
1553 Depth + 1);
1554 if (TmpV) {
1555 II->setArgOperand(OpNum, TmpV);
1556 MadeChange = true;
1557 }
1558
1559 // Pack the operand's UNDEF elements, one lane at a time.
1560 OpUndefElts = OpUndefElts.zext(VWidth);
1561 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1562 APInt LaneElts = OpUndefElts.lshr(InnerVWidthPerLane * Lane);
1563 LaneElts = LaneElts.getLoBits(InnerVWidthPerLane);
Renato Golin4abfb3d2017-04-23 12:15:30 +00001564 LaneElts = LaneElts.shl(InnerVWidthPerLane * (2 * Lane + OpNum));
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001565 UndefElts |= LaneElts;
1566 }
1567 }
1568 break;
1569 }
1570
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001571 // PSHUFB
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001572 case Intrinsic::x86_ssse3_pshuf_b_128:
1573 case Intrinsic::x86_avx2_pshuf_b:
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001574 case Intrinsic::x86_avx512_pshuf_b_512:
1575 // PERMILVAR
1576 case Intrinsic::x86_avx_vpermilvar_ps:
1577 case Intrinsic::x86_avx_vpermilvar_ps_256:
1578 case Intrinsic::x86_avx512_vpermilvar_ps_512:
1579 case Intrinsic::x86_avx_vpermilvar_pd:
1580 case Intrinsic::x86_avx_vpermilvar_pd_256:
Simon Pilgrimfe2c0ed2017-01-18 14:47:49 +00001581 case Intrinsic::x86_avx512_vpermilvar_pd_512:
1582 // PERMV
1583 case Intrinsic::x86_avx2_permd:
1584 case Intrinsic::x86_avx2_permps: {
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001585 Value *Op1 = II->getArgOperand(1);
1586 TmpV = SimplifyDemandedVectorElts(Op1, DemandedElts, UndefElts,
1587 Depth + 1);
1588 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1589 break;
1590 }
1591
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001592 // SSE4A instructions leave the upper 64-bits of the 128-bit result
1593 // in an undefined state.
1594 case Intrinsic::x86_sse4a_extrq:
1595 case Intrinsic::x86_sse4a_extrqi:
1596 case Intrinsic::x86_sse4a_insertq:
1597 case Intrinsic::x86_sse4a_insertqi:
Craig Topper3a86a042017-03-19 05:49:16 +00001598 UndefElts.setHighBits(VWidth / 2);
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001599 break;
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001600 case Intrinsic::amdgcn_buffer_load:
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001601 case Intrinsic::amdgcn_buffer_load_format:
1602 case Intrinsic::amdgcn_image_sample:
1603 case Intrinsic::amdgcn_image_sample_cl:
1604 case Intrinsic::amdgcn_image_sample_d:
1605 case Intrinsic::amdgcn_image_sample_d_cl:
1606 case Intrinsic::amdgcn_image_sample_l:
1607 case Intrinsic::amdgcn_image_sample_b:
1608 case Intrinsic::amdgcn_image_sample_b_cl:
1609 case Intrinsic::amdgcn_image_sample_lz:
1610 case Intrinsic::amdgcn_image_sample_cd:
1611 case Intrinsic::amdgcn_image_sample_cd_cl:
1612
1613 case Intrinsic::amdgcn_image_sample_c:
1614 case Intrinsic::amdgcn_image_sample_c_cl:
1615 case Intrinsic::amdgcn_image_sample_c_d:
1616 case Intrinsic::amdgcn_image_sample_c_d_cl:
1617 case Intrinsic::amdgcn_image_sample_c_l:
1618 case Intrinsic::amdgcn_image_sample_c_b:
1619 case Intrinsic::amdgcn_image_sample_c_b_cl:
1620 case Intrinsic::amdgcn_image_sample_c_lz:
1621 case Intrinsic::amdgcn_image_sample_c_cd:
1622 case Intrinsic::amdgcn_image_sample_c_cd_cl:
1623
1624 case Intrinsic::amdgcn_image_sample_o:
1625 case Intrinsic::amdgcn_image_sample_cl_o:
1626 case Intrinsic::amdgcn_image_sample_d_o:
1627 case Intrinsic::amdgcn_image_sample_d_cl_o:
1628 case Intrinsic::amdgcn_image_sample_l_o:
1629 case Intrinsic::amdgcn_image_sample_b_o:
1630 case Intrinsic::amdgcn_image_sample_b_cl_o:
1631 case Intrinsic::amdgcn_image_sample_lz_o:
1632 case Intrinsic::amdgcn_image_sample_cd_o:
1633 case Intrinsic::amdgcn_image_sample_cd_cl_o:
1634
1635 case Intrinsic::amdgcn_image_sample_c_o:
1636 case Intrinsic::amdgcn_image_sample_c_cl_o:
1637 case Intrinsic::amdgcn_image_sample_c_d_o:
1638 case Intrinsic::amdgcn_image_sample_c_d_cl_o:
1639 case Intrinsic::amdgcn_image_sample_c_l_o:
1640 case Intrinsic::amdgcn_image_sample_c_b_o:
1641 case Intrinsic::amdgcn_image_sample_c_b_cl_o:
1642 case Intrinsic::amdgcn_image_sample_c_lz_o:
1643 case Intrinsic::amdgcn_image_sample_c_cd_o:
1644 case Intrinsic::amdgcn_image_sample_c_cd_cl_o:
1645
1646 case Intrinsic::amdgcn_image_getlod: {
Craig Topperd33ee1b2017-04-03 16:34:59 +00001647 if (VWidth == 1 || !DemandedElts.isMask())
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001648 return nullptr;
1649
1650 // TODO: Handle 3 vectors when supported in code gen.
1651 unsigned NewNumElts = PowerOf2Ceil(DemandedElts.countTrailingOnes());
1652 if (NewNumElts == VWidth)
1653 return nullptr;
1654
1655 Module *M = II->getParent()->getParent()->getParent();
1656 Type *EltTy = V->getType()->getVectorElementType();
1657
1658 Type *NewTy = (NewNumElts == 1) ? EltTy :
1659 VectorType::get(EltTy, NewNumElts);
1660
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001661 auto IID = II->getIntrinsicID();
1662
1663 bool IsBuffer = IID == Intrinsic::amdgcn_buffer_load ||
1664 IID == Intrinsic::amdgcn_buffer_load_format;
1665
1666 Function *NewIntrin = IsBuffer ?
1667 Intrinsic::getDeclaration(M, IID, NewTy) :
1668 // Samplers have 3 mangled types.
1669 Intrinsic::getDeclaration(M, IID,
1670 { NewTy, II->getArgOperand(0)->getType(),
1671 II->getArgOperand(1)->getType()});
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001672
1673 SmallVector<Value *, 5> Args;
1674 for (unsigned I = 0, E = II->getNumArgOperands(); I != E; ++I)
1675 Args.push_back(II->getArgOperand(I));
1676
Matt Arsenaulta3bdd8f2017-03-10 05:25:49 +00001677 IRBuilderBase::InsertPointGuard Guard(*Builder);
1678 Builder->SetInsertPoint(II);
1679
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001680 CallInst *NewCall = Builder->CreateCall(NewIntrin, Args);
1681 NewCall->takeName(II);
1682 NewCall->copyMetadata(*II);
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001683
1684 if (!IsBuffer) {
1685 ConstantInt *DMask = dyn_cast<ConstantInt>(NewCall->getArgOperand(3));
1686 if (DMask) {
1687 unsigned DMaskVal = DMask->getZExtValue() & 0xf;
1688
1689 unsigned PopCnt = 0;
1690 unsigned NewDMask = 0;
1691 for (unsigned I = 0; I < 4; ++I) {
1692 const unsigned Bit = 1 << I;
1693 if (!!(DMaskVal & Bit)) {
1694 if (++PopCnt > NewNumElts)
1695 break;
1696
1697 NewDMask |= Bit;
1698 }
1699 }
1700
1701 NewCall->setArgOperand(3, ConstantInt::get(DMask->getType(), NewDMask));
1702 }
1703 }
1704
1705
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001706 if (NewNumElts == 1) {
1707 return Builder->CreateInsertElement(UndefValue::get(V->getType()),
1708 NewCall, static_cast<uint64_t>(0));
1709 }
1710
1711 SmallVector<uint32_t, 8> EltMask;
1712 for (unsigned I = 0; I < VWidth; ++I)
1713 EltMask.push_back(I);
1714
1715 Value *Shuffle = Builder->CreateShuffleVector(
1716 NewCall, UndefValue::get(NewTy), EltMask);
1717
1718 MadeChange = true;
1719 return Shuffle;
1720 }
Chris Lattner7e044912010-01-04 07:17:19 +00001721 }
1722 break;
1723 }
1724 }
Craig Topperf40110f2014-04-25 05:29:35 +00001725 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001726}