blob: 3dbb1b190be65b1b40d89c82fff71b60b945979b [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000017#include "llvm/IR/PatternMatch.h"
Chris Lattner7e044912010-01-04 07:17:19 +000018
19using namespace llvm;
Shuxin Yang63e999e2012-12-04 00:04:54 +000020using namespace llvm::PatternMatch;
Chris Lattner7e044912010-01-04 07:17:19 +000021
Chandler Carruth964daaa2014-04-22 02:55:47 +000022#define DEBUG_TYPE "instcombine"
23
Craig Topper4c947752012-12-22 18:09:02 +000024/// ShrinkDemandedConstant - Check to see if the specified operand of the
Chris Lattner7e044912010-01-04 07:17:19 +000025/// specified instruction is a constant integer. If so, check to see if there
26/// are any bits set in the constant that are not demanded. If so, shrink the
27/// 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
David Majnemer49775e02014-08-22 17:11:04 +000046 // If either 'nsw' or 'nuw' is set and the constant is negative,
47 // removing *any* bits from the constant could make overflow occur.
48 // Remove 'nsw' and 'nuw' from the instruction in this case.
49 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(I)) {
50 assert(OBO->getOpcode() == Instruction::Add);
51 if (OBO->hasNoSignedWrap() || OBO->hasNoUnsignedWrap()) {
52 if (OpC->getValue().isNegative()) {
53 cast<BinaryOperator>(OBO)->setHasNoSignedWrap(false);
54 cast<BinaryOperator>(OBO)->setHasNoUnsignedWrap(false);
55 }
56 }
57 }
David Majnemer42b83a52014-08-22 07:56:32 +000058
Chris Lattner7e044912010-01-04 07:17:19 +000059 return true;
60}
61
62
63
64/// SimplifyDemandedInstructionBits - Inst is an integer instruction that
65/// SimplifyDemandedBits knows about. See if the instruction has any
66/// properties that allow us to simplify its operands.
67bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
68 unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
69 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
70 APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
Craig Topper4c947752012-12-22 18:09:02 +000071
Mehdi Aminia28d91d2015-03-10 02:37:25 +000072 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, KnownZero, KnownOne,
73 0, &Inst);
Craig Topperf40110f2014-04-25 05:29:35 +000074 if (!V) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000075 if (V == &Inst) return true;
76 ReplaceInstUsesWith(Inst, V);
77 return true;
78}
79
80/// SimplifyDemandedBits - This form of SimplifyDemandedBits simplifies the
81/// specified instruction operand if possible, updating it in place. It returns
82/// true if it made any change and false otherwise.
Craig Topper4c947752012-12-22 18:09:02 +000083bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +000084 APInt &KnownZero, APInt &KnownOne,
85 unsigned Depth) {
David Majnemerfe58d132015-04-22 20:59:28 +000086 auto *UserI = dyn_cast<Instruction>(U.getUser());
87 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, KnownZero,
88 KnownOne, Depth, UserI);
Craig Topperf40110f2014-04-25 05:29:35 +000089 if (!NewVal) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000090 U = NewVal;
91 return true;
92}
93
94
95/// SimplifyDemandedUseBits - This function attempts to replace V with a simpler
96/// value based on the demanded bits. When this function is called, it is known
97/// that only the bits set in DemandedMask of the result of V are ever used
98/// downstream. Consequently, depending on the mask and V, it may be possible
99/// to replace V with a constant or one of its operands. In such cases, this
100/// function does the replacement and returns true. In all other cases, it
101/// returns false after analyzing the expression and setting KnownOne and known
102/// to be one in the expression. KnownZero contains all the bits that are known
103/// to be zero in the expression. These are provided to potentially allow the
104/// caller (which might recursively be SimplifyDemandedBits itself) to simplify
Craig Topper4c947752012-12-22 18:09:02 +0000105/// the expression. KnownOne and KnownZero always follow the invariant that
Chris Lattner7e044912010-01-04 07:17:19 +0000106/// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that
107/// the bits in KnownOne and KnownZero may only be accurate for those bits set
108/// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
109/// and KnownOne must all be the same.
110///
111/// This returns null if it did not change anything and it permits no
112/// simplification. This returns V itself if it did some simplification of V's
113/// operands based on the information about what bits are demanded. This returns
114/// some other non-null value if it found out that V is equal to another value
115/// in the context where the specified bits are demanded, but not for all users.
116Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
117 APInt &KnownZero, APInt &KnownOne,
Hal Finkel60db0582014-09-07 18:57:58 +0000118 unsigned Depth,
119 Instruction *CxtI) {
Craig Toppere73658d2014-04-28 04:05:08 +0000120 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000121 assert(Depth <= 6 && "Limit Search Depth");
122 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000123 Type *VTy = V->getType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000124 assert(
125 (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
126 KnownZero.getBitWidth() == BitWidth &&
127 KnownOne.getBitWidth() == BitWidth &&
128 "Value *V, DemandedMask, KnownZero and KnownOne "
129 "must have same BitWidth");
Chris Lattner7e044912010-01-04 07:17:19 +0000130 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
131 // We know all of the bits for a constant!
132 KnownOne = CI->getValue() & DemandedMask;
133 KnownZero = ~KnownOne & DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000134 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000135 }
136 if (isa<ConstantPointerNull>(V)) {
137 // We know all of the bits for a constant!
Jay Foad25a5e4c2010-12-01 08:53:58 +0000138 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000139 KnownZero = DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000140 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000141 }
142
Jay Foad25a5e4c2010-12-01 08:53:58 +0000143 KnownZero.clearAllBits();
144 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000145 if (DemandedMask == 0) { // Not demanding any bits from V.
146 if (isa<UndefValue>(V))
Craig Topperf40110f2014-04-25 05:29:35 +0000147 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000148 return UndefValue::get(VTy);
149 }
Craig Topper4c947752012-12-22 18:09:02 +0000150
Chris Lattner7e044912010-01-04 07:17:19 +0000151 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000152 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000153
Chris Lattner7e044912010-01-04 07:17:19 +0000154 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000155 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +0000156
157 Instruction *I = dyn_cast<Instruction>(V);
158 if (!I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000159 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000160 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000161 }
162
163 // If there are multiple uses of this value and we aren't at the root, then
164 // we can't do any simplifications of the operands, because DemandedMask
165 // only reflects the bits demanded by *one* of the users.
166 if (Depth != 0 && !I->hasOneUse()) {
167 // Despite the fact that we can't simplify this instruction in all User's
168 // context, we can at least compute the knownzero/knownone bits, and we can
169 // do simplifications that apply to *just* the one user if we know that
170 // this instruction has a simpler value in that context.
171 if (I->getOpcode() == Instruction::And) {
172 // If either the LHS or the RHS are Zero, the result is zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000173 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000174 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000175 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000176 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000177
Chris Lattner7e044912010-01-04 07:17:19 +0000178 // If all of the demanded bits are known 1 on one side, return the other.
179 // These bits cannot contribute to the result of the 'and' in this
180 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000181 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000182 (DemandedMask & ~LHSKnownZero))
183 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000184 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000185 (DemandedMask & ~RHSKnownZero))
186 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000187
Chris Lattner7e044912010-01-04 07:17:19 +0000188 // If all of the demanded bits in the inputs are known zeros, return zero.
189 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
190 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000191
Chris Lattner7e044912010-01-04 07:17:19 +0000192 } else if (I->getOpcode() == Instruction::Or) {
193 // We can simplify (X|Y) -> X or Y in the user's context if we know that
194 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000195
Chris Lattner7e044912010-01-04 07:17:19 +0000196 // If either the LHS or the RHS are One, the result is One.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000197 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000198 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000199 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000200 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000201
Chris Lattner7e044912010-01-04 07:17:19 +0000202 // If all of the demanded bits are known zero on one side, return the
203 // other. These bits cannot contribute to the result of the 'or' in this
204 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000205 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000206 (DemandedMask & ~LHSKnownOne))
207 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000208 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000209 (DemandedMask & ~RHSKnownOne))
210 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000211
Chris Lattner7e044912010-01-04 07:17:19 +0000212 // If all of the potentially set bits on one side are known to be set on
213 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000214 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000215 (DemandedMask & (~RHSKnownZero)))
216 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000217 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000218 (DemandedMask & (~LHSKnownZero)))
219 return I->getOperand(1);
Shuxin Yang73285932012-12-04 22:15:32 +0000220 } else if (I->getOpcode() == Instruction::Xor) {
221 // We can simplify (X^Y) -> X or Y in the user's context if we know that
222 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000223
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000224 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000225 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000226 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000227 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000228
Shuxin Yang73285932012-12-04 22:15:32 +0000229 // If all of the demanded bits are known zero on one side, return the
Craig Topper4c947752012-12-22 18:09:02 +0000230 // other.
Shuxin Yang73285932012-12-04 22:15:32 +0000231 if ((DemandedMask & RHSKnownZero) == DemandedMask)
232 return I->getOperand(0);
233 if ((DemandedMask & LHSKnownZero) == DemandedMask)
234 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000235 }
Shuxin Yang73285932012-12-04 22:15:32 +0000236
Chris Lattner7e044912010-01-04 07:17:19 +0000237 // Compute the KnownZero/KnownOne bits to simplify things downstream.
Hal Finkel60db0582014-09-07 18:57:58 +0000238 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000239 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000240 }
Craig Topper4c947752012-12-22 18:09:02 +0000241
Chris Lattner7e044912010-01-04 07:17:19 +0000242 // If this is the root being simplified, allow it to have multiple uses,
243 // just set the DemandedMask to all bits so that we can try to simplify the
244 // operands. This allows visitTruncInst (for example) to simplify the
245 // operand of a trunc without duplicating all the logic below.
246 if (Depth == 0 && !V->hasOneUse())
247 DemandedMask = APInt::getAllOnesValue(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000248
Chris Lattner7e044912010-01-04 07:17:19 +0000249 switch (I->getOpcode()) {
250 default:
Hal Finkel60db0582014-09-07 18:57:58 +0000251 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000252 break;
253 case Instruction::And:
254 // If either the LHS or the RHS are Zero, the result is zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000255 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, RHSKnownZero,
256 RHSKnownOne, Depth + 1) ||
Chris Lattner7e044912010-01-04 07:17:19 +0000257 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000258 LHSKnownZero, LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000259 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000260 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
261 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000262
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000263 // If the client is only demanding bits that we know, return the known
264 // constant.
265 if ((DemandedMask & ((RHSKnownZero | LHSKnownZero)|
266 (RHSKnownOne & LHSKnownOne))) == DemandedMask)
267 return Constant::getIntegerValue(VTy, RHSKnownOne & LHSKnownOne);
268
Chris Lattner7e044912010-01-04 07:17:19 +0000269 // If all of the demanded bits are known 1 on one side, return the other.
270 // These bits cannot contribute to the result of the 'and'.
Craig Topper4c947752012-12-22 18:09:02 +0000271 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000272 (DemandedMask & ~LHSKnownZero))
273 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000274 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000275 (DemandedMask & ~RHSKnownZero))
276 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000277
Chris Lattner7e044912010-01-04 07:17:19 +0000278 // If all of the demanded bits in the inputs are known zeros, return zero.
279 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
280 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000281
Chris Lattner7e044912010-01-04 07:17:19 +0000282 // If the RHS is a constant, see if we can simplify it.
283 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
284 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000285
Chris Lattner7e044912010-01-04 07:17:19 +0000286 // Output known-1 bits are only known if set in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000287 KnownOne = RHSKnownOne & LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000288 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000289 KnownZero = RHSKnownZero | LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000290 break;
291 case Instruction::Or:
292 // If either the LHS or the RHS are One, the result is One.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000293 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, RHSKnownZero,
294 RHSKnownOne, Depth + 1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000295 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000296 LHSKnownZero, LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000297 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000298 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
299 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
300
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000301 // If the client is only demanding bits that we know, return the known
302 // constant.
303 if ((DemandedMask & ((RHSKnownZero & LHSKnownZero)|
304 (RHSKnownOne | LHSKnownOne))) == DemandedMask)
305 return Constant::getIntegerValue(VTy, RHSKnownOne | LHSKnownOne);
306
Chris Lattner7e044912010-01-04 07:17:19 +0000307 // If all of the demanded bits are known zero on one side, return the other.
308 // These bits cannot contribute to the result of the 'or'.
Craig Topper4c947752012-12-22 18:09:02 +0000309 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000310 (DemandedMask & ~LHSKnownOne))
311 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000312 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000313 (DemandedMask & ~RHSKnownOne))
314 return I->getOperand(1);
315
316 // If all of the potentially set bits on one side are known to be set on
317 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000318 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000319 (DemandedMask & (~RHSKnownZero)))
320 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000321 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000322 (DemandedMask & (~LHSKnownZero)))
323 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000324
Chris Lattner7e044912010-01-04 07:17:19 +0000325 // If the RHS is a constant, see if we can simplify it.
326 if (ShrinkDemandedConstant(I, 1, DemandedMask))
327 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000328
Chris Lattner7e044912010-01-04 07:17:19 +0000329 // Output known-0 bits are only known if clear in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000330 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000331 // Output known-1 are known to be set if set in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000332 KnownOne = RHSKnownOne | LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000333 break;
334 case Instruction::Xor: {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000335 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, RHSKnownZero,
336 RHSKnownOne, Depth + 1) ||
337 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, LHSKnownZero,
338 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000339 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000340 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
341 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
342
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000343 // Output known-0 bits are known if clear or set in both the LHS & RHS.
344 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
345 (RHSKnownOne & LHSKnownOne);
346 // Output known-1 are known to be set if set in only one of the LHS, RHS.
347 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
348 (RHSKnownOne & LHSKnownZero);
349
350 // If the client is only demanding bits that we know, return the known
351 // constant.
352 if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
353 return Constant::getIntegerValue(VTy, IKnownOne);
354
Chris Lattner7e044912010-01-04 07:17:19 +0000355 // If all of the demanded bits are known zero on one side, return the other.
356 // These bits cannot contribute to the result of the 'xor'.
357 if ((DemandedMask & RHSKnownZero) == DemandedMask)
358 return I->getOperand(0);
359 if ((DemandedMask & LHSKnownZero) == DemandedMask)
360 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000361
Chris Lattner7e044912010-01-04 07:17:19 +0000362 // If all of the demanded bits are known to be zero on one side or the
363 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000364 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Chris Lattner7e044912010-01-04 07:17:19 +0000365 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
Craig Topper4c947752012-12-22 18:09:02 +0000366 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000367 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
368 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000369 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000370 }
Craig Topper4c947752012-12-22 18:09:02 +0000371
Chris Lattner7e044912010-01-04 07:17:19 +0000372 // If all of the demanded bits on one side are known, and all of the set
373 // bits on that side are also known to be set on the other side, turn this
374 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000375 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper4c947752012-12-22 18:09:02 +0000376 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
Chris Lattner7e044912010-01-04 07:17:19 +0000377 // all known
378 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
379 Constant *AndC = Constant::getIntegerValue(VTy,
380 ~RHSKnownOne & DemandedMask);
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000381 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000382 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000383 }
384 }
Craig Topper4c947752012-12-22 18:09:02 +0000385
Chris Lattner7e044912010-01-04 07:17:19 +0000386 // If the RHS is a constant, see if we can simplify it.
387 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
388 if (ShrinkDemandedConstant(I, 1, DemandedMask))
389 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000390
Chris Lattner7e044912010-01-04 07:17:19 +0000391 // If our LHS is an 'and' and if it has one use, and if any of the bits we
392 // are flipping are known to be set, then the xor is just resetting those
393 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
394 // simplifying both of them.
395 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
396 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
397 isa<ConstantInt>(I->getOperand(1)) &&
398 isa<ConstantInt>(LHSInst->getOperand(1)) &&
399 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
400 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
401 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
402 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000403
Chris Lattner7e044912010-01-04 07:17:19 +0000404 Constant *AndC =
405 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000406 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000407 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000408
Chris Lattner7e044912010-01-04 07:17:19 +0000409 Constant *XorC =
410 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000411 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000412 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000413 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000414
415 // Output known-0 bits are known if clear or set in both the LHS & RHS.
416 KnownZero= (RHSKnownZero & LHSKnownZero) | (RHSKnownOne & LHSKnownOne);
417 // Output known-1 are known to be set if set in only one of the LHS, RHS.
418 KnownOne = (RHSKnownZero & LHSKnownOne) | (RHSKnownOne & LHSKnownZero);
Chris Lattner7e044912010-01-04 07:17:19 +0000419 break;
420 }
421 case Instruction::Select:
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000422 if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask, RHSKnownZero,
423 RHSKnownOne, Depth + 1) ||
424 SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, LHSKnownZero,
425 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000426 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000427 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
428 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
429
Chris Lattner7e044912010-01-04 07:17:19 +0000430 // If the operands are constants, see if we can simplify them.
431 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
432 ShrinkDemandedConstant(I, 2, DemandedMask))
433 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000434
Chris Lattner7e044912010-01-04 07:17:19 +0000435 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000436 KnownOne = RHSKnownOne & LHSKnownOne;
437 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000438 break;
439 case Instruction::Trunc: {
440 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000441 DemandedMask = DemandedMask.zext(truncBf);
442 KnownZero = KnownZero.zext(truncBf);
443 KnownOne = KnownOne.zext(truncBf);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000444 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, KnownZero,
445 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000446 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000447 DemandedMask = DemandedMask.trunc(BitWidth);
448 KnownZero = KnownZero.trunc(BitWidth);
449 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000450 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000451 break;
452 }
453 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000454 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000455 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000456
Chris Lattner229907c2011-07-18 04:54:35 +0000457 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
458 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000459 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
460 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
461 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000462 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000463 } else
464 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000465 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000466 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000467 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000468 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000469
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000470 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, KnownZero,
471 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000472 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000473 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000474 break;
475 case Instruction::ZExt: {
476 // Compute the bits in the result that are not present in the input.
477 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000478
Jay Foad583abbc2010-12-07 08:25:19 +0000479 DemandedMask = DemandedMask.trunc(SrcBitWidth);
480 KnownZero = KnownZero.trunc(SrcBitWidth);
481 KnownOne = KnownOne.trunc(SrcBitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000482 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, KnownZero,
483 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000484 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000485 DemandedMask = DemandedMask.zext(BitWidth);
486 KnownZero = KnownZero.zext(BitWidth);
487 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000488 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000489 // The top bits are known to be zero.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000490 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000491 break;
492 }
493 case Instruction::SExt: {
494 // Compute the bits in the result that are not present in the input.
495 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000496
497 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000498 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
499
500 APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
501 // If any of the sign extended bits are demanded, we know that the sign
502 // bit is demanded.
503 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000504 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000505
Jay Foad583abbc2010-12-07 08:25:19 +0000506 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
507 KnownZero = KnownZero.trunc(SrcBitWidth);
508 KnownOne = KnownOne.trunc(SrcBitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000509 if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits, KnownZero,
510 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000511 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000512 InputDemandedBits = InputDemandedBits.zext(BitWidth);
513 KnownZero = KnownZero.zext(BitWidth);
514 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000515 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
516
Chris Lattner7e044912010-01-04 07:17:19 +0000517 // If the sign bit of the input is known set or clear, then we know the
518 // top bits of the result.
519
520 // If the input sign bit is known zero, or if the NewBits are not demanded
521 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000522 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000523 // Convert to ZExt cast
524 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000525 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000526 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
527 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000528 }
529 break;
530 }
531 case Instruction::Add: {
532 // Figure out what the input bits are. If the top bits of the and result
533 // are not demanded, then the add doesn't demand them from its input
534 // either.
535 unsigned NLZ = DemandedMask.countLeadingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000536
Chris Lattner7e044912010-01-04 07:17:19 +0000537 // If there is a constant on the RHS, there are a variety of xformations
538 // we can do.
539 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
540 // If null, this should be simplified elsewhere. Some of the xforms here
541 // won't work if the RHS is zero.
542 if (RHS->isZero())
543 break;
Craig Topper4c947752012-12-22 18:09:02 +0000544
Chris Lattner7e044912010-01-04 07:17:19 +0000545 // If the top bit of the output is demanded, demand everything from the
546 // input. Otherwise, we demand all the input bits except NLZ top bits.
547 APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
548
549 // Find information about known zero/one bits in the input.
Craig Topper4c947752012-12-22 18:09:02 +0000550 if (SimplifyDemandedBits(I->getOperandUse(0), InDemandedBits,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000551 LHSKnownZero, LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000552 return I;
553
554 // If the RHS of the add has bits set that can't affect the input, reduce
555 // the constant.
556 if (ShrinkDemandedConstant(I, 1, InDemandedBits))
557 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000558
Chris Lattner7e044912010-01-04 07:17:19 +0000559 // Avoid excess work.
560 if (LHSKnownZero == 0 && LHSKnownOne == 0)
561 break;
Craig Topper4c947752012-12-22 18:09:02 +0000562
Chris Lattner7e044912010-01-04 07:17:19 +0000563 // Turn it into OR if input bits are zero.
564 if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
565 Instruction *Or =
566 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
567 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000568 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000569 }
Craig Topper4c947752012-12-22 18:09:02 +0000570
Chris Lattner7e044912010-01-04 07:17:19 +0000571 // We can say something about the output known-zero and known-one bits,
572 // depending on potential carries from the input constant and the
573 // unknowns. For example if the LHS is known to have at most the 0x0F0F0
574 // bits set and the RHS constant is 0x01001, then we know we have a known
575 // one mask of 0x00001 and a known zero mask of 0xE0F0E.
Craig Topper4c947752012-12-22 18:09:02 +0000576
Chris Lattner7e044912010-01-04 07:17:19 +0000577 // To compute this, we first compute the potential carry bits. These are
578 // the bits which may be modified. I'm not aware of a better way to do
579 // this scan.
580 const APInt &RHSVal = RHS->getValue();
581 APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
Craig Topper4c947752012-12-22 18:09:02 +0000582
Chris Lattner7e044912010-01-04 07:17:19 +0000583 // Now that we know which bits have carries, compute the known-1/0 sets.
Craig Topper4c947752012-12-22 18:09:02 +0000584
Chris Lattner7e044912010-01-04 07:17:19 +0000585 // Bits are known one if they are known zero in one operand and one in the
586 // other, and there is no input carry.
Craig Topper4c947752012-12-22 18:09:02 +0000587 KnownOne = ((LHSKnownZero & RHSVal) |
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000588 (LHSKnownOne & ~RHSVal)) & ~CarryBits;
Craig Topper4c947752012-12-22 18:09:02 +0000589
Chris Lattner7e044912010-01-04 07:17:19 +0000590 // Bits are known zero if they are known zero in both operands and there
591 // is no input carry.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000592 KnownZero = LHSKnownZero & ~RHSVal & ~CarryBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000593 } else {
594 // If the high-bits of this ADD are not demanded, then it does not demand
595 // the high bits of its LHS or RHS.
596 if (DemandedMask[BitWidth-1] == 0) {
597 // Right fill the mask of bits for this ADD to demand the most
598 // significant bit and all those below it.
599 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
600 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000601 LHSKnownZero, LHSKnownOne, Depth + 1) ||
Chris Lattner7e044912010-01-04 07:17:19 +0000602 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
David Majnemer7d0e99c2015-04-22 22:42:05 +0000603 LHSKnownZero, LHSKnownOne, Depth + 1)) {
604 cast<BinaryOperator>(I)->setHasNoSignedWrap(false);
605 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(false);
Chris Lattner7e044912010-01-04 07:17:19 +0000606 return I;
David Majnemer7d0e99c2015-04-22 22:42:05 +0000607 }
Chris Lattner7e044912010-01-04 07:17:19 +0000608 }
609 }
610 break;
611 }
612 case Instruction::Sub:
613 // If the high-bits of this SUB are not demanded, then it does not demand
614 // the high bits of its LHS or RHS.
615 if (DemandedMask[BitWidth-1] == 0) {
616 // Right fill the mask of bits for this SUB to demand the most
617 // significant bit and all those below it.
618 uint32_t NLZ = DemandedMask.countLeadingZeros();
619 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
620 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000621 LHSKnownZero, LHSKnownOne, Depth + 1) ||
Chris Lattner7e044912010-01-04 07:17:19 +0000622 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
David Majnemer7d0e99c2015-04-22 22:42:05 +0000623 LHSKnownZero, LHSKnownOne, Depth + 1)) {
624 cast<BinaryOperator>(I)->setHasNoSignedWrap(false);
625 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(false);
Chris Lattner7e044912010-01-04 07:17:19 +0000626 return I;
David Majnemer7d0e99c2015-04-22 22:42:05 +0000627 }
Chris Lattner7e044912010-01-04 07:17:19 +0000628 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000629
Jay Foada0653a32014-05-14 21:14:37 +0000630 // Otherwise just hand the sub off to computeKnownBits to fill in
Chris Lattner7e044912010-01-04 07:17:19 +0000631 // the known zeros and ones.
Hal Finkel60db0582014-09-07 18:57:58 +0000632 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Benjamin Kramer010337c2011-12-24 17:31:38 +0000633
634 // Turn this into a xor if LHS is 2^n-1 and the remaining bits are known
635 // zero.
636 if (ConstantInt *C0 = dyn_cast<ConstantInt>(I->getOperand(0))) {
637 APInt I0 = C0->getValue();
638 if ((I0 + 1).isPowerOf2() && (I0 | KnownZero).isAllOnesValue()) {
639 Instruction *Xor = BinaryOperator::CreateXor(I->getOperand(1), C0);
640 return InsertNewInstWith(Xor, *I);
641 }
642 }
Chris Lattner7e044912010-01-04 07:17:19 +0000643 break;
644 case Instruction::Shl:
645 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000646 {
647 Value *VarX; ConstantInt *C1;
648 if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
649 Instruction *Shr = cast<Instruction>(I->getOperand(0));
650 Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
651 KnownZero, KnownOne);
652 if (R)
653 return R;
654 }
655 }
656
Chris Lattner768003c2011-02-10 05:09:34 +0000657 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000658 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000659
Chris Lattner768003c2011-02-10 05:09:34 +0000660 // If the shift is NUW/NSW, then it does demand the high bits.
661 ShlOperator *IOp = cast<ShlOperator>(I);
662 if (IOp->hasNoSignedWrap())
663 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
664 else if (IOp->hasNoUnsignedWrap())
665 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000666
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000667 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, KnownZero,
668 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000669 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000670 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
671 KnownZero <<= ShiftAmt;
672 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000673 // low bits known zero.
674 if (ShiftAmt)
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000675 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000676 }
677 break;
678 case Instruction::LShr:
679 // For a logical shift right
680 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000681 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000682
Chris Lattner7e044912010-01-04 07:17:19 +0000683 // Unsigned shift right.
684 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000685
Chris Lattner768003c2011-02-10 05:09:34 +0000686 // If the shift is exact, then it does demand the low bits (and knows that
687 // they are zero).
688 if (cast<LShrOperator>(I)->isExact())
689 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000690
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000691 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, KnownZero,
692 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000693 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000694 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
695 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
696 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000697 if (ShiftAmt) {
698 // Compute the new bits that are at the top now.
699 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000700 KnownZero |= HighBits; // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000701 }
702 }
703 break;
704 case Instruction::AShr:
705 // If this is an arithmetic shift right and only the low-bit is set, we can
706 // always convert this into a logical shr, even if the shift amount is
707 // variable. The low bit of the shift cannot be an input sign bit unless
708 // the shift amount is >= the size of the datatype, which is undefined.
709 if (DemandedMask == 1) {
710 // Perform the logical shift right.
711 Instruction *NewVal = BinaryOperator::CreateLShr(
712 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000713 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000714 }
Chris Lattner7e044912010-01-04 07:17:19 +0000715
716 // If the sign bit is the only bit demanded by this ashr, then there is no
717 // need to do it, the shift doesn't change the high bit.
718 if (DemandedMask.isSignBit())
719 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000720
Chris Lattner7e044912010-01-04 07:17:19 +0000721 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000722 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000723
Chris Lattner7e044912010-01-04 07:17:19 +0000724 // Signed shift right.
725 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
726 // If any of the "high bits" are demanded, we should set the sign bit as
727 // demanded.
728 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000729 DemandedMaskIn.setBit(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000730
Chris Lattner768003c2011-02-10 05:09:34 +0000731 // If the shift is exact, then it does demand the low bits (and knows that
732 // they are zero).
733 if (cast<AShrOperator>(I)->isExact())
734 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000735
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000736 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, KnownZero,
737 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000738 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000739 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000740 // Compute the new bits that are at the top now.
741 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000742 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
743 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000744
Chris Lattner7e044912010-01-04 07:17:19 +0000745 // Handle the sign bits.
746 APInt SignBit(APInt::getSignBit(BitWidth));
747 // Adjust to where it is now in the mask.
Craig Topper4c947752012-12-22 18:09:02 +0000748 SignBit = APIntOps::lshr(SignBit, ShiftAmt);
749
Chris Lattner7e044912010-01-04 07:17:19 +0000750 // If the input sign bit is known to be zero, or if none of the top bits
751 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000752 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Chris Lattner7e044912010-01-04 07:17:19 +0000753 (HighBits & ~DemandedMask) == HighBits) {
754 // Perform the logical shift right.
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000755 BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
756 SA, I->getName());
757 NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000758 return InsertNewInstWith(NewVal, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000759 } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
760 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000761 }
762 }
763 break;
764 case Instruction::SRem:
765 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000766 // X % -1 demands all the bits because we don't want to introduce
767 // INT_MIN % -1 (== undef) by accident.
768 if (Rem->isAllOnesValue())
769 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000770 APInt RA = Rem->getValue().abs();
771 if (RA.isPowerOf2()) {
772 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
773 return I->getOperand(0);
774
775 APInt LowBits = RA - 1;
776 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000777 if (SimplifyDemandedBits(I->getOperandUse(0), Mask2, LHSKnownZero,
778 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000779 return I;
780
Duncan Sands3a48b872010-01-28 17:22:42 +0000781 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000782 KnownZero = LHSKnownZero & LowBits;
783 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000784
Duncan Sands3a48b872010-01-28 17:22:42 +0000785 // If LHS is non-negative or has all low bits zero, then the upper bits
786 // are all zero.
787 if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
788 KnownZero |= ~LowBits;
789
790 // If LHS is negative and not all low bits are zero, then the upper bits
791 // are all one.
792 if (LHSKnownOne[BitWidth-1] && ((LHSKnownOne & LowBits) != 0))
793 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000794
Craig Topper4c947752012-12-22 18:09:02 +0000795 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000796 }
797 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000798
799 // The sign bit is the LHS's sign bit, except when the result of the
800 // remainder is zero.
801 if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
Nick Lewyckye4679792011-03-07 01:50:10 +0000802 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000803 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000804 CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000805 // If it's known zero, our sign bit is also zero.
806 if (LHSKnownZero.isNegative())
Benjamin Kramer21b972a2013-05-09 16:32:32 +0000807 KnownZero.setBit(KnownZero.getBitWidth() - 1);
Nick Lewyckye4679792011-03-07 01:50:10 +0000808 }
Chris Lattner7e044912010-01-04 07:17:19 +0000809 break;
810 case Instruction::URem: {
811 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
812 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000813 if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes, KnownZero2,
814 KnownOne2, Depth + 1) ||
815 SimplifyDemandedBits(I->getOperandUse(1), AllOnes, KnownZero2,
816 KnownOne2, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000817 return I;
818
819 unsigned Leaders = KnownZero2.countLeadingOnes();
820 Leaders = std::max(Leaders,
821 KnownZero2.countLeadingOnes());
822 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
823 break;
824 }
825 case Instruction::Call:
826 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
827 switch (II->getIntrinsicID()) {
828 default: break;
829 case Intrinsic::bswap: {
830 // If the only bits demanded come from one byte of the bswap result,
831 // just shift the input byte into position to eliminate the bswap.
832 unsigned NLZ = DemandedMask.countLeadingZeros();
833 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000834
Chris Lattner7e044912010-01-04 07:17:19 +0000835 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
836 // we need all the bits down to bit 8. Likewise, round NLZ. If we
837 // have 14 leading zeros, round to 8.
838 NLZ &= ~7;
839 NTZ &= ~7;
840 // If we need exactly one byte, we can do this transformation.
841 if (BitWidth-NLZ-NTZ == 8) {
842 unsigned ResultBit = NTZ;
843 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000844
Chris Lattner7e044912010-01-04 07:17:19 +0000845 // Replace this with either a left or right shift to get the byte into
846 // the right place.
847 Instruction *NewVal;
848 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000849 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000850 ConstantInt::get(I->getType(), InputBit-ResultBit));
851 else
Gabor Greif79430172010-06-24 12:35:13 +0000852 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000853 ConstantInt::get(I->getType(), ResultBit-InputBit));
854 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000855 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000856 }
Craig Topper4c947752012-12-22 18:09:02 +0000857
Chris Lattner7e044912010-01-04 07:17:19 +0000858 // TODO: Could compute known zero/one bits based on the input.
859 break;
860 }
Chad Rosierb3628842011-05-26 23:13:19 +0000861 case Intrinsic::x86_sse42_crc32_64_64:
Evan Chenge8d2e9e2011-05-20 00:54:37 +0000862 KnownZero = APInt::getHighBitsSet(64, 32);
Craig Topperf40110f2014-04-25 05:29:35 +0000863 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000864 }
865 }
Hal Finkel60db0582014-09-07 18:57:58 +0000866 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000867 break;
868 }
Craig Topper4c947752012-12-22 18:09:02 +0000869
Chris Lattner7e044912010-01-04 07:17:19 +0000870 // If the client is only demanding bits that we know, return the known
871 // constant.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000872 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
873 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000874 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000875}
876
Shuxin Yang63e999e2012-12-04 00:04:54 +0000877/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
878/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
879/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
880/// of "C2-C1".
881///
882/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
883/// ..., bn}, without considering the specific value X is holding.
884/// This transformation is legal iff one of following conditions is hold:
885/// 1) All the bit in S are 0, in this case E1 == E2.
886/// 2) We don't care those bits in S, per the input DemandedMask.
887/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
888/// rest bits.
889///
890/// Currently we only test condition 2).
891///
892/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
893/// not successful.
894Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
895 Instruction *Shl, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne) {
896
Benjamin Kramer010f1082013-08-30 14:35:35 +0000897 const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
898 const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
899 if (!ShlOp1 || !ShrOp1)
Craig Topperf40110f2014-04-25 05:29:35 +0000900 return nullptr; // Noop.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000901
902 Value *VarX = Shr->getOperand(0);
903 Type *Ty = VarX->getType();
904 unsigned BitWidth = Ty->getIntegerBitWidth();
905 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000906 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000907
908 unsigned ShlAmt = ShlOp1.getZExtValue();
909 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000910
911 KnownOne.clearAllBits();
912 KnownZero = APInt::getBitsSet(KnownZero.getBitWidth(), 0, ShlAmt-1);
913 KnownZero &= DemandedMask;
914
Benjamin Kramer010f1082013-08-30 14:35:35 +0000915 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
916 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000917
918 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
919 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
920 (BitMask1.ashr(ShrAmt) << ShlAmt);
921
922 if (ShrAmt <= ShlAmt) {
923 BitMask2 <<= (ShlAmt - ShrAmt);
924 } else {
925 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
926 BitMask2.ashr(ShrAmt - ShlAmt);
927 }
928
929 // Check if condition-2 (see the comment to this function) is satified.
930 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
931 if (ShrAmt == ShlAmt)
932 return VarX;
933
934 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000935 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000936
937 BinaryOperator *New;
938 if (ShrAmt < ShlAmt) {
939 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
940 New = BinaryOperator::CreateShl(VarX, Amt);
941 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
942 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
943 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
944 } else {
945 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000946 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
947 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000948 if (cast<BinaryOperator>(Shr)->isExact())
949 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000950 }
951
952 return InsertNewInstWith(New, *Shl);
953 }
954
Craig Topperf40110f2014-04-25 05:29:35 +0000955 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000956}
Chris Lattner7e044912010-01-04 07:17:19 +0000957
958/// SimplifyDemandedVectorElts - The specified value produces a vector with
959/// any number of elements. DemandedElts contains the set of elements that are
960/// actually used by the caller. This method analyzes which elements of the
961/// operand are undef and returns that information in UndefElts.
962///
963/// If the information about demanded elements can be used to simplify the
964/// operation, the operation is simplified, then the resultant value is
965/// returned. This returns null if no change was made.
966Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000967 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000968 unsigned Depth) {
969 unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
970 APInt EltMask(APInt::getAllOnesValue(VWidth));
971 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
972
973 if (isa<UndefValue>(V)) {
974 // If the entire vector is undefined, just return this info.
975 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000976 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000977 }
Craig Topper4c947752012-12-22 18:09:02 +0000978
Chris Lattnerb22423c2010-02-08 23:56:03 +0000979 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000980 UndefElts = EltMask;
981 return UndefValue::get(V->getType());
982 }
983
984 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000985
Chris Lattner67058832012-01-25 06:48:06 +0000986 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
987 if (Constant *C = dyn_cast<Constant>(V)) {
988 // Check if this is identity. If so, return 0 since we are not simplifying
989 // anything.
990 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000991 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000992
Chris Lattner229907c2011-07-18 04:54:35 +0000993 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000994 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000995
Chris Lattner67058832012-01-25 06:48:06 +0000996 SmallVector<Constant*, 16> Elts;
997 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000998 if (!DemandedElts[i]) { // If not demanded, set to undef.
999 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +00001000 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +00001001 continue;
1002 }
Craig Topper4c947752012-12-22 18:09:02 +00001003
Chris Lattner67058832012-01-25 06:48:06 +00001004 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +00001005 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +00001006
Chris Lattner67058832012-01-25 06:48:06 +00001007 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001008 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +00001009 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001010 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +00001011 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +00001012 }
Chris Lattner67058832012-01-25 06:48:06 +00001013 }
Craig Topper4c947752012-12-22 18:09:02 +00001014
Chris Lattner7e044912010-01-04 07:17:19 +00001015 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +00001016 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +00001017 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001018 }
Craig Topper4c947752012-12-22 18:09:02 +00001019
Chris Lattner7e044912010-01-04 07:17:19 +00001020 // Limit search depth.
1021 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +00001022 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001023
Stuart Hastings5bd18b62011-05-17 22:13:31 +00001024 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +00001025 // simplification conservatively assuming that all elements
1026 // are needed.
1027 if (!V->hasOneUse()) {
1028 // Quit if we find multiple users of a non-root value though.
1029 // They'll be handled when it's their turn to be visited by
1030 // the main instcombine process.
1031 if (Depth != 0)
1032 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +00001033 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001034
1035 // Conservatively assume that all elements are needed.
1036 DemandedElts = EltMask;
1037 }
Craig Topper4c947752012-12-22 18:09:02 +00001038
Chris Lattner7e044912010-01-04 07:17:19 +00001039 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +00001040 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +00001041
Chris Lattner7e044912010-01-04 07:17:19 +00001042 bool MadeChange = false;
1043 APInt UndefElts2(VWidth, 0);
1044 Value *TmpV;
1045 switch (I->getOpcode()) {
1046 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001047
Chris Lattner7e044912010-01-04 07:17:19 +00001048 case Instruction::InsertElement: {
1049 // If this is a variable index, we don't know which element it overwrites.
1050 // demand exactly the same input as we produce.
1051 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +00001052 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +00001053 // Note that we can't propagate undef elt info, because we don't know
1054 // which elt is getting updated.
1055 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001056 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001057 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1058 break;
1059 }
Craig Topper4c947752012-12-22 18:09:02 +00001060
Chris Lattner7e044912010-01-04 07:17:19 +00001061 // If this is inserting an element that isn't demanded, remove this
1062 // insertelement.
1063 unsigned IdxNo = Idx->getZExtValue();
1064 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1065 Worklist.Add(I);
1066 return I->getOperand(0);
1067 }
Craig Topper4c947752012-12-22 18:09:02 +00001068
Chris Lattner7e044912010-01-04 07:17:19 +00001069 // Otherwise, the element inserted overwrites whatever was there, so the
1070 // input demanded set is simpler than the output set.
1071 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001072 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001073 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001074 UndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001075 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1076
1077 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001078 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001079 break;
1080 }
1081 case Instruction::ShuffleVector: {
1082 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
1083 uint64_t LHSVWidth =
1084 cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements();
1085 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1086 for (unsigned i = 0; i < VWidth; i++) {
1087 if (DemandedElts[i]) {
1088 unsigned MaskVal = Shuffle->getMaskValue(i);
1089 if (MaskVal != -1u) {
1090 assert(MaskVal < LHSVWidth * 2 &&
1091 "shufflevector mask index out of range!");
1092 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001093 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001094 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001095 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001096 }
1097 }
1098 }
1099
1100 APInt UndefElts4(LHSVWidth, 0);
1101 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001102 UndefElts4, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001103 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1104
1105 APInt UndefElts3(LHSVWidth, 0);
1106 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001107 UndefElts3, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001108 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1109
1110 bool NewUndefElts = false;
1111 for (unsigned i = 0; i < VWidth; i++) {
1112 unsigned MaskVal = Shuffle->getMaskValue(i);
1113 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001114 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001115 } else if (!DemandedElts[i]) {
1116 NewUndefElts = true;
1117 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001118 } else if (MaskVal < LHSVWidth) {
1119 if (UndefElts4[MaskVal]) {
1120 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001121 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001122 }
1123 } else {
1124 if (UndefElts3[MaskVal - LHSVWidth]) {
1125 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001126 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001127 }
1128 }
1129 }
1130
1131 if (NewUndefElts) {
1132 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001133 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001134 for (unsigned i = 0; i < VWidth; ++i) {
1135 if (UndefElts[i])
1136 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1137 else
1138 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1139 Shuffle->getMaskValue(i)));
1140 }
1141 I->setOperand(2, ConstantVector::get(Elts));
1142 MadeChange = true;
1143 }
1144 break;
1145 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001146 case Instruction::Select: {
1147 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1148 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1149 for (unsigned i = 0; i < VWidth; i++) {
1150 if (CV->getAggregateElement(i)->isNullValue())
1151 LeftDemanded.clearBit(i);
1152 else
1153 RightDemanded.clearBit(i);
1154 }
1155 }
1156
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001157 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded, UndefElts,
1158 Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001159 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1160
1161 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001162 UndefElts2, Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001163 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001164
Pete Cooperabc13af2012-07-26 23:10:24 +00001165 // Output elements are undefined if both are undefined.
1166 UndefElts &= UndefElts2;
1167 break;
1168 }
Chris Lattner7e044912010-01-04 07:17:19 +00001169 case Instruction::BitCast: {
1170 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001171 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001172 if (!VTy) break;
1173 unsigned InVWidth = VTy->getNumElements();
1174 APInt InputDemandedElts(InVWidth, 0);
1175 unsigned Ratio;
1176
1177 if (VWidth == InVWidth) {
1178 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1179 // elements as are demanded of us.
1180 Ratio = 1;
1181 InputDemandedElts = DemandedElts;
1182 } else if (VWidth > InVWidth) {
1183 // Untested so far.
1184 break;
Craig Topper4c947752012-12-22 18:09:02 +00001185
Chris Lattner7e044912010-01-04 07:17:19 +00001186 // If there are more elements in the result than there are in the source,
1187 // then an input element is live if any of the corresponding output
1188 // elements are live.
1189 Ratio = VWidth/InVWidth;
1190 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1191 if (DemandedElts[OutIdx])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001192 InputDemandedElts.setBit(OutIdx/Ratio);
Chris Lattner7e044912010-01-04 07:17:19 +00001193 }
1194 } else {
1195 // Untested so far.
1196 break;
Craig Topper4c947752012-12-22 18:09:02 +00001197
Chris Lattner7e044912010-01-04 07:17:19 +00001198 // If there are more elements in the source than there are in the result,
1199 // then an input element is live if the corresponding output element is
1200 // live.
1201 Ratio = InVWidth/VWidth;
1202 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1203 if (DemandedElts[InIdx/Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001204 InputDemandedElts.setBit(InIdx);
Chris Lattner7e044912010-01-04 07:17:19 +00001205 }
Craig Topper4c947752012-12-22 18:09:02 +00001206
Chris Lattner7e044912010-01-04 07:17:19 +00001207 // div/rem demand all inputs, because they don't want divide by zero.
1208 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001209 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001210 if (TmpV) {
1211 I->setOperand(0, TmpV);
1212 MadeChange = true;
1213 }
Craig Topper4c947752012-12-22 18:09:02 +00001214
Chris Lattner7e044912010-01-04 07:17:19 +00001215 UndefElts = UndefElts2;
1216 if (VWidth > InVWidth) {
1217 llvm_unreachable("Unimp");
1218 // If there are more elements in the result than there are in the source,
1219 // then an output element is undef if the corresponding input element is
1220 // undef.
1221 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1222 if (UndefElts2[OutIdx/Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001223 UndefElts.setBit(OutIdx);
Chris Lattner7e044912010-01-04 07:17:19 +00001224 } else if (VWidth < InVWidth) {
1225 llvm_unreachable("Unimp");
1226 // If there are more elements in the source than there are in the result,
1227 // then a result element is undef if all of the corresponding input
1228 // elements are undef.
1229 UndefElts = ~0ULL >> (64-VWidth); // Start out all undef.
1230 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1231 if (!UndefElts2[InIdx]) // Not undef?
Jay Foad25a5e4c2010-12-01 08:53:58 +00001232 UndefElts.clearBit(InIdx/Ratio); // Clear undef bit.
Chris Lattner7e044912010-01-04 07:17:19 +00001233 }
1234 break;
1235 }
1236 case Instruction::And:
1237 case Instruction::Or:
1238 case Instruction::Xor:
1239 case Instruction::Add:
1240 case Instruction::Sub:
1241 case Instruction::Mul:
1242 // div/rem demand all inputs, because they don't want divide by zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001243 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1244 Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001245 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1246 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001247 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001248 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001249
Chris Lattner7e044912010-01-04 07:17:19 +00001250 // Output elements are undefined if both are undefined. Consider things
1251 // like undef&0. The result is known zero, not undef.
1252 UndefElts &= UndefElts2;
1253 break;
Pete Coopere807e452012-07-26 22:37:04 +00001254 case Instruction::FPTrunc:
1255 case Instruction::FPExt:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001256 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1257 Depth + 1);
Pete Coopere807e452012-07-26 22:37:04 +00001258 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1259 break;
Craig Topper4c947752012-12-22 18:09:02 +00001260
Chris Lattner7e044912010-01-04 07:17:19 +00001261 case Instruction::Call: {
1262 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1263 if (!II) break;
1264 switch (II->getIntrinsicID()) {
1265 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001266
Chris Lattner7e044912010-01-04 07:17:19 +00001267 // Binary vector operations that work column-wise. A dest element is a
1268 // function of the corresponding input elements from the two inputs.
1269 case Intrinsic::x86_sse_sub_ss:
1270 case Intrinsic::x86_sse_mul_ss:
1271 case Intrinsic::x86_sse_min_ss:
1272 case Intrinsic::x86_sse_max_ss:
1273 case Intrinsic::x86_sse2_sub_sd:
1274 case Intrinsic::x86_sse2_mul_sd:
1275 case Intrinsic::x86_sse2_min_sd:
1276 case Intrinsic::x86_sse2_max_sd:
Gabor Greife23efee2010-06-28 16:45:00 +00001277 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001278 UndefElts, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001279 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1280 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001281 UndefElts2, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001282 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001283
1284 // If only the low elt is demanded and this is a scalarizable intrinsic,
1285 // scalarize it now.
1286 if (DemandedElts == 1) {
1287 switch (II->getIntrinsicID()) {
1288 default: break;
1289 case Intrinsic::x86_sse_sub_ss:
1290 case Intrinsic::x86_sse_mul_ss:
1291 case Intrinsic::x86_sse2_sub_sd:
1292 case Intrinsic::x86_sse2_mul_sd:
1293 // TODO: Lower MIN/MAX/ABS/etc
Gabor Greif79430172010-06-24 12:35:13 +00001294 Value *LHS = II->getArgOperand(0);
1295 Value *RHS = II->getArgOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +00001296 // Extract the element as scalars.
Craig Topper4c947752012-12-22 18:09:02 +00001297 LHS = InsertNewInstWith(ExtractElementInst::Create(LHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001298 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Eli Friedman6efb64e2011-05-19 01:20:42 +00001299 RHS = InsertNewInstWith(ExtractElementInst::Create(RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001300 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Craig Topper4c947752012-12-22 18:09:02 +00001301
Chris Lattner7e044912010-01-04 07:17:19 +00001302 switch (II->getIntrinsicID()) {
1303 default: llvm_unreachable("Case stmts out of sync!");
1304 case Intrinsic::x86_sse_sub_ss:
1305 case Intrinsic::x86_sse2_sub_sd:
Eli Friedman6efb64e2011-05-19 01:20:42 +00001306 TmpV = InsertNewInstWith(BinaryOperator::CreateFSub(LHS, RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001307 II->getName()), *II);
1308 break;
1309 case Intrinsic::x86_sse_mul_ss:
1310 case Intrinsic::x86_sse2_mul_sd:
Eli Friedman6efb64e2011-05-19 01:20:42 +00001311 TmpV = InsertNewInstWith(BinaryOperator::CreateFMul(LHS, RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001312 II->getName()), *II);
1313 break;
1314 }
Craig Topper4c947752012-12-22 18:09:02 +00001315
Chris Lattner7e044912010-01-04 07:17:19 +00001316 Instruction *New =
1317 InsertElementInst::Create(
1318 UndefValue::get(II->getType()), TmpV,
1319 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U, false),
1320 II->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +00001321 InsertNewInstWith(New, *II);
Chris Lattner7e044912010-01-04 07:17:19 +00001322 return New;
Craig Topper4c947752012-12-22 18:09:02 +00001323 }
Chris Lattner7e044912010-01-04 07:17:19 +00001324 }
Craig Topper4c947752012-12-22 18:09:02 +00001325
Chris Lattner7e044912010-01-04 07:17:19 +00001326 // Output elements are undefined if both are undefined. Consider things
1327 // like undef&0. The result is known zero, not undef.
1328 UndefElts &= UndefElts2;
1329 break;
1330 }
1331 break;
1332 }
1333 }
Craig Topperf40110f2014-04-25 05:29:35 +00001334 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001335}