blob: bef2532da972f871d069025c7b0f6c76419a0c7d [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"
Craig Topperb45eabc2017-04-26 16:39:58 +000019#include "llvm/Support/KnownBits.h"
Chris Lattner7e044912010-01-04 07:17:19 +000020
21using namespace llvm;
Shuxin Yang63e999e2012-12-04 00:04:54 +000022using namespace llvm::PatternMatch;
Chris Lattner7e044912010-01-04 07:17:19 +000023
Chandler Carruth964daaa2014-04-22 02:55:47 +000024#define DEBUG_TYPE "instcombine"
25
Nicolai Haehnleb29ee702018-06-21 13:37:31 +000026namespace {
27
28struct AMDGPUImageDMaskIntrinsic {
29 unsigned Intr;
30};
31
32#define GET_AMDGPUImageDMaskIntrinsicTable_IMPL
33#include "InstCombineTables.inc"
34
35} // end anonymous namespace
36
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000037/// Check to see if the specified operand of the specified instruction is a
38/// constant integer. If so, check to see if there are any bits set in the
39/// constant that are not demanded. If so, shrink the constant and return true.
Craig Topper4c947752012-12-22 18:09:02 +000040static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
Craig Topper358cd9a2017-04-20 23:58:27 +000041 const APInt &Demanded) {
Chris Lattner7e044912010-01-04 07:17:19 +000042 assert(I && "No instruction?");
43 assert(OpNo < I->getNumOperands() && "Operand index too large");
44
Sanjay Patelae3b43e2017-02-09 21:43:06 +000045 // The operand must be a constant integer or splat integer.
46 Value *Op = I->getOperand(OpNo);
47 const APInt *C;
48 if (!match(Op, m_APInt(C)))
49 return false;
Chris Lattner7e044912010-01-04 07:17:19 +000050
51 // If there are no bits set that aren't demanded, nothing to do.
Craig Toppera8129a12017-04-20 16:17:13 +000052 if (C->isSubsetOf(Demanded))
Chris Lattner7e044912010-01-04 07:17:19 +000053 return false;
54
55 // This instruction is producing bits that are not demanded. Shrink the RHS.
Craig Topper358cd9a2017-04-20 23:58:27 +000056 I->setOperand(OpNo, ConstantInt::get(Op->getType(), *C & Demanded));
David Majnemer42b83a52014-08-22 07:56:32 +000057
Chris Lattner7e044912010-01-04 07:17:19 +000058 return true;
59}
60
61
62
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000063/// Inst is an integer instruction that SimplifyDemandedBits knows about. See if
64/// the instruction has any properties that allow us to simplify its operands.
Chris Lattner7e044912010-01-04 07:17:19 +000065bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
66 unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
Craig Topperb45eabc2017-04-26 16:39:58 +000067 KnownBits Known(BitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +000068 APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
Craig Topper4c947752012-12-22 18:09:02 +000069
Craig Topperb45eabc2017-04-26 16:39:58 +000070 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, Known,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000071 0, &Inst);
Craig Topperf40110f2014-04-25 05:29:35 +000072 if (!V) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000073 if (V == &Inst) return true;
Sanjay Patel4b198802016-02-01 22:23:39 +000074 replaceInstUsesWith(Inst, V);
Chris Lattner7e044912010-01-04 07:17:19 +000075 return true;
76}
77
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000078/// This form of SimplifyDemandedBits simplifies the specified instruction
79/// operand if possible, updating it in place. It returns true if it made any
80/// change and false otherwise.
Craig Topper47596dd2017-03-25 06:52:52 +000081bool InstCombiner::SimplifyDemandedBits(Instruction *I, unsigned OpNo,
82 const APInt &DemandedMask,
Craig Topperb45eabc2017-04-26 16:39:58 +000083 KnownBits &Known,
Chris Lattner7e044912010-01-04 07:17:19 +000084 unsigned Depth) {
Craig Topper47596dd2017-03-25 06:52:52 +000085 Use &U = I->getOperandUse(OpNo);
Craig Topperb45eabc2017-04-26 16:39:58 +000086 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, Known,
87 Depth, I);
Craig Topperf40110f2014-04-25 05:29:35 +000088 if (!NewVal) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000089 U = NewVal;
90 return true;
91}
92
93
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000094/// This function attempts to replace V with a simpler value based on the
95/// demanded bits. When this function is called, it is known that only the bits
96/// set in DemandedMask of the result of V are ever used downstream.
97/// Consequently, depending on the mask and V, it may be possible to replace V
98/// with a constant or one of its operands. In such cases, this function does
99/// the replacement and returns true. In all other cases, it returns false after
100/// analyzing the expression and setting KnownOne and known to be one in the
Craig Topperb45eabc2017-04-26 16:39:58 +0000101/// expression. Known.Zero contains all the bits that are known to be zero in
102/// the expression. These are provided to potentially allow the caller (which
103/// might recursively be SimplifyDemandedBits itself) to simplify the
104/// expression.
105/// Known.One and Known.Zero always follow the invariant that:
106/// Known.One & Known.Zero == 0.
107/// That is, a bit can't be both 1 and 0. Note that the bits in Known.One and
108/// Known.Zero may only be accurate for those bits set in DemandedMask. Note
109/// also that the bitwidth of V, DemandedMask, Known.Zero and Known.One must all
110/// be the same.
Chris Lattner7e044912010-01-04 07:17:19 +0000111///
112/// This returns null if it did not change anything and it permits no
113/// simplification. This returns V itself if it did some simplification of V's
114/// operands based on the information about what bits are demanded. This returns
115/// some other non-null value if it found out that V is equal to another value
116/// in the context where the specified bits are demanded, but not for all users.
117Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
Craig Topperb45eabc2017-04-26 16:39:58 +0000118 KnownBits &Known, unsigned Depth,
Hal Finkel60db0582014-09-07 18:57:58 +0000119 Instruction *CxtI) {
Craig Toppere73658d2014-04-28 04:05:08 +0000120 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000121 assert(Depth <= 6 && "Limit Search Depth");
122 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000123 Type *VTy = V->getType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000124 assert(
125 (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
Craig Topperb45eabc2017-04-26 16:39:58 +0000126 Known.getBitWidth() == BitWidth &&
127 "Value *V, DemandedMask and Known must have same BitWidth");
Craig Topper83dc1c62017-04-20 16:14:58 +0000128
129 if (isa<Constant>(V)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000130 computeKnownBits(V, Known, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000131 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000132 }
133
Craig Topperf0aeee02017-05-05 17:36:09 +0000134 Known.resetAll();
Craig Topper73ba1c82017-06-07 07:40:37 +0000135 if (DemandedMask.isNullValue()) // Not demanding any bits from V.
Chris Lattner7e044912010-01-04 07:17:19 +0000136 return UndefValue::get(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000137
Chris Lattner7e044912010-01-04 07:17:19 +0000138 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000139 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000140
Chris Lattner7e044912010-01-04 07:17:19 +0000141 Instruction *I = dyn_cast<Instruction>(V);
142 if (!I) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000143 computeKnownBits(V, Known, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000144 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000145 }
146
147 // If there are multiple uses of this value and we aren't at the root, then
148 // we can't do any simplifications of the operands, because DemandedMask
149 // only reflects the bits demanded by *one* of the users.
Craig Topper7603dce2017-04-25 16:48:19 +0000150 if (Depth != 0 && !I->hasOneUse())
Craig Topperb45eabc2017-04-26 16:39:58 +0000151 return SimplifyMultipleUseDemandedBits(I, DemandedMask, Known, Depth, CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000152
Craig Topperb45eabc2017-04-26 16:39:58 +0000153 KnownBits LHSKnown(BitWidth), RHSKnown(BitWidth);
Craig Topperb0076fe2017-04-12 18:05:21 +0000154
Chris Lattner7e044912010-01-04 07:17:19 +0000155 // If this is the root being simplified, allow it to have multiple uses,
156 // just set the DemandedMask to all bits so that we can try to simplify the
157 // operands. This allows visitTruncInst (for example) to simplify the
158 // operand of a trunc without duplicating all the logic below.
159 if (Depth == 0 && !V->hasOneUse())
Craig Toppere06b6bc2017-04-04 05:03:02 +0000160 DemandedMask.setAllBits();
Craig Topper4c947752012-12-22 18:09:02 +0000161
Chris Lattner7e044912010-01-04 07:17:19 +0000162 switch (I->getOpcode()) {
163 default:
Craig Topperb45eabc2017-04-26 16:39:58 +0000164 computeKnownBits(I, Known, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000165 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000166 case Instruction::And: {
Chris Lattner7e044912010-01-04 07:17:19 +0000167 // If either the LHS or the RHS are Zero, the result is zero.
Craig Topperb45eabc2017-04-26 16:39:58 +0000168 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
169 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.Zero, LHSKnown,
170 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000171 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000172 assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
173 assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000174
Craig Topper9a458cd2017-04-14 22:34:14 +0000175 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000176 APInt IKnownZero = RHSKnown.Zero | LHSKnown.Zero;
Craig Topper9a458cd2017-04-14 22:34:14 +0000177 // Output known-1 bits are only known if set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000178 APInt IKnownOne = RHSKnown.One & LHSKnown.One;
Craig Topper9a458cd2017-04-14 22:34:14 +0000179
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000180 // If the client is only demanding bits that we know, return the known
181 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000182 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000183 return Constant::getIntegerValue(VTy, IKnownOne);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000184
Chris Lattner7e044912010-01-04 07:17:19 +0000185 // If all of the demanded bits are known 1 on one side, return the other.
186 // These bits cannot contribute to the result of the 'and'.
Craig Topperb45eabc2017-04-26 16:39:58 +0000187 if (DemandedMask.isSubsetOf(LHSKnown.Zero | RHSKnown.One))
Chris Lattner7e044912010-01-04 07:17:19 +0000188 return I->getOperand(0);
Craig Topperb45eabc2017-04-26 16:39:58 +0000189 if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.One))
Chris Lattner7e044912010-01-04 07:17:19 +0000190 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000191
Chris Lattner7e044912010-01-04 07:17:19 +0000192 // If the RHS is a constant, see if we can simplify it.
Craig Topperb45eabc2017-04-26 16:39:58 +0000193 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnown.Zero))
Chris Lattner7e044912010-01-04 07:17:19 +0000194 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000195
Craig Topperb45eabc2017-04-26 16:39:58 +0000196 Known.Zero = std::move(IKnownZero);
197 Known.One = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000198 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000199 }
200 case Instruction::Or: {
Chris Lattner7e044912010-01-04 07:17:19 +0000201 // If either the LHS or the RHS are One, the result is One.
Craig Topperb45eabc2017-04-26 16:39:58 +0000202 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
203 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.One, LHSKnown,
204 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000205 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000206 assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
207 assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
Craig Topper4c947752012-12-22 18:09:02 +0000208
Craig Topper9a458cd2017-04-14 22:34:14 +0000209 // Output known-0 bits are only known if clear in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000210 APInt IKnownZero = RHSKnown.Zero & LHSKnown.Zero;
211 // Output known-1 are known. to be set if s.et in either the LHS | RHS.
212 APInt IKnownOne = RHSKnown.One | LHSKnown.One;
Craig Topper9a458cd2017-04-14 22:34:14 +0000213
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000214 // If the client is only demanding bits that we know, return the known
215 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000216 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000217 return Constant::getIntegerValue(VTy, IKnownOne);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000218
Chris Lattner7e044912010-01-04 07:17:19 +0000219 // If all of the demanded bits are known zero on one side, return the other.
220 // These bits cannot contribute to the result of the 'or'.
Craig Topperb45eabc2017-04-26 16:39:58 +0000221 if (DemandedMask.isSubsetOf(LHSKnown.One | RHSKnown.Zero))
Chris Lattner7e044912010-01-04 07:17:19 +0000222 return I->getOperand(0);
Craig Topperb45eabc2017-04-26 16:39:58 +0000223 if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero))
Chris Lattner7e044912010-01-04 07:17:19 +0000224 return I->getOperand(1);
225
Chris Lattner7e044912010-01-04 07:17:19 +0000226 // If the RHS is a constant, see if we can simplify it.
227 if (ShrinkDemandedConstant(I, 1, DemandedMask))
228 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000229
Craig Topperb45eabc2017-04-26 16:39:58 +0000230 Known.Zero = std::move(IKnownZero);
231 Known.One = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000232 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000233 }
Chris Lattner7e044912010-01-04 07:17:19 +0000234 case Instruction::Xor: {
Craig Topperb45eabc2017-04-26 16:39:58 +0000235 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
236 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000237 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000238 assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
239 assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
Craig Topper4c947752012-12-22 18:09:02 +0000240
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000241 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000242 APInt IKnownZero = (RHSKnown.Zero & LHSKnown.Zero) |
243 (RHSKnown.One & LHSKnown.One);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000244 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000245 APInt IKnownOne = (RHSKnown.Zero & LHSKnown.One) |
246 (RHSKnown.One & LHSKnown.Zero);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000247
248 // If the client is only demanding bits that we know, return the known
249 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000250 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000251 return Constant::getIntegerValue(VTy, IKnownOne);
252
Chris Lattner7e044912010-01-04 07:17:19 +0000253 // If all of the demanded bits are known zero on one side, return the other.
254 // These bits cannot contribute to the result of the 'xor'.
Craig Topperb45eabc2017-04-26 16:39:58 +0000255 if (DemandedMask.isSubsetOf(RHSKnown.Zero))
Chris Lattner7e044912010-01-04 07:17:19 +0000256 return I->getOperand(0);
Craig Topperb45eabc2017-04-26 16:39:58 +0000257 if (DemandedMask.isSubsetOf(LHSKnown.Zero))
Chris Lattner7e044912010-01-04 07:17:19 +0000258 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000259
Chris Lattner7e044912010-01-04 07:17:19 +0000260 // If all of the demanded bits are known to be zero on one side or the
261 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000262 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Craig Topperb45eabc2017-04-26 16:39:58 +0000263 if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.Zero)) {
Craig Topper4c947752012-12-22 18:09:02 +0000264 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000265 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
266 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000267 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000268 }
Craig Topper4c947752012-12-22 18:09:02 +0000269
Chris Lattner7e044912010-01-04 07:17:19 +0000270 // If all of the demanded bits on one side are known, and all of the set
271 // bits on that side are also known to be set on the other side, turn this
272 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000273 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topperb45eabc2017-04-26 16:39:58 +0000274 if (DemandedMask.isSubsetOf(RHSKnown.Zero|RHSKnown.One) &&
275 RHSKnown.One.isSubsetOf(LHSKnown.One)) {
Craig Topper17f37ba2017-04-20 20:47:35 +0000276 Constant *AndC = Constant::getIntegerValue(VTy,
Craig Topperb45eabc2017-04-26 16:39:58 +0000277 ~RHSKnown.One & DemandedMask);
Craig Topper17f37ba2017-04-20 20:47:35 +0000278 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
279 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000280 }
Craig Topper4c947752012-12-22 18:09:02 +0000281
Sanjay Patel8ce1d4c2017-04-21 20:29:17 +0000282 // If the RHS is a constant, see if we can simplify it.
283 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
284 if (ShrinkDemandedConstant(I, 1, DemandedMask))
285 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000286
Chris Lattner7e044912010-01-04 07:17:19 +0000287 // If our LHS is an 'and' and if it has one use, and if any of the bits we
288 // are flipping are known to be set, then the xor is just resetting those
289 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
290 // simplifying both of them.
291 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
292 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
293 isa<ConstantInt>(I->getOperand(1)) &&
294 isa<ConstantInt>(LHSInst->getOperand(1)) &&
Craig Topperb45eabc2017-04-26 16:39:58 +0000295 (LHSKnown.One & RHSKnown.One & DemandedMask) != 0) {
Chris Lattner7e044912010-01-04 07:17:19 +0000296 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
297 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
Craig Topperb45eabc2017-04-26 16:39:58 +0000298 APInt NewMask = ~(LHSKnown.One & RHSKnown.One & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000299
Chris Lattner7e044912010-01-04 07:17:19 +0000300 Constant *AndC =
301 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000302 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000303 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000304
Chris Lattner7e044912010-01-04 07:17:19 +0000305 Constant *XorC =
306 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000307 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000308 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000309 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000310
311 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000312 Known.Zero = std::move(IKnownZero);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000313 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000314 Known.One = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000315 break;
316 }
317 case Instruction::Select:
James Molloy2b21a7c2015-05-20 18:41:25 +0000318 // If this is a select as part of a min/max pattern, don't simplify any
319 // further in case we break the structure.
320 Value *LHS, *RHS;
James Molloy134bec22015-08-11 09:12:57 +0000321 if (matchSelectPattern(I, LHS, RHS).Flavor != SPF_UNKNOWN)
James Molloy2b21a7c2015-05-20 18:41:25 +0000322 return nullptr;
Simon Pilgrim61116dd2015-09-17 20:32:45 +0000323
Craig Topperb45eabc2017-04-26 16:39:58 +0000324 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnown, Depth + 1) ||
325 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnown, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000326 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000327 assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
328 assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
Craig Topper4c947752012-12-22 18:09:02 +0000329
Chris Lattner7e044912010-01-04 07:17:19 +0000330 // If the operands are constants, see if we can simplify them.
331 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
332 ShrinkDemandedConstant(I, 2, DemandedMask))
333 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000334
Chris Lattner7e044912010-01-04 07:17:19 +0000335 // Only known if known in both the LHS and RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000336 Known.One = RHSKnown.One & LHSKnown.One;
337 Known.Zero = RHSKnown.Zero & LHSKnown.Zero;
Chris Lattner7e044912010-01-04 07:17:19 +0000338 break;
Craig Topper2f9c6da2017-05-24 18:40:25 +0000339 case Instruction::ZExt:
Chris Lattner7e044912010-01-04 07:17:19 +0000340 case Instruction::Trunc: {
Craig Topper2f9c6da2017-05-24 18:40:25 +0000341 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
342
343 APInt InputDemandedMask = DemandedMask.zextOrTrunc(SrcBitWidth);
344 KnownBits InputKnown(SrcBitWidth);
345 if (SimplifyDemandedBits(I, 0, InputDemandedMask, InputKnown, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000346 return I;
Sanjay Patelaa766ef2018-01-17 14:39:28 +0000347 Known = InputKnown.zextOrTrunc(BitWidth);
Craig Topper2f9c6da2017-05-24 18:40:25 +0000348 // Any top bits are known to be zero.
349 if (BitWidth > SrcBitWidth)
350 Known.Zero.setBitsFrom(SrcBitWidth);
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000351 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000352 break;
353 }
354 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000355 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000356 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000357
Chris Lattner229907c2011-07-18 04:54:35 +0000358 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
359 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000360 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
361 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
362 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000363 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000364 } else
365 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000366 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000367 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000368 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000369 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000370
Craig Topperb45eabc2017-04-26 16:39:58 +0000371 if (SimplifyDemandedBits(I, 0, DemandedMask, Known, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000372 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000373 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000374 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000375 case Instruction::SExt: {
376 // Compute the bits in the result that are not present in the input.
Craig Topper1c660db2017-05-24 17:33:30 +0000377 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000378
Craig Topper1c660db2017-05-24 17:33:30 +0000379 APInt InputDemandedBits = DemandedMask.trunc(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000380
Chris Lattner7e044912010-01-04 07:17:19 +0000381 // If any of the sign extended bits are demanded, we know that the sign
382 // bit is demanded.
Craig Topper1c660db2017-05-24 17:33:30 +0000383 if (DemandedMask.getActiveBits() > SrcBitWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000384 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000385
Craig Topper1c660db2017-05-24 17:33:30 +0000386 KnownBits InputKnown(SrcBitWidth);
387 if (SimplifyDemandedBits(I, 0, InputDemandedBits, InputKnown, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000388 return I;
Chris Lattner7e044912010-01-04 07:17:19 +0000389
390 // If the input sign bit is known zero, or if the NewBits are not demanded
391 // convert this into a zero extension.
Craig Topper1c660db2017-05-24 17:33:30 +0000392 if (InputKnown.isNonNegative() ||
393 DemandedMask.getActiveBits() <= SrcBitWidth) {
394 // Convert to ZExt cast.
Chris Lattner7e044912010-01-04 07:17:19 +0000395 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000396 return InsertNewInstWith(NewCast, *I);
Craig Topper1c660db2017-05-24 17:33:30 +0000397 }
398
399 // If the sign bit of the input is known set or clear, then we know the
400 // top bits of the result.
401 Known = InputKnown.sext(BitWidth);
402 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000403 break;
404 }
Matthias Braune48484c2015-04-30 22:05:30 +0000405 case Instruction::Add:
406 case Instruction::Sub: {
407 /// If the high-bits of an ADD/SUB are not demanded, then we do not care
408 /// about the high bits of the operands.
Chris Lattner7e044912010-01-04 07:17:19 +0000409 unsigned NLZ = DemandedMask.countLeadingZeros();
Craig Topper35171e52017-08-25 18:39:40 +0000410 // Right fill the mask of bits for this ADD/SUB to demand the most
411 // significant bit and all those below it.
412 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
413 if (ShrinkDemandedConstant(I, 0, DemandedFromOps) ||
414 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnown, Depth + 1) ||
415 ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
416 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1)) {
417 if (NLZ > 0) {
Matthias Braune48484c2015-04-30 22:05:30 +0000418 // Disable the nsw and nuw flags here: We can no longer guarantee that
419 // we won't wrap after simplification. Removing the nsw/nuw flags is
420 // legal here because the top bit is not demanded.
421 BinaryOperator &BinOP = *cast<BinaryOperator>(I);
422 BinOP.setHasNoSignedWrap(false);
423 BinOP.setHasNoUnsignedWrap(false);
David Majnemer7d0e99c2015-04-22 22:42:05 +0000424 }
Craig Topper35171e52017-08-25 18:39:40 +0000425 return I;
Chris Lattner7e044912010-01-04 07:17:19 +0000426 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000427
Craig Topper35171e52017-08-25 18:39:40 +0000428 // If we are known to be adding/subtracting zeros to every bit below
429 // the highest demanded bit, we just return the other side.
430 if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
431 return I->getOperand(0);
432 // We can't do this with the LHS for subtraction, unless we are only
433 // demanding the LSB.
434 if ((I->getOpcode() == Instruction::Add ||
435 DemandedFromOps.isOneValue()) &&
436 DemandedFromOps.isSubsetOf(LHSKnown.Zero))
437 return I->getOperand(1);
438
439 // Otherwise just compute the known bits of the result.
Craig Topper3763f0e2017-08-28 18:44:28 +0000440 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
Craig Topper35171e52017-08-25 18:39:40 +0000441 Known = KnownBits::computeForAddSub(I->getOpcode() == Instruction::Add,
442 NSW, LHSKnown, RHSKnown);
Chris Lattner7e044912010-01-04 07:17:19 +0000443 break;
Matthias Braune48484c2015-04-30 22:05:30 +0000444 }
Sanjay Patel3e1ae722017-04-20 21:33:02 +0000445 case Instruction::Shl: {
446 const APInt *SA;
447 if (match(I->getOperand(1), m_APInt(SA))) {
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000448 const APInt *ShrAmt;
Simon Pilgrima42a5422017-12-09 23:42:56 +0000449 if (match(I->getOperand(0), m_Shr(m_Value(), m_APInt(ShrAmt))))
450 if (Instruction *Shr = dyn_cast<Instruction>(I->getOperand(0)))
451 if (Value *R = simplifyShrShlDemandedBits(Shr, *ShrAmt, I, *SA,
452 DemandedMask, Known))
453 return R;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000454
Chris Lattner768003c2011-02-10 05:09:34 +0000455 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000456 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000457
Chris Lattner768003c2011-02-10 05:09:34 +0000458 // If the shift is NUW/NSW, then it does demand the high bits.
459 ShlOperator *IOp = cast<ShlOperator>(I);
460 if (IOp->hasNoSignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000461 DemandedMaskIn.setHighBits(ShiftAmt+1);
Chris Lattner768003c2011-02-10 05:09:34 +0000462 else if (IOp->hasNoUnsignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000463 DemandedMaskIn.setHighBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000464
Craig Topperb45eabc2017-04-26 16:39:58 +0000465 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000466 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000467 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Craig Topperb45eabc2017-04-26 16:39:58 +0000468 Known.Zero <<= ShiftAmt;
469 Known.One <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000470 // low bits known zero.
471 if (ShiftAmt)
Craig Topperb45eabc2017-04-26 16:39:58 +0000472 Known.Zero.setLowBits(ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000473 }
474 break;
Sanjay Patel3e1ae722017-04-20 21:33:02 +0000475 }
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000476 case Instruction::LShr: {
477 const APInt *SA;
478 if (match(I->getOperand(1), m_APInt(SA))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000479 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000480
Chris Lattner7e044912010-01-04 07:17:19 +0000481 // Unsigned shift right.
482 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000483
Chris Lattner768003c2011-02-10 05:09:34 +0000484 // If the shift is exact, then it does demand the low bits (and knows that
485 // they are zero).
486 if (cast<LShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000487 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000488
Craig Topperb45eabc2017-04-26 16:39:58 +0000489 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000490 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000491 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Craig Topperb45eabc2017-04-26 16:39:58 +0000492 Known.Zero.lshrInPlace(ShiftAmt);
493 Known.One.lshrInPlace(ShiftAmt);
Craig Topper3a86a042017-03-19 05:49:16 +0000494 if (ShiftAmt)
Craig Topperb45eabc2017-04-26 16:39:58 +0000495 Known.Zero.setHighBits(ShiftAmt); // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000496 }
497 break;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000498 }
499 case Instruction::AShr: {
Chris Lattner7e044912010-01-04 07:17:19 +0000500 // If this is an arithmetic shift right and only the low-bit is set, we can
501 // always convert this into a logical shr, even if the shift amount is
502 // variable. The low bit of the shift cannot be an input sign bit unless
503 // the shift amount is >= the size of the datatype, which is undefined.
Craig Topper73ba1c82017-06-07 07:40:37 +0000504 if (DemandedMask.isOneValue()) {
Chris Lattner7e044912010-01-04 07:17:19 +0000505 // Perform the logical shift right.
506 Instruction *NewVal = BinaryOperator::CreateLShr(
507 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000508 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000509 }
Chris Lattner7e044912010-01-04 07:17:19 +0000510
511 // If the sign bit is the only bit demanded by this ashr, then there is no
512 // need to do it, the shift doesn't change the high bit.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000513 if (DemandedMask.isSignMask())
Chris Lattner7e044912010-01-04 07:17:19 +0000514 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000515
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000516 const APInt *SA;
517 if (match(I->getOperand(1), m_APInt(SA))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000518 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000519
Chris Lattner7e044912010-01-04 07:17:19 +0000520 // Signed shift right.
521 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000522 // If any of the high bits are demanded, we should set the sign bit as
Chris Lattner7e044912010-01-04 07:17:19 +0000523 // demanded.
524 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Craig Topperc9a4fc02017-04-14 05:09:04 +0000525 DemandedMaskIn.setSignBit();
Craig Topper4c947752012-12-22 18:09:02 +0000526
Chris Lattner768003c2011-02-10 05:09:34 +0000527 // If the shift is exact, then it does demand the low bits (and knows that
528 // they are zero).
529 if (cast<AShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000530 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000531
Craig Topperb45eabc2017-04-26 16:39:58 +0000532 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000533 return I;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000534
Amjad Aboud22178dd2017-08-25 11:07:54 +0000535 unsigned SignBits = ComputeNumSignBits(I->getOperand(0), Depth + 1, CxtI);
536
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000537 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Amjad Aboud22178dd2017-08-25 11:07:54 +0000538 // Compute the new bits that are at the top now plus sign bits.
539 APInt HighBits(APInt::getHighBitsSet(
540 BitWidth, std::min(SignBits + ShiftAmt - 1, BitWidth)));
Craig Topperb45eabc2017-04-26 16:39:58 +0000541 Known.Zero.lshrInPlace(ShiftAmt);
542 Known.One.lshrInPlace(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000543
Chris Lattner7e044912010-01-04 07:17:19 +0000544 // If the input sign bit is known to be zero, or if none of the top bits
545 // are demanded, turn this into an unsigned shift right.
Craig Topper4d5050b2017-08-01 15:10:25 +0000546 assert(BitWidth > ShiftAmt && "Shift amount not saturated?");
547 if (Known.Zero[BitWidth-ShiftAmt-1] ||
Craig Topperff238892017-04-20 21:24:37 +0000548 !DemandedMask.intersects(HighBits)) {
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000549 BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0),
550 I->getOperand(1));
551 LShr->setIsExact(cast<BinaryOperator>(I)->isExact());
552 return InsertNewInstWith(LShr, *I);
Craig Topperfc9bf502017-08-02 21:05:40 +0000553 } else if (Known.One[BitWidth-ShiftAmt-1]) { // New bits are known one.
Craig Topperb45eabc2017-04-26 16:39:58 +0000554 Known.One |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000555 }
556 }
557 break;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000558 }
Benjamin Kramer0d2fc1a2018-05-09 22:27:34 +0000559 case Instruction::UDiv: {
560 // UDiv doesn't demand low bits that are zero in the divisor.
561 const APInt *SA;
562 if (match(I->getOperand(1), m_APInt(SA))) {
563 // If the shift is exact, then it does demand the low bits.
564 if (cast<UDivOperator>(I)->isExact())
565 break;
566
567 // FIXME: Take the demanded mask of the result into account.
Benjamin Kramer456f4732018-05-10 11:45:18 +0000568 unsigned RHSTrailingZeros = SA->countTrailingZeros();
Benjamin Kramer0d2fc1a2018-05-09 22:27:34 +0000569 APInt DemandedMaskIn =
Benjamin Kramer456f4732018-05-10 11:45:18 +0000570 APInt::getHighBitsSet(BitWidth, BitWidth - RHSTrailingZeros);
571 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, LHSKnown, Depth + 1))
Benjamin Kramer0d2fc1a2018-05-09 22:27:34 +0000572 return I;
Benjamin Kramer456f4732018-05-10 11:45:18 +0000573
574 // Propagate zero bits from the input.
575 Known.Zero.setHighBits(std::min(
576 BitWidth, LHSKnown.Zero.countLeadingOnes() + RHSTrailingZeros));
Benjamin Kramer0d2fc1a2018-05-09 22:27:34 +0000577 }
578 break;
579 }
Chris Lattner7e044912010-01-04 07:17:19 +0000580 case Instruction::SRem:
581 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000582 // X % -1 demands all the bits because we don't want to introduce
583 // INT_MIN % -1 (== undef) by accident.
Craig Topper79ab6432017-07-06 18:39:47 +0000584 if (Rem->isMinusOne())
Eli Friedmana81a82d2011-03-09 01:28:35 +0000585 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000586 APInt RA = Rem->getValue().abs();
587 if (RA.isPowerOf2()) {
588 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
589 return I->getOperand(0);
590
591 APInt LowBits = RA - 1;
Craig Topperbcfd2d12017-04-20 16:56:25 +0000592 APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
Craig Topperb45eabc2017-04-26 16:39:58 +0000593 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000594 return I;
595
Duncan Sands3a48b872010-01-28 17:22:42 +0000596 // The low bits of LHS are unchanged by the srem.
Craig Topperb45eabc2017-04-26 16:39:58 +0000597 Known.Zero = LHSKnown.Zero & LowBits;
598 Known.One = LHSKnown.One & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000599
Duncan Sands3a48b872010-01-28 17:22:42 +0000600 // If LHS is non-negative or has all low bits zero, then the upper bits
601 // are all zero.
Craig Topperca48af32017-04-29 16:43:11 +0000602 if (LHSKnown.isNonNegative() || LowBits.isSubsetOf(LHSKnown.Zero))
Craig Topperb45eabc2017-04-26 16:39:58 +0000603 Known.Zero |= ~LowBits;
Duncan Sands3a48b872010-01-28 17:22:42 +0000604
605 // If LHS is negative and not all low bits are zero, then the upper bits
606 // are all one.
Craig Topperca48af32017-04-29 16:43:11 +0000607 if (LHSKnown.isNegative() && LowBits.intersects(LHSKnown.One))
Craig Topperb45eabc2017-04-26 16:39:58 +0000608 Known.One |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000609
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000610 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Craig Topperda886c62017-04-16 21:46:12 +0000611 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000612 }
613 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000614
615 // The sign bit is the LHS's sign bit, except when the result of the
616 // remainder is zero.
Craig Topperd23004c2017-04-17 16:38:20 +0000617 if (DemandedMask.isSignBitSet()) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000618 computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000619 // If it's known zero, our sign bit is also zero.
Craig Topperca48af32017-04-29 16:43:11 +0000620 if (LHSKnown.isNonNegative())
621 Known.makeNonNegative();
Nick Lewyckye4679792011-03-07 01:50:10 +0000622 }
Chris Lattner7e044912010-01-04 07:17:19 +0000623 break;
624 case Instruction::URem: {
Craig Topperb45eabc2017-04-26 16:39:58 +0000625 KnownBits Known2(BitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000626 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
Craig Topperb45eabc2017-04-26 16:39:58 +0000627 if (SimplifyDemandedBits(I, 0, AllOnes, Known2, Depth + 1) ||
628 SimplifyDemandedBits(I, 1, AllOnes, Known2, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000629 return I;
630
Craig Topper8df66c62017-05-12 17:20:30 +0000631 unsigned Leaders = Known2.countMinLeadingZeros();
Craig Topperb45eabc2017-04-26 16:39:58 +0000632 Known.Zero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
Chris Lattner7e044912010-01-04 07:17:19 +0000633 break;
634 }
635 case Instruction::Call:
636 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
637 switch (II->getIntrinsicID()) {
638 default: break;
639 case Intrinsic::bswap: {
640 // If the only bits demanded come from one byte of the bswap result,
641 // just shift the input byte into position to eliminate the bswap.
642 unsigned NLZ = DemandedMask.countLeadingZeros();
643 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000644
Chris Lattner7e044912010-01-04 07:17:19 +0000645 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
646 // we need all the bits down to bit 8. Likewise, round NLZ. If we
647 // have 14 leading zeros, round to 8.
648 NLZ &= ~7;
649 NTZ &= ~7;
650 // If we need exactly one byte, we can do this transformation.
651 if (BitWidth-NLZ-NTZ == 8) {
652 unsigned ResultBit = NTZ;
653 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000654
Chris Lattner7e044912010-01-04 07:17:19 +0000655 // Replace this with either a left or right shift to get the byte into
656 // the right place.
657 Instruction *NewVal;
658 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000659 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000660 ConstantInt::get(I->getType(), InputBit-ResultBit));
661 else
Gabor Greif79430172010-06-24 12:35:13 +0000662 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000663 ConstantInt::get(I->getType(), ResultBit-InputBit));
664 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000665 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000666 }
Craig Topper4c947752012-12-22 18:09:02 +0000667
Chris Lattner7e044912010-01-04 07:17:19 +0000668 // TODO: Could compute known zero/one bits based on the input.
669 break;
670 }
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000671 case Intrinsic::x86_mmx_pmovmskb:
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000672 case Intrinsic::x86_sse_movmsk_ps:
673 case Intrinsic::x86_sse2_movmsk_pd:
674 case Intrinsic::x86_sse2_pmovmskb_128:
675 case Intrinsic::x86_avx_movmsk_ps_256:
676 case Intrinsic::x86_avx_movmsk_pd_256:
677 case Intrinsic::x86_avx2_pmovmskb: {
678 // MOVMSK copies the vector elements' sign bits to the low bits
679 // and zeros the high bits.
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000680 unsigned ArgWidth;
681 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
682 ArgWidth = 8; // Arg is x86_mmx, but treated as <8 x i8>.
683 } else {
684 auto Arg = II->getArgOperand(0);
685 auto ArgType = cast<VectorType>(Arg->getType());
686 ArgWidth = ArgType->getNumElements();
687 }
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000688
689 // If we don't need any of low bits then return zero,
690 // we know that DemandedMask is non-zero already.
691 APInt DemandedElts = DemandedMask.zextOrTrunc(ArgWidth);
Craig Topper73ba1c82017-06-07 07:40:37 +0000692 if (DemandedElts.isNullValue())
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000693 return ConstantInt::getNullValue(VTy);
694
Ahmed Bougacha17482a52016-04-28 14:36:07 +0000695 // We know that the upper bits are set to zero.
Craig Topperb45eabc2017-04-26 16:39:58 +0000696 Known.Zero.setBitsFrom(ArgWidth);
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000697 return nullptr;
698 }
Chad Rosierb3628842011-05-26 23:13:19 +0000699 case Intrinsic::x86_sse42_crc32_64_64:
Craig Topperb45eabc2017-04-26 16:39:58 +0000700 Known.Zero.setBitsFrom(32);
Craig Topperf40110f2014-04-25 05:29:35 +0000701 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000702 }
703 }
Craig Topperb45eabc2017-04-26 16:39:58 +0000704 computeKnownBits(V, Known, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000705 break;
706 }
Craig Topper4c947752012-12-22 18:09:02 +0000707
Chris Lattner7e044912010-01-04 07:17:19 +0000708 // If the client is only demanding bits that we know, return the known
709 // constant.
Craig Topperb45eabc2017-04-26 16:39:58 +0000710 if (DemandedMask.isSubsetOf(Known.Zero|Known.One))
711 return Constant::getIntegerValue(VTy, Known.One);
Craig Topperf40110f2014-04-25 05:29:35 +0000712 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000713}
714
Craig Topperb45eabc2017-04-26 16:39:58 +0000715/// Helper routine of SimplifyDemandedUseBits. It computes Known
Craig Topperb0076fe2017-04-12 18:05:21 +0000716/// bits. It also tries to handle simplifications that can be done based on
717/// DemandedMask, but without modifying the Instruction.
718Value *InstCombiner::SimplifyMultipleUseDemandedBits(Instruction *I,
719 const APInt &DemandedMask,
Craig Topperb45eabc2017-04-26 16:39:58 +0000720 KnownBits &Known,
Craig Topperb0076fe2017-04-12 18:05:21 +0000721 unsigned Depth,
722 Instruction *CxtI) {
723 unsigned BitWidth = DemandedMask.getBitWidth();
724 Type *ITy = I->getType();
725
Craig Topperb45eabc2017-04-26 16:39:58 +0000726 KnownBits LHSKnown(BitWidth);
727 KnownBits RHSKnown(BitWidth);
Craig Topperb0076fe2017-04-12 18:05:21 +0000728
729 // Despite the fact that we can't simplify this instruction in all User's
Craig Topperb45eabc2017-04-26 16:39:58 +0000730 // context, we can at least compute the known bits, and we can
Craig Topperb0076fe2017-04-12 18:05:21 +0000731 // do simplifications that apply to *just* the one user if we know that
732 // this instruction has a simpler value in that context.
Craig Topperf35a7f72017-04-12 18:25:25 +0000733 switch (I->getOpcode()) {
Craig Topper9a458cd2017-04-14 22:34:14 +0000734 case Instruction::And: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000735 // If either the LHS or the RHS are Zero, the result is zero.
Craig Topperb45eabc2017-04-26 16:39:58 +0000736 computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
737 computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1,
Craig Topperb0076fe2017-04-12 18:05:21 +0000738 CxtI);
739
Craig Topper9a458cd2017-04-14 22:34:14 +0000740 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000741 APInt IKnownZero = RHSKnown.Zero | LHSKnown.Zero;
Craig Topper9a458cd2017-04-14 22:34:14 +0000742 // Output known-1 bits are only known if set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000743 APInt IKnownOne = RHSKnown.One & LHSKnown.One;
Craig Topper9a458cd2017-04-14 22:34:14 +0000744
Craig Topperc75f94b2017-04-12 19:32:47 +0000745 // If the client is only demanding bits that we know, return the known
746 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000747 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000748 return Constant::getIntegerValue(ITy, IKnownOne);
Craig Topperc75f94b2017-04-12 19:32:47 +0000749
Craig Topperb0076fe2017-04-12 18:05:21 +0000750 // If all of the demanded bits are known 1 on one side, return the other.
751 // These bits cannot contribute to the result of the 'and' in this
752 // context.
Craig Topperb45eabc2017-04-26 16:39:58 +0000753 if (DemandedMask.isSubsetOf(LHSKnown.Zero | RHSKnown.One))
Craig Topperb0076fe2017-04-12 18:05:21 +0000754 return I->getOperand(0);
Craig Topperb45eabc2017-04-26 16:39:58 +0000755 if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.One))
Craig Topperb0076fe2017-04-12 18:05:21 +0000756 return I->getOperand(1);
757
Craig Topperb45eabc2017-04-26 16:39:58 +0000758 Known.Zero = std::move(IKnownZero);
759 Known.One = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000760 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000761 }
762 case Instruction::Or: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000763 // We can simplify (X|Y) -> X or Y in the user's context if we know that
764 // only bits from X or Y are demanded.
765
766 // If either the LHS or the RHS are One, the result is One.
Craig Topperb45eabc2017-04-26 16:39:58 +0000767 computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
768 computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1,
Craig Topperb0076fe2017-04-12 18:05:21 +0000769 CxtI);
770
Craig Topper9a458cd2017-04-14 22:34:14 +0000771 // Output known-0 bits are only known if clear in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000772 APInt IKnownZero = RHSKnown.Zero & LHSKnown.Zero;
Craig Topper9a458cd2017-04-14 22:34:14 +0000773 // Output known-1 are known to be set if set in either the LHS | RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000774 APInt IKnownOne = RHSKnown.One | LHSKnown.One;
Craig Topper9a458cd2017-04-14 22:34:14 +0000775
Craig Topperc75f94b2017-04-12 19:32:47 +0000776 // If the client is only demanding bits that we know, return the known
777 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000778 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000779 return Constant::getIntegerValue(ITy, IKnownOne);
Craig Topperc75f94b2017-04-12 19:32:47 +0000780
Craig Topperb0076fe2017-04-12 18:05:21 +0000781 // If all of the demanded bits are known zero on one side, return the
782 // other. These bits cannot contribute to the result of the 'or' in this
783 // context.
Craig Topperb45eabc2017-04-26 16:39:58 +0000784 if (DemandedMask.isSubsetOf(LHSKnown.One | RHSKnown.Zero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000785 return I->getOperand(0);
Craig Topperb45eabc2017-04-26 16:39:58 +0000786 if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000787 return I->getOperand(1);
788
Craig Topperb45eabc2017-04-26 16:39:58 +0000789 Known.Zero = std::move(IKnownZero);
790 Known.One = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000791 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000792 }
Craig Topperc75f94b2017-04-12 19:32:47 +0000793 case Instruction::Xor: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000794 // We can simplify (X^Y) -> X or Y in the user's context if we know that
795 // only bits from X or Y are demanded.
796
Craig Topperb45eabc2017-04-26 16:39:58 +0000797 computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
798 computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1,
Craig Topperb0076fe2017-04-12 18:05:21 +0000799 CxtI);
800
Craig Topperc75f94b2017-04-12 19:32:47 +0000801 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000802 APInt IKnownZero = (RHSKnown.Zero & LHSKnown.Zero) |
803 (RHSKnown.One & LHSKnown.One);
Craig Topperc75f94b2017-04-12 19:32:47 +0000804 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000805 APInt IKnownOne = (RHSKnown.Zero & LHSKnown.One) |
806 (RHSKnown.One & LHSKnown.Zero);
Craig Topperc75f94b2017-04-12 19:32:47 +0000807
808 // If the client is only demanding bits that we know, return the known
809 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000810 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topperc75f94b2017-04-12 19:32:47 +0000811 return Constant::getIntegerValue(ITy, IKnownOne);
812
Craig Topperb0076fe2017-04-12 18:05:21 +0000813 // If all of the demanded bits are known zero on one side, return the
814 // other.
Craig Topperb45eabc2017-04-26 16:39:58 +0000815 if (DemandedMask.isSubsetOf(RHSKnown.Zero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000816 return I->getOperand(0);
Craig Topperb45eabc2017-04-26 16:39:58 +0000817 if (DemandedMask.isSubsetOf(LHSKnown.Zero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000818 return I->getOperand(1);
Craig Topperf35a7f72017-04-12 18:25:25 +0000819
Craig Topperc75f94b2017-04-12 19:32:47 +0000820 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000821 Known.Zero = std::move(IKnownZero);
Craig Topperc75f94b2017-04-12 19:32:47 +0000822 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000823 Known.One = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000824 break;
Craig Topperb0076fe2017-04-12 18:05:21 +0000825 }
Craig Topperc75f94b2017-04-12 19:32:47 +0000826 default:
Craig Topperb45eabc2017-04-26 16:39:58 +0000827 // Compute the Known bits to simplify things downstream.
828 computeKnownBits(I, Known, Depth, CxtI);
Craig Topperb0076fe2017-04-12 18:05:21 +0000829
Craig Topperc75f94b2017-04-12 19:32:47 +0000830 // If this user is only demanding bits that we know, return the known
831 // constant.
Craig Topperb45eabc2017-04-26 16:39:58 +0000832 if (DemandedMask.isSubsetOf(Known.Zero|Known.One))
833 return Constant::getIntegerValue(ITy, Known.One);
Craig Topper9a51c7f2017-04-12 18:17:46 +0000834
Craig Topperc75f94b2017-04-12 19:32:47 +0000835 break;
836 }
Craig Topper9a51c7f2017-04-12 18:17:46 +0000837
Craig Topperb0076fe2017-04-12 18:05:21 +0000838 return nullptr;
839}
840
841
Shuxin Yang63e999e2012-12-04 00:04:54 +0000842/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
843/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
844/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
845/// of "C2-C1".
846///
847/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
848/// ..., bn}, without considering the specific value X is holding.
849/// This transformation is legal iff one of following conditions is hold:
850/// 1) All the bit in S are 0, in this case E1 == E2.
851/// 2) We don't care those bits in S, per the input DemandedMask.
852/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
853/// rest bits.
854///
855/// Currently we only test condition 2).
856///
857/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
858/// not successful.
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000859Value *
Sanjay Patelcc663b82017-04-20 22:37:01 +0000860InstCombiner::simplifyShrShlDemandedBits(Instruction *Shr, const APInt &ShrOp1,
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000861 Instruction *Shl, const APInt &ShlOp1,
862 const APInt &DemandedMask,
Craig Topperb45eabc2017-04-26 16:39:58 +0000863 KnownBits &Known) {
Benjamin Kramer010f1082013-08-30 14:35:35 +0000864 if (!ShlOp1 || !ShrOp1)
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000865 return nullptr; // No-op.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000866
867 Value *VarX = Shr->getOperand(0);
868 Type *Ty = VarX->getType();
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000869 unsigned BitWidth = Ty->getScalarSizeInBits();
Benjamin Kramer010f1082013-08-30 14:35:35 +0000870 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000871 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000872
873 unsigned ShlAmt = ShlOp1.getZExtValue();
874 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000875
Craig Topperb45eabc2017-04-26 16:39:58 +0000876 Known.One.clearAllBits();
877 Known.Zero.setLowBits(ShlAmt - 1);
878 Known.Zero &= DemandedMask;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000879
Benjamin Kramer010f1082013-08-30 14:35:35 +0000880 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
881 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000882
883 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
884 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
885 (BitMask1.ashr(ShrAmt) << ShlAmt);
886
887 if (ShrAmt <= ShlAmt) {
888 BitMask2 <<= (ShlAmt - ShrAmt);
889 } else {
890 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
891 BitMask2.ashr(ShrAmt - ShlAmt);
892 }
893
894 // Check if condition-2 (see the comment to this function) is satified.
895 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
896 if (ShrAmt == ShlAmt)
897 return VarX;
898
899 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000900 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000901
902 BinaryOperator *New;
903 if (ShrAmt < ShlAmt) {
904 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
905 New = BinaryOperator::CreateShl(VarX, Amt);
906 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
907 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
908 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
909 } else {
910 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000911 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
912 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000913 if (cast<BinaryOperator>(Shr)->isExact())
914 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000915 }
916
917 return InsertNewInstWith(New, *Shl);
918 }
919
Craig Topperf40110f2014-04-25 05:29:35 +0000920 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000921}
Chris Lattner7e044912010-01-04 07:17:19 +0000922
Nicolai Haehnleb29ee702018-06-21 13:37:31 +0000923/// Implement SimplifyDemandedVectorElts for amdgcn buffer and image intrinsics.
924Value *InstCombiner::simplifyAMDGCNMemoryIntrinsicDemanded(IntrinsicInst *II,
925 APInt DemandedElts,
926 int DMaskIdx) {
927 unsigned VWidth = II->getType()->getVectorNumElements();
928 if (VWidth == 1)
929 return nullptr;
930
931 ConstantInt *NewDMask = nullptr;
932
933 if (DMaskIdx < 0) {
934 // Pretend that a prefix of elements is demanded to simplify the code
935 // below.
936 DemandedElts = (1 << DemandedElts.getActiveBits()) - 1;
937 } else {
938 ConstantInt *DMask = dyn_cast<ConstantInt>(II->getArgOperand(DMaskIdx));
939 if (!DMask)
940 return nullptr; // non-constant dmask is not supported by codegen
941
942 unsigned DMaskVal = DMask->getZExtValue() & 0xf;
943
944 // Mask off values that are undefined because the dmask doesn't cover them
945 DemandedElts &= (1 << countPopulation(DMaskVal)) - 1;
946
947 unsigned NewDMaskVal = 0;
948 unsigned OrigLoadIdx = 0;
949 for (unsigned SrcIdx = 0; SrcIdx < 4; ++SrcIdx) {
950 const unsigned Bit = 1 << SrcIdx;
951 if (!!(DMaskVal & Bit)) {
952 if (!!(DemandedElts & (1 << OrigLoadIdx)))
953 NewDMaskVal |= Bit;
954 OrigLoadIdx++;
955 }
956 }
957
958 if (DMaskVal != NewDMaskVal)
959 NewDMask = ConstantInt::get(DMask->getType(), NewDMaskVal);
960 }
961
962 // TODO: Handle 3 vectors when supported in code gen.
963 unsigned NewNumElts = PowerOf2Ceil(DemandedElts.countPopulation());
964 if (!NewNumElts)
965 return UndefValue::get(II->getType());
966
967 if (NewNumElts >= VWidth && DemandedElts.isMask()) {
968 if (NewDMask)
969 II->setArgOperand(DMaskIdx, NewDMask);
970 return nullptr;
971 }
972
973 // Determine the overload types of the original intrinsic.
974 auto IID = II->getIntrinsicID();
975 SmallVector<Intrinsic::IITDescriptor, 16> Table;
976 getIntrinsicInfoTableEntries(IID, Table);
977 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
978
979 FunctionType *FTy = II->getCalledFunction()->getFunctionType();
980 SmallVector<Type *, 6> OverloadTys;
981 Intrinsic::matchIntrinsicType(FTy->getReturnType(), TableRef, OverloadTys);
982 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
983 Intrinsic::matchIntrinsicType(FTy->getParamType(i), TableRef, OverloadTys);
984
985 // Get the new return type overload of the intrinsic.
986 Module *M = II->getParent()->getParent()->getParent();
987 Type *EltTy = II->getType()->getVectorElementType();
988 Type *NewTy = (NewNumElts == 1) ? EltTy : VectorType::get(EltTy, NewNumElts);
989
990 OverloadTys[0] = NewTy;
991 Function *NewIntrin = Intrinsic::getDeclaration(M, IID, OverloadTys);
992
993 SmallVector<Value *, 16> Args;
994 for (unsigned I = 0, E = II->getNumArgOperands(); I != E; ++I)
995 Args.push_back(II->getArgOperand(I));
996
997 if (NewDMask)
998 Args[DMaskIdx] = NewDMask;
999
1000 IRBuilderBase::InsertPointGuard Guard(Builder);
1001 Builder.SetInsertPoint(II);
1002
1003 CallInst *NewCall = Builder.CreateCall(NewIntrin, Args);
1004 NewCall->takeName(II);
1005 NewCall->copyMetadata(*II);
1006
1007 if (NewNumElts == 1) {
1008 return Builder.CreateInsertElement(UndefValue::get(II->getType()), NewCall,
1009 DemandedElts.countTrailingZeros());
1010 }
1011
1012 SmallVector<uint32_t, 8> EltMask;
1013 unsigned NewLoadIdx = 0;
1014 for (unsigned OrigLoadIdx = 0; OrigLoadIdx < VWidth; ++OrigLoadIdx) {
1015 if (!!(DemandedElts & (1 << OrigLoadIdx)))
1016 EltMask.push_back(NewLoadIdx++);
1017 else
1018 EltMask.push_back(NewNumElts);
1019 }
1020
1021 Value *Shuffle =
1022 Builder.CreateShuffleVector(NewCall, UndefValue::get(NewTy), EltMask);
1023
1024 return Shuffle;
1025}
1026
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +00001027/// The specified value produces a vector with any number of elements.
1028/// DemandedElts contains the set of elements that are actually used by the
1029/// caller. This method analyzes which elements of the operand are undef and
1030/// returns that information in UndefElts.
Chris Lattner7e044912010-01-04 07:17:19 +00001031///
1032/// If the information about demanded elements can be used to simplify the
1033/// operation, the operation is simplified, then the resultant value is
1034/// returned. This returns null if no change was made.
1035Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +00001036 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +00001037 unsigned Depth) {
Sanjay Patel9190b4a2016-04-29 20:54:56 +00001038 unsigned VWidth = V->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +00001039 APInt EltMask(APInt::getAllOnesValue(VWidth));
1040 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
1041
1042 if (isa<UndefValue>(V)) {
1043 // If the entire vector is undefined, just return this info.
1044 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +00001045 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +00001046 }
Craig Topper4c947752012-12-22 18:09:02 +00001047
Craig Topper73ba1c82017-06-07 07:40:37 +00001048 if (DemandedElts.isNullValue()) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001049 UndefElts = EltMask;
1050 return UndefValue::get(V->getType());
1051 }
1052
1053 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +00001054
Chris Lattner67058832012-01-25 06:48:06 +00001055 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
1056 if (Constant *C = dyn_cast<Constant>(V)) {
1057 // Check if this is identity. If so, return 0 since we are not simplifying
1058 // anything.
1059 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +00001060 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +00001061
Chris Lattner229907c2011-07-18 04:54:35 +00001062 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +00001063 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +00001064
Chris Lattner67058832012-01-25 06:48:06 +00001065 SmallVector<Constant*, 16> Elts;
1066 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +00001067 if (!DemandedElts[i]) { // If not demanded, set to undef.
1068 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +00001069 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +00001070 continue;
1071 }
Craig Topper4c947752012-12-22 18:09:02 +00001072
Chris Lattner67058832012-01-25 06:48:06 +00001073 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +00001074 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +00001075
Chris Lattner67058832012-01-25 06:48:06 +00001076 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001077 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +00001078 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001079 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +00001080 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +00001081 }
Chris Lattner67058832012-01-25 06:48:06 +00001082 }
Craig Topper4c947752012-12-22 18:09:02 +00001083
Chris Lattner7e044912010-01-04 07:17:19 +00001084 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +00001085 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +00001086 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001087 }
Craig Topper4c947752012-12-22 18:09:02 +00001088
Chris Lattner7e044912010-01-04 07:17:19 +00001089 // Limit search depth.
1090 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +00001091 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001092
Stuart Hastings5bd18b62011-05-17 22:13:31 +00001093 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +00001094 // simplification conservatively assuming that all elements
1095 // are needed.
1096 if (!V->hasOneUse()) {
1097 // Quit if we find multiple users of a non-root value though.
1098 // They'll be handled when it's their turn to be visited by
1099 // the main instcombine process.
1100 if (Depth != 0)
1101 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +00001102 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001103
1104 // Conservatively assume that all elements are needed.
1105 DemandedElts = EltMask;
1106 }
Craig Topper4c947752012-12-22 18:09:02 +00001107
Chris Lattner7e044912010-01-04 07:17:19 +00001108 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +00001109 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +00001110
Chris Lattner7e044912010-01-04 07:17:19 +00001111 bool MadeChange = false;
1112 APInt UndefElts2(VWidth, 0);
Craig Topper23ebd952016-12-11 08:54:52 +00001113 APInt UndefElts3(VWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001114 Value *TmpV;
1115 switch (I->getOpcode()) {
1116 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001117
Chris Lattner7e044912010-01-04 07:17:19 +00001118 case Instruction::InsertElement: {
1119 // If this is a variable index, we don't know which element it overwrites.
1120 // demand exactly the same input as we produce.
1121 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +00001122 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +00001123 // Note that we can't propagate undef elt info, because we don't know
1124 // which elt is getting updated.
1125 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001126 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001127 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1128 break;
1129 }
Craig Topper4c947752012-12-22 18:09:02 +00001130
Sanjay Patele6b48a12017-08-31 15:57:17 +00001131 // The element inserted overwrites whatever was there, so the input demanded
1132 // set is simpler than the output set.
1133 unsigned IdxNo = Idx->getZExtValue();
1134 APInt PreInsertDemandedElts = DemandedElts;
1135 if (IdxNo < VWidth)
1136 PreInsertDemandedElts.clearBit(IdxNo);
1137 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), PreInsertDemandedElts,
1138 UndefElts, Depth + 1);
1139 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1140
Chris Lattner7e044912010-01-04 07:17:19 +00001141 // If this is inserting an element that isn't demanded, remove this
1142 // insertelement.
Chris Lattner7e044912010-01-04 07:17:19 +00001143 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1144 Worklist.Add(I);
1145 return I->getOperand(0);
1146 }
Craig Topper4c947752012-12-22 18:09:02 +00001147
Chris Lattner7e044912010-01-04 07:17:19 +00001148 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001149 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001150 break;
1151 }
1152 case Instruction::ShuffleVector: {
1153 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Craig Topper2e18bcf2016-12-29 04:24:32 +00001154 unsigned LHSVWidth =
1155 Shuffle->getOperand(0)->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +00001156 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1157 for (unsigned i = 0; i < VWidth; i++) {
1158 if (DemandedElts[i]) {
1159 unsigned MaskVal = Shuffle->getMaskValue(i);
1160 if (MaskVal != -1u) {
1161 assert(MaskVal < LHSVWidth * 2 &&
1162 "shufflevector mask index out of range!");
1163 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001164 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001165 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001166 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001167 }
1168 }
1169 }
1170
Alexey Bataevfee90782016-09-23 09:14:08 +00001171 APInt LHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001172 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001173 LHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001174 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1175
Alexey Bataevfee90782016-09-23 09:14:08 +00001176 APInt RHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001177 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001178 RHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001179 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1180
1181 bool NewUndefElts = false;
Alexey Bataev793c9462016-09-26 13:18:59 +00001182 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1183 unsigned RHSIdx = -1u, RHSValIdx = -1u;
Alexey Bataevfee90782016-09-23 09:14:08 +00001184 bool LHSUniform = true;
1185 bool RHSUniform = true;
Chris Lattner7e044912010-01-04 07:17:19 +00001186 for (unsigned i = 0; i < VWidth; i++) {
1187 unsigned MaskVal = Shuffle->getMaskValue(i);
1188 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001189 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001190 } else if (!DemandedElts[i]) {
1191 NewUndefElts = true;
1192 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001193 } else if (MaskVal < LHSVWidth) {
Alexey Bataevfee90782016-09-23 09:14:08 +00001194 if (LHSUndefElts[MaskVal]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001195 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001196 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001197 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001198 LHSIdx = LHSIdx == -1u ? i : LHSVWidth;
1199 LHSValIdx = LHSValIdx == -1u ? MaskVal : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001200 LHSUniform = LHSUniform && (MaskVal == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001201 }
1202 } else {
Alexey Bataevfee90782016-09-23 09:14:08 +00001203 if (RHSUndefElts[MaskVal - LHSVWidth]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001204 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001205 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001206 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001207 RHSIdx = RHSIdx == -1u ? i : LHSVWidth;
1208 RHSValIdx = RHSValIdx == -1u ? MaskVal - LHSVWidth : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001209 RHSUniform = RHSUniform && (MaskVal - LHSVWidth == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001210 }
1211 }
1212 }
1213
Alexey Bataevfee90782016-09-23 09:14:08 +00001214 // Try to transform shuffle with constant vector and single element from
1215 // this constant vector to single insertelement instruction.
1216 // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
1217 // insertelement V, C[ci], ci-n
1218 if (LHSVWidth == Shuffle->getType()->getNumElements()) {
1219 Value *Op = nullptr;
1220 Constant *Value = nullptr;
1221 unsigned Idx = -1u;
1222
Craig Topper62f06e22016-12-29 05:38:31 +00001223 // Find constant vector with the single element in shuffle (LHS or RHS).
Alexey Bataevfee90782016-09-23 09:14:08 +00001224 if (LHSIdx < LHSVWidth && RHSUniform) {
1225 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1226 Op = Shuffle->getOperand(1);
Alexey Bataev793c9462016-09-26 13:18:59 +00001227 Value = CV->getOperand(LHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001228 Idx = LHSIdx;
1229 }
1230 }
1231 if (RHSIdx < LHSVWidth && LHSUniform) {
1232 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1233 Op = Shuffle->getOperand(0);
Alexey Bataev793c9462016-09-26 13:18:59 +00001234 Value = CV->getOperand(RHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001235 Idx = RHSIdx;
1236 }
1237 }
1238 // Found constant vector with single element - convert to insertelement.
1239 if (Op && Value) {
1240 Instruction *New = InsertElementInst::Create(
1241 Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx),
1242 Shuffle->getName());
1243 InsertNewInstWith(New, *Shuffle);
1244 return New;
1245 }
1246 }
Chris Lattner7e044912010-01-04 07:17:19 +00001247 if (NewUndefElts) {
1248 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001249 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001250 for (unsigned i = 0; i < VWidth; ++i) {
1251 if (UndefElts[i])
1252 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1253 else
1254 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1255 Shuffle->getMaskValue(i)));
1256 }
1257 I->setOperand(2, ConstantVector::get(Elts));
1258 MadeChange = true;
1259 }
1260 break;
1261 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001262 case Instruction::Select: {
1263 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1264 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1265 for (unsigned i = 0; i < VWidth; i++) {
Andrea Di Biagio40f59e42015-10-06 10:34:53 +00001266 Constant *CElt = CV->getAggregateElement(i);
1267 // Method isNullValue always returns false when called on a
1268 // ConstantExpr. If CElt is a ConstantExpr then skip it in order to
1269 // to avoid propagating incorrect information.
1270 if (isa<ConstantExpr>(CElt))
1271 continue;
1272 if (CElt->isNullValue())
Pete Cooperabc13af2012-07-26 23:10:24 +00001273 LeftDemanded.clearBit(i);
1274 else
1275 RightDemanded.clearBit(i);
1276 }
1277 }
1278
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001279 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded, UndefElts,
1280 Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001281 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1282
1283 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001284 UndefElts2, Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001285 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001286
Pete Cooperabc13af2012-07-26 23:10:24 +00001287 // Output elements are undefined if both are undefined.
1288 UndefElts &= UndefElts2;
1289 break;
1290 }
Chris Lattner7e044912010-01-04 07:17:19 +00001291 case Instruction::BitCast: {
1292 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001293 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001294 if (!VTy) break;
1295 unsigned InVWidth = VTy->getNumElements();
1296 APInt InputDemandedElts(InVWidth, 0);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001297 UndefElts2 = APInt(InVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001298 unsigned Ratio;
1299
1300 if (VWidth == InVWidth) {
1301 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1302 // elements as are demanded of us.
1303 Ratio = 1;
1304 InputDemandedElts = DemandedElts;
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001305 } else if ((VWidth % InVWidth) == 0) {
1306 // If the number of elements in the output is a multiple of the number of
1307 // elements in the input then an input element is live if any of the
1308 // corresponding output elements are live.
1309 Ratio = VWidth / InVWidth;
1310 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Chris Lattner7e044912010-01-04 07:17:19 +00001311 if (DemandedElts[OutIdx])
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001312 InputDemandedElts.setBit(OutIdx / Ratio);
1313 } else if ((InVWidth % VWidth) == 0) {
1314 // If the number of elements in the input is a multiple of the number of
1315 // elements in the output then an input element is live if the
1316 // corresponding output element is live.
1317 Ratio = InVWidth / VWidth;
Chris Lattner7e044912010-01-04 07:17:19 +00001318 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001319 if (DemandedElts[InIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001320 InputDemandedElts.setBit(InIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001321 } else {
1322 // Unsupported so far.
1323 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001324 }
Craig Topper4c947752012-12-22 18:09:02 +00001325
Chris Lattner7e044912010-01-04 07:17:19 +00001326 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001327 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001328 if (TmpV) {
1329 I->setOperand(0, TmpV);
1330 MadeChange = true;
1331 }
Craig Topper4c947752012-12-22 18:09:02 +00001332
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001333 if (VWidth == InVWidth) {
1334 UndefElts = UndefElts2;
1335 } else if ((VWidth % InVWidth) == 0) {
1336 // If the number of elements in the output is a multiple of the number of
1337 // elements in the input then an output element is undef if the
1338 // corresponding input element is undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001339 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001340 if (UndefElts2[OutIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001341 UndefElts.setBit(OutIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001342 } else if ((InVWidth % VWidth) == 0) {
1343 // If the number of elements in the input is a multiple of the number of
1344 // elements in the output then an output element is undef if all of the
1345 // corresponding input elements are undef.
1346 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1347 APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
1348 if (SubUndef.countPopulation() == Ratio)
1349 UndefElts.setBit(OutIdx);
1350 }
1351 } else {
Chris Lattner7e044912010-01-04 07:17:19 +00001352 llvm_unreachable("Unimp");
Chris Lattner7e044912010-01-04 07:17:19 +00001353 }
1354 break;
1355 }
1356 case Instruction::And:
1357 case Instruction::Or:
1358 case Instruction::Xor:
1359 case Instruction::Add:
1360 case Instruction::Sub:
1361 case Instruction::Mul:
1362 // div/rem demand all inputs, because they don't want divide by zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001363 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1364 Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001365 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1366 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001367 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001368 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001369
Chris Lattner7e044912010-01-04 07:17:19 +00001370 // Output elements are undefined if both are undefined. Consider things
1371 // like undef&0. The result is known zero, not undef.
1372 UndefElts &= UndefElts2;
1373 break;
Pete Coopere807e452012-07-26 22:37:04 +00001374 case Instruction::FPTrunc:
1375 case Instruction::FPExt:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001376 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1377 Depth + 1);
Pete Coopere807e452012-07-26 22:37:04 +00001378 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1379 break;
Craig Topper4c947752012-12-22 18:09:02 +00001380
Chris Lattner7e044912010-01-04 07:17:19 +00001381 case Instruction::Call: {
1382 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1383 if (!II) break;
1384 switch (II->getIntrinsicID()) {
Craig Topper7fc6d342016-12-11 22:32:38 +00001385 case Intrinsic::x86_xop_vfrcz_ss:
1386 case Intrinsic::x86_xop_vfrcz_sd:
1387 // The instructions for these intrinsics are speced to zero upper bits not
1388 // pass them through like other scalar intrinsics. So we shouldn't just
1389 // use Arg0 if DemandedElts[0] is clear like we do for other intrinsics.
1390 // Instead we should return a zero vector.
Craig Topper1a8a3372016-12-29 03:30:17 +00001391 if (!DemandedElts[0]) {
1392 Worklist.Add(II);
Craig Topper7fc6d342016-12-11 22:32:38 +00001393 return ConstantAggregateZero::get(II->getType());
Craig Topper1a8a3372016-12-29 03:30:17 +00001394 }
Craig Topper7fc6d342016-12-11 22:32:38 +00001395
Craig Topperac75bca2016-12-13 07:45:45 +00001396 // Only the lower element is used.
1397 DemandedElts = 1;
Craig Topper7fc6d342016-12-11 22:32:38 +00001398 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1399 UndefElts, Depth + 1);
1400 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperac75bca2016-12-13 07:45:45 +00001401
1402 // Only the lower element is undefined. The high elements are zero.
1403 UndefElts = UndefElts[0];
Craig Topper7fc6d342016-12-11 22:32:38 +00001404 break;
1405
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001406 // Unary scalar-as-vector operations that work column-wise.
Simon Pilgrim83020942016-04-24 18:23:14 +00001407 case Intrinsic::x86_sse_rcp_ss:
1408 case Intrinsic::x86_sse_rsqrt_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001409 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1410 UndefElts, Depth + 1);
1411 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1412
1413 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001414 if (!DemandedElts[0]) {
1415 Worklist.Add(II);
Simon Pilgrim83020942016-04-24 18:23:14 +00001416 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001417 }
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001418 // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions
1419 // checks).
Simon Pilgrim83020942016-04-24 18:23:14 +00001420 break;
1421
Craig Toppera0372de2016-12-14 03:17:27 +00001422 // Binary scalar-as-vector operations that work column-wise. The high
1423 // elements come from operand 0. The low element is a function of both
1424 // operands.
Chris Lattner7e044912010-01-04 07:17:19 +00001425 case Intrinsic::x86_sse_min_ss:
1426 case Intrinsic::x86_sse_max_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001427 case Intrinsic::x86_sse_cmp_ss:
Chris Lattner7e044912010-01-04 07:17:19 +00001428 case Intrinsic::x86_sse2_min_sd:
1429 case Intrinsic::x86_sse2_max_sd:
Craig Toppera0372de2016-12-14 03:17:27 +00001430 case Intrinsic::x86_sse2_cmp_sd: {
1431 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1432 UndefElts, Depth + 1);
1433 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1434
1435 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001436 if (!DemandedElts[0]) {
1437 Worklist.Add(II);
Craig Toppera0372de2016-12-14 03:17:27 +00001438 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001439 }
Craig Toppera0372de2016-12-14 03:17:27 +00001440
1441 // Only lower element is used for operand 1.
1442 DemandedElts = 1;
1443 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1444 UndefElts2, Depth + 1);
1445 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1446
1447 // Lower element is undefined if both lower elements are undefined.
1448 // Consider things like undef&0. The result is known zero, not undef.
1449 if (!UndefElts2[0])
1450 UndefElts.clearBit(0);
1451
1452 break;
1453 }
1454
Craig Toppereb6a20e2016-12-14 03:17:30 +00001455 // Binary scalar-as-vector operations that work column-wise. The high
1456 // elements come from operand 0 and the low element comes from operand 1.
Simon Pilgrim83020942016-04-24 18:23:14 +00001457 case Intrinsic::x86_sse41_round_ss:
Craig Toppereb6a20e2016-12-14 03:17:30 +00001458 case Intrinsic::x86_sse41_round_sd: {
1459 // Don't use the low element of operand 0.
1460 APInt DemandedElts2 = DemandedElts;
1461 DemandedElts2.clearBit(0);
1462 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001463 UndefElts, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001464 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001465
1466 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001467 if (!DemandedElts[0]) {
1468 Worklist.Add(II);
Craig Toppereb6a20e2016-12-14 03:17:30 +00001469 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001470 }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001471
1472 // Only lower element is used for operand 1.
1473 DemandedElts = 1;
Gabor Greife23efee2010-06-28 16:45:00 +00001474 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001475 UndefElts2, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001476 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001477
Craig Toppereb6a20e2016-12-14 03:17:30 +00001478 // Take the high undef elements from operand 0 and take the lower element
1479 // from operand 1.
1480 UndefElts.clearBit(0);
1481 UndefElts |= UndefElts2[0];
Chris Lattner7e044912010-01-04 07:17:19 +00001482 break;
Craig Toppereb6a20e2016-12-14 03:17:30 +00001483 }
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001484
Craig Topperdfd268d2016-12-14 05:43:05 +00001485 // Three input scalar-as-vector operations that work column-wise. The high
1486 // elements come from operand 0 and the low element is a function of all
1487 // three inputs.
Craig Topper268b3ab2016-12-14 06:06:58 +00001488 case Intrinsic::x86_avx512_mask_add_ss_round:
1489 case Intrinsic::x86_avx512_mask_div_ss_round:
1490 case Intrinsic::x86_avx512_mask_mul_ss_round:
1491 case Intrinsic::x86_avx512_mask_sub_ss_round:
1492 case Intrinsic::x86_avx512_mask_max_ss_round:
1493 case Intrinsic::x86_avx512_mask_min_ss_round:
1494 case Intrinsic::x86_avx512_mask_add_sd_round:
1495 case Intrinsic::x86_avx512_mask_div_sd_round:
1496 case Intrinsic::x86_avx512_mask_mul_sd_round:
1497 case Intrinsic::x86_avx512_mask_sub_sd_round:
1498 case Intrinsic::x86_avx512_mask_max_sd_round:
1499 case Intrinsic::x86_avx512_mask_min_sd_round:
Craig Topper23ebd952016-12-11 08:54:52 +00001500 case Intrinsic::x86_fma_vfmadd_ss:
Craig Topper23ebd952016-12-11 08:54:52 +00001501 case Intrinsic::x86_fma_vfmadd_sd:
Craig Topperab5f3552016-12-15 03:49:45 +00001502 case Intrinsic::x86_avx512_mask_vfmadd_ss:
1503 case Intrinsic::x86_avx512_mask_vfmadd_sd:
1504 case Intrinsic::x86_avx512_maskz_vfmadd_ss:
1505 case Intrinsic::x86_avx512_maskz_vfmadd_sd:
Craig Topper23ebd952016-12-11 08:54:52 +00001506 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1507 UndefElts, Depth + 1);
1508 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperdfd268d2016-12-14 05:43:05 +00001509
1510 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001511 if (!DemandedElts[0]) {
1512 Worklist.Add(II);
Craig Topperdfd268d2016-12-14 05:43:05 +00001513 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001514 }
Craig Topperdfd268d2016-12-14 05:43:05 +00001515
1516 // Only lower element is used for operand 1 and 2.
1517 DemandedElts = 1;
Craig Topper23ebd952016-12-11 08:54:52 +00001518 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1519 UndefElts2, Depth + 1);
1520 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1521 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1522 UndefElts3, Depth + 1);
1523 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1524
Craig Topperdfd268d2016-12-14 05:43:05 +00001525 // Lower element is undefined if all three lower elements are undefined.
1526 // Consider things like undef&0. The result is known zero, not undef.
1527 if (!UndefElts2[0] || !UndefElts3[0])
1528 UndefElts.clearBit(0);
Craig Topper23ebd952016-12-11 08:54:52 +00001529
Craig Topper23ebd952016-12-11 08:54:52 +00001530 break;
1531
Craig Topperab5f3552016-12-15 03:49:45 +00001532 case Intrinsic::x86_avx512_mask3_vfmadd_ss:
1533 case Intrinsic::x86_avx512_mask3_vfmadd_sd:
1534 case Intrinsic::x86_avx512_mask3_vfmsub_ss:
1535 case Intrinsic::x86_avx512_mask3_vfmsub_sd:
1536 case Intrinsic::x86_avx512_mask3_vfnmsub_ss:
1537 case Intrinsic::x86_avx512_mask3_vfnmsub_sd:
1538 // These intrinsics get the passthru bits from operand 2.
1539 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1540 UndefElts, Depth + 1);
1541 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1542
1543 // If lowest element of a scalar op isn't used then use Arg2.
Craig Topper1a8a3372016-12-29 03:30:17 +00001544 if (!DemandedElts[0]) {
1545 Worklist.Add(II);
Craig Topperab5f3552016-12-15 03:49:45 +00001546 return II->getArgOperand(2);
Craig Topper1a8a3372016-12-29 03:30:17 +00001547 }
Craig Topperab5f3552016-12-15 03:49:45 +00001548
1549 // Only lower element is used for operand 0 and 1.
1550 DemandedElts = 1;
1551 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1552 UndefElts2, Depth + 1);
1553 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1554 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1555 UndefElts3, Depth + 1);
1556 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1557
1558 // Lower element is undefined if all three lower elements are undefined.
1559 // Consider things like undef&0. The result is known zero, not undef.
1560 if (!UndefElts2[0] || !UndefElts3[0])
1561 UndefElts.clearBit(0);
1562
1563 break;
1564
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001565 case Intrinsic::x86_sse2_packssdw_128:
1566 case Intrinsic::x86_sse2_packsswb_128:
1567 case Intrinsic::x86_sse2_packuswb_128:
1568 case Intrinsic::x86_sse41_packusdw:
1569 case Intrinsic::x86_avx2_packssdw:
1570 case Intrinsic::x86_avx2_packsswb:
1571 case Intrinsic::x86_avx2_packusdw:
Craig Topper3731f4d2017-02-16 07:35:23 +00001572 case Intrinsic::x86_avx2_packuswb:
1573 case Intrinsic::x86_avx512_packssdw_512:
1574 case Intrinsic::x86_avx512_packsswb_512:
1575 case Intrinsic::x86_avx512_packusdw_512:
1576 case Intrinsic::x86_avx512_packuswb_512: {
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001577 auto *Ty0 = II->getArgOperand(0)->getType();
1578 unsigned InnerVWidth = Ty0->getVectorNumElements();
1579 assert(VWidth == (InnerVWidth * 2) && "Unexpected input size");
1580
1581 unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128;
1582 unsigned VWidthPerLane = VWidth / NumLanes;
1583 unsigned InnerVWidthPerLane = InnerVWidth / NumLanes;
1584
1585 // Per lane, pack the elements of the first input and then the second.
1586 // e.g.
1587 // v8i16 PACK(v4i32 X, v4i32 Y) - (X[0..3],Y[0..3])
1588 // v32i8 PACK(v16i16 X, v16i16 Y) - (X[0..7],Y[0..7]),(X[8..15],Y[8..15])
1589 for (int OpNum = 0; OpNum != 2; ++OpNum) {
1590 APInt OpDemandedElts(InnerVWidth, 0);
1591 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1592 unsigned LaneIdx = Lane * VWidthPerLane;
1593 for (unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) {
1594 unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum;
1595 if (DemandedElts[Idx])
1596 OpDemandedElts.setBit((Lane * InnerVWidthPerLane) + Elt);
1597 }
1598 }
1599
1600 // Demand elements from the operand.
1601 auto *Op = II->getArgOperand(OpNum);
1602 APInt OpUndefElts(InnerVWidth, 0);
1603 TmpV = SimplifyDemandedVectorElts(Op, OpDemandedElts, OpUndefElts,
1604 Depth + 1);
1605 if (TmpV) {
1606 II->setArgOperand(OpNum, TmpV);
1607 MadeChange = true;
1608 }
1609
1610 // Pack the operand's UNDEF elements, one lane at a time.
1611 OpUndefElts = OpUndefElts.zext(VWidth);
1612 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1613 APInt LaneElts = OpUndefElts.lshr(InnerVWidthPerLane * Lane);
1614 LaneElts = LaneElts.getLoBits(InnerVWidthPerLane);
Craig Topper24e71012017-04-28 03:36:24 +00001615 LaneElts <<= InnerVWidthPerLane * (2 * Lane + OpNum);
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001616 UndefElts |= LaneElts;
1617 }
1618 }
1619 break;
1620 }
1621
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001622 // PSHUFB
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001623 case Intrinsic::x86_ssse3_pshuf_b_128:
1624 case Intrinsic::x86_avx2_pshuf_b:
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001625 case Intrinsic::x86_avx512_pshuf_b_512:
1626 // PERMILVAR
1627 case Intrinsic::x86_avx_vpermilvar_ps:
1628 case Intrinsic::x86_avx_vpermilvar_ps_256:
1629 case Intrinsic::x86_avx512_vpermilvar_ps_512:
1630 case Intrinsic::x86_avx_vpermilvar_pd:
1631 case Intrinsic::x86_avx_vpermilvar_pd_256:
Simon Pilgrimfe2c0ed2017-01-18 14:47:49 +00001632 case Intrinsic::x86_avx512_vpermilvar_pd_512:
1633 // PERMV
1634 case Intrinsic::x86_avx2_permd:
1635 case Intrinsic::x86_avx2_permps: {
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001636 Value *Op1 = II->getArgOperand(1);
1637 TmpV = SimplifyDemandedVectorElts(Op1, DemandedElts, UndefElts,
1638 Depth + 1);
1639 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1640 break;
1641 }
1642
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001643 // SSE4A instructions leave the upper 64-bits of the 128-bit result
1644 // in an undefined state.
1645 case Intrinsic::x86_sse4a_extrq:
1646 case Intrinsic::x86_sse4a_extrqi:
1647 case Intrinsic::x86_sse4a_insertq:
1648 case Intrinsic::x86_sse4a_insertqi:
Craig Topper3a86a042017-03-19 05:49:16 +00001649 UndefElts.setHighBits(VWidth / 2);
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001650 break;
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001651 case Intrinsic::amdgcn_buffer_load:
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001652 case Intrinsic::amdgcn_buffer_load_format:
Nicolai Haehnledb6911a2018-06-21 13:37:45 +00001653 return simplifyAMDGCNMemoryIntrinsicDemanded(II, DemandedElts);
Nicolai Haehnleb29ee702018-06-21 13:37:31 +00001654 default: {
1655 if (getAMDGPUImageDMaskIntrinsic(II->getIntrinsicID()))
1656 return simplifyAMDGCNMemoryIntrinsicDemanded(II, DemandedElts, 0);
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001657
Nicolai Haehnleb29ee702018-06-21 13:37:31 +00001658 break;
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001659 }
Chris Lattner7e044912010-01-04 07:17:19 +00001660 }
1661 break;
1662 }
1663 }
Craig Topperf40110f2014-04-25 05:29:35 +00001664 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001665}