blob: 955776e0f85cf454259583e0e9829b70db89b1cd [file] [log] [blame]
Chris Lattner7e044912010-01-04 07:17:19 +00001//===- InstCombineSimplifyDemanded.cpp ------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains logic for simplifying instructions based on information
11// about how they are used.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carrutha9174582015-01-22 05:25:13 +000015#include "InstCombineInternal.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000017#include "llvm/IR/PatternMatch.h"
Chris Lattner7e044912010-01-04 07:17:19 +000018
19using namespace llvm;
Shuxin Yang63e999e2012-12-04 00:04:54 +000020using namespace llvm::PatternMatch;
Chris Lattner7e044912010-01-04 07:17:19 +000021
Chandler Carruth964daaa2014-04-22 02:55:47 +000022#define DEBUG_TYPE "instcombine"
23
Craig Topper4c947752012-12-22 18:09:02 +000024/// ShrinkDemandedConstant - Check to see if the specified operand of the
Chris Lattner7e044912010-01-04 07:17:19 +000025/// specified instruction is a constant integer. If so, check to see if there
26/// are any bits set in the constant that are not demanded. If so, shrink the
27/// constant and return true.
Craig Topper4c947752012-12-22 18:09:02 +000028static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
Chris Lattner7e044912010-01-04 07:17:19 +000029 APInt Demanded) {
30 assert(I && "No instruction?");
31 assert(OpNo < I->getNumOperands() && "Operand index too large");
32
33 // If the operand is not a constant integer, nothing to do.
34 ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo));
35 if (!OpC) return false;
36
37 // If there are no bits set that aren't demanded, nothing to do.
Jay Foad583abbc2010-12-07 08:25:19 +000038 Demanded = Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
Chris Lattner7e044912010-01-04 07:17:19 +000039 if ((~Demanded & OpC->getValue()) == 0)
40 return false;
41
42 // This instruction is producing bits that are not demanded. Shrink the RHS.
43 Demanded &= OpC->getValue();
44 I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Demanded));
David Majnemer42b83a52014-08-22 07:56:32 +000045
Chris Lattner7e044912010-01-04 07:17:19 +000046 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 Topper4c947752012-12-22 18:09:02 +000058
Mehdi Aminia28d91d2015-03-10 02:37:25 +000059 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, KnownZero, KnownOne,
60 0, &Inst);
Craig Topperf40110f2014-04-25 05:29:35 +000061 if (!V) return false;
Chris Lattner7e044912010-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 Topper4c947752012-12-22 18:09:02 +000070bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +000071 APInt &KnownZero, APInt &KnownOne,
72 unsigned Depth) {
David Majnemerfe58d132015-04-22 20:59:28 +000073 auto *UserI = dyn_cast<Instruction>(U.getUser());
74 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, KnownZero,
75 KnownOne, Depth, UserI);
Craig Topperf40110f2014-04-25 05:29:35 +000076 if (!NewVal) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000077 U = NewVal;
78 return true;
79}
80
81
82/// SimplifyDemandedUseBits - This function attempts to replace V with a simpler
83/// value based on the demanded bits. When this function is called, it is known
84/// that only the bits set in DemandedMask of the result of V are ever used
85/// downstream. Consequently, depending on the mask and V, it may be possible
86/// to replace V with a constant or one of its operands. In such cases, this
87/// function does the replacement and returns true. In all other cases, it
88/// returns false after analyzing the expression and setting KnownOne and known
89/// to be one in the expression. KnownZero contains all the bits that are known
90/// to be zero in the expression. These are provided to potentially allow the
91/// caller (which might recursively be SimplifyDemandedBits itself) to simplify
Craig Topper4c947752012-12-22 18:09:02 +000092/// the expression. KnownOne and KnownZero always follow the invariant that
Chris Lattner7e044912010-01-04 07:17:19 +000093/// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that
94/// the bits in KnownOne and KnownZero may only be accurate for those bits set
95/// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
96/// and KnownOne must all be the same.
97///
98/// This returns null if it did not change anything and it permits no
99/// simplification. This returns V itself if it did some simplification of V's
100/// operands based on the information about what bits are demanded. This returns
101/// some other non-null value if it found out that V is equal to another value
102/// in the context where the specified bits are demanded, but not for all users.
103Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
104 APInt &KnownZero, APInt &KnownOne,
Hal Finkel60db0582014-09-07 18:57:58 +0000105 unsigned Depth,
106 Instruction *CxtI) {
Craig Toppere73658d2014-04-28 04:05:08 +0000107 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000108 assert(Depth <= 6 && "Limit Search Depth");
109 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000110 Type *VTy = V->getType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000111 assert(
112 (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
113 KnownZero.getBitWidth() == BitWidth &&
114 KnownOne.getBitWidth() == BitWidth &&
115 "Value *V, DemandedMask, KnownZero and KnownOne "
116 "must have same BitWidth");
Chris Lattner7e044912010-01-04 07:17:19 +0000117 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
118 // We know all of the bits for a constant!
119 KnownOne = CI->getValue() & DemandedMask;
120 KnownZero = ~KnownOne & DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000121 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000122 }
123 if (isa<ConstantPointerNull>(V)) {
124 // We know all of the bits for a constant!
Jay Foad25a5e4c2010-12-01 08:53:58 +0000125 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000126 KnownZero = DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000127 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000128 }
129
Jay Foad25a5e4c2010-12-01 08:53:58 +0000130 KnownZero.clearAllBits();
131 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000132 if (DemandedMask == 0) { // Not demanding any bits from V.
133 if (isa<UndefValue>(V))
Craig Topperf40110f2014-04-25 05:29:35 +0000134 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000135 return UndefValue::get(VTy);
136 }
Craig Topper4c947752012-12-22 18:09:02 +0000137
Chris Lattner7e044912010-01-04 07:17:19 +0000138 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000139 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000140
Chris Lattner7e044912010-01-04 07:17:19 +0000141 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000142 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +0000143
144 Instruction *I = dyn_cast<Instruction>(V);
145 if (!I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000146 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000147 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000148 }
149
150 // If there are multiple uses of this value and we aren't at the root, then
151 // we can't do any simplifications of the operands, because DemandedMask
152 // only reflects the bits demanded by *one* of the users.
153 if (Depth != 0 && !I->hasOneUse()) {
154 // Despite the fact that we can't simplify this instruction in all User's
155 // context, we can at least compute the knownzero/knownone bits, and we can
156 // do simplifications that apply to *just* the one user if we know that
157 // this instruction has a simpler value in that context.
158 if (I->getOpcode() == Instruction::And) {
159 // If either the LHS or the RHS are Zero, the result is zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000160 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000161 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000162 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000163 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000164
Chris Lattner7e044912010-01-04 07:17:19 +0000165 // If all of the demanded bits are known 1 on one side, return the other.
166 // These bits cannot contribute to the result of the 'and' in this
167 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000168 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000169 (DemandedMask & ~LHSKnownZero))
170 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000171 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000172 (DemandedMask & ~RHSKnownZero))
173 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000174
Chris Lattner7e044912010-01-04 07:17:19 +0000175 // If all of the demanded bits in the inputs are known zeros, return zero.
176 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
177 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000178
Chris Lattner7e044912010-01-04 07:17:19 +0000179 } else if (I->getOpcode() == Instruction::Or) {
180 // We can simplify (X|Y) -> X or Y in the user's context if we know that
181 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000182
Chris Lattner7e044912010-01-04 07:17:19 +0000183 // If either the LHS or the RHS are One, the result is One.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000184 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000185 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000186 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000187 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000188
Chris Lattner7e044912010-01-04 07:17:19 +0000189 // If all of the demanded bits are known zero on one side, return the
190 // other. These bits cannot contribute to the result of the 'or' in this
191 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000192 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000193 (DemandedMask & ~LHSKnownOne))
194 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000195 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000196 (DemandedMask & ~RHSKnownOne))
197 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000198
Chris Lattner7e044912010-01-04 07:17:19 +0000199 // If all of the potentially set bits on one side are known to be set on
200 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000201 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000202 (DemandedMask & (~RHSKnownZero)))
203 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000204 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000205 (DemandedMask & (~LHSKnownZero)))
206 return I->getOperand(1);
Shuxin Yang73285932012-12-04 22:15:32 +0000207 } else if (I->getOpcode() == Instruction::Xor) {
208 // We can simplify (X^Y) -> X or Y in the user's context if we know that
209 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000210
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000211 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000212 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000213 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000214 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000215
Shuxin Yang73285932012-12-04 22:15:32 +0000216 // If all of the demanded bits are known zero on one side, return the
Craig Topper4c947752012-12-22 18:09:02 +0000217 // other.
Shuxin Yang73285932012-12-04 22:15:32 +0000218 if ((DemandedMask & RHSKnownZero) == DemandedMask)
219 return I->getOperand(0);
220 if ((DemandedMask & LHSKnownZero) == DemandedMask)
221 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000222 }
Shuxin Yang73285932012-12-04 22:15:32 +0000223
Chris Lattner7e044912010-01-04 07:17:19 +0000224 // Compute the KnownZero/KnownOne bits to simplify things downstream.
Hal Finkel60db0582014-09-07 18:57:58 +0000225 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000226 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000227 }
Craig Topper4c947752012-12-22 18:09:02 +0000228
Chris Lattner7e044912010-01-04 07:17:19 +0000229 // If this is the root being simplified, allow it to have multiple uses,
230 // just set the DemandedMask to all bits so that we can try to simplify the
231 // operands. This allows visitTruncInst (for example) to simplify the
232 // operand of a trunc without duplicating all the logic below.
233 if (Depth == 0 && !V->hasOneUse())
234 DemandedMask = APInt::getAllOnesValue(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000235
Chris Lattner7e044912010-01-04 07:17:19 +0000236 switch (I->getOpcode()) {
237 default:
Hal Finkel60db0582014-09-07 18:57:58 +0000238 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000239 break;
240 case Instruction::And:
241 // If either the LHS or the RHS are Zero, the result is zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000242 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, RHSKnownZero,
243 RHSKnownOne, Depth + 1) ||
Chris Lattner7e044912010-01-04 07:17:19 +0000244 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000245 LHSKnownZero, LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000246 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000247 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
248 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000249
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000250 // If the client is only demanding bits that we know, return the known
251 // constant.
252 if ((DemandedMask & ((RHSKnownZero | LHSKnownZero)|
253 (RHSKnownOne & LHSKnownOne))) == DemandedMask)
254 return Constant::getIntegerValue(VTy, RHSKnownOne & LHSKnownOne);
255
Chris Lattner7e044912010-01-04 07:17:19 +0000256 // If all of the demanded bits are known 1 on one side, return the other.
257 // These bits cannot contribute to the result of the 'and'.
Craig Topper4c947752012-12-22 18:09:02 +0000258 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000259 (DemandedMask & ~LHSKnownZero))
260 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000261 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000262 (DemandedMask & ~RHSKnownZero))
263 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000264
Chris Lattner7e044912010-01-04 07:17:19 +0000265 // If all of the demanded bits in the inputs are known zeros, return zero.
266 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
267 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000268
Chris Lattner7e044912010-01-04 07:17:19 +0000269 // If the RHS is a constant, see if we can simplify it.
270 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
271 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000272
Chris Lattner7e044912010-01-04 07:17:19 +0000273 // Output known-1 bits are only known if set in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000274 KnownOne = RHSKnownOne & LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000275 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000276 KnownZero = RHSKnownZero | LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000277 break;
278 case Instruction::Or:
279 // If either the LHS or the RHS are One, the result is One.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000280 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, RHSKnownZero,
281 RHSKnownOne, Depth + 1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000282 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000283 LHSKnownZero, LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000284 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000285 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
286 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
287
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000288 // If the client is only demanding bits that we know, return the known
289 // constant.
290 if ((DemandedMask & ((RHSKnownZero & LHSKnownZero)|
291 (RHSKnownOne | LHSKnownOne))) == DemandedMask)
292 return Constant::getIntegerValue(VTy, RHSKnownOne | LHSKnownOne);
293
Chris Lattner7e044912010-01-04 07:17:19 +0000294 // If all of the demanded bits are known zero on one side, return the other.
295 // These bits cannot contribute to the result of the 'or'.
Craig Topper4c947752012-12-22 18:09:02 +0000296 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000297 (DemandedMask & ~LHSKnownOne))
298 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000299 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000300 (DemandedMask & ~RHSKnownOne))
301 return I->getOperand(1);
302
303 // If all of the potentially set bits on one side are known to be set on
304 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000305 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000306 (DemandedMask & (~RHSKnownZero)))
307 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000308 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000309 (DemandedMask & (~LHSKnownZero)))
310 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000311
Chris Lattner7e044912010-01-04 07:17:19 +0000312 // If the RHS is a constant, see if we can simplify it.
313 if (ShrinkDemandedConstant(I, 1, DemandedMask))
314 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000315
Chris Lattner7e044912010-01-04 07:17:19 +0000316 // Output known-0 bits are only known if clear in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000317 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000318 // Output known-1 are known to be set if set in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000319 KnownOne = RHSKnownOne | LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000320 break;
321 case Instruction::Xor: {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000322 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, RHSKnownZero,
323 RHSKnownOne, Depth + 1) ||
324 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, LHSKnownZero,
325 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000326 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000327 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
328 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
329
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000330 // Output known-0 bits are known if clear or set in both the LHS & RHS.
331 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
332 (RHSKnownOne & LHSKnownOne);
333 // Output known-1 are known to be set if set in only one of the LHS, RHS.
334 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
335 (RHSKnownOne & LHSKnownZero);
336
337 // If the client is only demanding bits that we know, return the known
338 // constant.
339 if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
340 return Constant::getIntegerValue(VTy, IKnownOne);
341
Chris Lattner7e044912010-01-04 07:17:19 +0000342 // If all of the demanded bits are known zero on one side, return the other.
343 // These bits cannot contribute to the result of the 'xor'.
344 if ((DemandedMask & RHSKnownZero) == DemandedMask)
345 return I->getOperand(0);
346 if ((DemandedMask & LHSKnownZero) == DemandedMask)
347 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000348
Chris Lattner7e044912010-01-04 07:17:19 +0000349 // If all of the demanded bits are known to be zero on one side or the
350 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000351 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Chris Lattner7e044912010-01-04 07:17:19 +0000352 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
Craig Topper4c947752012-12-22 18:09:02 +0000353 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000354 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
355 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000356 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000357 }
Craig Topper4c947752012-12-22 18:09:02 +0000358
Chris Lattner7e044912010-01-04 07:17:19 +0000359 // If all of the demanded bits on one side are known, and all of the set
360 // bits on that side are also known to be set on the other side, turn this
361 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000362 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper4c947752012-12-22 18:09:02 +0000363 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
Chris Lattner7e044912010-01-04 07:17:19 +0000364 // all known
365 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
366 Constant *AndC = Constant::getIntegerValue(VTy,
367 ~RHSKnownOne & DemandedMask);
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000368 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000369 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000370 }
371 }
Craig Topper4c947752012-12-22 18:09:02 +0000372
Chris Lattner7e044912010-01-04 07:17:19 +0000373 // If the RHS is a constant, see if we can simplify it.
374 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
375 if (ShrinkDemandedConstant(I, 1, DemandedMask))
376 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000377
Chris Lattner7e044912010-01-04 07:17:19 +0000378 // If our LHS is an 'and' and if it has one use, and if any of the bits we
379 // are flipping are known to be set, then the xor is just resetting those
380 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
381 // simplifying both of them.
382 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
383 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
384 isa<ConstantInt>(I->getOperand(1)) &&
385 isa<ConstantInt>(LHSInst->getOperand(1)) &&
386 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
387 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
388 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
389 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000390
Chris Lattner7e044912010-01-04 07:17:19 +0000391 Constant *AndC =
392 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000393 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000394 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000395
Chris Lattner7e044912010-01-04 07:17:19 +0000396 Constant *XorC =
397 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000398 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000399 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000400 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000401
402 // Output known-0 bits are known if clear or set in both the LHS & RHS.
403 KnownZero= (RHSKnownZero & LHSKnownZero) | (RHSKnownOne & LHSKnownOne);
404 // Output known-1 are known to be set if set in only one of the LHS, RHS.
405 KnownOne = (RHSKnownZero & LHSKnownOne) | (RHSKnownOne & LHSKnownZero);
Chris Lattner7e044912010-01-04 07:17:19 +0000406 break;
407 }
408 case Instruction::Select:
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000409 if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask, RHSKnownZero,
410 RHSKnownOne, Depth + 1) ||
411 SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, LHSKnownZero,
412 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000413 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000414 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
415 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
416
Chris Lattner7e044912010-01-04 07:17:19 +0000417 // If the operands are constants, see if we can simplify them.
418 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
419 ShrinkDemandedConstant(I, 2, DemandedMask))
420 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000421
Chris Lattner7e044912010-01-04 07:17:19 +0000422 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000423 KnownOne = RHSKnownOne & LHSKnownOne;
424 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000425 break;
426 case Instruction::Trunc: {
427 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000428 DemandedMask = DemandedMask.zext(truncBf);
429 KnownZero = KnownZero.zext(truncBf);
430 KnownOne = KnownOne.zext(truncBf);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000431 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, KnownZero,
432 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000433 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000434 DemandedMask = DemandedMask.trunc(BitWidth);
435 KnownZero = KnownZero.trunc(BitWidth);
436 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000437 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000438 break;
439 }
440 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000441 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000442 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000443
Chris Lattner229907c2011-07-18 04:54:35 +0000444 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
445 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000446 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
447 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
448 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000449 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000450 } else
451 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000452 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000453 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000454 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000455 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000456
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000457 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, KnownZero,
458 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000459 return I;
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 break;
462 case Instruction::ZExt: {
463 // Compute the bits in the result that are not present in the input.
464 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000465
Jay Foad583abbc2010-12-07 08:25:19 +0000466 DemandedMask = DemandedMask.trunc(SrcBitWidth);
467 KnownZero = KnownZero.trunc(SrcBitWidth);
468 KnownOne = KnownOne.trunc(SrcBitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000469 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, KnownZero,
470 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000471 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000472 DemandedMask = DemandedMask.zext(BitWidth);
473 KnownZero = KnownZero.zext(BitWidth);
474 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000475 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000476 // The top bits are known to be zero.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000477 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000478 break;
479 }
480 case Instruction::SExt: {
481 // Compute the bits in the result that are not present in the input.
482 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000483
484 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000485 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
486
487 APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
488 // If any of the sign extended bits are demanded, we know that the sign
489 // bit is demanded.
490 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000491 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000492
Jay Foad583abbc2010-12-07 08:25:19 +0000493 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
494 KnownZero = KnownZero.trunc(SrcBitWidth);
495 KnownOne = KnownOne.trunc(SrcBitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000496 if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits, KnownZero,
497 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000498 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000499 InputDemandedBits = InputDemandedBits.zext(BitWidth);
500 KnownZero = KnownZero.zext(BitWidth);
501 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000502 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
503
Chris Lattner7e044912010-01-04 07:17:19 +0000504 // If the sign bit of the input is known set or clear, then we know the
505 // top bits of the result.
506
507 // If the input sign bit is known zero, or if the NewBits are not demanded
508 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000509 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000510 // Convert to ZExt cast
511 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000512 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000513 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
514 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000515 }
516 break;
517 }
Matthias Braune48484c2015-04-30 22:05:30 +0000518 case Instruction::Add:
519 case Instruction::Sub: {
520 /// If the high-bits of an ADD/SUB are not demanded, then we do not care
521 /// about the high bits of the operands.
Chris Lattner7e044912010-01-04 07:17:19 +0000522 unsigned NLZ = DemandedMask.countLeadingZeros();
Matthias Braune48484c2015-04-30 22:05:30 +0000523 if (NLZ > 0) {
524 // Right fill the mask of bits for this ADD/SUB to demand the most
Chris Lattner7e044912010-01-04 07:17:19 +0000525 // significant bit and all those below it.
Chris Lattner7e044912010-01-04 07:17:19 +0000526 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
527 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000528 LHSKnownZero, LHSKnownOne, Depth + 1) ||
Matthias Braune48484c2015-04-30 22:05:30 +0000529 ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
Chris Lattner7e044912010-01-04 07:17:19 +0000530 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
David Majnemer7d0e99c2015-04-22 22:42:05 +0000531 LHSKnownZero, LHSKnownOne, Depth + 1)) {
Matthias Braune48484c2015-04-30 22:05:30 +0000532 // Disable the nsw and nuw flags here: We can no longer guarantee that
533 // we won't wrap after simplification. Removing the nsw/nuw flags is
534 // legal here because the top bit is not demanded.
535 BinaryOperator &BinOP = *cast<BinaryOperator>(I);
536 BinOP.setHasNoSignedWrap(false);
537 BinOP.setHasNoUnsignedWrap(false);
Chris Lattner7e044912010-01-04 07:17:19 +0000538 return I;
David Majnemer7d0e99c2015-04-22 22:42:05 +0000539 }
Chris Lattner7e044912010-01-04 07:17:19 +0000540 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000541
Matthias Braune48484c2015-04-30 22:05:30 +0000542 // Otherwise just hand the add/sub off to computeKnownBits to fill in
Chris Lattner7e044912010-01-04 07:17:19 +0000543 // the known zeros and ones.
Hal Finkel60db0582014-09-07 18:57:58 +0000544 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000545 break;
Matthias Braune48484c2015-04-30 22:05:30 +0000546 }
Chris Lattner7e044912010-01-04 07:17:19 +0000547 case Instruction::Shl:
548 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000549 {
550 Value *VarX; ConstantInt *C1;
551 if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
552 Instruction *Shr = cast<Instruction>(I->getOperand(0));
553 Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
554 KnownZero, KnownOne);
555 if (R)
556 return R;
557 }
558 }
559
Chris Lattner768003c2011-02-10 05:09:34 +0000560 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000561 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000562
Chris Lattner768003c2011-02-10 05:09:34 +0000563 // If the shift is NUW/NSW, then it does demand the high bits.
564 ShlOperator *IOp = cast<ShlOperator>(I);
565 if (IOp->hasNoSignedWrap())
566 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
567 else if (IOp->hasNoUnsignedWrap())
568 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000569
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000570 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, KnownZero,
571 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000572 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000573 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
574 KnownZero <<= ShiftAmt;
575 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000576 // low bits known zero.
577 if (ShiftAmt)
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000578 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000579 }
580 break;
581 case Instruction::LShr:
582 // For a logical shift right
583 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000584 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000585
Chris Lattner7e044912010-01-04 07:17:19 +0000586 // Unsigned shift right.
587 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000588
Chris Lattner768003c2011-02-10 05:09:34 +0000589 // If the shift is exact, then it does demand the low bits (and knows that
590 // they are zero).
591 if (cast<LShrOperator>(I)->isExact())
592 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000593
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000594 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, KnownZero,
595 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000596 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000597 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
598 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
599 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000600 if (ShiftAmt) {
601 // Compute the new bits that are at the top now.
602 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000603 KnownZero |= HighBits; // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000604 }
605 }
606 break;
607 case Instruction::AShr:
608 // If this is an arithmetic shift right and only the low-bit is set, we can
609 // always convert this into a logical shr, even if the shift amount is
610 // variable. The low bit of the shift cannot be an input sign bit unless
611 // the shift amount is >= the size of the datatype, which is undefined.
612 if (DemandedMask == 1) {
613 // Perform the logical shift right.
614 Instruction *NewVal = BinaryOperator::CreateLShr(
615 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000616 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000617 }
Chris Lattner7e044912010-01-04 07:17:19 +0000618
619 // If the sign bit is the only bit demanded by this ashr, then there is no
620 // need to do it, the shift doesn't change the high bit.
621 if (DemandedMask.isSignBit())
622 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000623
Chris Lattner7e044912010-01-04 07:17:19 +0000624 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000625 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000626
Chris Lattner7e044912010-01-04 07:17:19 +0000627 // Signed shift right.
628 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
629 // If any of the "high bits" are demanded, we should set the sign bit as
630 // demanded.
631 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000632 DemandedMaskIn.setBit(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000633
Chris Lattner768003c2011-02-10 05:09:34 +0000634 // If the shift is exact, then it does demand the low bits (and knows that
635 // they are zero).
636 if (cast<AShrOperator>(I)->isExact())
637 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000638
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000639 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, KnownZero,
640 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000641 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000642 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000643 // Compute the new bits that are at the top now.
644 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000645 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
646 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000647
Chris Lattner7e044912010-01-04 07:17:19 +0000648 // Handle the sign bits.
649 APInt SignBit(APInt::getSignBit(BitWidth));
650 // Adjust to where it is now in the mask.
Craig Topper4c947752012-12-22 18:09:02 +0000651 SignBit = APIntOps::lshr(SignBit, ShiftAmt);
652
Chris Lattner7e044912010-01-04 07:17:19 +0000653 // If the input sign bit is known to be zero, or if none of the top bits
654 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000655 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Chris Lattner7e044912010-01-04 07:17:19 +0000656 (HighBits & ~DemandedMask) == HighBits) {
657 // Perform the logical shift right.
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000658 BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
659 SA, I->getName());
660 NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000661 return InsertNewInstWith(NewVal, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000662 } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
663 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000664 }
665 }
666 break;
667 case Instruction::SRem:
668 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000669 // X % -1 demands all the bits because we don't want to introduce
670 // INT_MIN % -1 (== undef) by accident.
671 if (Rem->isAllOnesValue())
672 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000673 APInt RA = Rem->getValue().abs();
674 if (RA.isPowerOf2()) {
675 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
676 return I->getOperand(0);
677
678 APInt LowBits = RA - 1;
679 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000680 if (SimplifyDemandedBits(I->getOperandUse(0), Mask2, LHSKnownZero,
681 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000682 return I;
683
Duncan Sands3a48b872010-01-28 17:22:42 +0000684 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000685 KnownZero = LHSKnownZero & LowBits;
686 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000687
Duncan Sands3a48b872010-01-28 17:22:42 +0000688 // If LHS is non-negative or has all low bits zero, then the upper bits
689 // are all zero.
690 if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
691 KnownZero |= ~LowBits;
692
693 // If LHS is negative and not all low bits are zero, then the upper bits
694 // are all one.
695 if (LHSKnownOne[BitWidth-1] && ((LHSKnownOne & LowBits) != 0))
696 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000697
Craig Topper4c947752012-12-22 18:09:02 +0000698 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000699 }
700 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000701
702 // The sign bit is the LHS's sign bit, except when the result of the
703 // remainder is zero.
704 if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
Nick Lewyckye4679792011-03-07 01:50:10 +0000705 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000706 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000707 CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000708 // If it's known zero, our sign bit is also zero.
709 if (LHSKnownZero.isNegative())
Benjamin Kramer21b972a2013-05-09 16:32:32 +0000710 KnownZero.setBit(KnownZero.getBitWidth() - 1);
Nick Lewyckye4679792011-03-07 01:50:10 +0000711 }
Chris Lattner7e044912010-01-04 07:17:19 +0000712 break;
713 case Instruction::URem: {
714 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
715 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000716 if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes, KnownZero2,
717 KnownOne2, Depth + 1) ||
718 SimplifyDemandedBits(I->getOperandUse(1), AllOnes, KnownZero2,
719 KnownOne2, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000720 return I;
721
722 unsigned Leaders = KnownZero2.countLeadingOnes();
723 Leaders = std::max(Leaders,
724 KnownZero2.countLeadingOnes());
725 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
726 break;
727 }
728 case Instruction::Call:
729 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
730 switch (II->getIntrinsicID()) {
731 default: break;
732 case Intrinsic::bswap: {
733 // If the only bits demanded come from one byte of the bswap result,
734 // just shift the input byte into position to eliminate the bswap.
735 unsigned NLZ = DemandedMask.countLeadingZeros();
736 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000737
Chris Lattner7e044912010-01-04 07:17:19 +0000738 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
739 // we need all the bits down to bit 8. Likewise, round NLZ. If we
740 // have 14 leading zeros, round to 8.
741 NLZ &= ~7;
742 NTZ &= ~7;
743 // If we need exactly one byte, we can do this transformation.
744 if (BitWidth-NLZ-NTZ == 8) {
745 unsigned ResultBit = NTZ;
746 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000747
Chris Lattner7e044912010-01-04 07:17:19 +0000748 // Replace this with either a left or right shift to get the byte into
749 // the right place.
750 Instruction *NewVal;
751 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000752 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000753 ConstantInt::get(I->getType(), InputBit-ResultBit));
754 else
Gabor Greif79430172010-06-24 12:35:13 +0000755 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000756 ConstantInt::get(I->getType(), ResultBit-InputBit));
757 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000758 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000759 }
Craig Topper4c947752012-12-22 18:09:02 +0000760
Chris Lattner7e044912010-01-04 07:17:19 +0000761 // TODO: Could compute known zero/one bits based on the input.
762 break;
763 }
Chad Rosierb3628842011-05-26 23:13:19 +0000764 case Intrinsic::x86_sse42_crc32_64_64:
Evan Chenge8d2e9e2011-05-20 00:54:37 +0000765 KnownZero = APInt::getHighBitsSet(64, 32);
Craig Topperf40110f2014-04-25 05:29:35 +0000766 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000767 }
768 }
Hal Finkel60db0582014-09-07 18:57:58 +0000769 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000770 break;
771 }
Craig Topper4c947752012-12-22 18:09:02 +0000772
Chris Lattner7e044912010-01-04 07:17:19 +0000773 // If the client is only demanding bits that we know, return the known
774 // constant.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000775 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
776 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000777 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000778}
779
Shuxin Yang63e999e2012-12-04 00:04:54 +0000780/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
781/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
782/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
783/// of "C2-C1".
784///
785/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
786/// ..., bn}, without considering the specific value X is holding.
787/// This transformation is legal iff one of following conditions is hold:
788/// 1) All the bit in S are 0, in this case E1 == E2.
789/// 2) We don't care those bits in S, per the input DemandedMask.
790/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
791/// rest bits.
792///
793/// Currently we only test condition 2).
794///
795/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
796/// not successful.
797Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
798 Instruction *Shl, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne) {
799
Benjamin Kramer010f1082013-08-30 14:35:35 +0000800 const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
801 const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
802 if (!ShlOp1 || !ShrOp1)
Craig Topperf40110f2014-04-25 05:29:35 +0000803 return nullptr; // Noop.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000804
805 Value *VarX = Shr->getOperand(0);
806 Type *Ty = VarX->getType();
807 unsigned BitWidth = Ty->getIntegerBitWidth();
808 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000809 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000810
811 unsigned ShlAmt = ShlOp1.getZExtValue();
812 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000813
814 KnownOne.clearAllBits();
815 KnownZero = APInt::getBitsSet(KnownZero.getBitWidth(), 0, ShlAmt-1);
816 KnownZero &= DemandedMask;
817
Benjamin Kramer010f1082013-08-30 14:35:35 +0000818 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
819 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000820
821 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
822 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
823 (BitMask1.ashr(ShrAmt) << ShlAmt);
824
825 if (ShrAmt <= ShlAmt) {
826 BitMask2 <<= (ShlAmt - ShrAmt);
827 } else {
828 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
829 BitMask2.ashr(ShrAmt - ShlAmt);
830 }
831
832 // Check if condition-2 (see the comment to this function) is satified.
833 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
834 if (ShrAmt == ShlAmt)
835 return VarX;
836
837 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000838 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000839
840 BinaryOperator *New;
841 if (ShrAmt < ShlAmt) {
842 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
843 New = BinaryOperator::CreateShl(VarX, Amt);
844 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
845 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
846 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
847 } else {
848 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000849 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
850 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000851 if (cast<BinaryOperator>(Shr)->isExact())
852 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000853 }
854
855 return InsertNewInstWith(New, *Shl);
856 }
857
Craig Topperf40110f2014-04-25 05:29:35 +0000858 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000859}
Chris Lattner7e044912010-01-04 07:17:19 +0000860
861/// SimplifyDemandedVectorElts - The specified value produces a vector with
862/// any number of elements. DemandedElts contains the set of elements that are
863/// actually used by the caller. This method analyzes which elements of the
864/// operand are undef and returns that information in UndefElts.
865///
866/// If the information about demanded elements can be used to simplify the
867/// operation, the operation is simplified, then the resultant value is
868/// returned. This returns null if no change was made.
869Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000870 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000871 unsigned Depth) {
872 unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
873 APInt EltMask(APInt::getAllOnesValue(VWidth));
874 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
875
876 if (isa<UndefValue>(V)) {
877 // If the entire vector is undefined, just return this info.
878 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000879 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000880 }
Craig Topper4c947752012-12-22 18:09:02 +0000881
Chris Lattnerb22423c2010-02-08 23:56:03 +0000882 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000883 UndefElts = EltMask;
884 return UndefValue::get(V->getType());
885 }
886
887 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000888
Chris Lattner67058832012-01-25 06:48:06 +0000889 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
890 if (Constant *C = dyn_cast<Constant>(V)) {
891 // Check if this is identity. If so, return 0 since we are not simplifying
892 // anything.
893 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000894 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000895
Chris Lattner229907c2011-07-18 04:54:35 +0000896 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000897 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000898
Chris Lattner67058832012-01-25 06:48:06 +0000899 SmallVector<Constant*, 16> Elts;
900 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000901 if (!DemandedElts[i]) { // If not demanded, set to undef.
902 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000903 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +0000904 continue;
905 }
Craig Topper4c947752012-12-22 18:09:02 +0000906
Chris Lattner67058832012-01-25 06:48:06 +0000907 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000908 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000909
Chris Lattner67058832012-01-25 06:48:06 +0000910 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000911 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000912 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +0000913 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +0000914 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +0000915 }
Chris Lattner67058832012-01-25 06:48:06 +0000916 }
Craig Topper4c947752012-12-22 18:09:02 +0000917
Chris Lattner7e044912010-01-04 07:17:19 +0000918 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +0000919 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +0000920 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000921 }
Craig Topper4c947752012-12-22 18:09:02 +0000922
Chris Lattner7e044912010-01-04 07:17:19 +0000923 // Limit search depth.
924 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +0000925 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000926
Stuart Hastings5bd18b62011-05-17 22:13:31 +0000927 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +0000928 // simplification conservatively assuming that all elements
929 // are needed.
930 if (!V->hasOneUse()) {
931 // Quit if we find multiple users of a non-root value though.
932 // They'll be handled when it's their turn to be visited by
933 // the main instcombine process.
934 if (Depth != 0)
935 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +0000936 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000937
938 // Conservatively assume that all elements are needed.
939 DemandedElts = EltMask;
940 }
Craig Topper4c947752012-12-22 18:09:02 +0000941
Chris Lattner7e044912010-01-04 07:17:19 +0000942 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +0000943 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +0000944
Chris Lattner7e044912010-01-04 07:17:19 +0000945 bool MadeChange = false;
946 APInt UndefElts2(VWidth, 0);
947 Value *TmpV;
948 switch (I->getOpcode()) {
949 default: break;
Craig Topper4c947752012-12-22 18:09:02 +0000950
Chris Lattner7e044912010-01-04 07:17:19 +0000951 case Instruction::InsertElement: {
952 // If this is a variable index, we don't know which element it overwrites.
953 // demand exactly the same input as we produce.
954 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +0000955 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +0000956 // Note that we can't propagate undef elt info, because we don't know
957 // which elt is getting updated.
958 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000959 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +0000960 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
961 break;
962 }
Craig Topper4c947752012-12-22 18:09:02 +0000963
Chris Lattner7e044912010-01-04 07:17:19 +0000964 // If this is inserting an element that isn't demanded, remove this
965 // insertelement.
966 unsigned IdxNo = Idx->getZExtValue();
967 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
968 Worklist.Add(I);
969 return I->getOperand(0);
970 }
Craig Topper4c947752012-12-22 18:09:02 +0000971
Chris Lattner7e044912010-01-04 07:17:19 +0000972 // Otherwise, the element inserted overwrites whatever was there, so the
973 // input demanded set is simpler than the output set.
974 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +0000975 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +0000976 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000977 UndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +0000978 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
979
980 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +0000981 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +0000982 break;
983 }
984 case Instruction::ShuffleVector: {
985 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
986 uint64_t LHSVWidth =
987 cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements();
988 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
989 for (unsigned i = 0; i < VWidth; i++) {
990 if (DemandedElts[i]) {
991 unsigned MaskVal = Shuffle->getMaskValue(i);
992 if (MaskVal != -1u) {
993 assert(MaskVal < LHSVWidth * 2 &&
994 "shufflevector mask index out of range!");
995 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000996 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +0000997 else
Jay Foad25a5e4c2010-12-01 08:53:58 +0000998 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000999 }
1000 }
1001 }
1002
1003 APInt UndefElts4(LHSVWidth, 0);
1004 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001005 UndefElts4, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001006 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1007
1008 APInt UndefElts3(LHSVWidth, 0);
1009 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001010 UndefElts3, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001011 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1012
1013 bool NewUndefElts = false;
1014 for (unsigned i = 0; i < VWidth; i++) {
1015 unsigned MaskVal = Shuffle->getMaskValue(i);
1016 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001017 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001018 } else if (!DemandedElts[i]) {
1019 NewUndefElts = true;
1020 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001021 } else if (MaskVal < LHSVWidth) {
1022 if (UndefElts4[MaskVal]) {
1023 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001024 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001025 }
1026 } else {
1027 if (UndefElts3[MaskVal - LHSVWidth]) {
1028 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001029 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001030 }
1031 }
1032 }
1033
1034 if (NewUndefElts) {
1035 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001036 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001037 for (unsigned i = 0; i < VWidth; ++i) {
1038 if (UndefElts[i])
1039 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1040 else
1041 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1042 Shuffle->getMaskValue(i)));
1043 }
1044 I->setOperand(2, ConstantVector::get(Elts));
1045 MadeChange = true;
1046 }
1047 break;
1048 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001049 case Instruction::Select: {
1050 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1051 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1052 for (unsigned i = 0; i < VWidth; i++) {
1053 if (CV->getAggregateElement(i)->isNullValue())
1054 LeftDemanded.clearBit(i);
1055 else
1056 RightDemanded.clearBit(i);
1057 }
1058 }
1059
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001060 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded, UndefElts,
1061 Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001062 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1063
1064 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001065 UndefElts2, Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001066 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001067
Pete Cooperabc13af2012-07-26 23:10:24 +00001068 // Output elements are undefined if both are undefined.
1069 UndefElts &= UndefElts2;
1070 break;
1071 }
Chris Lattner7e044912010-01-04 07:17:19 +00001072 case Instruction::BitCast: {
1073 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001074 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001075 if (!VTy) break;
1076 unsigned InVWidth = VTy->getNumElements();
1077 APInt InputDemandedElts(InVWidth, 0);
1078 unsigned Ratio;
1079
1080 if (VWidth == InVWidth) {
1081 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1082 // elements as are demanded of us.
1083 Ratio = 1;
1084 InputDemandedElts = DemandedElts;
1085 } else if (VWidth > InVWidth) {
1086 // Untested so far.
1087 break;
Craig Topper4c947752012-12-22 18:09:02 +00001088
Chris Lattner7e044912010-01-04 07:17:19 +00001089 // If there are more elements in the result than there are in the source,
1090 // then an input element is live if any of the corresponding output
1091 // elements are live.
1092 Ratio = VWidth/InVWidth;
1093 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1094 if (DemandedElts[OutIdx])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001095 InputDemandedElts.setBit(OutIdx/Ratio);
Chris Lattner7e044912010-01-04 07:17:19 +00001096 }
1097 } else {
1098 // Untested so far.
1099 break;
Craig Topper4c947752012-12-22 18:09:02 +00001100
Chris Lattner7e044912010-01-04 07:17:19 +00001101 // If there are more elements in the source than there are in the result,
1102 // then an input element is live if the corresponding output element is
1103 // live.
1104 Ratio = InVWidth/VWidth;
1105 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1106 if (DemandedElts[InIdx/Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001107 InputDemandedElts.setBit(InIdx);
Chris Lattner7e044912010-01-04 07:17:19 +00001108 }
Craig Topper4c947752012-12-22 18:09:02 +00001109
Chris Lattner7e044912010-01-04 07:17:19 +00001110 // div/rem demand all inputs, because they don't want divide by zero.
1111 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001112 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001113 if (TmpV) {
1114 I->setOperand(0, TmpV);
1115 MadeChange = true;
1116 }
Craig Topper4c947752012-12-22 18:09:02 +00001117
Chris Lattner7e044912010-01-04 07:17:19 +00001118 UndefElts = UndefElts2;
1119 if (VWidth > InVWidth) {
1120 llvm_unreachable("Unimp");
1121 // If there are more elements in the result than there are in the source,
1122 // then an output element is undef if the corresponding input element is
1123 // undef.
1124 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1125 if (UndefElts2[OutIdx/Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001126 UndefElts.setBit(OutIdx);
Chris Lattner7e044912010-01-04 07:17:19 +00001127 } else if (VWidth < InVWidth) {
1128 llvm_unreachable("Unimp");
1129 // If there are more elements in the source than there are in the result,
1130 // then a result element is undef if all of the corresponding input
1131 // elements are undef.
1132 UndefElts = ~0ULL >> (64-VWidth); // Start out all undef.
1133 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1134 if (!UndefElts2[InIdx]) // Not undef?
Jay Foad25a5e4c2010-12-01 08:53:58 +00001135 UndefElts.clearBit(InIdx/Ratio); // Clear undef bit.
Chris Lattner7e044912010-01-04 07:17:19 +00001136 }
1137 break;
1138 }
1139 case Instruction::And:
1140 case Instruction::Or:
1141 case Instruction::Xor:
1142 case Instruction::Add:
1143 case Instruction::Sub:
1144 case Instruction::Mul:
1145 // div/rem demand all inputs, because they don't want divide by zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001146 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1147 Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001148 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1149 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001150 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001151 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001152
Chris Lattner7e044912010-01-04 07:17:19 +00001153 // Output elements are undefined if both are undefined. Consider things
1154 // like undef&0. The result is known zero, not undef.
1155 UndefElts &= UndefElts2;
1156 break;
Pete Coopere807e452012-07-26 22:37:04 +00001157 case Instruction::FPTrunc:
1158 case Instruction::FPExt:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001159 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1160 Depth + 1);
Pete Coopere807e452012-07-26 22:37:04 +00001161 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1162 break;
Craig Topper4c947752012-12-22 18:09:02 +00001163
Chris Lattner7e044912010-01-04 07:17:19 +00001164 case Instruction::Call: {
1165 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1166 if (!II) break;
1167 switch (II->getIntrinsicID()) {
1168 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001169
Chris Lattner7e044912010-01-04 07:17:19 +00001170 // Binary vector operations that work column-wise. A dest element is a
1171 // function of the corresponding input elements from the two inputs.
1172 case Intrinsic::x86_sse_sub_ss:
1173 case Intrinsic::x86_sse_mul_ss:
1174 case Intrinsic::x86_sse_min_ss:
1175 case Intrinsic::x86_sse_max_ss:
1176 case Intrinsic::x86_sse2_sub_sd:
1177 case Intrinsic::x86_sse2_mul_sd:
1178 case Intrinsic::x86_sse2_min_sd:
1179 case Intrinsic::x86_sse2_max_sd:
Gabor Greife23efee2010-06-28 16:45:00 +00001180 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001181 UndefElts, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001182 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1183 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001184 UndefElts2, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001185 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001186
1187 // If only the low elt is demanded and this is a scalarizable intrinsic,
1188 // scalarize it now.
1189 if (DemandedElts == 1) {
1190 switch (II->getIntrinsicID()) {
1191 default: break;
1192 case Intrinsic::x86_sse_sub_ss:
1193 case Intrinsic::x86_sse_mul_ss:
1194 case Intrinsic::x86_sse2_sub_sd:
1195 case Intrinsic::x86_sse2_mul_sd:
1196 // TODO: Lower MIN/MAX/ABS/etc
Gabor Greif79430172010-06-24 12:35:13 +00001197 Value *LHS = II->getArgOperand(0);
1198 Value *RHS = II->getArgOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +00001199 // Extract the element as scalars.
Craig Topper4c947752012-12-22 18:09:02 +00001200 LHS = InsertNewInstWith(ExtractElementInst::Create(LHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001201 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Eli Friedman6efb64e2011-05-19 01:20:42 +00001202 RHS = InsertNewInstWith(ExtractElementInst::Create(RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001203 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Craig Topper4c947752012-12-22 18:09:02 +00001204
Chris Lattner7e044912010-01-04 07:17:19 +00001205 switch (II->getIntrinsicID()) {
1206 default: llvm_unreachable("Case stmts out of sync!");
1207 case Intrinsic::x86_sse_sub_ss:
1208 case Intrinsic::x86_sse2_sub_sd:
Eli Friedman6efb64e2011-05-19 01:20:42 +00001209 TmpV = InsertNewInstWith(BinaryOperator::CreateFSub(LHS, RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001210 II->getName()), *II);
1211 break;
1212 case Intrinsic::x86_sse_mul_ss:
1213 case Intrinsic::x86_sse2_mul_sd:
Eli Friedman6efb64e2011-05-19 01:20:42 +00001214 TmpV = InsertNewInstWith(BinaryOperator::CreateFMul(LHS, RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001215 II->getName()), *II);
1216 break;
1217 }
Craig Topper4c947752012-12-22 18:09:02 +00001218
Chris Lattner7e044912010-01-04 07:17:19 +00001219 Instruction *New =
1220 InsertElementInst::Create(
1221 UndefValue::get(II->getType()), TmpV,
1222 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U, false),
1223 II->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +00001224 InsertNewInstWith(New, *II);
Chris Lattner7e044912010-01-04 07:17:19 +00001225 return New;
Craig Topper4c947752012-12-22 18:09:02 +00001226 }
Chris Lattner7e044912010-01-04 07:17:19 +00001227 }
Craig Topper4c947752012-12-22 18:09:02 +00001228
Chris Lattner7e044912010-01-04 07:17:19 +00001229 // Output elements are undefined if both are undefined. Consider things
1230 // like undef&0. The result is known zero, not undef.
1231 UndefElts &= UndefElts2;
1232 break;
1233 }
1234 break;
1235 }
1236 }
Craig Topperf40110f2014-04-25 05:29:35 +00001237 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001238}