blob: f0c96bdf76838b9db3c52b196f784286fb8c6971 [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,
Chris Lattner7e044912010-01-04 07:17:19 +000074 KnownZero, KnownOne, 0);
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,
88 KnownZero, KnownOne, Depth);
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,
118 unsigned Depth) {
Craig Toppere73658d2014-04-28 04:05:08 +0000119 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000120 assert(Depth <= 6 && "Limit Search Depth");
121 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000122 Type *VTy = V->getType();
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000123 assert((DL || !VTy->isPointerTy()) &&
Chris Lattner7e044912010-01-04 07:17:19 +0000124 "SimplifyDemandedBits needs to know bit widths!");
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000125 assert((!DL || DL->getTypeSizeInBits(VTy->getScalarType()) == BitWidth) &&
Duncan Sands9dff9be2010-02-15 16:12:20 +0000126 (!VTy->isIntOrIntVectorTy() ||
Chris Lattner7e044912010-01-04 07:17:19 +0000127 VTy->getScalarSizeInBits() == BitWidth) &&
128 KnownZero.getBitWidth() == BitWidth &&
129 KnownOne.getBitWidth() == BitWidth &&
130 "Value *V, DemandedMask, KnownZero and KnownOne "
131 "must have same BitWidth");
132 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
133 // We know all of the bits for a constant!
134 KnownOne = CI->getValue() & DemandedMask;
135 KnownZero = ~KnownOne & DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000136 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000137 }
138 if (isa<ConstantPointerNull>(V)) {
139 // We know all of the bits for a constant!
Jay Foad25a5e4c2010-12-01 08:53:58 +0000140 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000141 KnownZero = DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000142 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000143 }
144
Jay Foad25a5e4c2010-12-01 08:53:58 +0000145 KnownZero.clearAllBits();
146 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000147 if (DemandedMask == 0) { // Not demanding any bits from V.
148 if (isa<UndefValue>(V))
Craig Topperf40110f2014-04-25 05:29:35 +0000149 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000150 return UndefValue::get(VTy);
151 }
Craig Topper4c947752012-12-22 18:09:02 +0000152
Chris Lattner7e044912010-01-04 07:17:19 +0000153 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000154 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000155
Chris Lattner7e044912010-01-04 07:17:19 +0000156 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000157 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +0000158
159 Instruction *I = dyn_cast<Instruction>(V);
160 if (!I) {
Jay Foada0653a32014-05-14 21:14:37 +0000161 computeKnownBits(V, KnownZero, KnownOne, Depth);
Craig Topperf40110f2014-04-25 05:29:35 +0000162 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000163 }
164
165 // If there are multiple uses of this value and we aren't at the root, then
166 // we can't do any simplifications of the operands, because DemandedMask
167 // only reflects the bits demanded by *one* of the users.
168 if (Depth != 0 && !I->hasOneUse()) {
169 // Despite the fact that we can't simplify this instruction in all User's
170 // context, we can at least compute the knownzero/knownone bits, and we can
171 // do simplifications that apply to *just* the one user if we know that
172 // this instruction has a simpler value in that context.
173 if (I->getOpcode() == Instruction::And) {
174 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +0000175 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
176 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
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.
Jay Foada0653a32014-05-14 21:14:37 +0000197 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
198 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
Craig Topper4c947752012-12-22 18:09:02 +0000199
Chris Lattner7e044912010-01-04 07:17:19 +0000200 // If all of the demanded bits are known zero on one side, return the
201 // other. These bits cannot contribute to the result of the 'or' in this
202 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000203 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000204 (DemandedMask & ~LHSKnownOne))
205 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000206 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000207 (DemandedMask & ~RHSKnownOne))
208 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000209
Chris Lattner7e044912010-01-04 07:17:19 +0000210 // If all of the potentially set bits on one side are known to be set on
211 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000212 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000213 (DemandedMask & (~RHSKnownZero)))
214 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000215 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000216 (DemandedMask & (~LHSKnownZero)))
217 return I->getOperand(1);
Shuxin Yang73285932012-12-04 22:15:32 +0000218 } else if (I->getOpcode() == Instruction::Xor) {
219 // We can simplify (X^Y) -> X or Y in the user's context if we know that
220 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000221
Jay Foada0653a32014-05-14 21:14:37 +0000222 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
223 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
Craig Topper4c947752012-12-22 18:09:02 +0000224
Shuxin Yang73285932012-12-04 22:15:32 +0000225 // If all of the demanded bits are known zero on one side, return the
Craig Topper4c947752012-12-22 18:09:02 +0000226 // other.
Shuxin Yang73285932012-12-04 22:15:32 +0000227 if ((DemandedMask & RHSKnownZero) == DemandedMask)
228 return I->getOperand(0);
229 if ((DemandedMask & LHSKnownZero) == DemandedMask)
230 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000231 }
Shuxin Yang73285932012-12-04 22:15:32 +0000232
Chris Lattner7e044912010-01-04 07:17:19 +0000233 // Compute the KnownZero/KnownOne bits to simplify things downstream.
Jay Foada0653a32014-05-14 21:14:37 +0000234 computeKnownBits(I, KnownZero, KnownOne, Depth);
Craig Topperf40110f2014-04-25 05:29:35 +0000235 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000236 }
Craig Topper4c947752012-12-22 18:09:02 +0000237
Chris Lattner7e044912010-01-04 07:17:19 +0000238 // If this is the root being simplified, allow it to have multiple uses,
239 // just set the DemandedMask to all bits so that we can try to simplify the
240 // operands. This allows visitTruncInst (for example) to simplify the
241 // operand of a trunc without duplicating all the logic below.
242 if (Depth == 0 && !V->hasOneUse())
243 DemandedMask = APInt::getAllOnesValue(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000244
Chris Lattner7e044912010-01-04 07:17:19 +0000245 switch (I->getOpcode()) {
246 default:
Jay Foada0653a32014-05-14 21:14:37 +0000247 computeKnownBits(I, KnownZero, KnownOne, Depth);
Chris Lattner7e044912010-01-04 07:17:19 +0000248 break;
249 case Instruction::And:
250 // If either the LHS or the RHS are Zero, the result is zero.
251 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
252 RHSKnownZero, RHSKnownOne, Depth+1) ||
253 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
254 LHSKnownZero, LHSKnownOne, Depth+1))
255 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000256 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
257 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000258
259 // If all of the demanded bits are known 1 on one side, return the other.
260 // These bits cannot contribute to the result of the 'and'.
Craig Topper4c947752012-12-22 18:09:02 +0000261 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000262 (DemandedMask & ~LHSKnownZero))
263 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000264 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000265 (DemandedMask & ~RHSKnownZero))
266 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000267
Chris Lattner7e044912010-01-04 07:17:19 +0000268 // If all of the demanded bits in the inputs are known zeros, return zero.
269 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
270 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000271
Chris Lattner7e044912010-01-04 07:17:19 +0000272 // If the RHS is a constant, see if we can simplify it.
273 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
274 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000275
Chris Lattner7e044912010-01-04 07:17:19 +0000276 // Output known-1 bits are only known if set in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000277 KnownOne = RHSKnownOne & LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000278 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000279 KnownZero = RHSKnownZero | LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000280 break;
281 case Instruction::Or:
282 // If either the LHS or the RHS are One, the result is One.
Craig Topper4c947752012-12-22 18:09:02 +0000283 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +0000284 RHSKnownZero, RHSKnownOne, Depth+1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000285 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne,
Chris Lattner7e044912010-01-04 07:17:19 +0000286 LHSKnownZero, LHSKnownOne, Depth+1))
287 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000288 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
289 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
290
Chris Lattner7e044912010-01-04 07:17:19 +0000291 // If all of the demanded bits are known zero on one side, return the other.
292 // These bits cannot contribute to the result of the 'or'.
Craig Topper4c947752012-12-22 18:09:02 +0000293 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000294 (DemandedMask & ~LHSKnownOne))
295 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000296 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000297 (DemandedMask & ~RHSKnownOne))
298 return I->getOperand(1);
299
300 // If all of the potentially set bits on one side are known to be set on
301 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000302 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000303 (DemandedMask & (~RHSKnownZero)))
304 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000305 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000306 (DemandedMask & (~LHSKnownZero)))
307 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000308
Chris Lattner7e044912010-01-04 07:17:19 +0000309 // If the RHS is a constant, see if we can simplify it.
310 if (ShrinkDemandedConstant(I, 1, DemandedMask))
311 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000312
Chris Lattner7e044912010-01-04 07:17:19 +0000313 // Output known-0 bits are only known if clear in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000314 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000315 // Output known-1 are known to be set if set in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000316 KnownOne = RHSKnownOne | LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000317 break;
318 case Instruction::Xor: {
319 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
320 RHSKnownZero, RHSKnownOne, Depth+1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000321 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +0000322 LHSKnownZero, LHSKnownOne, Depth+1))
323 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000324 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
325 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
326
Chris Lattner7e044912010-01-04 07:17:19 +0000327 // If all of the demanded bits are known zero on one side, return the other.
328 // These bits cannot contribute to the result of the 'xor'.
329 if ((DemandedMask & RHSKnownZero) == DemandedMask)
330 return I->getOperand(0);
331 if ((DemandedMask & LHSKnownZero) == DemandedMask)
332 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000333
Chris Lattner7e044912010-01-04 07:17:19 +0000334 // If all of the demanded bits are known to be zero on one side or the
335 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000336 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Chris Lattner7e044912010-01-04 07:17:19 +0000337 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
Craig Topper4c947752012-12-22 18:09:02 +0000338 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000339 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
340 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000341 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000342 }
Craig Topper4c947752012-12-22 18:09:02 +0000343
Chris Lattner7e044912010-01-04 07:17:19 +0000344 // If all of the demanded bits on one side are known, and all of the set
345 // bits on that side are also known to be set on the other side, turn this
346 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000347 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper4c947752012-12-22 18:09:02 +0000348 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
Chris Lattner7e044912010-01-04 07:17:19 +0000349 // all known
350 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
351 Constant *AndC = Constant::getIntegerValue(VTy,
352 ~RHSKnownOne & DemandedMask);
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000353 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000354 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000355 }
356 }
Craig Topper4c947752012-12-22 18:09:02 +0000357
Chris Lattner7e044912010-01-04 07:17:19 +0000358 // If the RHS is a constant, see if we can simplify it.
359 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
360 if (ShrinkDemandedConstant(I, 1, DemandedMask))
361 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000362
Chris Lattner7e044912010-01-04 07:17:19 +0000363 // If our LHS is an 'and' and if it has one use, and if any of the bits we
364 // are flipping are known to be set, then the xor is just resetting those
365 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
366 // simplifying both of them.
367 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
368 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
369 isa<ConstantInt>(I->getOperand(1)) &&
370 isa<ConstantInt>(LHSInst->getOperand(1)) &&
371 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
372 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
373 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
374 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000375
Chris Lattner7e044912010-01-04 07:17:19 +0000376 Constant *AndC =
377 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000378 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000379 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000380
Chris Lattner7e044912010-01-04 07:17:19 +0000381 Constant *XorC =
382 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000383 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000384 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000385 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000386
387 // Output known-0 bits are known if clear or set in both the LHS & RHS.
388 KnownZero= (RHSKnownZero & LHSKnownZero) | (RHSKnownOne & LHSKnownOne);
389 // Output known-1 are known to be set if set in only one of the LHS, RHS.
390 KnownOne = (RHSKnownZero & LHSKnownOne) | (RHSKnownOne & LHSKnownZero);
Chris Lattner7e044912010-01-04 07:17:19 +0000391 break;
392 }
393 case Instruction::Select:
394 if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask,
395 RHSKnownZero, RHSKnownOne, Depth+1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000396 SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +0000397 LHSKnownZero, LHSKnownOne, Depth+1))
398 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000399 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
400 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
401
Chris Lattner7e044912010-01-04 07:17:19 +0000402 // If the operands are constants, see if we can simplify them.
403 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
404 ShrinkDemandedConstant(I, 2, DemandedMask))
405 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000406
Chris Lattner7e044912010-01-04 07:17:19 +0000407 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000408 KnownOne = RHSKnownOne & LHSKnownOne;
409 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000410 break;
411 case Instruction::Trunc: {
412 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000413 DemandedMask = DemandedMask.zext(truncBf);
414 KnownZero = KnownZero.zext(truncBf);
415 KnownOne = KnownOne.zext(truncBf);
Craig Topper4c947752012-12-22 18:09:02 +0000416 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000417 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000418 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000419 DemandedMask = DemandedMask.trunc(BitWidth);
420 KnownZero = KnownZero.trunc(BitWidth);
421 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000422 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000423 break;
424 }
425 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000426 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000427 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000428
Chris Lattner229907c2011-07-18 04:54:35 +0000429 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
430 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000431 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
432 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
433 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000434 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000435 } else
436 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000437 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000438 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000439 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000440 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000441
442 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000443 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000444 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000445 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000446 break;
447 case Instruction::ZExt: {
448 // Compute the bits in the result that are not present in the input.
449 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000450
Jay Foad583abbc2010-12-07 08:25:19 +0000451 DemandedMask = DemandedMask.trunc(SrcBitWidth);
452 KnownZero = KnownZero.trunc(SrcBitWidth);
453 KnownOne = KnownOne.trunc(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000454 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000455 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000456 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000457 DemandedMask = DemandedMask.zext(BitWidth);
458 KnownZero = KnownZero.zext(BitWidth);
459 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000460 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000461 // The top bits are known to be zero.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000462 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000463 break;
464 }
465 case Instruction::SExt: {
466 // Compute the bits in the result that are not present in the input.
467 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000468
469 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000470 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
471
472 APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
473 // If any of the sign extended bits are demanded, we know that the sign
474 // bit is demanded.
475 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000476 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000477
Jay Foad583abbc2010-12-07 08:25:19 +0000478 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
479 KnownZero = KnownZero.trunc(SrcBitWidth);
480 KnownOne = KnownOne.trunc(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000481 if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000482 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000483 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000484 InputDemandedBits = InputDemandedBits.zext(BitWidth);
485 KnownZero = KnownZero.zext(BitWidth);
486 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000487 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
488
Chris Lattner7e044912010-01-04 07:17:19 +0000489 // If the sign bit of the input is known set or clear, then we know the
490 // top bits of the result.
491
492 // If the input sign bit is known zero, or if the NewBits are not demanded
493 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000494 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000495 // Convert to ZExt cast
496 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000497 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000498 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
499 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000500 }
501 break;
502 }
503 case Instruction::Add: {
504 // Figure out what the input bits are. If the top bits of the and result
505 // are not demanded, then the add doesn't demand them from its input
506 // either.
507 unsigned NLZ = DemandedMask.countLeadingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000508
Chris Lattner7e044912010-01-04 07:17:19 +0000509 // If there is a constant on the RHS, there are a variety of xformations
510 // we can do.
511 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
512 // If null, this should be simplified elsewhere. Some of the xforms here
513 // won't work if the RHS is zero.
514 if (RHS->isZero())
515 break;
Craig Topper4c947752012-12-22 18:09:02 +0000516
Chris Lattner7e044912010-01-04 07:17:19 +0000517 // If the top bit of the output is demanded, demand everything from the
518 // input. Otherwise, we demand all the input bits except NLZ top bits.
519 APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
520
521 // Find information about known zero/one bits in the input.
Craig Topper4c947752012-12-22 18:09:02 +0000522 if (SimplifyDemandedBits(I->getOperandUse(0), InDemandedBits,
Chris Lattner7e044912010-01-04 07:17:19 +0000523 LHSKnownZero, LHSKnownOne, Depth+1))
524 return I;
525
526 // If the RHS of the add has bits set that can't affect the input, reduce
527 // the constant.
528 if (ShrinkDemandedConstant(I, 1, InDemandedBits))
529 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000530
Chris Lattner7e044912010-01-04 07:17:19 +0000531 // Avoid excess work.
532 if (LHSKnownZero == 0 && LHSKnownOne == 0)
533 break;
Craig Topper4c947752012-12-22 18:09:02 +0000534
Chris Lattner7e044912010-01-04 07:17:19 +0000535 // Turn it into OR if input bits are zero.
536 if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
537 Instruction *Or =
538 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
539 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000540 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000541 }
Craig Topper4c947752012-12-22 18:09:02 +0000542
Chris Lattner7e044912010-01-04 07:17:19 +0000543 // We can say something about the output known-zero and known-one bits,
544 // depending on potential carries from the input constant and the
545 // unknowns. For example if the LHS is known to have at most the 0x0F0F0
546 // bits set and the RHS constant is 0x01001, then we know we have a known
547 // one mask of 0x00001 and a known zero mask of 0xE0F0E.
Craig Topper4c947752012-12-22 18:09:02 +0000548
Chris Lattner7e044912010-01-04 07:17:19 +0000549 // To compute this, we first compute the potential carry bits. These are
550 // the bits which may be modified. I'm not aware of a better way to do
551 // this scan.
552 const APInt &RHSVal = RHS->getValue();
553 APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
Craig Topper4c947752012-12-22 18:09:02 +0000554
Chris Lattner7e044912010-01-04 07:17:19 +0000555 // Now that we know which bits have carries, compute the known-1/0 sets.
Craig Topper4c947752012-12-22 18:09:02 +0000556
Chris Lattner7e044912010-01-04 07:17:19 +0000557 // Bits are known one if they are known zero in one operand and one in the
558 // other, and there is no input carry.
Craig Topper4c947752012-12-22 18:09:02 +0000559 KnownOne = ((LHSKnownZero & RHSVal) |
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000560 (LHSKnownOne & ~RHSVal)) & ~CarryBits;
Craig Topper4c947752012-12-22 18:09:02 +0000561
Chris Lattner7e044912010-01-04 07:17:19 +0000562 // Bits are known zero if they are known zero in both operands and there
563 // is no input carry.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000564 KnownZero = LHSKnownZero & ~RHSVal & ~CarryBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000565 } else {
566 // If the high-bits of this ADD are not demanded, then it does not demand
567 // the high bits of its LHS or RHS.
568 if (DemandedMask[BitWidth-1] == 0) {
569 // Right fill the mask of bits for this ADD to demand the most
570 // significant bit and all those below it.
571 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
572 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
573 LHSKnownZero, LHSKnownOne, Depth+1) ||
574 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
575 LHSKnownZero, LHSKnownOne, Depth+1))
576 return I;
577 }
578 }
579 break;
580 }
581 case Instruction::Sub:
582 // If the high-bits of this SUB are not demanded, then it does not demand
583 // the high bits of its LHS or RHS.
584 if (DemandedMask[BitWidth-1] == 0) {
585 // Right fill the mask of bits for this SUB to demand the most
586 // significant bit and all those below it.
587 uint32_t NLZ = DemandedMask.countLeadingZeros();
588 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
589 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
590 LHSKnownZero, LHSKnownOne, Depth+1) ||
591 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
592 LHSKnownZero, LHSKnownOne, Depth+1))
593 return I;
594 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000595
Jay Foada0653a32014-05-14 21:14:37 +0000596 // Otherwise just hand the sub off to computeKnownBits to fill in
Chris Lattner7e044912010-01-04 07:17:19 +0000597 // the known zeros and ones.
Jay Foada0653a32014-05-14 21:14:37 +0000598 computeKnownBits(V, KnownZero, KnownOne, Depth);
Benjamin Kramer010337c2011-12-24 17:31:38 +0000599
600 // Turn this into a xor if LHS is 2^n-1 and the remaining bits are known
601 // zero.
602 if (ConstantInt *C0 = dyn_cast<ConstantInt>(I->getOperand(0))) {
603 APInt I0 = C0->getValue();
604 if ((I0 + 1).isPowerOf2() && (I0 | KnownZero).isAllOnesValue()) {
605 Instruction *Xor = BinaryOperator::CreateXor(I->getOperand(1), C0);
606 return InsertNewInstWith(Xor, *I);
607 }
608 }
Chris Lattner7e044912010-01-04 07:17:19 +0000609 break;
610 case Instruction::Shl:
611 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000612 {
613 Value *VarX; ConstantInt *C1;
614 if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
615 Instruction *Shr = cast<Instruction>(I->getOperand(0));
616 Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
617 KnownZero, KnownOne);
618 if (R)
619 return R;
620 }
621 }
622
Chris Lattner768003c2011-02-10 05:09:34 +0000623 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000624 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000625
Chris Lattner768003c2011-02-10 05:09:34 +0000626 // If the shift is NUW/NSW, then it does demand the high bits.
627 ShlOperator *IOp = cast<ShlOperator>(I);
628 if (IOp->hasNoSignedWrap())
629 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
630 else if (IOp->hasNoUnsignedWrap())
631 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000632
633 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000634 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000635 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000636 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
637 KnownZero <<= ShiftAmt;
638 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000639 // low bits known zero.
640 if (ShiftAmt)
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000641 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000642 }
643 break;
644 case Instruction::LShr:
645 // For a logical shift right
646 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000647 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000648
Chris Lattner7e044912010-01-04 07:17:19 +0000649 // Unsigned shift right.
650 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000651
Chris Lattner768003c2011-02-10 05:09:34 +0000652 // If the shift is exact, then it does demand the low bits (and knows that
653 // they are zero).
654 if (cast<LShrOperator>(I)->isExact())
655 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000656
Chris Lattner7e044912010-01-04 07:17:19 +0000657 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000658 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000659 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000660 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
661 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
662 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000663 if (ShiftAmt) {
664 // Compute the new bits that are at the top now.
665 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000666 KnownZero |= HighBits; // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000667 }
668 }
669 break;
670 case Instruction::AShr:
671 // If this is an arithmetic shift right and only the low-bit is set, we can
672 // always convert this into a logical shr, even if the shift amount is
673 // variable. The low bit of the shift cannot be an input sign bit unless
674 // the shift amount is >= the size of the datatype, which is undefined.
675 if (DemandedMask == 1) {
676 // Perform the logical shift right.
677 Instruction *NewVal = BinaryOperator::CreateLShr(
678 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000679 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000680 }
Chris Lattner7e044912010-01-04 07:17:19 +0000681
682 // If the sign bit is the only bit demanded by this ashr, then there is no
683 // need to do it, the shift doesn't change the high bit.
684 if (DemandedMask.isSignBit())
685 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000686
Chris Lattner7e044912010-01-04 07:17:19 +0000687 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000688 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000689
Chris Lattner7e044912010-01-04 07:17:19 +0000690 // Signed shift right.
691 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
692 // If any of the "high bits" are demanded, we should set the sign bit as
693 // demanded.
694 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000695 DemandedMaskIn.setBit(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000696
Chris Lattner768003c2011-02-10 05:09:34 +0000697 // If the shift is exact, then it does demand the low bits (and knows that
698 // they are zero).
699 if (cast<AShrOperator>(I)->isExact())
700 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000701
Chris Lattner7e044912010-01-04 07:17:19 +0000702 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000703 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000704 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000705 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000706 // Compute the new bits that are at the top now.
707 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000708 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
709 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000710
Chris Lattner7e044912010-01-04 07:17:19 +0000711 // Handle the sign bits.
712 APInt SignBit(APInt::getSignBit(BitWidth));
713 // Adjust to where it is now in the mask.
Craig Topper4c947752012-12-22 18:09:02 +0000714 SignBit = APIntOps::lshr(SignBit, ShiftAmt);
715
Chris Lattner7e044912010-01-04 07:17:19 +0000716 // If the input sign bit is known to be zero, or if none of the top bits
717 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000718 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Chris Lattner7e044912010-01-04 07:17:19 +0000719 (HighBits & ~DemandedMask) == HighBits) {
720 // Perform the logical shift right.
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000721 BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
722 SA, I->getName());
723 NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000724 return InsertNewInstWith(NewVal, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000725 } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
726 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000727 }
728 }
729 break;
730 case Instruction::SRem:
731 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000732 // X % -1 demands all the bits because we don't want to introduce
733 // INT_MIN % -1 (== undef) by accident.
734 if (Rem->isAllOnesValue())
735 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000736 APInt RA = Rem->getValue().abs();
737 if (RA.isPowerOf2()) {
738 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
739 return I->getOperand(0);
740
741 APInt LowBits = RA - 1;
742 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
743 if (SimplifyDemandedBits(I->getOperandUse(0), Mask2,
744 LHSKnownZero, LHSKnownOne, Depth+1))
745 return I;
746
Duncan Sands3a48b872010-01-28 17:22:42 +0000747 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000748 KnownZero = LHSKnownZero & LowBits;
749 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000750
Duncan Sands3a48b872010-01-28 17:22:42 +0000751 // If LHS is non-negative or has all low bits zero, then the upper bits
752 // are all zero.
753 if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
754 KnownZero |= ~LowBits;
755
756 // If LHS is negative and not all low bits are zero, then the upper bits
757 // are all one.
758 if (LHSKnownOne[BitWidth-1] && ((LHSKnownOne & LowBits) != 0))
759 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000760
Craig Topper4c947752012-12-22 18:09:02 +0000761 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000762 }
763 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000764
765 // The sign bit is the LHS's sign bit, except when the result of the
766 // remainder is zero.
767 if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
Nick Lewyckye4679792011-03-07 01:50:10 +0000768 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Jay Foada0653a32014-05-14 21:14:37 +0000769 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
Nick Lewyckye4679792011-03-07 01:50:10 +0000770 // If it's known zero, our sign bit is also zero.
771 if (LHSKnownZero.isNegative())
Benjamin Kramer21b972a2013-05-09 16:32:32 +0000772 KnownZero.setBit(KnownZero.getBitWidth() - 1);
Nick Lewyckye4679792011-03-07 01:50:10 +0000773 }
Chris Lattner7e044912010-01-04 07:17:19 +0000774 break;
775 case Instruction::URem: {
776 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
777 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
778 if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes,
779 KnownZero2, KnownOne2, Depth+1) ||
780 SimplifyDemandedBits(I->getOperandUse(1), AllOnes,
781 KnownZero2, KnownOne2, Depth+1))
782 return I;
783
784 unsigned Leaders = KnownZero2.countLeadingOnes();
785 Leaders = std::max(Leaders,
786 KnownZero2.countLeadingOnes());
787 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
788 break;
789 }
790 case Instruction::Call:
791 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
792 switch (II->getIntrinsicID()) {
793 default: break;
794 case Intrinsic::bswap: {
795 // If the only bits demanded come from one byte of the bswap result,
796 // just shift the input byte into position to eliminate the bswap.
797 unsigned NLZ = DemandedMask.countLeadingZeros();
798 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000799
Chris Lattner7e044912010-01-04 07:17:19 +0000800 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
801 // we need all the bits down to bit 8. Likewise, round NLZ. If we
802 // have 14 leading zeros, round to 8.
803 NLZ &= ~7;
804 NTZ &= ~7;
805 // If we need exactly one byte, we can do this transformation.
806 if (BitWidth-NLZ-NTZ == 8) {
807 unsigned ResultBit = NTZ;
808 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000809
Chris Lattner7e044912010-01-04 07:17:19 +0000810 // Replace this with either a left or right shift to get the byte into
811 // the right place.
812 Instruction *NewVal;
813 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000814 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000815 ConstantInt::get(I->getType(), InputBit-ResultBit));
816 else
Gabor Greif79430172010-06-24 12:35:13 +0000817 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000818 ConstantInt::get(I->getType(), ResultBit-InputBit));
819 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000820 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000821 }
Craig Topper4c947752012-12-22 18:09:02 +0000822
Chris Lattner7e044912010-01-04 07:17:19 +0000823 // TODO: Could compute known zero/one bits based on the input.
824 break;
825 }
Chad Rosierb3628842011-05-26 23:13:19 +0000826 case Intrinsic::x86_sse42_crc32_64_64:
Evan Chenge8d2e9e2011-05-20 00:54:37 +0000827 KnownZero = APInt::getHighBitsSet(64, 32);
Craig Topperf40110f2014-04-25 05:29:35 +0000828 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000829 }
830 }
Jay Foada0653a32014-05-14 21:14:37 +0000831 computeKnownBits(V, KnownZero, KnownOne, Depth);
Chris Lattner7e044912010-01-04 07:17:19 +0000832 break;
833 }
Craig Topper4c947752012-12-22 18:09:02 +0000834
Chris Lattner7e044912010-01-04 07:17:19 +0000835 // If the client is only demanding bits that we know, return the known
836 // constant.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000837 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
838 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000839 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000840}
841
Shuxin Yang63e999e2012-12-04 00:04:54 +0000842/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
843/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
844/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
845/// of "C2-C1".
846///
847/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
848/// ..., bn}, without considering the specific value X is holding.
849/// This transformation is legal iff one of following conditions is hold:
850/// 1) All the bit in S are 0, in this case E1 == E2.
851/// 2) We don't care those bits in S, per the input DemandedMask.
852/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
853/// rest bits.
854///
855/// Currently we only test condition 2).
856///
857/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
858/// not successful.
859Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
860 Instruction *Shl, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne) {
861
Benjamin Kramer010f1082013-08-30 14:35:35 +0000862 const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
863 const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
864 if (!ShlOp1 || !ShrOp1)
Craig Topperf40110f2014-04-25 05:29:35 +0000865 return nullptr; // Noop.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000866
867 Value *VarX = Shr->getOperand(0);
868 Type *Ty = VarX->getType();
869 unsigned BitWidth = Ty->getIntegerBitWidth();
870 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000871 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000872
873 unsigned ShlAmt = ShlOp1.getZExtValue();
874 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000875
876 KnownOne.clearAllBits();
877 KnownZero = APInt::getBitsSet(KnownZero.getBitWidth(), 0, ShlAmt-1);
878 KnownZero &= DemandedMask;
879
Benjamin Kramer010f1082013-08-30 14:35:35 +0000880 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
881 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000882
883 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
884 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
885 (BitMask1.ashr(ShrAmt) << ShlAmt);
886
887 if (ShrAmt <= ShlAmt) {
888 BitMask2 <<= (ShlAmt - ShrAmt);
889 } else {
890 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
891 BitMask2.ashr(ShrAmt - ShlAmt);
892 }
893
894 // Check if condition-2 (see the comment to this function) is satified.
895 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
896 if (ShrAmt == ShlAmt)
897 return VarX;
898
899 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000900 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000901
902 BinaryOperator *New;
903 if (ShrAmt < ShlAmt) {
904 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
905 New = BinaryOperator::CreateShl(VarX, Amt);
906 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
907 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
908 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
909 } else {
910 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000911 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
912 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000913 if (cast<BinaryOperator>(Shr)->isExact())
914 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000915 }
916
917 return InsertNewInstWith(New, *Shl);
918 }
919
Craig Topperf40110f2014-04-25 05:29:35 +0000920 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000921}
Chris Lattner7e044912010-01-04 07:17:19 +0000922
923/// SimplifyDemandedVectorElts - The specified value produces a vector with
924/// any number of elements. DemandedElts contains the set of elements that are
925/// actually used by the caller. This method analyzes which elements of the
926/// operand are undef and returns that information in UndefElts.
927///
928/// If the information about demanded elements can be used to simplify the
929/// operation, the operation is simplified, then the resultant value is
930/// returned. This returns null if no change was made.
931Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000932 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000933 unsigned Depth) {
934 unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
935 APInt EltMask(APInt::getAllOnesValue(VWidth));
936 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
937
938 if (isa<UndefValue>(V)) {
939 // If the entire vector is undefined, just return this info.
940 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000941 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000942 }
Craig Topper4c947752012-12-22 18:09:02 +0000943
Chris Lattnerb22423c2010-02-08 23:56:03 +0000944 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000945 UndefElts = EltMask;
946 return UndefValue::get(V->getType());
947 }
948
949 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000950
Chris Lattner67058832012-01-25 06:48:06 +0000951 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
952 if (Constant *C = dyn_cast<Constant>(V)) {
953 // Check if this is identity. If so, return 0 since we are not simplifying
954 // anything.
955 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000956 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000957
Chris Lattner229907c2011-07-18 04:54:35 +0000958 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000959 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000960
Chris Lattner67058832012-01-25 06:48:06 +0000961 SmallVector<Constant*, 16> Elts;
962 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000963 if (!DemandedElts[i]) { // If not demanded, set to undef.
964 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000965 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +0000966 continue;
967 }
Craig Topper4c947752012-12-22 18:09:02 +0000968
Chris Lattner67058832012-01-25 06:48:06 +0000969 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000970 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000971
Chris Lattner67058832012-01-25 06:48:06 +0000972 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000973 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000974 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +0000975 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +0000976 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +0000977 }
Chris Lattner67058832012-01-25 06:48:06 +0000978 }
Craig Topper4c947752012-12-22 18:09:02 +0000979
Chris Lattner7e044912010-01-04 07:17:19 +0000980 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +0000981 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +0000982 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000983 }
Craig Topper4c947752012-12-22 18:09:02 +0000984
Chris Lattner7e044912010-01-04 07:17:19 +0000985 // Limit search depth.
986 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +0000987 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000988
Stuart Hastings5bd18b62011-05-17 22:13:31 +0000989 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +0000990 // simplification conservatively assuming that all elements
991 // are needed.
992 if (!V->hasOneUse()) {
993 // Quit if we find multiple users of a non-root value though.
994 // They'll be handled when it's their turn to be visited by
995 // the main instcombine process.
996 if (Depth != 0)
997 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +0000998 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000999
1000 // Conservatively assume that all elements are needed.
1001 DemandedElts = EltMask;
1002 }
Craig Topper4c947752012-12-22 18:09:02 +00001003
Chris Lattner7e044912010-01-04 07:17:19 +00001004 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +00001005 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +00001006
Chris Lattner7e044912010-01-04 07:17:19 +00001007 bool MadeChange = false;
1008 APInt UndefElts2(VWidth, 0);
1009 Value *TmpV;
1010 switch (I->getOpcode()) {
1011 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001012
Chris Lattner7e044912010-01-04 07:17:19 +00001013 case Instruction::InsertElement: {
1014 // If this is a variable index, we don't know which element it overwrites.
1015 // demand exactly the same input as we produce.
1016 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +00001017 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +00001018 // Note that we can't propagate undef elt info, because we don't know
1019 // which elt is getting updated.
1020 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1021 UndefElts2, Depth+1);
1022 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1023 break;
1024 }
Craig Topper4c947752012-12-22 18:09:02 +00001025
Chris Lattner7e044912010-01-04 07:17:19 +00001026 // If this is inserting an element that isn't demanded, remove this
1027 // insertelement.
1028 unsigned IdxNo = Idx->getZExtValue();
1029 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1030 Worklist.Add(I);
1031 return I->getOperand(0);
1032 }
Craig Topper4c947752012-12-22 18:09:02 +00001033
Chris Lattner7e044912010-01-04 07:17:19 +00001034 // Otherwise, the element inserted overwrites whatever was there, so the
1035 // input demanded set is simpler than the output set.
1036 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001037 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001038 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
1039 UndefElts, Depth+1);
1040 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1041
1042 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001043 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001044 break;
1045 }
1046 case Instruction::ShuffleVector: {
1047 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
1048 uint64_t LHSVWidth =
1049 cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements();
1050 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1051 for (unsigned i = 0; i < VWidth; i++) {
1052 if (DemandedElts[i]) {
1053 unsigned MaskVal = Shuffle->getMaskValue(i);
1054 if (MaskVal != -1u) {
1055 assert(MaskVal < LHSVWidth * 2 &&
1056 "shufflevector mask index out of range!");
1057 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001058 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001059 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001060 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001061 }
1062 }
1063 }
1064
1065 APInt UndefElts4(LHSVWidth, 0);
1066 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
1067 UndefElts4, Depth+1);
1068 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1069
1070 APInt UndefElts3(LHSVWidth, 0);
1071 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
1072 UndefElts3, Depth+1);
1073 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1074
1075 bool NewUndefElts = false;
1076 for (unsigned i = 0; i < VWidth; i++) {
1077 unsigned MaskVal = Shuffle->getMaskValue(i);
1078 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001079 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001080 } else if (!DemandedElts[i]) {
1081 NewUndefElts = true;
1082 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001083 } else if (MaskVal < LHSVWidth) {
1084 if (UndefElts4[MaskVal]) {
1085 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001086 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001087 }
1088 } else {
1089 if (UndefElts3[MaskVal - LHSVWidth]) {
1090 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001091 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001092 }
1093 }
1094 }
1095
1096 if (NewUndefElts) {
1097 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001098 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001099 for (unsigned i = 0; i < VWidth; ++i) {
1100 if (UndefElts[i])
1101 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1102 else
1103 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1104 Shuffle->getMaskValue(i)));
1105 }
1106 I->setOperand(2, ConstantVector::get(Elts));
1107 MadeChange = true;
1108 }
1109 break;
1110 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001111 case Instruction::Select: {
1112 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1113 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1114 for (unsigned i = 0; i < VWidth; i++) {
1115 if (CV->getAggregateElement(i)->isNullValue())
1116 LeftDemanded.clearBit(i);
1117 else
1118 RightDemanded.clearBit(i);
1119 }
1120 }
1121
1122 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded,
1123 UndefElts, Depth+1);
1124 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1125
1126 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
1127 UndefElts2, Depth+1);
1128 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001129
Pete Cooperabc13af2012-07-26 23:10:24 +00001130 // Output elements are undefined if both are undefined.
1131 UndefElts &= UndefElts2;
1132 break;
1133 }
Chris Lattner7e044912010-01-04 07:17:19 +00001134 case Instruction::BitCast: {
1135 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001136 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001137 if (!VTy) break;
1138 unsigned InVWidth = VTy->getNumElements();
1139 APInt InputDemandedElts(InVWidth, 0);
1140 unsigned Ratio;
1141
1142 if (VWidth == InVWidth) {
1143 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1144 // elements as are demanded of us.
1145 Ratio = 1;
1146 InputDemandedElts = DemandedElts;
1147 } else if (VWidth > InVWidth) {
1148 // Untested so far.
1149 break;
Craig Topper4c947752012-12-22 18:09:02 +00001150
Chris Lattner7e044912010-01-04 07:17:19 +00001151 // If there are more elements in the result than there are in the source,
1152 // then an input element is live if any of the corresponding output
1153 // elements are live.
1154 Ratio = VWidth/InVWidth;
1155 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1156 if (DemandedElts[OutIdx])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001157 InputDemandedElts.setBit(OutIdx/Ratio);
Chris Lattner7e044912010-01-04 07:17:19 +00001158 }
1159 } else {
1160 // Untested so far.
1161 break;
Craig Topper4c947752012-12-22 18:09:02 +00001162
Chris Lattner7e044912010-01-04 07:17:19 +00001163 // If there are more elements in the source than there are in the result,
1164 // then an input element is live if the corresponding output element is
1165 // live.
1166 Ratio = InVWidth/VWidth;
1167 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1168 if (DemandedElts[InIdx/Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001169 InputDemandedElts.setBit(InIdx);
Chris Lattner7e044912010-01-04 07:17:19 +00001170 }
Craig Topper4c947752012-12-22 18:09:02 +00001171
Chris Lattner7e044912010-01-04 07:17:19 +00001172 // div/rem demand all inputs, because they don't want divide by zero.
1173 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
1174 UndefElts2, Depth+1);
1175 if (TmpV) {
1176 I->setOperand(0, TmpV);
1177 MadeChange = true;
1178 }
Craig Topper4c947752012-12-22 18:09:02 +00001179
Chris Lattner7e044912010-01-04 07:17:19 +00001180 UndefElts = UndefElts2;
1181 if (VWidth > InVWidth) {
1182 llvm_unreachable("Unimp");
1183 // If there are more elements in the result than there are in the source,
1184 // then an output element is undef if the corresponding input element is
1185 // undef.
1186 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1187 if (UndefElts2[OutIdx/Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001188 UndefElts.setBit(OutIdx);
Chris Lattner7e044912010-01-04 07:17:19 +00001189 } else if (VWidth < InVWidth) {
1190 llvm_unreachable("Unimp");
1191 // If there are more elements in the source than there are in the result,
1192 // then a result element is undef if all of the corresponding input
1193 // elements are undef.
1194 UndefElts = ~0ULL >> (64-VWidth); // Start out all undef.
1195 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1196 if (!UndefElts2[InIdx]) // Not undef?
Jay Foad25a5e4c2010-12-01 08:53:58 +00001197 UndefElts.clearBit(InIdx/Ratio); // Clear undef bit.
Chris Lattner7e044912010-01-04 07:17:19 +00001198 }
1199 break;
1200 }
1201 case Instruction::And:
1202 case Instruction::Or:
1203 case Instruction::Xor:
1204 case Instruction::Add:
1205 case Instruction::Sub:
1206 case Instruction::Mul:
1207 // div/rem demand all inputs, because they don't want divide by zero.
1208 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1209 UndefElts, Depth+1);
1210 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1211 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
1212 UndefElts2, Depth+1);
1213 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001214
Chris Lattner7e044912010-01-04 07:17:19 +00001215 // Output elements are undefined if both are undefined. Consider things
1216 // like undef&0. The result is known zero, not undef.
1217 UndefElts &= UndefElts2;
1218 break;
Pete Coopere807e452012-07-26 22:37:04 +00001219 case Instruction::FPTrunc:
1220 case Instruction::FPExt:
1221 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1222 UndefElts, Depth+1);
1223 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1224 break;
Craig Topper4c947752012-12-22 18:09:02 +00001225
Chris Lattner7e044912010-01-04 07:17:19 +00001226 case Instruction::Call: {
1227 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1228 if (!II) break;
1229 switch (II->getIntrinsicID()) {
1230 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001231
Chris Lattner7e044912010-01-04 07:17:19 +00001232 // Binary vector operations that work column-wise. A dest element is a
1233 // function of the corresponding input elements from the two inputs.
1234 case Intrinsic::x86_sse_sub_ss:
1235 case Intrinsic::x86_sse_mul_ss:
1236 case Intrinsic::x86_sse_min_ss:
1237 case Intrinsic::x86_sse_max_ss:
1238 case Intrinsic::x86_sse2_sub_sd:
1239 case Intrinsic::x86_sse2_mul_sd:
1240 case Intrinsic::x86_sse2_min_sd:
1241 case Intrinsic::x86_sse2_max_sd:
Gabor Greife23efee2010-06-28 16:45:00 +00001242 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
Eric Christopher7258dcd2010-04-16 23:37:20 +00001243 UndefElts, Depth+1);
Gabor Greife23efee2010-06-28 16:45:00 +00001244 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1245 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Eric Christopher7258dcd2010-04-16 23:37:20 +00001246 UndefElts2, Depth+1);
Gabor Greife23efee2010-06-28 16:45:00 +00001247 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001248
1249 // If only the low elt is demanded and this is a scalarizable intrinsic,
1250 // scalarize it now.
1251 if (DemandedElts == 1) {
1252 switch (II->getIntrinsicID()) {
1253 default: break;
1254 case Intrinsic::x86_sse_sub_ss:
1255 case Intrinsic::x86_sse_mul_ss:
1256 case Intrinsic::x86_sse2_sub_sd:
1257 case Intrinsic::x86_sse2_mul_sd:
1258 // TODO: Lower MIN/MAX/ABS/etc
Gabor Greif79430172010-06-24 12:35:13 +00001259 Value *LHS = II->getArgOperand(0);
1260 Value *RHS = II->getArgOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +00001261 // Extract the element as scalars.
Craig Topper4c947752012-12-22 18:09:02 +00001262 LHS = InsertNewInstWith(ExtractElementInst::Create(LHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001263 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Eli Friedman6efb64e2011-05-19 01:20:42 +00001264 RHS = InsertNewInstWith(ExtractElementInst::Create(RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001265 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Craig Topper4c947752012-12-22 18:09:02 +00001266
Chris Lattner7e044912010-01-04 07:17:19 +00001267 switch (II->getIntrinsicID()) {
1268 default: llvm_unreachable("Case stmts out of sync!");
1269 case Intrinsic::x86_sse_sub_ss:
1270 case Intrinsic::x86_sse2_sub_sd:
Eli Friedman6efb64e2011-05-19 01:20:42 +00001271 TmpV = InsertNewInstWith(BinaryOperator::CreateFSub(LHS, RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001272 II->getName()), *II);
1273 break;
1274 case Intrinsic::x86_sse_mul_ss:
1275 case Intrinsic::x86_sse2_mul_sd:
Eli Friedman6efb64e2011-05-19 01:20:42 +00001276 TmpV = InsertNewInstWith(BinaryOperator::CreateFMul(LHS, RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001277 II->getName()), *II);
1278 break;
1279 }
Craig Topper4c947752012-12-22 18:09:02 +00001280
Chris Lattner7e044912010-01-04 07:17:19 +00001281 Instruction *New =
1282 InsertElementInst::Create(
1283 UndefValue::get(II->getType()), TmpV,
1284 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U, false),
1285 II->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +00001286 InsertNewInstWith(New, *II);
Chris Lattner7e044912010-01-04 07:17:19 +00001287 return New;
Craig Topper4c947752012-12-22 18:09:02 +00001288 }
Chris Lattner7e044912010-01-04 07:17:19 +00001289 }
Craig Topper4c947752012-12-22 18:09:02 +00001290
Chris Lattner7e044912010-01-04 07:17:19 +00001291 // Output elements are undefined if both are undefined. Consider things
1292 // like undef&0. The result is known zero, not undef.
1293 UndefElts &= UndefElts2;
1294 break;
1295 }
1296 break;
1297 }
1298 }
Craig Topperf40110f2014-04-25 05:29:35 +00001299 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001300}