blob: c5812abb792eb89e15b2905ffd5842f04aa1f292 [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.
141 if (Depth != 0 && !I->hasOneUse()) {
Craig Topperb0076fe2017-04-12 18:05:21 +0000142 return SimplifyMultipleUseDemandedBits(I, DemandedMask, KnownZero, KnownOne,
143 Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000144 }
Craig Topper4c947752012-12-22 18:09:02 +0000145
Craig Topperb0076fe2017-04-12 18:05:21 +0000146 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
147 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
148
Chris Lattner7e044912010-01-04 07:17:19 +0000149 // If this is the root being simplified, allow it to have multiple uses,
150 // just set the DemandedMask to all bits so that we can try to simplify the
151 // operands. This allows visitTruncInst (for example) to simplify the
152 // operand of a trunc without duplicating all the logic below.
153 if (Depth == 0 && !V->hasOneUse())
Craig Toppere06b6bc2017-04-04 05:03:02 +0000154 DemandedMask.setAllBits();
Craig Topper4c947752012-12-22 18:09:02 +0000155
Chris Lattner7e044912010-01-04 07:17:19 +0000156 switch (I->getOpcode()) {
157 default:
Hal Finkel60db0582014-09-07 18:57:58 +0000158 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000159 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000160 case Instruction::And: {
Chris Lattner7e044912010-01-04 07:17:19 +0000161 // If either the LHS or the RHS are Zero, the result is zero.
Craig Topper47596dd2017-03-25 06:52:52 +0000162 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
163 Depth + 1) ||
164 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnownZero, LHSKnownZero,
165 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000166 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000167 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
168 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000169
Craig Topper9a458cd2017-04-14 22:34:14 +0000170 // Output known-0 are known to be clear if zero in either the LHS | RHS.
171 APInt IKnownZero = RHSKnownZero | LHSKnownZero;
172 // Output known-1 bits are only known if set in both the LHS & RHS.
173 APInt IKnownOne = RHSKnownOne & LHSKnownOne;
174
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000175 // If the client is only demanding bits that we know, return the known
176 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000177 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000178 return Constant::getIntegerValue(VTy, IKnownOne);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000179
Chris Lattner7e044912010-01-04 07:17:19 +0000180 // If all of the demanded bits are known 1 on one side, return the other.
181 // These bits cannot contribute to the result of the 'and'.
Craig Topper17f37ba2017-04-20 20:47:35 +0000182 if (DemandedMask.isSubsetOf(LHSKnownZero | RHSKnownOne))
Chris Lattner7e044912010-01-04 07:17:19 +0000183 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000184 if (DemandedMask.isSubsetOf(RHSKnownZero | LHSKnownOne))
Chris Lattner7e044912010-01-04 07:17:19 +0000185 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000186
Chris Lattner7e044912010-01-04 07:17:19 +0000187 // If the RHS is a constant, see if we can simplify it.
188 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
189 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000190
Craig Topper9a458cd2017-04-14 22:34:14 +0000191 KnownZero = std::move(IKnownZero);
192 KnownOne = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000193 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000194 }
195 case Instruction::Or: {
Chris Lattner7e044912010-01-04 07:17:19 +0000196 // If either the LHS or the RHS are One, the result is One.
Craig Topper47596dd2017-03-25 06:52:52 +0000197 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
198 Depth + 1) ||
199 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnownOne, LHSKnownZero,
200 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000201 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000202 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
203 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
204
Craig Topper9a458cd2017-04-14 22:34:14 +0000205 // Output known-0 bits are only known if clear in both the LHS & RHS.
206 APInt IKnownZero = RHSKnownZero & LHSKnownZero;
207 // Output known-1 are known to be set if set in either the LHS | RHS.
208 APInt IKnownOne = RHSKnownOne | LHSKnownOne;
209
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000210 // If the client is only demanding bits that we know, return the known
211 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000212 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000213 return Constant::getIntegerValue(VTy, IKnownOne);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000214
Chris Lattner7e044912010-01-04 07:17:19 +0000215 // If all of the demanded bits are known zero on one side, return the other.
216 // These bits cannot contribute to the result of the 'or'.
Craig Topper17f37ba2017-04-20 20:47:35 +0000217 if (DemandedMask.isSubsetOf(LHSKnownOne | RHSKnownZero))
Chris Lattner7e044912010-01-04 07:17:19 +0000218 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000219 if (DemandedMask.isSubsetOf(RHSKnownOne | LHSKnownZero))
Chris Lattner7e044912010-01-04 07:17:19 +0000220 return I->getOperand(1);
221
Chris Lattner7e044912010-01-04 07:17:19 +0000222 // If the RHS is a constant, see if we can simplify it.
223 if (ShrinkDemandedConstant(I, 1, DemandedMask))
224 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000225
Craig Topper9a458cd2017-04-14 22:34:14 +0000226 KnownZero = std::move(IKnownZero);
227 KnownOne = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000228 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000229 }
Chris Lattner7e044912010-01-04 07:17:19 +0000230 case Instruction::Xor: {
Craig Topper47596dd2017-03-25 06:52:52 +0000231 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
232 Depth + 1) ||
233 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnownZero, LHSKnownOne,
234 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000235 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000236 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
237 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
238
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000239 // Output known-0 bits are known if clear or set in both the LHS & RHS.
240 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
241 (RHSKnownOne & LHSKnownOne);
242 // Output known-1 are known to be set if set in only one of the LHS, RHS.
243 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
244 (RHSKnownOne & LHSKnownZero);
245
246 // If the client is only demanding bits that we know, return the known
247 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000248 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000249 return Constant::getIntegerValue(VTy, IKnownOne);
250
Chris Lattner7e044912010-01-04 07:17:19 +0000251 // If all of the demanded bits are known zero on one side, return the other.
252 // These bits cannot contribute to the result of the 'xor'.
Craig Topper17f37ba2017-04-20 20:47:35 +0000253 if (DemandedMask.isSubsetOf(RHSKnownZero))
Chris Lattner7e044912010-01-04 07:17:19 +0000254 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000255 if (DemandedMask.isSubsetOf(LHSKnownZero))
Chris Lattner7e044912010-01-04 07:17:19 +0000256 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000257
Chris Lattner7e044912010-01-04 07:17:19 +0000258 // If all of the demanded bits are known to be zero on one side or the
259 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000260 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Craig Topper17f37ba2017-04-20 20:47:35 +0000261 if (DemandedMask.isSubsetOf(RHSKnownZero | LHSKnownZero)) {
Craig Topper4c947752012-12-22 18:09:02 +0000262 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000263 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
264 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000265 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000266 }
Craig Topper4c947752012-12-22 18:09:02 +0000267
Chris Lattner7e044912010-01-04 07:17:19 +0000268 // If all of the demanded bits on one side are known, and all of the set
269 // bits on that side are also known to be set on the other side, turn this
270 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000271 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper17f37ba2017-04-20 20:47:35 +0000272 if (DemandedMask.isSubsetOf(RHSKnownZero|RHSKnownOne) &&
273 RHSKnownOne.isSubsetOf(LHSKnownOne)) {
274 Constant *AndC = Constant::getIntegerValue(VTy,
275 ~RHSKnownOne & DemandedMask);
276 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
277 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000278 }
Craig Topper4c947752012-12-22 18:09:02 +0000279
Sanjay Patel8ce1d4c2017-04-21 20:29:17 +0000280 // If the RHS is a constant, see if we can simplify it.
281 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
282 if (ShrinkDemandedConstant(I, 1, DemandedMask))
283 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000284
Chris Lattner7e044912010-01-04 07:17:19 +0000285 // If our LHS is an 'and' and if it has one use, and if any of the bits we
286 // are flipping are known to be set, then the xor is just resetting those
287 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
288 // simplifying both of them.
289 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
290 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
291 isa<ConstantInt>(I->getOperand(1)) &&
292 isa<ConstantInt>(LHSInst->getOperand(1)) &&
293 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
294 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
295 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
296 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000297
Chris Lattner7e044912010-01-04 07:17:19 +0000298 Constant *AndC =
299 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000300 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000301 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000302
Chris Lattner7e044912010-01-04 07:17:19 +0000303 Constant *XorC =
304 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000305 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000306 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000307 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000308
309 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topper9a458cd2017-04-14 22:34:14 +0000310 KnownZero = std::move(IKnownZero);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000311 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topper9a458cd2017-04-14 22:34:14 +0000312 KnownOne = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000313 break;
314 }
315 case Instruction::Select:
James Molloy2b21a7c2015-05-20 18:41:25 +0000316 // If this is a select as part of a min/max pattern, don't simplify any
317 // further in case we break the structure.
318 Value *LHS, *RHS;
James Molloy134bec22015-08-11 09:12:57 +0000319 if (matchSelectPattern(I, LHS, RHS).Flavor != SPF_UNKNOWN)
James Molloy2b21a7c2015-05-20 18:41:25 +0000320 return nullptr;
Simon Pilgrim61116dd2015-09-17 20:32:45 +0000321
Craig Topper47596dd2017-03-25 06:52:52 +0000322 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnownZero, RHSKnownOne,
323 Depth + 1) ||
324 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnownZero, LHSKnownOne,
325 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000326 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000327 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
328 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
329
Chris Lattner7e044912010-01-04 07:17:19 +0000330 // If the operands are constants, see if we can simplify them.
331 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
332 ShrinkDemandedConstant(I, 2, DemandedMask))
333 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000334
Chris Lattner7e044912010-01-04 07:17:19 +0000335 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000336 KnownOne = RHSKnownOne & LHSKnownOne;
337 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000338 break;
339 case Instruction::Trunc: {
340 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000341 DemandedMask = DemandedMask.zext(truncBf);
342 KnownZero = KnownZero.zext(truncBf);
343 KnownOne = KnownOne.zext(truncBf);
Craig Topper47596dd2017-03-25 06:52:52 +0000344 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
345 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000346 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000347 DemandedMask = DemandedMask.trunc(BitWidth);
348 KnownZero = KnownZero.trunc(BitWidth);
349 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000350 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000351 break;
352 }
353 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000354 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000355 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000356
Chris Lattner229907c2011-07-18 04:54:35 +0000357 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
358 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000359 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
360 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
361 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000362 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000363 } else
364 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000365 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000366 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000367 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000368 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000369
Craig Topper47596dd2017-03-25 06:52:52 +0000370 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
371 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000372 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000373 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000374 break;
375 case Instruction::ZExt: {
376 // Compute the bits in the result that are not present in the input.
377 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000378
Jay Foad583abbc2010-12-07 08:25:19 +0000379 DemandedMask = DemandedMask.trunc(SrcBitWidth);
380 KnownZero = KnownZero.trunc(SrcBitWidth);
381 KnownOne = KnownOne.trunc(SrcBitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000382 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
383 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000384 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000385 DemandedMask = DemandedMask.zext(BitWidth);
386 KnownZero = KnownZero.zext(BitWidth);
387 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000388 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000389 // The top bits are known to be zero.
Craig Topper3a86a042017-03-19 05:49:16 +0000390 KnownZero.setBitsFrom(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000391 break;
392 }
393 case Instruction::SExt: {
394 // Compute the bits in the result that are not present in the input.
395 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000396
397 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000398 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
399
Craig Topper3a86a042017-03-19 05:49:16 +0000400 APInt NewBits(APInt::getBitsSetFrom(BitWidth, SrcBitWidth));
Chris Lattner7e044912010-01-04 07:17:19 +0000401 // If any of the sign extended bits are demanded, we know that the sign
402 // bit is demanded.
403 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000404 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000405
Jay Foad583abbc2010-12-07 08:25:19 +0000406 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
407 KnownZero = KnownZero.trunc(SrcBitWidth);
408 KnownOne = KnownOne.trunc(SrcBitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000409 if (SimplifyDemandedBits(I, 0, InputDemandedBits, KnownZero, KnownOne,
410 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000411 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000412 InputDemandedBits = InputDemandedBits.zext(BitWidth);
413 KnownZero = KnownZero.zext(BitWidth);
414 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000415 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
416
Chris Lattner7e044912010-01-04 07:17:19 +0000417 // If the sign bit of the input is known set or clear, then we know the
418 // top bits of the result.
419
420 // If the input sign bit is known zero, or if the NewBits are not demanded
421 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000422 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000423 // Convert to ZExt cast
424 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000425 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000426 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
427 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000428 }
429 break;
430 }
Matthias Braune48484c2015-04-30 22:05:30 +0000431 case Instruction::Add:
432 case Instruction::Sub: {
433 /// If the high-bits of an ADD/SUB are not demanded, then we do not care
434 /// about the high bits of the operands.
Chris Lattner7e044912010-01-04 07:17:19 +0000435 unsigned NLZ = DemandedMask.countLeadingZeros();
Matthias Braune48484c2015-04-30 22:05:30 +0000436 if (NLZ > 0) {
437 // Right fill the mask of bits for this ADD/SUB to demand the most
Chris Lattner7e044912010-01-04 07:17:19 +0000438 // significant bit and all those below it.
Chris Lattner7e044912010-01-04 07:17:19 +0000439 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
Craig Topper07f29152017-03-22 04:03:53 +0000440 if (ShrinkDemandedConstant(I, 0, DemandedFromOps) ||
Craig Topper47596dd2017-03-25 06:52:52 +0000441 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnownZero, LHSKnownOne,
442 Depth + 1) ||
Matthias Braune48484c2015-04-30 22:05:30 +0000443 ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
Craig Topper845033a2017-04-12 16:49:59 +0000444 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnownZero, RHSKnownOne,
Craig Topper47596dd2017-03-25 06:52:52 +0000445 Depth + 1)) {
Matthias Braune48484c2015-04-30 22:05:30 +0000446 // Disable the nsw and nuw flags here: We can no longer guarantee that
447 // we won't wrap after simplification. Removing the nsw/nuw flags is
448 // legal here because the top bit is not demanded.
449 BinaryOperator &BinOP = *cast<BinaryOperator>(I);
450 BinOP.setHasNoSignedWrap(false);
451 BinOP.setHasNoUnsignedWrap(false);
Chris Lattner7e044912010-01-04 07:17:19 +0000452 return I;
David Majnemer7d0e99c2015-04-22 22:42:05 +0000453 }
Craig Topper845033a2017-04-12 16:49:59 +0000454
455 // If we are known to be adding/subtracting zeros to every bit below
456 // the highest demanded bit, we just return the other side.
457 if ((DemandedFromOps & RHSKnownZero) == DemandedFromOps)
458 return I->getOperand(0);
459 // We can't do this with the LHS for subtraction.
460 if (I->getOpcode() == Instruction::Add &&
461 (DemandedFromOps & LHSKnownZero) == DemandedFromOps)
462 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000463 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000464
Craig Topper8fbb74b2017-03-24 22:12:10 +0000465 // Otherwise just hand the add/sub off to computeKnownBits to fill in
466 // the known zeros and ones.
467 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000468 break;
Matthias Braune48484c2015-04-30 22:05:30 +0000469 }
Sanjay Patel3e1ae722017-04-20 21:33:02 +0000470 case Instruction::Shl: {
471 const APInt *SA;
472 if (match(I->getOperand(1), m_APInt(SA))) {
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000473 const APInt *ShrAmt;
474 if (match(I->getOperand(0), m_Shr(m_Value(), m_APInt(ShrAmt)))) {
475 Instruction *Shr = cast<Instruction>(I->getOperand(0));
Sanjay Patelcc663b82017-04-20 22:37:01 +0000476 if (Value *R = simplifyShrShlDemandedBits(
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000477 Shr, *ShrAmt, I, *SA, DemandedMask, KnownZero, KnownOne))
478 return R;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000479 }
480
Chris Lattner768003c2011-02-10 05:09:34 +0000481 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000482 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000483
Chris Lattner768003c2011-02-10 05:09:34 +0000484 // If the shift is NUW/NSW, then it does demand the high bits.
485 ShlOperator *IOp = cast<ShlOperator>(I);
486 if (IOp->hasNoSignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000487 DemandedMaskIn.setHighBits(ShiftAmt+1);
Chris Lattner768003c2011-02-10 05:09:34 +0000488 else if (IOp->hasNoUnsignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000489 DemandedMaskIn.setHighBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000490
Craig Topper47596dd2017-03-25 06:52:52 +0000491 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
492 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000493 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000494 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
495 KnownZero <<= ShiftAmt;
496 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000497 // low bits known zero.
498 if (ShiftAmt)
Craig Topper3a86a042017-03-19 05:49:16 +0000499 KnownZero.setLowBits(ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000500 }
501 break;
Sanjay Patel3e1ae722017-04-20 21:33:02 +0000502 }
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000503 case Instruction::LShr: {
504 const APInt *SA;
505 if (match(I->getOperand(1), m_APInt(SA))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000506 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000507
Chris Lattner7e044912010-01-04 07:17:19 +0000508 // Unsigned shift right.
509 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000510
Chris Lattner768003c2011-02-10 05:09:34 +0000511 // If the shift is exact, then it does demand the low bits (and knows that
512 // they are zero).
513 if (cast<LShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000514 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000515
Craig Topper47596dd2017-03-25 06:52:52 +0000516 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
517 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000518 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000519 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Craig Topperfc947bc2017-04-18 17:14:21 +0000520 KnownZero.lshrInPlace(ShiftAmt);
521 KnownOne.lshrInPlace(ShiftAmt);
Craig Topper3a86a042017-03-19 05:49:16 +0000522 if (ShiftAmt)
523 KnownZero.setHighBits(ShiftAmt); // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000524 }
525 break;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000526 }
527 case Instruction::AShr: {
Chris Lattner7e044912010-01-04 07:17:19 +0000528 // If this is an arithmetic shift right and only the low-bit is set, we can
529 // always convert this into a logical shr, even if the shift amount is
530 // variable. The low bit of the shift cannot be an input sign bit unless
531 // the shift amount is >= the size of the datatype, which is undefined.
532 if (DemandedMask == 1) {
533 // Perform the logical shift right.
534 Instruction *NewVal = BinaryOperator::CreateLShr(
535 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000536 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000537 }
Chris Lattner7e044912010-01-04 07:17:19 +0000538
539 // If the sign bit is the only bit demanded by this ashr, then there is no
540 // need to do it, the shift doesn't change the high bit.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000541 if (DemandedMask.isSignMask())
Chris Lattner7e044912010-01-04 07:17:19 +0000542 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000543
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000544 const APInt *SA;
545 if (match(I->getOperand(1), m_APInt(SA))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000546 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000547
Chris Lattner7e044912010-01-04 07:17:19 +0000548 // Signed shift right.
549 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000550 // If any of the high bits are demanded, we should set the sign bit as
Chris Lattner7e044912010-01-04 07:17:19 +0000551 // demanded.
552 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Craig Topperc9a4fc02017-04-14 05:09:04 +0000553 DemandedMaskIn.setSignBit();
Craig Topper4c947752012-12-22 18:09:02 +0000554
Chris Lattner768003c2011-02-10 05:09:34 +0000555 // If the shift is exact, then it does demand the low bits (and knows that
556 // they are zero).
557 if (cast<AShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000558 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000559
Craig Topper47596dd2017-03-25 06:52:52 +0000560 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
561 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000562 return I;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000563
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000564 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000565 // Compute the new bits that are at the top now.
566 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Craig Topperfc947bc2017-04-18 17:14:21 +0000567 KnownZero.lshrInPlace(ShiftAmt);
568 KnownOne.lshrInPlace(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000569
Chris Lattner7e044912010-01-04 07:17:19 +0000570 // Handle the sign bits.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000571 APInt SignMask(APInt::getSignMask(BitWidth));
Chris Lattner7e044912010-01-04 07:17:19 +0000572 // Adjust to where it is now in the mask.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000573 SignMask.lshrInPlace(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000574
Chris Lattner7e044912010-01-04 07:17:19 +0000575 // If the input sign bit is known to be zero, or if none of the top bits
576 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000577 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Craig Topperff238892017-04-20 21:24:37 +0000578 !DemandedMask.intersects(HighBits)) {
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000579 BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0),
580 I->getOperand(1));
581 LShr->setIsExact(cast<BinaryOperator>(I)->isExact());
582 return InsertNewInstWith(LShr, *I);
Craig Topperff238892017-04-20 21:24:37 +0000583 } else if (KnownOne.intersects(SignMask)) { // New bits are known one.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000584 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000585 }
586 }
587 break;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000588 }
Chris Lattner7e044912010-01-04 07:17:19 +0000589 case Instruction::SRem:
590 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000591 // X % -1 demands all the bits because we don't want to introduce
592 // INT_MIN % -1 (== undef) by accident.
593 if (Rem->isAllOnesValue())
594 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000595 APInt RA = Rem->getValue().abs();
596 if (RA.isPowerOf2()) {
597 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
598 return I->getOperand(0);
599
600 APInt LowBits = RA - 1;
Craig Topperbcfd2d12017-04-20 16:56:25 +0000601 APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000602 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnownZero, LHSKnownOne,
603 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000604 return I;
605
Duncan Sands3a48b872010-01-28 17:22:42 +0000606 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000607 KnownZero = LHSKnownZero & LowBits;
608 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000609
Duncan Sands3a48b872010-01-28 17:22:42 +0000610 // If LHS is non-negative or has all low bits zero, then the upper bits
611 // are all zero.
Craig Topperff238892017-04-20 21:24:37 +0000612 if (LHSKnownZero.isSignBitSet() || LowBits.isSubsetOf(LHSKnownZero))
Duncan Sands3a48b872010-01-28 17:22:42 +0000613 KnownZero |= ~LowBits;
614
615 // If LHS is negative and not all low bits are zero, then the upper bits
616 // are all one.
Craig Topperff238892017-04-20 21:24:37 +0000617 if (LHSKnownOne.isSignBitSet() && LowBits.intersects(LHSKnownOne))
Duncan Sands3a48b872010-01-28 17:22:42 +0000618 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000619
Craig Topper4c947752012-12-22 18:09:02 +0000620 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Craig Topperda886c62017-04-16 21:46:12 +0000621 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000622 }
623 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000624
625 // The sign bit is the LHS's sign bit, except when the result of the
626 // remainder is zero.
Craig Topperd23004c2017-04-17 16:38:20 +0000627 if (DemandedMask.isSignBitSet()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000628 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000629 CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000630 // If it's known zero, our sign bit is also zero.
Craig Topperd23004c2017-04-17 16:38:20 +0000631 if (LHSKnownZero.isSignBitSet())
Craig Topper3a86a042017-03-19 05:49:16 +0000632 KnownZero.setSignBit();
Nick Lewyckye4679792011-03-07 01:50:10 +0000633 }
Chris Lattner7e044912010-01-04 07:17:19 +0000634 break;
635 case Instruction::URem: {
636 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
637 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000638 if (SimplifyDemandedBits(I, 0, AllOnes, KnownZero2, KnownOne2, Depth + 1) ||
639 SimplifyDemandedBits(I, 1, AllOnes, KnownZero2, KnownOne2, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000640 return I;
641
642 unsigned Leaders = KnownZero2.countLeadingOnes();
Chris Lattner7e044912010-01-04 07:17:19 +0000643 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
644 break;
645 }
646 case Instruction::Call:
647 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
648 switch (II->getIntrinsicID()) {
649 default: break;
650 case Intrinsic::bswap: {
651 // If the only bits demanded come from one byte of the bswap result,
652 // just shift the input byte into position to eliminate the bswap.
653 unsigned NLZ = DemandedMask.countLeadingZeros();
654 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000655
Chris Lattner7e044912010-01-04 07:17:19 +0000656 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
657 // we need all the bits down to bit 8. Likewise, round NLZ. If we
658 // have 14 leading zeros, round to 8.
659 NLZ &= ~7;
660 NTZ &= ~7;
661 // If we need exactly one byte, we can do this transformation.
662 if (BitWidth-NLZ-NTZ == 8) {
663 unsigned ResultBit = NTZ;
664 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000665
Chris Lattner7e044912010-01-04 07:17:19 +0000666 // Replace this with either a left or right shift to get the byte into
667 // the right place.
668 Instruction *NewVal;
669 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000670 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000671 ConstantInt::get(I->getType(), InputBit-ResultBit));
672 else
Gabor Greif79430172010-06-24 12:35:13 +0000673 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000674 ConstantInt::get(I->getType(), ResultBit-InputBit));
675 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000676 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000677 }
Craig Topper4c947752012-12-22 18:09:02 +0000678
Chris Lattner7e044912010-01-04 07:17:19 +0000679 // TODO: Could compute known zero/one bits based on the input.
680 break;
681 }
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000682 case Intrinsic::x86_mmx_pmovmskb:
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000683 case Intrinsic::x86_sse_movmsk_ps:
684 case Intrinsic::x86_sse2_movmsk_pd:
685 case Intrinsic::x86_sse2_pmovmskb_128:
686 case Intrinsic::x86_avx_movmsk_ps_256:
687 case Intrinsic::x86_avx_movmsk_pd_256:
688 case Intrinsic::x86_avx2_pmovmskb: {
689 // MOVMSK copies the vector elements' sign bits to the low bits
690 // and zeros the high bits.
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000691 unsigned ArgWidth;
692 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
693 ArgWidth = 8; // Arg is x86_mmx, but treated as <8 x i8>.
694 } else {
695 auto Arg = II->getArgOperand(0);
696 auto ArgType = cast<VectorType>(Arg->getType());
697 ArgWidth = ArgType->getNumElements();
698 }
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000699
700 // If we don't need any of low bits then return zero,
701 // we know that DemandedMask is non-zero already.
702 APInt DemandedElts = DemandedMask.zextOrTrunc(ArgWidth);
703 if (DemandedElts == 0)
704 return ConstantInt::getNullValue(VTy);
705
Ahmed Bougacha17482a52016-04-28 14:36:07 +0000706 // We know that the upper bits are set to zero.
Craig Topper3a86a042017-03-19 05:49:16 +0000707 KnownZero.setBitsFrom(ArgWidth);
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000708 return nullptr;
709 }
Chad Rosierb3628842011-05-26 23:13:19 +0000710 case Intrinsic::x86_sse42_crc32_64_64:
Craig Topper3a86a042017-03-19 05:49:16 +0000711 KnownZero.setBitsFrom(32);
Craig Topperf40110f2014-04-25 05:29:35 +0000712 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000713 }
714 }
Hal Finkel60db0582014-09-07 18:57:58 +0000715 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000716 break;
717 }
Craig Topper4c947752012-12-22 18:09:02 +0000718
Chris Lattner7e044912010-01-04 07:17:19 +0000719 // If the client is only demanding bits that we know, return the known
720 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000721 if (DemandedMask.isSubsetOf(KnownZero|KnownOne))
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000722 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000723 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000724}
725
Craig Topperb0076fe2017-04-12 18:05:21 +0000726/// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
727/// bits. It also tries to handle simplifications that can be done based on
728/// DemandedMask, but without modifying the Instruction.
729Value *InstCombiner::SimplifyMultipleUseDemandedBits(Instruction *I,
730 const APInt &DemandedMask,
731 APInt &KnownZero,
732 APInt &KnownOne,
733 unsigned Depth,
734 Instruction *CxtI) {
735 unsigned BitWidth = DemandedMask.getBitWidth();
736 Type *ITy = I->getType();
737
738 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
739 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
740
741 // Despite the fact that we can't simplify this instruction in all User's
742 // context, we can at least compute the knownzero/knownone bits, and we can
743 // do simplifications that apply to *just* the one user if we know that
744 // this instruction has a simpler value in that context.
Craig Topperf35a7f72017-04-12 18:25:25 +0000745 switch (I->getOpcode()) {
Craig Topper9a458cd2017-04-14 22:34:14 +0000746 case Instruction::And: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000747 // If either the LHS or the RHS are Zero, the result is zero.
748 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
749 CxtI);
750 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
751 CxtI);
752
Craig Topper9a458cd2017-04-14 22:34:14 +0000753 // Output known-0 are known to be clear if zero in either the LHS | RHS.
754 APInt IKnownZero = RHSKnownZero | LHSKnownZero;
755 // Output known-1 bits are only known if set in both the LHS & RHS.
756 APInt IKnownOne = RHSKnownOne & LHSKnownOne;
757
Craig Topperc75f94b2017-04-12 19:32:47 +0000758 // If the client is only demanding bits that we know, return the known
759 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000760 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000761 return Constant::getIntegerValue(ITy, IKnownOne);
Craig Topperc75f94b2017-04-12 19:32:47 +0000762
Craig Topperb0076fe2017-04-12 18:05:21 +0000763 // If all of the demanded bits are known 1 on one side, return the other.
764 // These bits cannot contribute to the result of the 'and' in this
765 // context.
Craig Topper17f37ba2017-04-20 20:47:35 +0000766 if (DemandedMask.isSubsetOf(LHSKnownZero | RHSKnownOne))
Craig Topperb0076fe2017-04-12 18:05:21 +0000767 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000768 if (DemandedMask.isSubsetOf(RHSKnownZero | LHSKnownOne))
Craig Topperb0076fe2017-04-12 18:05:21 +0000769 return I->getOperand(1);
770
Craig Topper9a458cd2017-04-14 22:34:14 +0000771 KnownZero = std::move(IKnownZero);
772 KnownOne = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000773 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000774 }
775 case Instruction::Or: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000776 // We can simplify (X|Y) -> X or Y in the user's context if we know that
777 // only bits from X or Y are demanded.
778
779 // If either the LHS or the RHS are One, the result is One.
780 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
781 CxtI);
782 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
783 CxtI);
784
Craig Topper9a458cd2017-04-14 22:34:14 +0000785 // Output known-0 bits are only known if clear in both the LHS & RHS.
786 APInt IKnownZero = RHSKnownZero & LHSKnownZero;
787 // Output known-1 are known to be set if set in either the LHS | RHS.
788 APInt IKnownOne = RHSKnownOne | LHSKnownOne;
789
Craig Topperc75f94b2017-04-12 19:32:47 +0000790 // If the client is only demanding bits that we know, return the known
791 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000792 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000793 return Constant::getIntegerValue(ITy, IKnownOne);
Craig Topperc75f94b2017-04-12 19:32:47 +0000794
Craig Topperb0076fe2017-04-12 18:05:21 +0000795 // If all of the demanded bits are known zero on one side, return the
796 // other. These bits cannot contribute to the result of the 'or' in this
797 // context.
Craig Topper17f37ba2017-04-20 20:47:35 +0000798 if (DemandedMask.isSubsetOf(LHSKnownOne | RHSKnownZero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000799 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000800 if (DemandedMask.isSubsetOf(RHSKnownOne | LHSKnownZero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000801 return I->getOperand(1);
802
Craig Topper9a458cd2017-04-14 22:34:14 +0000803 KnownZero = std::move(IKnownZero);
804 KnownOne = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000805 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000806 }
Craig Topperc75f94b2017-04-12 19:32:47 +0000807 case Instruction::Xor: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000808 // We can simplify (X^Y) -> X or Y in the user's context if we know that
809 // only bits from X or Y are demanded.
810
811 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
812 CxtI);
813 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
814 CxtI);
815
Craig Topperc75f94b2017-04-12 19:32:47 +0000816 // Output known-0 bits are known if clear or set in both the LHS & RHS.
817 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
818 (RHSKnownOne & LHSKnownOne);
819 // Output known-1 are known to be set if set in only one of the LHS, RHS.
820 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
821 (RHSKnownOne & LHSKnownZero);
822
823 // If the client is only demanding bits that we know, return the known
824 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000825 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topperc75f94b2017-04-12 19:32:47 +0000826 return Constant::getIntegerValue(ITy, IKnownOne);
827
Craig Topperb0076fe2017-04-12 18:05:21 +0000828 // If all of the demanded bits are known zero on one side, return the
829 // other.
Craig Topper17f37ba2017-04-20 20:47:35 +0000830 if (DemandedMask.isSubsetOf(RHSKnownZero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000831 return I->getOperand(0);
Craig Topper17f37ba2017-04-20 20:47:35 +0000832 if (DemandedMask.isSubsetOf(LHSKnownZero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000833 return I->getOperand(1);
Craig Topperf35a7f72017-04-12 18:25:25 +0000834
Craig Topperc75f94b2017-04-12 19:32:47 +0000835 // Output known-0 bits are known if clear or set in both the LHS & RHS.
836 KnownZero = std::move(IKnownZero);
837 // Output known-1 are known to be set if set in only one of the LHS, RHS.
838 KnownOne = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000839 break;
Craig Topperb0076fe2017-04-12 18:05:21 +0000840 }
Craig Topperc75f94b2017-04-12 19:32:47 +0000841 default:
842 // Compute the KnownZero/KnownOne bits to simplify things downstream.
843 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Craig Topperb0076fe2017-04-12 18:05:21 +0000844
Craig Topperc75f94b2017-04-12 19:32:47 +0000845 // If this user is only demanding bits that we know, return the known
846 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000847 if (DemandedMask.isSubsetOf(KnownZero|KnownOne))
Craig Topperc75f94b2017-04-12 19:32:47 +0000848 return Constant::getIntegerValue(ITy, KnownOne);
Craig Topper9a51c7f2017-04-12 18:17:46 +0000849
Craig Topperc75f94b2017-04-12 19:32:47 +0000850 break;
851 }
Craig Topper9a51c7f2017-04-12 18:17:46 +0000852
Craig Topperb0076fe2017-04-12 18:05:21 +0000853 return nullptr;
854}
855
856
Shuxin Yang63e999e2012-12-04 00:04:54 +0000857/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
858/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
859/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
860/// of "C2-C1".
861///
862/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
863/// ..., bn}, without considering the specific value X is holding.
864/// This transformation is legal iff one of following conditions is hold:
865/// 1) All the bit in S are 0, in this case E1 == E2.
866/// 2) We don't care those bits in S, per the input DemandedMask.
867/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
868/// rest bits.
869///
870/// Currently we only test condition 2).
871///
872/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
873/// not successful.
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000874Value *
Sanjay Patelcc663b82017-04-20 22:37:01 +0000875InstCombiner::simplifyShrShlDemandedBits(Instruction *Shr, const APInt &ShrOp1,
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000876 Instruction *Shl, const APInt &ShlOp1,
877 const APInt &DemandedMask,
878 APInt &KnownZero, APInt &KnownOne) {
Benjamin Kramer010f1082013-08-30 14:35:35 +0000879 if (!ShlOp1 || !ShrOp1)
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000880 return nullptr; // No-op.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000881
882 Value *VarX = Shr->getOperand(0);
883 Type *Ty = VarX->getType();
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000884 unsigned BitWidth = Ty->getScalarSizeInBits();
Benjamin Kramer010f1082013-08-30 14:35:35 +0000885 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000886 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000887
888 unsigned ShlAmt = ShlOp1.getZExtValue();
889 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000890
891 KnownOne.clearAllBits();
Craig Topper3a86a042017-03-19 05:49:16 +0000892 KnownZero.setLowBits(ShlAmt - 1);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000893 KnownZero &= DemandedMask;
894
Benjamin Kramer010f1082013-08-30 14:35:35 +0000895 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
896 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000897
898 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
899 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
900 (BitMask1.ashr(ShrAmt) << ShlAmt);
901
902 if (ShrAmt <= ShlAmt) {
903 BitMask2 <<= (ShlAmt - ShrAmt);
904 } else {
905 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
906 BitMask2.ashr(ShrAmt - ShlAmt);
907 }
908
909 // Check if condition-2 (see the comment to this function) is satified.
910 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
911 if (ShrAmt == ShlAmt)
912 return VarX;
913
914 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000915 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000916
917 BinaryOperator *New;
918 if (ShrAmt < ShlAmt) {
919 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
920 New = BinaryOperator::CreateShl(VarX, Amt);
921 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
922 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
923 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
924 } else {
925 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000926 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
927 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000928 if (cast<BinaryOperator>(Shr)->isExact())
929 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000930 }
931
932 return InsertNewInstWith(New, *Shl);
933 }
934
Craig Topperf40110f2014-04-25 05:29:35 +0000935 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000936}
Chris Lattner7e044912010-01-04 07:17:19 +0000937
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +0000938/// The specified value produces a vector with any number of elements.
939/// DemandedElts contains the set of elements that are actually used by the
940/// caller. This method analyzes which elements of the operand are undef and
941/// returns that information in UndefElts.
Chris Lattner7e044912010-01-04 07:17:19 +0000942///
943/// If the information about demanded elements can be used to simplify the
944/// operation, the operation is simplified, then the resultant value is
945/// returned. This returns null if no change was made.
946Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000947 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000948 unsigned Depth) {
Sanjay Patel9190b4a2016-04-29 20:54:56 +0000949 unsigned VWidth = V->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +0000950 APInt EltMask(APInt::getAllOnesValue(VWidth));
951 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
952
953 if (isa<UndefValue>(V)) {
954 // If the entire vector is undefined, just return this info.
955 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000956 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000957 }
Craig Topper4c947752012-12-22 18:09:02 +0000958
Chris Lattnerb22423c2010-02-08 23:56:03 +0000959 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000960 UndefElts = EltMask;
961 return UndefValue::get(V->getType());
962 }
963
964 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000965
Chris Lattner67058832012-01-25 06:48:06 +0000966 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
967 if (Constant *C = dyn_cast<Constant>(V)) {
968 // Check if this is identity. If so, return 0 since we are not simplifying
969 // anything.
970 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000971 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000972
Chris Lattner229907c2011-07-18 04:54:35 +0000973 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000974 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000975
Chris Lattner67058832012-01-25 06:48:06 +0000976 SmallVector<Constant*, 16> Elts;
977 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000978 if (!DemandedElts[i]) { // If not demanded, set to undef.
979 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000980 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +0000981 continue;
982 }
Craig Topper4c947752012-12-22 18:09:02 +0000983
Chris Lattner67058832012-01-25 06:48:06 +0000984 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000985 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000986
Chris Lattner67058832012-01-25 06:48:06 +0000987 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000988 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000989 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +0000990 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +0000991 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +0000992 }
Chris Lattner67058832012-01-25 06:48:06 +0000993 }
Craig Topper4c947752012-12-22 18:09:02 +0000994
Chris Lattner7e044912010-01-04 07:17:19 +0000995 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +0000996 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +0000997 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000998 }
Craig Topper4c947752012-12-22 18:09:02 +0000999
Chris Lattner7e044912010-01-04 07:17:19 +00001000 // Limit search depth.
1001 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +00001002 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001003
Stuart Hastings5bd18b62011-05-17 22:13:31 +00001004 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +00001005 // simplification conservatively assuming that all elements
1006 // are needed.
1007 if (!V->hasOneUse()) {
1008 // Quit if we find multiple users of a non-root value though.
1009 // They'll be handled when it's their turn to be visited by
1010 // the main instcombine process.
1011 if (Depth != 0)
1012 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +00001013 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001014
1015 // Conservatively assume that all elements are needed.
1016 DemandedElts = EltMask;
1017 }
Craig Topper4c947752012-12-22 18:09:02 +00001018
Chris Lattner7e044912010-01-04 07:17:19 +00001019 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +00001020 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +00001021
Chris Lattner7e044912010-01-04 07:17:19 +00001022 bool MadeChange = false;
1023 APInt UndefElts2(VWidth, 0);
Craig Topper23ebd952016-12-11 08:54:52 +00001024 APInt UndefElts3(VWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001025 Value *TmpV;
1026 switch (I->getOpcode()) {
1027 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001028
Chris Lattner7e044912010-01-04 07:17:19 +00001029 case Instruction::InsertElement: {
1030 // If this is a variable index, we don't know which element it overwrites.
1031 // demand exactly the same input as we produce.
1032 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +00001033 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +00001034 // Note that we can't propagate undef elt info, because we don't know
1035 // which elt is getting updated.
1036 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001037 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001038 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1039 break;
1040 }
Craig Topper4c947752012-12-22 18:09:02 +00001041
Chris Lattner7e044912010-01-04 07:17:19 +00001042 // If this is inserting an element that isn't demanded, remove this
1043 // insertelement.
1044 unsigned IdxNo = Idx->getZExtValue();
1045 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1046 Worklist.Add(I);
1047 return I->getOperand(0);
1048 }
Craig Topper4c947752012-12-22 18:09:02 +00001049
Chris Lattner7e044912010-01-04 07:17:19 +00001050 // Otherwise, the element inserted overwrites whatever was there, so the
1051 // input demanded set is simpler than the output set.
1052 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001053 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001054 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001055 UndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001056 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1057
1058 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001059 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001060 break;
1061 }
1062 case Instruction::ShuffleVector: {
1063 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Craig Topper2e18bcf2016-12-29 04:24:32 +00001064 unsigned LHSVWidth =
1065 Shuffle->getOperand(0)->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +00001066 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1067 for (unsigned i = 0; i < VWidth; i++) {
1068 if (DemandedElts[i]) {
1069 unsigned MaskVal = Shuffle->getMaskValue(i);
1070 if (MaskVal != -1u) {
1071 assert(MaskVal < LHSVWidth * 2 &&
1072 "shufflevector mask index out of range!");
1073 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001074 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001075 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001076 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001077 }
1078 }
1079 }
1080
Alexey Bataevfee90782016-09-23 09:14:08 +00001081 APInt LHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001082 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001083 LHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001084 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1085
Alexey Bataevfee90782016-09-23 09:14:08 +00001086 APInt RHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001087 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001088 RHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001089 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1090
1091 bool NewUndefElts = false;
Alexey Bataev793c9462016-09-26 13:18:59 +00001092 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1093 unsigned RHSIdx = -1u, RHSValIdx = -1u;
Alexey Bataevfee90782016-09-23 09:14:08 +00001094 bool LHSUniform = true;
1095 bool RHSUniform = true;
Chris Lattner7e044912010-01-04 07:17:19 +00001096 for (unsigned i = 0; i < VWidth; i++) {
1097 unsigned MaskVal = Shuffle->getMaskValue(i);
1098 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001099 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001100 } else if (!DemandedElts[i]) {
1101 NewUndefElts = true;
1102 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001103 } else if (MaskVal < LHSVWidth) {
Alexey Bataevfee90782016-09-23 09:14:08 +00001104 if (LHSUndefElts[MaskVal]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001105 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001106 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001107 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001108 LHSIdx = LHSIdx == -1u ? i : LHSVWidth;
1109 LHSValIdx = LHSValIdx == -1u ? MaskVal : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001110 LHSUniform = LHSUniform && (MaskVal == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001111 }
1112 } else {
Alexey Bataevfee90782016-09-23 09:14:08 +00001113 if (RHSUndefElts[MaskVal - LHSVWidth]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001114 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001115 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001116 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001117 RHSIdx = RHSIdx == -1u ? i : LHSVWidth;
1118 RHSValIdx = RHSValIdx == -1u ? MaskVal - LHSVWidth : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001119 RHSUniform = RHSUniform && (MaskVal - LHSVWidth == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001120 }
1121 }
1122 }
1123
Alexey Bataevfee90782016-09-23 09:14:08 +00001124 // Try to transform shuffle with constant vector and single element from
1125 // this constant vector to single insertelement instruction.
1126 // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
1127 // insertelement V, C[ci], ci-n
1128 if (LHSVWidth == Shuffle->getType()->getNumElements()) {
1129 Value *Op = nullptr;
1130 Constant *Value = nullptr;
1131 unsigned Idx = -1u;
1132
Craig Topper62f06e22016-12-29 05:38:31 +00001133 // Find constant vector with the single element in shuffle (LHS or RHS).
Alexey Bataevfee90782016-09-23 09:14:08 +00001134 if (LHSIdx < LHSVWidth && RHSUniform) {
1135 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1136 Op = Shuffle->getOperand(1);
Alexey Bataev793c9462016-09-26 13:18:59 +00001137 Value = CV->getOperand(LHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001138 Idx = LHSIdx;
1139 }
1140 }
1141 if (RHSIdx < LHSVWidth && LHSUniform) {
1142 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1143 Op = Shuffle->getOperand(0);
Alexey Bataev793c9462016-09-26 13:18:59 +00001144 Value = CV->getOperand(RHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001145 Idx = RHSIdx;
1146 }
1147 }
1148 // Found constant vector with single element - convert to insertelement.
1149 if (Op && Value) {
1150 Instruction *New = InsertElementInst::Create(
1151 Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx),
1152 Shuffle->getName());
1153 InsertNewInstWith(New, *Shuffle);
1154 return New;
1155 }
1156 }
Chris Lattner7e044912010-01-04 07:17:19 +00001157 if (NewUndefElts) {
1158 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001159 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001160 for (unsigned i = 0; i < VWidth; ++i) {
1161 if (UndefElts[i])
1162 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1163 else
1164 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1165 Shuffle->getMaskValue(i)));
1166 }
1167 I->setOperand(2, ConstantVector::get(Elts));
1168 MadeChange = true;
1169 }
1170 break;
1171 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001172 case Instruction::Select: {
1173 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1174 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1175 for (unsigned i = 0; i < VWidth; i++) {
Andrea Di Biagio40f59e42015-10-06 10:34:53 +00001176 Constant *CElt = CV->getAggregateElement(i);
1177 // Method isNullValue always returns false when called on a
1178 // ConstantExpr. If CElt is a ConstantExpr then skip it in order to
1179 // to avoid propagating incorrect information.
1180 if (isa<ConstantExpr>(CElt))
1181 continue;
1182 if (CElt->isNullValue())
Pete Cooperabc13af2012-07-26 23:10:24 +00001183 LeftDemanded.clearBit(i);
1184 else
1185 RightDemanded.clearBit(i);
1186 }
1187 }
1188
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001189 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded, UndefElts,
1190 Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001191 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1192
1193 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001194 UndefElts2, Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001195 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001196
Pete Cooperabc13af2012-07-26 23:10:24 +00001197 // Output elements are undefined if both are undefined.
1198 UndefElts &= UndefElts2;
1199 break;
1200 }
Chris Lattner7e044912010-01-04 07:17:19 +00001201 case Instruction::BitCast: {
1202 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001203 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001204 if (!VTy) break;
1205 unsigned InVWidth = VTy->getNumElements();
1206 APInt InputDemandedElts(InVWidth, 0);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001207 UndefElts2 = APInt(InVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001208 unsigned Ratio;
1209
1210 if (VWidth == InVWidth) {
1211 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1212 // elements as are demanded of us.
1213 Ratio = 1;
1214 InputDemandedElts = DemandedElts;
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001215 } else if ((VWidth % InVWidth) == 0) {
1216 // If the number of elements in the output is a multiple of the number of
1217 // elements in the input then an input element is live if any of the
1218 // corresponding output elements are live.
1219 Ratio = VWidth / InVWidth;
1220 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Chris Lattner7e044912010-01-04 07:17:19 +00001221 if (DemandedElts[OutIdx])
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001222 InputDemandedElts.setBit(OutIdx / Ratio);
1223 } else if ((InVWidth % VWidth) == 0) {
1224 // If the number of elements in the input is a multiple of the number of
1225 // elements in the output then an input element is live if the
1226 // corresponding output element is live.
1227 Ratio = InVWidth / VWidth;
Chris Lattner7e044912010-01-04 07:17:19 +00001228 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001229 if (DemandedElts[InIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001230 InputDemandedElts.setBit(InIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001231 } else {
1232 // Unsupported so far.
1233 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001234 }
Craig Topper4c947752012-12-22 18:09:02 +00001235
Chris Lattner7e044912010-01-04 07:17:19 +00001236 // div/rem demand all inputs, because they don't want divide by zero.
1237 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001238 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001239 if (TmpV) {
1240 I->setOperand(0, TmpV);
1241 MadeChange = true;
1242 }
Craig Topper4c947752012-12-22 18:09:02 +00001243
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001244 if (VWidth == InVWidth) {
1245 UndefElts = UndefElts2;
1246 } else if ((VWidth % InVWidth) == 0) {
1247 // If the number of elements in the output is a multiple of the number of
1248 // elements in the input then an output element is undef if the
1249 // corresponding input element is undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001250 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001251 if (UndefElts2[OutIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001252 UndefElts.setBit(OutIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001253 } else if ((InVWidth % VWidth) == 0) {
1254 // If the number of elements in the input is a multiple of the number of
1255 // elements in the output then an output element is undef if all of the
1256 // corresponding input elements are undef.
1257 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1258 APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
1259 if (SubUndef.countPopulation() == Ratio)
1260 UndefElts.setBit(OutIdx);
1261 }
1262 } else {
Chris Lattner7e044912010-01-04 07:17:19 +00001263 llvm_unreachable("Unimp");
Chris Lattner7e044912010-01-04 07:17:19 +00001264 }
1265 break;
1266 }
1267 case Instruction::And:
1268 case Instruction::Or:
1269 case Instruction::Xor:
1270 case Instruction::Add:
1271 case Instruction::Sub:
1272 case Instruction::Mul:
1273 // div/rem demand all inputs, because they don't want divide by zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001274 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1275 Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001276 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1277 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001278 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001279 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001280
Chris Lattner7e044912010-01-04 07:17:19 +00001281 // Output elements are undefined if both are undefined. Consider things
1282 // like undef&0. The result is known zero, not undef.
1283 UndefElts &= UndefElts2;
1284 break;
Pete Coopere807e452012-07-26 22:37:04 +00001285 case Instruction::FPTrunc:
1286 case Instruction::FPExt:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001287 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1288 Depth + 1);
Pete Coopere807e452012-07-26 22:37:04 +00001289 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1290 break;
Craig Topper4c947752012-12-22 18:09:02 +00001291
Chris Lattner7e044912010-01-04 07:17:19 +00001292 case Instruction::Call: {
1293 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1294 if (!II) break;
1295 switch (II->getIntrinsicID()) {
1296 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001297
Craig Topper7fc6d342016-12-11 22:32:38 +00001298 case Intrinsic::x86_xop_vfrcz_ss:
1299 case Intrinsic::x86_xop_vfrcz_sd:
1300 // The instructions for these intrinsics are speced to zero upper bits not
1301 // pass them through like other scalar intrinsics. So we shouldn't just
1302 // use Arg0 if DemandedElts[0] is clear like we do for other intrinsics.
1303 // Instead we should return a zero vector.
Craig Topper1a8a3372016-12-29 03:30:17 +00001304 if (!DemandedElts[0]) {
1305 Worklist.Add(II);
Craig Topper7fc6d342016-12-11 22:32:38 +00001306 return ConstantAggregateZero::get(II->getType());
Craig Topper1a8a3372016-12-29 03:30:17 +00001307 }
Craig Topper7fc6d342016-12-11 22:32:38 +00001308
Craig Topperac75bca2016-12-13 07:45:45 +00001309 // Only the lower element is used.
1310 DemandedElts = 1;
Craig Topper7fc6d342016-12-11 22:32:38 +00001311 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1312 UndefElts, Depth + 1);
1313 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperac75bca2016-12-13 07:45:45 +00001314
1315 // Only the lower element is undefined. The high elements are zero.
1316 UndefElts = UndefElts[0];
Craig Topper7fc6d342016-12-11 22:32:38 +00001317 break;
1318
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001319 // Unary scalar-as-vector operations that work column-wise.
Simon Pilgrim83020942016-04-24 18:23:14 +00001320 case Intrinsic::x86_sse_rcp_ss:
1321 case Intrinsic::x86_sse_rsqrt_ss:
1322 case Intrinsic::x86_sse_sqrt_ss:
1323 case Intrinsic::x86_sse2_sqrt_sd:
Simon Pilgrim83020942016-04-24 18:23:14 +00001324 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1325 UndefElts, Depth + 1);
1326 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1327
1328 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001329 if (!DemandedElts[0]) {
1330 Worklist.Add(II);
Simon Pilgrim83020942016-04-24 18:23:14 +00001331 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001332 }
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001333 // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions
1334 // checks).
Simon Pilgrim83020942016-04-24 18:23:14 +00001335 break;
1336
Craig Toppera0372de2016-12-14 03:17:27 +00001337 // Binary scalar-as-vector operations that work column-wise. The high
1338 // elements come from operand 0. The low element is a function of both
1339 // operands.
Chris Lattner7e044912010-01-04 07:17:19 +00001340 case Intrinsic::x86_sse_min_ss:
1341 case Intrinsic::x86_sse_max_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001342 case Intrinsic::x86_sse_cmp_ss:
Chris Lattner7e044912010-01-04 07:17:19 +00001343 case Intrinsic::x86_sse2_min_sd:
1344 case Intrinsic::x86_sse2_max_sd:
Craig Toppera0372de2016-12-14 03:17:27 +00001345 case Intrinsic::x86_sse2_cmp_sd: {
1346 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1347 UndefElts, Depth + 1);
1348 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1349
1350 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001351 if (!DemandedElts[0]) {
1352 Worklist.Add(II);
Craig Toppera0372de2016-12-14 03:17:27 +00001353 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001354 }
Craig Toppera0372de2016-12-14 03:17:27 +00001355
1356 // Only lower element is used for operand 1.
1357 DemandedElts = 1;
1358 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1359 UndefElts2, Depth + 1);
1360 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1361
1362 // Lower element is undefined if both lower elements are undefined.
1363 // Consider things like undef&0. The result is known zero, not undef.
1364 if (!UndefElts2[0])
1365 UndefElts.clearBit(0);
1366
1367 break;
1368 }
1369
Craig Toppereb6a20e2016-12-14 03:17:30 +00001370 // Binary scalar-as-vector operations that work column-wise. The high
1371 // elements come from operand 0 and the low element comes from operand 1.
Simon Pilgrim83020942016-04-24 18:23:14 +00001372 case Intrinsic::x86_sse41_round_ss:
Craig Toppereb6a20e2016-12-14 03:17:30 +00001373 case Intrinsic::x86_sse41_round_sd: {
1374 // Don't use the low element of operand 0.
1375 APInt DemandedElts2 = DemandedElts;
1376 DemandedElts2.clearBit(0);
1377 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001378 UndefElts, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001379 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001380
1381 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001382 if (!DemandedElts[0]) {
1383 Worklist.Add(II);
Craig Toppereb6a20e2016-12-14 03:17:30 +00001384 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001385 }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001386
1387 // Only lower element is used for operand 1.
1388 DemandedElts = 1;
Gabor Greife23efee2010-06-28 16:45:00 +00001389 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001390 UndefElts2, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001391 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001392
Craig Toppereb6a20e2016-12-14 03:17:30 +00001393 // Take the high undef elements from operand 0 and take the lower element
1394 // from operand 1.
1395 UndefElts.clearBit(0);
1396 UndefElts |= UndefElts2[0];
Chris Lattner7e044912010-01-04 07:17:19 +00001397 break;
Craig Toppereb6a20e2016-12-14 03:17:30 +00001398 }
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001399
Craig Topperdfd268d2016-12-14 05:43:05 +00001400 // Three input scalar-as-vector operations that work column-wise. The high
1401 // elements come from operand 0 and the low element is a function of all
1402 // three inputs.
Craig Topper268b3ab2016-12-14 06:06:58 +00001403 case Intrinsic::x86_avx512_mask_add_ss_round:
1404 case Intrinsic::x86_avx512_mask_div_ss_round:
1405 case Intrinsic::x86_avx512_mask_mul_ss_round:
1406 case Intrinsic::x86_avx512_mask_sub_ss_round:
1407 case Intrinsic::x86_avx512_mask_max_ss_round:
1408 case Intrinsic::x86_avx512_mask_min_ss_round:
1409 case Intrinsic::x86_avx512_mask_add_sd_round:
1410 case Intrinsic::x86_avx512_mask_div_sd_round:
1411 case Intrinsic::x86_avx512_mask_mul_sd_round:
1412 case Intrinsic::x86_avx512_mask_sub_sd_round:
1413 case Intrinsic::x86_avx512_mask_max_sd_round:
1414 case Intrinsic::x86_avx512_mask_min_sd_round:
Craig Topper23ebd952016-12-11 08:54:52 +00001415 case Intrinsic::x86_fma_vfmadd_ss:
1416 case Intrinsic::x86_fma_vfmsub_ss:
1417 case Intrinsic::x86_fma_vfnmadd_ss:
1418 case Intrinsic::x86_fma_vfnmsub_ss:
1419 case Intrinsic::x86_fma_vfmadd_sd:
1420 case Intrinsic::x86_fma_vfmsub_sd:
1421 case Intrinsic::x86_fma_vfnmadd_sd:
1422 case Intrinsic::x86_fma_vfnmsub_sd:
Craig Topperab5f3552016-12-15 03:49:45 +00001423 case Intrinsic::x86_avx512_mask_vfmadd_ss:
1424 case Intrinsic::x86_avx512_mask_vfmadd_sd:
1425 case Intrinsic::x86_avx512_maskz_vfmadd_ss:
1426 case Intrinsic::x86_avx512_maskz_vfmadd_sd:
Craig Topper23ebd952016-12-11 08:54:52 +00001427 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1428 UndefElts, Depth + 1);
1429 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperdfd268d2016-12-14 05:43:05 +00001430
1431 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001432 if (!DemandedElts[0]) {
1433 Worklist.Add(II);
Craig Topperdfd268d2016-12-14 05:43:05 +00001434 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001435 }
Craig Topperdfd268d2016-12-14 05:43:05 +00001436
1437 // Only lower element is used for operand 1 and 2.
1438 DemandedElts = 1;
Craig Topper23ebd952016-12-11 08:54:52 +00001439 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1440 UndefElts2, Depth + 1);
1441 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1442 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1443 UndefElts3, Depth + 1);
1444 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1445
Craig Topperdfd268d2016-12-14 05:43:05 +00001446 // Lower element is undefined if all three lower elements are undefined.
1447 // Consider things like undef&0. The result is known zero, not undef.
1448 if (!UndefElts2[0] || !UndefElts3[0])
1449 UndefElts.clearBit(0);
Craig Topper23ebd952016-12-11 08:54:52 +00001450
Craig Topper23ebd952016-12-11 08:54:52 +00001451 break;
1452
Craig Topperab5f3552016-12-15 03:49:45 +00001453 case Intrinsic::x86_avx512_mask3_vfmadd_ss:
1454 case Intrinsic::x86_avx512_mask3_vfmadd_sd:
1455 case Intrinsic::x86_avx512_mask3_vfmsub_ss:
1456 case Intrinsic::x86_avx512_mask3_vfmsub_sd:
1457 case Intrinsic::x86_avx512_mask3_vfnmsub_ss:
1458 case Intrinsic::x86_avx512_mask3_vfnmsub_sd:
1459 // These intrinsics get the passthru bits from operand 2.
1460 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1461 UndefElts, Depth + 1);
1462 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1463
1464 // If lowest element of a scalar op isn't used then use Arg2.
Craig Topper1a8a3372016-12-29 03:30:17 +00001465 if (!DemandedElts[0]) {
1466 Worklist.Add(II);
Craig Topperab5f3552016-12-15 03:49:45 +00001467 return II->getArgOperand(2);
Craig Topper1a8a3372016-12-29 03:30:17 +00001468 }
Craig Topperab5f3552016-12-15 03:49:45 +00001469
1470 // Only lower element is used for operand 0 and 1.
1471 DemandedElts = 1;
1472 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1473 UndefElts2, Depth + 1);
1474 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1475 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1476 UndefElts3, Depth + 1);
1477 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1478
1479 // Lower element is undefined if all three lower elements are undefined.
1480 // Consider things like undef&0. The result is known zero, not undef.
1481 if (!UndefElts2[0] || !UndefElts3[0])
1482 UndefElts.clearBit(0);
1483
1484 break;
1485
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001486 case Intrinsic::x86_sse2_pmulu_dq:
1487 case Intrinsic::x86_sse41_pmuldq:
1488 case Intrinsic::x86_avx2_pmul_dq:
Craig Topper72f2d4e2016-12-27 05:30:09 +00001489 case Intrinsic::x86_avx2_pmulu_dq:
1490 case Intrinsic::x86_avx512_pmul_dq_512:
1491 case Intrinsic::x86_avx512_pmulu_dq_512: {
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001492 Value *Op0 = II->getArgOperand(0);
1493 Value *Op1 = II->getArgOperand(1);
1494 unsigned InnerVWidth = Op0->getType()->getVectorNumElements();
1495 assert((VWidth * 2) == InnerVWidth && "Unexpected input size");
1496
1497 APInt InnerDemandedElts(InnerVWidth, 0);
1498 for (unsigned i = 0; i != VWidth; ++i)
1499 if (DemandedElts[i])
1500 InnerDemandedElts.setBit(i * 2);
1501
1502 UndefElts2 = APInt(InnerVWidth, 0);
1503 TmpV = SimplifyDemandedVectorElts(Op0, InnerDemandedElts, UndefElts2,
1504 Depth + 1);
1505 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1506
1507 UndefElts3 = APInt(InnerVWidth, 0);
1508 TmpV = SimplifyDemandedVectorElts(Op1, InnerDemandedElts, UndefElts3,
1509 Depth + 1);
1510 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1511
1512 break;
1513 }
1514
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001515 case Intrinsic::x86_sse2_packssdw_128:
1516 case Intrinsic::x86_sse2_packsswb_128:
1517 case Intrinsic::x86_sse2_packuswb_128:
1518 case Intrinsic::x86_sse41_packusdw:
1519 case Intrinsic::x86_avx2_packssdw:
1520 case Intrinsic::x86_avx2_packsswb:
1521 case Intrinsic::x86_avx2_packusdw:
Craig Topper3731f4d2017-02-16 07:35:23 +00001522 case Intrinsic::x86_avx2_packuswb:
1523 case Intrinsic::x86_avx512_packssdw_512:
1524 case Intrinsic::x86_avx512_packsswb_512:
1525 case Intrinsic::x86_avx512_packusdw_512:
1526 case Intrinsic::x86_avx512_packuswb_512: {
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001527 auto *Ty0 = II->getArgOperand(0)->getType();
1528 unsigned InnerVWidth = Ty0->getVectorNumElements();
1529 assert(VWidth == (InnerVWidth * 2) && "Unexpected input size");
1530
1531 unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128;
1532 unsigned VWidthPerLane = VWidth / NumLanes;
1533 unsigned InnerVWidthPerLane = InnerVWidth / NumLanes;
1534
1535 // Per lane, pack the elements of the first input and then the second.
1536 // e.g.
1537 // v8i16 PACK(v4i32 X, v4i32 Y) - (X[0..3],Y[0..3])
1538 // v32i8 PACK(v16i16 X, v16i16 Y) - (X[0..7],Y[0..7]),(X[8..15],Y[8..15])
1539 for (int OpNum = 0; OpNum != 2; ++OpNum) {
1540 APInt OpDemandedElts(InnerVWidth, 0);
1541 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1542 unsigned LaneIdx = Lane * VWidthPerLane;
1543 for (unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) {
1544 unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum;
1545 if (DemandedElts[Idx])
1546 OpDemandedElts.setBit((Lane * InnerVWidthPerLane) + Elt);
1547 }
1548 }
1549
1550 // Demand elements from the operand.
1551 auto *Op = II->getArgOperand(OpNum);
1552 APInt OpUndefElts(InnerVWidth, 0);
1553 TmpV = SimplifyDemandedVectorElts(Op, OpDemandedElts, OpUndefElts,
1554 Depth + 1);
1555 if (TmpV) {
1556 II->setArgOperand(OpNum, TmpV);
1557 MadeChange = true;
1558 }
1559
1560 // Pack the operand's UNDEF elements, one lane at a time.
1561 OpUndefElts = OpUndefElts.zext(VWidth);
1562 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1563 APInt LaneElts = OpUndefElts.lshr(InnerVWidthPerLane * Lane);
1564 LaneElts = LaneElts.getLoBits(InnerVWidthPerLane);
Craig Topper5f68af02017-04-23 05:18:31 +00001565 LaneElts <<= InnerVWidthPerLane * (2 * Lane + OpNum);
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001566 UndefElts |= LaneElts;
1567 }
1568 }
1569 break;
1570 }
1571
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001572 // PSHUFB
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001573 case Intrinsic::x86_ssse3_pshuf_b_128:
1574 case Intrinsic::x86_avx2_pshuf_b:
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001575 case Intrinsic::x86_avx512_pshuf_b_512:
1576 // PERMILVAR
1577 case Intrinsic::x86_avx_vpermilvar_ps:
1578 case Intrinsic::x86_avx_vpermilvar_ps_256:
1579 case Intrinsic::x86_avx512_vpermilvar_ps_512:
1580 case Intrinsic::x86_avx_vpermilvar_pd:
1581 case Intrinsic::x86_avx_vpermilvar_pd_256:
Simon Pilgrimfe2c0ed2017-01-18 14:47:49 +00001582 case Intrinsic::x86_avx512_vpermilvar_pd_512:
1583 // PERMV
1584 case Intrinsic::x86_avx2_permd:
1585 case Intrinsic::x86_avx2_permps: {
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001586 Value *Op1 = II->getArgOperand(1);
1587 TmpV = SimplifyDemandedVectorElts(Op1, DemandedElts, UndefElts,
1588 Depth + 1);
1589 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1590 break;
1591 }
1592
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001593 // SSE4A instructions leave the upper 64-bits of the 128-bit result
1594 // in an undefined state.
1595 case Intrinsic::x86_sse4a_extrq:
1596 case Intrinsic::x86_sse4a_extrqi:
1597 case Intrinsic::x86_sse4a_insertq:
1598 case Intrinsic::x86_sse4a_insertqi:
Craig Topper3a86a042017-03-19 05:49:16 +00001599 UndefElts.setHighBits(VWidth / 2);
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001600 break;
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001601 case Intrinsic::amdgcn_buffer_load:
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001602 case Intrinsic::amdgcn_buffer_load_format:
1603 case Intrinsic::amdgcn_image_sample:
1604 case Intrinsic::amdgcn_image_sample_cl:
1605 case Intrinsic::amdgcn_image_sample_d:
1606 case Intrinsic::amdgcn_image_sample_d_cl:
1607 case Intrinsic::amdgcn_image_sample_l:
1608 case Intrinsic::amdgcn_image_sample_b:
1609 case Intrinsic::amdgcn_image_sample_b_cl:
1610 case Intrinsic::amdgcn_image_sample_lz:
1611 case Intrinsic::amdgcn_image_sample_cd:
1612 case Intrinsic::amdgcn_image_sample_cd_cl:
1613
1614 case Intrinsic::amdgcn_image_sample_c:
1615 case Intrinsic::amdgcn_image_sample_c_cl:
1616 case Intrinsic::amdgcn_image_sample_c_d:
1617 case Intrinsic::amdgcn_image_sample_c_d_cl:
1618 case Intrinsic::amdgcn_image_sample_c_l:
1619 case Intrinsic::amdgcn_image_sample_c_b:
1620 case Intrinsic::amdgcn_image_sample_c_b_cl:
1621 case Intrinsic::amdgcn_image_sample_c_lz:
1622 case Intrinsic::amdgcn_image_sample_c_cd:
1623 case Intrinsic::amdgcn_image_sample_c_cd_cl:
1624
1625 case Intrinsic::amdgcn_image_sample_o:
1626 case Intrinsic::amdgcn_image_sample_cl_o:
1627 case Intrinsic::amdgcn_image_sample_d_o:
1628 case Intrinsic::amdgcn_image_sample_d_cl_o:
1629 case Intrinsic::amdgcn_image_sample_l_o:
1630 case Intrinsic::amdgcn_image_sample_b_o:
1631 case Intrinsic::amdgcn_image_sample_b_cl_o:
1632 case Intrinsic::amdgcn_image_sample_lz_o:
1633 case Intrinsic::amdgcn_image_sample_cd_o:
1634 case Intrinsic::amdgcn_image_sample_cd_cl_o:
1635
1636 case Intrinsic::amdgcn_image_sample_c_o:
1637 case Intrinsic::amdgcn_image_sample_c_cl_o:
1638 case Intrinsic::amdgcn_image_sample_c_d_o:
1639 case Intrinsic::amdgcn_image_sample_c_d_cl_o:
1640 case Intrinsic::amdgcn_image_sample_c_l_o:
1641 case Intrinsic::amdgcn_image_sample_c_b_o:
1642 case Intrinsic::amdgcn_image_sample_c_b_cl_o:
1643 case Intrinsic::amdgcn_image_sample_c_lz_o:
1644 case Intrinsic::amdgcn_image_sample_c_cd_o:
1645 case Intrinsic::amdgcn_image_sample_c_cd_cl_o:
1646
1647 case Intrinsic::amdgcn_image_getlod: {
Craig Topperd33ee1b2017-04-03 16:34:59 +00001648 if (VWidth == 1 || !DemandedElts.isMask())
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001649 return nullptr;
1650
1651 // TODO: Handle 3 vectors when supported in code gen.
1652 unsigned NewNumElts = PowerOf2Ceil(DemandedElts.countTrailingOnes());
1653 if (NewNumElts == VWidth)
1654 return nullptr;
1655
1656 Module *M = II->getParent()->getParent()->getParent();
1657 Type *EltTy = V->getType()->getVectorElementType();
1658
1659 Type *NewTy = (NewNumElts == 1) ? EltTy :
1660 VectorType::get(EltTy, NewNumElts);
1661
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001662 auto IID = II->getIntrinsicID();
1663
1664 bool IsBuffer = IID == Intrinsic::amdgcn_buffer_load ||
1665 IID == Intrinsic::amdgcn_buffer_load_format;
1666
1667 Function *NewIntrin = IsBuffer ?
1668 Intrinsic::getDeclaration(M, IID, NewTy) :
1669 // Samplers have 3 mangled types.
1670 Intrinsic::getDeclaration(M, IID,
1671 { NewTy, II->getArgOperand(0)->getType(),
1672 II->getArgOperand(1)->getType()});
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001673
1674 SmallVector<Value *, 5> Args;
1675 for (unsigned I = 0, E = II->getNumArgOperands(); I != E; ++I)
1676 Args.push_back(II->getArgOperand(I));
1677
Matt Arsenaulta3bdd8f2017-03-10 05:25:49 +00001678 IRBuilderBase::InsertPointGuard Guard(*Builder);
1679 Builder->SetInsertPoint(II);
1680
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001681 CallInst *NewCall = Builder->CreateCall(NewIntrin, Args);
1682 NewCall->takeName(II);
1683 NewCall->copyMetadata(*II);
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001684
1685 if (!IsBuffer) {
1686 ConstantInt *DMask = dyn_cast<ConstantInt>(NewCall->getArgOperand(3));
1687 if (DMask) {
1688 unsigned DMaskVal = DMask->getZExtValue() & 0xf;
1689
1690 unsigned PopCnt = 0;
1691 unsigned NewDMask = 0;
1692 for (unsigned I = 0; I < 4; ++I) {
1693 const unsigned Bit = 1 << I;
1694 if (!!(DMaskVal & Bit)) {
1695 if (++PopCnt > NewNumElts)
1696 break;
1697
1698 NewDMask |= Bit;
1699 }
1700 }
1701
1702 NewCall->setArgOperand(3, ConstantInt::get(DMask->getType(), NewDMask));
1703 }
1704 }
1705
1706
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001707 if (NewNumElts == 1) {
1708 return Builder->CreateInsertElement(UndefValue::get(V->getType()),
1709 NewCall, static_cast<uint64_t>(0));
1710 }
1711
1712 SmallVector<uint32_t, 8> EltMask;
1713 for (unsigned I = 0; I < VWidth; ++I)
1714 EltMask.push_back(I);
1715
1716 Value *Shuffle = Builder->CreateShuffleVector(
1717 NewCall, UndefValue::get(NewTy), EltMask);
1718
1719 MadeChange = true;
1720 return Shuffle;
1721 }
Chris Lattner7e044912010-01-04 07:17:19 +00001722 }
1723 break;
1724 }
1725 }
Craig Topperf40110f2014-04-25 05:29:35 +00001726 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001727}