blob: adc433c4b70794b9007db06d79b955e3a339e358 [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"
James Molloy2b21a7c2015-05-20 18:41:25 +000016#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#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
Chris Lattner7e044912010-01-04 07:17:19 +000047 return true;
48}
49
50
51
52/// SimplifyDemandedInstructionBits - Inst is an integer instruction that
53/// SimplifyDemandedBits knows about. See if the instruction has any
54/// properties that allow us to simplify its operands.
55bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
56 unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
57 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
58 APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
Craig Topper4c947752012-12-22 18:09:02 +000059
Mehdi Aminia28d91d2015-03-10 02:37:25 +000060 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, KnownZero, KnownOne,
61 0, &Inst);
Craig Topperf40110f2014-04-25 05:29:35 +000062 if (!V) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000063 if (V == &Inst) return true;
Sanjay Patel4b198802016-02-01 22:23:39 +000064 replaceInstUsesWith(Inst, V);
Chris Lattner7e044912010-01-04 07:17:19 +000065 return true;
66}
67
68/// SimplifyDemandedBits - This form of SimplifyDemandedBits simplifies the
69/// specified instruction operand if possible, updating it in place. It returns
70/// true if it made any change and false otherwise.
Craig Topper4c947752012-12-22 18:09:02 +000071bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +000072 APInt &KnownZero, APInt &KnownOne,
73 unsigned Depth) {
David Majnemerfe58d132015-04-22 20:59:28 +000074 auto *UserI = dyn_cast<Instruction>(U.getUser());
75 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, KnownZero,
76 KnownOne, Depth, UserI);
Craig Topperf40110f2014-04-25 05:29:35 +000077 if (!NewVal) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000078 U = NewVal;
79 return true;
80}
81
82
83/// SimplifyDemandedUseBits - This function attempts to replace V with a simpler
84/// value based on the demanded bits. When this function is called, it is known
85/// that only the bits set in DemandedMask of the result of V are ever used
86/// downstream. Consequently, depending on the mask and V, it may be possible
87/// to replace V with a constant or one of its operands. In such cases, this
88/// function does the replacement and returns true. In all other cases, it
89/// returns false after analyzing the expression and setting KnownOne and known
90/// to be one in the expression. KnownZero contains all the bits that are known
91/// to be zero in the expression. These are provided to potentially allow the
92/// caller (which might recursively be SimplifyDemandedBits itself) to simplify
Craig Topper4c947752012-12-22 18:09:02 +000093/// the expression. KnownOne and KnownZero always follow the invariant that
Chris Lattner7e044912010-01-04 07:17:19 +000094/// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that
95/// the bits in KnownOne and KnownZero may only be accurate for those bits set
96/// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero
97/// and KnownOne must all be the same.
98///
99/// This returns null if it did not change anything and it permits no
100/// simplification. This returns V itself if it did some simplification of V's
101/// operands based on the information about what bits are demanded. This returns
102/// some other non-null value if it found out that V is equal to another value
103/// in the context where the specified bits are demanded, but not for all users.
104Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
105 APInt &KnownZero, APInt &KnownOne,
Hal Finkel60db0582014-09-07 18:57:58 +0000106 unsigned Depth,
107 Instruction *CxtI) {
Craig Toppere73658d2014-04-28 04:05:08 +0000108 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000109 assert(Depth <= 6 && "Limit Search Depth");
110 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000111 Type *VTy = V->getType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000112 assert(
113 (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
114 KnownZero.getBitWidth() == BitWidth &&
115 KnownOne.getBitWidth() == BitWidth &&
116 "Value *V, DemandedMask, KnownZero and KnownOne "
117 "must have same BitWidth");
Chris Lattner7e044912010-01-04 07:17:19 +0000118 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
119 // We know all of the bits for a constant!
120 KnownOne = CI->getValue() & DemandedMask;
121 KnownZero = ~KnownOne & DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000122 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000123 }
124 if (isa<ConstantPointerNull>(V)) {
125 // We know all of the bits for a constant!
Jay Foad25a5e4c2010-12-01 08:53:58 +0000126 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000127 KnownZero = DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000128 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000129 }
130
Jay Foad25a5e4c2010-12-01 08:53:58 +0000131 KnownZero.clearAllBits();
132 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000133 if (DemandedMask == 0) { // Not demanding any bits from V.
134 if (isa<UndefValue>(V))
Craig Topperf40110f2014-04-25 05:29:35 +0000135 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000136 return UndefValue::get(VTy);
137 }
Craig Topper4c947752012-12-22 18:09:02 +0000138
Chris Lattner7e044912010-01-04 07:17:19 +0000139 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000140 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000141
Chris Lattner7e044912010-01-04 07:17:19 +0000142 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000143 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +0000144
145 Instruction *I = dyn_cast<Instruction>(V);
146 if (!I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000147 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000148 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000149 }
150
151 // If there are multiple uses of this value and we aren't at the root, then
152 // we can't do any simplifications of the operands, because DemandedMask
153 // only reflects the bits demanded by *one* of the users.
154 if (Depth != 0 && !I->hasOneUse()) {
155 // Despite the fact that we can't simplify this instruction in all User's
156 // context, we can at least compute the knownzero/knownone bits, and we can
157 // do simplifications that apply to *just* the one user if we know that
158 // this instruction has a simpler value in that context.
159 if (I->getOpcode() == Instruction::And) {
160 // If either the LHS or the RHS are Zero, the result is zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000161 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000162 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000163 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000164 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000165
Chris Lattner7e044912010-01-04 07:17:19 +0000166 // If all of the demanded bits are known 1 on one side, return the other.
167 // These bits cannot contribute to the result of the 'and' in this
168 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000169 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000170 (DemandedMask & ~LHSKnownZero))
171 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000172 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000173 (DemandedMask & ~RHSKnownZero))
174 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000175
Chris Lattner7e044912010-01-04 07:17:19 +0000176 // If all of the demanded bits in the inputs are known zeros, return zero.
177 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
178 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000179
Chris Lattner7e044912010-01-04 07:17:19 +0000180 } else if (I->getOpcode() == Instruction::Or) {
181 // We can simplify (X|Y) -> X or Y in the user's context if we know that
182 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000183
Chris Lattner7e044912010-01-04 07:17:19 +0000184 // If either the LHS or the RHS are One, the result is One.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000185 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000186 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000187 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000188 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000189
Chris Lattner7e044912010-01-04 07:17:19 +0000190 // If all of the demanded bits are known zero on one side, return the
191 // other. These bits cannot contribute to the result of the 'or' in this
192 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000193 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000194 (DemandedMask & ~LHSKnownOne))
195 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000196 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000197 (DemandedMask & ~RHSKnownOne))
198 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000199
Chris Lattner7e044912010-01-04 07:17:19 +0000200 // If all of the potentially set bits on one side are known to be set on
201 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000202 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000203 (DemandedMask & (~RHSKnownZero)))
204 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000205 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000206 (DemandedMask & (~LHSKnownZero)))
207 return I->getOperand(1);
Shuxin Yang73285932012-12-04 22:15:32 +0000208 } else if (I->getOpcode() == Instruction::Xor) {
209 // We can simplify (X^Y) -> X or Y in the user's context if we know that
210 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000211
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000212 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000213 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000214 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000215 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000216
Shuxin Yang73285932012-12-04 22:15:32 +0000217 // If all of the demanded bits are known zero on one side, return the
Craig Topper4c947752012-12-22 18:09:02 +0000218 // other.
Shuxin Yang73285932012-12-04 22:15:32 +0000219 if ((DemandedMask & RHSKnownZero) == DemandedMask)
220 return I->getOperand(0);
221 if ((DemandedMask & LHSKnownZero) == DemandedMask)
222 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000223 }
Shuxin Yang73285932012-12-04 22:15:32 +0000224
Chris Lattner7e044912010-01-04 07:17:19 +0000225 // Compute the KnownZero/KnownOne bits to simplify things downstream.
Hal Finkel60db0582014-09-07 18:57:58 +0000226 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000227 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000228 }
Craig Topper4c947752012-12-22 18:09:02 +0000229
Chris Lattner7e044912010-01-04 07:17:19 +0000230 // If this is the root being simplified, allow it to have multiple uses,
231 // just set the DemandedMask to all bits so that we can try to simplify the
232 // operands. This allows visitTruncInst (for example) to simplify the
233 // operand of a trunc without duplicating all the logic below.
234 if (Depth == 0 && !V->hasOneUse())
235 DemandedMask = APInt::getAllOnesValue(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000236
Chris Lattner7e044912010-01-04 07:17:19 +0000237 switch (I->getOpcode()) {
238 default:
Hal Finkel60db0582014-09-07 18:57:58 +0000239 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000240 break;
241 case Instruction::And:
242 // If either the LHS or the RHS are Zero, the result is zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000243 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, RHSKnownZero,
244 RHSKnownOne, Depth + 1) ||
Chris Lattner7e044912010-01-04 07:17:19 +0000245 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000246 LHSKnownZero, LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000247 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000248 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
249 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000250
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000251 // If the client is only demanding bits that we know, return the known
252 // constant.
253 if ((DemandedMask & ((RHSKnownZero | LHSKnownZero)|
254 (RHSKnownOne & LHSKnownOne))) == DemandedMask)
255 return Constant::getIntegerValue(VTy, RHSKnownOne & LHSKnownOne);
256
Chris Lattner7e044912010-01-04 07:17:19 +0000257 // If all of the demanded bits are known 1 on one side, return the other.
258 // These bits cannot contribute to the result of the 'and'.
Craig Topper4c947752012-12-22 18:09:02 +0000259 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000260 (DemandedMask & ~LHSKnownZero))
261 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000262 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000263 (DemandedMask & ~RHSKnownZero))
264 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000265
Chris Lattner7e044912010-01-04 07:17:19 +0000266 // If all of the demanded bits in the inputs are known zeros, return zero.
267 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
268 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000269
Chris Lattner7e044912010-01-04 07:17:19 +0000270 // If the RHS is a constant, see if we can simplify it.
271 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
272 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000273
Chris Lattner7e044912010-01-04 07:17:19 +0000274 // Output known-1 bits are only known if set in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000275 KnownOne = RHSKnownOne & LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000276 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000277 KnownZero = RHSKnownZero | LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000278 break;
279 case Instruction::Or:
280 // If either the LHS or the RHS are One, the result is One.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000281 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, RHSKnownZero,
282 RHSKnownOne, Depth + 1) ||
Craig Topper4c947752012-12-22 18:09:02 +0000283 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000284 LHSKnownZero, LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000285 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000286 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
287 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
288
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000289 // If the client is only demanding bits that we know, return the known
290 // constant.
291 if ((DemandedMask & ((RHSKnownZero & LHSKnownZero)|
292 (RHSKnownOne | LHSKnownOne))) == DemandedMask)
293 return Constant::getIntegerValue(VTy, RHSKnownOne | LHSKnownOne);
294
Chris Lattner7e044912010-01-04 07:17:19 +0000295 // If all of the demanded bits are known zero on one side, return the other.
296 // These bits cannot contribute to the result of the 'or'.
Craig Topper4c947752012-12-22 18:09:02 +0000297 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000298 (DemandedMask & ~LHSKnownOne))
299 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000300 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000301 (DemandedMask & ~RHSKnownOne))
302 return I->getOperand(1);
303
304 // If all of the potentially set bits on one side are known to be set on
305 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000306 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000307 (DemandedMask & (~RHSKnownZero)))
308 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000309 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000310 (DemandedMask & (~LHSKnownZero)))
311 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000312
Chris Lattner7e044912010-01-04 07:17:19 +0000313 // If the RHS is a constant, see if we can simplify it.
314 if (ShrinkDemandedConstant(I, 1, DemandedMask))
315 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000316
Chris Lattner7e044912010-01-04 07:17:19 +0000317 // Output known-0 bits are only known if clear in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000318 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000319 // Output known-1 are known to be set if set in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000320 KnownOne = RHSKnownOne | LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000321 break;
322 case Instruction::Xor: {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000323 if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, RHSKnownZero,
324 RHSKnownOne, Depth + 1) ||
325 SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, LHSKnownZero,
326 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000327 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000328 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
329 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
330
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000331 // Output known-0 bits are known if clear or set in both the LHS & RHS.
332 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
333 (RHSKnownOne & LHSKnownOne);
334 // Output known-1 are known to be set if set in only one of the LHS, RHS.
335 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
336 (RHSKnownOne & LHSKnownZero);
337
338 // If the client is only demanding bits that we know, return the known
339 // constant.
340 if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
341 return Constant::getIntegerValue(VTy, IKnownOne);
342
Chris Lattner7e044912010-01-04 07:17:19 +0000343 // If all of the demanded bits are known zero on one side, return the other.
344 // These bits cannot contribute to the result of the 'xor'.
345 if ((DemandedMask & RHSKnownZero) == DemandedMask)
346 return I->getOperand(0);
347 if ((DemandedMask & LHSKnownZero) == DemandedMask)
348 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000349
Chris Lattner7e044912010-01-04 07:17:19 +0000350 // If all of the demanded bits are known to be zero on one side or the
351 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000352 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Chris Lattner7e044912010-01-04 07:17:19 +0000353 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
Craig Topper4c947752012-12-22 18:09:02 +0000354 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000355 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
356 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000357 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000358 }
Craig Topper4c947752012-12-22 18:09:02 +0000359
Chris Lattner7e044912010-01-04 07:17:19 +0000360 // If all of the demanded bits on one side are known, and all of the set
361 // bits on that side are also known to be set on the other side, turn this
362 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000363 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper4c947752012-12-22 18:09:02 +0000364 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
Chris Lattner7e044912010-01-04 07:17:19 +0000365 // all known
366 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
367 Constant *AndC = Constant::getIntegerValue(VTy,
368 ~RHSKnownOne & DemandedMask);
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000369 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000370 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000371 }
372 }
Craig Topper4c947752012-12-22 18:09:02 +0000373
Chris Lattner7e044912010-01-04 07:17:19 +0000374 // If the RHS is a constant, see if we can simplify it.
375 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
376 if (ShrinkDemandedConstant(I, 1, DemandedMask))
377 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000378
Chris Lattner7e044912010-01-04 07:17:19 +0000379 // If our LHS is an 'and' and if it has one use, and if any of the bits we
380 // are flipping are known to be set, then the xor is just resetting those
381 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
382 // simplifying both of them.
383 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
384 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
385 isa<ConstantInt>(I->getOperand(1)) &&
386 isa<ConstantInt>(LHSInst->getOperand(1)) &&
387 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
388 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
389 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
390 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000391
Chris Lattner7e044912010-01-04 07:17:19 +0000392 Constant *AndC =
393 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000394 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000395 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000396
Chris Lattner7e044912010-01-04 07:17:19 +0000397 Constant *XorC =
398 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000399 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000400 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000401 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000402
403 // Output known-0 bits are known if clear or set in both the LHS & RHS.
404 KnownZero= (RHSKnownZero & LHSKnownZero) | (RHSKnownOne & LHSKnownOne);
405 // Output known-1 are known to be set if set in only one of the LHS, RHS.
406 KnownOne = (RHSKnownZero & LHSKnownOne) | (RHSKnownOne & LHSKnownZero);
Chris Lattner7e044912010-01-04 07:17:19 +0000407 break;
408 }
409 case Instruction::Select:
James Molloy2b21a7c2015-05-20 18:41:25 +0000410 // If this is a select as part of a min/max pattern, don't simplify any
411 // further in case we break the structure.
412 Value *LHS, *RHS;
James Molloy134bec22015-08-11 09:12:57 +0000413 if (matchSelectPattern(I, LHS, RHS).Flavor != SPF_UNKNOWN)
James Molloy2b21a7c2015-05-20 18:41:25 +0000414 return nullptr;
Simon Pilgrim61116dd2015-09-17 20:32:45 +0000415
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000416 if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask, RHSKnownZero,
417 RHSKnownOne, Depth + 1) ||
418 SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, LHSKnownZero,
419 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000420 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000421 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
422 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
423
Chris Lattner7e044912010-01-04 07:17:19 +0000424 // If the operands are constants, see if we can simplify them.
425 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
426 ShrinkDemandedConstant(I, 2, DemandedMask))
427 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000428
Chris Lattner7e044912010-01-04 07:17:19 +0000429 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000430 KnownOne = RHSKnownOne & LHSKnownOne;
431 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000432 break;
433 case Instruction::Trunc: {
434 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000435 DemandedMask = DemandedMask.zext(truncBf);
436 KnownZero = KnownZero.zext(truncBf);
437 KnownOne = KnownOne.zext(truncBf);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000438 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, KnownZero,
439 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000440 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000441 DemandedMask = DemandedMask.trunc(BitWidth);
442 KnownZero = KnownZero.trunc(BitWidth);
443 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000444 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000445 break;
446 }
447 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000448 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000449 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000450
Chris Lattner229907c2011-07-18 04:54:35 +0000451 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
452 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000453 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
454 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
455 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000456 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000457 } else
458 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000459 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000460 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000461 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000462 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000463
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000464 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, KnownZero,
465 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000466 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000467 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000468 break;
469 case Instruction::ZExt: {
470 // Compute the bits in the result that are not present in the input.
471 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000472
Jay Foad583abbc2010-12-07 08:25:19 +0000473 DemandedMask = DemandedMask.trunc(SrcBitWidth);
474 KnownZero = KnownZero.trunc(SrcBitWidth);
475 KnownOne = KnownOne.trunc(SrcBitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000476 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, KnownZero,
477 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000478 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000479 DemandedMask = DemandedMask.zext(BitWidth);
480 KnownZero = KnownZero.zext(BitWidth);
481 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000482 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000483 // The top bits are known to be zero.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000484 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000485 break;
486 }
487 case Instruction::SExt: {
488 // Compute the bits in the result that are not present in the input.
489 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000490
491 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000492 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
493
494 APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
495 // If any of the sign extended bits are demanded, we know that the sign
496 // bit is demanded.
497 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000498 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000499
Jay Foad583abbc2010-12-07 08:25:19 +0000500 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
501 KnownZero = KnownZero.trunc(SrcBitWidth);
502 KnownOne = KnownOne.trunc(SrcBitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000503 if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits, KnownZero,
504 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000505 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000506 InputDemandedBits = InputDemandedBits.zext(BitWidth);
507 KnownZero = KnownZero.zext(BitWidth);
508 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000509 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
510
Chris Lattner7e044912010-01-04 07:17:19 +0000511 // If the sign bit of the input is known set or clear, then we know the
512 // top bits of the result.
513
514 // If the input sign bit is known zero, or if the NewBits are not demanded
515 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000516 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000517 // Convert to ZExt cast
518 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000519 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000520 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
521 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000522 }
523 break;
524 }
Matthias Braune48484c2015-04-30 22:05:30 +0000525 case Instruction::Add:
526 case Instruction::Sub: {
527 /// If the high-bits of an ADD/SUB are not demanded, then we do not care
528 /// about the high bits of the operands.
Chris Lattner7e044912010-01-04 07:17:19 +0000529 unsigned NLZ = DemandedMask.countLeadingZeros();
Matthias Braune48484c2015-04-30 22:05:30 +0000530 if (NLZ > 0) {
531 // Right fill the mask of bits for this ADD/SUB to demand the most
Chris Lattner7e044912010-01-04 07:17:19 +0000532 // significant bit and all those below it.
Chris Lattner7e044912010-01-04 07:17:19 +0000533 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
534 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000535 LHSKnownZero, LHSKnownOne, Depth + 1) ||
Matthias Braune48484c2015-04-30 22:05:30 +0000536 ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
Chris Lattner7e044912010-01-04 07:17:19 +0000537 SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
David Majnemer7d0e99c2015-04-22 22:42:05 +0000538 LHSKnownZero, LHSKnownOne, Depth + 1)) {
Matthias Braune48484c2015-04-30 22:05:30 +0000539 // Disable the nsw and nuw flags here: We can no longer guarantee that
540 // we won't wrap after simplification. Removing the nsw/nuw flags is
541 // legal here because the top bit is not demanded.
542 BinaryOperator &BinOP = *cast<BinaryOperator>(I);
543 BinOP.setHasNoSignedWrap(false);
544 BinOP.setHasNoUnsignedWrap(false);
Chris Lattner7e044912010-01-04 07:17:19 +0000545 return I;
David Majnemer7d0e99c2015-04-22 22:42:05 +0000546 }
Chris Lattner7e044912010-01-04 07:17:19 +0000547 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000548
Matthias Braune48484c2015-04-30 22:05:30 +0000549 // Otherwise just hand the add/sub off to computeKnownBits to fill in
Chris Lattner7e044912010-01-04 07:17:19 +0000550 // the known zeros and ones.
Hal Finkel60db0582014-09-07 18:57:58 +0000551 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000552 break;
Matthias Braune48484c2015-04-30 22:05:30 +0000553 }
Chris Lattner7e044912010-01-04 07:17:19 +0000554 case Instruction::Shl:
555 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000556 {
557 Value *VarX; ConstantInt *C1;
558 if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
559 Instruction *Shr = cast<Instruction>(I->getOperand(0));
560 Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
561 KnownZero, KnownOne);
562 if (R)
563 return R;
564 }
565 }
566
Chris Lattner768003c2011-02-10 05:09:34 +0000567 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000568 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000569
Chris Lattner768003c2011-02-10 05:09:34 +0000570 // If the shift is NUW/NSW, then it does demand the high bits.
571 ShlOperator *IOp = cast<ShlOperator>(I);
572 if (IOp->hasNoSignedWrap())
573 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
574 else if (IOp->hasNoUnsignedWrap())
575 DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000576
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000577 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, KnownZero,
578 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000579 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000580 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
581 KnownZero <<= ShiftAmt;
582 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000583 // low bits known zero.
584 if (ShiftAmt)
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000585 KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000586 }
587 break;
588 case Instruction::LShr:
589 // For a logical shift right
590 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000591 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000592
Chris Lattner7e044912010-01-04 07:17:19 +0000593 // Unsigned shift right.
594 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000595
Chris Lattner768003c2011-02-10 05:09:34 +0000596 // If the shift is exact, then it does demand the low bits (and knows that
597 // they are zero).
598 if (cast<LShrOperator>(I)->isExact())
599 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000600
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000601 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, KnownZero,
602 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000603 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000604 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
605 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
606 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000607 if (ShiftAmt) {
608 // Compute the new bits that are at the top now.
609 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000610 KnownZero |= HighBits; // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000611 }
612 }
613 break;
614 case Instruction::AShr:
615 // If this is an arithmetic shift right and only the low-bit is set, we can
616 // always convert this into a logical shr, even if the shift amount is
617 // variable. The low bit of the shift cannot be an input sign bit unless
618 // the shift amount is >= the size of the datatype, which is undefined.
619 if (DemandedMask == 1) {
620 // Perform the logical shift right.
621 Instruction *NewVal = BinaryOperator::CreateLShr(
622 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000623 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000624 }
Chris Lattner7e044912010-01-04 07:17:19 +0000625
626 // If the sign bit is the only bit demanded by this ashr, then there is no
627 // need to do it, the shift doesn't change the high bit.
628 if (DemandedMask.isSignBit())
629 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000630
Chris Lattner7e044912010-01-04 07:17:19 +0000631 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000632 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000633
Chris Lattner7e044912010-01-04 07:17:19 +0000634 // Signed shift right.
635 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
636 // If any of the "high bits" are demanded, we should set the sign bit as
637 // demanded.
638 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000639 DemandedMaskIn.setBit(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000640
Chris Lattner768003c2011-02-10 05:09:34 +0000641 // If the shift is exact, then it does demand the low bits (and knows that
642 // they are zero).
643 if (cast<AShrOperator>(I)->isExact())
644 DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000645
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000646 if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, KnownZero,
647 KnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000648 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000649 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000650 // Compute the new bits that are at the top now.
651 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000652 KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
653 KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000654
Chris Lattner7e044912010-01-04 07:17:19 +0000655 // Handle the sign bits.
656 APInt SignBit(APInt::getSignBit(BitWidth));
657 // Adjust to where it is now in the mask.
Craig Topper4c947752012-12-22 18:09:02 +0000658 SignBit = APIntOps::lshr(SignBit, ShiftAmt);
659
Chris Lattner7e044912010-01-04 07:17:19 +0000660 // If the input sign bit is known to be zero, or if none of the top bits
661 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000662 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Chris Lattner7e044912010-01-04 07:17:19 +0000663 (HighBits & ~DemandedMask) == HighBits) {
664 // Perform the logical shift right.
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000665 BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
666 SA, I->getName());
667 NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000668 return InsertNewInstWith(NewVal, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000669 } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
670 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000671 }
672 }
673 break;
674 case Instruction::SRem:
675 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000676 // X % -1 demands all the bits because we don't want to introduce
677 // INT_MIN % -1 (== undef) by accident.
678 if (Rem->isAllOnesValue())
679 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000680 APInt RA = Rem->getValue().abs();
681 if (RA.isPowerOf2()) {
682 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
683 return I->getOperand(0);
684
685 APInt LowBits = RA - 1;
686 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000687 if (SimplifyDemandedBits(I->getOperandUse(0), Mask2, LHSKnownZero,
688 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000689 return I;
690
Duncan Sands3a48b872010-01-28 17:22:42 +0000691 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000692 KnownZero = LHSKnownZero & LowBits;
693 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000694
Duncan Sands3a48b872010-01-28 17:22:42 +0000695 // If LHS is non-negative or has all low bits zero, then the upper bits
696 // are all zero.
697 if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
698 KnownZero |= ~LowBits;
699
700 // If LHS is negative and not all low bits are zero, then the upper bits
701 // are all one.
702 if (LHSKnownOne[BitWidth-1] && ((LHSKnownOne & LowBits) != 0))
703 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000704
Craig Topper4c947752012-12-22 18:09:02 +0000705 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000706 }
707 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000708
709 // The sign bit is the LHS's sign bit, except when the result of the
710 // remainder is zero.
711 if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
Nick Lewyckye4679792011-03-07 01:50:10 +0000712 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000713 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000714 CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000715 // If it's known zero, our sign bit is also zero.
716 if (LHSKnownZero.isNegative())
Benjamin Kramer21b972a2013-05-09 16:32:32 +0000717 KnownZero.setBit(KnownZero.getBitWidth() - 1);
Nick Lewyckye4679792011-03-07 01:50:10 +0000718 }
Chris Lattner7e044912010-01-04 07:17:19 +0000719 break;
720 case Instruction::URem: {
721 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
722 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000723 if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes, KnownZero2,
724 KnownOne2, Depth + 1) ||
725 SimplifyDemandedBits(I->getOperandUse(1), AllOnes, KnownZero2,
726 KnownOne2, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000727 return I;
728
729 unsigned Leaders = KnownZero2.countLeadingOnes();
730 Leaders = std::max(Leaders,
731 KnownZero2.countLeadingOnes());
732 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
733 break;
734 }
735 case Instruction::Call:
736 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
737 switch (II->getIntrinsicID()) {
738 default: break;
739 case Intrinsic::bswap: {
740 // If the only bits demanded come from one byte of the bswap result,
741 // just shift the input byte into position to eliminate the bswap.
742 unsigned NLZ = DemandedMask.countLeadingZeros();
743 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000744
Chris Lattner7e044912010-01-04 07:17:19 +0000745 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
746 // we need all the bits down to bit 8. Likewise, round NLZ. If we
747 // have 14 leading zeros, round to 8.
748 NLZ &= ~7;
749 NTZ &= ~7;
750 // If we need exactly one byte, we can do this transformation.
751 if (BitWidth-NLZ-NTZ == 8) {
752 unsigned ResultBit = NTZ;
753 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000754
Chris Lattner7e044912010-01-04 07:17:19 +0000755 // Replace this with either a left or right shift to get the byte into
756 // the right place.
757 Instruction *NewVal;
758 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000759 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000760 ConstantInt::get(I->getType(), InputBit-ResultBit));
761 else
Gabor Greif79430172010-06-24 12:35:13 +0000762 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000763 ConstantInt::get(I->getType(), ResultBit-InputBit));
764 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000765 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000766 }
Craig Topper4c947752012-12-22 18:09:02 +0000767
Chris Lattner7e044912010-01-04 07:17:19 +0000768 // TODO: Could compute known zero/one bits based on the input.
769 break;
770 }
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000771 case Intrinsic::x86_mmx_pmovmskb:
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000772 case Intrinsic::x86_sse_movmsk_ps:
773 case Intrinsic::x86_sse2_movmsk_pd:
774 case Intrinsic::x86_sse2_pmovmskb_128:
775 case Intrinsic::x86_avx_movmsk_ps_256:
776 case Intrinsic::x86_avx_movmsk_pd_256:
777 case Intrinsic::x86_avx2_pmovmskb: {
778 // MOVMSK copies the vector elements' sign bits to the low bits
779 // and zeros the high bits.
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000780 unsigned ArgWidth;
781 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
782 ArgWidth = 8; // Arg is x86_mmx, but treated as <8 x i8>.
783 } else {
784 auto Arg = II->getArgOperand(0);
785 auto ArgType = cast<VectorType>(Arg->getType());
786 ArgWidth = ArgType->getNumElements();
787 }
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000788
789 // If we don't need any of low bits then return zero,
790 // we know that DemandedMask is non-zero already.
791 APInt DemandedElts = DemandedMask.zextOrTrunc(ArgWidth);
792 if (DemandedElts == 0)
793 return ConstantInt::getNullValue(VTy);
794
Ahmed Bougacha17482a52016-04-28 14:36:07 +0000795 // We know that the upper bits are set to zero.
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000796 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - ArgWidth);
797 return nullptr;
798 }
Chad Rosierb3628842011-05-26 23:13:19 +0000799 case Intrinsic::x86_sse42_crc32_64_64:
Evan Chenge8d2e9e2011-05-20 00:54:37 +0000800 KnownZero = APInt::getHighBitsSet(64, 32);
Craig Topperf40110f2014-04-25 05:29:35 +0000801 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000802 }
803 }
Hal Finkel60db0582014-09-07 18:57:58 +0000804 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000805 break;
806 }
Craig Topper4c947752012-12-22 18:09:02 +0000807
Chris Lattner7e044912010-01-04 07:17:19 +0000808 // If the client is only demanding bits that we know, return the known
809 // constant.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000810 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
811 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000812 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000813}
814
Shuxin Yang63e999e2012-12-04 00:04:54 +0000815/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
816/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
817/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
818/// of "C2-C1".
819///
820/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
821/// ..., bn}, without considering the specific value X is holding.
822/// This transformation is legal iff one of following conditions is hold:
823/// 1) All the bit in S are 0, in this case E1 == E2.
824/// 2) We don't care those bits in S, per the input DemandedMask.
825/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
826/// rest bits.
827///
828/// Currently we only test condition 2).
829///
830/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
831/// not successful.
832Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
833 Instruction *Shl, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne) {
834
Benjamin Kramer010f1082013-08-30 14:35:35 +0000835 const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
836 const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
837 if (!ShlOp1 || !ShrOp1)
Craig Topperf40110f2014-04-25 05:29:35 +0000838 return nullptr; // Noop.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000839
840 Value *VarX = Shr->getOperand(0);
841 Type *Ty = VarX->getType();
842 unsigned BitWidth = Ty->getIntegerBitWidth();
843 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000844 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000845
846 unsigned ShlAmt = ShlOp1.getZExtValue();
847 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000848
849 KnownOne.clearAllBits();
850 KnownZero = APInt::getBitsSet(KnownZero.getBitWidth(), 0, ShlAmt-1);
851 KnownZero &= DemandedMask;
852
Benjamin Kramer010f1082013-08-30 14:35:35 +0000853 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
854 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000855
856 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
857 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
858 (BitMask1.ashr(ShrAmt) << ShlAmt);
859
860 if (ShrAmt <= ShlAmt) {
861 BitMask2 <<= (ShlAmt - ShrAmt);
862 } else {
863 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
864 BitMask2.ashr(ShrAmt - ShlAmt);
865 }
866
867 // Check if condition-2 (see the comment to this function) is satified.
868 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
869 if (ShrAmt == ShlAmt)
870 return VarX;
871
872 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000873 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000874
875 BinaryOperator *New;
876 if (ShrAmt < ShlAmt) {
877 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
878 New = BinaryOperator::CreateShl(VarX, Amt);
879 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
880 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
881 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
882 } else {
883 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000884 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
885 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000886 if (cast<BinaryOperator>(Shr)->isExact())
887 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000888 }
889
890 return InsertNewInstWith(New, *Shl);
891 }
892
Craig Topperf40110f2014-04-25 05:29:35 +0000893 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000894}
Chris Lattner7e044912010-01-04 07:17:19 +0000895
896/// SimplifyDemandedVectorElts - The specified value produces a vector with
897/// any number of elements. DemandedElts contains the set of elements that are
898/// actually used by the caller. This method analyzes which elements of the
899/// operand are undef and returns that information in UndefElts.
900///
901/// If the information about demanded elements can be used to simplify the
902/// operation, the operation is simplified, then the resultant value is
903/// returned. This returns null if no change was made.
904Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000905 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000906 unsigned Depth) {
Sanjay Patel9190b4a2016-04-29 20:54:56 +0000907 unsigned VWidth = V->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +0000908 APInt EltMask(APInt::getAllOnesValue(VWidth));
909 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
910
911 if (isa<UndefValue>(V)) {
912 // If the entire vector is undefined, just return this info.
913 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000914 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000915 }
Craig Topper4c947752012-12-22 18:09:02 +0000916
Chris Lattnerb22423c2010-02-08 23:56:03 +0000917 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000918 UndefElts = EltMask;
919 return UndefValue::get(V->getType());
920 }
921
922 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000923
Chris Lattner67058832012-01-25 06:48:06 +0000924 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
925 if (Constant *C = dyn_cast<Constant>(V)) {
926 // Check if this is identity. If so, return 0 since we are not simplifying
927 // anything.
928 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000929 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000930
Chris Lattner229907c2011-07-18 04:54:35 +0000931 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000932 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000933
Chris Lattner67058832012-01-25 06:48:06 +0000934 SmallVector<Constant*, 16> Elts;
935 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000936 if (!DemandedElts[i]) { // If not demanded, set to undef.
937 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000938 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +0000939 continue;
940 }
Craig Topper4c947752012-12-22 18:09:02 +0000941
Chris Lattner67058832012-01-25 06:48:06 +0000942 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000943 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000944
Chris Lattner67058832012-01-25 06:48:06 +0000945 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000946 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000947 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +0000948 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +0000949 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +0000950 }
Chris Lattner67058832012-01-25 06:48:06 +0000951 }
Craig Topper4c947752012-12-22 18:09:02 +0000952
Chris Lattner7e044912010-01-04 07:17:19 +0000953 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +0000954 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +0000955 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000956 }
Craig Topper4c947752012-12-22 18:09:02 +0000957
Chris Lattner7e044912010-01-04 07:17:19 +0000958 // Limit search depth.
959 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +0000960 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000961
Stuart Hastings5bd18b62011-05-17 22:13:31 +0000962 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +0000963 // simplification conservatively assuming that all elements
964 // are needed.
965 if (!V->hasOneUse()) {
966 // Quit if we find multiple users of a non-root value though.
967 // They'll be handled when it's their turn to be visited by
968 // the main instcombine process.
969 if (Depth != 0)
970 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +0000971 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000972
973 // Conservatively assume that all elements are needed.
974 DemandedElts = EltMask;
975 }
Craig Topper4c947752012-12-22 18:09:02 +0000976
Chris Lattner7e044912010-01-04 07:17:19 +0000977 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +0000978 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +0000979
Chris Lattner7e044912010-01-04 07:17:19 +0000980 bool MadeChange = false;
981 APInt UndefElts2(VWidth, 0);
982 Value *TmpV;
983 switch (I->getOpcode()) {
984 default: break;
Craig Topper4c947752012-12-22 18:09:02 +0000985
Chris Lattner7e044912010-01-04 07:17:19 +0000986 case Instruction::InsertElement: {
987 // If this is a variable index, we don't know which element it overwrites.
988 // demand exactly the same input as we produce.
989 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +0000990 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +0000991 // Note that we can't propagate undef elt info, because we don't know
992 // which elt is getting updated.
993 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000994 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +0000995 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
996 break;
997 }
Craig Topper4c947752012-12-22 18:09:02 +0000998
Chris Lattner7e044912010-01-04 07:17:19 +0000999 // If this is inserting an element that isn't demanded, remove this
1000 // insertelement.
1001 unsigned IdxNo = Idx->getZExtValue();
1002 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1003 Worklist.Add(I);
1004 return I->getOperand(0);
1005 }
Craig Topper4c947752012-12-22 18:09:02 +00001006
Chris Lattner7e044912010-01-04 07:17:19 +00001007 // Otherwise, the element inserted overwrites whatever was there, so the
1008 // input demanded set is simpler than the output set.
1009 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001010 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001011 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001012 UndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001013 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1014
1015 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001016 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001017 break;
1018 }
1019 case Instruction::ShuffleVector: {
1020 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
1021 uint64_t LHSVWidth =
1022 cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements();
1023 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1024 for (unsigned i = 0; i < VWidth; i++) {
1025 if (DemandedElts[i]) {
1026 unsigned MaskVal = Shuffle->getMaskValue(i);
1027 if (MaskVal != -1u) {
1028 assert(MaskVal < LHSVWidth * 2 &&
1029 "shufflevector mask index out of range!");
1030 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001031 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001032 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001033 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001034 }
1035 }
1036 }
1037
1038 APInt UndefElts4(LHSVWidth, 0);
1039 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001040 UndefElts4, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001041 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1042
1043 APInt UndefElts3(LHSVWidth, 0);
1044 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001045 UndefElts3, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001046 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1047
1048 bool NewUndefElts = false;
1049 for (unsigned i = 0; i < VWidth; i++) {
1050 unsigned MaskVal = Shuffle->getMaskValue(i);
1051 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001052 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001053 } else if (!DemandedElts[i]) {
1054 NewUndefElts = true;
1055 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001056 } else if (MaskVal < LHSVWidth) {
1057 if (UndefElts4[MaskVal]) {
1058 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001059 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001060 }
1061 } else {
1062 if (UndefElts3[MaskVal - LHSVWidth]) {
1063 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001064 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001065 }
1066 }
1067 }
1068
1069 if (NewUndefElts) {
1070 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001071 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001072 for (unsigned i = 0; i < VWidth; ++i) {
1073 if (UndefElts[i])
1074 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1075 else
1076 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1077 Shuffle->getMaskValue(i)));
1078 }
1079 I->setOperand(2, ConstantVector::get(Elts));
1080 MadeChange = true;
1081 }
1082 break;
1083 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001084 case Instruction::Select: {
1085 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1086 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1087 for (unsigned i = 0; i < VWidth; i++) {
Andrea Di Biagio40f59e42015-10-06 10:34:53 +00001088 Constant *CElt = CV->getAggregateElement(i);
1089 // Method isNullValue always returns false when called on a
1090 // ConstantExpr. If CElt is a ConstantExpr then skip it in order to
1091 // to avoid propagating incorrect information.
1092 if (isa<ConstantExpr>(CElt))
1093 continue;
1094 if (CElt->isNullValue())
Pete Cooperabc13af2012-07-26 23:10:24 +00001095 LeftDemanded.clearBit(i);
1096 else
1097 RightDemanded.clearBit(i);
1098 }
1099 }
1100
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001101 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded, UndefElts,
1102 Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001103 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1104
1105 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001106 UndefElts2, Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001107 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001108
Pete Cooperabc13af2012-07-26 23:10:24 +00001109 // Output elements are undefined if both are undefined.
1110 UndefElts &= UndefElts2;
1111 break;
1112 }
Chris Lattner7e044912010-01-04 07:17:19 +00001113 case Instruction::BitCast: {
1114 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001115 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001116 if (!VTy) break;
1117 unsigned InVWidth = VTy->getNumElements();
1118 APInt InputDemandedElts(InVWidth, 0);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001119 UndefElts2 = APInt(InVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001120 unsigned Ratio;
1121
1122 if (VWidth == InVWidth) {
1123 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1124 // elements as are demanded of us.
1125 Ratio = 1;
1126 InputDemandedElts = DemandedElts;
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001127 } else if ((VWidth % InVWidth) == 0) {
1128 // If the number of elements in the output is a multiple of the number of
1129 // elements in the input then an input element is live if any of the
1130 // corresponding output elements are live.
1131 Ratio = VWidth / InVWidth;
1132 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Chris Lattner7e044912010-01-04 07:17:19 +00001133 if (DemandedElts[OutIdx])
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001134 InputDemandedElts.setBit(OutIdx / Ratio);
1135 } else if ((InVWidth % VWidth) == 0) {
1136 // If the number of elements in the input is a multiple of the number of
1137 // elements in the output then an input element is live if the
1138 // corresponding output element is live.
1139 Ratio = InVWidth / VWidth;
Chris Lattner7e044912010-01-04 07:17:19 +00001140 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001141 if (DemandedElts[InIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001142 InputDemandedElts.setBit(InIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001143 } else {
1144 // Unsupported so far.
1145 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001146 }
Craig Topper4c947752012-12-22 18:09:02 +00001147
Chris Lattner7e044912010-01-04 07:17:19 +00001148 // div/rem demand all inputs, because they don't want divide by zero.
1149 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001150 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001151 if (TmpV) {
1152 I->setOperand(0, TmpV);
1153 MadeChange = true;
1154 }
Craig Topper4c947752012-12-22 18:09:02 +00001155
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001156 if (VWidth == InVWidth) {
1157 UndefElts = UndefElts2;
1158 } else if ((VWidth % InVWidth) == 0) {
1159 // If the number of elements in the output is a multiple of the number of
1160 // elements in the input then an output element is undef if the
1161 // corresponding input element is undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001162 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001163 if (UndefElts2[OutIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001164 UndefElts.setBit(OutIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001165 } else if ((InVWidth % VWidth) == 0) {
1166 // If the number of elements in the input is a multiple of the number of
1167 // elements in the output then an output element is undef if all of the
1168 // corresponding input elements are undef.
1169 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1170 APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
1171 if (SubUndef.countPopulation() == Ratio)
1172 UndefElts.setBit(OutIdx);
1173 }
1174 } else {
Chris Lattner7e044912010-01-04 07:17:19 +00001175 llvm_unreachable("Unimp");
Chris Lattner7e044912010-01-04 07:17:19 +00001176 }
1177 break;
1178 }
1179 case Instruction::And:
1180 case Instruction::Or:
1181 case Instruction::Xor:
1182 case Instruction::Add:
1183 case Instruction::Sub:
1184 case Instruction::Mul:
1185 // div/rem demand all inputs, because they don't want divide by zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001186 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1187 Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001188 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1189 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001190 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001191 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001192
Chris Lattner7e044912010-01-04 07:17:19 +00001193 // Output elements are undefined if both are undefined. Consider things
1194 // like undef&0. The result is known zero, not undef.
1195 UndefElts &= UndefElts2;
1196 break;
Pete Coopere807e452012-07-26 22:37:04 +00001197 case Instruction::FPTrunc:
1198 case Instruction::FPExt:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001199 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1200 Depth + 1);
Pete Coopere807e452012-07-26 22:37:04 +00001201 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1202 break;
Craig Topper4c947752012-12-22 18:09:02 +00001203
Chris Lattner7e044912010-01-04 07:17:19 +00001204 case Instruction::Call: {
1205 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1206 if (!II) break;
1207 switch (II->getIntrinsicID()) {
1208 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001209
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001210 // Unary scalar-as-vector operations that work column-wise.
Simon Pilgrim83020942016-04-24 18:23:14 +00001211 case Intrinsic::x86_sse_rcp_ss:
1212 case Intrinsic::x86_sse_rsqrt_ss:
1213 case Intrinsic::x86_sse_sqrt_ss:
1214 case Intrinsic::x86_sse2_sqrt_sd:
1215 case Intrinsic::x86_xop_vfrcz_ss:
1216 case Intrinsic::x86_xop_vfrcz_sd:
1217 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1218 UndefElts, Depth + 1);
1219 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1220
1221 // If lowest element of a scalar op isn't used then use Arg0.
1222 if (DemandedElts.getLoBits(1) != 1)
1223 return II->getArgOperand(0);
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001224 // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions
1225 // checks).
Simon Pilgrim83020942016-04-24 18:23:14 +00001226 break;
1227
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001228 // Binary scalar-as-vector operations that work column-wise. A dest element
1229 // is a function of the corresponding input elements from the two inputs.
Simon Pilgrim83020942016-04-24 18:23:14 +00001230 case Intrinsic::x86_sse_add_ss:
Chris Lattner7e044912010-01-04 07:17:19 +00001231 case Intrinsic::x86_sse_sub_ss:
1232 case Intrinsic::x86_sse_mul_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001233 case Intrinsic::x86_sse_div_ss:
Chris Lattner7e044912010-01-04 07:17:19 +00001234 case Intrinsic::x86_sse_min_ss:
1235 case Intrinsic::x86_sse_max_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001236 case Intrinsic::x86_sse_cmp_ss:
1237 case Intrinsic::x86_sse2_add_sd:
Chris Lattner7e044912010-01-04 07:17:19 +00001238 case Intrinsic::x86_sse2_sub_sd:
1239 case Intrinsic::x86_sse2_mul_sd:
Simon Pilgrim83020942016-04-24 18:23:14 +00001240 case Intrinsic::x86_sse2_div_sd:
Chris Lattner7e044912010-01-04 07:17:19 +00001241 case Intrinsic::x86_sse2_min_sd:
1242 case Intrinsic::x86_sse2_max_sd:
Simon Pilgrim83020942016-04-24 18:23:14 +00001243 case Intrinsic::x86_sse2_cmp_sd:
1244 case Intrinsic::x86_sse41_round_ss:
1245 case Intrinsic::x86_sse41_round_sd:
Gabor Greife23efee2010-06-28 16:45:00 +00001246 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001247 UndefElts, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001248 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1249 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001250 UndefElts2, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001251 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001252
1253 // If only the low elt is demanded and this is a scalarizable intrinsic,
1254 // scalarize it now.
1255 if (DemandedElts == 1) {
1256 switch (II->getIntrinsicID()) {
1257 default: break;
Simon Pilgrim83020942016-04-24 18:23:14 +00001258 case Intrinsic::x86_sse_add_ss:
Chris Lattner7e044912010-01-04 07:17:19 +00001259 case Intrinsic::x86_sse_sub_ss:
1260 case Intrinsic::x86_sse_mul_ss:
Simon Pilgrim4b5462f2016-04-24 18:35:59 +00001261 case Intrinsic::x86_sse_div_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001262 case Intrinsic::x86_sse2_add_sd:
Chris Lattner7e044912010-01-04 07:17:19 +00001263 case Intrinsic::x86_sse2_sub_sd:
1264 case Intrinsic::x86_sse2_mul_sd:
Simon Pilgrim4b5462f2016-04-24 18:35:59 +00001265 case Intrinsic::x86_sse2_div_sd:
Simon Pilgrim83020942016-04-24 18:23:14 +00001266 // TODO: Lower MIN/MAX/etc.
Gabor Greif79430172010-06-24 12:35:13 +00001267 Value *LHS = II->getArgOperand(0);
1268 Value *RHS = II->getArgOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +00001269 // Extract the element as scalars.
Craig Topper4c947752012-12-22 18:09:02 +00001270 LHS = InsertNewInstWith(ExtractElementInst::Create(LHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001271 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Eli Friedman6efb64e2011-05-19 01:20:42 +00001272 RHS = InsertNewInstWith(ExtractElementInst::Create(RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001273 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
Craig Topper4c947752012-12-22 18:09:02 +00001274
Chris Lattner7e044912010-01-04 07:17:19 +00001275 switch (II->getIntrinsicID()) {
1276 default: llvm_unreachable("Case stmts out of sync!");
Simon Pilgrim83020942016-04-24 18:23:14 +00001277 case Intrinsic::x86_sse_add_ss:
1278 case Intrinsic::x86_sse2_add_sd:
1279 TmpV = InsertNewInstWith(BinaryOperator::CreateFAdd(LHS, RHS,
1280 II->getName()), *II);
1281 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001282 case Intrinsic::x86_sse_sub_ss:
1283 case Intrinsic::x86_sse2_sub_sd:
Eli Friedman6efb64e2011-05-19 01:20:42 +00001284 TmpV = InsertNewInstWith(BinaryOperator::CreateFSub(LHS, RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001285 II->getName()), *II);
1286 break;
1287 case Intrinsic::x86_sse_mul_ss:
1288 case Intrinsic::x86_sse2_mul_sd:
Eli Friedman6efb64e2011-05-19 01:20:42 +00001289 TmpV = InsertNewInstWith(BinaryOperator::CreateFMul(LHS, RHS,
Chris Lattner7e044912010-01-04 07:17:19 +00001290 II->getName()), *II);
1291 break;
Simon Pilgrim4b5462f2016-04-24 18:35:59 +00001292 case Intrinsic::x86_sse_div_ss:
1293 case Intrinsic::x86_sse2_div_sd:
1294 TmpV = InsertNewInstWith(BinaryOperator::CreateFDiv(LHS, RHS,
1295 II->getName()), *II);
1296 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001297 }
Craig Topper4c947752012-12-22 18:09:02 +00001298
Chris Lattner7e044912010-01-04 07:17:19 +00001299 Instruction *New =
1300 InsertElementInst::Create(
1301 UndefValue::get(II->getType()), TmpV,
1302 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U, false),
1303 II->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +00001304 InsertNewInstWith(New, *II);
Chris Lattner7e044912010-01-04 07:17:19 +00001305 return New;
Craig Topper4c947752012-12-22 18:09:02 +00001306 }
Chris Lattner7e044912010-01-04 07:17:19 +00001307 }
Craig Topper4c947752012-12-22 18:09:02 +00001308
Simon Pilgrim83020942016-04-24 18:23:14 +00001309 // If lowest element of a scalar op isn't used then use Arg0.
1310 if (DemandedElts.getLoBits(1) != 1)
1311 return II->getArgOperand(0);
1312
Chris Lattner7e044912010-01-04 07:17:19 +00001313 // Output elements are undefined if both are undefined. Consider things
1314 // like undef&0. The result is known zero, not undef.
1315 UndefElts &= UndefElts2;
1316 break;
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001317
1318 // SSE4A instructions leave the upper 64-bits of the 128-bit result
1319 // in an undefined state.
1320 case Intrinsic::x86_sse4a_extrq:
1321 case Intrinsic::x86_sse4a_extrqi:
1322 case Intrinsic::x86_sse4a_insertq:
1323 case Intrinsic::x86_sse4a_insertqi:
1324 UndefElts |= APInt::getHighBitsSet(VWidth, VWidth / 2);
1325 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001326 }
1327 break;
1328 }
1329 }
Craig Topperf40110f2014-04-25 05:29:35 +00001330 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001331}