blob: 1b42d3d504a3d57dbceb54729c0c59b9e6eac159 [file] [log] [blame]
Chris Lattnere0b4b722010-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 Lattnere0b4b722010-01-04 07:17:19 +000015#include "InstCombine.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/DataLayout.h"
17#include "llvm/IR/IntrinsicInst.h"
Stephen Hines36b56882014-04-23 16:57:46 -070018#include "llvm/IR/PatternMatch.h"
Chris Lattnere0b4b722010-01-04 07:17:19 +000019
20using namespace llvm;
Shuxin Yangc8119762012-12-04 00:04:54 +000021using namespace llvm::PatternMatch;
Chris Lattnere0b4b722010-01-04 07:17:19 +000022
Stephen Hinesdce4a402014-05-29 02:49:00 -070023#define DEBUG_TYPE "instcombine"
24
Craig Topper8a8413d2012-12-22 18:09:02 +000025/// ShrinkDemandedConstant - Check to see if the specified operand of the
Chris Lattnere0b4b722010-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 Topper8a8413d2012-12-22 18:09:02 +000029static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
Chris Lattnere0b4b722010-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 Foad40f8f622010-12-07 08:25:19 +000039 Demanded = Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
Chris Lattnere0b4b722010-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));
46 return true;
47}
48
49
50
51/// SimplifyDemandedInstructionBits - Inst is an integer instruction that
52/// SimplifyDemandedBits knows about. See if the instruction has any
53/// properties that allow us to simplify its operands.
54bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
55 unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
56 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
57 APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
Craig Topper8a8413d2012-12-22 18:09:02 +000058
59 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask,
Chris Lattnere0b4b722010-01-04 07:17:19 +000060 KnownZero, KnownOne, 0);
Stephen Hinesdce4a402014-05-29 02:49:00 -070061 if (!V) return false;
Chris Lattnere0b4b722010-01-04 07:17:19 +000062 if (V == &Inst) return true;
63 ReplaceInstUsesWith(Inst, V);
64 return true;
65}
66
67/// SimplifyDemandedBits - This form of SimplifyDemandedBits simplifies the
68/// specified instruction operand if possible, updating it in place. It returns
69/// true if it made any change and false otherwise.
Craig Topper8a8413d2012-12-22 18:09:02 +000070bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask,
Chris Lattnere0b4b722010-01-04 07:17:19 +000071 APInt &KnownZero, APInt &KnownOne,
72 unsigned Depth) {
73 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask,
74 KnownZero, KnownOne, Depth);
Stephen Hinesdce4a402014-05-29 02:49:00 -070075 if (!NewVal) return false;
Chris Lattnere0b4b722010-01-04 07:17:19 +000076 U = NewVal;
77 return true;
78}
79
80
81/// SimplifyDemandedUseBits - This function attempts to replace V with a simpler
82/// value based on the demanded bits. When this function is called, it is known
83/// that only the bits set in DemandedMask of the result of V are ever used
84/// downstream. Consequently, depending on the mask and V, it may be possible
85/// to replace V with a constant or one of its operands. In such cases, this
86/// function does the replacement and returns true. In all other cases, it
87/// returns false after analyzing the expression and setting KnownOne and known
88/// to be one in the expression. KnownZero contains all the bits that are known
89/// to be zero in the expression. These are provided to potentially allow the
90/// caller (which might recursively be SimplifyDemandedBits itself) to simplify
Craig Topper8a8413d2012-12-22 18:09:02 +000091/// the expression. KnownOne and KnownZero always follow the invariant that
Chris Lattnere0b4b722010-01-04 07:17:19 +000092/// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that
93/// the bits in KnownOne and KnownZero may only be accurate for those bits set
94/// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
95/// and KnownOne must all be the same.
96///
97/// This returns null if it did not change anything and it permits no
98/// simplification. This returns V itself if it did some simplification of V's
99/// operands based on the information about what bits are demanded. This returns
100/// some other non-null value if it found out that V is equal to another value
101/// in the context where the specified bits are demanded, but not for all users.
102Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
103 APInt &KnownZero, APInt &KnownOne,
104 unsigned Depth) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700105 assert(V != nullptr && "Null pointer of Value???");
Chris Lattnere0b4b722010-01-04 07:17:19 +0000106 assert(Depth <= 6 && "Limit Search Depth");
107 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000108 Type *VTy = V->getType();
Stephen Hines36b56882014-04-23 16:57:46 -0700109 assert((DL || !VTy->isPointerTy()) &&
Chris Lattnere0b4b722010-01-04 07:17:19 +0000110 "SimplifyDemandedBits needs to know bit widths!");
Stephen Hines36b56882014-04-23 16:57:46 -0700111 assert((!DL || DL->getTypeSizeInBits(VTy->getScalarType()) == BitWidth) &&
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000112 (!VTy->isIntOrIntVectorTy() ||
Chris Lattnere0b4b722010-01-04 07:17:19 +0000113 VTy->getScalarSizeInBits() == BitWidth) &&
114 KnownZero.getBitWidth() == BitWidth &&
115 KnownOne.getBitWidth() == BitWidth &&
116 "Value *V, DemandedMask, KnownZero and KnownOne "
117 "must have same BitWidth");
118 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
119 // We know all of the bits for a constant!
120 KnownOne = CI->getValue() & DemandedMask;
121 KnownZero = ~KnownOne & DemandedMask;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700122 return nullptr;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000123 }
124 if (isa<ConstantPointerNull>(V)) {
125 // We know all of the bits for a constant!
Jay Foad7a874dd2010-12-01 08:53:58 +0000126 KnownOne.clearAllBits();
Chris Lattnere0b4b722010-01-04 07:17:19 +0000127 KnownZero = DemandedMask;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700128 return nullptr;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000129 }
130
Jay Foad7a874dd2010-12-01 08:53:58 +0000131 KnownZero.clearAllBits();
132 KnownOne.clearAllBits();
Chris Lattnere0b4b722010-01-04 07:17:19 +0000133 if (DemandedMask == 0) { // Not demanding any bits from V.
134 if (isa<UndefValue>(V))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700135 return nullptr;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000136 return UndefValue::get(VTy);
137 }
Craig Topper8a8413d2012-12-22 18:09:02 +0000138
Chris Lattnere0b4b722010-01-04 07:17:19 +0000139 if (Depth == 6) // Limit search depth.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700140 return nullptr;
Craig Topper8a8413d2012-12-22 18:09:02 +0000141
Chris Lattnere0b4b722010-01-04 07:17:19 +0000142 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Duncan Sandsac512172010-01-29 06:18:46 +0000143 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000144
145 Instruction *I = dyn_cast<Instruction>(V);
146 if (!I) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700147 computeKnownBits(V, KnownZero, KnownOne, Depth);
148 return nullptr; // Only analyze instructions.
Chris Lattnere0b4b722010-01-04 07:17:19 +0000149 }
150
151 // If there are multiple uses of this value and we aren't at the root, then
152 // we can't do any simplifications of the operands, because DemandedMask
153 // only reflects the bits demanded by *one* of the users.
154 if (Depth != 0 && !I->hasOneUse()) {
155 // Despite the fact that we can't simplify this instruction in all User's
156 // context, we can at least compute the knownzero/knownone bits, and we can
157 // do simplifications that apply to *just* the one user if we know that
158 // this instruction has a simpler value in that context.
159 if (I->getOpcode() == Instruction::And) {
160 // If either the LHS or the RHS are Zero, the result is zero.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700161 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
162 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
Craig Topper8a8413d2012-12-22 18:09:02 +0000163
Chris Lattnere0b4b722010-01-04 07:17:19 +0000164 // If all of the demanded bits are known 1 on one side, return the other.
165 // These bits cannot contribute to the result of the 'and' in this
166 // context.
Craig Topper8a8413d2012-12-22 18:09:02 +0000167 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattnere0b4b722010-01-04 07:17:19 +0000168 (DemandedMask & ~LHSKnownZero))
169 return I->getOperand(0);
Craig Topper8a8413d2012-12-22 18:09:02 +0000170 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattnere0b4b722010-01-04 07:17:19 +0000171 (DemandedMask & ~RHSKnownZero))
172 return I->getOperand(1);
Craig Topper8a8413d2012-12-22 18:09:02 +0000173
Chris Lattnere0b4b722010-01-04 07:17:19 +0000174 // If all of the demanded bits in the inputs are known zeros, return zero.
175 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
176 return Constant::getNullValue(VTy);
Craig Topper8a8413d2012-12-22 18:09:02 +0000177
Chris Lattnere0b4b722010-01-04 07:17:19 +0000178 } else if (I->getOpcode() == Instruction::Or) {
179 // We can simplify (X|Y) -> X or Y in the user's context if we know that
180 // only bits from X or Y are demanded.
Craig Topper8a8413d2012-12-22 18:09:02 +0000181
Chris Lattnere0b4b722010-01-04 07:17:19 +0000182 // If either the LHS or the RHS are One, the result is One.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700183 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
184 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
Craig Topper8a8413d2012-12-22 18:09:02 +0000185
Chris Lattnere0b4b722010-01-04 07:17:19 +0000186 // If all of the demanded bits are known zero on one side, return the
187 // other. These bits cannot contribute to the result of the 'or' in this
188 // context.
Craig Topper8a8413d2012-12-22 18:09:02 +0000189 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattnere0b4b722010-01-04 07:17:19 +0000190 (DemandedMask & ~LHSKnownOne))
191 return I->getOperand(0);
Craig Topper8a8413d2012-12-22 18:09:02 +0000192 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattnere0b4b722010-01-04 07:17:19 +0000193 (DemandedMask & ~RHSKnownOne))
194 return I->getOperand(1);
Craig Topper8a8413d2012-12-22 18:09:02 +0000195
Chris Lattnere0b4b722010-01-04 07:17:19 +0000196 // If all of the potentially set bits on one side are known to be set on
197 // the other side, just use the 'other' side.
Craig Topper8a8413d2012-12-22 18:09:02 +0000198 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattnere0b4b722010-01-04 07:17:19 +0000199 (DemandedMask & (~RHSKnownZero)))
200 return I->getOperand(0);
Craig Topper8a8413d2012-12-22 18:09:02 +0000201 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattnere0b4b722010-01-04 07:17:19 +0000202 (DemandedMask & (~LHSKnownZero)))
203 return I->getOperand(1);
Shuxin Yanga09e18f2012-12-04 22:15:32 +0000204 } else if (I->getOpcode() == Instruction::Xor) {
205 // We can simplify (X^Y) -> X or Y in the user's context if we know that
206 // only bits from X or Y are demanded.
Craig Topper8a8413d2012-12-22 18:09:02 +0000207
Stephen Hinesdce4a402014-05-29 02:49:00 -0700208 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
209 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
Craig Topper8a8413d2012-12-22 18:09:02 +0000210
Shuxin Yanga09e18f2012-12-04 22:15:32 +0000211 // If all of the demanded bits are known zero on one side, return the
Craig Topper8a8413d2012-12-22 18:09:02 +0000212 // other.
Shuxin Yanga09e18f2012-12-04 22:15:32 +0000213 if ((DemandedMask & RHSKnownZero) == DemandedMask)
214 return I->getOperand(0);
215 if ((DemandedMask & LHSKnownZero) == DemandedMask)
216 return I->getOperand(1);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000217 }
Shuxin Yanga09e18f2012-12-04 22:15:32 +0000218
Chris Lattnere0b4b722010-01-04 07:17:19 +0000219 // Compute the KnownZero/KnownOne bits to simplify things downstream.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700220 computeKnownBits(I, KnownZero, KnownOne, Depth);
221 return nullptr;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000222 }
Craig Topper8a8413d2012-12-22 18:09:02 +0000223
Chris Lattnere0b4b722010-01-04 07:17:19 +0000224 // If this is the root being simplified, allow it to have multiple uses,
225 // just set the DemandedMask to all bits so that we can try to simplify the
226 // operands. This allows visitTruncInst (for example) to simplify the
227 // operand of a trunc without duplicating all the logic below.
228 if (Depth == 0 && !V->hasOneUse())
229 DemandedMask = APInt::getAllOnesValue(BitWidth);
Craig Topper8a8413d2012-12-22 18:09:02 +0000230
Chris Lattnere0b4b722010-01-04 07:17:19 +0000231 switch (I->getOpcode()) {
232 default:
Stephen Hinesdce4a402014-05-29 02:49:00 -0700233 computeKnownBits(I, KnownZero, KnownOne, Depth);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000234 break;
235 case Instruction::And:
236 // If either the LHS or the RHS are Zero, the result is zero.
237 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
238 RHSKnownZero, RHSKnownOne, Depth+1) ||
239 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
240 LHSKnownZero, LHSKnownOne, Depth+1))
241 return I;
Craig Topper8a8413d2012-12-22 18:09:02 +0000242 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
243 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattnere0b4b722010-01-04 07:17:19 +0000244
245 // If all of the demanded bits are known 1 on one side, return the other.
246 // These bits cannot contribute to the result of the 'and'.
Craig Topper8a8413d2012-12-22 18:09:02 +0000247 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattnere0b4b722010-01-04 07:17:19 +0000248 (DemandedMask & ~LHSKnownZero))
249 return I->getOperand(0);
Craig Topper8a8413d2012-12-22 18:09:02 +0000250 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattnere0b4b722010-01-04 07:17:19 +0000251 (DemandedMask & ~RHSKnownZero))
252 return I->getOperand(1);
Craig Topper8a8413d2012-12-22 18:09:02 +0000253
Chris Lattnere0b4b722010-01-04 07:17:19 +0000254 // If all of the demanded bits in the inputs are known zeros, return zero.
255 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
256 return Constant::getNullValue(VTy);
Craig Topper8a8413d2012-12-22 18:09:02 +0000257
Chris Lattnere0b4b722010-01-04 07:17:19 +0000258 // If the RHS is a constant, see if we can simplify it.
259 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
260 return I;
Craig Topper8a8413d2012-12-22 18:09:02 +0000261
Chris Lattnere0b4b722010-01-04 07:17:19 +0000262 // Output known-1 bits are only known if set in both the LHS & RHS.
Duncan Sandsac512172010-01-29 06:18:46 +0000263 KnownOne = RHSKnownOne & LHSKnownOne;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000264 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Duncan Sandsac512172010-01-29 06:18:46 +0000265 KnownZero = RHSKnownZero | LHSKnownZero;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000266 break;
267 case Instruction::Or:
268 // If either the LHS or the RHS are One, the result is One.
Craig Topper8a8413d2012-12-22 18:09:02 +0000269 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
Chris Lattnere0b4b722010-01-04 07:17:19 +0000270 RHSKnownZero, RHSKnownOne, Depth+1) ||
Craig Topper8a8413d2012-12-22 18:09:02 +0000271 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne,
Chris Lattnere0b4b722010-01-04 07:17:19 +0000272 LHSKnownZero, LHSKnownOne, Depth+1))
273 return I;
Craig Topper8a8413d2012-12-22 18:09:02 +0000274 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
275 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
276
Chris Lattnere0b4b722010-01-04 07:17:19 +0000277 // If all of the demanded bits are known zero on one side, return the other.
278 // These bits cannot contribute to the result of the 'or'.
Craig Topper8a8413d2012-12-22 18:09:02 +0000279 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattnere0b4b722010-01-04 07:17:19 +0000280 (DemandedMask & ~LHSKnownOne))
281 return I->getOperand(0);
Craig Topper8a8413d2012-12-22 18:09:02 +0000282 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattnere0b4b722010-01-04 07:17:19 +0000283 (DemandedMask & ~RHSKnownOne))
284 return I->getOperand(1);
285
286 // If all of the potentially set bits on one side are known to be set on
287 // the other side, just use the 'other' side.
Craig Topper8a8413d2012-12-22 18:09:02 +0000288 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattnere0b4b722010-01-04 07:17:19 +0000289 (DemandedMask & (~RHSKnownZero)))
290 return I->getOperand(0);
Craig Topper8a8413d2012-12-22 18:09:02 +0000291 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattnere0b4b722010-01-04 07:17:19 +0000292 (DemandedMask & (~LHSKnownZero)))
293 return I->getOperand(1);
Craig Topper8a8413d2012-12-22 18:09:02 +0000294
Chris Lattnere0b4b722010-01-04 07:17:19 +0000295 // If the RHS is a constant, see if we can simplify it.
296 if (ShrinkDemandedConstant(I, 1, DemandedMask))
297 return I;
Craig Topper8a8413d2012-12-22 18:09:02 +0000298
Chris Lattnere0b4b722010-01-04 07:17:19 +0000299 // Output known-0 bits are only known if clear in both the LHS & RHS.
Duncan Sandsac512172010-01-29 06:18:46 +0000300 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000301 // Output known-1 are known to be set if set in either the LHS | RHS.
Duncan Sandsac512172010-01-29 06:18:46 +0000302 KnownOne = RHSKnownOne | LHSKnownOne;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000303 break;
304 case Instruction::Xor: {
305 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
306 RHSKnownZero, RHSKnownOne, Depth+1) ||
Craig Topper8a8413d2012-12-22 18:09:02 +0000307 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Chris Lattnere0b4b722010-01-04 07:17:19 +0000308 LHSKnownZero, LHSKnownOne, Depth+1))
309 return I;
Craig Topper8a8413d2012-12-22 18:09:02 +0000310 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
311 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
312
Chris Lattnere0b4b722010-01-04 07:17:19 +0000313 // If all of the demanded bits are known zero on one side, return the other.
314 // These bits cannot contribute to the result of the 'xor'.
315 if ((DemandedMask & RHSKnownZero) == DemandedMask)
316 return I->getOperand(0);
317 if ((DemandedMask & LHSKnownZero) == DemandedMask)
318 return I->getOperand(1);
Craig Topper8a8413d2012-12-22 18:09:02 +0000319
Chris Lattnere0b4b722010-01-04 07:17:19 +0000320 // If all of the demanded bits are known to be zero on one side or the
321 // other, turn this into an *inclusive* or.
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000322 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Chris Lattnere0b4b722010-01-04 07:17:19 +0000323 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
Craig Topper8a8413d2012-12-22 18:09:02 +0000324 Instruction *Or =
Chris Lattnere0b4b722010-01-04 07:17:19 +0000325 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
326 I->getName());
Eli Friedman6fd5a602011-05-19 01:20:42 +0000327 return InsertNewInstWith(Or, *I);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000328 }
Craig Topper8a8413d2012-12-22 18:09:02 +0000329
Chris Lattnere0b4b722010-01-04 07:17:19 +0000330 // If all of the demanded bits on one side are known, and all of the set
331 // bits on that side are also known to be set on the other side, turn this
332 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000333 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper8a8413d2012-12-22 18:09:02 +0000334 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
Chris Lattnere0b4b722010-01-04 07:17:19 +0000335 // all known
336 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
337 Constant *AndC = Constant::getIntegerValue(VTy,
338 ~RHSKnownOne & DemandedMask);
Benjamin Kramera9390a42011-09-27 20:39:19 +0000339 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6fd5a602011-05-19 01:20:42 +0000340 return InsertNewInstWith(And, *I);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000341 }
342 }
Craig Topper8a8413d2012-12-22 18:09:02 +0000343
Chris Lattnere0b4b722010-01-04 07:17:19 +0000344 // If the RHS is a constant, see if we can simplify it.
345 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
346 if (ShrinkDemandedConstant(I, 1, DemandedMask))
347 return I;
Craig Topper8a8413d2012-12-22 18:09:02 +0000348
Chris Lattnere0b4b722010-01-04 07:17:19 +0000349 // If our LHS is an 'and' and if it has one use, and if any of the bits we
350 // are flipping are known to be set, then the xor is just resetting those
351 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
352 // simplifying both of them.
353 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
354 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
355 isa<ConstantInt>(I->getOperand(1)) &&
356 isa<ConstantInt>(LHSInst->getOperand(1)) &&
357 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
358 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
359 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
360 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper8a8413d2012-12-22 18:09:02 +0000361
Chris Lattnere0b4b722010-01-04 07:17:19 +0000362 Constant *AndC =
363 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramera9390a42011-09-27 20:39:19 +0000364 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6fd5a602011-05-19 01:20:42 +0000365 InsertNewInstWith(NewAnd, *I);
Craig Topper8a8413d2012-12-22 18:09:02 +0000366
Chris Lattnere0b4b722010-01-04 07:17:19 +0000367 Constant *XorC =
368 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramera9390a42011-09-27 20:39:19 +0000369 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6fd5a602011-05-19 01:20:42 +0000370 return InsertNewInstWith(NewXor, *I);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000371 }
Duncan Sandsac512172010-01-29 06:18:46 +0000372
373 // Output known-0 bits are known if clear or set in both the LHS & RHS.
374 KnownZero= (RHSKnownZero & LHSKnownZero) | (RHSKnownOne & LHSKnownOne);
375 // Output known-1 are known to be set if set in only one of the LHS, RHS.
376 KnownOne = (RHSKnownZero & LHSKnownOne) | (RHSKnownOne & LHSKnownZero);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000377 break;
378 }
379 case Instruction::Select:
380 if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask,
381 RHSKnownZero, RHSKnownOne, Depth+1) ||
Craig Topper8a8413d2012-12-22 18:09:02 +0000382 SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
Chris Lattnere0b4b722010-01-04 07:17:19 +0000383 LHSKnownZero, LHSKnownOne, Depth+1))
384 return I;
Craig Topper8a8413d2012-12-22 18:09:02 +0000385 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
386 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
387
Chris Lattnere0b4b722010-01-04 07:17:19 +0000388 // If the operands are constants, see if we can simplify them.
389 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
390 ShrinkDemandedConstant(I, 2, DemandedMask))
391 return I;
Craig Topper8a8413d2012-12-22 18:09:02 +0000392
Chris Lattnere0b4b722010-01-04 07:17:19 +0000393 // Only known if known in both the LHS and RHS.
Duncan Sandsac512172010-01-29 06:18:46 +0000394 KnownOne = RHSKnownOne & LHSKnownOne;
395 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000396 break;
397 case Instruction::Trunc: {
398 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +0000399 DemandedMask = DemandedMask.zext(truncBf);
400 KnownZero = KnownZero.zext(truncBf);
401 KnownOne = KnownOne.zext(truncBf);
Craig Topper8a8413d2012-12-22 18:09:02 +0000402 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Duncan Sandsac512172010-01-29 06:18:46 +0000403 KnownZero, KnownOne, Depth+1))
Chris Lattnere0b4b722010-01-04 07:17:19 +0000404 return I;
Jay Foad40f8f622010-12-07 08:25:19 +0000405 DemandedMask = DemandedMask.trunc(BitWidth);
406 KnownZero = KnownZero.trunc(BitWidth);
407 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper8a8413d2012-12-22 18:09:02 +0000408 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattnere0b4b722010-01-04 07:17:19 +0000409 break;
410 }
411 case Instruction::BitCast:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000412 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700413 return nullptr; // vector->int or fp->int?
Chris Lattnere0b4b722010-01-04 07:17:19 +0000414
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000415 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
416 if (VectorType *SrcVTy =
Chris Lattnere0b4b722010-01-04 07:17:19 +0000417 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
418 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
419 // Don't touch a bitcast between vectors of different element counts.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700420 return nullptr;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000421 } else
422 // Don't touch a scalar-to-vector bitcast.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700423 return nullptr;
Duncan Sands1df98592010-02-16 11:11:14 +0000424 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattnere0b4b722010-01-04 07:17:19 +0000425 // Don't touch a vector-to-scalar bitcast.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700426 return nullptr;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000427
428 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Duncan Sandsac512172010-01-29 06:18:46 +0000429 KnownZero, KnownOne, Depth+1))
Chris Lattnere0b4b722010-01-04 07:17:19 +0000430 return I;
Craig Topper8a8413d2012-12-22 18:09:02 +0000431 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattnere0b4b722010-01-04 07:17:19 +0000432 break;
433 case Instruction::ZExt: {
434 // Compute the bits in the result that are not present in the input.
435 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper8a8413d2012-12-22 18:09:02 +0000436
Jay Foad40f8f622010-12-07 08:25:19 +0000437 DemandedMask = DemandedMask.trunc(SrcBitWidth);
438 KnownZero = KnownZero.trunc(SrcBitWidth);
439 KnownOne = KnownOne.trunc(SrcBitWidth);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000440 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Duncan Sandsac512172010-01-29 06:18:46 +0000441 KnownZero, KnownOne, Depth+1))
Chris Lattnere0b4b722010-01-04 07:17:19 +0000442 return I;
Jay Foad40f8f622010-12-07 08:25:19 +0000443 DemandedMask = DemandedMask.zext(BitWidth);
444 KnownZero = KnownZero.zext(BitWidth);
445 KnownOne = KnownOne.zext(BitWidth);
Craig Topper8a8413d2012-12-22 18:09:02 +0000446 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattnere0b4b722010-01-04 07:17:19 +0000447 // The top bits are known to be zero.
Duncan Sandsac512172010-01-29 06:18:46 +0000448 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000449 break;
450 }
451 case Instruction::SExt: {
452 // Compute the bits in the result that are not present in the input.
453 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper8a8413d2012-12-22 18:09:02 +0000454
455 APInt InputDemandedBits = DemandedMask &
Chris Lattnere0b4b722010-01-04 07:17:19 +0000456 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
457
458 APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
459 // If any of the sign extended bits are demanded, we know that the sign
460 // bit is demanded.
461 if ((NewBits & DemandedMask) != 0)
Jay Foad7a874dd2010-12-01 08:53:58 +0000462 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper8a8413d2012-12-22 18:09:02 +0000463
Jay Foad40f8f622010-12-07 08:25:19 +0000464 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
465 KnownZero = KnownZero.trunc(SrcBitWidth);
466 KnownOne = KnownOne.trunc(SrcBitWidth);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000467 if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits,
Duncan Sandsac512172010-01-29 06:18:46 +0000468 KnownZero, KnownOne, Depth+1))
Chris Lattnere0b4b722010-01-04 07:17:19 +0000469 return I;
Jay Foad40f8f622010-12-07 08:25:19 +0000470 InputDemandedBits = InputDemandedBits.zext(BitWidth);
471 KnownZero = KnownZero.zext(BitWidth);
472 KnownOne = KnownOne.zext(BitWidth);
Craig Topper8a8413d2012-12-22 18:09:02 +0000473 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
474
Chris Lattnere0b4b722010-01-04 07:17:19 +0000475 // If the sign bit of the input is known set or clear, then we know the
476 // top bits of the result.
477
478 // If the input sign bit is known zero, or if the NewBits are not demanded
479 // convert this into a zero extension.
Duncan Sandsac512172010-01-29 06:18:46 +0000480 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattnere0b4b722010-01-04 07:17:19 +0000481 // Convert to ZExt cast
482 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6fd5a602011-05-19 01:20:42 +0000483 return InsertNewInstWith(NewCast, *I);
Duncan Sandsac512172010-01-29 06:18:46 +0000484 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
485 KnownOne |= NewBits;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000486 }
487 break;
488 }
489 case Instruction::Add: {
490 // Figure out what the input bits are. If the top bits of the and result
491 // are not demanded, then the add doesn't demand them from its input
492 // either.
493 unsigned NLZ = DemandedMask.countLeadingZeros();
Craig Topper8a8413d2012-12-22 18:09:02 +0000494
Chris Lattnere0b4b722010-01-04 07:17:19 +0000495 // If there is a constant on the RHS, there are a variety of xformations
496 // we can do.
497 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
498 // If null, this should be simplified elsewhere. Some of the xforms here
499 // won't work if the RHS is zero.
500 if (RHS->isZero())
501 break;
Craig Topper8a8413d2012-12-22 18:09:02 +0000502
Chris Lattnere0b4b722010-01-04 07:17:19 +0000503 // If the top bit of the output is demanded, demand everything from the
504 // input. Otherwise, we demand all the input bits except NLZ top bits.
505 APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
506
507 // Find information about known zero/one bits in the input.
Craig Topper8a8413d2012-12-22 18:09:02 +0000508 if (SimplifyDemandedBits(I->getOperandUse(0), InDemandedBits,
Chris Lattnere0b4b722010-01-04 07:17:19 +0000509 LHSKnownZero, LHSKnownOne, Depth+1))
510 return I;
511
512 // If the RHS of the add has bits set that can't affect the input, reduce
513 // the constant.
514 if (ShrinkDemandedConstant(I, 1, InDemandedBits))
515 return I;
Craig Topper8a8413d2012-12-22 18:09:02 +0000516
Chris Lattnere0b4b722010-01-04 07:17:19 +0000517 // Avoid excess work.
518 if (LHSKnownZero == 0 && LHSKnownOne == 0)
519 break;
Craig Topper8a8413d2012-12-22 18:09:02 +0000520
Chris Lattnere0b4b722010-01-04 07:17:19 +0000521 // Turn it into OR if input bits are zero.
522 if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
523 Instruction *Or =
524 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
525 I->getName());
Eli Friedman6fd5a602011-05-19 01:20:42 +0000526 return InsertNewInstWith(Or, *I);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000527 }
Craig Topper8a8413d2012-12-22 18:09:02 +0000528
Chris Lattnere0b4b722010-01-04 07:17:19 +0000529 // We can say something about the output known-zero and known-one bits,
530 // depending on potential carries from the input constant and the
531 // unknowns. For example if the LHS is known to have at most the 0x0F0F0
532 // bits set and the RHS constant is 0x01001, then we know we have a known
533 // one mask of 0x00001 and a known zero mask of 0xE0F0E.
Craig Topper8a8413d2012-12-22 18:09:02 +0000534
Chris Lattnere0b4b722010-01-04 07:17:19 +0000535 // To compute this, we first compute the potential carry bits. These are
536 // the bits which may be modified. I'm not aware of a better way to do
537 // this scan.
538 const APInt &RHSVal = RHS->getValue();
539 APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
Craig Topper8a8413d2012-12-22 18:09:02 +0000540
Chris Lattnere0b4b722010-01-04 07:17:19 +0000541 // Now that we know which bits have carries, compute the known-1/0 sets.
Craig Topper8a8413d2012-12-22 18:09:02 +0000542
Chris Lattnere0b4b722010-01-04 07:17:19 +0000543 // Bits are known one if they are known zero in one operand and one in the
544 // other, and there is no input carry.
Craig Topper8a8413d2012-12-22 18:09:02 +0000545 KnownOne = ((LHSKnownZero & RHSVal) |
Duncan Sandsac512172010-01-29 06:18:46 +0000546 (LHSKnownOne & ~RHSVal)) & ~CarryBits;
Craig Topper8a8413d2012-12-22 18:09:02 +0000547
Chris Lattnere0b4b722010-01-04 07:17:19 +0000548 // Bits are known zero if they are known zero in both operands and there
549 // is no input carry.
Duncan Sandsac512172010-01-29 06:18:46 +0000550 KnownZero = LHSKnownZero & ~RHSVal & ~CarryBits;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000551 } else {
552 // If the high-bits of this ADD are not demanded, then it does not demand
553 // the high bits of its LHS or RHS.
554 if (DemandedMask[BitWidth-1] == 0) {
555 // Right fill the mask of bits for this ADD to demand the most
556 // significant bit and all those below it.
557 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
558 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
559 LHSKnownZero, LHSKnownOne, Depth+1) ||
560 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
561 LHSKnownZero, LHSKnownOne, Depth+1))
562 return I;
563 }
564 }
565 break;
566 }
567 case Instruction::Sub:
568 // If the high-bits of this SUB are not demanded, then it does not demand
569 // the high bits of its LHS or RHS.
570 if (DemandedMask[BitWidth-1] == 0) {
571 // Right fill the mask of bits for this SUB to demand the most
572 // significant bit and all those below it.
573 uint32_t NLZ = DemandedMask.countLeadingZeros();
574 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
575 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
576 LHSKnownZero, LHSKnownOne, Depth+1) ||
577 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
578 LHSKnownZero, LHSKnownOne, Depth+1))
579 return I;
580 }
Benjamin Kramer1fdfae02011-12-24 17:31:38 +0000581
Stephen Hinesdce4a402014-05-29 02:49:00 -0700582 // Otherwise just hand the sub off to computeKnownBits to fill in
Chris Lattnere0b4b722010-01-04 07:17:19 +0000583 // the known zeros and ones.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700584 computeKnownBits(V, KnownZero, KnownOne, Depth);
Benjamin Kramer1fdfae02011-12-24 17:31:38 +0000585
586 // Turn this into a xor if LHS is 2^n-1 and the remaining bits are known
587 // zero.
588 if (ConstantInt *C0 = dyn_cast<ConstantInt>(I->getOperand(0))) {
589 APInt I0 = C0->getValue();
590 if ((I0 + 1).isPowerOf2() && (I0 | KnownZero).isAllOnesValue()) {
591 Instruction *Xor = BinaryOperator::CreateXor(I->getOperand(1), C0);
592 return InsertNewInstWith(Xor, *I);
593 }
594 }
Chris Lattnere0b4b722010-01-04 07:17:19 +0000595 break;
596 case Instruction::Shl:
597 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Shuxin Yangc8119762012-12-04 00:04:54 +0000598 {
599 Value *VarX; ConstantInt *C1;
600 if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
601 Instruction *Shr = cast<Instruction>(I->getOperand(0));
602 Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
603 KnownZero, KnownOne);
604 if (R)
605 return R;
606 }
607 }
608
Chris Lattnera81556f2011-02-10 05:09:34 +0000609 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000610 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper8a8413d2012-12-22 18:09:02 +0000611
Chris Lattnera81556f2011-02-10 05:09:34 +0000612 // If the shift is NUW/NSW, then it does demand the high bits.
613 ShlOperator *IOp = cast<ShlOperator>(I);
614 if (IOp->hasNoSignedWrap())
615 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
616 else if (IOp->hasNoUnsignedWrap())
617 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
Craig Topper8a8413d2012-12-22 18:09:02 +0000618
619 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
Duncan Sandsac512172010-01-29 06:18:46 +0000620 KnownZero, KnownOne, Depth+1))
Chris Lattnere0b4b722010-01-04 07:17:19 +0000621 return I;
Duncan Sandsac512172010-01-29 06:18:46 +0000622 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
623 KnownZero <<= ShiftAmt;
624 KnownOne <<= ShiftAmt;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000625 // low bits known zero.
626 if (ShiftAmt)
Duncan Sandsac512172010-01-29 06:18:46 +0000627 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000628 }
629 break;
630 case Instruction::LShr:
631 // For a logical shift right
632 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattnera81556f2011-02-10 05:09:34 +0000633 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper8a8413d2012-12-22 18:09:02 +0000634
Chris Lattnere0b4b722010-01-04 07:17:19 +0000635 // Unsigned shift right.
636 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper8a8413d2012-12-22 18:09:02 +0000637
Chris Lattnera81556f2011-02-10 05:09:34 +0000638 // If the shift is exact, then it does demand the low bits (and knows that
639 // they are zero).
640 if (cast<LShrOperator>(I)->isExact())
641 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper8a8413d2012-12-22 18:09:02 +0000642
Chris Lattnere0b4b722010-01-04 07:17:19 +0000643 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
Duncan Sandsac512172010-01-29 06:18:46 +0000644 KnownZero, KnownOne, Depth+1))
Chris Lattnere0b4b722010-01-04 07:17:19 +0000645 return I;
Duncan Sandsac512172010-01-29 06:18:46 +0000646 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
647 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
648 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000649 if (ShiftAmt) {
650 // Compute the new bits that are at the top now.
651 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsac512172010-01-29 06:18:46 +0000652 KnownZero |= HighBits; // high bits known zero.
Chris Lattnere0b4b722010-01-04 07:17:19 +0000653 }
654 }
655 break;
656 case Instruction::AShr:
657 // If this is an arithmetic shift right and only the low-bit is set, we can
658 // always convert this into a logical shr, even if the shift amount is
659 // variable. The low bit of the shift cannot be an input sign bit unless
660 // the shift amount is >= the size of the datatype, which is undefined.
661 if (DemandedMask == 1) {
662 // Perform the logical shift right.
663 Instruction *NewVal = BinaryOperator::CreateLShr(
664 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6fd5a602011-05-19 01:20:42 +0000665 return InsertNewInstWith(NewVal, *I);
Craig Topper8a8413d2012-12-22 18:09:02 +0000666 }
Chris Lattnere0b4b722010-01-04 07:17:19 +0000667
668 // If the sign bit is the only bit demanded by this ashr, then there is no
669 // need to do it, the shift doesn't change the high bit.
670 if (DemandedMask.isSignBit())
671 return I->getOperand(0);
Craig Topper8a8413d2012-12-22 18:09:02 +0000672
Chris Lattnere0b4b722010-01-04 07:17:19 +0000673 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattnera81556f2011-02-10 05:09:34 +0000674 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper8a8413d2012-12-22 18:09:02 +0000675
Chris Lattnere0b4b722010-01-04 07:17:19 +0000676 // Signed shift right.
677 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
678 // If any of the "high bits" are demanded, we should set the sign bit as
679 // demanded.
680 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Jay Foad7a874dd2010-12-01 08:53:58 +0000681 DemandedMaskIn.setBit(BitWidth-1);
Craig Topper8a8413d2012-12-22 18:09:02 +0000682
Chris Lattnera81556f2011-02-10 05:09:34 +0000683 // If the shift is exact, then it does demand the low bits (and knows that
684 // they are zero).
685 if (cast<AShrOperator>(I)->isExact())
686 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper8a8413d2012-12-22 18:09:02 +0000687
Chris Lattnere0b4b722010-01-04 07:17:19 +0000688 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
Duncan Sandsac512172010-01-29 06:18:46 +0000689 KnownZero, KnownOne, Depth+1))
Chris Lattnere0b4b722010-01-04 07:17:19 +0000690 return I;
Duncan Sandsac512172010-01-29 06:18:46 +0000691 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattnere0b4b722010-01-04 07:17:19 +0000692 // Compute the new bits that are at the top now.
693 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsac512172010-01-29 06:18:46 +0000694 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
695 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Craig Topper8a8413d2012-12-22 18:09:02 +0000696
Chris Lattnere0b4b722010-01-04 07:17:19 +0000697 // Handle the sign bits.
698 APInt SignBit(APInt::getSignBit(BitWidth));
699 // Adjust to where it is now in the mask.
Craig Topper8a8413d2012-12-22 18:09:02 +0000700 SignBit = APIntOps::lshr(SignBit, ShiftAmt);
701
Chris Lattnere0b4b722010-01-04 07:17:19 +0000702 // If the input sign bit is known to be zero, or if none of the top bits
703 // are demanded, turn this into an unsigned shift right.
Craig Topper8a8413d2012-12-22 18:09:02 +0000704 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Chris Lattnere0b4b722010-01-04 07:17:19 +0000705 (HighBits & ~DemandedMask) == HighBits) {
706 // Perform the logical shift right.
Nick Lewycky148fd552012-01-04 09:28:29 +0000707 BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
708 SA, I->getName());
709 NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
Eli Friedman6fd5a602011-05-19 01:20:42 +0000710 return InsertNewInstWith(NewVal, *I);
Duncan Sandsac512172010-01-29 06:18:46 +0000711 } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
712 KnownOne |= HighBits;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000713 }
714 }
715 break;
716 case Instruction::SRem:
717 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmanc6b018b2011-03-09 01:28:35 +0000718 // X % -1 demands all the bits because we don't want to introduce
719 // INT_MIN % -1 (== undef) by accident.
720 if (Rem->isAllOnesValue())
721 break;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000722 APInt RA = Rem->getValue().abs();
723 if (RA.isPowerOf2()) {
724 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
725 return I->getOperand(0);
726
727 APInt LowBits = RA - 1;
728 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
729 if (SimplifyDemandedBits(I->getOperandUse(0), Mask2,
730 LHSKnownZero, LHSKnownOne, Depth+1))
731 return I;
732
Duncan Sands2c473682010-01-28 17:22:42 +0000733 // The low bits of LHS are unchanged by the srem.
Duncan Sandsac512172010-01-29 06:18:46 +0000734 KnownZero = LHSKnownZero & LowBits;
735 KnownOne = LHSKnownOne & LowBits;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000736
Duncan Sands2c473682010-01-28 17:22:42 +0000737 // If LHS is non-negative or has all low bits zero, then the upper bits
738 // are all zero.
739 if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
740 KnownZero |= ~LowBits;
741
742 // If LHS is negative and not all low bits are zero, then the upper bits
743 // are all one.
744 if (LHSKnownOne[BitWidth-1] && ((LHSKnownOne & LowBits) != 0))
745 KnownOne |= ~LowBits;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000746
Craig Topper8a8413d2012-12-22 18:09:02 +0000747 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattnere0b4b722010-01-04 07:17:19 +0000748 }
749 }
Nick Lewyckyc14bc772011-03-07 01:50:10 +0000750
751 // The sign bit is the LHS's sign bit, except when the result of the
752 // remainder is zero.
753 if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
Nick Lewyckyc14bc772011-03-07 01:50:10 +0000754 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700755 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
Nick Lewyckyc14bc772011-03-07 01:50:10 +0000756 // If it's known zero, our sign bit is also zero.
757 if (LHSKnownZero.isNegative())
Benjamin Kramera6ff92a2013-05-09 16:32:32 +0000758 KnownZero.setBit(KnownZero.getBitWidth() - 1);
Nick Lewyckyc14bc772011-03-07 01:50:10 +0000759 }
Chris Lattnere0b4b722010-01-04 07:17:19 +0000760 break;
761 case Instruction::URem: {
762 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
763 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
764 if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes,
765 KnownZero2, KnownOne2, Depth+1) ||
766 SimplifyDemandedBits(I->getOperandUse(1), AllOnes,
767 KnownZero2, KnownOne2, Depth+1))
768 return I;
769
770 unsigned Leaders = KnownZero2.countLeadingOnes();
771 Leaders = std::max(Leaders,
772 KnownZero2.countLeadingOnes());
773 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
774 break;
775 }
776 case Instruction::Call:
777 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
778 switch (II->getIntrinsicID()) {
779 default: break;
780 case Intrinsic::bswap: {
781 // If the only bits demanded come from one byte of the bswap result,
782 // just shift the input byte into position to eliminate the bswap.
783 unsigned NLZ = DemandedMask.countLeadingZeros();
784 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper8a8413d2012-12-22 18:09:02 +0000785
Chris Lattnere0b4b722010-01-04 07:17:19 +0000786 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
787 // we need all the bits down to bit 8. Likewise, round NLZ. If we
788 // have 14 leading zeros, round to 8.
789 NLZ &= ~7;
790 NTZ &= ~7;
791 // If we need exactly one byte, we can do this transformation.
792 if (BitWidth-NLZ-NTZ == 8) {
793 unsigned ResultBit = NTZ;
794 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper8a8413d2012-12-22 18:09:02 +0000795
Chris Lattnere0b4b722010-01-04 07:17:19 +0000796 // Replace this with either a left or right shift to get the byte into
797 // the right place.
798 Instruction *NewVal;
799 if (InputBit > ResultBit)
Gabor Greif3e84e2e2010-06-24 12:35:13 +0000800 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattnere0b4b722010-01-04 07:17:19 +0000801 ConstantInt::get(I->getType(), InputBit-ResultBit));
802 else
Gabor Greif3e84e2e2010-06-24 12:35:13 +0000803 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattnere0b4b722010-01-04 07:17:19 +0000804 ConstantInt::get(I->getType(), ResultBit-InputBit));
805 NewVal->takeName(I);
Eli Friedman6fd5a602011-05-19 01:20:42 +0000806 return InsertNewInstWith(NewVal, *I);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000807 }
Craig Topper8a8413d2012-12-22 18:09:02 +0000808
Chris Lattnere0b4b722010-01-04 07:17:19 +0000809 // TODO: Could compute known zero/one bits based on the input.
810 break;
811 }
Chad Rosier62660312011-05-26 23:13:19 +0000812 case Intrinsic::x86_sse42_crc32_64_64:
Evan Cheng2e649602011-05-20 00:54:37 +0000813 KnownZero = APInt::getHighBitsSet(64, 32);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700814 return nullptr;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000815 }
816 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700817 computeKnownBits(V, KnownZero, KnownOne, Depth);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000818 break;
819 }
Craig Topper8a8413d2012-12-22 18:09:02 +0000820
Chris Lattnere0b4b722010-01-04 07:17:19 +0000821 // If the client is only demanding bits that we know, return the known
822 // constant.
Duncan Sandsac512172010-01-29 06:18:46 +0000823 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
824 return Constant::getIntegerValue(VTy, KnownOne);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700825 return nullptr;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000826}
827
Shuxin Yangc8119762012-12-04 00:04:54 +0000828/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
829/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
830/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
831/// of "C2-C1".
832///
833/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
834/// ..., bn}, without considering the specific value X is holding.
835/// This transformation is legal iff one of following conditions is hold:
836/// 1) All the bit in S are 0, in this case E1 == E2.
837/// 2) We don't care those bits in S, per the input DemandedMask.
838/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
839/// rest bits.
840///
841/// Currently we only test condition 2).
842///
843/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
844/// not successful.
845Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
846 Instruction *Shl, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne) {
847
Benjamin Kramera8517ee2013-08-30 14:35:35 +0000848 const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
849 const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
850 if (!ShlOp1 || !ShrOp1)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700851 return nullptr; // Noop.
Benjamin Kramera8517ee2013-08-30 14:35:35 +0000852
853 Value *VarX = Shr->getOperand(0);
854 Type *Ty = VarX->getType();
855 unsigned BitWidth = Ty->getIntegerBitWidth();
856 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700857 return nullptr; // Undef.
Benjamin Kramera8517ee2013-08-30 14:35:35 +0000858
859 unsigned ShlAmt = ShlOp1.getZExtValue();
860 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yangc8119762012-12-04 00:04:54 +0000861
862 KnownOne.clearAllBits();
863 KnownZero = APInt::getBitsSet(KnownZero.getBitWidth(), 0, ShlAmt-1);
864 KnownZero &= DemandedMask;
865
Benjamin Kramera8517ee2013-08-30 14:35:35 +0000866 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
867 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yangc8119762012-12-04 00:04:54 +0000868
869 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
870 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
871 (BitMask1.ashr(ShrAmt) << ShlAmt);
872
873 if (ShrAmt <= ShlAmt) {
874 BitMask2 <<= (ShlAmt - ShrAmt);
875 } else {
876 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
877 BitMask2.ashr(ShrAmt - ShlAmt);
878 }
879
880 // Check if condition-2 (see the comment to this function) is satified.
881 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
882 if (ShrAmt == ShlAmt)
883 return VarX;
884
885 if (!Shr->hasOneUse())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700886 return nullptr;
Shuxin Yangc8119762012-12-04 00:04:54 +0000887
888 BinaryOperator *New;
889 if (ShrAmt < ShlAmt) {
890 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
891 New = BinaryOperator::CreateShl(VarX, Amt);
892 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
893 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
894 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
895 } else {
896 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yangbba3eb02012-12-04 03:28:32 +0000897 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
898 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang5f70c2e2012-12-12 00:29:03 +0000899 if (cast<BinaryOperator>(Shr)->isExact())
900 New->setIsExact(true);
Shuxin Yangc8119762012-12-04 00:04:54 +0000901 }
902
903 return InsertNewInstWith(New, *Shl);
904 }
905
Stephen Hinesdce4a402014-05-29 02:49:00 -0700906 return nullptr;
Shuxin Yangc8119762012-12-04 00:04:54 +0000907}
Chris Lattnere0b4b722010-01-04 07:17:19 +0000908
909/// SimplifyDemandedVectorElts - The specified value produces a vector with
910/// any number of elements. DemandedElts contains the set of elements that are
911/// actually used by the caller. This method analyzes which elements of the
912/// operand are undef and returns that information in UndefElts.
913///
914/// If the information about demanded elements can be used to simplify the
915/// operation, the operation is simplified, then the resultant value is
916/// returned. This returns null if no change was made.
917Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattner8609fda2010-02-08 23:56:03 +0000918 APInt &UndefElts,
Chris Lattnere0b4b722010-01-04 07:17:19 +0000919 unsigned Depth) {
920 unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
921 APInt EltMask(APInt::getAllOnesValue(VWidth));
922 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
923
924 if (isa<UndefValue>(V)) {
925 // If the entire vector is undefined, just return this info.
926 UndefElts = EltMask;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700927 return nullptr;
Chris Lattner8609fda2010-02-08 23:56:03 +0000928 }
Craig Topper8a8413d2012-12-22 18:09:02 +0000929
Chris Lattner8609fda2010-02-08 23:56:03 +0000930 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattnere0b4b722010-01-04 07:17:19 +0000931 UndefElts = EltMask;
932 return UndefValue::get(V->getType());
933 }
934
935 UndefElts = 0;
Craig Topper8a8413d2012-12-22 18:09:02 +0000936
Chris Lattnera1f00f42012-01-25 06:48:06 +0000937 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
938 if (Constant *C = dyn_cast<Constant>(V)) {
939 // Check if this is identity. If so, return 0 since we are not simplifying
940 // anything.
941 if (DemandedElts.isAllOnesValue())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700942 return nullptr;
Chris Lattnera1f00f42012-01-25 06:48:06 +0000943
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000944 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattnere0b4b722010-01-04 07:17:19 +0000945 Constant *Undef = UndefValue::get(EltTy);
Craig Topper8a8413d2012-12-22 18:09:02 +0000946
Chris Lattnera1f00f42012-01-25 06:48:06 +0000947 SmallVector<Constant*, 16> Elts;
948 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattnere0b4b722010-01-04 07:17:19 +0000949 if (!DemandedElts[i]) { // If not demanded, set to undef.
950 Elts.push_back(Undef);
Jay Foad7a874dd2010-12-01 08:53:58 +0000951 UndefElts.setBit(i);
Chris Lattnera1f00f42012-01-25 06:48:06 +0000952 continue;
953 }
Craig Topper8a8413d2012-12-22 18:09:02 +0000954
Chris Lattnera1f00f42012-01-25 06:48:06 +0000955 Constant *Elt = C->getAggregateElement(i);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700956 if (!Elt) return nullptr;
Craig Topper8a8413d2012-12-22 18:09:02 +0000957
Chris Lattnera1f00f42012-01-25 06:48:06 +0000958 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattnere0b4b722010-01-04 07:17:19 +0000959 Elts.push_back(Undef);
Jay Foad7a874dd2010-12-01 08:53:58 +0000960 UndefElts.setBit(i);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000961 } else { // Otherwise, defined.
Chris Lattnera1f00f42012-01-25 06:48:06 +0000962 Elts.push_back(Elt);
Chris Lattnere0b4b722010-01-04 07:17:19 +0000963 }
Chris Lattnera1f00f42012-01-25 06:48:06 +0000964 }
Craig Topper8a8413d2012-12-22 18:09:02 +0000965
Chris Lattnere0b4b722010-01-04 07:17:19 +0000966 // If we changed the constant, return it.
Chris Lattner4ca829e2012-01-25 06:02:56 +0000967 Constant *NewCV = ConstantVector::get(Elts);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700968 return NewCV != C ? NewCV : nullptr;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000969 }
Craig Topper8a8413d2012-12-22 18:09:02 +0000970
Chris Lattnere0b4b722010-01-04 07:17:19 +0000971 // Limit search depth.
972 if (Depth == 10)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700973 return nullptr;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000974
Stuart Hastingsca1ef482011-05-17 22:13:31 +0000975 // If multiple users are using the root value, proceed with
Chris Lattnere0b4b722010-01-04 07:17:19 +0000976 // simplification conservatively assuming that all elements
977 // are needed.
978 if (!V->hasOneUse()) {
979 // Quit if we find multiple users of a non-root value though.
980 // They'll be handled when it's their turn to be visited by
981 // the main instcombine process.
982 if (Depth != 0)
983 // TODO: Just compute the UndefElts information recursively.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700984 return nullptr;
Chris Lattnere0b4b722010-01-04 07:17:19 +0000985
986 // Conservatively assume that all elements are needed.
987 DemandedElts = EltMask;
988 }
Craig Topper8a8413d2012-12-22 18:09:02 +0000989
Chris Lattnere0b4b722010-01-04 07:17:19 +0000990 Instruction *I = dyn_cast<Instruction>(V);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700991 if (!I) return nullptr; // Only analyze instructions.
Craig Topper8a8413d2012-12-22 18:09:02 +0000992
Chris Lattnere0b4b722010-01-04 07:17:19 +0000993 bool MadeChange = false;
994 APInt UndefElts2(VWidth, 0);
995 Value *TmpV;
996 switch (I->getOpcode()) {
997 default: break;
Craig Topper8a8413d2012-12-22 18:09:02 +0000998
Chris Lattnere0b4b722010-01-04 07:17:19 +0000999 case Instruction::InsertElement: {
1000 // If this is a variable index, we don't know which element it overwrites.
1001 // demand exactly the same input as we produce.
1002 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Stephen Hinesdce4a402014-05-29 02:49:00 -07001003 if (!Idx) {
Chris Lattnere0b4b722010-01-04 07:17:19 +00001004 // Note that we can't propagate undef elt info, because we don't know
1005 // which elt is getting updated.
1006 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1007 UndefElts2, Depth+1);
1008 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1009 break;
1010 }
Craig Topper8a8413d2012-12-22 18:09:02 +00001011
Chris Lattnere0b4b722010-01-04 07:17:19 +00001012 // If this is inserting an element that isn't demanded, remove this
1013 // insertelement.
1014 unsigned IdxNo = Idx->getZExtValue();
1015 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1016 Worklist.Add(I);
1017 return I->getOperand(0);
1018 }
Craig Topper8a8413d2012-12-22 18:09:02 +00001019
Chris Lattnere0b4b722010-01-04 07:17:19 +00001020 // Otherwise, the element inserted overwrites whatever was there, so the
1021 // input demanded set is simpler than the output set.
1022 APInt DemandedElts2 = DemandedElts;
Jay Foad7a874dd2010-12-01 08:53:58 +00001023 DemandedElts2.clearBit(IdxNo);
Chris Lattnere0b4b722010-01-04 07:17:19 +00001024 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
1025 UndefElts, Depth+1);
1026 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1027
1028 // The inserted element is defined.
Jay Foad7a874dd2010-12-01 08:53:58 +00001029 UndefElts.clearBit(IdxNo);
Chris Lattnere0b4b722010-01-04 07:17:19 +00001030 break;
1031 }
1032 case Instruction::ShuffleVector: {
1033 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
1034 uint64_t LHSVWidth =
1035 cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements();
1036 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1037 for (unsigned i = 0; i < VWidth; i++) {
1038 if (DemandedElts[i]) {
1039 unsigned MaskVal = Shuffle->getMaskValue(i);
1040 if (MaskVal != -1u) {
1041 assert(MaskVal < LHSVWidth * 2 &&
1042 "shufflevector mask index out of range!");
1043 if (MaskVal < LHSVWidth)
Jay Foad7a874dd2010-12-01 08:53:58 +00001044 LeftDemanded.setBit(MaskVal);
Chris Lattnere0b4b722010-01-04 07:17:19 +00001045 else
Jay Foad7a874dd2010-12-01 08:53:58 +00001046 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattnere0b4b722010-01-04 07:17:19 +00001047 }
1048 }
1049 }
1050
1051 APInt UndefElts4(LHSVWidth, 0);
1052 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
1053 UndefElts4, Depth+1);
1054 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1055
1056 APInt UndefElts3(LHSVWidth, 0);
1057 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
1058 UndefElts3, Depth+1);
1059 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1060
1061 bool NewUndefElts = false;
1062 for (unsigned i = 0; i < VWidth; i++) {
1063 unsigned MaskVal = Shuffle->getMaskValue(i);
1064 if (MaskVal == -1u) {
Jay Foad7a874dd2010-12-01 08:53:58 +00001065 UndefElts.setBit(i);
Eli Friedmanc82751d2011-09-15 01:14:29 +00001066 } else if (!DemandedElts[i]) {
1067 NewUndefElts = true;
1068 UndefElts.setBit(i);
Chris Lattnere0b4b722010-01-04 07:17:19 +00001069 } else if (MaskVal < LHSVWidth) {
1070 if (UndefElts4[MaskVal]) {
1071 NewUndefElts = true;
Jay Foad7a874dd2010-12-01 08:53:58 +00001072 UndefElts.setBit(i);
Chris Lattnere0b4b722010-01-04 07:17:19 +00001073 }
1074 } else {
1075 if (UndefElts3[MaskVal - LHSVWidth]) {
1076 NewUndefElts = true;
Jay Foad7a874dd2010-12-01 08:53:58 +00001077 UndefElts.setBit(i);
Chris Lattnere0b4b722010-01-04 07:17:19 +00001078 }
1079 }
1080 }
1081
1082 if (NewUndefElts) {
1083 // Add additional discovered undefs.
Chris Lattnera78fa8c2012-01-27 03:08:05 +00001084 SmallVector<Constant*, 16> Elts;
Chris Lattnere0b4b722010-01-04 07:17:19 +00001085 for (unsigned i = 0; i < VWidth; ++i) {
1086 if (UndefElts[i])
1087 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1088 else
1089 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1090 Shuffle->getMaskValue(i)));
1091 }
1092 I->setOperand(2, ConstantVector::get(Elts));
1093 MadeChange = true;
1094 }
1095 break;
1096 }
Pete Cooper7971de42012-07-26 23:10:24 +00001097 case Instruction::Select: {
1098 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1099 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1100 for (unsigned i = 0; i < VWidth; i++) {
1101 if (CV->getAggregateElement(i)->isNullValue())
1102 LeftDemanded.clearBit(i);
1103 else
1104 RightDemanded.clearBit(i);
1105 }
1106 }
1107
1108 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded,
1109 UndefElts, Depth+1);
1110 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1111
1112 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
1113 UndefElts2, Depth+1);
1114 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper8a8413d2012-12-22 18:09:02 +00001115
Pete Cooper7971de42012-07-26 23:10:24 +00001116 // Output elements are undefined if both are undefined.
1117 UndefElts &= UndefElts2;
1118 break;
1119 }
Chris Lattnere0b4b722010-01-04 07:17:19 +00001120 case Instruction::BitCast: {
1121 // Vector->vector casts only.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001122 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattnere0b4b722010-01-04 07:17:19 +00001123 if (!VTy) break;
1124 unsigned InVWidth = VTy->getNumElements();
1125 APInt InputDemandedElts(InVWidth, 0);
1126 unsigned Ratio;
1127
1128 if (VWidth == InVWidth) {
1129 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1130 // elements as are demanded of us.
1131 Ratio = 1;
1132 InputDemandedElts = DemandedElts;
1133 } else if (VWidth > InVWidth) {
1134 // Untested so far.
1135 break;
Craig Topper8a8413d2012-12-22 18:09:02 +00001136
Chris Lattnere0b4b722010-01-04 07:17:19 +00001137 // If there are more elements in the result than there are in the source,
1138 // then an input element is live if any of the corresponding output
1139 // elements are live.
1140 Ratio = VWidth/InVWidth;
1141 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1142 if (DemandedElts[OutIdx])
Jay Foad7a874dd2010-12-01 08:53:58 +00001143 InputDemandedElts.setBit(OutIdx/Ratio);
Chris Lattnere0b4b722010-01-04 07:17:19 +00001144 }
1145 } else {
1146 // Untested so far.
1147 break;
Craig Topper8a8413d2012-12-22 18:09:02 +00001148
Chris Lattnere0b4b722010-01-04 07:17:19 +00001149 // If there are more elements in the source than there are in the result,
1150 // then an input element is live if the corresponding output element is
1151 // live.
1152 Ratio = InVWidth/VWidth;
1153 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1154 if (DemandedElts[InIdx/Ratio])
Jay Foad7a874dd2010-12-01 08:53:58 +00001155 InputDemandedElts.setBit(InIdx);
Chris Lattnere0b4b722010-01-04 07:17:19 +00001156 }
Craig Topper8a8413d2012-12-22 18:09:02 +00001157
Chris Lattnere0b4b722010-01-04 07:17:19 +00001158 // div/rem demand all inputs, because they don't want divide by zero.
1159 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
1160 UndefElts2, Depth+1);
1161 if (TmpV) {
1162 I->setOperand(0, TmpV);
1163 MadeChange = true;
1164 }
Craig Topper8a8413d2012-12-22 18:09:02 +00001165
Chris Lattnere0b4b722010-01-04 07:17:19 +00001166 UndefElts = UndefElts2;
1167 if (VWidth > InVWidth) {
1168 llvm_unreachable("Unimp");
1169 // If there are more elements in the result than there are in the source,
1170 // then an output element is undef if the corresponding input element is
1171 // undef.
1172 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1173 if (UndefElts2[OutIdx/Ratio])
Jay Foad7a874dd2010-12-01 08:53:58 +00001174 UndefElts.setBit(OutIdx);
Chris Lattnere0b4b722010-01-04 07:17:19 +00001175 } else if (VWidth < InVWidth) {
1176 llvm_unreachable("Unimp");
1177 // If there are more elements in the source than there are in the result,
1178 // then a result element is undef if all of the corresponding input
1179 // elements are undef.
1180 UndefElts = ~0ULL >> (64-VWidth); // Start out all undef.
1181 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1182 if (!UndefElts2[InIdx]) // Not undef?
Jay Foad7a874dd2010-12-01 08:53:58 +00001183 UndefElts.clearBit(InIdx/Ratio); // Clear undef bit.
Chris Lattnere0b4b722010-01-04 07:17:19 +00001184 }
1185 break;
1186 }
1187 case Instruction::And:
1188 case Instruction::Or:
1189 case Instruction::Xor:
1190 case Instruction::Add:
1191 case Instruction::Sub:
1192 case Instruction::Mul:
1193 // div/rem demand all inputs, because they don't want divide by zero.
1194 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1195 UndefElts, Depth+1);
1196 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1197 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
1198 UndefElts2, Depth+1);
1199 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper8a8413d2012-12-22 18:09:02 +00001200
Chris Lattnere0b4b722010-01-04 07:17:19 +00001201 // Output elements are undefined if both are undefined. Consider things
1202 // like undef&0. The result is known zero, not undef.
1203 UndefElts &= UndefElts2;
1204 break;
Pete Cooper1121c782012-07-26 22:37:04 +00001205 case Instruction::FPTrunc:
1206 case Instruction::FPExt:
1207 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1208 UndefElts, Depth+1);
1209 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1210 break;
Craig Topper8a8413d2012-12-22 18:09:02 +00001211
Chris Lattnere0b4b722010-01-04 07:17:19 +00001212 case Instruction::Call: {
1213 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1214 if (!II) break;
1215 switch (II->getIntrinsicID()) {
1216 default: break;
Craig Topper8a8413d2012-12-22 18:09:02 +00001217
Chris Lattnere0b4b722010-01-04 07:17:19 +00001218 // Binary vector operations that work column-wise. A dest element is a
1219 // function of the corresponding input elements from the two inputs.
1220 case Intrinsic::x86_sse_sub_ss:
1221 case Intrinsic::x86_sse_mul_ss:
1222 case Intrinsic::x86_sse_min_ss:
1223 case Intrinsic::x86_sse_max_ss:
1224 case Intrinsic::x86_sse2_sub_sd:
1225 case Intrinsic::x86_sse2_mul_sd:
1226 case Intrinsic::x86_sse2_min_sd:
1227 case Intrinsic::x86_sse2_max_sd:
Gabor Greif30d25772010-06-28 16:45:00 +00001228 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
Eric Christopher551754c2010-04-16 23:37:20 +00001229 UndefElts, Depth+1);
Gabor Greif30d25772010-06-28 16:45:00 +00001230 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1231 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Eric Christopher551754c2010-04-16 23:37:20 +00001232 UndefElts2, Depth+1);
Gabor Greif30d25772010-06-28 16:45:00 +00001233 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattnere0b4b722010-01-04 07:17:19 +00001234
1235 // If only the low elt is demanded and this is a scalarizable intrinsic,
1236 // scalarize it now.
1237 if (DemandedElts == 1) {
1238 switch (II->getIntrinsicID()) {
1239 default: break;
1240 case Intrinsic::x86_sse_sub_ss:
1241 case Intrinsic::x86_sse_mul_ss:
1242 case Intrinsic::x86_sse2_sub_sd:
1243 case Intrinsic::x86_sse2_mul_sd:
1244 // TODO: Lower MIN/MAX/ABS/etc
Gabor Greif3e84e2e2010-06-24 12:35:13 +00001245 Value *LHS = II->getArgOperand(0);
1246 Value *RHS = II->getArgOperand(1);
Chris Lattnere0b4b722010-01-04 07:17:19 +00001247 // Extract the element as scalars.
Craig Topper8a8413d2012-12-22 18:09:02 +00001248 LHS = InsertNewInstWith(ExtractElementInst::Create(LHS,
Chris Lattnere0b4b722010-01-04 07:17:19 +00001249 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Eli Friedman6fd5a602011-05-19 01:20:42 +00001250 RHS = InsertNewInstWith(ExtractElementInst::Create(RHS,
Chris Lattnere0b4b722010-01-04 07:17:19 +00001251 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Craig Topper8a8413d2012-12-22 18:09:02 +00001252
Chris Lattnere0b4b722010-01-04 07:17:19 +00001253 switch (II->getIntrinsicID()) {
1254 default: llvm_unreachable("Case stmts out of sync!");
1255 case Intrinsic::x86_sse_sub_ss:
1256 case Intrinsic::x86_sse2_sub_sd:
Eli Friedman6fd5a602011-05-19 01:20:42 +00001257 TmpV = InsertNewInstWith(BinaryOperator::CreateFSub(LHS, RHS,
Chris Lattnere0b4b722010-01-04 07:17:19 +00001258 II->getName()), *II);
1259 break;
1260 case Intrinsic::x86_sse_mul_ss:
1261 case Intrinsic::x86_sse2_mul_sd:
Eli Friedman6fd5a602011-05-19 01:20:42 +00001262 TmpV = InsertNewInstWith(BinaryOperator::CreateFMul(LHS, RHS,
Chris Lattnere0b4b722010-01-04 07:17:19 +00001263 II->getName()), *II);
1264 break;
1265 }
Craig Topper8a8413d2012-12-22 18:09:02 +00001266
Chris Lattnere0b4b722010-01-04 07:17:19 +00001267 Instruction *New =
1268 InsertElementInst::Create(
1269 UndefValue::get(II->getType()), TmpV,
1270 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U, false),
1271 II->getName());
Eli Friedman6fd5a602011-05-19 01:20:42 +00001272 InsertNewInstWith(New, *II);
Chris Lattnere0b4b722010-01-04 07:17:19 +00001273 return New;
Craig Topper8a8413d2012-12-22 18:09:02 +00001274 }
Chris Lattnere0b4b722010-01-04 07:17:19 +00001275 }
Craig Topper8a8413d2012-12-22 18:09:02 +00001276
Chris Lattnere0b4b722010-01-04 07:17:19 +00001277 // Output elements are undefined if both are undefined. Consider things
1278 // like undef&0. The result is known zero, not undef.
1279 UndefElts &= UndefElts2;
1280 break;
1281 }
1282 break;
1283 }
1284 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07001285 return MadeChange ? I : nullptr;
Chris Lattnere0b4b722010-01-04 07:17:19 +00001286}