blob: 249544a061f03186281f0df9dbf91bb7250df16b [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
Chris Lattner7e044912010-01-04 07:17:19 +000015#include "InstCombine.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/DataLayout.h"
17#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
Craig Topper4c947752012-12-22 18:09:02 +000025/// ShrinkDemandedConstant - Check to see if the specified operand of the
Chris Lattner7e044912010-01-04 07:17:19 +000026/// specified instruction is a constant integer. If so, check to see if there
27/// are any bits set in the constant that are not demanded. If so, shrink the
28/// constant and return true.
Craig Topper4c947752012-12-22 18:09:02 +000029static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
Chris Lattner7e044912010-01-04 07:17:19 +000030 APInt Demanded) {
31 assert(I && "No instruction?");
32 assert(OpNo < I->getNumOperands() && "Operand index too large");
33
34 // If the operand is not a constant integer, nothing to do.
35 ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo));
36 if (!OpC) return false;
37
38 // If there are no bits set that aren't demanded, nothing to do.
Jay Foad583abbc2010-12-07 08:25:19 +000039 Demanded = Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
Chris Lattner7e044912010-01-04 07:17:19 +000040 if ((~Demanded & OpC->getValue()) == 0)
41 return false;
42
43 // This instruction is producing bits that are not demanded. Shrink the RHS.
44 Demanded &= OpC->getValue();
45 I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Demanded));
David Majnemer42b83a52014-08-22 07:56:32 +000046
David Majnemer49775e02014-08-22 17:11:04 +000047 // If either 'nsw' or 'nuw' is set and the constant is negative,
48 // removing *any* bits from the constant could make overflow occur.
49 // Remove 'nsw' and 'nuw' from the instruction in this case.
50 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(I)) {
51 assert(OBO->getOpcode() == Instruction::Add);
52 if (OBO->hasNoSignedWrap() || OBO->hasNoUnsignedWrap()) {
53 if (OpC->getValue().isNegative()) {
54 cast<BinaryOperator>(OBO)->setHasNoSignedWrap(false);
55 cast<BinaryOperator>(OBO)->setHasNoUnsignedWrap(false);
56 }
57 }
58 }
David Majnemer42b83a52014-08-22 07:56:32 +000059
Chris Lattner7e044912010-01-04 07:17:19 +000060 return true;
61}
62
63
64
65/// SimplifyDemandedInstructionBits - Inst is an integer instruction that
66/// SimplifyDemandedBits knows about. See if the instruction has any
67/// properties that allow us to simplify its operands.
68bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
69 unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
70 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
71 APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
Craig Topper4c947752012-12-22 18:09:02 +000072
73 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask,
Hal Finkel60db0582014-09-07 18:57:58 +000074 KnownZero, KnownOne, 0, &Inst);
Craig Topperf40110f2014-04-25 05:29:35 +000075 if (!V) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000076 if (V == &Inst) return true;
77 ReplaceInstUsesWith(Inst, V);
78 return true;
79}
80
81/// SimplifyDemandedBits - This form of SimplifyDemandedBits simplifies the
82/// specified instruction operand if possible, updating it in place. It returns
83/// true if it made any change and false otherwise.
Craig Topper4c947752012-12-22 18:09:02 +000084bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +000085 APInt &KnownZero, APInt &KnownOne,
86 unsigned Depth) {
87 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask,
Hal Finkel60db0582014-09-07 18:57:58 +000088 KnownZero, KnownOne, Depth,
89 dyn_cast<Instruction>(U.getUser()));
Craig Topperf40110f2014-04-25 05:29:35 +000090 if (!NewVal) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000091 U = NewVal;
92 return true;
93}
94
95
96/// SimplifyDemandedUseBits - This function attempts to replace V with a simpler
97/// value based on the demanded bits. When this function is called, it is known
98/// that only the bits set in DemandedMask of the result of V are ever used
99/// downstream. Consequently, depending on the mask and V, it may be possible
100/// to replace V with a constant or one of its operands. In such cases, this
101/// function does the replacement and returns true. In all other cases, it
102/// returns false after analyzing the expression and setting KnownOne and known
103/// to be one in the expression. KnownZero contains all the bits that are known
104/// to be zero in the expression. These are provided to potentially allow the
105/// caller (which might recursively be SimplifyDemandedBits itself) to simplify
Craig Topper4c947752012-12-22 18:09:02 +0000106/// the expression. KnownOne and KnownZero always follow the invariant that
Chris Lattner7e044912010-01-04 07:17:19 +0000107/// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that
108/// the bits in KnownOne and KnownZero may only be accurate for those bits set
109/// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
110/// and KnownOne must all be the same.
111///
112/// This returns null if it did not change anything and it permits no
113/// simplification. This returns V itself if it did some simplification of V's
114/// operands based on the information about what bits are demanded. This returns
115/// some other non-null value if it found out that V is equal to another value
116/// in the context where the specified bits are demanded, but not for all users.
117Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
118 APInt &KnownZero, APInt &KnownOne,
Hal Finkel60db0582014-09-07 18:57:58 +0000119 unsigned Depth,
120 Instruction *CxtI) {
Craig Toppere73658d2014-04-28 04:05:08 +0000121 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000122 assert(Depth <= 6 && "Limit Search Depth");
123 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000124 Type *VTy = V->getType();
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000125 assert((DL || !VTy->isPointerTy()) &&
Chris Lattner7e044912010-01-04 07:17:19 +0000126 "SimplifyDemandedBits needs to know bit widths!");
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000127 assert((!DL || DL->getTypeSizeInBits(VTy->getScalarType()) == BitWidth) &&
Duncan Sands9dff9be2010-02-15 16:12:20 +0000128 (!VTy->isIntOrIntVectorTy() ||
Chris Lattner7e044912010-01-04 07:17:19 +0000129 VTy->getScalarSizeInBits() == BitWidth) &&
130 KnownZero.getBitWidth() == BitWidth &&
131 KnownOne.getBitWidth() == BitWidth &&
132 "Value *V, DemandedMask, KnownZero and KnownOne "
133 "must have same BitWidth");
134 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
135 // We know all of the bits for a constant!
136 KnownOne = CI->getValue() & DemandedMask;
137 KnownZero = ~KnownOne & DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000138 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000139 }
140 if (isa<ConstantPointerNull>(V)) {
141 // We know all of the bits for a constant!
Jay Foad25a5e4c2010-12-01 08:53:58 +0000142 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000143 KnownZero = DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000144 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000145 }
146
Jay Foad25a5e4c2010-12-01 08:53:58 +0000147 KnownZero.clearAllBits();
148 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000149 if (DemandedMask == 0) { // Not demanding any bits from V.
150 if (isa<UndefValue>(V))
Craig Topperf40110f2014-04-25 05:29:35 +0000151 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000152 return UndefValue::get(VTy);
153 }
Craig Topper4c947752012-12-22 18:09:02 +0000154
Chris Lattner7e044912010-01-04 07:17:19 +0000155 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000156 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000157
Chris Lattner7e044912010-01-04 07:17:19 +0000158 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000159 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +0000160
161 Instruction *I = dyn_cast<Instruction>(V);
162 if (!I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000163 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000164 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000165 }
166
167 // If there are multiple uses of this value and we aren't at the root, then
168 // we can't do any simplifications of the operands, because DemandedMask
169 // only reflects the bits demanded by *one* of the users.
170 if (Depth != 0 && !I->hasOneUse()) {
171 // Despite the fact that we can't simplify this instruction in all User's
172 // context, we can at least compute the knownzero/knownone bits, and we can
173 // do simplifications that apply to *just* the one user if we know that
174 // this instruction has a simpler value in that context.
175 if (I->getOpcode() == Instruction::And) {
176 // If either the LHS or the RHS are Zero, the result is zero.
Hal Finkel60db0582014-09-07 18:57:58 +0000177 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1,
178 CxtI);
179 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1,
180 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000181
Chris Lattner7e044912010-01-04 07:17:19 +0000182 // If all of the demanded bits are known 1 on one side, return the other.
183 // These bits cannot contribute to the result of the 'and' in this
184 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000185 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000186 (DemandedMask & ~LHSKnownZero))
187 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000188 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000189 (DemandedMask & ~RHSKnownZero))
190 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000191
Chris Lattner7e044912010-01-04 07:17:19 +0000192 // If all of the demanded bits in the inputs are known zeros, return zero.
193 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
194 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000195
Chris Lattner7e044912010-01-04 07:17:19 +0000196 } else if (I->getOpcode() == Instruction::Or) {
197 // We can simplify (X|Y) -> X or Y in the user's context if we know that
198 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000199
Chris Lattner7e044912010-01-04 07:17:19 +0000200 // If either the LHS or the RHS are One, the result is One.
Hal Finkel60db0582014-09-07 18:57:58 +0000201 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1,
202 CxtI);
203 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1,
204 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000205
Chris Lattner7e044912010-01-04 07:17:19 +0000206 // If all of the demanded bits are known zero on one side, return the
207 // other. These bits cannot contribute to the result of the 'or' in this
208 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000209 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000210 (DemandedMask & ~LHSKnownOne))
211 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000212 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000213 (DemandedMask & ~RHSKnownOne))
214 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000215
Chris Lattner7e044912010-01-04 07:17:19 +0000216 // If all of the potentially set bits on one side are known to be set on
217 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000218 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000219 (DemandedMask & (~RHSKnownZero)))
220 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000221 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000222 (DemandedMask & (~LHSKnownZero)))
223 return I->getOperand(1);
Shuxin Yang73285932012-12-04 22:15:32 +0000224 } else if (I->getOpcode() == Instruction::Xor) {
225 // We can simplify (X^Y) -> X or Y in the user's context if we know that
226 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000227
Hal Finkel60db0582014-09-07 18:57:58 +0000228 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1,
229 CxtI);
230 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1,
231 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000232
Shuxin Yang73285932012-12-04 22:15:32 +0000233 // If all of the demanded bits are known zero on one side, return the
Craig Topper4c947752012-12-22 18:09:02 +0000234 // other.
Shuxin Yang73285932012-12-04 22:15:32 +0000235 if ((DemandedMask & RHSKnownZero) == DemandedMask)
236 return I->getOperand(0);
237 if ((DemandedMask & LHSKnownZero) == DemandedMask)
238 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000239 }
Shuxin Yang73285932012-12-04 22:15:32 +0000240
Chris Lattner7e044912010-01-04 07:17:19 +0000241 // Compute the KnownZero/KnownOne bits to simplify things downstream.
Hal Finkel60db0582014-09-07 18:57:58 +0000242 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000243 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000244 }
Craig Topper4c947752012-12-22 18:09:02 +0000245
Chris Lattner7e044912010-01-04 07:17:19 +0000246 // If this is the root being simplified, allow it to have multiple uses,
247 // just set the DemandedMask to all bits so that we can try to simplify the
248 // operands. This allows visitTruncInst (for example) to simplify the
249 // operand of a trunc without duplicating all the logic below.
250 if (Depth == 0 && !V->hasOneUse())
251 DemandedMask = APInt::getAllOnesValue(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000252
Chris Lattner7e044912010-01-04 07:17:19 +0000253 switch (I->getOpcode()) {
254 default:
Hal Finkel60db0582014-09-07 18:57:58 +0000255 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000256 break;
257 case Instruction::And:
258 // If either the LHS or the RHS are Zero, the result is zero.
259 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
260 RHSKnownZero, RHSKnownOne, Depth+1) ||
261 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
262 LHSKnownZero, LHSKnownOne, Depth+1))
263 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000264 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
265 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000266
267 // If all of the demanded bits are known 1 on one side, return the other.
268 // These bits cannot contribute to the result of the 'and'.
Craig Topper4c947752012-12-22 18:09:02 +0000269 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000270 (DemandedMask & ~LHSKnownZero))
271 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000272 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000273 (DemandedMask & ~RHSKnownZero))
274 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000275
Chris Lattner7e044912010-01-04 07:17:19 +0000276 // If all of the demanded bits in the inputs are known zeros, return zero.
277 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
278 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000279
Chris Lattner7e044912010-01-04 07:17:19 +0000280 // If the RHS is a constant, see if we can simplify it.
281 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
282 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000283
Chris Lattner7e044912010-01-04 07:17:19 +0000284 // Output known-1 bits are only known if set in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000285 KnownOne = RHSKnownOne & LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000286 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000287 KnownZero = RHSKnownZero | LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000288 break;
289 case Instruction::Or:
290 // If either the LHS or the RHS are One, the result is One.
Craig Topper4c947752012-12-22 18:09:02 +0000291 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +0000292 RHSKnownZero, RHSKnownOne, Depth+1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000293 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne,
Chris Lattner7e044912010-01-04 07:17:19 +0000294 LHSKnownZero, LHSKnownOne, Depth+1))
295 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000296 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
297 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
298
Chris Lattner7e044912010-01-04 07:17:19 +0000299 // If all of the demanded bits are known zero on one side, return the other.
300 // These bits cannot contribute to the result of the 'or'.
Craig Topper4c947752012-12-22 18:09:02 +0000301 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000302 (DemandedMask & ~LHSKnownOne))
303 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000304 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000305 (DemandedMask & ~RHSKnownOne))
306 return I->getOperand(1);
307
308 // If all of the potentially set bits on one side are known to be set on
309 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000310 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000311 (DemandedMask & (~RHSKnownZero)))
312 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000313 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000314 (DemandedMask & (~LHSKnownZero)))
315 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000316
Chris Lattner7e044912010-01-04 07:17:19 +0000317 // If the RHS is a constant, see if we can simplify it.
318 if (ShrinkDemandedConstant(I, 1, DemandedMask))
319 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000320
Chris Lattner7e044912010-01-04 07:17:19 +0000321 // Output known-0 bits are only known if clear in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000322 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000323 // Output known-1 are known to be set if set in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000324 KnownOne = RHSKnownOne | LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000325 break;
326 case Instruction::Xor: {
327 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
328 RHSKnownZero, RHSKnownOne, Depth+1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000329 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +0000330 LHSKnownZero, LHSKnownOne, Depth+1))
331 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000332 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
333 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
334
Chris Lattner7e044912010-01-04 07:17:19 +0000335 // If all of the demanded bits are known zero on one side, return the other.
336 // These bits cannot contribute to the result of the 'xor'.
337 if ((DemandedMask & RHSKnownZero) == DemandedMask)
338 return I->getOperand(0);
339 if ((DemandedMask & LHSKnownZero) == DemandedMask)
340 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000341
Chris Lattner7e044912010-01-04 07:17:19 +0000342 // If all of the demanded bits are known to be zero on one side or the
343 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000344 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Chris Lattner7e044912010-01-04 07:17:19 +0000345 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
Craig Topper4c947752012-12-22 18:09:02 +0000346 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000347 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
348 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000349 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000350 }
Craig Topper4c947752012-12-22 18:09:02 +0000351
Chris Lattner7e044912010-01-04 07:17:19 +0000352 // If all of the demanded bits on one side are known, and all of the set
353 // bits on that side are also known to be set on the other side, turn this
354 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000355 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper4c947752012-12-22 18:09:02 +0000356 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
Chris Lattner7e044912010-01-04 07:17:19 +0000357 // all known
358 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
359 Constant *AndC = Constant::getIntegerValue(VTy,
360 ~RHSKnownOne & DemandedMask);
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000361 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000362 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000363 }
364 }
Craig Topper4c947752012-12-22 18:09:02 +0000365
Chris Lattner7e044912010-01-04 07:17:19 +0000366 // If the RHS is a constant, see if we can simplify it.
367 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
368 if (ShrinkDemandedConstant(I, 1, DemandedMask))
369 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000370
Chris Lattner7e044912010-01-04 07:17:19 +0000371 // If our LHS is an 'and' and if it has one use, and if any of the bits we
372 // are flipping are known to be set, then the xor is just resetting those
373 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
374 // simplifying both of them.
375 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
376 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
377 isa<ConstantInt>(I->getOperand(1)) &&
378 isa<ConstantInt>(LHSInst->getOperand(1)) &&
379 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
380 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
381 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
382 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000383
Chris Lattner7e044912010-01-04 07:17:19 +0000384 Constant *AndC =
385 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000386 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000387 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000388
Chris Lattner7e044912010-01-04 07:17:19 +0000389 Constant *XorC =
390 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000391 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000392 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000393 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000394
395 // Output known-0 bits are known if clear or set in both the LHS & RHS.
396 KnownZero= (RHSKnownZero & LHSKnownZero) | (RHSKnownOne & LHSKnownOne);
397 // Output known-1 are known to be set if set in only one of the LHS, RHS.
398 KnownOne = (RHSKnownZero & LHSKnownOne) | (RHSKnownOne & LHSKnownZero);
Chris Lattner7e044912010-01-04 07:17:19 +0000399 break;
400 }
401 case Instruction::Select:
402 if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask,
403 RHSKnownZero, RHSKnownOne, Depth+1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000404 SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +0000405 LHSKnownZero, LHSKnownOne, Depth+1))
406 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000407 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
408 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
409
Chris Lattner7e044912010-01-04 07:17:19 +0000410 // If the operands are constants, see if we can simplify them.
411 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
412 ShrinkDemandedConstant(I, 2, DemandedMask))
413 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000414
Chris Lattner7e044912010-01-04 07:17:19 +0000415 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000416 KnownOne = RHSKnownOne & LHSKnownOne;
417 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000418 break;
419 case Instruction::Trunc: {
420 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000421 DemandedMask = DemandedMask.zext(truncBf);
422 KnownZero = KnownZero.zext(truncBf);
423 KnownOne = KnownOne.zext(truncBf);
Craig Topper4c947752012-12-22 18:09:02 +0000424 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000425 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000426 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000427 DemandedMask = DemandedMask.trunc(BitWidth);
428 KnownZero = KnownZero.trunc(BitWidth);
429 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000430 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000431 break;
432 }
433 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000434 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000435 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000436
Chris Lattner229907c2011-07-18 04:54:35 +0000437 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
438 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000439 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
440 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
441 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000442 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000443 } else
444 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000445 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000446 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000447 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000448 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000449
450 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000451 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000452 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000453 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000454 break;
455 case Instruction::ZExt: {
456 // Compute the bits in the result that are not present in the input.
457 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000458
Jay Foad583abbc2010-12-07 08:25:19 +0000459 DemandedMask = DemandedMask.trunc(SrcBitWidth);
460 KnownZero = KnownZero.trunc(SrcBitWidth);
461 KnownOne = KnownOne.trunc(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000462 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000463 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000464 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000465 DemandedMask = DemandedMask.zext(BitWidth);
466 KnownZero = KnownZero.zext(BitWidth);
467 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000468 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000469 // The top bits are known to be zero.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000470 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000471 break;
472 }
473 case Instruction::SExt: {
474 // Compute the bits in the result that are not present in the input.
475 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000476
477 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000478 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
479
480 APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
481 // If any of the sign extended bits are demanded, we know that the sign
482 // bit is demanded.
483 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000484 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000485
Jay Foad583abbc2010-12-07 08:25:19 +0000486 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
487 KnownZero = KnownZero.trunc(SrcBitWidth);
488 KnownOne = KnownOne.trunc(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000489 if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000490 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000491 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000492 InputDemandedBits = InputDemandedBits.zext(BitWidth);
493 KnownZero = KnownZero.zext(BitWidth);
494 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000495 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
496
Chris Lattner7e044912010-01-04 07:17:19 +0000497 // If the sign bit of the input is known set or clear, then we know the
498 // top bits of the result.
499
500 // If the input sign bit is known zero, or if the NewBits are not demanded
501 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000502 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000503 // Convert to ZExt cast
504 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000505 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000506 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
507 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000508 }
509 break;
510 }
511 case Instruction::Add: {
512 // Figure out what the input bits are. If the top bits of the and result
513 // are not demanded, then the add doesn't demand them from its input
514 // either.
515 unsigned NLZ = DemandedMask.countLeadingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000516
Chris Lattner7e044912010-01-04 07:17:19 +0000517 // If there is a constant on the RHS, there are a variety of xformations
518 // we can do.
519 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
520 // If null, this should be simplified elsewhere. Some of the xforms here
521 // won't work if the RHS is zero.
522 if (RHS->isZero())
523 break;
Craig Topper4c947752012-12-22 18:09:02 +0000524
Chris Lattner7e044912010-01-04 07:17:19 +0000525 // If the top bit of the output is demanded, demand everything from the
526 // input. Otherwise, we demand all the input bits except NLZ top bits.
527 APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
528
529 // Find information about known zero/one bits in the input.
Craig Topper4c947752012-12-22 18:09:02 +0000530 if (SimplifyDemandedBits(I->getOperandUse(0), InDemandedBits,
Chris Lattner7e044912010-01-04 07:17:19 +0000531 LHSKnownZero, LHSKnownOne, Depth+1))
532 return I;
533
534 // If the RHS of the add has bits set that can't affect the input, reduce
535 // the constant.
536 if (ShrinkDemandedConstant(I, 1, InDemandedBits))
537 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000538
Chris Lattner7e044912010-01-04 07:17:19 +0000539 // Avoid excess work.
540 if (LHSKnownZero == 0 && LHSKnownOne == 0)
541 break;
Craig Topper4c947752012-12-22 18:09:02 +0000542
Chris Lattner7e044912010-01-04 07:17:19 +0000543 // Turn it into OR if input bits are zero.
544 if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
545 Instruction *Or =
546 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
547 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000548 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000549 }
Craig Topper4c947752012-12-22 18:09:02 +0000550
Chris Lattner7e044912010-01-04 07:17:19 +0000551 // We can say something about the output known-zero and known-one bits,
552 // depending on potential carries from the input constant and the
553 // unknowns. For example if the LHS is known to have at most the 0x0F0F0
554 // bits set and the RHS constant is 0x01001, then we know we have a known
555 // one mask of 0x00001 and a known zero mask of 0xE0F0E.
Craig Topper4c947752012-12-22 18:09:02 +0000556
Chris Lattner7e044912010-01-04 07:17:19 +0000557 // To compute this, we first compute the potential carry bits. These are
558 // the bits which may be modified. I'm not aware of a better way to do
559 // this scan.
560 const APInt &RHSVal = RHS->getValue();
561 APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
Craig Topper4c947752012-12-22 18:09:02 +0000562
Chris Lattner7e044912010-01-04 07:17:19 +0000563 // Now that we know which bits have carries, compute the known-1/0 sets.
Craig Topper4c947752012-12-22 18:09:02 +0000564
Chris Lattner7e044912010-01-04 07:17:19 +0000565 // Bits are known one if they are known zero in one operand and one in the
566 // other, and there is no input carry.
Craig Topper4c947752012-12-22 18:09:02 +0000567 KnownOne = ((LHSKnownZero & RHSVal) |
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000568 (LHSKnownOne & ~RHSVal)) & ~CarryBits;
Craig Topper4c947752012-12-22 18:09:02 +0000569
Chris Lattner7e044912010-01-04 07:17:19 +0000570 // Bits are known zero if they are known zero in both operands and there
571 // is no input carry.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000572 KnownZero = LHSKnownZero & ~RHSVal & ~CarryBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000573 } else {
574 // If the high-bits of this ADD are not demanded, then it does not demand
575 // the high bits of its LHS or RHS.
576 if (DemandedMask[BitWidth-1] == 0) {
577 // Right fill the mask of bits for this ADD to demand the most
578 // significant bit and all those below it.
579 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
580 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
581 LHSKnownZero, LHSKnownOne, Depth+1) ||
582 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
583 LHSKnownZero, LHSKnownOne, Depth+1))
584 return I;
585 }
586 }
587 break;
588 }
589 case Instruction::Sub:
590 // If the high-bits of this SUB are not demanded, then it does not demand
591 // the high bits of its LHS or RHS.
592 if (DemandedMask[BitWidth-1] == 0) {
593 // Right fill the mask of bits for this SUB to demand the most
594 // significant bit and all those below it.
595 uint32_t NLZ = DemandedMask.countLeadingZeros();
596 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
597 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
598 LHSKnownZero, LHSKnownOne, Depth+1) ||
599 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
600 LHSKnownZero, LHSKnownOne, Depth+1))
601 return I;
602 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000603
Jay Foada0653a32014-05-14 21:14:37 +0000604 // Otherwise just hand the sub off to computeKnownBits to fill in
Chris Lattner7e044912010-01-04 07:17:19 +0000605 // the known zeros and ones.
Hal Finkel60db0582014-09-07 18:57:58 +0000606 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Benjamin Kramer010337c2011-12-24 17:31:38 +0000607
608 // Turn this into a xor if LHS is 2^n-1 and the remaining bits are known
609 // zero.
610 if (ConstantInt *C0 = dyn_cast<ConstantInt>(I->getOperand(0))) {
611 APInt I0 = C0->getValue();
612 if ((I0 + 1).isPowerOf2() && (I0 | KnownZero).isAllOnesValue()) {
613 Instruction *Xor = BinaryOperator::CreateXor(I->getOperand(1), C0);
614 return InsertNewInstWith(Xor, *I);
615 }
616 }
Chris Lattner7e044912010-01-04 07:17:19 +0000617 break;
618 case Instruction::Shl:
619 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000620 {
621 Value *VarX; ConstantInt *C1;
622 if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
623 Instruction *Shr = cast<Instruction>(I->getOperand(0));
624 Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
625 KnownZero, KnownOne);
626 if (R)
627 return R;
628 }
629 }
630
Chris Lattner768003c2011-02-10 05:09:34 +0000631 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000632 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000633
Chris Lattner768003c2011-02-10 05:09:34 +0000634 // If the shift is NUW/NSW, then it does demand the high bits.
635 ShlOperator *IOp = cast<ShlOperator>(I);
636 if (IOp->hasNoSignedWrap())
637 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
638 else if (IOp->hasNoUnsignedWrap())
639 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000640
641 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000642 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000643 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000644 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
645 KnownZero <<= ShiftAmt;
646 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000647 // low bits known zero.
648 if (ShiftAmt)
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000649 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000650 }
651 break;
652 case Instruction::LShr:
653 // For a logical shift right
654 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000655 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000656
Chris Lattner7e044912010-01-04 07:17:19 +0000657 // Unsigned shift right.
658 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000659
Chris Lattner768003c2011-02-10 05:09:34 +0000660 // If the shift is exact, then it does demand the low bits (and knows that
661 // they are zero).
662 if (cast<LShrOperator>(I)->isExact())
663 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000664
Chris Lattner7e044912010-01-04 07:17:19 +0000665 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000666 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000667 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000668 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
669 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
670 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000671 if (ShiftAmt) {
672 // Compute the new bits that are at the top now.
673 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000674 KnownZero |= HighBits; // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000675 }
676 }
677 break;
678 case Instruction::AShr:
679 // If this is an arithmetic shift right and only the low-bit is set, we can
680 // always convert this into a logical shr, even if the shift amount is
681 // variable. The low bit of the shift cannot be an input sign bit unless
682 // the shift amount is >= the size of the datatype, which is undefined.
683 if (DemandedMask == 1) {
684 // Perform the logical shift right.
685 Instruction *NewVal = BinaryOperator::CreateLShr(
686 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000687 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000688 }
Chris Lattner7e044912010-01-04 07:17:19 +0000689
690 // If the sign bit is the only bit demanded by this ashr, then there is no
691 // need to do it, the shift doesn't change the high bit.
692 if (DemandedMask.isSignBit())
693 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000694
Chris Lattner7e044912010-01-04 07:17:19 +0000695 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000696 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000697
Chris Lattner7e044912010-01-04 07:17:19 +0000698 // Signed shift right.
699 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
700 // If any of the "high bits" are demanded, we should set the sign bit as
701 // demanded.
702 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000703 DemandedMaskIn.setBit(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000704
Chris Lattner768003c2011-02-10 05:09:34 +0000705 // If the shift is exact, then it does demand the low bits (and knows that
706 // they are zero).
707 if (cast<AShrOperator>(I)->isExact())
708 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000709
Chris Lattner7e044912010-01-04 07:17:19 +0000710 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000711 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000712 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000713 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000714 // Compute the new bits that are at the top now.
715 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000716 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
717 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000718
Chris Lattner7e044912010-01-04 07:17:19 +0000719 // Handle the sign bits.
720 APInt SignBit(APInt::getSignBit(BitWidth));
721 // Adjust to where it is now in the mask.
Craig Topper4c947752012-12-22 18:09:02 +0000722 SignBit = APIntOps::lshr(SignBit, ShiftAmt);
723
Chris Lattner7e044912010-01-04 07:17:19 +0000724 // If the input sign bit is known to be zero, or if none of the top bits
725 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000726 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Chris Lattner7e044912010-01-04 07:17:19 +0000727 (HighBits & ~DemandedMask) == HighBits) {
728 // Perform the logical shift right.
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000729 BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
730 SA, I->getName());
731 NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000732 return InsertNewInstWith(NewVal, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000733 } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
734 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000735 }
736 }
737 break;
738 case Instruction::SRem:
739 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000740 // X % -1 demands all the bits because we don't want to introduce
741 // INT_MIN % -1 (== undef) by accident.
742 if (Rem->isAllOnesValue())
743 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000744 APInt RA = Rem->getValue().abs();
745 if (RA.isPowerOf2()) {
746 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
747 return I->getOperand(0);
748
749 APInt LowBits = RA - 1;
750 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
751 if (SimplifyDemandedBits(I->getOperandUse(0), Mask2,
752 LHSKnownZero, LHSKnownOne, Depth+1))
753 return I;
754
Duncan Sands3a48b872010-01-28 17:22:42 +0000755 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000756 KnownZero = LHSKnownZero & LowBits;
757 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000758
Duncan Sands3a48b872010-01-28 17:22:42 +0000759 // If LHS is non-negative or has all low bits zero, then the upper bits
760 // are all zero.
761 if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
762 KnownZero |= ~LowBits;
763
764 // If LHS is negative and not all low bits are zero, then the upper bits
765 // are all one.
766 if (LHSKnownOne[BitWidth-1] && ((LHSKnownOne & LowBits) != 0))
767 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000768
Craig Topper4c947752012-12-22 18:09:02 +0000769 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000770 }
771 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000772
773 // The sign bit is the LHS's sign bit, except when the result of the
774 // remainder is zero.
775 if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
Nick Lewyckye4679792011-03-07 01:50:10 +0000776 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Hal Finkel60db0582014-09-07 18:57:58 +0000777 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1,
778 CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000779 // If it's known zero, our sign bit is also zero.
780 if (LHSKnownZero.isNegative())
Benjamin Kramer21b972a2013-05-09 16:32:32 +0000781 KnownZero.setBit(KnownZero.getBitWidth() - 1);
Nick Lewyckye4679792011-03-07 01:50:10 +0000782 }
Chris Lattner7e044912010-01-04 07:17:19 +0000783 break;
784 case Instruction::URem: {
785 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
786 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
787 if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes,
788 KnownZero2, KnownOne2, Depth+1) ||
789 SimplifyDemandedBits(I->getOperandUse(1), AllOnes,
790 KnownZero2, KnownOne2, Depth+1))
791 return I;
792
793 unsigned Leaders = KnownZero2.countLeadingOnes();
794 Leaders = std::max(Leaders,
795 KnownZero2.countLeadingOnes());
796 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
797 break;
798 }
799 case Instruction::Call:
800 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
801 switch (II->getIntrinsicID()) {
802 default: break;
803 case Intrinsic::bswap: {
804 // If the only bits demanded come from one byte of the bswap result,
805 // just shift the input byte into position to eliminate the bswap.
806 unsigned NLZ = DemandedMask.countLeadingZeros();
807 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000808
Chris Lattner7e044912010-01-04 07:17:19 +0000809 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
810 // we need all the bits down to bit 8. Likewise, round NLZ. If we
811 // have 14 leading zeros, round to 8.
812 NLZ &= ~7;
813 NTZ &= ~7;
814 // If we need exactly one byte, we can do this transformation.
815 if (BitWidth-NLZ-NTZ == 8) {
816 unsigned ResultBit = NTZ;
817 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000818
Chris Lattner7e044912010-01-04 07:17:19 +0000819 // Replace this with either a left or right shift to get the byte into
820 // the right place.
821 Instruction *NewVal;
822 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000823 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000824 ConstantInt::get(I->getType(), InputBit-ResultBit));
825 else
Gabor Greif79430172010-06-24 12:35:13 +0000826 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000827 ConstantInt::get(I->getType(), ResultBit-InputBit));
828 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000829 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000830 }
Craig Topper4c947752012-12-22 18:09:02 +0000831
Chris Lattner7e044912010-01-04 07:17:19 +0000832 // TODO: Could compute known zero/one bits based on the input.
833 break;
834 }
Chad Rosierb3628842011-05-26 23:13:19 +0000835 case Intrinsic::x86_sse42_crc32_64_64:
Evan Chenge8d2e9e2011-05-20 00:54:37 +0000836 KnownZero = APInt::getHighBitsSet(64, 32);
Craig Topperf40110f2014-04-25 05:29:35 +0000837 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000838 }
839 }
Hal Finkel60db0582014-09-07 18:57:58 +0000840 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000841 break;
842 }
Craig Topper4c947752012-12-22 18:09:02 +0000843
Chris Lattner7e044912010-01-04 07:17:19 +0000844 // If the client is only demanding bits that we know, return the known
845 // constant.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000846 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
847 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000848 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000849}
850
Shuxin Yang63e999e2012-12-04 00:04:54 +0000851/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
852/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
853/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
854/// of "C2-C1".
855///
856/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
857/// ..., bn}, without considering the specific value X is holding.
858/// This transformation is legal iff one of following conditions is hold:
859/// 1) All the bit in S are 0, in this case E1 == E2.
860/// 2) We don't care those bits in S, per the input DemandedMask.
861/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
862/// rest bits.
863///
864/// Currently we only test condition 2).
865///
866/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
867/// not successful.
868Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
869 Instruction *Shl, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne) {
870
Benjamin Kramer010f1082013-08-30 14:35:35 +0000871 const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
872 const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
873 if (!ShlOp1 || !ShrOp1)
Craig Topperf40110f2014-04-25 05:29:35 +0000874 return nullptr; // Noop.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000875
876 Value *VarX = Shr->getOperand(0);
877 Type *Ty = VarX->getType();
878 unsigned BitWidth = Ty->getIntegerBitWidth();
879 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000880 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000881
882 unsigned ShlAmt = ShlOp1.getZExtValue();
883 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000884
885 KnownOne.clearAllBits();
886 KnownZero = APInt::getBitsSet(KnownZero.getBitWidth(), 0, ShlAmt-1);
887 KnownZero &= DemandedMask;
888
Benjamin Kramer010f1082013-08-30 14:35:35 +0000889 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
890 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000891
892 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
893 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
894 (BitMask1.ashr(ShrAmt) << ShlAmt);
895
896 if (ShrAmt <= ShlAmt) {
897 BitMask2 <<= (ShlAmt - ShrAmt);
898 } else {
899 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
900 BitMask2.ashr(ShrAmt - ShlAmt);
901 }
902
903 // Check if condition-2 (see the comment to this function) is satified.
904 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
905 if (ShrAmt == ShlAmt)
906 return VarX;
907
908 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000909 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000910
911 BinaryOperator *New;
912 if (ShrAmt < ShlAmt) {
913 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
914 New = BinaryOperator::CreateShl(VarX, Amt);
915 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
916 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
917 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
918 } else {
919 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000920 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
921 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000922 if (cast<BinaryOperator>(Shr)->isExact())
923 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000924 }
925
926 return InsertNewInstWith(New, *Shl);
927 }
928
Craig Topperf40110f2014-04-25 05:29:35 +0000929 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000930}
Chris Lattner7e044912010-01-04 07:17:19 +0000931
932/// SimplifyDemandedVectorElts - The specified value produces a vector with
933/// any number of elements. DemandedElts contains the set of elements that are
934/// actually used by the caller. This method analyzes which elements of the
935/// operand are undef and returns that information in UndefElts.
936///
937/// If the information about demanded elements can be used to simplify the
938/// operation, the operation is simplified, then the resultant value is
939/// returned. This returns null if no change was made.
940Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000941 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000942 unsigned Depth) {
943 unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
944 APInt EltMask(APInt::getAllOnesValue(VWidth));
945 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
946
947 if (isa<UndefValue>(V)) {
948 // If the entire vector is undefined, just return this info.
949 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000950 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000951 }
Craig Topper4c947752012-12-22 18:09:02 +0000952
Chris Lattnerb22423c2010-02-08 23:56:03 +0000953 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000954 UndefElts = EltMask;
955 return UndefValue::get(V->getType());
956 }
957
958 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000959
Chris Lattner67058832012-01-25 06:48:06 +0000960 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
961 if (Constant *C = dyn_cast<Constant>(V)) {
962 // Check if this is identity. If so, return 0 since we are not simplifying
963 // anything.
964 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000965 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000966
Chris Lattner229907c2011-07-18 04:54:35 +0000967 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000968 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000969
Chris Lattner67058832012-01-25 06:48:06 +0000970 SmallVector<Constant*, 16> Elts;
971 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000972 if (!DemandedElts[i]) { // If not demanded, set to undef.
973 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000974 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +0000975 continue;
976 }
Craig Topper4c947752012-12-22 18:09:02 +0000977
Chris Lattner67058832012-01-25 06:48:06 +0000978 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000979 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000980
Chris Lattner67058832012-01-25 06:48:06 +0000981 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000982 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000983 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +0000984 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +0000985 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +0000986 }
Chris Lattner67058832012-01-25 06:48:06 +0000987 }
Craig Topper4c947752012-12-22 18:09:02 +0000988
Chris Lattner7e044912010-01-04 07:17:19 +0000989 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +0000990 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +0000991 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000992 }
Craig Topper4c947752012-12-22 18:09:02 +0000993
Chris Lattner7e044912010-01-04 07:17:19 +0000994 // Limit search depth.
995 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +0000996 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000997
Stuart Hastings5bd18b62011-05-17 22:13:31 +0000998 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +0000999 // simplification conservatively assuming that all elements
1000 // are needed.
1001 if (!V->hasOneUse()) {
1002 // Quit if we find multiple users of a non-root value though.
1003 // They'll be handled when it's their turn to be visited by
1004 // the main instcombine process.
1005 if (Depth != 0)
1006 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +00001007 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001008
1009 // Conservatively assume that all elements are needed.
1010 DemandedElts = EltMask;
1011 }
Craig Topper4c947752012-12-22 18:09:02 +00001012
Chris Lattner7e044912010-01-04 07:17:19 +00001013 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +00001014 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +00001015
Chris Lattner7e044912010-01-04 07:17:19 +00001016 bool MadeChange = false;
1017 APInt UndefElts2(VWidth, 0);
1018 Value *TmpV;
1019 switch (I->getOpcode()) {
1020 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001021
Chris Lattner7e044912010-01-04 07:17:19 +00001022 case Instruction::InsertElement: {
1023 // If this is a variable index, we don't know which element it overwrites.
1024 // demand exactly the same input as we produce.
1025 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +00001026 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +00001027 // Note that we can't propagate undef elt info, because we don't know
1028 // which elt is getting updated.
1029 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1030 UndefElts2, Depth+1);
1031 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1032 break;
1033 }
Craig Topper4c947752012-12-22 18:09:02 +00001034
Chris Lattner7e044912010-01-04 07:17:19 +00001035 // If this is inserting an element that isn't demanded, remove this
1036 // insertelement.
1037 unsigned IdxNo = Idx->getZExtValue();
1038 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1039 Worklist.Add(I);
1040 return I->getOperand(0);
1041 }
Craig Topper4c947752012-12-22 18:09:02 +00001042
Chris Lattner7e044912010-01-04 07:17:19 +00001043 // Otherwise, the element inserted overwrites whatever was there, so the
1044 // input demanded set is simpler than the output set.
1045 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001046 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001047 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
1048 UndefElts, Depth+1);
1049 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1050
1051 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001052 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001053 break;
1054 }
1055 case Instruction::ShuffleVector: {
1056 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
1057 uint64_t LHSVWidth =
1058 cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements();
1059 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1060 for (unsigned i = 0; i < VWidth; i++) {
1061 if (DemandedElts[i]) {
1062 unsigned MaskVal = Shuffle->getMaskValue(i);
1063 if (MaskVal != -1u) {
1064 assert(MaskVal < LHSVWidth * 2 &&
1065 "shufflevector mask index out of range!");
1066 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001067 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001068 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001069 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001070 }
1071 }
1072 }
1073
1074 APInt UndefElts4(LHSVWidth, 0);
1075 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
1076 UndefElts4, Depth+1);
1077 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1078
1079 APInt UndefElts3(LHSVWidth, 0);
1080 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
1081 UndefElts3, Depth+1);
1082 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1083
1084 bool NewUndefElts = false;
1085 for (unsigned i = 0; i < VWidth; i++) {
1086 unsigned MaskVal = Shuffle->getMaskValue(i);
1087 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001088 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001089 } else if (!DemandedElts[i]) {
1090 NewUndefElts = true;
1091 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001092 } else if (MaskVal < LHSVWidth) {
1093 if (UndefElts4[MaskVal]) {
1094 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001095 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001096 }
1097 } else {
1098 if (UndefElts3[MaskVal - LHSVWidth]) {
1099 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001100 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001101 }
1102 }
1103 }
1104
1105 if (NewUndefElts) {
1106 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001107 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001108 for (unsigned i = 0; i < VWidth; ++i) {
1109 if (UndefElts[i])
1110 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1111 else
1112 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1113 Shuffle->getMaskValue(i)));
1114 }
1115 I->setOperand(2, ConstantVector::get(Elts));
1116 MadeChange = true;
1117 }
1118 break;
1119 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001120 case Instruction::Select: {
1121 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1122 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1123 for (unsigned i = 0; i < VWidth; i++) {
1124 if (CV->getAggregateElement(i)->isNullValue())
1125 LeftDemanded.clearBit(i);
1126 else
1127 RightDemanded.clearBit(i);
1128 }
1129 }
1130
1131 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded,
1132 UndefElts, Depth+1);
1133 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1134
1135 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
1136 UndefElts2, Depth+1);
1137 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001138
Pete Cooperabc13af2012-07-26 23:10:24 +00001139 // Output elements are undefined if both are undefined.
1140 UndefElts &= UndefElts2;
1141 break;
1142 }
Chris Lattner7e044912010-01-04 07:17:19 +00001143 case Instruction::BitCast: {
1144 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001145 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001146 if (!VTy) break;
1147 unsigned InVWidth = VTy->getNumElements();
1148 APInt InputDemandedElts(InVWidth, 0);
1149 unsigned Ratio;
1150
1151 if (VWidth == InVWidth) {
1152 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1153 // elements as are demanded of us.
1154 Ratio = 1;
1155 InputDemandedElts = DemandedElts;
1156 } else if (VWidth > InVWidth) {
1157 // Untested so far.
1158 break;
Craig Topper4c947752012-12-22 18:09:02 +00001159
Chris Lattner7e044912010-01-04 07:17:19 +00001160 // If there are more elements in the result than there are in the source,
1161 // then an input element is live if any of the corresponding output
1162 // elements are live.
1163 Ratio = VWidth/InVWidth;
1164 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1165 if (DemandedElts[OutIdx])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001166 InputDemandedElts.setBit(OutIdx/Ratio);
Chris Lattner7e044912010-01-04 07:17:19 +00001167 }
1168 } else {
1169 // Untested so far.
1170 break;
Craig Topper4c947752012-12-22 18:09:02 +00001171
Chris Lattner7e044912010-01-04 07:17:19 +00001172 // If there are more elements in the source than there are in the result,
1173 // then an input element is live if the corresponding output element is
1174 // live.
1175 Ratio = InVWidth/VWidth;
1176 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1177 if (DemandedElts[InIdx/Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001178 InputDemandedElts.setBit(InIdx);
Chris Lattner7e044912010-01-04 07:17:19 +00001179 }
Craig Topper4c947752012-12-22 18:09:02 +00001180
Chris Lattner7e044912010-01-04 07:17:19 +00001181 // div/rem demand all inputs, because they don't want divide by zero.
1182 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
1183 UndefElts2, Depth+1);
1184 if (TmpV) {
1185 I->setOperand(0, TmpV);
1186 MadeChange = true;
1187 }
Craig Topper4c947752012-12-22 18:09:02 +00001188
Chris Lattner7e044912010-01-04 07:17:19 +00001189 UndefElts = UndefElts2;
1190 if (VWidth > InVWidth) {
1191 llvm_unreachable("Unimp");
1192 // If there are more elements in the result than there are in the source,
1193 // then an output element is undef if the corresponding input element is
1194 // undef.
1195 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1196 if (UndefElts2[OutIdx/Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001197 UndefElts.setBit(OutIdx);
Chris Lattner7e044912010-01-04 07:17:19 +00001198 } else if (VWidth < InVWidth) {
1199 llvm_unreachable("Unimp");
1200 // If there are more elements in the source than there are in the result,
1201 // then a result element is undef if all of the corresponding input
1202 // elements are undef.
1203 UndefElts = ~0ULL >> (64-VWidth); // Start out all undef.
1204 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1205 if (!UndefElts2[InIdx]) // Not undef?
Jay Foad25a5e4c2010-12-01 08:53:58 +00001206 UndefElts.clearBit(InIdx/Ratio); // Clear undef bit.
Chris Lattner7e044912010-01-04 07:17:19 +00001207 }
1208 break;
1209 }
1210 case Instruction::And:
1211 case Instruction::Or:
1212 case Instruction::Xor:
1213 case Instruction::Add:
1214 case Instruction::Sub:
1215 case Instruction::Mul:
1216 // div/rem demand all inputs, because they don't want divide by zero.
1217 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1218 UndefElts, Depth+1);
1219 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1220 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
1221 UndefElts2, Depth+1);
1222 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001223
Chris Lattner7e044912010-01-04 07:17:19 +00001224 // Output elements are undefined if both are undefined. Consider things
1225 // like undef&0. The result is known zero, not undef.
1226 UndefElts &= UndefElts2;
1227 break;
Pete Coopere807e452012-07-26 22:37:04 +00001228 case Instruction::FPTrunc:
1229 case Instruction::FPExt:
1230 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1231 UndefElts, Depth+1);
1232 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1233 break;
Craig Topper4c947752012-12-22 18:09:02 +00001234
Chris Lattner7e044912010-01-04 07:17:19 +00001235 case Instruction::Call: {
1236 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1237 if (!II) break;
1238 switch (II->getIntrinsicID()) {
1239 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001240
Chris Lattner7e044912010-01-04 07:17:19 +00001241 // Binary vector operations that work column-wise. A dest element is a
1242 // function of the corresponding input elements from the two inputs.
1243 case Intrinsic::x86_sse_sub_ss:
1244 case Intrinsic::x86_sse_mul_ss:
1245 case Intrinsic::x86_sse_min_ss:
1246 case Intrinsic::x86_sse_max_ss:
1247 case Intrinsic::x86_sse2_sub_sd:
1248 case Intrinsic::x86_sse2_mul_sd:
1249 case Intrinsic::x86_sse2_min_sd:
1250 case Intrinsic::x86_sse2_max_sd:
Gabor Greife23efee2010-06-28 16:45:00 +00001251 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
Eric Christopher7258dcd2010-04-16 23:37:20 +00001252 UndefElts, Depth+1);
Gabor Greife23efee2010-06-28 16:45:00 +00001253 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1254 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Eric Christopher7258dcd2010-04-16 23:37:20 +00001255 UndefElts2, Depth+1);
Gabor Greife23efee2010-06-28 16:45:00 +00001256 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001257
1258 // If only the low elt is demanded and this is a scalarizable intrinsic,
1259 // scalarize it now.
1260 if (DemandedElts == 1) {
1261 switch (II->getIntrinsicID()) {
1262 default: break;
1263 case Intrinsic::x86_sse_sub_ss:
1264 case Intrinsic::x86_sse_mul_ss:
1265 case Intrinsic::x86_sse2_sub_sd:
1266 case Intrinsic::x86_sse2_mul_sd:
1267 // TODO: Lower MIN/MAX/ABS/etc
Gabor Greif79430172010-06-24 12:35:13 +00001268 Value *LHS = II->getArgOperand(0);
1269 Value *RHS = II->getArgOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +00001270 // Extract the element as scalars.
Craig Topper4c947752012-12-22 18:09:02 +00001271 LHS = InsertNewInstWith(ExtractElementInst::Create(LHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001272 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Eli Friedman6efb64e2011-05-19 01:20:42 +00001273 RHS = InsertNewInstWith(ExtractElementInst::Create(RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001274 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Craig Topper4c947752012-12-22 18:09:02 +00001275
Chris Lattner7e044912010-01-04 07:17:19 +00001276 switch (II->getIntrinsicID()) {
1277 default: llvm_unreachable("Case stmts out of sync!");
1278 case Intrinsic::x86_sse_sub_ss:
1279 case Intrinsic::x86_sse2_sub_sd:
Eli Friedman6efb64e2011-05-19 01:20:42 +00001280 TmpV = InsertNewInstWith(BinaryOperator::CreateFSub(LHS, RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001281 II->getName()), *II);
1282 break;
1283 case Intrinsic::x86_sse_mul_ss:
1284 case Intrinsic::x86_sse2_mul_sd:
Eli Friedman6efb64e2011-05-19 01:20:42 +00001285 TmpV = InsertNewInstWith(BinaryOperator::CreateFMul(LHS, RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001286 II->getName()), *II);
1287 break;
1288 }
Craig Topper4c947752012-12-22 18:09:02 +00001289
Chris Lattner7e044912010-01-04 07:17:19 +00001290 Instruction *New =
1291 InsertElementInst::Create(
1292 UndefValue::get(II->getType()), TmpV,
1293 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U, false),
1294 II->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +00001295 InsertNewInstWith(New, *II);
Chris Lattner7e044912010-01-04 07:17:19 +00001296 return New;
Craig Topper4c947752012-12-22 18:09:02 +00001297 }
Chris Lattner7e044912010-01-04 07:17:19 +00001298 }
Craig Topper4c947752012-12-22 18:09:02 +00001299
Chris Lattner7e044912010-01-04 07:17:19 +00001300 // Output elements are undefined if both are undefined. Consider things
1301 // like undef&0. The result is known zero, not undef.
1302 UndefElts &= UndefElts2;
1303 break;
1304 }
1305 break;
1306 }
1307 }
Craig Topperf40110f2014-04-25 05:29:35 +00001308 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001309}