blob: e76556afda6552e08db7073250bfd41155b18450 [file] [log] [blame]
Chris Lattner7e044912010-01-04 07:17:19 +00001//===- InstCombineSimplifyDemanded.cpp ------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains logic for simplifying instructions based on information
11// about how they are used.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carrutha9174582015-01-22 05:25:13 +000015#include "InstCombineInternal.h"
James Molloy2b21a7c2015-05-20 18:41:25 +000016#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000018#include "llvm/IR/PatternMatch.h"
Chris Lattner7e044912010-01-04 07:17:19 +000019
20using namespace llvm;
Shuxin Yang63e999e2012-12-04 00:04:54 +000021using namespace llvm::PatternMatch;
Chris Lattner7e044912010-01-04 07:17:19 +000022
Chandler Carruth964daaa2014-04-22 02:55:47 +000023#define DEBUG_TYPE "instcombine"
24
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000025/// Check to see if the specified operand of the specified instruction is a
26/// constant integer. If so, check to see if there are any bits set in the
27/// constant that are not demanded. If so, shrink the constant and return true.
Craig Topper4c947752012-12-22 18:09:02 +000028static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
Chris Lattner7e044912010-01-04 07:17:19 +000029 APInt Demanded) {
30 assert(I && "No instruction?");
31 assert(OpNo < I->getNumOperands() && "Operand index too large");
32
33 // If the operand is not a constant integer, nothing to do.
34 ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo));
35 if (!OpC) return false;
36
37 // If there are no bits set that aren't demanded, nothing to do.
Jay Foad583abbc2010-12-07 08:25:19 +000038 Demanded = Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
Chris Lattner7e044912010-01-04 07:17:19 +000039 if ((~Demanded & OpC->getValue()) == 0)
40 return false;
41
42 // This instruction is producing bits that are not demanded. Shrink the RHS.
43 Demanded &= OpC->getValue();
44 I->setOperand(OpNo, ConstantInt::get(OpC->getType(), 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.
Benjamin Kramerc321e532016-06-08 19:09:22 +000069bool InstCombiner::SimplifyDemandedBits(Use &U, const APInt &DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +000070 APInt &KnownZero, APInt &KnownOne,
71 unsigned Depth) {
David Majnemerfe58d132015-04-22 20:59:28 +000072 auto *UserI = dyn_cast<Instruction>(U.getUser());
73 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, KnownZero,
74 KnownOne, Depth, UserI);
Craig Topperf40110f2014-04-25 05:29:35 +000075 if (!NewVal) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000076 U = NewVal;
77 return true;
78}
79
80
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000081/// This function attempts to replace V with a simpler value based on the
82/// demanded bits. When this function is called, it is known that only the bits
83/// set in DemandedMask of the result of V are ever used downstream.
84/// Consequently, depending on the mask and V, it may be possible to replace V
85/// with a constant or one of its operands. In such cases, this function does
86/// the replacement and returns true. In all other cases, it returns false after
87/// analyzing the expression and setting KnownOne and known to be one in the
88/// expression. KnownZero contains all the bits that are known to be zero in the
89/// expression. These are provided to potentially allow the caller (which might
90/// recursively be SimplifyDemandedBits itself) to simplify the expression.
91/// KnownOne and KnownZero always follow the invariant that:
92/// KnownOne & KnownZero == 0.
93/// That is, a bit can't be both 1 and 0. Note that the bits in KnownOne and
94/// KnownZero may only be accurate for those bits set in DemandedMask. Note also
95/// that the bitwidth of V, DemandedMask, KnownZero and KnownOne must all be the
96/// same.
Chris Lattner7e044912010-01-04 07:17:19 +000097///
98/// This returns null if it did not change anything and it permits no
99/// simplification. This returns V itself if it did some simplification of V's
100/// operands based on the information about what bits are demanded. This returns
101/// some other non-null value if it found out that V is equal to another value
102/// in the context where the specified bits are demanded, but not for all users.
103Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
104 APInt &KnownZero, APInt &KnownOne,
Hal Finkel60db0582014-09-07 18:57:58 +0000105 unsigned Depth,
106 Instruction *CxtI) {
Craig Toppere73658d2014-04-28 04:05:08 +0000107 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000108 assert(Depth <= 6 && "Limit Search Depth");
109 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000110 Type *VTy = V->getType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000111 assert(
112 (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
113 KnownZero.getBitWidth() == BitWidth &&
114 KnownOne.getBitWidth() == BitWidth &&
115 "Value *V, DemandedMask, KnownZero and KnownOne "
116 "must have same BitWidth");
Chris Lattner7e044912010-01-04 07:17:19 +0000117 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
118 // We know all of the bits for a constant!
119 KnownOne = CI->getValue() & DemandedMask;
120 KnownZero = ~KnownOne & DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000121 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000122 }
123 if (isa<ConstantPointerNull>(V)) {
124 // We know all of the bits for a constant!
Jay Foad25a5e4c2010-12-01 08:53:58 +0000125 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000126 KnownZero = DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000127 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000128 }
129
Jay Foad25a5e4c2010-12-01 08:53:58 +0000130 KnownZero.clearAllBits();
131 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000132 if (DemandedMask == 0) { // Not demanding any bits from V.
133 if (isa<UndefValue>(V))
Craig Topperf40110f2014-04-25 05:29:35 +0000134 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000135 return UndefValue::get(VTy);
136 }
Craig Topper4c947752012-12-22 18:09:02 +0000137
Chris Lattner7e044912010-01-04 07:17:19 +0000138 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000139 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000140
Chris Lattner7e044912010-01-04 07:17:19 +0000141 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000142 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +0000143
144 Instruction *I = dyn_cast<Instruction>(V);
145 if (!I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000146 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000147 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000148 }
149
150 // If there are multiple uses of this value and we aren't at the root, then
151 // we can't do any simplifications of the operands, because DemandedMask
152 // only reflects the bits demanded by *one* of the users.
153 if (Depth != 0 && !I->hasOneUse()) {
154 // Despite the fact that we can't simplify this instruction in all User's
155 // context, we can at least compute the knownzero/knownone bits, and we can
156 // do simplifications that apply to *just* the one user if we know that
157 // this instruction has a simpler value in that context.
158 if (I->getOpcode() == Instruction::And) {
159 // If either the LHS or the RHS are Zero, the result is zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000160 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000161 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000162 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000163 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000164
Chris Lattner7e044912010-01-04 07:17:19 +0000165 // If all of the demanded bits are known 1 on one side, return the other.
166 // These bits cannot contribute to the result of the 'and' in this
167 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000168 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000169 (DemandedMask & ~LHSKnownZero))
170 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000171 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000172 (DemandedMask & ~RHSKnownZero))
173 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000174
Chris Lattner7e044912010-01-04 07:17:19 +0000175 // If all of the demanded bits in the inputs are known zeros, return zero.
176 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
177 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000178
Chris Lattner7e044912010-01-04 07:17:19 +0000179 } else if (I->getOpcode() == Instruction::Or) {
180 // We can simplify (X|Y) -> X or Y in the user's context if we know that
181 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000182
Chris Lattner7e044912010-01-04 07:17:19 +0000183 // If either the LHS or the RHS are One, the result is One.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000184 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000185 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000186 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000187 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000188
Chris Lattner7e044912010-01-04 07:17:19 +0000189 // If all of the demanded bits are known zero on one side, return the
190 // other. These bits cannot contribute to the result of the 'or' in this
191 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000192 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000193 (DemandedMask & ~LHSKnownOne))
194 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000195 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000196 (DemandedMask & ~RHSKnownOne))
197 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000198
Chris Lattner7e044912010-01-04 07:17:19 +0000199 // If all of the potentially set bits on one side are known to be set on
200 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000201 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000202 (DemandedMask & (~RHSKnownZero)))
203 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000204 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000205 (DemandedMask & (~LHSKnownZero)))
206 return I->getOperand(1);
Shuxin Yang73285932012-12-04 22:15:32 +0000207 } else if (I->getOpcode() == Instruction::Xor) {
208 // We can simplify (X^Y) -> X or Y in the user's context if we know that
209 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000210
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000211 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000212 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000213 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000214 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000215
Shuxin Yang73285932012-12-04 22:15:32 +0000216 // If all of the demanded bits are known zero on one side, return the
Craig Topper4c947752012-12-22 18:09:02 +0000217 // other.
Shuxin Yang73285932012-12-04 22:15:32 +0000218 if ((DemandedMask & RHSKnownZero) == DemandedMask)
219 return I->getOperand(0);
220 if ((DemandedMask & LHSKnownZero) == DemandedMask)
221 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000222 }
Shuxin Yang73285932012-12-04 22:15:32 +0000223
Chris Lattner7e044912010-01-04 07:17:19 +0000224 // Compute the KnownZero/KnownOne bits to simplify things downstream.
Hal Finkel60db0582014-09-07 18:57:58 +0000225 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000226 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000227 }
Craig Topper4c947752012-12-22 18:09:02 +0000228
Chris Lattner7e044912010-01-04 07:17:19 +0000229 // If this is the root being simplified, allow it to have multiple uses,
230 // just set the DemandedMask to all bits so that we can try to simplify the
231 // operands. This allows visitTruncInst (for example) to simplify the
232 // operand of a trunc without duplicating all the logic below.
233 if (Depth == 0 && !V->hasOneUse())
234 DemandedMask = APInt::getAllOnesValue(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000235
Chris Lattner7e044912010-01-04 07:17:19 +0000236 switch (I->getOpcode()) {
237 default:
Hal Finkel60db0582014-09-07 18:57:58 +0000238 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000239 break;
240 case Instruction::And:
241 // If either the LHS or the RHS are Zero, the result is zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000242 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, RHSKnownZero,
243 RHSKnownOne, Depth + 1) ||
Chris Lattner7e044912010-01-04 07:17:19 +0000244 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000245 LHSKnownZero, LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000246 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000247 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
248 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000249
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000250 // If the client is only demanding bits that we know, return the known
251 // constant.
252 if ((DemandedMask & ((RHSKnownZero | LHSKnownZero)|
253 (RHSKnownOne & LHSKnownOne))) == DemandedMask)
254 return Constant::getIntegerValue(VTy, RHSKnownOne & LHSKnownOne);
255
Chris Lattner7e044912010-01-04 07:17:19 +0000256 // If all of the demanded bits are known 1 on one side, return the other.
257 // These bits cannot contribute to the result of the 'and'.
Craig Topper4c947752012-12-22 18:09:02 +0000258 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000259 (DemandedMask & ~LHSKnownZero))
260 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000261 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000262 (DemandedMask & ~RHSKnownZero))
263 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000264
Chris Lattner7e044912010-01-04 07:17:19 +0000265 // If all of the demanded bits in the inputs are known zeros, return zero.
266 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
267 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000268
Chris Lattner7e044912010-01-04 07:17:19 +0000269 // If the RHS is a constant, see if we can simplify it.
270 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
271 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000272
Chris Lattner7e044912010-01-04 07:17:19 +0000273 // Output known-1 bits are only known if set in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000274 KnownOne = RHSKnownOne & LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000275 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000276 KnownZero = RHSKnownZero | LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000277 break;
278 case Instruction::Or:
279 // If either the LHS or the RHS are One, the result is One.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000280 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, RHSKnownZero,
281 RHSKnownOne, Depth + 1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000282 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000283 LHSKnownZero, LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000284 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000285 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
286 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
287
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000288 // If the client is only demanding bits that we know, return the known
289 // constant.
290 if ((DemandedMask & ((RHSKnownZero & LHSKnownZero)|
291 (RHSKnownOne | LHSKnownOne))) == DemandedMask)
292 return Constant::getIntegerValue(VTy, RHSKnownOne | LHSKnownOne);
293
Chris Lattner7e044912010-01-04 07:17:19 +0000294 // If all of the demanded bits are known zero on one side, return the other.
295 // These bits cannot contribute to the result of the 'or'.
Craig Topper4c947752012-12-22 18:09:02 +0000296 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000297 (DemandedMask & ~LHSKnownOne))
298 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000299 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000300 (DemandedMask & ~RHSKnownOne))
301 return I->getOperand(1);
302
303 // If all of the potentially set bits on one side are known to be set on
304 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000305 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000306 (DemandedMask & (~RHSKnownZero)))
307 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000308 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000309 (DemandedMask & (~LHSKnownZero)))
310 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000311
Chris Lattner7e044912010-01-04 07:17:19 +0000312 // If the RHS is a constant, see if we can simplify it.
313 if (ShrinkDemandedConstant(I, 1, DemandedMask))
314 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000315
Chris Lattner7e044912010-01-04 07:17:19 +0000316 // Output known-0 bits are only known if clear in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000317 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000318 // Output known-1 are known to be set if set in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000319 KnownOne = RHSKnownOne | LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000320 break;
321 case Instruction::Xor: {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000322 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, RHSKnownZero,
323 RHSKnownOne, Depth + 1) ||
324 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, LHSKnownZero,
325 LHSKnownOne, 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
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000330 // Output known-0 bits are known if clear or set in both the LHS & RHS.
331 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
332 (RHSKnownOne & LHSKnownOne);
333 // Output known-1 are known to be set if set in only one of the LHS, RHS.
334 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
335 (RHSKnownOne & LHSKnownZero);
336
337 // If the client is only demanding bits that we know, return the known
338 // constant.
339 if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
340 return Constant::getIntegerValue(VTy, IKnownOne);
341
Chris Lattner7e044912010-01-04 07:17:19 +0000342 // If all of the demanded bits are known zero on one side, return the other.
343 // These bits cannot contribute to the result of the 'xor'.
344 if ((DemandedMask & RHSKnownZero) == DemandedMask)
345 return I->getOperand(0);
346 if ((DemandedMask & LHSKnownZero) == DemandedMask)
347 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000348
Chris Lattner7e044912010-01-04 07:17:19 +0000349 // If all of the demanded bits are known to be zero on one side or the
350 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000351 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Chris Lattner7e044912010-01-04 07:17:19 +0000352 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
Craig Topper4c947752012-12-22 18:09:02 +0000353 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000354 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
355 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000356 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000357 }
Craig Topper4c947752012-12-22 18:09:02 +0000358
Chris Lattner7e044912010-01-04 07:17:19 +0000359 // If all of the demanded bits on one side are known, and all of the set
360 // bits on that side are also known to be set on the other side, turn this
361 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000362 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper4c947752012-12-22 18:09:02 +0000363 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
Chris Lattner7e044912010-01-04 07:17:19 +0000364 // all known
365 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
366 Constant *AndC = Constant::getIntegerValue(VTy,
367 ~RHSKnownOne & DemandedMask);
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000368 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000369 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000370 }
371 }
Craig Topper4c947752012-12-22 18:09:02 +0000372
Chris Lattner7e044912010-01-04 07:17:19 +0000373 // If the RHS is a constant, see if we can simplify it.
374 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
375 if (ShrinkDemandedConstant(I, 1, DemandedMask))
376 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000377
Chris Lattner7e044912010-01-04 07:17:19 +0000378 // If our LHS is an 'and' and if it has one use, and if any of the bits we
379 // are flipping are known to be set, then the xor is just resetting those
380 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
381 // simplifying both of them.
382 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
383 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
384 isa<ConstantInt>(I->getOperand(1)) &&
385 isa<ConstantInt>(LHSInst->getOperand(1)) &&
386 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
387 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
388 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
389 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000390
Chris Lattner7e044912010-01-04 07:17:19 +0000391 Constant *AndC =
392 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000393 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000394 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000395
Chris Lattner7e044912010-01-04 07:17:19 +0000396 Constant *XorC =
397 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000398 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000399 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000400 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000401
402 // Output known-0 bits are known if clear or set in both the LHS & RHS.
403 KnownZero= (RHSKnownZero & LHSKnownZero) | (RHSKnownOne & LHSKnownOne);
404 // Output known-1 are known to be set if set in only one of the LHS, RHS.
405 KnownOne = (RHSKnownZero & LHSKnownOne) | (RHSKnownOne & LHSKnownZero);
Chris Lattner7e044912010-01-04 07:17:19 +0000406 break;
407 }
408 case Instruction::Select:
James Molloy2b21a7c2015-05-20 18:41:25 +0000409 // If this is a select as part of a min/max pattern, don't simplify any
410 // further in case we break the structure.
411 Value *LHS, *RHS;
James Molloy134bec22015-08-11 09:12:57 +0000412 if (matchSelectPattern(I, LHS, RHS).Flavor != SPF_UNKNOWN)
James Molloy2b21a7c2015-05-20 18:41:25 +0000413 return nullptr;
Simon Pilgrim61116dd2015-09-17 20:32:45 +0000414
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000415 if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask, RHSKnownZero,
416 RHSKnownOne, Depth + 1) ||
417 SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, LHSKnownZero,
418 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000419 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000420 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
421 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
422
Chris Lattner7e044912010-01-04 07:17:19 +0000423 // If the operands are constants, see if we can simplify them.
424 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
425 ShrinkDemandedConstant(I, 2, DemandedMask))
426 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000427
Chris Lattner7e044912010-01-04 07:17:19 +0000428 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000429 KnownOne = RHSKnownOne & LHSKnownOne;
430 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000431 break;
432 case Instruction::Trunc: {
433 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000434 DemandedMask = DemandedMask.zext(truncBf);
435 KnownZero = KnownZero.zext(truncBf);
436 KnownOne = KnownOne.zext(truncBf);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000437 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, KnownZero,
438 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000439 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000440 DemandedMask = DemandedMask.trunc(BitWidth);
441 KnownZero = KnownZero.trunc(BitWidth);
442 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000443 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000444 break;
445 }
446 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000447 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000448 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000449
Chris Lattner229907c2011-07-18 04:54:35 +0000450 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
451 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000452 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
453 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
454 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000455 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000456 } else
457 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000458 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000459 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000460 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000461 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000462
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000463 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, KnownZero,
464 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000465 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000466 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000467 break;
468 case Instruction::ZExt: {
469 // Compute the bits in the result that are not present in the input.
470 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000471
Jay Foad583abbc2010-12-07 08:25:19 +0000472 DemandedMask = DemandedMask.trunc(SrcBitWidth);
473 KnownZero = KnownZero.trunc(SrcBitWidth);
474 KnownOne = KnownOne.trunc(SrcBitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000475 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, KnownZero,
476 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000477 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000478 DemandedMask = DemandedMask.zext(BitWidth);
479 KnownZero = KnownZero.zext(BitWidth);
480 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000481 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000482 // The top bits are known to be zero.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000483 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000484 break;
485 }
486 case Instruction::SExt: {
487 // Compute the bits in the result that are not present in the input.
488 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000489
490 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000491 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
492
493 APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
494 // If any of the sign extended bits are demanded, we know that the sign
495 // bit is demanded.
496 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000497 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000498
Jay Foad583abbc2010-12-07 08:25:19 +0000499 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
500 KnownZero = KnownZero.trunc(SrcBitWidth);
501 KnownOne = KnownOne.trunc(SrcBitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000502 if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits, KnownZero,
503 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000504 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000505 InputDemandedBits = InputDemandedBits.zext(BitWidth);
506 KnownZero = KnownZero.zext(BitWidth);
507 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000508 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
509
Chris Lattner7e044912010-01-04 07:17:19 +0000510 // If the sign bit of the input is known set or clear, then we know the
511 // top bits of the result.
512
513 // If the input sign bit is known zero, or if the NewBits are not demanded
514 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000515 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000516 // Convert to ZExt cast
517 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000518 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000519 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
520 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000521 }
522 break;
523 }
Matthias Braune48484c2015-04-30 22:05:30 +0000524 case Instruction::Add:
525 case Instruction::Sub: {
526 /// If the high-bits of an ADD/SUB are not demanded, then we do not care
527 /// about the high bits of the operands.
Chris Lattner7e044912010-01-04 07:17:19 +0000528 unsigned NLZ = DemandedMask.countLeadingZeros();
Matthias Braune48484c2015-04-30 22:05:30 +0000529 if (NLZ > 0) {
530 // Right fill the mask of bits for this ADD/SUB to demand the most
Chris Lattner7e044912010-01-04 07:17:19 +0000531 // significant bit and all those below it.
Chris Lattner7e044912010-01-04 07:17:19 +0000532 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
533 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000534 LHSKnownZero, LHSKnownOne, Depth + 1) ||
Matthias Braune48484c2015-04-30 22:05:30 +0000535 ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
Chris Lattner7e044912010-01-04 07:17:19 +0000536 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
David Majnemer7d0e99c2015-04-22 22:42:05 +0000537 LHSKnownZero, LHSKnownOne, Depth + 1)) {
Matthias Braune48484c2015-04-30 22:05:30 +0000538 // Disable the nsw and nuw flags here: We can no longer guarantee that
539 // we won't wrap after simplification. Removing the nsw/nuw flags is
540 // legal here because the top bit is not demanded.
541 BinaryOperator &BinOP = *cast<BinaryOperator>(I);
542 BinOP.setHasNoSignedWrap(false);
543 BinOP.setHasNoUnsignedWrap(false);
Chris Lattner7e044912010-01-04 07:17:19 +0000544 return I;
David Majnemer7d0e99c2015-04-22 22:42:05 +0000545 }
Chris Lattner7e044912010-01-04 07:17:19 +0000546 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000547
Matthias Braune48484c2015-04-30 22:05:30 +0000548 // Otherwise just hand the add/sub off to computeKnownBits to fill in
Chris Lattner7e044912010-01-04 07:17:19 +0000549 // the known zeros and ones.
Hal Finkel60db0582014-09-07 18:57:58 +0000550 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000551 break;
Matthias Braune48484c2015-04-30 22:05:30 +0000552 }
Chris Lattner7e044912010-01-04 07:17:19 +0000553 case Instruction::Shl:
554 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000555 {
556 Value *VarX; ConstantInt *C1;
557 if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
558 Instruction *Shr = cast<Instruction>(I->getOperand(0));
559 Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
560 KnownZero, KnownOne);
561 if (R)
562 return R;
563 }
564 }
565
Chris Lattner768003c2011-02-10 05:09:34 +0000566 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000567 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000568
Chris Lattner768003c2011-02-10 05:09:34 +0000569 // If the shift is NUW/NSW, then it does demand the high bits.
570 ShlOperator *IOp = cast<ShlOperator>(I);
571 if (IOp->hasNoSignedWrap())
572 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
573 else if (IOp->hasNoUnsignedWrap())
574 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000575
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000576 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, KnownZero,
577 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000578 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000579 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
580 KnownZero <<= ShiftAmt;
581 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000582 // low bits known zero.
583 if (ShiftAmt)
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000584 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000585 }
586 break;
587 case Instruction::LShr:
588 // For a logical shift right
589 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000590 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000591
Chris Lattner7e044912010-01-04 07:17:19 +0000592 // Unsigned shift right.
593 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000594
Chris Lattner768003c2011-02-10 05:09:34 +0000595 // If the shift is exact, then it does demand the low bits (and knows that
596 // they are zero).
597 if (cast<LShrOperator>(I)->isExact())
598 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000599
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000600 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, KnownZero,
601 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000602 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000603 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
604 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
605 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000606 if (ShiftAmt) {
607 // Compute the new bits that are at the top now.
608 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000609 KnownZero |= HighBits; // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000610 }
611 }
612 break;
613 case Instruction::AShr:
614 // If this is an arithmetic shift right and only the low-bit is set, we can
615 // always convert this into a logical shr, even if the shift amount is
616 // variable. The low bit of the shift cannot be an input sign bit unless
617 // the shift amount is >= the size of the datatype, which is undefined.
618 if (DemandedMask == 1) {
619 // Perform the logical shift right.
620 Instruction *NewVal = BinaryOperator::CreateLShr(
621 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000622 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000623 }
Chris Lattner7e044912010-01-04 07:17:19 +0000624
625 // If the sign bit is the only bit demanded by this ashr, then there is no
626 // need to do it, the shift doesn't change the high bit.
627 if (DemandedMask.isSignBit())
628 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000629
Chris Lattner7e044912010-01-04 07:17:19 +0000630 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000631 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000632
Chris Lattner7e044912010-01-04 07:17:19 +0000633 // Signed shift right.
634 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
635 // If any of the "high bits" are demanded, we should set the sign bit as
636 // demanded.
637 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000638 DemandedMaskIn.setBit(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000639
Chris Lattner768003c2011-02-10 05:09:34 +0000640 // If the shift is exact, then it does demand the low bits (and knows that
641 // they are zero).
642 if (cast<AShrOperator>(I)->isExact())
643 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000644
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000645 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, KnownZero,
646 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000647 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000648 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000649 // Compute the new bits that are at the top now.
650 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000651 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
652 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000653
Chris Lattner7e044912010-01-04 07:17:19 +0000654 // Handle the sign bits.
655 APInt SignBit(APInt::getSignBit(BitWidth));
656 // Adjust to where it is now in the mask.
Craig Topper4c947752012-12-22 18:09:02 +0000657 SignBit = APIntOps::lshr(SignBit, ShiftAmt);
658
Chris Lattner7e044912010-01-04 07:17:19 +0000659 // If the input sign bit is known to be zero, or if none of the top bits
660 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000661 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Chris Lattner7e044912010-01-04 07:17:19 +0000662 (HighBits & ~DemandedMask) == HighBits) {
663 // Perform the logical shift right.
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000664 BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
665 SA, I->getName());
666 NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000667 return InsertNewInstWith(NewVal, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000668 } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
669 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000670 }
671 }
672 break;
673 case Instruction::SRem:
674 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000675 // X % -1 demands all the bits because we don't want to introduce
676 // INT_MIN % -1 (== undef) by accident.
677 if (Rem->isAllOnesValue())
678 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000679 APInt RA = Rem->getValue().abs();
680 if (RA.isPowerOf2()) {
681 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
682 return I->getOperand(0);
683
684 APInt LowBits = RA - 1;
685 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000686 if (SimplifyDemandedBits(I->getOperandUse(0), Mask2, LHSKnownZero,
687 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000688 return I;
689
Duncan Sands3a48b872010-01-28 17:22:42 +0000690 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000691 KnownZero = LHSKnownZero & LowBits;
692 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000693
Duncan Sands3a48b872010-01-28 17:22:42 +0000694 // If LHS is non-negative or has all low bits zero, then the upper bits
695 // are all zero.
696 if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
697 KnownZero |= ~LowBits;
698
699 // If LHS is negative and not all low bits are zero, then the upper bits
700 // are all one.
701 if (LHSKnownOne[BitWidth-1] && ((LHSKnownOne & LowBits) != 0))
702 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000703
Craig Topper4c947752012-12-22 18:09:02 +0000704 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000705 }
706 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000707
708 // The sign bit is the LHS's sign bit, except when the result of the
709 // remainder is zero.
710 if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
Nick Lewyckye4679792011-03-07 01:50:10 +0000711 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000712 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000713 CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000714 // If it's known zero, our sign bit is also zero.
715 if (LHSKnownZero.isNegative())
Benjamin Kramer21b972a2013-05-09 16:32:32 +0000716 KnownZero.setBit(KnownZero.getBitWidth() - 1);
Nick Lewyckye4679792011-03-07 01:50:10 +0000717 }
Chris Lattner7e044912010-01-04 07:17:19 +0000718 break;
719 case Instruction::URem: {
720 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
721 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000722 if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes, KnownZero2,
723 KnownOne2, Depth + 1) ||
724 SimplifyDemandedBits(I->getOperandUse(1), AllOnes, KnownZero2,
725 KnownOne2, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000726 return I;
727
728 unsigned Leaders = KnownZero2.countLeadingOnes();
729 Leaders = std::max(Leaders,
730 KnownZero2.countLeadingOnes());
731 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
732 break;
733 }
734 case Instruction::Call:
735 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
736 switch (II->getIntrinsicID()) {
737 default: break;
738 case Intrinsic::bswap: {
739 // If the only bits demanded come from one byte of the bswap result,
740 // just shift the input byte into position to eliminate the bswap.
741 unsigned NLZ = DemandedMask.countLeadingZeros();
742 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000743
Chris Lattner7e044912010-01-04 07:17:19 +0000744 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
745 // we need all the bits down to bit 8. Likewise, round NLZ. If we
746 // have 14 leading zeros, round to 8.
747 NLZ &= ~7;
748 NTZ &= ~7;
749 // If we need exactly one byte, we can do this transformation.
750 if (BitWidth-NLZ-NTZ == 8) {
751 unsigned ResultBit = NTZ;
752 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000753
Chris Lattner7e044912010-01-04 07:17:19 +0000754 // Replace this with either a left or right shift to get the byte into
755 // the right place.
756 Instruction *NewVal;
757 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000758 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000759 ConstantInt::get(I->getType(), InputBit-ResultBit));
760 else
Gabor Greif79430172010-06-24 12:35:13 +0000761 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000762 ConstantInt::get(I->getType(), ResultBit-InputBit));
763 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000764 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000765 }
Craig Topper4c947752012-12-22 18:09:02 +0000766
Chris Lattner7e044912010-01-04 07:17:19 +0000767 // TODO: Could compute known zero/one bits based on the input.
768 break;
769 }
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000770 case Intrinsic::x86_mmx_pmovmskb:
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000771 case Intrinsic::x86_sse_movmsk_ps:
772 case Intrinsic::x86_sse2_movmsk_pd:
773 case Intrinsic::x86_sse2_pmovmskb_128:
774 case Intrinsic::x86_avx_movmsk_ps_256:
775 case Intrinsic::x86_avx_movmsk_pd_256:
776 case Intrinsic::x86_avx2_pmovmskb: {
777 // MOVMSK copies the vector elements' sign bits to the low bits
778 // and zeros the high bits.
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000779 unsigned ArgWidth;
780 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
781 ArgWidth = 8; // Arg is x86_mmx, but treated as <8 x i8>.
782 } else {
783 auto Arg = II->getArgOperand(0);
784 auto ArgType = cast<VectorType>(Arg->getType());
785 ArgWidth = ArgType->getNumElements();
786 }
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000787
788 // If we don't need any of low bits then return zero,
789 // we know that DemandedMask is non-zero already.
790 APInt DemandedElts = DemandedMask.zextOrTrunc(ArgWidth);
791 if (DemandedElts == 0)
792 return ConstantInt::getNullValue(VTy);
793
Ahmed Bougacha17482a52016-04-28 14:36:07 +0000794 // We know that the upper bits are set to zero.
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000795 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - ArgWidth);
796 return nullptr;
797 }
Chad Rosierb3628842011-05-26 23:13:19 +0000798 case Intrinsic::x86_sse42_crc32_64_64:
Evan Chenge8d2e9e2011-05-20 00:54:37 +0000799 KnownZero = APInt::getHighBitsSet(64, 32);
Craig Topperf40110f2014-04-25 05:29:35 +0000800 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000801 }
802 }
Hal Finkel60db0582014-09-07 18:57:58 +0000803 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000804 break;
805 }
Craig Topper4c947752012-12-22 18:09:02 +0000806
Chris Lattner7e044912010-01-04 07:17:19 +0000807 // If the client is only demanding bits that we know, return the known
808 // constant.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000809 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
810 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000811 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000812}
813
Shuxin Yang63e999e2012-12-04 00:04:54 +0000814/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
815/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
816/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
817/// of "C2-C1".
818///
819/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
820/// ..., bn}, without considering the specific value X is holding.
821/// This transformation is legal iff one of following conditions is hold:
822/// 1) All the bit in S are 0, in this case E1 == E2.
823/// 2) We don't care those bits in S, per the input DemandedMask.
824/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
825/// rest bits.
826///
827/// Currently we only test condition 2).
828///
829/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
830/// not successful.
831Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
Benjamin Kramerc321e532016-06-08 19:09:22 +0000832 Instruction *Shl,
833 const APInt &DemandedMask,
834 APInt &KnownZero,
835 APInt &KnownOne) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000836
Benjamin Kramer010f1082013-08-30 14:35:35 +0000837 const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
838 const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
839 if (!ShlOp1 || !ShrOp1)
Craig Topperf40110f2014-04-25 05:29:35 +0000840 return nullptr; // Noop.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000841
842 Value *VarX = Shr->getOperand(0);
843 Type *Ty = VarX->getType();
844 unsigned BitWidth = Ty->getIntegerBitWidth();
845 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000846 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000847
848 unsigned ShlAmt = ShlOp1.getZExtValue();
849 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000850
851 KnownOne.clearAllBits();
852 KnownZero = APInt::getBitsSet(KnownZero.getBitWidth(), 0, ShlAmt-1);
853 KnownZero &= DemandedMask;
854
Benjamin Kramer010f1082013-08-30 14:35:35 +0000855 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
856 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000857
858 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
859 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
860 (BitMask1.ashr(ShrAmt) << ShlAmt);
861
862 if (ShrAmt <= ShlAmt) {
863 BitMask2 <<= (ShlAmt - ShrAmt);
864 } else {
865 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
866 BitMask2.ashr(ShrAmt - ShlAmt);
867 }
868
869 // Check if condition-2 (see the comment to this function) is satified.
870 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
871 if (ShrAmt == ShlAmt)
872 return VarX;
873
874 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000875 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000876
877 BinaryOperator *New;
878 if (ShrAmt < ShlAmt) {
879 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
880 New = BinaryOperator::CreateShl(VarX, Amt);
881 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
882 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
883 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
884 } else {
885 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000886 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
887 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000888 if (cast<BinaryOperator>(Shr)->isExact())
889 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000890 }
891
892 return InsertNewInstWith(New, *Shl);
893 }
894
Craig Topperf40110f2014-04-25 05:29:35 +0000895 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000896}
Chris Lattner7e044912010-01-04 07:17:19 +0000897
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +0000898/// The specified value produces a vector with any number of elements.
899/// DemandedElts contains the set of elements that are actually used by the
900/// caller. This method analyzes which elements of the operand are undef and
901/// returns that information in UndefElts.
Chris Lattner7e044912010-01-04 07:17:19 +0000902///
903/// If the information about demanded elements can be used to simplify the
904/// operation, the operation is simplified, then the resultant value is
905/// returned. This returns null if no change was made.
906Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000907 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000908 unsigned Depth) {
Sanjay Patel9190b4a2016-04-29 20:54:56 +0000909 unsigned VWidth = V->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +0000910 APInt EltMask(APInt::getAllOnesValue(VWidth));
911 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
912
913 if (isa<UndefValue>(V)) {
914 // If the entire vector is undefined, just return this info.
915 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000916 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000917 }
Craig Topper4c947752012-12-22 18:09:02 +0000918
Chris Lattnerb22423c2010-02-08 23:56:03 +0000919 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000920 UndefElts = EltMask;
921 return UndefValue::get(V->getType());
922 }
923
924 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000925
Chris Lattner67058832012-01-25 06:48:06 +0000926 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
927 if (Constant *C = dyn_cast<Constant>(V)) {
928 // Check if this is identity. If so, return 0 since we are not simplifying
929 // anything.
930 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000931 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000932
Chris Lattner229907c2011-07-18 04:54:35 +0000933 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000934 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000935
Chris Lattner67058832012-01-25 06:48:06 +0000936 SmallVector<Constant*, 16> Elts;
937 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000938 if (!DemandedElts[i]) { // If not demanded, set to undef.
939 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000940 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +0000941 continue;
942 }
Craig Topper4c947752012-12-22 18:09:02 +0000943
Chris Lattner67058832012-01-25 06:48:06 +0000944 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000945 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000946
Chris Lattner67058832012-01-25 06:48:06 +0000947 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000948 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000949 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +0000950 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +0000951 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +0000952 }
Chris Lattner67058832012-01-25 06:48:06 +0000953 }
Craig Topper4c947752012-12-22 18:09:02 +0000954
Chris Lattner7e044912010-01-04 07:17:19 +0000955 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +0000956 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +0000957 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000958 }
Craig Topper4c947752012-12-22 18:09:02 +0000959
Chris Lattner7e044912010-01-04 07:17:19 +0000960 // Limit search depth.
961 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +0000962 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000963
Stuart Hastings5bd18b62011-05-17 22:13:31 +0000964 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +0000965 // simplification conservatively assuming that all elements
966 // are needed.
967 if (!V->hasOneUse()) {
968 // Quit if we find multiple users of a non-root value though.
969 // They'll be handled when it's their turn to be visited by
970 // the main instcombine process.
971 if (Depth != 0)
972 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +0000973 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000974
975 // Conservatively assume that all elements are needed.
976 DemandedElts = EltMask;
977 }
Craig Topper4c947752012-12-22 18:09:02 +0000978
Chris Lattner7e044912010-01-04 07:17:19 +0000979 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +0000980 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +0000981
Chris Lattner7e044912010-01-04 07:17:19 +0000982 bool MadeChange = false;
983 APInt UndefElts2(VWidth, 0);
Craig Topper23ebd952016-12-11 08:54:52 +0000984 APInt UndefElts3(VWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +0000985 Value *TmpV;
986 switch (I->getOpcode()) {
987 default: break;
Craig Topper4c947752012-12-22 18:09:02 +0000988
Chris Lattner7e044912010-01-04 07:17:19 +0000989 case Instruction::InsertElement: {
990 // If this is a variable index, we don't know which element it overwrites.
991 // demand exactly the same input as we produce.
992 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +0000993 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +0000994 // Note that we can't propagate undef elt info, because we don't know
995 // which elt is getting updated.
996 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000997 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +0000998 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
999 break;
1000 }
Craig Topper4c947752012-12-22 18:09:02 +00001001
Chris Lattner7e044912010-01-04 07:17:19 +00001002 // If this is inserting an element that isn't demanded, remove this
1003 // insertelement.
1004 unsigned IdxNo = Idx->getZExtValue();
1005 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1006 Worklist.Add(I);
1007 return I->getOperand(0);
1008 }
Craig Topper4c947752012-12-22 18:09:02 +00001009
Chris Lattner7e044912010-01-04 07:17:19 +00001010 // Otherwise, the element inserted overwrites whatever was there, so the
1011 // input demanded set is simpler than the output set.
1012 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001013 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001014 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001015 UndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001016 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1017
1018 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001019 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001020 break;
1021 }
1022 case Instruction::ShuffleVector: {
1023 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Craig Topper2e18bcf2016-12-29 04:24:32 +00001024 unsigned LHSVWidth =
1025 Shuffle->getOperand(0)->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +00001026 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1027 for (unsigned i = 0; i < VWidth; i++) {
1028 if (DemandedElts[i]) {
1029 unsigned MaskVal = Shuffle->getMaskValue(i);
1030 if (MaskVal != -1u) {
1031 assert(MaskVal < LHSVWidth * 2 &&
1032 "shufflevector mask index out of range!");
1033 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001034 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001035 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001036 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001037 }
1038 }
1039 }
1040
Alexey Bataevfee90782016-09-23 09:14:08 +00001041 APInt LHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001042 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001043 LHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001044 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1045
Alexey Bataevfee90782016-09-23 09:14:08 +00001046 APInt RHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001047 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001048 RHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001049 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1050
1051 bool NewUndefElts = false;
Alexey Bataev793c9462016-09-26 13:18:59 +00001052 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1053 unsigned RHSIdx = -1u, RHSValIdx = -1u;
Alexey Bataevfee90782016-09-23 09:14:08 +00001054 bool LHSUniform = true;
1055 bool RHSUniform = true;
Chris Lattner7e044912010-01-04 07:17:19 +00001056 for (unsigned i = 0; i < VWidth; i++) {
1057 unsigned MaskVal = Shuffle->getMaskValue(i);
1058 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001059 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001060 } else if (!DemandedElts[i]) {
1061 NewUndefElts = true;
1062 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001063 } else if (MaskVal < LHSVWidth) {
Alexey Bataevfee90782016-09-23 09:14:08 +00001064 if (LHSUndefElts[MaskVal]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001065 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001066 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001067 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001068 LHSIdx = LHSIdx == -1u ? i : LHSVWidth;
1069 LHSValIdx = LHSValIdx == -1u ? MaskVal : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001070 LHSUniform = LHSUniform && (MaskVal == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001071 }
1072 } else {
Alexey Bataevfee90782016-09-23 09:14:08 +00001073 if (RHSUndefElts[MaskVal - LHSVWidth]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001074 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001075 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001076 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001077 RHSIdx = RHSIdx == -1u ? i : LHSVWidth;
1078 RHSValIdx = RHSValIdx == -1u ? MaskVal - LHSVWidth : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001079 RHSUniform = RHSUniform && (MaskVal - LHSVWidth == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001080 }
1081 }
1082 }
1083
Alexey Bataevfee90782016-09-23 09:14:08 +00001084 // Try to transform shuffle with constant vector and single element from
1085 // this constant vector to single insertelement instruction.
1086 // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
1087 // insertelement V, C[ci], ci-n
1088 if (LHSVWidth == Shuffle->getType()->getNumElements()) {
1089 Value *Op = nullptr;
1090 Constant *Value = nullptr;
1091 unsigned Idx = -1u;
1092
1093 // Find constant vector wigth the single element in shuffle (LHS or RHS).
1094 if (LHSIdx < LHSVWidth && RHSUniform) {
1095 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1096 Op = Shuffle->getOperand(1);
Alexey Bataev793c9462016-09-26 13:18:59 +00001097 Value = CV->getOperand(LHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001098 Idx = LHSIdx;
1099 }
1100 }
1101 if (RHSIdx < LHSVWidth && LHSUniform) {
1102 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1103 Op = Shuffle->getOperand(0);
Alexey Bataev793c9462016-09-26 13:18:59 +00001104 Value = CV->getOperand(RHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001105 Idx = RHSIdx;
1106 }
1107 }
1108 // Found constant vector with single element - convert to insertelement.
1109 if (Op && Value) {
1110 Instruction *New = InsertElementInst::Create(
1111 Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx),
1112 Shuffle->getName());
1113 InsertNewInstWith(New, *Shuffle);
1114 return New;
1115 }
1116 }
Chris Lattner7e044912010-01-04 07:17:19 +00001117 if (NewUndefElts) {
1118 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001119 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001120 for (unsigned i = 0; i < VWidth; ++i) {
1121 if (UndefElts[i])
1122 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1123 else
1124 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1125 Shuffle->getMaskValue(i)));
1126 }
1127 I->setOperand(2, ConstantVector::get(Elts));
1128 MadeChange = true;
1129 }
1130 break;
1131 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001132 case Instruction::Select: {
1133 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1134 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1135 for (unsigned i = 0; i < VWidth; i++) {
Andrea Di Biagio40f59e42015-10-06 10:34:53 +00001136 Constant *CElt = CV->getAggregateElement(i);
1137 // Method isNullValue always returns false when called on a
1138 // ConstantExpr. If CElt is a ConstantExpr then skip it in order to
1139 // to avoid propagating incorrect information.
1140 if (isa<ConstantExpr>(CElt))
1141 continue;
1142 if (CElt->isNullValue())
Pete Cooperabc13af2012-07-26 23:10:24 +00001143 LeftDemanded.clearBit(i);
1144 else
1145 RightDemanded.clearBit(i);
1146 }
1147 }
1148
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001149 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded, UndefElts,
1150 Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001151 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1152
1153 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001154 UndefElts2, Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001155 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001156
Pete Cooperabc13af2012-07-26 23:10:24 +00001157 // Output elements are undefined if both are undefined.
1158 UndefElts &= UndefElts2;
1159 break;
1160 }
Chris Lattner7e044912010-01-04 07:17:19 +00001161 case Instruction::BitCast: {
1162 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001163 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001164 if (!VTy) break;
1165 unsigned InVWidth = VTy->getNumElements();
1166 APInt InputDemandedElts(InVWidth, 0);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001167 UndefElts2 = APInt(InVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001168 unsigned Ratio;
1169
1170 if (VWidth == InVWidth) {
1171 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1172 // elements as are demanded of us.
1173 Ratio = 1;
1174 InputDemandedElts = DemandedElts;
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001175 } else if ((VWidth % InVWidth) == 0) {
1176 // If the number of elements in the output is a multiple of the number of
1177 // elements in the input then an input element is live if any of the
1178 // corresponding output elements are live.
1179 Ratio = VWidth / InVWidth;
1180 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Chris Lattner7e044912010-01-04 07:17:19 +00001181 if (DemandedElts[OutIdx])
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001182 InputDemandedElts.setBit(OutIdx / Ratio);
1183 } else if ((InVWidth % VWidth) == 0) {
1184 // If the number of elements in the input is a multiple of the number of
1185 // elements in the output then an input element is live if the
1186 // corresponding output element is live.
1187 Ratio = InVWidth / VWidth;
Chris Lattner7e044912010-01-04 07:17:19 +00001188 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001189 if (DemandedElts[InIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001190 InputDemandedElts.setBit(InIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001191 } else {
1192 // Unsupported so far.
1193 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001194 }
Craig Topper4c947752012-12-22 18:09:02 +00001195
Chris Lattner7e044912010-01-04 07:17:19 +00001196 // div/rem demand all inputs, because they don't want divide by zero.
1197 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001198 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001199 if (TmpV) {
1200 I->setOperand(0, TmpV);
1201 MadeChange = true;
1202 }
Craig Topper4c947752012-12-22 18:09:02 +00001203
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001204 if (VWidth == InVWidth) {
1205 UndefElts = UndefElts2;
1206 } else if ((VWidth % InVWidth) == 0) {
1207 // If the number of elements in the output is a multiple of the number of
1208 // elements in the input then an output element is undef if the
1209 // corresponding input element is undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001210 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001211 if (UndefElts2[OutIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001212 UndefElts.setBit(OutIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001213 } else if ((InVWidth % VWidth) == 0) {
1214 // If the number of elements in the input is a multiple of the number of
1215 // elements in the output then an output element is undef if all of the
1216 // corresponding input elements are undef.
1217 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1218 APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
1219 if (SubUndef.countPopulation() == Ratio)
1220 UndefElts.setBit(OutIdx);
1221 }
1222 } else {
Chris Lattner7e044912010-01-04 07:17:19 +00001223 llvm_unreachable("Unimp");
Chris Lattner7e044912010-01-04 07:17:19 +00001224 }
1225 break;
1226 }
1227 case Instruction::And:
1228 case Instruction::Or:
1229 case Instruction::Xor:
1230 case Instruction::Add:
1231 case Instruction::Sub:
1232 case Instruction::Mul:
1233 // div/rem demand all inputs, because they don't want divide by zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001234 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1235 Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001236 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1237 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001238 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001239 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001240
Chris Lattner7e044912010-01-04 07:17:19 +00001241 // Output elements are undefined if both are undefined. Consider things
1242 // like undef&0. The result is known zero, not undef.
1243 UndefElts &= UndefElts2;
1244 break;
Pete Coopere807e452012-07-26 22:37:04 +00001245 case Instruction::FPTrunc:
1246 case Instruction::FPExt:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001247 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1248 Depth + 1);
Pete Coopere807e452012-07-26 22:37:04 +00001249 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1250 break;
Craig Topper4c947752012-12-22 18:09:02 +00001251
Chris Lattner7e044912010-01-04 07:17:19 +00001252 case Instruction::Call: {
1253 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1254 if (!II) break;
1255 switch (II->getIntrinsicID()) {
1256 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001257
Craig Topper7fc6d342016-12-11 22:32:38 +00001258 case Intrinsic::x86_xop_vfrcz_ss:
1259 case Intrinsic::x86_xop_vfrcz_sd:
1260 // The instructions for these intrinsics are speced to zero upper bits not
1261 // pass them through like other scalar intrinsics. So we shouldn't just
1262 // use Arg0 if DemandedElts[0] is clear like we do for other intrinsics.
1263 // Instead we should return a zero vector.
Craig Topper1a8a3372016-12-29 03:30:17 +00001264 if (!DemandedElts[0]) {
1265 Worklist.Add(II);
Craig Topper7fc6d342016-12-11 22:32:38 +00001266 return ConstantAggregateZero::get(II->getType());
Craig Topper1a8a3372016-12-29 03:30:17 +00001267 }
Craig Topper7fc6d342016-12-11 22:32:38 +00001268
Craig Topperac75bca2016-12-13 07:45:45 +00001269 // Only the lower element is used.
1270 DemandedElts = 1;
Craig Topper7fc6d342016-12-11 22:32:38 +00001271 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1272 UndefElts, Depth + 1);
1273 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperac75bca2016-12-13 07:45:45 +00001274
1275 // Only the lower element is undefined. The high elements are zero.
1276 UndefElts = UndefElts[0];
Craig Topper7fc6d342016-12-11 22:32:38 +00001277 break;
1278
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001279 // Unary scalar-as-vector operations that work column-wise.
Simon Pilgrim83020942016-04-24 18:23:14 +00001280 case Intrinsic::x86_sse_rcp_ss:
1281 case Intrinsic::x86_sse_rsqrt_ss:
1282 case Intrinsic::x86_sse_sqrt_ss:
1283 case Intrinsic::x86_sse2_sqrt_sd:
Simon Pilgrim83020942016-04-24 18:23:14 +00001284 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1285 UndefElts, Depth + 1);
1286 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1287
1288 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001289 if (!DemandedElts[0]) {
1290 Worklist.Add(II);
Simon Pilgrim83020942016-04-24 18:23:14 +00001291 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001292 }
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001293 // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions
1294 // checks).
Simon Pilgrim83020942016-04-24 18:23:14 +00001295 break;
1296
Craig Toppera0372de2016-12-14 03:17:27 +00001297 // Binary scalar-as-vector operations that work column-wise. The high
1298 // elements come from operand 0. The low element is a function of both
1299 // operands.
Chris Lattner7e044912010-01-04 07:17:19 +00001300 case Intrinsic::x86_sse_min_ss:
1301 case Intrinsic::x86_sse_max_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001302 case Intrinsic::x86_sse_cmp_ss:
Chris Lattner7e044912010-01-04 07:17:19 +00001303 case Intrinsic::x86_sse2_min_sd:
1304 case Intrinsic::x86_sse2_max_sd:
Craig Toppera0372de2016-12-14 03:17:27 +00001305 case Intrinsic::x86_sse2_cmp_sd: {
1306 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1307 UndefElts, Depth + 1);
1308 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1309
1310 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001311 if (!DemandedElts[0]) {
1312 Worklist.Add(II);
Craig Toppera0372de2016-12-14 03:17:27 +00001313 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001314 }
Craig Toppera0372de2016-12-14 03:17:27 +00001315
1316 // Only lower element is used for operand 1.
1317 DemandedElts = 1;
1318 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1319 UndefElts2, Depth + 1);
1320 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1321
1322 // Lower element is undefined if both lower elements are undefined.
1323 // Consider things like undef&0. The result is known zero, not undef.
1324 if (!UndefElts2[0])
1325 UndefElts.clearBit(0);
1326
1327 break;
1328 }
1329
Craig Toppereb6a20e2016-12-14 03:17:30 +00001330 // Binary scalar-as-vector operations that work column-wise. The high
1331 // elements come from operand 0 and the low element comes from operand 1.
Simon Pilgrim83020942016-04-24 18:23:14 +00001332 case Intrinsic::x86_sse41_round_ss:
Craig Toppereb6a20e2016-12-14 03:17:30 +00001333 case Intrinsic::x86_sse41_round_sd: {
1334 // Don't use the low element of operand 0.
1335 APInt DemandedElts2 = DemandedElts;
1336 DemandedElts2.clearBit(0);
1337 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001338 UndefElts, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001339 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001340
1341 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001342 if (!DemandedElts[0]) {
1343 Worklist.Add(II);
Craig Toppereb6a20e2016-12-14 03:17:30 +00001344 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001345 }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001346
1347 // Only lower element is used for operand 1.
1348 DemandedElts = 1;
Gabor Greife23efee2010-06-28 16:45:00 +00001349 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001350 UndefElts2, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001351 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001352
Craig Toppereb6a20e2016-12-14 03:17:30 +00001353 // Take the high undef elements from operand 0 and take the lower element
1354 // from operand 1.
1355 UndefElts.clearBit(0);
1356 UndefElts |= UndefElts2[0];
Chris Lattner7e044912010-01-04 07:17:19 +00001357 break;
Craig Toppereb6a20e2016-12-14 03:17:30 +00001358 }
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001359
Craig Topperdfd268d2016-12-14 05:43:05 +00001360 // Three input scalar-as-vector operations that work column-wise. The high
1361 // elements come from operand 0 and the low element is a function of all
1362 // three inputs.
Craig Topper268b3ab2016-12-14 06:06:58 +00001363 case Intrinsic::x86_avx512_mask_add_ss_round:
1364 case Intrinsic::x86_avx512_mask_div_ss_round:
1365 case Intrinsic::x86_avx512_mask_mul_ss_round:
1366 case Intrinsic::x86_avx512_mask_sub_ss_round:
1367 case Intrinsic::x86_avx512_mask_max_ss_round:
1368 case Intrinsic::x86_avx512_mask_min_ss_round:
1369 case Intrinsic::x86_avx512_mask_add_sd_round:
1370 case Intrinsic::x86_avx512_mask_div_sd_round:
1371 case Intrinsic::x86_avx512_mask_mul_sd_round:
1372 case Intrinsic::x86_avx512_mask_sub_sd_round:
1373 case Intrinsic::x86_avx512_mask_max_sd_round:
1374 case Intrinsic::x86_avx512_mask_min_sd_round:
Craig Topper23ebd952016-12-11 08:54:52 +00001375 case Intrinsic::x86_fma_vfmadd_ss:
1376 case Intrinsic::x86_fma_vfmsub_ss:
1377 case Intrinsic::x86_fma_vfnmadd_ss:
1378 case Intrinsic::x86_fma_vfnmsub_ss:
1379 case Intrinsic::x86_fma_vfmadd_sd:
1380 case Intrinsic::x86_fma_vfmsub_sd:
1381 case Intrinsic::x86_fma_vfnmadd_sd:
1382 case Intrinsic::x86_fma_vfnmsub_sd:
Craig Topperab5f3552016-12-15 03:49:45 +00001383 case Intrinsic::x86_avx512_mask_vfmadd_ss:
1384 case Intrinsic::x86_avx512_mask_vfmadd_sd:
1385 case Intrinsic::x86_avx512_maskz_vfmadd_ss:
1386 case Intrinsic::x86_avx512_maskz_vfmadd_sd:
Craig Topper23ebd952016-12-11 08:54:52 +00001387 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1388 UndefElts, Depth + 1);
1389 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperdfd268d2016-12-14 05:43:05 +00001390
1391 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001392 if (!DemandedElts[0]) {
1393 Worklist.Add(II);
Craig Topperdfd268d2016-12-14 05:43:05 +00001394 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001395 }
Craig Topperdfd268d2016-12-14 05:43:05 +00001396
1397 // Only lower element is used for operand 1 and 2.
1398 DemandedElts = 1;
Craig Topper23ebd952016-12-11 08:54:52 +00001399 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1400 UndefElts2, Depth + 1);
1401 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1402 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1403 UndefElts3, Depth + 1);
1404 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1405
Craig Topperdfd268d2016-12-14 05:43:05 +00001406 // Lower element is undefined if all three lower elements are undefined.
1407 // Consider things like undef&0. The result is known zero, not undef.
1408 if (!UndefElts2[0] || !UndefElts3[0])
1409 UndefElts.clearBit(0);
Craig Topper23ebd952016-12-11 08:54:52 +00001410
Craig Topper23ebd952016-12-11 08:54:52 +00001411 break;
1412
Craig Topperab5f3552016-12-15 03:49:45 +00001413 case Intrinsic::x86_avx512_mask3_vfmadd_ss:
1414 case Intrinsic::x86_avx512_mask3_vfmadd_sd:
1415 case Intrinsic::x86_avx512_mask3_vfmsub_ss:
1416 case Intrinsic::x86_avx512_mask3_vfmsub_sd:
1417 case Intrinsic::x86_avx512_mask3_vfnmsub_ss:
1418 case Intrinsic::x86_avx512_mask3_vfnmsub_sd:
1419 // These intrinsics get the passthru bits from operand 2.
1420 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1421 UndefElts, Depth + 1);
1422 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1423
1424 // If lowest element of a scalar op isn't used then use Arg2.
Craig Topper1a8a3372016-12-29 03:30:17 +00001425 if (!DemandedElts[0]) {
1426 Worklist.Add(II);
Craig Topperab5f3552016-12-15 03:49:45 +00001427 return II->getArgOperand(2);
Craig Topper1a8a3372016-12-29 03:30:17 +00001428 }
Craig Topperab5f3552016-12-15 03:49:45 +00001429
1430 // Only lower element is used for operand 0 and 1.
1431 DemandedElts = 1;
1432 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1433 UndefElts2, Depth + 1);
1434 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1435 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1436 UndefElts3, Depth + 1);
1437 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1438
1439 // Lower element is undefined if all three lower elements are undefined.
1440 // Consider things like undef&0. The result is known zero, not undef.
1441 if (!UndefElts2[0] || !UndefElts3[0])
1442 UndefElts.clearBit(0);
1443
1444 break;
1445
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001446 case Intrinsic::x86_sse2_pmulu_dq:
1447 case Intrinsic::x86_sse41_pmuldq:
1448 case Intrinsic::x86_avx2_pmul_dq:
Craig Topper72f2d4e2016-12-27 05:30:09 +00001449 case Intrinsic::x86_avx2_pmulu_dq:
1450 case Intrinsic::x86_avx512_pmul_dq_512:
1451 case Intrinsic::x86_avx512_pmulu_dq_512: {
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001452 Value *Op0 = II->getArgOperand(0);
1453 Value *Op1 = II->getArgOperand(1);
1454 unsigned InnerVWidth = Op0->getType()->getVectorNumElements();
1455 assert((VWidth * 2) == InnerVWidth && "Unexpected input size");
1456
1457 APInt InnerDemandedElts(InnerVWidth, 0);
1458 for (unsigned i = 0; i != VWidth; ++i)
1459 if (DemandedElts[i])
1460 InnerDemandedElts.setBit(i * 2);
1461
1462 UndefElts2 = APInt(InnerVWidth, 0);
1463 TmpV = SimplifyDemandedVectorElts(Op0, InnerDemandedElts, UndefElts2,
1464 Depth + 1);
1465 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1466
1467 UndefElts3 = APInt(InnerVWidth, 0);
1468 TmpV = SimplifyDemandedVectorElts(Op1, InnerDemandedElts, UndefElts3,
1469 Depth + 1);
1470 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1471
1472 break;
1473 }
1474
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001475 // SSE4A instructions leave the upper 64-bits of the 128-bit result
1476 // in an undefined state.
1477 case Intrinsic::x86_sse4a_extrq:
1478 case Intrinsic::x86_sse4a_extrqi:
1479 case Intrinsic::x86_sse4a_insertq:
1480 case Intrinsic::x86_sse4a_insertqi:
1481 UndefElts |= APInt::getHighBitsSet(VWidth, VWidth / 2);
1482 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001483 }
1484 break;
1485 }
1486 }
Craig Topperf40110f2014-04-25 05:29:35 +00001487 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001488}