blob: c26766a95ac4fdd8672cd4242b126e7c34573ee5 [file] [log] [blame]
Chris Lattner7e044912010-01-04 07:17:19 +00001//===- InstCombineSimplifyDemanded.cpp ------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains logic for simplifying instructions based on information
11// about how they are used.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner7e044912010-01-04 07:17:19 +000015#include "InstCombine.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/DataLayout.h"
17#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000018#include "llvm/IR/PatternMatch.h"
Chris Lattner7e044912010-01-04 07:17:19 +000019
20using namespace llvm;
Shuxin Yang63e999e2012-12-04 00:04:54 +000021using namespace llvm::PatternMatch;
Chris Lattner7e044912010-01-04 07:17:19 +000022
Chandler Carruth964daaa2014-04-22 02:55:47 +000023#define DEBUG_TYPE "instcombine"
24
Craig Topper4c947752012-12-22 18:09:02 +000025/// ShrinkDemandedConstant - Check to see if the specified operand of the
Chris Lattner7e044912010-01-04 07:17:19 +000026/// specified instruction is a constant integer. If so, check to see if there
27/// are any bits set in the constant that are not demanded. If so, shrink the
28/// constant and return true.
Craig Topper4c947752012-12-22 18:09:02 +000029static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
Chris Lattner7e044912010-01-04 07:17:19 +000030 APInt Demanded) {
31 assert(I && "No instruction?");
32 assert(OpNo < I->getNumOperands() && "Operand index too large");
33
34 // If the operand is not a constant integer, nothing to do.
35 ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo));
36 if (!OpC) return false;
37
38 // If there are no bits set that aren't demanded, nothing to do.
Jay Foad583abbc2010-12-07 08:25:19 +000039 Demanded = Demanded.zextOrTrunc(OpC->getValue().getBitWidth());
Chris Lattner7e044912010-01-04 07:17:19 +000040 if ((~Demanded & OpC->getValue()) == 0)
41 return false;
42
43 // This instruction is producing bits that are not demanded. Shrink the RHS.
44 Demanded &= OpC->getValue();
45 I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Demanded));
David Majnemer42b83a52014-08-22 07:56:32 +000046
47 // If 'nsw' is set and the constant is negative, removing *any* bits from the
48 // constant could make overflow occur. Remove 'nsw' from the instruction in
49 // this case.
50 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(I))
51 if (OBO->hasNoSignedWrap() && OpC->getValue().isNegative())
52 cast<BinaryOperator>(OBO)->setHasNoSignedWrap(false);
53
Chris Lattner7e044912010-01-04 07:17:19 +000054 return true;
55}
56
57
58
59/// SimplifyDemandedInstructionBits - Inst is an integer instruction that
60/// SimplifyDemandedBits knows about. See if the instruction has any
61/// properties that allow us to simplify its operands.
62bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
63 unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
64 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
65 APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
Craig Topper4c947752012-12-22 18:09:02 +000066
67 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +000068 KnownZero, KnownOne, 0);
Craig Topperf40110f2014-04-25 05:29:35 +000069 if (!V) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000070 if (V == &Inst) return true;
71 ReplaceInstUsesWith(Inst, V);
72 return true;
73}
74
75/// SimplifyDemandedBits - This form of SimplifyDemandedBits simplifies the
76/// specified instruction operand if possible, updating it in place. It returns
77/// true if it made any change and false otherwise.
Craig Topper4c947752012-12-22 18:09:02 +000078bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +000079 APInt &KnownZero, APInt &KnownOne,
80 unsigned Depth) {
81 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask,
82 KnownZero, KnownOne, Depth);
Craig Topperf40110f2014-04-25 05:29:35 +000083 if (!NewVal) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000084 U = NewVal;
85 return true;
86}
87
88
89/// SimplifyDemandedUseBits - This function attempts to replace V with a simpler
90/// value based on the demanded bits. When this function is called, it is known
91/// that only the bits set in DemandedMask of the result of V are ever used
92/// downstream. Consequently, depending on the mask and V, it may be possible
93/// to replace V with a constant or one of its operands. In such cases, this
94/// function does the replacement and returns true. In all other cases, it
95/// returns false after analyzing the expression and setting KnownOne and known
96/// to be one in the expression. KnownZero contains all the bits that are known
97/// to be zero in the expression. These are provided to potentially allow the
98/// caller (which might recursively be SimplifyDemandedBits itself) to simplify
Craig Topper4c947752012-12-22 18:09:02 +000099/// the expression. KnownOne and KnownZero always follow the invariant that
Chris Lattner7e044912010-01-04 07:17:19 +0000100/// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that
101/// the bits in KnownOne and KnownZero may only be accurate for those bits set
102/// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
103/// and KnownOne must all be the same.
104///
105/// This returns null if it did not change anything and it permits no
106/// simplification. This returns V itself if it did some simplification of V's
107/// operands based on the information about what bits are demanded. This returns
108/// some other non-null value if it found out that V is equal to another value
109/// in the context where the specified bits are demanded, but not for all users.
110Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
111 APInt &KnownZero, APInt &KnownOne,
112 unsigned Depth) {
Craig Toppere73658d2014-04-28 04:05:08 +0000113 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000114 assert(Depth <= 6 && "Limit Search Depth");
115 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000116 Type *VTy = V->getType();
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000117 assert((DL || !VTy->isPointerTy()) &&
Chris Lattner7e044912010-01-04 07:17:19 +0000118 "SimplifyDemandedBits needs to know bit widths!");
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000119 assert((!DL || DL->getTypeSizeInBits(VTy->getScalarType()) == BitWidth) &&
Duncan Sands9dff9be2010-02-15 16:12:20 +0000120 (!VTy->isIntOrIntVectorTy() ||
Chris Lattner7e044912010-01-04 07:17:19 +0000121 VTy->getScalarSizeInBits() == BitWidth) &&
122 KnownZero.getBitWidth() == BitWidth &&
123 KnownOne.getBitWidth() == BitWidth &&
124 "Value *V, DemandedMask, KnownZero and KnownOne "
125 "must have same BitWidth");
126 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
127 // We know all of the bits for a constant!
128 KnownOne = CI->getValue() & DemandedMask;
129 KnownZero = ~KnownOne & DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000130 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000131 }
132 if (isa<ConstantPointerNull>(V)) {
133 // We know all of the bits for a constant!
Jay Foad25a5e4c2010-12-01 08:53:58 +0000134 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000135 KnownZero = DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000136 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000137 }
138
Jay Foad25a5e4c2010-12-01 08:53:58 +0000139 KnownZero.clearAllBits();
140 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000141 if (DemandedMask == 0) { // Not demanding any bits from V.
142 if (isa<UndefValue>(V))
Craig Topperf40110f2014-04-25 05:29:35 +0000143 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000144 return UndefValue::get(VTy);
145 }
Craig Topper4c947752012-12-22 18:09:02 +0000146
Chris Lattner7e044912010-01-04 07:17:19 +0000147 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000148 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000149
Chris Lattner7e044912010-01-04 07:17:19 +0000150 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000151 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +0000152
153 Instruction *I = dyn_cast<Instruction>(V);
154 if (!I) {
Jay Foada0653a32014-05-14 21:14:37 +0000155 computeKnownBits(V, KnownZero, KnownOne, Depth);
Craig Topperf40110f2014-04-25 05:29:35 +0000156 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000157 }
158
159 // If there are multiple uses of this value and we aren't at the root, then
160 // we can't do any simplifications of the operands, because DemandedMask
161 // only reflects the bits demanded by *one* of the users.
162 if (Depth != 0 && !I->hasOneUse()) {
163 // Despite the fact that we can't simplify this instruction in all User's
164 // context, we can at least compute the knownzero/knownone bits, and we can
165 // do simplifications that apply to *just* the one user if we know that
166 // this instruction has a simpler value in that context.
167 if (I->getOpcode() == Instruction::And) {
168 // If either the LHS or the RHS are Zero, the result is zero.
Jay Foada0653a32014-05-14 21:14:37 +0000169 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
170 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
Craig Topper4c947752012-12-22 18:09:02 +0000171
Chris Lattner7e044912010-01-04 07:17:19 +0000172 // If all of the demanded bits are known 1 on one side, return the other.
173 // These bits cannot contribute to the result of the 'and' in this
174 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000175 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000176 (DemandedMask & ~LHSKnownZero))
177 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000178 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000179 (DemandedMask & ~RHSKnownZero))
180 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000181
Chris Lattner7e044912010-01-04 07:17:19 +0000182 // If all of the demanded bits in the inputs are known zeros, return zero.
183 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
184 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000185
Chris Lattner7e044912010-01-04 07:17:19 +0000186 } else if (I->getOpcode() == Instruction::Or) {
187 // We can simplify (X|Y) -> X or Y in the user's context if we know that
188 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000189
Chris Lattner7e044912010-01-04 07:17:19 +0000190 // If either the LHS or the RHS are One, the result is One.
Jay Foada0653a32014-05-14 21:14:37 +0000191 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
192 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
Craig Topper4c947752012-12-22 18:09:02 +0000193
Chris Lattner7e044912010-01-04 07:17:19 +0000194 // If all of the demanded bits are known zero on one side, return the
195 // other. These bits cannot contribute to the result of the 'or' in this
196 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000197 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000198 (DemandedMask & ~LHSKnownOne))
199 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000200 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000201 (DemandedMask & ~RHSKnownOne))
202 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000203
Chris Lattner7e044912010-01-04 07:17:19 +0000204 // If all of the potentially set bits on one side are known to be set on
205 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000206 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000207 (DemandedMask & (~RHSKnownZero)))
208 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000209 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000210 (DemandedMask & (~LHSKnownZero)))
211 return I->getOperand(1);
Shuxin Yang73285932012-12-04 22:15:32 +0000212 } else if (I->getOpcode() == Instruction::Xor) {
213 // We can simplify (X^Y) -> X or Y in the user's context if we know that
214 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000215
Jay Foada0653a32014-05-14 21:14:37 +0000216 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
217 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
Craig Topper4c947752012-12-22 18:09:02 +0000218
Shuxin Yang73285932012-12-04 22:15:32 +0000219 // If all of the demanded bits are known zero on one side, return the
Craig Topper4c947752012-12-22 18:09:02 +0000220 // other.
Shuxin Yang73285932012-12-04 22:15:32 +0000221 if ((DemandedMask & RHSKnownZero) == DemandedMask)
222 return I->getOperand(0);
223 if ((DemandedMask & LHSKnownZero) == DemandedMask)
224 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000225 }
Shuxin Yang73285932012-12-04 22:15:32 +0000226
Chris Lattner7e044912010-01-04 07:17:19 +0000227 // Compute the KnownZero/KnownOne bits to simplify things downstream.
Jay Foada0653a32014-05-14 21:14:37 +0000228 computeKnownBits(I, KnownZero, KnownOne, Depth);
Craig Topperf40110f2014-04-25 05:29:35 +0000229 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000230 }
Craig Topper4c947752012-12-22 18:09:02 +0000231
Chris Lattner7e044912010-01-04 07:17:19 +0000232 // If this is the root being simplified, allow it to have multiple uses,
233 // just set the DemandedMask to all bits so that we can try to simplify the
234 // operands. This allows visitTruncInst (for example) to simplify the
235 // operand of a trunc without duplicating all the logic below.
236 if (Depth == 0 && !V->hasOneUse())
237 DemandedMask = APInt::getAllOnesValue(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000238
Chris Lattner7e044912010-01-04 07:17:19 +0000239 switch (I->getOpcode()) {
240 default:
Jay Foada0653a32014-05-14 21:14:37 +0000241 computeKnownBits(I, KnownZero, KnownOne, Depth);
Chris Lattner7e044912010-01-04 07:17:19 +0000242 break;
243 case Instruction::And:
244 // If either the LHS or the RHS are Zero, the result is zero.
245 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
246 RHSKnownZero, RHSKnownOne, Depth+1) ||
247 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
248 LHSKnownZero, LHSKnownOne, Depth+1))
249 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000250 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
251 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000252
253 // If all of the demanded bits are known 1 on one side, return the other.
254 // These bits cannot contribute to the result of the 'and'.
Craig Topper4c947752012-12-22 18:09:02 +0000255 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000256 (DemandedMask & ~LHSKnownZero))
257 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000258 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000259 (DemandedMask & ~RHSKnownZero))
260 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000261
Chris Lattner7e044912010-01-04 07:17:19 +0000262 // If all of the demanded bits in the inputs are known zeros, return zero.
263 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
264 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000265
Chris Lattner7e044912010-01-04 07:17:19 +0000266 // If the RHS is a constant, see if we can simplify it.
267 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
268 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000269
Chris Lattner7e044912010-01-04 07:17:19 +0000270 // Output known-1 bits are only known if set in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000271 KnownOne = RHSKnownOne & LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000272 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000273 KnownZero = RHSKnownZero | LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000274 break;
275 case Instruction::Or:
276 // If either the LHS or the RHS are One, the result is One.
Craig Topper4c947752012-12-22 18:09:02 +0000277 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +0000278 RHSKnownZero, RHSKnownOne, Depth+1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000279 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne,
Chris Lattner7e044912010-01-04 07:17:19 +0000280 LHSKnownZero, LHSKnownOne, Depth+1))
281 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000282 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
283 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
284
Chris Lattner7e044912010-01-04 07:17:19 +0000285 // If all of the demanded bits are known zero on one side, return the other.
286 // These bits cannot contribute to the result of the 'or'.
Craig Topper4c947752012-12-22 18:09:02 +0000287 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000288 (DemandedMask & ~LHSKnownOne))
289 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000290 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000291 (DemandedMask & ~RHSKnownOne))
292 return I->getOperand(1);
293
294 // If all of the potentially set bits on one side are known to be set on
295 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000296 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000297 (DemandedMask & (~RHSKnownZero)))
298 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000299 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000300 (DemandedMask & (~LHSKnownZero)))
301 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000302
Chris Lattner7e044912010-01-04 07:17:19 +0000303 // If the RHS is a constant, see if we can simplify it.
304 if (ShrinkDemandedConstant(I, 1, DemandedMask))
305 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000306
Chris Lattner7e044912010-01-04 07:17:19 +0000307 // Output known-0 bits are only known if clear in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000308 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000309 // Output known-1 are known to be set if set in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000310 KnownOne = RHSKnownOne | LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000311 break;
312 case Instruction::Xor: {
313 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
314 RHSKnownZero, RHSKnownOne, Depth+1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000315 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +0000316 LHSKnownZero, LHSKnownOne, Depth+1))
317 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000318 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
319 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
320
Chris Lattner7e044912010-01-04 07:17:19 +0000321 // If all of the demanded bits are known zero on one side, return the other.
322 // These bits cannot contribute to the result of the 'xor'.
323 if ((DemandedMask & RHSKnownZero) == DemandedMask)
324 return I->getOperand(0);
325 if ((DemandedMask & LHSKnownZero) == DemandedMask)
326 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000327
Chris Lattner7e044912010-01-04 07:17:19 +0000328 // If all of the demanded bits are known to be zero on one side or the
329 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000330 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Chris Lattner7e044912010-01-04 07:17:19 +0000331 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
Craig Topper4c947752012-12-22 18:09:02 +0000332 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000333 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
334 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000335 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000336 }
Craig Topper4c947752012-12-22 18:09:02 +0000337
Chris Lattner7e044912010-01-04 07:17:19 +0000338 // If all of the demanded bits on one side are known, and all of the set
339 // bits on that side are also known to be set on the other side, turn this
340 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000341 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper4c947752012-12-22 18:09:02 +0000342 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
Chris Lattner7e044912010-01-04 07:17:19 +0000343 // all known
344 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
345 Constant *AndC = Constant::getIntegerValue(VTy,
346 ~RHSKnownOne & DemandedMask);
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000347 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000348 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000349 }
350 }
Craig Topper4c947752012-12-22 18:09:02 +0000351
Chris Lattner7e044912010-01-04 07:17:19 +0000352 // If the RHS is a constant, see if we can simplify it.
353 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
354 if (ShrinkDemandedConstant(I, 1, DemandedMask))
355 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000356
Chris Lattner7e044912010-01-04 07:17:19 +0000357 // If our LHS is an 'and' and if it has one use, and if any of the bits we
358 // are flipping are known to be set, then the xor is just resetting those
359 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
360 // simplifying both of them.
361 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
362 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
363 isa<ConstantInt>(I->getOperand(1)) &&
364 isa<ConstantInt>(LHSInst->getOperand(1)) &&
365 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
366 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
367 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
368 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000369
Chris Lattner7e044912010-01-04 07:17:19 +0000370 Constant *AndC =
371 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000372 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000373 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000374
Chris Lattner7e044912010-01-04 07:17:19 +0000375 Constant *XorC =
376 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000377 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000378 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000379 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000380
381 // Output known-0 bits are known if clear or set in both the LHS & RHS.
382 KnownZero= (RHSKnownZero & LHSKnownZero) | (RHSKnownOne & LHSKnownOne);
383 // Output known-1 are known to be set if set in only one of the LHS, RHS.
384 KnownOne = (RHSKnownZero & LHSKnownOne) | (RHSKnownOne & LHSKnownZero);
Chris Lattner7e044912010-01-04 07:17:19 +0000385 break;
386 }
387 case Instruction::Select:
388 if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask,
389 RHSKnownZero, RHSKnownOne, Depth+1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000390 SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +0000391 LHSKnownZero, LHSKnownOne, Depth+1))
392 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000393 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
394 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
395
Chris Lattner7e044912010-01-04 07:17:19 +0000396 // If the operands are constants, see if we can simplify them.
397 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
398 ShrinkDemandedConstant(I, 2, DemandedMask))
399 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000400
Chris Lattner7e044912010-01-04 07:17:19 +0000401 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000402 KnownOne = RHSKnownOne & LHSKnownOne;
403 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000404 break;
405 case Instruction::Trunc: {
406 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000407 DemandedMask = DemandedMask.zext(truncBf);
408 KnownZero = KnownZero.zext(truncBf);
409 KnownOne = KnownOne.zext(truncBf);
Craig Topper4c947752012-12-22 18:09:02 +0000410 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000411 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000412 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000413 DemandedMask = DemandedMask.trunc(BitWidth);
414 KnownZero = KnownZero.trunc(BitWidth);
415 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000416 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000417 break;
418 }
419 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000420 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000421 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000422
Chris Lattner229907c2011-07-18 04:54:35 +0000423 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
424 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000425 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
426 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
427 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000428 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000429 } else
430 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000431 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000432 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000433 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000434 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000435
436 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000437 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000438 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000439 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000440 break;
441 case Instruction::ZExt: {
442 // Compute the bits in the result that are not present in the input.
443 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000444
Jay Foad583abbc2010-12-07 08:25:19 +0000445 DemandedMask = DemandedMask.trunc(SrcBitWidth);
446 KnownZero = KnownZero.trunc(SrcBitWidth);
447 KnownOne = KnownOne.trunc(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000448 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000449 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000450 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000451 DemandedMask = DemandedMask.zext(BitWidth);
452 KnownZero = KnownZero.zext(BitWidth);
453 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000454 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000455 // The top bits are known to be zero.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000456 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000457 break;
458 }
459 case Instruction::SExt: {
460 // Compute the bits in the result that are not present in the input.
461 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000462
463 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000464 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
465
466 APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
467 // If any of the sign extended bits are demanded, we know that the sign
468 // bit is demanded.
469 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000470 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000471
Jay Foad583abbc2010-12-07 08:25:19 +0000472 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
473 KnownZero = KnownZero.trunc(SrcBitWidth);
474 KnownOne = KnownOne.trunc(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000475 if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000476 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000477 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000478 InputDemandedBits = InputDemandedBits.zext(BitWidth);
479 KnownZero = KnownZero.zext(BitWidth);
480 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000481 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
482
Chris Lattner7e044912010-01-04 07:17:19 +0000483 // If the sign bit of the input is known set or clear, then we know the
484 // top bits of the result.
485
486 // If the input sign bit is known zero, or if the NewBits are not demanded
487 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000488 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000489 // Convert to ZExt cast
490 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000491 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000492 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
493 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000494 }
495 break;
496 }
497 case Instruction::Add: {
498 // Figure out what the input bits are. If the top bits of the and result
499 // are not demanded, then the add doesn't demand them from its input
500 // either.
501 unsigned NLZ = DemandedMask.countLeadingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000502
Chris Lattner7e044912010-01-04 07:17:19 +0000503 // If there is a constant on the RHS, there are a variety of xformations
504 // we can do.
505 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
506 // If null, this should be simplified elsewhere. Some of the xforms here
507 // won't work if the RHS is zero.
508 if (RHS->isZero())
509 break;
Craig Topper4c947752012-12-22 18:09:02 +0000510
Chris Lattner7e044912010-01-04 07:17:19 +0000511 // If the top bit of the output is demanded, demand everything from the
512 // input. Otherwise, we demand all the input bits except NLZ top bits.
513 APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
514
515 // Find information about known zero/one bits in the input.
Craig Topper4c947752012-12-22 18:09:02 +0000516 if (SimplifyDemandedBits(I->getOperandUse(0), InDemandedBits,
Chris Lattner7e044912010-01-04 07:17:19 +0000517 LHSKnownZero, LHSKnownOne, Depth+1))
518 return I;
519
520 // If the RHS of the add has bits set that can't affect the input, reduce
521 // the constant.
522 if (ShrinkDemandedConstant(I, 1, InDemandedBits))
523 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000524
Chris Lattner7e044912010-01-04 07:17:19 +0000525 // Avoid excess work.
526 if (LHSKnownZero == 0 && LHSKnownOne == 0)
527 break;
Craig Topper4c947752012-12-22 18:09:02 +0000528
Chris Lattner7e044912010-01-04 07:17:19 +0000529 // Turn it into OR if input bits are zero.
530 if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
531 Instruction *Or =
532 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
533 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000534 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000535 }
Craig Topper4c947752012-12-22 18:09:02 +0000536
Chris Lattner7e044912010-01-04 07:17:19 +0000537 // We can say something about the output known-zero and known-one bits,
538 // depending on potential carries from the input constant and the
539 // unknowns. For example if the LHS is known to have at most the 0x0F0F0
540 // bits set and the RHS constant is 0x01001, then we know we have a known
541 // one mask of 0x00001 and a known zero mask of 0xE0F0E.
Craig Topper4c947752012-12-22 18:09:02 +0000542
Chris Lattner7e044912010-01-04 07:17:19 +0000543 // To compute this, we first compute the potential carry bits. These are
544 // the bits which may be modified. I'm not aware of a better way to do
545 // this scan.
546 const APInt &RHSVal = RHS->getValue();
547 APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
Craig Topper4c947752012-12-22 18:09:02 +0000548
Chris Lattner7e044912010-01-04 07:17:19 +0000549 // Now that we know which bits have carries, compute the known-1/0 sets.
Craig Topper4c947752012-12-22 18:09:02 +0000550
Chris Lattner7e044912010-01-04 07:17:19 +0000551 // Bits are known one if they are known zero in one operand and one in the
552 // other, and there is no input carry.
Craig Topper4c947752012-12-22 18:09:02 +0000553 KnownOne = ((LHSKnownZero & RHSVal) |
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000554 (LHSKnownOne & ~RHSVal)) & ~CarryBits;
Craig Topper4c947752012-12-22 18:09:02 +0000555
Chris Lattner7e044912010-01-04 07:17:19 +0000556 // Bits are known zero if they are known zero in both operands and there
557 // is no input carry.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000558 KnownZero = LHSKnownZero & ~RHSVal & ~CarryBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000559 } else {
560 // If the high-bits of this ADD are not demanded, then it does not demand
561 // the high bits of its LHS or RHS.
562 if (DemandedMask[BitWidth-1] == 0) {
563 // Right fill the mask of bits for this ADD to demand the most
564 // significant bit and all those below it.
565 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
566 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
567 LHSKnownZero, LHSKnownOne, Depth+1) ||
568 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
569 LHSKnownZero, LHSKnownOne, Depth+1))
570 return I;
571 }
572 }
573 break;
574 }
575 case Instruction::Sub:
576 // If the high-bits of this SUB are not demanded, then it does not demand
577 // the high bits of its LHS or RHS.
578 if (DemandedMask[BitWidth-1] == 0) {
579 // Right fill the mask of bits for this SUB to demand the most
580 // significant bit and all those below it.
581 uint32_t NLZ = DemandedMask.countLeadingZeros();
582 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
583 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
584 LHSKnownZero, LHSKnownOne, Depth+1) ||
585 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
586 LHSKnownZero, LHSKnownOne, Depth+1))
587 return I;
588 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000589
Jay Foada0653a32014-05-14 21:14:37 +0000590 // Otherwise just hand the sub off to computeKnownBits to fill in
Chris Lattner7e044912010-01-04 07:17:19 +0000591 // the known zeros and ones.
Jay Foada0653a32014-05-14 21:14:37 +0000592 computeKnownBits(V, KnownZero, KnownOne, Depth);
Benjamin Kramer010337c2011-12-24 17:31:38 +0000593
594 // Turn this into a xor if LHS is 2^n-1 and the remaining bits are known
595 // zero.
596 if (ConstantInt *C0 = dyn_cast<ConstantInt>(I->getOperand(0))) {
597 APInt I0 = C0->getValue();
598 if ((I0 + 1).isPowerOf2() && (I0 | KnownZero).isAllOnesValue()) {
599 Instruction *Xor = BinaryOperator::CreateXor(I->getOperand(1), C0);
600 return InsertNewInstWith(Xor, *I);
601 }
602 }
Chris Lattner7e044912010-01-04 07:17:19 +0000603 break;
604 case Instruction::Shl:
605 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000606 {
607 Value *VarX; ConstantInt *C1;
608 if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
609 Instruction *Shr = cast<Instruction>(I->getOperand(0));
610 Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
611 KnownZero, KnownOne);
612 if (R)
613 return R;
614 }
615 }
616
Chris Lattner768003c2011-02-10 05:09:34 +0000617 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000618 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000619
Chris Lattner768003c2011-02-10 05:09:34 +0000620 // If the shift is NUW/NSW, then it does demand the high bits.
621 ShlOperator *IOp = cast<ShlOperator>(I);
622 if (IOp->hasNoSignedWrap())
623 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
624 else if (IOp->hasNoUnsignedWrap())
625 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000626
627 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000628 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000629 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000630 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
631 KnownZero <<= ShiftAmt;
632 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000633 // low bits known zero.
634 if (ShiftAmt)
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000635 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000636 }
637 break;
638 case Instruction::LShr:
639 // For a logical shift right
640 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000641 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000642
Chris Lattner7e044912010-01-04 07:17:19 +0000643 // Unsigned shift right.
644 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000645
Chris Lattner768003c2011-02-10 05:09:34 +0000646 // If the shift is exact, then it does demand the low bits (and knows that
647 // they are zero).
648 if (cast<LShrOperator>(I)->isExact())
649 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000650
Chris Lattner7e044912010-01-04 07:17:19 +0000651 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000652 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000653 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000654 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
655 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
656 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000657 if (ShiftAmt) {
658 // Compute the new bits that are at the top now.
659 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000660 KnownZero |= HighBits; // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000661 }
662 }
663 break;
664 case Instruction::AShr:
665 // If this is an arithmetic shift right and only the low-bit is set, we can
666 // always convert this into a logical shr, even if the shift amount is
667 // variable. The low bit of the shift cannot be an input sign bit unless
668 // the shift amount is >= the size of the datatype, which is undefined.
669 if (DemandedMask == 1) {
670 // Perform the logical shift right.
671 Instruction *NewVal = BinaryOperator::CreateLShr(
672 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000673 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000674 }
Chris Lattner7e044912010-01-04 07:17:19 +0000675
676 // If the sign bit is the only bit demanded by this ashr, then there is no
677 // need to do it, the shift doesn't change the high bit.
678 if (DemandedMask.isSignBit())
679 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000680
Chris Lattner7e044912010-01-04 07:17:19 +0000681 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000682 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000683
Chris Lattner7e044912010-01-04 07:17:19 +0000684 // Signed shift right.
685 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
686 // If any of the "high bits" are demanded, we should set the sign bit as
687 // demanded.
688 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000689 DemandedMaskIn.setBit(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000690
Chris Lattner768003c2011-02-10 05:09:34 +0000691 // If the shift is exact, then it does demand the low bits (and knows that
692 // they are zero).
693 if (cast<AShrOperator>(I)->isExact())
694 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000695
Chris Lattner7e044912010-01-04 07:17:19 +0000696 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000697 KnownZero, KnownOne, Depth+1))
Chris Lattner7e044912010-01-04 07:17:19 +0000698 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000699 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000700 // Compute the new bits that are at the top now.
701 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000702 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
703 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000704
Chris Lattner7e044912010-01-04 07:17:19 +0000705 // Handle the sign bits.
706 APInt SignBit(APInt::getSignBit(BitWidth));
707 // Adjust to where it is now in the mask.
Craig Topper4c947752012-12-22 18:09:02 +0000708 SignBit = APIntOps::lshr(SignBit, ShiftAmt);
709
Chris Lattner7e044912010-01-04 07:17:19 +0000710 // If the input sign bit is known to be zero, or if none of the top bits
711 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000712 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Chris Lattner7e044912010-01-04 07:17:19 +0000713 (HighBits & ~DemandedMask) == HighBits) {
714 // Perform the logical shift right.
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000715 BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
716 SA, I->getName());
717 NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000718 return InsertNewInstWith(NewVal, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000719 } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
720 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000721 }
722 }
723 break;
724 case Instruction::SRem:
725 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000726 // X % -1 demands all the bits because we don't want to introduce
727 // INT_MIN % -1 (== undef) by accident.
728 if (Rem->isAllOnesValue())
729 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000730 APInt RA = Rem->getValue().abs();
731 if (RA.isPowerOf2()) {
732 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
733 return I->getOperand(0);
734
735 APInt LowBits = RA - 1;
736 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
737 if (SimplifyDemandedBits(I->getOperandUse(0), Mask2,
738 LHSKnownZero, LHSKnownOne, Depth+1))
739 return I;
740
Duncan Sands3a48b872010-01-28 17:22:42 +0000741 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000742 KnownZero = LHSKnownZero & LowBits;
743 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000744
Duncan Sands3a48b872010-01-28 17:22:42 +0000745 // If LHS is non-negative or has all low bits zero, then the upper bits
746 // are all zero.
747 if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
748 KnownZero |= ~LowBits;
749
750 // If LHS is negative and not all low bits are zero, then the upper bits
751 // are all one.
752 if (LHSKnownOne[BitWidth-1] && ((LHSKnownOne & LowBits) != 0))
753 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000754
Craig Topper4c947752012-12-22 18:09:02 +0000755 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000756 }
757 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000758
759 // The sign bit is the LHS's sign bit, except when the result of the
760 // remainder is zero.
761 if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
Nick Lewyckye4679792011-03-07 01:50:10 +0000762 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Jay Foada0653a32014-05-14 21:14:37 +0000763 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
Nick Lewyckye4679792011-03-07 01:50:10 +0000764 // If it's known zero, our sign bit is also zero.
765 if (LHSKnownZero.isNegative())
Benjamin Kramer21b972a2013-05-09 16:32:32 +0000766 KnownZero.setBit(KnownZero.getBitWidth() - 1);
Nick Lewyckye4679792011-03-07 01:50:10 +0000767 }
Chris Lattner7e044912010-01-04 07:17:19 +0000768 break;
769 case Instruction::URem: {
770 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
771 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
772 if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes,
773 KnownZero2, KnownOne2, Depth+1) ||
774 SimplifyDemandedBits(I->getOperandUse(1), AllOnes,
775 KnownZero2, KnownOne2, Depth+1))
776 return I;
777
778 unsigned Leaders = KnownZero2.countLeadingOnes();
779 Leaders = std::max(Leaders,
780 KnownZero2.countLeadingOnes());
781 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
782 break;
783 }
784 case Instruction::Call:
785 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
786 switch (II->getIntrinsicID()) {
787 default: break;
788 case Intrinsic::bswap: {
789 // If the only bits demanded come from one byte of the bswap result,
790 // just shift the input byte into position to eliminate the bswap.
791 unsigned NLZ = DemandedMask.countLeadingZeros();
792 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000793
Chris Lattner7e044912010-01-04 07:17:19 +0000794 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
795 // we need all the bits down to bit 8. Likewise, round NLZ. If we
796 // have 14 leading zeros, round to 8.
797 NLZ &= ~7;
798 NTZ &= ~7;
799 // If we need exactly one byte, we can do this transformation.
800 if (BitWidth-NLZ-NTZ == 8) {
801 unsigned ResultBit = NTZ;
802 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000803
Chris Lattner7e044912010-01-04 07:17:19 +0000804 // Replace this with either a left or right shift to get the byte into
805 // the right place.
806 Instruction *NewVal;
807 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000808 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000809 ConstantInt::get(I->getType(), InputBit-ResultBit));
810 else
Gabor Greif79430172010-06-24 12:35:13 +0000811 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000812 ConstantInt::get(I->getType(), ResultBit-InputBit));
813 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000814 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000815 }
Craig Topper4c947752012-12-22 18:09:02 +0000816
Chris Lattner7e044912010-01-04 07:17:19 +0000817 // TODO: Could compute known zero/one bits based on the input.
818 break;
819 }
Chad Rosierb3628842011-05-26 23:13:19 +0000820 case Intrinsic::x86_sse42_crc32_64_64:
Evan Chenge8d2e9e2011-05-20 00:54:37 +0000821 KnownZero = APInt::getHighBitsSet(64, 32);
Craig Topperf40110f2014-04-25 05:29:35 +0000822 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000823 }
824 }
Jay Foada0653a32014-05-14 21:14:37 +0000825 computeKnownBits(V, KnownZero, KnownOne, Depth);
Chris Lattner7e044912010-01-04 07:17:19 +0000826 break;
827 }
Craig Topper4c947752012-12-22 18:09:02 +0000828
Chris Lattner7e044912010-01-04 07:17:19 +0000829 // If the client is only demanding bits that we know, return the known
830 // constant.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000831 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
832 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000833 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000834}
835
Shuxin Yang63e999e2012-12-04 00:04:54 +0000836/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
837/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
838/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
839/// of "C2-C1".
840///
841/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
842/// ..., bn}, without considering the specific value X is holding.
843/// This transformation is legal iff one of following conditions is hold:
844/// 1) All the bit in S are 0, in this case E1 == E2.
845/// 2) We don't care those bits in S, per the input DemandedMask.
846/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
847/// rest bits.
848///
849/// Currently we only test condition 2).
850///
851/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
852/// not successful.
853Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
854 Instruction *Shl, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne) {
855
Benjamin Kramer010f1082013-08-30 14:35:35 +0000856 const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
857 const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
858 if (!ShlOp1 || !ShrOp1)
Craig Topperf40110f2014-04-25 05:29:35 +0000859 return nullptr; // Noop.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000860
861 Value *VarX = Shr->getOperand(0);
862 Type *Ty = VarX->getType();
863 unsigned BitWidth = Ty->getIntegerBitWidth();
864 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000865 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000866
867 unsigned ShlAmt = ShlOp1.getZExtValue();
868 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000869
870 KnownOne.clearAllBits();
871 KnownZero = APInt::getBitsSet(KnownZero.getBitWidth(), 0, ShlAmt-1);
872 KnownZero &= DemandedMask;
873
Benjamin Kramer010f1082013-08-30 14:35:35 +0000874 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
875 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000876
877 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
878 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
879 (BitMask1.ashr(ShrAmt) << ShlAmt);
880
881 if (ShrAmt <= ShlAmt) {
882 BitMask2 <<= (ShlAmt - ShrAmt);
883 } else {
884 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
885 BitMask2.ashr(ShrAmt - ShlAmt);
886 }
887
888 // Check if condition-2 (see the comment to this function) is satified.
889 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
890 if (ShrAmt == ShlAmt)
891 return VarX;
892
893 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000894 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000895
896 BinaryOperator *New;
897 if (ShrAmt < ShlAmt) {
898 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
899 New = BinaryOperator::CreateShl(VarX, Amt);
900 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
901 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
902 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
903 } else {
904 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000905 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
906 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000907 if (cast<BinaryOperator>(Shr)->isExact())
908 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000909 }
910
911 return InsertNewInstWith(New, *Shl);
912 }
913
Craig Topperf40110f2014-04-25 05:29:35 +0000914 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000915}
Chris Lattner7e044912010-01-04 07:17:19 +0000916
917/// SimplifyDemandedVectorElts - The specified value produces a vector with
918/// any number of elements. DemandedElts contains the set of elements that are
919/// actually used by the caller. This method analyzes which elements of the
920/// operand are undef and returns that information in UndefElts.
921///
922/// If the information about demanded elements can be used to simplify the
923/// operation, the operation is simplified, then the resultant value is
924/// returned. This returns null if no change was made.
925Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000926 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000927 unsigned Depth) {
928 unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
929 APInt EltMask(APInt::getAllOnesValue(VWidth));
930 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
931
932 if (isa<UndefValue>(V)) {
933 // If the entire vector is undefined, just return this info.
934 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000935 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000936 }
Craig Topper4c947752012-12-22 18:09:02 +0000937
Chris Lattnerb22423c2010-02-08 23:56:03 +0000938 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000939 UndefElts = EltMask;
940 return UndefValue::get(V->getType());
941 }
942
943 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000944
Chris Lattner67058832012-01-25 06:48:06 +0000945 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
946 if (Constant *C = dyn_cast<Constant>(V)) {
947 // Check if this is identity. If so, return 0 since we are not simplifying
948 // anything.
949 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000950 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000951
Chris Lattner229907c2011-07-18 04:54:35 +0000952 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000953 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000954
Chris Lattner67058832012-01-25 06:48:06 +0000955 SmallVector<Constant*, 16> Elts;
956 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000957 if (!DemandedElts[i]) { // If not demanded, set to undef.
958 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000959 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +0000960 continue;
961 }
Craig Topper4c947752012-12-22 18:09:02 +0000962
Chris Lattner67058832012-01-25 06:48:06 +0000963 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000964 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000965
Chris Lattner67058832012-01-25 06:48:06 +0000966 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000967 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000968 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +0000969 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +0000970 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +0000971 }
Chris Lattner67058832012-01-25 06:48:06 +0000972 }
Craig Topper4c947752012-12-22 18:09:02 +0000973
Chris Lattner7e044912010-01-04 07:17:19 +0000974 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +0000975 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +0000976 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000977 }
Craig Topper4c947752012-12-22 18:09:02 +0000978
Chris Lattner7e044912010-01-04 07:17:19 +0000979 // Limit search depth.
980 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +0000981 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000982
Stuart Hastings5bd18b62011-05-17 22:13:31 +0000983 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +0000984 // simplification conservatively assuming that all elements
985 // are needed.
986 if (!V->hasOneUse()) {
987 // Quit if we find multiple users of a non-root value though.
988 // They'll be handled when it's their turn to be visited by
989 // the main instcombine process.
990 if (Depth != 0)
991 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +0000992 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000993
994 // Conservatively assume that all elements are needed.
995 DemandedElts = EltMask;
996 }
Craig Topper4c947752012-12-22 18:09:02 +0000997
Chris Lattner7e044912010-01-04 07:17:19 +0000998 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +0000999 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +00001000
Chris Lattner7e044912010-01-04 07:17:19 +00001001 bool MadeChange = false;
1002 APInt UndefElts2(VWidth, 0);
1003 Value *TmpV;
1004 switch (I->getOpcode()) {
1005 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001006
Chris Lattner7e044912010-01-04 07:17:19 +00001007 case Instruction::InsertElement: {
1008 // If this is a variable index, we don't know which element it overwrites.
1009 // demand exactly the same input as we produce.
1010 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +00001011 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +00001012 // Note that we can't propagate undef elt info, because we don't know
1013 // which elt is getting updated.
1014 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1015 UndefElts2, Depth+1);
1016 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1017 break;
1018 }
Craig Topper4c947752012-12-22 18:09:02 +00001019
Chris Lattner7e044912010-01-04 07:17:19 +00001020 // If this is inserting an element that isn't demanded, remove this
1021 // insertelement.
1022 unsigned IdxNo = Idx->getZExtValue();
1023 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1024 Worklist.Add(I);
1025 return I->getOperand(0);
1026 }
Craig Topper4c947752012-12-22 18:09:02 +00001027
Chris Lattner7e044912010-01-04 07:17:19 +00001028 // Otherwise, the element inserted overwrites whatever was there, so the
1029 // input demanded set is simpler than the output set.
1030 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001031 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001032 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
1033 UndefElts, Depth+1);
1034 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1035
1036 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001037 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001038 break;
1039 }
1040 case Instruction::ShuffleVector: {
1041 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
1042 uint64_t LHSVWidth =
1043 cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements();
1044 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1045 for (unsigned i = 0; i < VWidth; i++) {
1046 if (DemandedElts[i]) {
1047 unsigned MaskVal = Shuffle->getMaskValue(i);
1048 if (MaskVal != -1u) {
1049 assert(MaskVal < LHSVWidth * 2 &&
1050 "shufflevector mask index out of range!");
1051 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001052 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001053 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001054 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001055 }
1056 }
1057 }
1058
1059 APInt UndefElts4(LHSVWidth, 0);
1060 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
1061 UndefElts4, Depth+1);
1062 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1063
1064 APInt UndefElts3(LHSVWidth, 0);
1065 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
1066 UndefElts3, Depth+1);
1067 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1068
1069 bool NewUndefElts = false;
1070 for (unsigned i = 0; i < VWidth; i++) {
1071 unsigned MaskVal = Shuffle->getMaskValue(i);
1072 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001073 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001074 } else if (!DemandedElts[i]) {
1075 NewUndefElts = true;
1076 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001077 } else if (MaskVal < LHSVWidth) {
1078 if (UndefElts4[MaskVal]) {
1079 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001080 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001081 }
1082 } else {
1083 if (UndefElts3[MaskVal - LHSVWidth]) {
1084 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001085 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001086 }
1087 }
1088 }
1089
1090 if (NewUndefElts) {
1091 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001092 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001093 for (unsigned i = 0; i < VWidth; ++i) {
1094 if (UndefElts[i])
1095 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1096 else
1097 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1098 Shuffle->getMaskValue(i)));
1099 }
1100 I->setOperand(2, ConstantVector::get(Elts));
1101 MadeChange = true;
1102 }
1103 break;
1104 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001105 case Instruction::Select: {
1106 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1107 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1108 for (unsigned i = 0; i < VWidth; i++) {
1109 if (CV->getAggregateElement(i)->isNullValue())
1110 LeftDemanded.clearBit(i);
1111 else
1112 RightDemanded.clearBit(i);
1113 }
1114 }
1115
1116 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded,
1117 UndefElts, Depth+1);
1118 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1119
1120 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
1121 UndefElts2, Depth+1);
1122 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001123
Pete Cooperabc13af2012-07-26 23:10:24 +00001124 // Output elements are undefined if both are undefined.
1125 UndefElts &= UndefElts2;
1126 break;
1127 }
Chris Lattner7e044912010-01-04 07:17:19 +00001128 case Instruction::BitCast: {
1129 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001130 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001131 if (!VTy) break;
1132 unsigned InVWidth = VTy->getNumElements();
1133 APInt InputDemandedElts(InVWidth, 0);
1134 unsigned Ratio;
1135
1136 if (VWidth == InVWidth) {
1137 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1138 // elements as are demanded of us.
1139 Ratio = 1;
1140 InputDemandedElts = DemandedElts;
1141 } else if (VWidth > InVWidth) {
1142 // Untested so far.
1143 break;
Craig Topper4c947752012-12-22 18:09:02 +00001144
Chris Lattner7e044912010-01-04 07:17:19 +00001145 // If there are more elements in the result than there are in the source,
1146 // then an input element is live if any of the corresponding output
1147 // elements are live.
1148 Ratio = VWidth/InVWidth;
1149 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1150 if (DemandedElts[OutIdx])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001151 InputDemandedElts.setBit(OutIdx/Ratio);
Chris Lattner7e044912010-01-04 07:17:19 +00001152 }
1153 } else {
1154 // Untested so far.
1155 break;
Craig Topper4c947752012-12-22 18:09:02 +00001156
Chris Lattner7e044912010-01-04 07:17:19 +00001157 // If there are more elements in the source than there are in the result,
1158 // then an input element is live if the corresponding output element is
1159 // live.
1160 Ratio = InVWidth/VWidth;
1161 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1162 if (DemandedElts[InIdx/Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001163 InputDemandedElts.setBit(InIdx);
Chris Lattner7e044912010-01-04 07:17:19 +00001164 }
Craig Topper4c947752012-12-22 18:09:02 +00001165
Chris Lattner7e044912010-01-04 07:17:19 +00001166 // div/rem demand all inputs, because they don't want divide by zero.
1167 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
1168 UndefElts2, Depth+1);
1169 if (TmpV) {
1170 I->setOperand(0, TmpV);
1171 MadeChange = true;
1172 }
Craig Topper4c947752012-12-22 18:09:02 +00001173
Chris Lattner7e044912010-01-04 07:17:19 +00001174 UndefElts = UndefElts2;
1175 if (VWidth > InVWidth) {
1176 llvm_unreachable("Unimp");
1177 // If there are more elements in the result than there are in the source,
1178 // then an output element is undef if the corresponding input element is
1179 // undef.
1180 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1181 if (UndefElts2[OutIdx/Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001182 UndefElts.setBit(OutIdx);
Chris Lattner7e044912010-01-04 07:17:19 +00001183 } else if (VWidth < InVWidth) {
1184 llvm_unreachable("Unimp");
1185 // If there are more elements in the source than there are in the result,
1186 // then a result element is undef if all of the corresponding input
1187 // elements are undef.
1188 UndefElts = ~0ULL >> (64-VWidth); // Start out all undef.
1189 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1190 if (!UndefElts2[InIdx]) // Not undef?
Jay Foad25a5e4c2010-12-01 08:53:58 +00001191 UndefElts.clearBit(InIdx/Ratio); // Clear undef bit.
Chris Lattner7e044912010-01-04 07:17:19 +00001192 }
1193 break;
1194 }
1195 case Instruction::And:
1196 case Instruction::Or:
1197 case Instruction::Xor:
1198 case Instruction::Add:
1199 case Instruction::Sub:
1200 case Instruction::Mul:
1201 // div/rem demand all inputs, because they don't want divide by zero.
1202 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1203 UndefElts, Depth+1);
1204 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1205 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
1206 UndefElts2, Depth+1);
1207 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001208
Chris Lattner7e044912010-01-04 07:17:19 +00001209 // Output elements are undefined if both are undefined. Consider things
1210 // like undef&0. The result is known zero, not undef.
1211 UndefElts &= UndefElts2;
1212 break;
Pete Coopere807e452012-07-26 22:37:04 +00001213 case Instruction::FPTrunc:
1214 case Instruction::FPExt:
1215 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1216 UndefElts, Depth+1);
1217 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1218 break;
Craig Topper4c947752012-12-22 18:09:02 +00001219
Chris Lattner7e044912010-01-04 07:17:19 +00001220 case Instruction::Call: {
1221 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1222 if (!II) break;
1223 switch (II->getIntrinsicID()) {
1224 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001225
Chris Lattner7e044912010-01-04 07:17:19 +00001226 // Binary vector operations that work column-wise. A dest element is a
1227 // function of the corresponding input elements from the two inputs.
1228 case Intrinsic::x86_sse_sub_ss:
1229 case Intrinsic::x86_sse_mul_ss:
1230 case Intrinsic::x86_sse_min_ss:
1231 case Intrinsic::x86_sse_max_ss:
1232 case Intrinsic::x86_sse2_sub_sd:
1233 case Intrinsic::x86_sse2_mul_sd:
1234 case Intrinsic::x86_sse2_min_sd:
1235 case Intrinsic::x86_sse2_max_sd:
Gabor Greife23efee2010-06-28 16:45:00 +00001236 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
Eric Christopher7258dcd2010-04-16 23:37:20 +00001237 UndefElts, Depth+1);
Gabor Greife23efee2010-06-28 16:45:00 +00001238 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1239 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Eric Christopher7258dcd2010-04-16 23:37:20 +00001240 UndefElts2, Depth+1);
Gabor Greife23efee2010-06-28 16:45:00 +00001241 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001242
1243 // If only the low elt is demanded and this is a scalarizable intrinsic,
1244 // scalarize it now.
1245 if (DemandedElts == 1) {
1246 switch (II->getIntrinsicID()) {
1247 default: break;
1248 case Intrinsic::x86_sse_sub_ss:
1249 case Intrinsic::x86_sse_mul_ss:
1250 case Intrinsic::x86_sse2_sub_sd:
1251 case Intrinsic::x86_sse2_mul_sd:
1252 // TODO: Lower MIN/MAX/ABS/etc
Gabor Greif79430172010-06-24 12:35:13 +00001253 Value *LHS = II->getArgOperand(0);
1254 Value *RHS = II->getArgOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +00001255 // Extract the element as scalars.
Craig Topper4c947752012-12-22 18:09:02 +00001256 LHS = InsertNewInstWith(ExtractElementInst::Create(LHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001257 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Eli Friedman6efb64e2011-05-19 01:20:42 +00001258 RHS = InsertNewInstWith(ExtractElementInst::Create(RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001259 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Craig Topper4c947752012-12-22 18:09:02 +00001260
Chris Lattner7e044912010-01-04 07:17:19 +00001261 switch (II->getIntrinsicID()) {
1262 default: llvm_unreachable("Case stmts out of sync!");
1263 case Intrinsic::x86_sse_sub_ss:
1264 case Intrinsic::x86_sse2_sub_sd:
Eli Friedman6efb64e2011-05-19 01:20:42 +00001265 TmpV = InsertNewInstWith(BinaryOperator::CreateFSub(LHS, RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001266 II->getName()), *II);
1267 break;
1268 case Intrinsic::x86_sse_mul_ss:
1269 case Intrinsic::x86_sse2_mul_sd:
Eli Friedman6efb64e2011-05-19 01:20:42 +00001270 TmpV = InsertNewInstWith(BinaryOperator::CreateFMul(LHS, RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001271 II->getName()), *II);
1272 break;
1273 }
Craig Topper4c947752012-12-22 18:09:02 +00001274
Chris Lattner7e044912010-01-04 07:17:19 +00001275 Instruction *New =
1276 InsertElementInst::Create(
1277 UndefValue::get(II->getType()), TmpV,
1278 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U, false),
1279 II->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +00001280 InsertNewInstWith(New, *II);
Chris Lattner7e044912010-01-04 07:17:19 +00001281 return New;
Craig Topper4c947752012-12-22 18:09:02 +00001282 }
Chris Lattner7e044912010-01-04 07:17:19 +00001283 }
Craig Topper4c947752012-12-22 18:09:02 +00001284
Chris Lattner7e044912010-01-04 07:17:19 +00001285 // Output elements are undefined if both are undefined. Consider things
1286 // like undef&0. The result is known zero, not undef.
1287 UndefElts &= UndefElts2;
1288 break;
1289 }
1290 break;
1291 }
1292 }
Craig Topperf40110f2014-04-25 05:29:35 +00001293 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001294}