blob: 934fcfe78cb3058f626350411ef5b1ad53c90901 [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
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000025/// Check to see if the specified operand of the specified instruction is a
26/// constant integer. If so, check to see if there are any bits set in the
27/// constant that are not demanded. If so, shrink the constant and return true.
Craig Topper4c947752012-12-22 18:09:02 +000028static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
Chris Lattner7e044912010-01-04 07:17:19 +000029 APInt Demanded) {
30 assert(I && "No instruction?");
31 assert(OpNo < I->getNumOperands() && "Operand index too large");
32
Sanjay Patelae3b43e2017-02-09 21:43:06 +000033 // The operand must be a constant integer or splat integer.
34 Value *Op = I->getOperand(OpNo);
35 const APInt *C;
36 if (!match(Op, m_APInt(C)))
37 return false;
Chris Lattner7e044912010-01-04 07:17:19 +000038
39 // If there are no bits set that aren't demanded, nothing to do.
Sanjay Patelae3b43e2017-02-09 21:43:06 +000040 Demanded = Demanded.zextOrTrunc(C->getBitWidth());
41 if ((~Demanded & *C) == 0)
Chris Lattner7e044912010-01-04 07:17:19 +000042 return false;
43
44 // This instruction is producing bits that are not demanded. Shrink the RHS.
Sanjay Patelae3b43e2017-02-09 21:43:06 +000045 Demanded &= *C;
46 I->setOperand(OpNo, ConstantInt::get(Op->getType(), Demanded));
David Majnemer42b83a52014-08-22 07:56:32 +000047
Chris Lattner7e044912010-01-04 07:17:19 +000048 return true;
49}
50
51
52
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000053/// Inst is an integer instruction that SimplifyDemandedBits knows about. See if
54/// the instruction has any properties that allow us to simplify its operands.
Chris Lattner7e044912010-01-04 07:17:19 +000055bool 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
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000068/// This form of SimplifyDemandedBits simplifies the specified instruction
69/// operand if possible, updating it in place. It returns true if it made any
70/// change and false otherwise.
Craig Topper47596dd2017-03-25 06:52:52 +000071bool InstCombiner::SimplifyDemandedBits(Instruction *I, unsigned OpNo,
72 const APInt &DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +000073 APInt &KnownZero, APInt &KnownOne,
74 unsigned Depth) {
Craig Topper47596dd2017-03-25 06:52:52 +000075 Use &U = I->getOperandUse(OpNo);
David Majnemerfe58d132015-04-22 20:59:28 +000076 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, KnownZero,
Craig Topper47596dd2017-03-25 06:52:52 +000077 KnownOne, Depth, I);
Craig Topperf40110f2014-04-25 05:29:35 +000078 if (!NewVal) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000079 U = NewVal;
80 return true;
81}
82
83
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000084/// This function attempts to replace V with a simpler value based on the
85/// demanded bits. When this function is called, it is known that only the bits
86/// set in DemandedMask of the result of V are ever used downstream.
87/// Consequently, depending on the mask and V, it may be possible to replace V
88/// with a constant or one of its operands. In such cases, this function does
89/// the replacement and returns true. In all other cases, it returns false after
90/// analyzing the expression and setting KnownOne and known to be one in the
91/// expression. KnownZero contains all the bits that are known to be zero in the
92/// expression. These are provided to potentially allow the caller (which might
93/// recursively be SimplifyDemandedBits itself) to simplify the expression.
94/// KnownOne and KnownZero always follow the invariant that:
95/// KnownOne & KnownZero == 0.
96/// That is, a bit can't be both 1 and 0. Note that the bits in KnownOne and
97/// KnownZero may only be accurate for those bits set in DemandedMask. Note also
98/// that the bitwidth of V, DemandedMask, KnownZero and KnownOne must all be the
99/// same.
Chris Lattner7e044912010-01-04 07:17:19 +0000100///
101/// This returns null if it did not change anything and it permits no
102/// simplification. This returns V itself if it did some simplification of V's
103/// operands based on the information about what bits are demanded. This returns
104/// some other non-null value if it found out that V is equal to another value
105/// in the context where the specified bits are demanded, but not for all users.
106Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
107 APInt &KnownZero, APInt &KnownOne,
Hal Finkel60db0582014-09-07 18:57:58 +0000108 unsigned Depth,
109 Instruction *CxtI) {
Craig Toppere73658d2014-04-28 04:05:08 +0000110 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000111 assert(Depth <= 6 && "Limit Search Depth");
112 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000113 Type *VTy = V->getType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000114 assert(
115 (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
116 KnownZero.getBitWidth() == BitWidth &&
117 KnownOne.getBitWidth() == BitWidth &&
118 "Value *V, DemandedMask, KnownZero and KnownOne "
119 "must have same BitWidth");
Sanjay Patelae3b43e2017-02-09 21:43:06 +0000120 const APInt *C;
121 if (match(V, m_APInt(C))) {
122 // We know all of the bits for a scalar constant or a splat vector constant!
123 KnownOne = *C & DemandedMask;
Chris Lattner7e044912010-01-04 07:17:19 +0000124 KnownZero = ~KnownOne & DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000125 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000126 }
127 if (isa<ConstantPointerNull>(V)) {
128 // We know all of the bits for a constant!
Jay Foad25a5e4c2010-12-01 08:53:58 +0000129 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000130 KnownZero = DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000131 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000132 }
133
Jay Foad25a5e4c2010-12-01 08:53:58 +0000134 KnownZero.clearAllBits();
135 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000136 if (DemandedMask == 0) { // Not demanding any bits from V.
137 if (isa<UndefValue>(V))
Craig Topperf40110f2014-04-25 05:29:35 +0000138 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000139 return UndefValue::get(VTy);
140 }
Craig Topper4c947752012-12-22 18:09:02 +0000141
Chris Lattner7e044912010-01-04 07:17:19 +0000142 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000143 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000144
Chris Lattner7e044912010-01-04 07:17:19 +0000145 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000146 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +0000147
148 Instruction *I = dyn_cast<Instruction>(V);
149 if (!I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000150 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000151 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000152 }
153
154 // If there are multiple uses of this value and we aren't at the root, then
155 // we can't do any simplifications of the operands, because DemandedMask
156 // only reflects the bits demanded by *one* of the users.
157 if (Depth != 0 && !I->hasOneUse()) {
158 // Despite the fact that we can't simplify this instruction in all User's
159 // context, we can at least compute the knownzero/knownone bits, and we can
160 // do simplifications that apply to *just* the one user if we know that
161 // this instruction has a simpler value in that context.
162 if (I->getOpcode() == Instruction::And) {
163 // If either the LHS or the RHS are Zero, the result is zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000164 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000165 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000166 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000167 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000168
Chris Lattner7e044912010-01-04 07:17:19 +0000169 // If all of the demanded bits are known 1 on one side, return the other.
170 // These bits cannot contribute to the result of the 'and' in this
171 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000172 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000173 (DemandedMask & ~LHSKnownZero))
174 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000175 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000176 (DemandedMask & ~RHSKnownZero))
177 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000178
Chris Lattner7e044912010-01-04 07:17:19 +0000179 // If all of the demanded bits in the inputs are known zeros, return zero.
180 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
181 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000182
Chris Lattner7e044912010-01-04 07:17:19 +0000183 } else if (I->getOpcode() == Instruction::Or) {
184 // We can simplify (X|Y) -> X or Y in the user's context if we know that
185 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000186
Chris Lattner7e044912010-01-04 07:17:19 +0000187 // If either the LHS or the RHS are One, the result is One.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000188 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000189 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000190 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000191 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000192
Chris Lattner7e044912010-01-04 07:17:19 +0000193 // If all of the demanded bits are known zero on one side, return the
194 // other. These bits cannot contribute to the result of the 'or' in this
195 // context.
Craig Topper4c947752012-12-22 18:09:02 +0000196 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000197 (DemandedMask & ~LHSKnownOne))
198 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000199 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000200 (DemandedMask & ~RHSKnownOne))
201 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000202
Chris Lattner7e044912010-01-04 07:17:19 +0000203 // If all of the potentially set bits on one side are known to be set on
204 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000205 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000206 (DemandedMask & (~RHSKnownZero)))
207 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000208 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000209 (DemandedMask & (~LHSKnownZero)))
210 return I->getOperand(1);
Shuxin Yang73285932012-12-04 22:15:32 +0000211 } else if (I->getOpcode() == Instruction::Xor) {
212 // We can simplify (X^Y) -> X or Y in the user's context if we know that
213 // only bits from X or Y are demanded.
Craig Topper4c947752012-12-22 18:09:02 +0000214
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000215 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000216 CxtI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000217 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000218 CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000219
Shuxin Yang73285932012-12-04 22:15:32 +0000220 // If all of the demanded bits are known zero on one side, return the
Craig Topper4c947752012-12-22 18:09:02 +0000221 // other.
Shuxin Yang73285932012-12-04 22:15:32 +0000222 if ((DemandedMask & RHSKnownZero) == DemandedMask)
223 return I->getOperand(0);
224 if ((DemandedMask & LHSKnownZero) == DemandedMask)
225 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000226 }
Shuxin Yang73285932012-12-04 22:15:32 +0000227
Chris Lattner7e044912010-01-04 07:17:19 +0000228 // Compute the KnownZero/KnownOne bits to simplify things downstream.
Hal Finkel60db0582014-09-07 18:57:58 +0000229 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000230 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000231 }
Craig Topper4c947752012-12-22 18:09:02 +0000232
Chris Lattner7e044912010-01-04 07:17:19 +0000233 // If this is the root being simplified, allow it to have multiple uses,
234 // just set the DemandedMask to all bits so that we can try to simplify the
235 // operands. This allows visitTruncInst (for example) to simplify the
236 // operand of a trunc without duplicating all the logic below.
237 if (Depth == 0 && !V->hasOneUse())
Craig Toppere06b6bc2017-04-04 05:03:02 +0000238 DemandedMask.setAllBits();
Craig Topper4c947752012-12-22 18:09:02 +0000239
Chris Lattner7e044912010-01-04 07:17:19 +0000240 switch (I->getOpcode()) {
241 default:
Hal Finkel60db0582014-09-07 18:57:58 +0000242 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000243 break;
244 case Instruction::And:
245 // If either the LHS or the RHS are Zero, the result is zero.
Craig Topper47596dd2017-03-25 06:52:52 +0000246 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
247 Depth + 1) ||
248 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnownZero, LHSKnownZero,
249 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000250 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000251 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
252 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000253
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000254 // If the client is only demanding bits that we know, return the known
255 // constant.
256 if ((DemandedMask & ((RHSKnownZero | LHSKnownZero)|
257 (RHSKnownOne & LHSKnownOne))) == DemandedMask)
258 return Constant::getIntegerValue(VTy, RHSKnownOne & LHSKnownOne);
259
Chris Lattner7e044912010-01-04 07:17:19 +0000260 // If all of the demanded bits are known 1 on one side, return the other.
261 // These bits cannot contribute to the result of the 'and'.
Craig Topper4c947752012-12-22 18:09:02 +0000262 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000263 (DemandedMask & ~LHSKnownZero))
264 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000265 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000266 (DemandedMask & ~RHSKnownZero))
267 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000268
Chris Lattner7e044912010-01-04 07:17:19 +0000269 // If all of the demanded bits in the inputs are known zeros, return zero.
270 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
271 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000272
Chris Lattner7e044912010-01-04 07:17:19 +0000273 // If the RHS is a constant, see if we can simplify it.
274 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
275 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000276
Chris Lattner7e044912010-01-04 07:17:19 +0000277 // Output known-1 bits are only known if set in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000278 KnownOne = RHSKnownOne & LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000279 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000280 KnownZero = RHSKnownZero | LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000281 break;
282 case Instruction::Or:
283 // If either the LHS or the RHS are One, the result is One.
Craig Topper47596dd2017-03-25 06:52:52 +0000284 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
285 Depth + 1) ||
286 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnownOne, LHSKnownZero,
287 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000288 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000289 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
290 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
291
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000292 // If the client is only demanding bits that we know, return the known
293 // constant.
294 if ((DemandedMask & ((RHSKnownZero & LHSKnownZero)|
295 (RHSKnownOne | LHSKnownOne))) == DemandedMask)
296 return Constant::getIntegerValue(VTy, RHSKnownOne | LHSKnownOne);
297
Chris Lattner7e044912010-01-04 07:17:19 +0000298 // If all of the demanded bits are known zero on one side, return the other.
299 // These bits cannot contribute to the result of the 'or'.
Craig Topper4c947752012-12-22 18:09:02 +0000300 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000301 (DemandedMask & ~LHSKnownOne))
302 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000303 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000304 (DemandedMask & ~RHSKnownOne))
305 return I->getOperand(1);
306
307 // If all of the potentially set bits on one side are known to be set on
308 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000309 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000310 (DemandedMask & (~RHSKnownZero)))
311 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000312 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000313 (DemandedMask & (~LHSKnownZero)))
314 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000315
Chris Lattner7e044912010-01-04 07:17:19 +0000316 // If the RHS is a constant, see if we can simplify it.
317 if (ShrinkDemandedConstant(I, 1, DemandedMask))
318 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000319
Chris Lattner7e044912010-01-04 07:17:19 +0000320 // Output known-0 bits are only known if clear in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000321 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000322 // Output known-1 are known to be set if set in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000323 KnownOne = RHSKnownOne | LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000324 break;
325 case Instruction::Xor: {
Craig Topper47596dd2017-03-25 06:52:52 +0000326 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
327 Depth + 1) ||
328 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnownZero, LHSKnownOne,
329 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000330 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000331 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
332 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
333
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000334 // Output known-0 bits are known if clear or set in both the LHS & RHS.
335 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
336 (RHSKnownOne & LHSKnownOne);
337 // Output known-1 are known to be set if set in only one of the LHS, RHS.
338 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
339 (RHSKnownOne & LHSKnownZero);
340
341 // If the client is only demanding bits that we know, return the known
342 // constant.
343 if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
344 return Constant::getIntegerValue(VTy, IKnownOne);
345
Chris Lattner7e044912010-01-04 07:17:19 +0000346 // If all of the demanded bits are known zero on one side, return the other.
347 // These bits cannot contribute to the result of the 'xor'.
348 if ((DemandedMask & RHSKnownZero) == DemandedMask)
349 return I->getOperand(0);
350 if ((DemandedMask & LHSKnownZero) == DemandedMask)
351 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000352
Chris Lattner7e044912010-01-04 07:17:19 +0000353 // If all of the demanded bits are known to be zero on one side or the
354 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000355 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Chris Lattner7e044912010-01-04 07:17:19 +0000356 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
Craig Topper4c947752012-12-22 18:09:02 +0000357 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000358 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
359 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000360 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000361 }
Craig Topper4c947752012-12-22 18:09:02 +0000362
Chris Lattner7e044912010-01-04 07:17:19 +0000363 // If all of the demanded bits on one side are known, and all of the set
364 // bits on that side are also known to be set on the other side, turn this
365 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000366 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper4c947752012-12-22 18:09:02 +0000367 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
Chris Lattner7e044912010-01-04 07:17:19 +0000368 // all known
369 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
370 Constant *AndC = Constant::getIntegerValue(VTy,
371 ~RHSKnownOne & DemandedMask);
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000372 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000373 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000374 }
375 }
Craig Topper4c947752012-12-22 18:09:02 +0000376
Chris Lattner7e044912010-01-04 07:17:19 +0000377 // If the RHS is a constant, see if we can simplify it.
378 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
379 if (ShrinkDemandedConstant(I, 1, DemandedMask))
380 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000381
Chris Lattner7e044912010-01-04 07:17:19 +0000382 // If our LHS is an 'and' and if it has one use, and if any of the bits we
383 // are flipping are known to be set, then the xor is just resetting those
384 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
385 // simplifying both of them.
386 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
387 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
388 isa<ConstantInt>(I->getOperand(1)) &&
389 isa<ConstantInt>(LHSInst->getOperand(1)) &&
390 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
391 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
392 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
393 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000394
Chris Lattner7e044912010-01-04 07:17:19 +0000395 Constant *AndC =
396 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000397 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000398 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000399
Chris Lattner7e044912010-01-04 07:17:19 +0000400 Constant *XorC =
401 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000402 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000403 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000404 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000405
406 // Output known-0 bits are known if clear or set in both the LHS & RHS.
407 KnownZero= (RHSKnownZero & LHSKnownZero) | (RHSKnownOne & LHSKnownOne);
408 // Output known-1 are known to be set if set in only one of the LHS, RHS.
409 KnownOne = (RHSKnownZero & LHSKnownOne) | (RHSKnownOne & LHSKnownZero);
Chris Lattner7e044912010-01-04 07:17:19 +0000410 break;
411 }
412 case Instruction::Select:
James Molloy2b21a7c2015-05-20 18:41:25 +0000413 // If this is a select as part of a min/max pattern, don't simplify any
414 // further in case we break the structure.
415 Value *LHS, *RHS;
James Molloy134bec22015-08-11 09:12:57 +0000416 if (matchSelectPattern(I, LHS, RHS).Flavor != SPF_UNKNOWN)
James Molloy2b21a7c2015-05-20 18:41:25 +0000417 return nullptr;
Simon Pilgrim61116dd2015-09-17 20:32:45 +0000418
Craig Topper47596dd2017-03-25 06:52:52 +0000419 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnownZero, RHSKnownOne,
420 Depth + 1) ||
421 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnownZero, LHSKnownOne,
422 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000423 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000424 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
425 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
426
Chris Lattner7e044912010-01-04 07:17:19 +0000427 // If the operands are constants, see if we can simplify them.
428 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
429 ShrinkDemandedConstant(I, 2, DemandedMask))
430 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000431
Chris Lattner7e044912010-01-04 07:17:19 +0000432 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000433 KnownOne = RHSKnownOne & LHSKnownOne;
434 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000435 break;
436 case Instruction::Trunc: {
437 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000438 DemandedMask = DemandedMask.zext(truncBf);
439 KnownZero = KnownZero.zext(truncBf);
440 KnownOne = KnownOne.zext(truncBf);
Craig Topper47596dd2017-03-25 06:52:52 +0000441 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
442 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000443 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000444 DemandedMask = DemandedMask.trunc(BitWidth);
445 KnownZero = KnownZero.trunc(BitWidth);
446 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000447 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000448 break;
449 }
450 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000451 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000452 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000453
Chris Lattner229907c2011-07-18 04:54:35 +0000454 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
455 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000456 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
457 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
458 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000459 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000460 } else
461 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000462 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000463 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000464 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000465 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000466
Craig Topper47596dd2017-03-25 06:52:52 +0000467 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
468 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000469 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000470 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000471 break;
472 case Instruction::ZExt: {
473 // Compute the bits in the result that are not present in the input.
474 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000475
Jay Foad583abbc2010-12-07 08:25:19 +0000476 DemandedMask = DemandedMask.trunc(SrcBitWidth);
477 KnownZero = KnownZero.trunc(SrcBitWidth);
478 KnownOne = KnownOne.trunc(SrcBitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000479 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
480 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000481 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000482 DemandedMask = DemandedMask.zext(BitWidth);
483 KnownZero = KnownZero.zext(BitWidth);
484 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000485 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000486 // The top bits are known to be zero.
Craig Topper3a86a042017-03-19 05:49:16 +0000487 KnownZero.setBitsFrom(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000488 break;
489 }
490 case Instruction::SExt: {
491 // Compute the bits in the result that are not present in the input.
492 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000493
494 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000495 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
496
Craig Topper3a86a042017-03-19 05:49:16 +0000497 APInt NewBits(APInt::getBitsSetFrom(BitWidth, SrcBitWidth));
Chris Lattner7e044912010-01-04 07:17:19 +0000498 // If any of the sign extended bits are demanded, we know that the sign
499 // bit is demanded.
500 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000501 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000502
Jay Foad583abbc2010-12-07 08:25:19 +0000503 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
504 KnownZero = KnownZero.trunc(SrcBitWidth);
505 KnownOne = KnownOne.trunc(SrcBitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000506 if (SimplifyDemandedBits(I, 0, InputDemandedBits, KnownZero, KnownOne,
507 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000508 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000509 InputDemandedBits = InputDemandedBits.zext(BitWidth);
510 KnownZero = KnownZero.zext(BitWidth);
511 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000512 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
513
Chris Lattner7e044912010-01-04 07:17:19 +0000514 // If the sign bit of the input is known set or clear, then we know the
515 // top bits of the result.
516
517 // If the input sign bit is known zero, or if the NewBits are not demanded
518 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000519 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000520 // Convert to ZExt cast
521 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000522 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000523 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
524 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000525 }
526 break;
527 }
Matthias Braune48484c2015-04-30 22:05:30 +0000528 case Instruction::Add:
529 case Instruction::Sub: {
530 /// If the high-bits of an ADD/SUB are not demanded, then we do not care
531 /// about the high bits of the operands.
Chris Lattner7e044912010-01-04 07:17:19 +0000532 unsigned NLZ = DemandedMask.countLeadingZeros();
Matthias Braune48484c2015-04-30 22:05:30 +0000533 if (NLZ > 0) {
534 // Right fill the mask of bits for this ADD/SUB to demand the most
Chris Lattner7e044912010-01-04 07:17:19 +0000535 // significant bit and all those below it.
Chris Lattner7e044912010-01-04 07:17:19 +0000536 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
Craig Topper07f29152017-03-22 04:03:53 +0000537 if (ShrinkDemandedConstant(I, 0, DemandedFromOps) ||
Craig Topper47596dd2017-03-25 06:52:52 +0000538 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnownZero, LHSKnownOne,
539 Depth + 1) ||
Matthias Braune48484c2015-04-30 22:05:30 +0000540 ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
Craig Topper845033a2017-04-12 16:49:59 +0000541 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnownZero, RHSKnownOne,
Craig Topper47596dd2017-03-25 06:52:52 +0000542 Depth + 1)) {
Matthias Braune48484c2015-04-30 22:05:30 +0000543 // Disable the nsw and nuw flags here: We can no longer guarantee that
544 // we won't wrap after simplification. Removing the nsw/nuw flags is
545 // legal here because the top bit is not demanded.
546 BinaryOperator &BinOP = *cast<BinaryOperator>(I);
547 BinOP.setHasNoSignedWrap(false);
548 BinOP.setHasNoUnsignedWrap(false);
Chris Lattner7e044912010-01-04 07:17:19 +0000549 return I;
David Majnemer7d0e99c2015-04-22 22:42:05 +0000550 }
Craig Topper845033a2017-04-12 16:49:59 +0000551
552 // If we are known to be adding/subtracting zeros to every bit below
553 // the highest demanded bit, we just return the other side.
554 if ((DemandedFromOps & RHSKnownZero) == DemandedFromOps)
555 return I->getOperand(0);
556 // We can't do this with the LHS for subtraction.
557 if (I->getOpcode() == Instruction::Add &&
558 (DemandedFromOps & LHSKnownZero) == DemandedFromOps)
559 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000560 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000561
Craig Topper8fbb74b2017-03-24 22:12:10 +0000562 // Otherwise just hand the add/sub off to computeKnownBits to fill in
563 // the known zeros and ones.
564 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000565 break;
Matthias Braune48484c2015-04-30 22:05:30 +0000566 }
Chris Lattner7e044912010-01-04 07:17:19 +0000567 case Instruction::Shl:
568 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000569 {
570 Value *VarX; ConstantInt *C1;
571 if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
572 Instruction *Shr = cast<Instruction>(I->getOperand(0));
573 Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
574 KnownZero, KnownOne);
575 if (R)
576 return R;
577 }
578 }
579
Chris Lattner768003c2011-02-10 05:09:34 +0000580 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000581 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000582
Chris Lattner768003c2011-02-10 05:09:34 +0000583 // If the shift is NUW/NSW, then it does demand the high bits.
584 ShlOperator *IOp = cast<ShlOperator>(I);
585 if (IOp->hasNoSignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000586 DemandedMaskIn.setHighBits(ShiftAmt+1);
Chris Lattner768003c2011-02-10 05:09:34 +0000587 else if (IOp->hasNoUnsignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000588 DemandedMaskIn.setHighBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000589
Craig Topper47596dd2017-03-25 06:52:52 +0000590 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
591 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000592 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000593 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
594 KnownZero <<= ShiftAmt;
595 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000596 // low bits known zero.
597 if (ShiftAmt)
Craig Topper3a86a042017-03-19 05:49:16 +0000598 KnownZero.setLowBits(ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000599 }
600 break;
601 case Instruction::LShr:
602 // For a logical shift right
603 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000604 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000605
Chris Lattner7e044912010-01-04 07:17:19 +0000606 // Unsigned shift right.
607 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000608
Chris Lattner768003c2011-02-10 05:09:34 +0000609 // If the shift is exact, then it does demand the low bits (and knows that
610 // they are zero).
611 if (cast<LShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000612 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000613
Craig Topper47596dd2017-03-25 06:52:52 +0000614 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
615 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000616 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000617 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Craig Topper885fa122017-03-31 20:01:16 +0000618 KnownZero = KnownZero.lshr(ShiftAmt);
619 KnownOne = KnownOne.lshr(ShiftAmt);
Craig Topper3a86a042017-03-19 05:49:16 +0000620 if (ShiftAmt)
621 KnownZero.setHighBits(ShiftAmt); // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000622 }
623 break;
624 case Instruction::AShr:
625 // If this is an arithmetic shift right and only the low-bit is set, we can
626 // always convert this into a logical shr, even if the shift amount is
627 // variable. The low bit of the shift cannot be an input sign bit unless
628 // the shift amount is >= the size of the datatype, which is undefined.
629 if (DemandedMask == 1) {
630 // Perform the logical shift right.
631 Instruction *NewVal = BinaryOperator::CreateLShr(
632 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000633 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000634 }
Chris Lattner7e044912010-01-04 07:17:19 +0000635
636 // If the sign bit is the only bit demanded by this ashr, then there is no
637 // need to do it, the shift doesn't change the high bit.
638 if (DemandedMask.isSignBit())
639 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000640
Chris Lattner7e044912010-01-04 07:17:19 +0000641 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000642 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000643
Chris Lattner7e044912010-01-04 07:17:19 +0000644 // Signed shift right.
645 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
646 // If any of the "high bits" are demanded, we should set the sign bit as
647 // demanded.
648 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000649 DemandedMaskIn.setBit(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000650
Chris Lattner768003c2011-02-10 05:09:34 +0000651 // If the shift is exact, then it does demand the low bits (and knows that
652 // they are zero).
653 if (cast<AShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000654 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000655
Craig Topper47596dd2017-03-25 06:52:52 +0000656 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
657 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000658 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000659 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000660 // Compute the new bits that are at the top now.
661 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Craig Topper885fa122017-03-31 20:01:16 +0000662 KnownZero = KnownZero.lshr(ShiftAmt);
663 KnownOne = KnownOne.lshr(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000664
Chris Lattner7e044912010-01-04 07:17:19 +0000665 // Handle the sign bits.
666 APInt SignBit(APInt::getSignBit(BitWidth));
667 // Adjust to where it is now in the mask.
Craig Topper885fa122017-03-31 20:01:16 +0000668 SignBit = SignBit.lshr(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000669
Chris Lattner7e044912010-01-04 07:17:19 +0000670 // If the input sign bit is known to be zero, or if none of the top bits
671 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000672 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Chris Lattner7e044912010-01-04 07:17:19 +0000673 (HighBits & ~DemandedMask) == HighBits) {
674 // Perform the logical shift right.
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000675 BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
676 SA, I->getName());
677 NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000678 return InsertNewInstWith(NewVal, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000679 } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
680 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000681 }
682 }
683 break;
684 case Instruction::SRem:
685 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000686 // X % -1 demands all the bits because we don't want to introduce
687 // INT_MIN % -1 (== undef) by accident.
688 if (Rem->isAllOnesValue())
689 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000690 APInt RA = Rem->getValue().abs();
691 if (RA.isPowerOf2()) {
692 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
693 return I->getOperand(0);
694
695 APInt LowBits = RA - 1;
696 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000697 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnownZero, LHSKnownOne,
698 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000699 return I;
700
Duncan Sands3a48b872010-01-28 17:22:42 +0000701 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000702 KnownZero = LHSKnownZero & LowBits;
703 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000704
Duncan Sands3a48b872010-01-28 17:22:42 +0000705 // If LHS is non-negative or has all low bits zero, then the upper bits
706 // are all zero.
707 if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
708 KnownZero |= ~LowBits;
709
710 // If LHS is negative and not all low bits are zero, then the upper bits
711 // are all one.
712 if (LHSKnownOne[BitWidth-1] && ((LHSKnownOne & LowBits) != 0))
713 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000714
Craig Topper4c947752012-12-22 18:09:02 +0000715 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000716 }
717 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000718
719 // The sign bit is the LHS's sign bit, except when the result of the
720 // remainder is zero.
721 if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
Nick Lewyckye4679792011-03-07 01:50:10 +0000722 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000723 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000724 CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000725 // If it's known zero, our sign bit is also zero.
726 if (LHSKnownZero.isNegative())
Craig Topper3a86a042017-03-19 05:49:16 +0000727 KnownZero.setSignBit();
Nick Lewyckye4679792011-03-07 01:50:10 +0000728 }
Chris Lattner7e044912010-01-04 07:17:19 +0000729 break;
730 case Instruction::URem: {
731 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
732 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000733 if (SimplifyDemandedBits(I, 0, AllOnes, KnownZero2, KnownOne2, Depth + 1) ||
734 SimplifyDemandedBits(I, 1, AllOnes, KnownZero2, KnownOne2, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000735 return I;
736
737 unsigned Leaders = KnownZero2.countLeadingOnes();
Chris Lattner7e044912010-01-04 07:17:19 +0000738 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
739 break;
740 }
741 case Instruction::Call:
742 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
743 switch (II->getIntrinsicID()) {
744 default: break;
745 case Intrinsic::bswap: {
746 // If the only bits demanded come from one byte of the bswap result,
747 // just shift the input byte into position to eliminate the bswap.
748 unsigned NLZ = DemandedMask.countLeadingZeros();
749 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000750
Chris Lattner7e044912010-01-04 07:17:19 +0000751 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
752 // we need all the bits down to bit 8. Likewise, round NLZ. If we
753 // have 14 leading zeros, round to 8.
754 NLZ &= ~7;
755 NTZ &= ~7;
756 // If we need exactly one byte, we can do this transformation.
757 if (BitWidth-NLZ-NTZ == 8) {
758 unsigned ResultBit = NTZ;
759 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000760
Chris Lattner7e044912010-01-04 07:17:19 +0000761 // Replace this with either a left or right shift to get the byte into
762 // the right place.
763 Instruction *NewVal;
764 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000765 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000766 ConstantInt::get(I->getType(), InputBit-ResultBit));
767 else
Gabor Greif79430172010-06-24 12:35:13 +0000768 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000769 ConstantInt::get(I->getType(), ResultBit-InputBit));
770 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000771 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000772 }
Craig Topper4c947752012-12-22 18:09:02 +0000773
Chris Lattner7e044912010-01-04 07:17:19 +0000774 // TODO: Could compute known zero/one bits based on the input.
775 break;
776 }
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000777 case Intrinsic::x86_mmx_pmovmskb:
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000778 case Intrinsic::x86_sse_movmsk_ps:
779 case Intrinsic::x86_sse2_movmsk_pd:
780 case Intrinsic::x86_sse2_pmovmskb_128:
781 case Intrinsic::x86_avx_movmsk_ps_256:
782 case Intrinsic::x86_avx_movmsk_pd_256:
783 case Intrinsic::x86_avx2_pmovmskb: {
784 // MOVMSK copies the vector elements' sign bits to the low bits
785 // and zeros the high bits.
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000786 unsigned ArgWidth;
787 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
788 ArgWidth = 8; // Arg is x86_mmx, but treated as <8 x i8>.
789 } else {
790 auto Arg = II->getArgOperand(0);
791 auto ArgType = cast<VectorType>(Arg->getType());
792 ArgWidth = ArgType->getNumElements();
793 }
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000794
795 // If we don't need any of low bits then return zero,
796 // we know that DemandedMask is non-zero already.
797 APInt DemandedElts = DemandedMask.zextOrTrunc(ArgWidth);
798 if (DemandedElts == 0)
799 return ConstantInt::getNullValue(VTy);
800
Ahmed Bougacha17482a52016-04-28 14:36:07 +0000801 // We know that the upper bits are set to zero.
Craig Topper3a86a042017-03-19 05:49:16 +0000802 KnownZero.setBitsFrom(ArgWidth);
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000803 return nullptr;
804 }
Chad Rosierb3628842011-05-26 23:13:19 +0000805 case Intrinsic::x86_sse42_crc32_64_64:
Craig Topper3a86a042017-03-19 05:49:16 +0000806 KnownZero.setBitsFrom(32);
Craig Topperf40110f2014-04-25 05:29:35 +0000807 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000808 }
809 }
Hal Finkel60db0582014-09-07 18:57:58 +0000810 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000811 break;
812 }
Craig Topper4c947752012-12-22 18:09:02 +0000813
Chris Lattner7e044912010-01-04 07:17:19 +0000814 // If the client is only demanding bits that we know, return the known
815 // constant.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000816 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
817 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000818 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000819}
820
Shuxin Yang63e999e2012-12-04 00:04:54 +0000821/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
822/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
823/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
824/// of "C2-C1".
825///
826/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
827/// ..., bn}, without considering the specific value X is holding.
828/// This transformation is legal iff one of following conditions is hold:
829/// 1) All the bit in S are 0, in this case E1 == E2.
830/// 2) We don't care those bits in S, per the input DemandedMask.
831/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
832/// rest bits.
833///
834/// Currently we only test condition 2).
835///
836/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
837/// not successful.
838Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
Benjamin Kramerc321e532016-06-08 19:09:22 +0000839 Instruction *Shl,
840 const APInt &DemandedMask,
841 APInt &KnownZero,
842 APInt &KnownOne) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000843
Benjamin Kramer010f1082013-08-30 14:35:35 +0000844 const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
845 const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
846 if (!ShlOp1 || !ShrOp1)
Craig Topperf40110f2014-04-25 05:29:35 +0000847 return nullptr; // Noop.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000848
849 Value *VarX = Shr->getOperand(0);
850 Type *Ty = VarX->getType();
851 unsigned BitWidth = Ty->getIntegerBitWidth();
852 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000853 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000854
855 unsigned ShlAmt = ShlOp1.getZExtValue();
856 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000857
858 KnownOne.clearAllBits();
Craig Topper3a86a042017-03-19 05:49:16 +0000859 KnownZero.setLowBits(ShlAmt - 1);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000860 KnownZero &= DemandedMask;
861
Benjamin Kramer010f1082013-08-30 14:35:35 +0000862 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
863 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000864
865 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
866 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
867 (BitMask1.ashr(ShrAmt) << ShlAmt);
868
869 if (ShrAmt <= ShlAmt) {
870 BitMask2 <<= (ShlAmt - ShrAmt);
871 } else {
872 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
873 BitMask2.ashr(ShrAmt - ShlAmt);
874 }
875
876 // Check if condition-2 (see the comment to this function) is satified.
877 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
878 if (ShrAmt == ShlAmt)
879 return VarX;
880
881 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000882 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000883
884 BinaryOperator *New;
885 if (ShrAmt < ShlAmt) {
886 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
887 New = BinaryOperator::CreateShl(VarX, Amt);
888 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
889 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
890 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
891 } else {
892 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000893 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
894 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000895 if (cast<BinaryOperator>(Shr)->isExact())
896 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000897 }
898
899 return InsertNewInstWith(New, *Shl);
900 }
901
Craig Topperf40110f2014-04-25 05:29:35 +0000902 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000903}
Chris Lattner7e044912010-01-04 07:17:19 +0000904
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +0000905/// The specified value produces a vector with any number of elements.
906/// DemandedElts contains the set of elements that are actually used by the
907/// caller. This method analyzes which elements of the operand are undef and
908/// returns that information in UndefElts.
Chris Lattner7e044912010-01-04 07:17:19 +0000909///
910/// If the information about demanded elements can be used to simplify the
911/// operation, the operation is simplified, then the resultant value is
912/// returned. This returns null if no change was made.
913Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000914 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000915 unsigned Depth) {
Sanjay Patel9190b4a2016-04-29 20:54:56 +0000916 unsigned VWidth = V->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +0000917 APInt EltMask(APInt::getAllOnesValue(VWidth));
918 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
919
920 if (isa<UndefValue>(V)) {
921 // If the entire vector is undefined, just return this info.
922 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000923 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000924 }
Craig Topper4c947752012-12-22 18:09:02 +0000925
Chris Lattnerb22423c2010-02-08 23:56:03 +0000926 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000927 UndefElts = EltMask;
928 return UndefValue::get(V->getType());
929 }
930
931 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000932
Chris Lattner67058832012-01-25 06:48:06 +0000933 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
934 if (Constant *C = dyn_cast<Constant>(V)) {
935 // Check if this is identity. If so, return 0 since we are not simplifying
936 // anything.
937 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000938 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000939
Chris Lattner229907c2011-07-18 04:54:35 +0000940 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000941 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000942
Chris Lattner67058832012-01-25 06:48:06 +0000943 SmallVector<Constant*, 16> Elts;
944 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000945 if (!DemandedElts[i]) { // If not demanded, set to undef.
946 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000947 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +0000948 continue;
949 }
Craig Topper4c947752012-12-22 18:09:02 +0000950
Chris Lattner67058832012-01-25 06:48:06 +0000951 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000952 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000953
Chris Lattner67058832012-01-25 06:48:06 +0000954 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000955 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000956 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +0000957 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +0000958 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +0000959 }
Chris Lattner67058832012-01-25 06:48:06 +0000960 }
Craig Topper4c947752012-12-22 18:09:02 +0000961
Chris Lattner7e044912010-01-04 07:17:19 +0000962 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +0000963 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +0000964 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000965 }
Craig Topper4c947752012-12-22 18:09:02 +0000966
Chris Lattner7e044912010-01-04 07:17:19 +0000967 // Limit search depth.
968 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +0000969 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000970
Stuart Hastings5bd18b62011-05-17 22:13:31 +0000971 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +0000972 // simplification conservatively assuming that all elements
973 // are needed.
974 if (!V->hasOneUse()) {
975 // Quit if we find multiple users of a non-root value though.
976 // They'll be handled when it's their turn to be visited by
977 // the main instcombine process.
978 if (Depth != 0)
979 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +0000980 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000981
982 // Conservatively assume that all elements are needed.
983 DemandedElts = EltMask;
984 }
Craig Topper4c947752012-12-22 18:09:02 +0000985
Chris Lattner7e044912010-01-04 07:17:19 +0000986 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +0000987 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +0000988
Chris Lattner7e044912010-01-04 07:17:19 +0000989 bool MadeChange = false;
990 APInt UndefElts2(VWidth, 0);
Craig Topper23ebd952016-12-11 08:54:52 +0000991 APInt UndefElts3(VWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +0000992 Value *TmpV;
993 switch (I->getOpcode()) {
994 default: break;
Craig Topper4c947752012-12-22 18:09:02 +0000995
Chris Lattner7e044912010-01-04 07:17:19 +0000996 case Instruction::InsertElement: {
997 // If this is a variable index, we don't know which element it overwrites.
998 // demand exactly the same input as we produce.
999 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +00001000 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +00001001 // Note that we can't propagate undef elt info, because we don't know
1002 // which elt is getting updated.
1003 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001004 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001005 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1006 break;
1007 }
Craig Topper4c947752012-12-22 18:09:02 +00001008
Chris Lattner7e044912010-01-04 07:17:19 +00001009 // If this is inserting an element that isn't demanded, remove this
1010 // insertelement.
1011 unsigned IdxNo = Idx->getZExtValue();
1012 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1013 Worklist.Add(I);
1014 return I->getOperand(0);
1015 }
Craig Topper4c947752012-12-22 18:09:02 +00001016
Chris Lattner7e044912010-01-04 07:17:19 +00001017 // Otherwise, the element inserted overwrites whatever was there, so the
1018 // input demanded set is simpler than the output set.
1019 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001020 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001021 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001022 UndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001023 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1024
1025 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001026 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001027 break;
1028 }
1029 case Instruction::ShuffleVector: {
1030 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Craig Topper2e18bcf2016-12-29 04:24:32 +00001031 unsigned LHSVWidth =
1032 Shuffle->getOperand(0)->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +00001033 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1034 for (unsigned i = 0; i < VWidth; i++) {
1035 if (DemandedElts[i]) {
1036 unsigned MaskVal = Shuffle->getMaskValue(i);
1037 if (MaskVal != -1u) {
1038 assert(MaskVal < LHSVWidth * 2 &&
1039 "shufflevector mask index out of range!");
1040 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001041 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001042 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001043 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001044 }
1045 }
1046 }
1047
Alexey Bataevfee90782016-09-23 09:14:08 +00001048 APInt LHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001049 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001050 LHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001051 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1052
Alexey Bataevfee90782016-09-23 09:14:08 +00001053 APInt RHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001054 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001055 RHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001056 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1057
1058 bool NewUndefElts = false;
Alexey Bataev793c9462016-09-26 13:18:59 +00001059 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1060 unsigned RHSIdx = -1u, RHSValIdx = -1u;
Alexey Bataevfee90782016-09-23 09:14:08 +00001061 bool LHSUniform = true;
1062 bool RHSUniform = true;
Chris Lattner7e044912010-01-04 07:17:19 +00001063 for (unsigned i = 0; i < VWidth; i++) {
1064 unsigned MaskVal = Shuffle->getMaskValue(i);
1065 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001066 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001067 } else if (!DemandedElts[i]) {
1068 NewUndefElts = true;
1069 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001070 } else if (MaskVal < LHSVWidth) {
Alexey Bataevfee90782016-09-23 09:14:08 +00001071 if (LHSUndefElts[MaskVal]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001072 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001073 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001074 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001075 LHSIdx = LHSIdx == -1u ? i : LHSVWidth;
1076 LHSValIdx = LHSValIdx == -1u ? MaskVal : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001077 LHSUniform = LHSUniform && (MaskVal == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001078 }
1079 } else {
Alexey Bataevfee90782016-09-23 09:14:08 +00001080 if (RHSUndefElts[MaskVal - LHSVWidth]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001081 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001082 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001083 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001084 RHSIdx = RHSIdx == -1u ? i : LHSVWidth;
1085 RHSValIdx = RHSValIdx == -1u ? MaskVal - LHSVWidth : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001086 RHSUniform = RHSUniform && (MaskVal - LHSVWidth == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001087 }
1088 }
1089 }
1090
Alexey Bataevfee90782016-09-23 09:14:08 +00001091 // Try to transform shuffle with constant vector and single element from
1092 // this constant vector to single insertelement instruction.
1093 // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
1094 // insertelement V, C[ci], ci-n
1095 if (LHSVWidth == Shuffle->getType()->getNumElements()) {
1096 Value *Op = nullptr;
1097 Constant *Value = nullptr;
1098 unsigned Idx = -1u;
1099
Craig Topper62f06e22016-12-29 05:38:31 +00001100 // Find constant vector with the single element in shuffle (LHS or RHS).
Alexey Bataevfee90782016-09-23 09:14:08 +00001101 if (LHSIdx < LHSVWidth && RHSUniform) {
1102 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1103 Op = Shuffle->getOperand(1);
Alexey Bataev793c9462016-09-26 13:18:59 +00001104 Value = CV->getOperand(LHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001105 Idx = LHSIdx;
1106 }
1107 }
1108 if (RHSIdx < LHSVWidth && LHSUniform) {
1109 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1110 Op = Shuffle->getOperand(0);
Alexey Bataev793c9462016-09-26 13:18:59 +00001111 Value = CV->getOperand(RHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001112 Idx = RHSIdx;
1113 }
1114 }
1115 // Found constant vector with single element - convert to insertelement.
1116 if (Op && Value) {
1117 Instruction *New = InsertElementInst::Create(
1118 Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx),
1119 Shuffle->getName());
1120 InsertNewInstWith(New, *Shuffle);
1121 return New;
1122 }
1123 }
Chris Lattner7e044912010-01-04 07:17:19 +00001124 if (NewUndefElts) {
1125 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001126 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001127 for (unsigned i = 0; i < VWidth; ++i) {
1128 if (UndefElts[i])
1129 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1130 else
1131 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1132 Shuffle->getMaskValue(i)));
1133 }
1134 I->setOperand(2, ConstantVector::get(Elts));
1135 MadeChange = true;
1136 }
1137 break;
1138 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001139 case Instruction::Select: {
1140 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1141 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1142 for (unsigned i = 0; i < VWidth; i++) {
Andrea Di Biagio40f59e42015-10-06 10:34:53 +00001143 Constant *CElt = CV->getAggregateElement(i);
1144 // Method isNullValue always returns false when called on a
1145 // ConstantExpr. If CElt is a ConstantExpr then skip it in order to
1146 // to avoid propagating incorrect information.
1147 if (isa<ConstantExpr>(CElt))
1148 continue;
1149 if (CElt->isNullValue())
Pete Cooperabc13af2012-07-26 23:10:24 +00001150 LeftDemanded.clearBit(i);
1151 else
1152 RightDemanded.clearBit(i);
1153 }
1154 }
1155
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001156 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded, UndefElts,
1157 Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001158 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1159
1160 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001161 UndefElts2, Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001162 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001163
Pete Cooperabc13af2012-07-26 23:10:24 +00001164 // Output elements are undefined if both are undefined.
1165 UndefElts &= UndefElts2;
1166 break;
1167 }
Chris Lattner7e044912010-01-04 07:17:19 +00001168 case Instruction::BitCast: {
1169 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001170 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001171 if (!VTy) break;
1172 unsigned InVWidth = VTy->getNumElements();
1173 APInt InputDemandedElts(InVWidth, 0);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001174 UndefElts2 = APInt(InVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001175 unsigned Ratio;
1176
1177 if (VWidth == InVWidth) {
1178 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1179 // elements as are demanded of us.
1180 Ratio = 1;
1181 InputDemandedElts = DemandedElts;
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001182 } else if ((VWidth % InVWidth) == 0) {
1183 // If the number of elements in the output is a multiple of the number of
1184 // elements in the input then an input element is live if any of the
1185 // corresponding output elements are live.
1186 Ratio = VWidth / InVWidth;
1187 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Chris Lattner7e044912010-01-04 07:17:19 +00001188 if (DemandedElts[OutIdx])
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001189 InputDemandedElts.setBit(OutIdx / Ratio);
1190 } else if ((InVWidth % VWidth) == 0) {
1191 // If the number of elements in the input is a multiple of the number of
1192 // elements in the output then an input element is live if the
1193 // corresponding output element is live.
1194 Ratio = InVWidth / VWidth;
Chris Lattner7e044912010-01-04 07:17:19 +00001195 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001196 if (DemandedElts[InIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001197 InputDemandedElts.setBit(InIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001198 } else {
1199 // Unsupported so far.
1200 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001201 }
Craig Topper4c947752012-12-22 18:09:02 +00001202
Chris Lattner7e044912010-01-04 07:17:19 +00001203 // div/rem demand all inputs, because they don't want divide by zero.
1204 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001205 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001206 if (TmpV) {
1207 I->setOperand(0, TmpV);
1208 MadeChange = true;
1209 }
Craig Topper4c947752012-12-22 18:09:02 +00001210
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001211 if (VWidth == InVWidth) {
1212 UndefElts = UndefElts2;
1213 } else if ((VWidth % InVWidth) == 0) {
1214 // If the number of elements in the output is a multiple of the number of
1215 // elements in the input then an output element is undef if the
1216 // corresponding input element is undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001217 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001218 if (UndefElts2[OutIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001219 UndefElts.setBit(OutIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001220 } else if ((InVWidth % VWidth) == 0) {
1221 // If the number of elements in the input is a multiple of the number of
1222 // elements in the output then an output element is undef if all of the
1223 // corresponding input elements are undef.
1224 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1225 APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
1226 if (SubUndef.countPopulation() == Ratio)
1227 UndefElts.setBit(OutIdx);
1228 }
1229 } else {
Chris Lattner7e044912010-01-04 07:17:19 +00001230 llvm_unreachable("Unimp");
Chris Lattner7e044912010-01-04 07:17:19 +00001231 }
1232 break;
1233 }
1234 case Instruction::And:
1235 case Instruction::Or:
1236 case Instruction::Xor:
1237 case Instruction::Add:
1238 case Instruction::Sub:
1239 case Instruction::Mul:
1240 // div/rem demand all inputs, because they don't want divide by zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001241 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1242 Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001243 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1244 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001245 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001246 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001247
Chris Lattner7e044912010-01-04 07:17:19 +00001248 // Output elements are undefined if both are undefined. Consider things
1249 // like undef&0. The result is known zero, not undef.
1250 UndefElts &= UndefElts2;
1251 break;
Pete Coopere807e452012-07-26 22:37:04 +00001252 case Instruction::FPTrunc:
1253 case Instruction::FPExt:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001254 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1255 Depth + 1);
Pete Coopere807e452012-07-26 22:37:04 +00001256 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1257 break;
Craig Topper4c947752012-12-22 18:09:02 +00001258
Chris Lattner7e044912010-01-04 07:17:19 +00001259 case Instruction::Call: {
1260 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1261 if (!II) break;
1262 switch (II->getIntrinsicID()) {
1263 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001264
Craig Topper7fc6d342016-12-11 22:32:38 +00001265 case Intrinsic::x86_xop_vfrcz_ss:
1266 case Intrinsic::x86_xop_vfrcz_sd:
1267 // The instructions for these intrinsics are speced to zero upper bits not
1268 // pass them through like other scalar intrinsics. So we shouldn't just
1269 // use Arg0 if DemandedElts[0] is clear like we do for other intrinsics.
1270 // Instead we should return a zero vector.
Craig Topper1a8a3372016-12-29 03:30:17 +00001271 if (!DemandedElts[0]) {
1272 Worklist.Add(II);
Craig Topper7fc6d342016-12-11 22:32:38 +00001273 return ConstantAggregateZero::get(II->getType());
Craig Topper1a8a3372016-12-29 03:30:17 +00001274 }
Craig Topper7fc6d342016-12-11 22:32:38 +00001275
Craig Topperac75bca2016-12-13 07:45:45 +00001276 // Only the lower element is used.
1277 DemandedElts = 1;
Craig Topper7fc6d342016-12-11 22:32:38 +00001278 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1279 UndefElts, Depth + 1);
1280 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperac75bca2016-12-13 07:45:45 +00001281
1282 // Only the lower element is undefined. The high elements are zero.
1283 UndefElts = UndefElts[0];
Craig Topper7fc6d342016-12-11 22:32:38 +00001284 break;
1285
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001286 // Unary scalar-as-vector operations that work column-wise.
Simon Pilgrim83020942016-04-24 18:23:14 +00001287 case Intrinsic::x86_sse_rcp_ss:
1288 case Intrinsic::x86_sse_rsqrt_ss:
1289 case Intrinsic::x86_sse_sqrt_ss:
1290 case Intrinsic::x86_sse2_sqrt_sd:
Simon Pilgrim83020942016-04-24 18:23:14 +00001291 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1292 UndefElts, Depth + 1);
1293 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1294
1295 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001296 if (!DemandedElts[0]) {
1297 Worklist.Add(II);
Simon Pilgrim83020942016-04-24 18:23:14 +00001298 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001299 }
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001300 // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions
1301 // checks).
Simon Pilgrim83020942016-04-24 18:23:14 +00001302 break;
1303
Craig Toppera0372de2016-12-14 03:17:27 +00001304 // Binary scalar-as-vector operations that work column-wise. The high
1305 // elements come from operand 0. The low element is a function of both
1306 // operands.
Chris Lattner7e044912010-01-04 07:17:19 +00001307 case Intrinsic::x86_sse_min_ss:
1308 case Intrinsic::x86_sse_max_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001309 case Intrinsic::x86_sse_cmp_ss:
Chris Lattner7e044912010-01-04 07:17:19 +00001310 case Intrinsic::x86_sse2_min_sd:
1311 case Intrinsic::x86_sse2_max_sd:
Craig Toppera0372de2016-12-14 03:17:27 +00001312 case Intrinsic::x86_sse2_cmp_sd: {
1313 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1314 UndefElts, Depth + 1);
1315 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1316
1317 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001318 if (!DemandedElts[0]) {
1319 Worklist.Add(II);
Craig Toppera0372de2016-12-14 03:17:27 +00001320 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001321 }
Craig Toppera0372de2016-12-14 03:17:27 +00001322
1323 // Only lower element is used for operand 1.
1324 DemandedElts = 1;
1325 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1326 UndefElts2, Depth + 1);
1327 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1328
1329 // Lower element is undefined if both lower elements are undefined.
1330 // Consider things like undef&0. The result is known zero, not undef.
1331 if (!UndefElts2[0])
1332 UndefElts.clearBit(0);
1333
1334 break;
1335 }
1336
Craig Toppereb6a20e2016-12-14 03:17:30 +00001337 // Binary scalar-as-vector operations that work column-wise. The high
1338 // elements come from operand 0 and the low element comes from operand 1.
Simon Pilgrim83020942016-04-24 18:23:14 +00001339 case Intrinsic::x86_sse41_round_ss:
Craig Toppereb6a20e2016-12-14 03:17:30 +00001340 case Intrinsic::x86_sse41_round_sd: {
1341 // Don't use the low element of operand 0.
1342 APInt DemandedElts2 = DemandedElts;
1343 DemandedElts2.clearBit(0);
1344 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001345 UndefElts, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001346 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001347
1348 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001349 if (!DemandedElts[0]) {
1350 Worklist.Add(II);
Craig Toppereb6a20e2016-12-14 03:17:30 +00001351 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001352 }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001353
1354 // Only lower element is used for operand 1.
1355 DemandedElts = 1;
Gabor Greife23efee2010-06-28 16:45:00 +00001356 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001357 UndefElts2, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001358 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001359
Craig Toppereb6a20e2016-12-14 03:17:30 +00001360 // Take the high undef elements from operand 0 and take the lower element
1361 // from operand 1.
1362 UndefElts.clearBit(0);
1363 UndefElts |= UndefElts2[0];
Chris Lattner7e044912010-01-04 07:17:19 +00001364 break;
Craig Toppereb6a20e2016-12-14 03:17:30 +00001365 }
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001366
Craig Topperdfd268d2016-12-14 05:43:05 +00001367 // Three input scalar-as-vector operations that work column-wise. The high
1368 // elements come from operand 0 and the low element is a function of all
1369 // three inputs.
Craig Topper268b3ab2016-12-14 06:06:58 +00001370 case Intrinsic::x86_avx512_mask_add_ss_round:
1371 case Intrinsic::x86_avx512_mask_div_ss_round:
1372 case Intrinsic::x86_avx512_mask_mul_ss_round:
1373 case Intrinsic::x86_avx512_mask_sub_ss_round:
1374 case Intrinsic::x86_avx512_mask_max_ss_round:
1375 case Intrinsic::x86_avx512_mask_min_ss_round:
1376 case Intrinsic::x86_avx512_mask_add_sd_round:
1377 case Intrinsic::x86_avx512_mask_div_sd_round:
1378 case Intrinsic::x86_avx512_mask_mul_sd_round:
1379 case Intrinsic::x86_avx512_mask_sub_sd_round:
1380 case Intrinsic::x86_avx512_mask_max_sd_round:
1381 case Intrinsic::x86_avx512_mask_min_sd_round:
Craig Topper23ebd952016-12-11 08:54:52 +00001382 case Intrinsic::x86_fma_vfmadd_ss:
1383 case Intrinsic::x86_fma_vfmsub_ss:
1384 case Intrinsic::x86_fma_vfnmadd_ss:
1385 case Intrinsic::x86_fma_vfnmsub_ss:
1386 case Intrinsic::x86_fma_vfmadd_sd:
1387 case Intrinsic::x86_fma_vfmsub_sd:
1388 case Intrinsic::x86_fma_vfnmadd_sd:
1389 case Intrinsic::x86_fma_vfnmsub_sd:
Craig Topperab5f3552016-12-15 03:49:45 +00001390 case Intrinsic::x86_avx512_mask_vfmadd_ss:
1391 case Intrinsic::x86_avx512_mask_vfmadd_sd:
1392 case Intrinsic::x86_avx512_maskz_vfmadd_ss:
1393 case Intrinsic::x86_avx512_maskz_vfmadd_sd:
Craig Topper23ebd952016-12-11 08:54:52 +00001394 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1395 UndefElts, Depth + 1);
1396 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperdfd268d2016-12-14 05:43:05 +00001397
1398 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001399 if (!DemandedElts[0]) {
1400 Worklist.Add(II);
Craig Topperdfd268d2016-12-14 05:43:05 +00001401 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001402 }
Craig Topperdfd268d2016-12-14 05:43:05 +00001403
1404 // Only lower element is used for operand 1 and 2.
1405 DemandedElts = 1;
Craig Topper23ebd952016-12-11 08:54:52 +00001406 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1407 UndefElts2, Depth + 1);
1408 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1409 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1410 UndefElts3, Depth + 1);
1411 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1412
Craig Topperdfd268d2016-12-14 05:43:05 +00001413 // Lower element is undefined if all three lower elements are undefined.
1414 // Consider things like undef&0. The result is known zero, not undef.
1415 if (!UndefElts2[0] || !UndefElts3[0])
1416 UndefElts.clearBit(0);
Craig Topper23ebd952016-12-11 08:54:52 +00001417
Craig Topper23ebd952016-12-11 08:54:52 +00001418 break;
1419
Craig Topperab5f3552016-12-15 03:49:45 +00001420 case Intrinsic::x86_avx512_mask3_vfmadd_ss:
1421 case Intrinsic::x86_avx512_mask3_vfmadd_sd:
1422 case Intrinsic::x86_avx512_mask3_vfmsub_ss:
1423 case Intrinsic::x86_avx512_mask3_vfmsub_sd:
1424 case Intrinsic::x86_avx512_mask3_vfnmsub_ss:
1425 case Intrinsic::x86_avx512_mask3_vfnmsub_sd:
1426 // These intrinsics get the passthru bits from operand 2.
1427 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1428 UndefElts, Depth + 1);
1429 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1430
1431 // If lowest element of a scalar op isn't used then use Arg2.
Craig Topper1a8a3372016-12-29 03:30:17 +00001432 if (!DemandedElts[0]) {
1433 Worklist.Add(II);
Craig Topperab5f3552016-12-15 03:49:45 +00001434 return II->getArgOperand(2);
Craig Topper1a8a3372016-12-29 03:30:17 +00001435 }
Craig Topperab5f3552016-12-15 03:49:45 +00001436
1437 // Only lower element is used for operand 0 and 1.
1438 DemandedElts = 1;
1439 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1440 UndefElts2, Depth + 1);
1441 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1442 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1443 UndefElts3, Depth + 1);
1444 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1445
1446 // Lower element is undefined if all three lower elements are undefined.
1447 // Consider things like undef&0. The result is known zero, not undef.
1448 if (!UndefElts2[0] || !UndefElts3[0])
1449 UndefElts.clearBit(0);
1450
1451 break;
1452
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001453 case Intrinsic::x86_sse2_pmulu_dq:
1454 case Intrinsic::x86_sse41_pmuldq:
1455 case Intrinsic::x86_avx2_pmul_dq:
Craig Topper72f2d4e2016-12-27 05:30:09 +00001456 case Intrinsic::x86_avx2_pmulu_dq:
1457 case Intrinsic::x86_avx512_pmul_dq_512:
1458 case Intrinsic::x86_avx512_pmulu_dq_512: {
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001459 Value *Op0 = II->getArgOperand(0);
1460 Value *Op1 = II->getArgOperand(1);
1461 unsigned InnerVWidth = Op0->getType()->getVectorNumElements();
1462 assert((VWidth * 2) == InnerVWidth && "Unexpected input size");
1463
1464 APInt InnerDemandedElts(InnerVWidth, 0);
1465 for (unsigned i = 0; i != VWidth; ++i)
1466 if (DemandedElts[i])
1467 InnerDemandedElts.setBit(i * 2);
1468
1469 UndefElts2 = APInt(InnerVWidth, 0);
1470 TmpV = SimplifyDemandedVectorElts(Op0, InnerDemandedElts, UndefElts2,
1471 Depth + 1);
1472 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1473
1474 UndefElts3 = APInt(InnerVWidth, 0);
1475 TmpV = SimplifyDemandedVectorElts(Op1, InnerDemandedElts, UndefElts3,
1476 Depth + 1);
1477 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1478
1479 break;
1480 }
1481
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001482 case Intrinsic::x86_sse2_packssdw_128:
1483 case Intrinsic::x86_sse2_packsswb_128:
1484 case Intrinsic::x86_sse2_packuswb_128:
1485 case Intrinsic::x86_sse41_packusdw:
1486 case Intrinsic::x86_avx2_packssdw:
1487 case Intrinsic::x86_avx2_packsswb:
1488 case Intrinsic::x86_avx2_packusdw:
Craig Topper3731f4d2017-02-16 07:35:23 +00001489 case Intrinsic::x86_avx2_packuswb:
1490 case Intrinsic::x86_avx512_packssdw_512:
1491 case Intrinsic::x86_avx512_packsswb_512:
1492 case Intrinsic::x86_avx512_packusdw_512:
1493 case Intrinsic::x86_avx512_packuswb_512: {
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001494 auto *Ty0 = II->getArgOperand(0)->getType();
1495 unsigned InnerVWidth = Ty0->getVectorNumElements();
1496 assert(VWidth == (InnerVWidth * 2) && "Unexpected input size");
1497
1498 unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128;
1499 unsigned VWidthPerLane = VWidth / NumLanes;
1500 unsigned InnerVWidthPerLane = InnerVWidth / NumLanes;
1501
1502 // Per lane, pack the elements of the first input and then the second.
1503 // e.g.
1504 // v8i16 PACK(v4i32 X, v4i32 Y) - (X[0..3],Y[0..3])
1505 // v32i8 PACK(v16i16 X, v16i16 Y) - (X[0..7],Y[0..7]),(X[8..15],Y[8..15])
1506 for (int OpNum = 0; OpNum != 2; ++OpNum) {
1507 APInt OpDemandedElts(InnerVWidth, 0);
1508 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1509 unsigned LaneIdx = Lane * VWidthPerLane;
1510 for (unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) {
1511 unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum;
1512 if (DemandedElts[Idx])
1513 OpDemandedElts.setBit((Lane * InnerVWidthPerLane) + Elt);
1514 }
1515 }
1516
1517 // Demand elements from the operand.
1518 auto *Op = II->getArgOperand(OpNum);
1519 APInt OpUndefElts(InnerVWidth, 0);
1520 TmpV = SimplifyDemandedVectorElts(Op, OpDemandedElts, OpUndefElts,
1521 Depth + 1);
1522 if (TmpV) {
1523 II->setArgOperand(OpNum, TmpV);
1524 MadeChange = true;
1525 }
1526
1527 // Pack the operand's UNDEF elements, one lane at a time.
1528 OpUndefElts = OpUndefElts.zext(VWidth);
1529 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1530 APInt LaneElts = OpUndefElts.lshr(InnerVWidthPerLane * Lane);
1531 LaneElts = LaneElts.getLoBits(InnerVWidthPerLane);
1532 LaneElts = LaneElts.shl(InnerVWidthPerLane * (2 * Lane + OpNum));
1533 UndefElts |= LaneElts;
1534 }
1535 }
1536 break;
1537 }
1538
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001539 // PSHUFB
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001540 case Intrinsic::x86_ssse3_pshuf_b_128:
1541 case Intrinsic::x86_avx2_pshuf_b:
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001542 case Intrinsic::x86_avx512_pshuf_b_512:
1543 // PERMILVAR
1544 case Intrinsic::x86_avx_vpermilvar_ps:
1545 case Intrinsic::x86_avx_vpermilvar_ps_256:
1546 case Intrinsic::x86_avx512_vpermilvar_ps_512:
1547 case Intrinsic::x86_avx_vpermilvar_pd:
1548 case Intrinsic::x86_avx_vpermilvar_pd_256:
Simon Pilgrimfe2c0ed2017-01-18 14:47:49 +00001549 case Intrinsic::x86_avx512_vpermilvar_pd_512:
1550 // PERMV
1551 case Intrinsic::x86_avx2_permd:
1552 case Intrinsic::x86_avx2_permps: {
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001553 Value *Op1 = II->getArgOperand(1);
1554 TmpV = SimplifyDemandedVectorElts(Op1, DemandedElts, UndefElts,
1555 Depth + 1);
1556 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1557 break;
1558 }
1559
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001560 // SSE4A instructions leave the upper 64-bits of the 128-bit result
1561 // in an undefined state.
1562 case Intrinsic::x86_sse4a_extrq:
1563 case Intrinsic::x86_sse4a_extrqi:
1564 case Intrinsic::x86_sse4a_insertq:
1565 case Intrinsic::x86_sse4a_insertqi:
Craig Topper3a86a042017-03-19 05:49:16 +00001566 UndefElts.setHighBits(VWidth / 2);
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001567 break;
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001568 case Intrinsic::amdgcn_buffer_load:
1569 case Intrinsic::amdgcn_buffer_load_format: {
Craig Topperd33ee1b2017-04-03 16:34:59 +00001570 if (VWidth == 1 || !DemandedElts.isMask())
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001571 return nullptr;
1572
1573 // TODO: Handle 3 vectors when supported in code gen.
1574 unsigned NewNumElts = PowerOf2Ceil(DemandedElts.countTrailingOnes());
1575 if (NewNumElts == VWidth)
1576 return nullptr;
1577
1578 Module *M = II->getParent()->getParent()->getParent();
1579 Type *EltTy = V->getType()->getVectorElementType();
1580
1581 Type *NewTy = (NewNumElts == 1) ? EltTy :
1582 VectorType::get(EltTy, NewNumElts);
1583
1584 Function *NewIntrin = Intrinsic::getDeclaration(M, II->getIntrinsicID(),
1585 NewTy);
1586
1587 SmallVector<Value *, 5> Args;
1588 for (unsigned I = 0, E = II->getNumArgOperands(); I != E; ++I)
1589 Args.push_back(II->getArgOperand(I));
1590
Matt Arsenaulta3bdd8f2017-03-10 05:25:49 +00001591 IRBuilderBase::InsertPointGuard Guard(*Builder);
1592 Builder->SetInsertPoint(II);
1593
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001594 CallInst *NewCall = Builder->CreateCall(NewIntrin, Args);
1595 NewCall->takeName(II);
1596 NewCall->copyMetadata(*II);
1597 if (NewNumElts == 1) {
1598 return Builder->CreateInsertElement(UndefValue::get(V->getType()),
1599 NewCall, static_cast<uint64_t>(0));
1600 }
1601
1602 SmallVector<uint32_t, 8> EltMask;
1603 for (unsigned I = 0; I < VWidth; ++I)
1604 EltMask.push_back(I);
1605
1606 Value *Shuffle = Builder->CreateShuffleVector(
1607 NewCall, UndefValue::get(NewTy), EltMask);
1608
1609 MadeChange = true;
1610 return Shuffle;
1611 }
Chris Lattner7e044912010-01-04 07:17:19 +00001612 }
1613 break;
1614 }
1615 }
Craig Topperf40110f2014-04-25 05:29:35 +00001616 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001617}