blob: 9b58fae5e4cd6728f49889d3e0342ee4258dd40b [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
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000026/// Check to see if the specified operand of the specified instruction is a
27/// constant integer. If so, check to see if there are any bits set in the
28/// constant that are not demanded. If so, shrink the constant and return true.
Craig Topper4c947752012-12-22 18:09:02 +000029static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
Craig Topper358cd9a2017-04-20 23:58:27 +000030 const APInt &Demanded) {
Chris Lattner7e044912010-01-04 07:17:19 +000031 assert(I && "No instruction?");
32 assert(OpNo < I->getNumOperands() && "Operand index too large");
33
Sanjay Patelae3b43e2017-02-09 21:43:06 +000034 // The operand must be a constant integer or splat integer.
35 Value *Op = I->getOperand(OpNo);
36 const APInt *C;
37 if (!match(Op, m_APInt(C)))
38 return false;
Chris Lattner7e044912010-01-04 07:17:19 +000039
40 // If there are no bits set that aren't demanded, nothing to do.
Craig Toppera8129a12017-04-20 16:17:13 +000041 if (C->isSubsetOf(Demanded))
Chris Lattner7e044912010-01-04 07:17:19 +000042 return false;
43
44 // This instruction is producing bits that are not demanded. Shrink the RHS.
Craig Topper358cd9a2017-04-20 23:58:27 +000045 I->setOperand(OpNo, ConstantInt::get(Op->getType(), *C & Demanded));
David Majnemer42b83a52014-08-22 07:56:32 +000046
Chris Lattner7e044912010-01-04 07:17:19 +000047 return true;
48}
49
50
51
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000052/// Inst is an integer instruction that SimplifyDemandedBits knows about. See if
53/// the instruction has any properties that allow us to simplify its operands.
Chris Lattner7e044912010-01-04 07:17:19 +000054bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
55 unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
Craig Topperb45eabc2017-04-26 16:39:58 +000056 KnownBits Known(BitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +000057 APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
Craig Topper4c947752012-12-22 18:09:02 +000058
Craig Topperb45eabc2017-04-26 16:39:58 +000059 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, Known,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000060 0, &Inst);
Craig Topperf40110f2014-04-25 05:29:35 +000061 if (!V) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000062 if (V == &Inst) return true;
Sanjay Patel4b198802016-02-01 22:23:39 +000063 replaceInstUsesWith(Inst, V);
Chris Lattner7e044912010-01-04 07:17:19 +000064 return true;
65}
66
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000067/// This form of SimplifyDemandedBits simplifies the specified instruction
68/// operand if possible, updating it in place. It returns true if it made any
69/// change and false otherwise.
Craig Topper47596dd2017-03-25 06:52:52 +000070bool InstCombiner::SimplifyDemandedBits(Instruction *I, unsigned OpNo,
71 const APInt &DemandedMask,
Craig Topperb45eabc2017-04-26 16:39:58 +000072 KnownBits &Known,
Chris Lattner7e044912010-01-04 07:17:19 +000073 unsigned Depth) {
Craig Topper47596dd2017-03-25 06:52:52 +000074 Use &U = I->getOperandUse(OpNo);
Craig Topperb45eabc2017-04-26 16:39:58 +000075 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, Known,
76 Depth, I);
Craig Topperf40110f2014-04-25 05:29:35 +000077 if (!NewVal) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000078 U = NewVal;
79 return true;
80}
81
82
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000083/// This function attempts to replace V with a simpler value based on the
84/// demanded bits. When this function is called, it is known that only the bits
85/// set in DemandedMask of the result of V are ever used downstream.
86/// Consequently, depending on the mask and V, it may be possible to replace V
87/// with a constant or one of its operands. In such cases, this function does
88/// the replacement and returns true. In all other cases, it returns false after
89/// analyzing the expression and setting KnownOne and known to be one in the
Craig Topperb45eabc2017-04-26 16:39:58 +000090/// expression. Known.Zero contains all the bits that are known to be zero in
91/// the expression. These are provided to potentially allow the caller (which
92/// might recursively be SimplifyDemandedBits itself) to simplify the
93/// expression.
94/// Known.One and Known.Zero always follow the invariant that:
95/// Known.One & Known.Zero == 0.
96/// That is, a bit can't be both 1 and 0. Note that the bits in Known.One and
97/// Known.Zero may only be accurate for those bits set in DemandedMask. Note
98/// also that the bitwidth of V, DemandedMask, Known.Zero and Known.One must all
99/// be the same.
Chris Lattner7e044912010-01-04 07:17:19 +0000100///
101/// This returns null if it did not change anything and it permits no
102/// simplification. This returns V itself if it did some simplification of V's
103/// operands based on the information about what bits are demanded. This returns
104/// some other non-null value if it found out that V is equal to another value
105/// in the context where the specified bits are demanded, but not for all users.
106Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
Craig Topperb45eabc2017-04-26 16:39:58 +0000107 KnownBits &Known, unsigned Depth,
Hal Finkel60db0582014-09-07 18:57:58 +0000108 Instruction *CxtI) {
Craig Toppere73658d2014-04-28 04:05:08 +0000109 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000110 assert(Depth <= 6 && "Limit Search Depth");
111 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000112 Type *VTy = V->getType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000113 assert(
114 (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
Craig Topperb45eabc2017-04-26 16:39:58 +0000115 Known.getBitWidth() == BitWidth &&
116 "Value *V, DemandedMask and Known must have same BitWidth");
Craig Topper83dc1c62017-04-20 16:14:58 +0000117
118 if (isa<Constant>(V)) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000119 computeKnownBits(V, Known, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000120 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000121 }
122
Craig Topperf0aeee02017-05-05 17:36:09 +0000123 Known.resetAll();
Craig Topper83dc1c62017-04-20 16:14:58 +0000124 if (DemandedMask == 0) // Not demanding any bits from V.
Chris Lattner7e044912010-01-04 07:17:19 +0000125 return UndefValue::get(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000126
Chris Lattner7e044912010-01-04 07:17:19 +0000127 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000128 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000129
Chris Lattner7e044912010-01-04 07:17:19 +0000130 Instruction *I = dyn_cast<Instruction>(V);
131 if (!I) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000132 computeKnownBits(V, Known, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000133 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000134 }
135
136 // If there are multiple uses of this value and we aren't at the root, then
137 // we can't do any simplifications of the operands, because DemandedMask
138 // only reflects the bits demanded by *one* of the users.
Craig Topper7603dce2017-04-25 16:48:19 +0000139 if (Depth != 0 && !I->hasOneUse())
Craig Topperb45eabc2017-04-26 16:39:58 +0000140 return SimplifyMultipleUseDemandedBits(I, DemandedMask, Known, Depth, CxtI);
Craig Topper4c947752012-12-22 18:09:02 +0000141
Craig Topperb45eabc2017-04-26 16:39:58 +0000142 KnownBits LHSKnown(BitWidth), RHSKnown(BitWidth);
Craig Topperb0076fe2017-04-12 18:05:21 +0000143
Chris Lattner7e044912010-01-04 07:17:19 +0000144 // If this is the root being simplified, allow it to have multiple uses,
145 // just set the DemandedMask to all bits so that we can try to simplify the
146 // operands. This allows visitTruncInst (for example) to simplify the
147 // operand of a trunc without duplicating all the logic below.
148 if (Depth == 0 && !V->hasOneUse())
Craig Toppere06b6bc2017-04-04 05:03:02 +0000149 DemandedMask.setAllBits();
Craig Topper4c947752012-12-22 18:09:02 +0000150
Chris Lattner7e044912010-01-04 07:17:19 +0000151 switch (I->getOpcode()) {
152 default:
Craig Topperb45eabc2017-04-26 16:39:58 +0000153 computeKnownBits(I, Known, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000154 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000155 case Instruction::And: {
Chris Lattner7e044912010-01-04 07:17:19 +0000156 // If either the LHS or the RHS are Zero, the result is zero.
Craig Topperb45eabc2017-04-26 16:39:58 +0000157 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
158 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.Zero, LHSKnown,
159 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000160 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000161 assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
162 assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000163
Craig Topper9a458cd2017-04-14 22:34:14 +0000164 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000165 APInt IKnownZero = RHSKnown.Zero | LHSKnown.Zero;
Craig Topper9a458cd2017-04-14 22:34:14 +0000166 // Output known-1 bits are only known if set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000167 APInt IKnownOne = RHSKnown.One & LHSKnown.One;
Craig Topper9a458cd2017-04-14 22:34:14 +0000168
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000169 // If the client is only demanding bits that we know, return the known
170 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000171 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000172 return Constant::getIntegerValue(VTy, IKnownOne);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000173
Chris Lattner7e044912010-01-04 07:17:19 +0000174 // If all of the demanded bits are known 1 on one side, return the other.
175 // These bits cannot contribute to the result of the 'and'.
Craig Topperb45eabc2017-04-26 16:39:58 +0000176 if (DemandedMask.isSubsetOf(LHSKnown.Zero | RHSKnown.One))
Chris Lattner7e044912010-01-04 07:17:19 +0000177 return I->getOperand(0);
Craig Topperb45eabc2017-04-26 16:39:58 +0000178 if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.One))
Chris Lattner7e044912010-01-04 07:17:19 +0000179 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000180
Chris Lattner7e044912010-01-04 07:17:19 +0000181 // If the RHS is a constant, see if we can simplify it.
Craig Topperb45eabc2017-04-26 16:39:58 +0000182 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnown.Zero))
Chris Lattner7e044912010-01-04 07:17:19 +0000183 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000184
Craig Topperb45eabc2017-04-26 16:39:58 +0000185 Known.Zero = std::move(IKnownZero);
186 Known.One = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000187 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000188 }
189 case Instruction::Or: {
Chris Lattner7e044912010-01-04 07:17:19 +0000190 // If either the LHS or the RHS are One, the result is One.
Craig Topperb45eabc2017-04-26 16:39:58 +0000191 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
192 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.One, LHSKnown,
193 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000194 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000195 assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
196 assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
Craig Topper4c947752012-12-22 18:09:02 +0000197
Craig Topper9a458cd2017-04-14 22:34:14 +0000198 // Output known-0 bits are only known if clear in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000199 APInt IKnownZero = RHSKnown.Zero & LHSKnown.Zero;
200 // Output known-1 are known. to be set if s.et in either the LHS | RHS.
201 APInt IKnownOne = RHSKnown.One | LHSKnown.One;
Craig Topper9a458cd2017-04-14 22:34:14 +0000202
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000203 // If the client is only demanding bits that we know, return the known
204 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000205 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000206 return Constant::getIntegerValue(VTy, IKnownOne);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000207
Chris Lattner7e044912010-01-04 07:17:19 +0000208 // If all of the demanded bits are known zero on one side, return the other.
209 // These bits cannot contribute to the result of the 'or'.
Craig Topperb45eabc2017-04-26 16:39:58 +0000210 if (DemandedMask.isSubsetOf(LHSKnown.One | RHSKnown.Zero))
Chris Lattner7e044912010-01-04 07:17:19 +0000211 return I->getOperand(0);
Craig Topperb45eabc2017-04-26 16:39:58 +0000212 if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero))
Chris Lattner7e044912010-01-04 07:17:19 +0000213 return I->getOperand(1);
214
Chris Lattner7e044912010-01-04 07:17:19 +0000215 // If the RHS is a constant, see if we can simplify it.
216 if (ShrinkDemandedConstant(I, 1, DemandedMask))
217 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000218
Craig Topperb45eabc2017-04-26 16:39:58 +0000219 Known.Zero = std::move(IKnownZero);
220 Known.One = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000221 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000222 }
Chris Lattner7e044912010-01-04 07:17:19 +0000223 case Instruction::Xor: {
Craig Topperb45eabc2017-04-26 16:39:58 +0000224 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
225 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000226 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000227 assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
228 assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
Craig Topper4c947752012-12-22 18:09:02 +0000229
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000230 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000231 APInt IKnownZero = (RHSKnown.Zero & LHSKnown.Zero) |
232 (RHSKnown.One & LHSKnown.One);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000233 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000234 APInt IKnownOne = (RHSKnown.Zero & LHSKnown.One) |
235 (RHSKnown.One & LHSKnown.Zero);
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000236
237 // If the client is only demanding bits that we know, return the known
238 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000239 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000240 return Constant::getIntegerValue(VTy, IKnownOne);
241
Chris Lattner7e044912010-01-04 07:17:19 +0000242 // If all of the demanded bits are known zero on one side, return the other.
243 // These bits cannot contribute to the result of the 'xor'.
Craig Topperb45eabc2017-04-26 16:39:58 +0000244 if (DemandedMask.isSubsetOf(RHSKnown.Zero))
Chris Lattner7e044912010-01-04 07:17:19 +0000245 return I->getOperand(0);
Craig Topperb45eabc2017-04-26 16:39:58 +0000246 if (DemandedMask.isSubsetOf(LHSKnown.Zero))
Chris Lattner7e044912010-01-04 07:17:19 +0000247 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000248
Chris Lattner7e044912010-01-04 07:17:19 +0000249 // If all of the demanded bits are known to be zero on one side or the
250 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000251 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Craig Topperb45eabc2017-04-26 16:39:58 +0000252 if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.Zero)) {
Craig Topper4c947752012-12-22 18:09:02 +0000253 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000254 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
255 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000256 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000257 }
Craig Topper4c947752012-12-22 18:09:02 +0000258
Chris Lattner7e044912010-01-04 07:17:19 +0000259 // If all of the demanded bits on one side are known, and all of the set
260 // bits on that side are also known to be set on the other side, turn this
261 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000262 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topperb45eabc2017-04-26 16:39:58 +0000263 if (DemandedMask.isSubsetOf(RHSKnown.Zero|RHSKnown.One) &&
264 RHSKnown.One.isSubsetOf(LHSKnown.One)) {
Craig Topper17f37ba2017-04-20 20:47:35 +0000265 Constant *AndC = Constant::getIntegerValue(VTy,
Craig Topperb45eabc2017-04-26 16:39:58 +0000266 ~RHSKnown.One & DemandedMask);
Craig Topper17f37ba2017-04-20 20:47:35 +0000267 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
268 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000269 }
Craig Topper4c947752012-12-22 18:09:02 +0000270
Sanjay Patel8ce1d4c2017-04-21 20:29:17 +0000271 // If the RHS is a constant, see if we can simplify it.
272 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
273 if (ShrinkDemandedConstant(I, 1, DemandedMask))
274 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000275
Chris Lattner7e044912010-01-04 07:17:19 +0000276 // If our LHS is an 'and' and if it has one use, and if any of the bits we
277 // are flipping are known to be set, then the xor is just resetting those
278 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
279 // simplifying both of them.
280 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
281 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
282 isa<ConstantInt>(I->getOperand(1)) &&
283 isa<ConstantInt>(LHSInst->getOperand(1)) &&
Craig Topperb45eabc2017-04-26 16:39:58 +0000284 (LHSKnown.One & RHSKnown.One & DemandedMask) != 0) {
Chris Lattner7e044912010-01-04 07:17:19 +0000285 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
286 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
Craig Topperb45eabc2017-04-26 16:39:58 +0000287 APInt NewMask = ~(LHSKnown.One & RHSKnown.One & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000288
Chris Lattner7e044912010-01-04 07:17:19 +0000289 Constant *AndC =
290 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000291 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000292 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000293
Chris Lattner7e044912010-01-04 07:17:19 +0000294 Constant *XorC =
295 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000296 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000297 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000298 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000299
300 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000301 Known.Zero = std::move(IKnownZero);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000302 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000303 Known.One = std::move(IKnownOne);
Chris Lattner7e044912010-01-04 07:17:19 +0000304 break;
305 }
306 case Instruction::Select:
James Molloy2b21a7c2015-05-20 18:41:25 +0000307 // If this is a select as part of a min/max pattern, don't simplify any
308 // further in case we break the structure.
309 Value *LHS, *RHS;
James Molloy134bec22015-08-11 09:12:57 +0000310 if (matchSelectPattern(I, LHS, RHS).Flavor != SPF_UNKNOWN)
James Molloy2b21a7c2015-05-20 18:41:25 +0000311 return nullptr;
Simon Pilgrim61116dd2015-09-17 20:32:45 +0000312
Craig Topperb45eabc2017-04-26 16:39:58 +0000313 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnown, Depth + 1) ||
314 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnown, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000315 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000316 assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
317 assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
Craig Topper4c947752012-12-22 18:09:02 +0000318
Chris Lattner7e044912010-01-04 07:17:19 +0000319 // If the operands are constants, see if we can simplify them.
320 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
321 ShrinkDemandedConstant(I, 2, DemandedMask))
322 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000323
Chris Lattner7e044912010-01-04 07:17:19 +0000324 // Only known if known in both the LHS and RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000325 Known.One = RHSKnown.One & LHSKnown.One;
326 Known.Zero = RHSKnown.Zero & LHSKnown.Zero;
Chris Lattner7e044912010-01-04 07:17:19 +0000327 break;
328 case Instruction::Trunc: {
329 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000330 DemandedMask = DemandedMask.zext(truncBf);
Craig Topperd938fd12017-05-03 22:07:25 +0000331 Known = Known.zext(truncBf);
Craig Topperb45eabc2017-04-26 16:39:58 +0000332 if (SimplifyDemandedBits(I, 0, DemandedMask, Known, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000333 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000334 DemandedMask = DemandedMask.trunc(BitWidth);
Craig Topperd938fd12017-05-03 22:07:25 +0000335 Known = Known.trunc(BitWidth);
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000336 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000337 break;
338 }
339 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000340 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000341 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000342
Chris Lattner229907c2011-07-18 04:54:35 +0000343 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
344 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000345 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
346 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
347 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000348 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000349 } else
350 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000351 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000352 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000353 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000354 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000355
Craig Topperb45eabc2017-04-26 16:39:58 +0000356 if (SimplifyDemandedBits(I, 0, DemandedMask, Known, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000357 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000358 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000359 break;
360 case Instruction::ZExt: {
361 // Compute the bits in the result that are not present in the input.
362 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000363
Jay Foad583abbc2010-12-07 08:25:19 +0000364 DemandedMask = DemandedMask.trunc(SrcBitWidth);
Craig Topperd938fd12017-05-03 22:07:25 +0000365 Known = Known.trunc(SrcBitWidth);
Craig Topperb45eabc2017-04-26 16:39:58 +0000366 if (SimplifyDemandedBits(I, 0, DemandedMask, Known, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000367 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000368 DemandedMask = DemandedMask.zext(BitWidth);
Craig Topperd938fd12017-05-03 22:07:25 +0000369 Known = Known.zext(BitWidth);
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000370 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000371 // The top bits are known to be zero.
Craig Topperb45eabc2017-04-26 16:39:58 +0000372 Known.Zero.setBitsFrom(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000373 break;
374 }
375 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();
Matthias Braune48484c2015-04-30 22:05:30 +0000410 if (NLZ > 0) {
411 // Right fill the mask of bits for this ADD/SUB to demand the most
Chris Lattner7e044912010-01-04 07:17:19 +0000412 // significant bit and all those below it.
Chris Lattner7e044912010-01-04 07:17:19 +0000413 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
Craig Topper07f29152017-03-22 04:03:53 +0000414 if (ShrinkDemandedConstant(I, 0, DemandedFromOps) ||
Craig Topperb45eabc2017-04-26 16:39:58 +0000415 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnown, Depth + 1) ||
Matthias Braune48484c2015-04-30 22:05:30 +0000416 ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
Craig Topperb45eabc2017-04-26 16:39:58 +0000417 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1)) {
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);
Chris Lattner7e044912010-01-04 07:17:19 +0000424 return I;
David Majnemer7d0e99c2015-04-22 22:42:05 +0000425 }
Craig Topper845033a2017-04-12 16:49:59 +0000426
427 // If we are known to be adding/subtracting zeros to every bit below
428 // the highest demanded bit, we just return the other side.
Craig Topperb45eabc2017-04-26 16:39:58 +0000429 if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
Craig Topper845033a2017-04-12 16:49:59 +0000430 return I->getOperand(0);
431 // We can't do this with the LHS for subtraction.
432 if (I->getOpcode() == Instruction::Add &&
Craig Topperb45eabc2017-04-26 16:39:58 +0000433 DemandedFromOps.isSubsetOf(LHSKnown.Zero))
Craig Topper845033a2017-04-12 16:49:59 +0000434 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000435 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000436
Craig Topper8fbb74b2017-03-24 22:12:10 +0000437 // Otherwise just hand the add/sub off to computeKnownBits to fill in
438 // the known zeros and ones.
Craig Topperb45eabc2017-04-26 16:39:58 +0000439 computeKnownBits(V, Known, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000440 break;
Matthias Braune48484c2015-04-30 22:05:30 +0000441 }
Sanjay Patel3e1ae722017-04-20 21:33:02 +0000442 case Instruction::Shl: {
443 const APInt *SA;
444 if (match(I->getOperand(1), m_APInt(SA))) {
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000445 const APInt *ShrAmt;
446 if (match(I->getOperand(0), m_Shr(m_Value(), m_APInt(ShrAmt)))) {
447 Instruction *Shr = cast<Instruction>(I->getOperand(0));
Sanjay Patelcc663b82017-04-20 22:37:01 +0000448 if (Value *R = simplifyShrShlDemandedBits(
Craig Topperb45eabc2017-04-26 16:39:58 +0000449 Shr, *ShrAmt, I, *SA, DemandedMask, Known))
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000450 return R;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000451 }
452
Chris Lattner768003c2011-02-10 05:09:34 +0000453 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000454 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000455
Chris Lattner768003c2011-02-10 05:09:34 +0000456 // If the shift is NUW/NSW, then it does demand the high bits.
457 ShlOperator *IOp = cast<ShlOperator>(I);
458 if (IOp->hasNoSignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000459 DemandedMaskIn.setHighBits(ShiftAmt+1);
Chris Lattner768003c2011-02-10 05:09:34 +0000460 else if (IOp->hasNoUnsignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000461 DemandedMaskIn.setHighBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000462
Craig Topperb45eabc2017-04-26 16:39:58 +0000463 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000464 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000465 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Craig Topperb45eabc2017-04-26 16:39:58 +0000466 Known.Zero <<= ShiftAmt;
467 Known.One <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000468 // low bits known zero.
469 if (ShiftAmt)
Craig Topperb45eabc2017-04-26 16:39:58 +0000470 Known.Zero.setLowBits(ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000471 }
472 break;
Sanjay Patel3e1ae722017-04-20 21:33:02 +0000473 }
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000474 case Instruction::LShr: {
475 const APInt *SA;
476 if (match(I->getOperand(1), m_APInt(SA))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000477 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000478
Chris Lattner7e044912010-01-04 07:17:19 +0000479 // Unsigned shift right.
480 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000481
Chris Lattner768003c2011-02-10 05:09:34 +0000482 // If the shift is exact, then it does demand the low bits (and knows that
483 // they are zero).
484 if (cast<LShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000485 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000486
Craig Topperb45eabc2017-04-26 16:39:58 +0000487 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000488 return I;
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000489 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Craig Topperb45eabc2017-04-26 16:39:58 +0000490 Known.Zero.lshrInPlace(ShiftAmt);
491 Known.One.lshrInPlace(ShiftAmt);
Craig Topper3a86a042017-03-19 05:49:16 +0000492 if (ShiftAmt)
Craig Topperb45eabc2017-04-26 16:39:58 +0000493 Known.Zero.setHighBits(ShiftAmt); // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000494 }
495 break;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000496 }
497 case Instruction::AShr: {
Chris Lattner7e044912010-01-04 07:17:19 +0000498 // If this is an arithmetic shift right and only the low-bit is set, we can
499 // always convert this into a logical shr, even if the shift amount is
500 // variable. The low bit of the shift cannot be an input sign bit unless
501 // the shift amount is >= the size of the datatype, which is undefined.
502 if (DemandedMask == 1) {
503 // Perform the logical shift right.
504 Instruction *NewVal = BinaryOperator::CreateLShr(
505 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000506 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000507 }
Chris Lattner7e044912010-01-04 07:17:19 +0000508
509 // If the sign bit is the only bit demanded by this ashr, then there is no
510 // need to do it, the shift doesn't change the high bit.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000511 if (DemandedMask.isSignMask())
Chris Lattner7e044912010-01-04 07:17:19 +0000512 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000513
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000514 const APInt *SA;
515 if (match(I->getOperand(1), m_APInt(SA))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000516 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000517
Chris Lattner7e044912010-01-04 07:17:19 +0000518 // Signed shift right.
519 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000520 // If any of the high bits are demanded, we should set the sign bit as
Chris Lattner7e044912010-01-04 07:17:19 +0000521 // demanded.
522 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Craig Topperc9a4fc02017-04-14 05:09:04 +0000523 DemandedMaskIn.setSignBit();
Craig Topper4c947752012-12-22 18:09:02 +0000524
Chris Lattner768003c2011-02-10 05:09:34 +0000525 // If the shift is exact, then it does demand the low bits (and knows that
526 // they are zero).
527 if (cast<AShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000528 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000529
Craig Topperb45eabc2017-04-26 16:39:58 +0000530 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000531 return I;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000532
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000533 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000534 // Compute the new bits that are at the top now.
535 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Craig Topperb45eabc2017-04-26 16:39:58 +0000536 Known.Zero.lshrInPlace(ShiftAmt);
537 Known.One.lshrInPlace(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000538
Chris Lattner7e044912010-01-04 07:17:19 +0000539 // Handle the sign bits.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000540 APInt SignMask(APInt::getSignMask(BitWidth));
Chris Lattner7e044912010-01-04 07:17:19 +0000541 // Adjust to where it is now in the mask.
Craig Topperbcfd2d12017-04-20 16:56:25 +0000542 SignMask.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 Topperb45eabc2017-04-26 16:39:58 +0000546 if (BitWidth <= ShiftAmt || Known.Zero[BitWidth-ShiftAmt-1] ||
Craig Topperff238892017-04-20 21:24:37 +0000547 !DemandedMask.intersects(HighBits)) {
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000548 BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0),
549 I->getOperand(1));
550 LShr->setIsExact(cast<BinaryOperator>(I)->isExact());
551 return InsertNewInstWith(LShr, *I);
Craig Topperb45eabc2017-04-26 16:39:58 +0000552 } else if (Known.One.intersects(SignMask)) { // New bits are known one.
553 Known.One |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000554 }
555 }
556 break;
Sanjay Patelfb5b3e72017-04-20 20:59:02 +0000557 }
Chris Lattner7e044912010-01-04 07:17:19 +0000558 case Instruction::SRem:
559 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000560 // X % -1 demands all the bits because we don't want to introduce
561 // INT_MIN % -1 (== undef) by accident.
562 if (Rem->isAllOnesValue())
563 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000564 APInt RA = Rem->getValue().abs();
565 if (RA.isPowerOf2()) {
566 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
567 return I->getOperand(0);
568
569 APInt LowBits = RA - 1;
Craig Topperbcfd2d12017-04-20 16:56:25 +0000570 APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
Craig Topperb45eabc2017-04-26 16:39:58 +0000571 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000572 return I;
573
Duncan Sands3a48b872010-01-28 17:22:42 +0000574 // The low bits of LHS are unchanged by the srem.
Craig Topperb45eabc2017-04-26 16:39:58 +0000575 Known.Zero = LHSKnown.Zero & LowBits;
576 Known.One = LHSKnown.One & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000577
Duncan Sands3a48b872010-01-28 17:22:42 +0000578 // If LHS is non-negative or has all low bits zero, then the upper bits
579 // are all zero.
Craig Topperca48af32017-04-29 16:43:11 +0000580 if (LHSKnown.isNonNegative() || LowBits.isSubsetOf(LHSKnown.Zero))
Craig Topperb45eabc2017-04-26 16:39:58 +0000581 Known.Zero |= ~LowBits;
Duncan Sands3a48b872010-01-28 17:22:42 +0000582
583 // If LHS is negative and not all low bits are zero, then the upper bits
584 // are all one.
Craig Topperca48af32017-04-29 16:43:11 +0000585 if (LHSKnown.isNegative() && LowBits.intersects(LHSKnown.One))
Craig Topperb45eabc2017-04-26 16:39:58 +0000586 Known.One |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000587
Craig Topper7e0aeeb2017-05-23 07:18:37 +0000588 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Craig Topperda886c62017-04-16 21:46:12 +0000589 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000590 }
591 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000592
593 // The sign bit is the LHS's sign bit, except when the result of the
594 // remainder is zero.
Craig Topperd23004c2017-04-17 16:38:20 +0000595 if (DemandedMask.isSignBitSet()) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000596 computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000597 // If it's known zero, our sign bit is also zero.
Craig Topperca48af32017-04-29 16:43:11 +0000598 if (LHSKnown.isNonNegative())
599 Known.makeNonNegative();
Nick Lewyckye4679792011-03-07 01:50:10 +0000600 }
Chris Lattner7e044912010-01-04 07:17:19 +0000601 break;
602 case Instruction::URem: {
Craig Topperb45eabc2017-04-26 16:39:58 +0000603 KnownBits Known2(BitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000604 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
Craig Topperb45eabc2017-04-26 16:39:58 +0000605 if (SimplifyDemandedBits(I, 0, AllOnes, Known2, Depth + 1) ||
606 SimplifyDemandedBits(I, 1, AllOnes, Known2, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000607 return I;
608
Craig Topper8df66c62017-05-12 17:20:30 +0000609 unsigned Leaders = Known2.countMinLeadingZeros();
Craig Topperb45eabc2017-04-26 16:39:58 +0000610 Known.Zero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
Chris Lattner7e044912010-01-04 07:17:19 +0000611 break;
612 }
613 case Instruction::Call:
614 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
615 switch (II->getIntrinsicID()) {
616 default: break;
617 case Intrinsic::bswap: {
618 // If the only bits demanded come from one byte of the bswap result,
619 // just shift the input byte into position to eliminate the bswap.
620 unsigned NLZ = DemandedMask.countLeadingZeros();
621 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000622
Chris Lattner7e044912010-01-04 07:17:19 +0000623 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
624 // we need all the bits down to bit 8. Likewise, round NLZ. If we
625 // have 14 leading zeros, round to 8.
626 NLZ &= ~7;
627 NTZ &= ~7;
628 // If we need exactly one byte, we can do this transformation.
629 if (BitWidth-NLZ-NTZ == 8) {
630 unsigned ResultBit = NTZ;
631 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000632
Chris Lattner7e044912010-01-04 07:17:19 +0000633 // Replace this with either a left or right shift to get the byte into
634 // the right place.
635 Instruction *NewVal;
636 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000637 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000638 ConstantInt::get(I->getType(), InputBit-ResultBit));
639 else
Gabor Greif79430172010-06-24 12:35:13 +0000640 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000641 ConstantInt::get(I->getType(), ResultBit-InputBit));
642 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000643 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000644 }
Craig Topper4c947752012-12-22 18:09:02 +0000645
Chris Lattner7e044912010-01-04 07:17:19 +0000646 // TODO: Could compute known zero/one bits based on the input.
647 break;
648 }
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000649 case Intrinsic::x86_mmx_pmovmskb:
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000650 case Intrinsic::x86_sse_movmsk_ps:
651 case Intrinsic::x86_sse2_movmsk_pd:
652 case Intrinsic::x86_sse2_pmovmskb_128:
653 case Intrinsic::x86_avx_movmsk_ps_256:
654 case Intrinsic::x86_avx_movmsk_pd_256:
655 case Intrinsic::x86_avx2_pmovmskb: {
656 // MOVMSK copies the vector elements' sign bits to the low bits
657 // and zeros the high bits.
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000658 unsigned ArgWidth;
659 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
660 ArgWidth = 8; // Arg is x86_mmx, but treated as <8 x i8>.
661 } else {
662 auto Arg = II->getArgOperand(0);
663 auto ArgType = cast<VectorType>(Arg->getType());
664 ArgWidth = ArgType->getNumElements();
665 }
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000666
667 // If we don't need any of low bits then return zero,
668 // we know that DemandedMask is non-zero already.
669 APInt DemandedElts = DemandedMask.zextOrTrunc(ArgWidth);
670 if (DemandedElts == 0)
671 return ConstantInt::getNullValue(VTy);
672
Ahmed Bougacha17482a52016-04-28 14:36:07 +0000673 // We know that the upper bits are set to zero.
Craig Topperb45eabc2017-04-26 16:39:58 +0000674 Known.Zero.setBitsFrom(ArgWidth);
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000675 return nullptr;
676 }
Chad Rosierb3628842011-05-26 23:13:19 +0000677 case Intrinsic::x86_sse42_crc32_64_64:
Craig Topperb45eabc2017-04-26 16:39:58 +0000678 Known.Zero.setBitsFrom(32);
Craig Topperf40110f2014-04-25 05:29:35 +0000679 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000680 }
681 }
Craig Topperb45eabc2017-04-26 16:39:58 +0000682 computeKnownBits(V, Known, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000683 break;
684 }
Craig Topper4c947752012-12-22 18:09:02 +0000685
Chris Lattner7e044912010-01-04 07:17:19 +0000686 // If the client is only demanding bits that we know, return the known
687 // constant.
Craig Topperb45eabc2017-04-26 16:39:58 +0000688 if (DemandedMask.isSubsetOf(Known.Zero|Known.One))
689 return Constant::getIntegerValue(VTy, Known.One);
Craig Topperf40110f2014-04-25 05:29:35 +0000690 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000691}
692
Craig Topperb45eabc2017-04-26 16:39:58 +0000693/// Helper routine of SimplifyDemandedUseBits. It computes Known
Craig Topperb0076fe2017-04-12 18:05:21 +0000694/// bits. It also tries to handle simplifications that can be done based on
695/// DemandedMask, but without modifying the Instruction.
696Value *InstCombiner::SimplifyMultipleUseDemandedBits(Instruction *I,
697 const APInt &DemandedMask,
Craig Topperb45eabc2017-04-26 16:39:58 +0000698 KnownBits &Known,
Craig Topperb0076fe2017-04-12 18:05:21 +0000699 unsigned Depth,
700 Instruction *CxtI) {
701 unsigned BitWidth = DemandedMask.getBitWidth();
702 Type *ITy = I->getType();
703
Craig Topperb45eabc2017-04-26 16:39:58 +0000704 KnownBits LHSKnown(BitWidth);
705 KnownBits RHSKnown(BitWidth);
Craig Topperb0076fe2017-04-12 18:05:21 +0000706
707 // Despite the fact that we can't simplify this instruction in all User's
Craig Topperb45eabc2017-04-26 16:39:58 +0000708 // context, we can at least compute the known bits, and we can
Craig Topperb0076fe2017-04-12 18:05:21 +0000709 // do simplifications that apply to *just* the one user if we know that
710 // this instruction has a simpler value in that context.
Craig Topperf35a7f72017-04-12 18:25:25 +0000711 switch (I->getOpcode()) {
Craig Topper9a458cd2017-04-14 22:34:14 +0000712 case Instruction::And: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000713 // If either the LHS or the RHS are Zero, the result is zero.
Craig Topperb45eabc2017-04-26 16:39:58 +0000714 computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
715 computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1,
Craig Topperb0076fe2017-04-12 18:05:21 +0000716 CxtI);
717
Craig Topper9a458cd2017-04-14 22:34:14 +0000718 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000719 APInt IKnownZero = RHSKnown.Zero | LHSKnown.Zero;
Craig Topper9a458cd2017-04-14 22:34:14 +0000720 // Output known-1 bits are only known if set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000721 APInt IKnownOne = RHSKnown.One & LHSKnown.One;
Craig Topper9a458cd2017-04-14 22:34:14 +0000722
Craig Topperc75f94b2017-04-12 19:32:47 +0000723 // If the client is only demanding bits that we know, return the known
724 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000725 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000726 return Constant::getIntegerValue(ITy, IKnownOne);
Craig Topperc75f94b2017-04-12 19:32:47 +0000727
Craig Topperb0076fe2017-04-12 18:05:21 +0000728 // If all of the demanded bits are known 1 on one side, return the other.
729 // These bits cannot contribute to the result of the 'and' in this
730 // context.
Craig Topperb45eabc2017-04-26 16:39:58 +0000731 if (DemandedMask.isSubsetOf(LHSKnown.Zero | RHSKnown.One))
Craig Topperb0076fe2017-04-12 18:05:21 +0000732 return I->getOperand(0);
Craig Topperb45eabc2017-04-26 16:39:58 +0000733 if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.One))
Craig Topperb0076fe2017-04-12 18:05:21 +0000734 return I->getOperand(1);
735
Craig Topperb45eabc2017-04-26 16:39:58 +0000736 Known.Zero = std::move(IKnownZero);
737 Known.One = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000738 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000739 }
740 case Instruction::Or: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000741 // We can simplify (X|Y) -> X or Y in the user's context if we know that
742 // only bits from X or Y are demanded.
743
744 // If either the LHS or the RHS are One, the result is One.
Craig Topperb45eabc2017-04-26 16:39:58 +0000745 computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
746 computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1,
Craig Topperb0076fe2017-04-12 18:05:21 +0000747 CxtI);
748
Craig Topper9a458cd2017-04-14 22:34:14 +0000749 // Output known-0 bits are only known if clear in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000750 APInt IKnownZero = RHSKnown.Zero & LHSKnown.Zero;
Craig Topper9a458cd2017-04-14 22:34:14 +0000751 // Output known-1 are known to be set if set in either the LHS | RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000752 APInt IKnownOne = RHSKnown.One | LHSKnown.One;
Craig Topper9a458cd2017-04-14 22:34:14 +0000753
Craig Topperc75f94b2017-04-12 19:32:47 +0000754 // If the client is only demanding bits that we know, return the known
755 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000756 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topper9a458cd2017-04-14 22:34:14 +0000757 return Constant::getIntegerValue(ITy, IKnownOne);
Craig Topperc75f94b2017-04-12 19:32:47 +0000758
Craig Topperb0076fe2017-04-12 18:05:21 +0000759 // If all of the demanded bits are known zero on one side, return the
760 // other. These bits cannot contribute to the result of the 'or' in this
761 // context.
Craig Topperb45eabc2017-04-26 16:39:58 +0000762 if (DemandedMask.isSubsetOf(LHSKnown.One | RHSKnown.Zero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000763 return I->getOperand(0);
Craig Topperb45eabc2017-04-26 16:39:58 +0000764 if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000765 return I->getOperand(1);
766
Craig Topperb45eabc2017-04-26 16:39:58 +0000767 Known.Zero = std::move(IKnownZero);
768 Known.One = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000769 break;
Craig Topper9a458cd2017-04-14 22:34:14 +0000770 }
Craig Topperc75f94b2017-04-12 19:32:47 +0000771 case Instruction::Xor: {
Craig Topperb0076fe2017-04-12 18:05:21 +0000772 // We can simplify (X^Y) -> X or Y in the user's context if we know that
773 // only bits from X or Y are demanded.
774
Craig Topperb45eabc2017-04-26 16:39:58 +0000775 computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
776 computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1,
Craig Topperb0076fe2017-04-12 18:05:21 +0000777 CxtI);
778
Craig Topperc75f94b2017-04-12 19:32:47 +0000779 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000780 APInt IKnownZero = (RHSKnown.Zero & LHSKnown.Zero) |
781 (RHSKnown.One & LHSKnown.One);
Craig Topperc75f94b2017-04-12 19:32:47 +0000782 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000783 APInt IKnownOne = (RHSKnown.Zero & LHSKnown.One) |
784 (RHSKnown.One & LHSKnown.Zero);
Craig Topperc75f94b2017-04-12 19:32:47 +0000785
786 // If the client is only demanding bits that we know, return the known
787 // constant.
Craig Topper17f37ba2017-04-20 20:47:35 +0000788 if (DemandedMask.isSubsetOf(IKnownZero|IKnownOne))
Craig Topperc75f94b2017-04-12 19:32:47 +0000789 return Constant::getIntegerValue(ITy, IKnownOne);
790
Craig Topperb0076fe2017-04-12 18:05:21 +0000791 // If all of the demanded bits are known zero on one side, return the
792 // other.
Craig Topperb45eabc2017-04-26 16:39:58 +0000793 if (DemandedMask.isSubsetOf(RHSKnown.Zero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000794 return I->getOperand(0);
Craig Topperb45eabc2017-04-26 16:39:58 +0000795 if (DemandedMask.isSubsetOf(LHSKnown.Zero))
Craig Topperb0076fe2017-04-12 18:05:21 +0000796 return I->getOperand(1);
Craig Topperf35a7f72017-04-12 18:25:25 +0000797
Craig Topperc75f94b2017-04-12 19:32:47 +0000798 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000799 Known.Zero = std::move(IKnownZero);
Craig Topperc75f94b2017-04-12 19:32:47 +0000800 // Output known-1 are known to be set if set in only one of the LHS, RHS.
Craig Topperb45eabc2017-04-26 16:39:58 +0000801 Known.One = std::move(IKnownOne);
Craig Topperf35a7f72017-04-12 18:25:25 +0000802 break;
Craig Topperb0076fe2017-04-12 18:05:21 +0000803 }
Craig Topperc75f94b2017-04-12 19:32:47 +0000804 default:
Craig Topperb45eabc2017-04-26 16:39:58 +0000805 // Compute the Known bits to simplify things downstream.
806 computeKnownBits(I, Known, Depth, CxtI);
Craig Topperb0076fe2017-04-12 18:05:21 +0000807
Craig Topperc75f94b2017-04-12 19:32:47 +0000808 // If this user is only demanding bits that we know, return the known
809 // constant.
Craig Topperb45eabc2017-04-26 16:39:58 +0000810 if (DemandedMask.isSubsetOf(Known.Zero|Known.One))
811 return Constant::getIntegerValue(ITy, Known.One);
Craig Topper9a51c7f2017-04-12 18:17:46 +0000812
Craig Topperc75f94b2017-04-12 19:32:47 +0000813 break;
814 }
Craig Topper9a51c7f2017-04-12 18:17:46 +0000815
Craig Topperb0076fe2017-04-12 18:05:21 +0000816 return nullptr;
817}
818
819
Shuxin Yang63e999e2012-12-04 00:04:54 +0000820/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
821/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
822/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
823/// of "C2-C1".
824///
825/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
826/// ..., bn}, without considering the specific value X is holding.
827/// This transformation is legal iff one of following conditions is hold:
828/// 1) All the bit in S are 0, in this case E1 == E2.
829/// 2) We don't care those bits in S, per the input DemandedMask.
830/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
831/// rest bits.
832///
833/// Currently we only test condition 2).
834///
835/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
836/// not successful.
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000837Value *
Sanjay Patelcc663b82017-04-20 22:37:01 +0000838InstCombiner::simplifyShrShlDemandedBits(Instruction *Shr, const APInt &ShrOp1,
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000839 Instruction *Shl, const APInt &ShlOp1,
840 const APInt &DemandedMask,
Craig Topperb45eabc2017-04-26 16:39:58 +0000841 KnownBits &Known) {
Benjamin Kramer010f1082013-08-30 14:35:35 +0000842 if (!ShlOp1 || !ShrOp1)
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000843 return nullptr; // No-op.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000844
845 Value *VarX = Shr->getOperand(0);
846 Type *Ty = VarX->getType();
Sanjay Patelc9485ca2017-04-20 22:33:54 +0000847 unsigned BitWidth = Ty->getScalarSizeInBits();
Benjamin Kramer010f1082013-08-30 14:35:35 +0000848 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000849 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000850
851 unsigned ShlAmt = ShlOp1.getZExtValue();
852 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000853
Craig Topperb45eabc2017-04-26 16:39:58 +0000854 Known.One.clearAllBits();
855 Known.Zero.setLowBits(ShlAmt - 1);
856 Known.Zero &= DemandedMask;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000857
Benjamin Kramer010f1082013-08-30 14:35:35 +0000858 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
859 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000860
861 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
862 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
863 (BitMask1.ashr(ShrAmt) << ShlAmt);
864
865 if (ShrAmt <= ShlAmt) {
866 BitMask2 <<= (ShlAmt - ShrAmt);
867 } else {
868 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
869 BitMask2.ashr(ShrAmt - ShlAmt);
870 }
871
872 // Check if condition-2 (see the comment to this function) is satified.
873 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
874 if (ShrAmt == ShlAmt)
875 return VarX;
876
877 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000878 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000879
880 BinaryOperator *New;
881 if (ShrAmt < ShlAmt) {
882 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
883 New = BinaryOperator::CreateShl(VarX, Amt);
884 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
885 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
886 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
887 } else {
888 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000889 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
890 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000891 if (cast<BinaryOperator>(Shr)->isExact())
892 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000893 }
894
895 return InsertNewInstWith(New, *Shl);
896 }
897
Craig Topperf40110f2014-04-25 05:29:35 +0000898 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000899}
Chris Lattner7e044912010-01-04 07:17:19 +0000900
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +0000901/// The specified value produces a vector with any number of elements.
902/// DemandedElts contains the set of elements that are actually used by the
903/// caller. This method analyzes which elements of the operand are undef and
904/// returns that information in UndefElts.
Chris Lattner7e044912010-01-04 07:17:19 +0000905///
906/// If the information about demanded elements can be used to simplify the
907/// operation, the operation is simplified, then the resultant value is
908/// returned. This returns null if no change was made.
909Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000910 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000911 unsigned Depth) {
Sanjay Patel9190b4a2016-04-29 20:54:56 +0000912 unsigned VWidth = V->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +0000913 APInt EltMask(APInt::getAllOnesValue(VWidth));
914 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
915
916 if (isa<UndefValue>(V)) {
917 // If the entire vector is undefined, just return this info.
918 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000919 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000920 }
Craig Topper4c947752012-12-22 18:09:02 +0000921
Chris Lattnerb22423c2010-02-08 23:56:03 +0000922 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000923 UndefElts = EltMask;
924 return UndefValue::get(V->getType());
925 }
926
927 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000928
Chris Lattner67058832012-01-25 06:48:06 +0000929 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
930 if (Constant *C = dyn_cast<Constant>(V)) {
931 // Check if this is identity. If so, return 0 since we are not simplifying
932 // anything.
933 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000934 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000935
Chris Lattner229907c2011-07-18 04:54:35 +0000936 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000937 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000938
Chris Lattner67058832012-01-25 06:48:06 +0000939 SmallVector<Constant*, 16> Elts;
940 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000941 if (!DemandedElts[i]) { // If not demanded, set to undef.
942 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000943 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +0000944 continue;
945 }
Craig Topper4c947752012-12-22 18:09:02 +0000946
Chris Lattner67058832012-01-25 06:48:06 +0000947 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000948 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000949
Chris Lattner67058832012-01-25 06:48:06 +0000950 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000951 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000952 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +0000953 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +0000954 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +0000955 }
Chris Lattner67058832012-01-25 06:48:06 +0000956 }
Craig Topper4c947752012-12-22 18:09:02 +0000957
Chris Lattner7e044912010-01-04 07:17:19 +0000958 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +0000959 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +0000960 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000961 }
Craig Topper4c947752012-12-22 18:09:02 +0000962
Chris Lattner7e044912010-01-04 07:17:19 +0000963 // Limit search depth.
964 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +0000965 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000966
Stuart Hastings5bd18b62011-05-17 22:13:31 +0000967 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +0000968 // simplification conservatively assuming that all elements
969 // are needed.
970 if (!V->hasOneUse()) {
971 // Quit if we find multiple users of a non-root value though.
972 // They'll be handled when it's their turn to be visited by
973 // the main instcombine process.
974 if (Depth != 0)
975 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +0000976 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000977
978 // Conservatively assume that all elements are needed.
979 DemandedElts = EltMask;
980 }
Craig Topper4c947752012-12-22 18:09:02 +0000981
Chris Lattner7e044912010-01-04 07:17:19 +0000982 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +0000983 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +0000984
Chris Lattner7e044912010-01-04 07:17:19 +0000985 bool MadeChange = false;
986 APInt UndefElts2(VWidth, 0);
Craig Topper23ebd952016-12-11 08:54:52 +0000987 APInt UndefElts3(VWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +0000988 Value *TmpV;
989 switch (I->getOpcode()) {
990 default: break;
Craig Topper4c947752012-12-22 18:09:02 +0000991
Chris Lattner7e044912010-01-04 07:17:19 +0000992 case Instruction::InsertElement: {
993 // If this is a variable index, we don't know which element it overwrites.
994 // demand exactly the same input as we produce.
995 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +0000996 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +0000997 // Note that we can't propagate undef elt info, because we don't know
998 // which elt is getting updated.
999 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001000 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001001 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1002 break;
1003 }
Craig Topper4c947752012-12-22 18:09:02 +00001004
Chris Lattner7e044912010-01-04 07:17:19 +00001005 // If this is inserting an element that isn't demanded, remove this
1006 // insertelement.
1007 unsigned IdxNo = Idx->getZExtValue();
1008 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1009 Worklist.Add(I);
1010 return I->getOperand(0);
1011 }
Craig Topper4c947752012-12-22 18:09:02 +00001012
Chris Lattner7e044912010-01-04 07:17:19 +00001013 // Otherwise, the element inserted overwrites whatever was there, so the
1014 // input demanded set is simpler than the output set.
1015 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001016 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001017 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001018 UndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001019 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1020
1021 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001022 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001023 break;
1024 }
1025 case Instruction::ShuffleVector: {
1026 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Craig Topper2e18bcf2016-12-29 04:24:32 +00001027 unsigned LHSVWidth =
1028 Shuffle->getOperand(0)->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +00001029 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1030 for (unsigned i = 0; i < VWidth; i++) {
1031 if (DemandedElts[i]) {
1032 unsigned MaskVal = Shuffle->getMaskValue(i);
1033 if (MaskVal != -1u) {
1034 assert(MaskVal < LHSVWidth * 2 &&
1035 "shufflevector mask index out of range!");
1036 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001037 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001038 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001039 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001040 }
1041 }
1042 }
1043
Alexey Bataevfee90782016-09-23 09:14:08 +00001044 APInt LHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001045 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001046 LHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001047 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1048
Alexey Bataevfee90782016-09-23 09:14:08 +00001049 APInt RHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001050 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001051 RHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001052 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1053
1054 bool NewUndefElts = false;
Alexey Bataev793c9462016-09-26 13:18:59 +00001055 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1056 unsigned RHSIdx = -1u, RHSValIdx = -1u;
Alexey Bataevfee90782016-09-23 09:14:08 +00001057 bool LHSUniform = true;
1058 bool RHSUniform = true;
Chris Lattner7e044912010-01-04 07:17:19 +00001059 for (unsigned i = 0; i < VWidth; i++) {
1060 unsigned MaskVal = Shuffle->getMaskValue(i);
1061 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001062 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001063 } else if (!DemandedElts[i]) {
1064 NewUndefElts = true;
1065 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001066 } else if (MaskVal < LHSVWidth) {
Alexey Bataevfee90782016-09-23 09:14:08 +00001067 if (LHSUndefElts[MaskVal]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001068 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001069 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001070 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001071 LHSIdx = LHSIdx == -1u ? i : LHSVWidth;
1072 LHSValIdx = LHSValIdx == -1u ? MaskVal : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001073 LHSUniform = LHSUniform && (MaskVal == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001074 }
1075 } else {
Alexey Bataevfee90782016-09-23 09:14:08 +00001076 if (RHSUndefElts[MaskVal - LHSVWidth]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001077 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001078 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001079 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001080 RHSIdx = RHSIdx == -1u ? i : LHSVWidth;
1081 RHSValIdx = RHSValIdx == -1u ? MaskVal - LHSVWidth : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001082 RHSUniform = RHSUniform && (MaskVal - LHSVWidth == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001083 }
1084 }
1085 }
1086
Alexey Bataevfee90782016-09-23 09:14:08 +00001087 // Try to transform shuffle with constant vector and single element from
1088 // this constant vector to single insertelement instruction.
1089 // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
1090 // insertelement V, C[ci], ci-n
1091 if (LHSVWidth == Shuffle->getType()->getNumElements()) {
1092 Value *Op = nullptr;
1093 Constant *Value = nullptr;
1094 unsigned Idx = -1u;
1095
Craig Topper62f06e22016-12-29 05:38:31 +00001096 // Find constant vector with the single element in shuffle (LHS or RHS).
Alexey Bataevfee90782016-09-23 09:14:08 +00001097 if (LHSIdx < LHSVWidth && RHSUniform) {
1098 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1099 Op = Shuffle->getOperand(1);
Alexey Bataev793c9462016-09-26 13:18:59 +00001100 Value = CV->getOperand(LHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001101 Idx = LHSIdx;
1102 }
1103 }
1104 if (RHSIdx < LHSVWidth && LHSUniform) {
1105 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1106 Op = Shuffle->getOperand(0);
Alexey Bataev793c9462016-09-26 13:18:59 +00001107 Value = CV->getOperand(RHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001108 Idx = RHSIdx;
1109 }
1110 }
1111 // Found constant vector with single element - convert to insertelement.
1112 if (Op && Value) {
1113 Instruction *New = InsertElementInst::Create(
1114 Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx),
1115 Shuffle->getName());
1116 InsertNewInstWith(New, *Shuffle);
1117 return New;
1118 }
1119 }
Chris Lattner7e044912010-01-04 07:17:19 +00001120 if (NewUndefElts) {
1121 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001122 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001123 for (unsigned i = 0; i < VWidth; ++i) {
1124 if (UndefElts[i])
1125 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1126 else
1127 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1128 Shuffle->getMaskValue(i)));
1129 }
1130 I->setOperand(2, ConstantVector::get(Elts));
1131 MadeChange = true;
1132 }
1133 break;
1134 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001135 case Instruction::Select: {
1136 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1137 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1138 for (unsigned i = 0; i < VWidth; i++) {
Andrea Di Biagio40f59e42015-10-06 10:34:53 +00001139 Constant *CElt = CV->getAggregateElement(i);
1140 // Method isNullValue always returns false when called on a
1141 // ConstantExpr. If CElt is a ConstantExpr then skip it in order to
1142 // to avoid propagating incorrect information.
1143 if (isa<ConstantExpr>(CElt))
1144 continue;
1145 if (CElt->isNullValue())
Pete Cooperabc13af2012-07-26 23:10:24 +00001146 LeftDemanded.clearBit(i);
1147 else
1148 RightDemanded.clearBit(i);
1149 }
1150 }
1151
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001152 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded, UndefElts,
1153 Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001154 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1155
1156 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001157 UndefElts2, Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001158 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001159
Pete Cooperabc13af2012-07-26 23:10:24 +00001160 // Output elements are undefined if both are undefined.
1161 UndefElts &= UndefElts2;
1162 break;
1163 }
Chris Lattner7e044912010-01-04 07:17:19 +00001164 case Instruction::BitCast: {
1165 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001166 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001167 if (!VTy) break;
1168 unsigned InVWidth = VTy->getNumElements();
1169 APInt InputDemandedElts(InVWidth, 0);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001170 UndefElts2 = APInt(InVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001171 unsigned Ratio;
1172
1173 if (VWidth == InVWidth) {
1174 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1175 // elements as are demanded of us.
1176 Ratio = 1;
1177 InputDemandedElts = DemandedElts;
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001178 } else if ((VWidth % InVWidth) == 0) {
1179 // If the number of elements in the output is a multiple of the number of
1180 // elements in the input then an input element is live if any of the
1181 // corresponding output elements are live.
1182 Ratio = VWidth / InVWidth;
1183 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Chris Lattner7e044912010-01-04 07:17:19 +00001184 if (DemandedElts[OutIdx])
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001185 InputDemandedElts.setBit(OutIdx / Ratio);
1186 } else if ((InVWidth % VWidth) == 0) {
1187 // If the number of elements in the input is a multiple of the number of
1188 // elements in the output then an input element is live if the
1189 // corresponding output element is live.
1190 Ratio = InVWidth / VWidth;
Chris Lattner7e044912010-01-04 07:17:19 +00001191 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001192 if (DemandedElts[InIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001193 InputDemandedElts.setBit(InIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001194 } else {
1195 // Unsupported so far.
1196 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001197 }
Craig Topper4c947752012-12-22 18:09:02 +00001198
Chris Lattner7e044912010-01-04 07:17:19 +00001199 // div/rem demand all inputs, because they don't want divide by zero.
1200 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001201 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001202 if (TmpV) {
1203 I->setOperand(0, TmpV);
1204 MadeChange = true;
1205 }
Craig Topper4c947752012-12-22 18:09:02 +00001206
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001207 if (VWidth == InVWidth) {
1208 UndefElts = UndefElts2;
1209 } else if ((VWidth % InVWidth) == 0) {
1210 // If the number of elements in the output is a multiple of the number of
1211 // elements in the input then an output element is undef if the
1212 // corresponding input element is undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001213 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001214 if (UndefElts2[OutIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001215 UndefElts.setBit(OutIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001216 } else if ((InVWidth % VWidth) == 0) {
1217 // If the number of elements in the input is a multiple of the number of
1218 // elements in the output then an output element is undef if all of the
1219 // corresponding input elements are undef.
1220 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1221 APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
1222 if (SubUndef.countPopulation() == Ratio)
1223 UndefElts.setBit(OutIdx);
1224 }
1225 } else {
Chris Lattner7e044912010-01-04 07:17:19 +00001226 llvm_unreachable("Unimp");
Chris Lattner7e044912010-01-04 07:17:19 +00001227 }
1228 break;
1229 }
1230 case Instruction::And:
1231 case Instruction::Or:
1232 case Instruction::Xor:
1233 case Instruction::Add:
1234 case Instruction::Sub:
1235 case Instruction::Mul:
1236 // div/rem demand all inputs, because they don't want divide by zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001237 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1238 Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001239 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1240 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001241 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001242 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001243
Chris Lattner7e044912010-01-04 07:17:19 +00001244 // Output elements are undefined if both are undefined. Consider things
1245 // like undef&0. The result is known zero, not undef.
1246 UndefElts &= UndefElts2;
1247 break;
Pete Coopere807e452012-07-26 22:37:04 +00001248 case Instruction::FPTrunc:
1249 case Instruction::FPExt:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001250 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1251 Depth + 1);
Pete Coopere807e452012-07-26 22:37:04 +00001252 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1253 break;
Craig Topper4c947752012-12-22 18:09:02 +00001254
Chris Lattner7e044912010-01-04 07:17:19 +00001255 case Instruction::Call: {
1256 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1257 if (!II) break;
1258 switch (II->getIntrinsicID()) {
1259 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001260
Craig Topper7fc6d342016-12-11 22:32:38 +00001261 case Intrinsic::x86_xop_vfrcz_ss:
1262 case Intrinsic::x86_xop_vfrcz_sd:
1263 // The instructions for these intrinsics are speced to zero upper bits not
1264 // pass them through like other scalar intrinsics. So we shouldn't just
1265 // use Arg0 if DemandedElts[0] is clear like we do for other intrinsics.
1266 // Instead we should return a zero vector.
Craig Topper1a8a3372016-12-29 03:30:17 +00001267 if (!DemandedElts[0]) {
1268 Worklist.Add(II);
Craig Topper7fc6d342016-12-11 22:32:38 +00001269 return ConstantAggregateZero::get(II->getType());
Craig Topper1a8a3372016-12-29 03:30:17 +00001270 }
Craig Topper7fc6d342016-12-11 22:32:38 +00001271
Craig Topperac75bca2016-12-13 07:45:45 +00001272 // Only the lower element is used.
1273 DemandedElts = 1;
Craig Topper7fc6d342016-12-11 22:32:38 +00001274 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1275 UndefElts, Depth + 1);
1276 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperac75bca2016-12-13 07:45:45 +00001277
1278 // Only the lower element is undefined. The high elements are zero.
1279 UndefElts = UndefElts[0];
Craig Topper7fc6d342016-12-11 22:32:38 +00001280 break;
1281
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001282 // Unary scalar-as-vector operations that work column-wise.
Simon Pilgrim83020942016-04-24 18:23:14 +00001283 case Intrinsic::x86_sse_rcp_ss:
1284 case Intrinsic::x86_sse_rsqrt_ss:
1285 case Intrinsic::x86_sse_sqrt_ss:
1286 case Intrinsic::x86_sse2_sqrt_sd:
Simon Pilgrim83020942016-04-24 18:23:14 +00001287 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1288 UndefElts, Depth + 1);
1289 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1290
1291 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001292 if (!DemandedElts[0]) {
1293 Worklist.Add(II);
Simon Pilgrim83020942016-04-24 18:23:14 +00001294 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001295 }
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001296 // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions
1297 // checks).
Simon Pilgrim83020942016-04-24 18:23:14 +00001298 break;
1299
Craig Toppera0372de2016-12-14 03:17:27 +00001300 // Binary scalar-as-vector operations that work column-wise. The high
1301 // elements come from operand 0. The low element is a function of both
1302 // operands.
Chris Lattner7e044912010-01-04 07:17:19 +00001303 case Intrinsic::x86_sse_min_ss:
1304 case Intrinsic::x86_sse_max_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001305 case Intrinsic::x86_sse_cmp_ss:
Chris Lattner7e044912010-01-04 07:17:19 +00001306 case Intrinsic::x86_sse2_min_sd:
1307 case Intrinsic::x86_sse2_max_sd:
Craig Toppera0372de2016-12-14 03:17:27 +00001308 case Intrinsic::x86_sse2_cmp_sd: {
1309 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1310 UndefElts, Depth + 1);
1311 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1312
1313 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001314 if (!DemandedElts[0]) {
1315 Worklist.Add(II);
Craig Toppera0372de2016-12-14 03:17:27 +00001316 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001317 }
Craig Toppera0372de2016-12-14 03:17:27 +00001318
1319 // Only lower element is used for operand 1.
1320 DemandedElts = 1;
1321 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1322 UndefElts2, Depth + 1);
1323 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1324
1325 // Lower element is undefined if both lower elements are undefined.
1326 // Consider things like undef&0. The result is known zero, not undef.
1327 if (!UndefElts2[0])
1328 UndefElts.clearBit(0);
1329
1330 break;
1331 }
1332
Craig Toppereb6a20e2016-12-14 03:17:30 +00001333 // Binary scalar-as-vector operations that work column-wise. The high
1334 // elements come from operand 0 and the low element comes from operand 1.
Simon Pilgrim83020942016-04-24 18:23:14 +00001335 case Intrinsic::x86_sse41_round_ss:
Craig Toppereb6a20e2016-12-14 03:17:30 +00001336 case Intrinsic::x86_sse41_round_sd: {
1337 // Don't use the low element of operand 0.
1338 APInt DemandedElts2 = DemandedElts;
1339 DemandedElts2.clearBit(0);
1340 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001341 UndefElts, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001342 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001343
1344 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001345 if (!DemandedElts[0]) {
1346 Worklist.Add(II);
Craig Toppereb6a20e2016-12-14 03:17:30 +00001347 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001348 }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001349
1350 // Only lower element is used for operand 1.
1351 DemandedElts = 1;
Gabor Greife23efee2010-06-28 16:45:00 +00001352 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001353 UndefElts2, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001354 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001355
Craig Toppereb6a20e2016-12-14 03:17:30 +00001356 // Take the high undef elements from operand 0 and take the lower element
1357 // from operand 1.
1358 UndefElts.clearBit(0);
1359 UndefElts |= UndefElts2[0];
Chris Lattner7e044912010-01-04 07:17:19 +00001360 break;
Craig Toppereb6a20e2016-12-14 03:17:30 +00001361 }
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001362
Craig Topperdfd268d2016-12-14 05:43:05 +00001363 // Three input scalar-as-vector operations that work column-wise. The high
1364 // elements come from operand 0 and the low element is a function of all
1365 // three inputs.
Craig Topper268b3ab2016-12-14 06:06:58 +00001366 case Intrinsic::x86_avx512_mask_add_ss_round:
1367 case Intrinsic::x86_avx512_mask_div_ss_round:
1368 case Intrinsic::x86_avx512_mask_mul_ss_round:
1369 case Intrinsic::x86_avx512_mask_sub_ss_round:
1370 case Intrinsic::x86_avx512_mask_max_ss_round:
1371 case Intrinsic::x86_avx512_mask_min_ss_round:
1372 case Intrinsic::x86_avx512_mask_add_sd_round:
1373 case Intrinsic::x86_avx512_mask_div_sd_round:
1374 case Intrinsic::x86_avx512_mask_mul_sd_round:
1375 case Intrinsic::x86_avx512_mask_sub_sd_round:
1376 case Intrinsic::x86_avx512_mask_max_sd_round:
1377 case Intrinsic::x86_avx512_mask_min_sd_round:
Craig Topper23ebd952016-12-11 08:54:52 +00001378 case Intrinsic::x86_fma_vfmadd_ss:
1379 case Intrinsic::x86_fma_vfmsub_ss:
1380 case Intrinsic::x86_fma_vfnmadd_ss:
1381 case Intrinsic::x86_fma_vfnmsub_ss:
1382 case Intrinsic::x86_fma_vfmadd_sd:
1383 case Intrinsic::x86_fma_vfmsub_sd:
1384 case Intrinsic::x86_fma_vfnmadd_sd:
1385 case Intrinsic::x86_fma_vfnmsub_sd:
Craig Topperab5f3552016-12-15 03:49:45 +00001386 case Intrinsic::x86_avx512_mask_vfmadd_ss:
1387 case Intrinsic::x86_avx512_mask_vfmadd_sd:
1388 case Intrinsic::x86_avx512_maskz_vfmadd_ss:
1389 case Intrinsic::x86_avx512_maskz_vfmadd_sd:
Craig Topper23ebd952016-12-11 08:54:52 +00001390 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1391 UndefElts, Depth + 1);
1392 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperdfd268d2016-12-14 05:43:05 +00001393
1394 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001395 if (!DemandedElts[0]) {
1396 Worklist.Add(II);
Craig Topperdfd268d2016-12-14 05:43:05 +00001397 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001398 }
Craig Topperdfd268d2016-12-14 05:43:05 +00001399
1400 // Only lower element is used for operand 1 and 2.
1401 DemandedElts = 1;
Craig Topper23ebd952016-12-11 08:54:52 +00001402 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1403 UndefElts2, Depth + 1);
1404 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1405 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1406 UndefElts3, Depth + 1);
1407 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1408
Craig Topperdfd268d2016-12-14 05:43:05 +00001409 // Lower element is undefined if all three lower elements are undefined.
1410 // Consider things like undef&0. The result is known zero, not undef.
1411 if (!UndefElts2[0] || !UndefElts3[0])
1412 UndefElts.clearBit(0);
Craig Topper23ebd952016-12-11 08:54:52 +00001413
Craig Topper23ebd952016-12-11 08:54:52 +00001414 break;
1415
Craig Topperab5f3552016-12-15 03:49:45 +00001416 case Intrinsic::x86_avx512_mask3_vfmadd_ss:
1417 case Intrinsic::x86_avx512_mask3_vfmadd_sd:
1418 case Intrinsic::x86_avx512_mask3_vfmsub_ss:
1419 case Intrinsic::x86_avx512_mask3_vfmsub_sd:
1420 case Intrinsic::x86_avx512_mask3_vfnmsub_ss:
1421 case Intrinsic::x86_avx512_mask3_vfnmsub_sd:
1422 // These intrinsics get the passthru bits from operand 2.
1423 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1424 UndefElts, Depth + 1);
1425 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1426
1427 // If lowest element of a scalar op isn't used then use Arg2.
Craig Topper1a8a3372016-12-29 03:30:17 +00001428 if (!DemandedElts[0]) {
1429 Worklist.Add(II);
Craig Topperab5f3552016-12-15 03:49:45 +00001430 return II->getArgOperand(2);
Craig Topper1a8a3372016-12-29 03:30:17 +00001431 }
Craig Topperab5f3552016-12-15 03:49:45 +00001432
1433 // Only lower element is used for operand 0 and 1.
1434 DemandedElts = 1;
1435 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1436 UndefElts2, Depth + 1);
1437 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1438 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1439 UndefElts3, Depth + 1);
1440 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1441
1442 // Lower element is undefined if all three lower elements are undefined.
1443 // Consider things like undef&0. The result is known zero, not undef.
1444 if (!UndefElts2[0] || !UndefElts3[0])
1445 UndefElts.clearBit(0);
1446
1447 break;
1448
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001449 case Intrinsic::x86_sse2_pmulu_dq:
1450 case Intrinsic::x86_sse41_pmuldq:
1451 case Intrinsic::x86_avx2_pmul_dq:
Craig Topper72f2d4e2016-12-27 05:30:09 +00001452 case Intrinsic::x86_avx2_pmulu_dq:
1453 case Intrinsic::x86_avx512_pmul_dq_512:
1454 case Intrinsic::x86_avx512_pmulu_dq_512: {
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001455 Value *Op0 = II->getArgOperand(0);
1456 Value *Op1 = II->getArgOperand(1);
1457 unsigned InnerVWidth = Op0->getType()->getVectorNumElements();
1458 assert((VWidth * 2) == InnerVWidth && "Unexpected input size");
1459
1460 APInt InnerDemandedElts(InnerVWidth, 0);
1461 for (unsigned i = 0; i != VWidth; ++i)
1462 if (DemandedElts[i])
1463 InnerDemandedElts.setBit(i * 2);
1464
1465 UndefElts2 = APInt(InnerVWidth, 0);
1466 TmpV = SimplifyDemandedVectorElts(Op0, InnerDemandedElts, UndefElts2,
1467 Depth + 1);
1468 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1469
1470 UndefElts3 = APInt(InnerVWidth, 0);
1471 TmpV = SimplifyDemandedVectorElts(Op1, InnerDemandedElts, UndefElts3,
1472 Depth + 1);
1473 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1474
1475 break;
1476 }
1477
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001478 case Intrinsic::x86_sse2_packssdw_128:
1479 case Intrinsic::x86_sse2_packsswb_128:
1480 case Intrinsic::x86_sse2_packuswb_128:
1481 case Intrinsic::x86_sse41_packusdw:
1482 case Intrinsic::x86_avx2_packssdw:
1483 case Intrinsic::x86_avx2_packsswb:
1484 case Intrinsic::x86_avx2_packusdw:
Craig Topper3731f4d2017-02-16 07:35:23 +00001485 case Intrinsic::x86_avx2_packuswb:
1486 case Intrinsic::x86_avx512_packssdw_512:
1487 case Intrinsic::x86_avx512_packsswb_512:
1488 case Intrinsic::x86_avx512_packusdw_512:
1489 case Intrinsic::x86_avx512_packuswb_512: {
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001490 auto *Ty0 = II->getArgOperand(0)->getType();
1491 unsigned InnerVWidth = Ty0->getVectorNumElements();
1492 assert(VWidth == (InnerVWidth * 2) && "Unexpected input size");
1493
1494 unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128;
1495 unsigned VWidthPerLane = VWidth / NumLanes;
1496 unsigned InnerVWidthPerLane = InnerVWidth / NumLanes;
1497
1498 // Per lane, pack the elements of the first input and then the second.
1499 // e.g.
1500 // v8i16 PACK(v4i32 X, v4i32 Y) - (X[0..3],Y[0..3])
1501 // v32i8 PACK(v16i16 X, v16i16 Y) - (X[0..7],Y[0..7]),(X[8..15],Y[8..15])
1502 for (int OpNum = 0; OpNum != 2; ++OpNum) {
1503 APInt OpDemandedElts(InnerVWidth, 0);
1504 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1505 unsigned LaneIdx = Lane * VWidthPerLane;
1506 for (unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) {
1507 unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum;
1508 if (DemandedElts[Idx])
1509 OpDemandedElts.setBit((Lane * InnerVWidthPerLane) + Elt);
1510 }
1511 }
1512
1513 // Demand elements from the operand.
1514 auto *Op = II->getArgOperand(OpNum);
1515 APInt OpUndefElts(InnerVWidth, 0);
1516 TmpV = SimplifyDemandedVectorElts(Op, OpDemandedElts, OpUndefElts,
1517 Depth + 1);
1518 if (TmpV) {
1519 II->setArgOperand(OpNum, TmpV);
1520 MadeChange = true;
1521 }
1522
1523 // Pack the operand's UNDEF elements, one lane at a time.
1524 OpUndefElts = OpUndefElts.zext(VWidth);
1525 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1526 APInt LaneElts = OpUndefElts.lshr(InnerVWidthPerLane * Lane);
1527 LaneElts = LaneElts.getLoBits(InnerVWidthPerLane);
Craig Topper24e71012017-04-28 03:36:24 +00001528 LaneElts <<= InnerVWidthPerLane * (2 * Lane + OpNum);
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001529 UndefElts |= LaneElts;
1530 }
1531 }
1532 break;
1533 }
1534
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001535 // PSHUFB
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001536 case Intrinsic::x86_ssse3_pshuf_b_128:
1537 case Intrinsic::x86_avx2_pshuf_b:
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001538 case Intrinsic::x86_avx512_pshuf_b_512:
1539 // PERMILVAR
1540 case Intrinsic::x86_avx_vpermilvar_ps:
1541 case Intrinsic::x86_avx_vpermilvar_ps_256:
1542 case Intrinsic::x86_avx512_vpermilvar_ps_512:
1543 case Intrinsic::x86_avx_vpermilvar_pd:
1544 case Intrinsic::x86_avx_vpermilvar_pd_256:
Simon Pilgrimfe2c0ed2017-01-18 14:47:49 +00001545 case Intrinsic::x86_avx512_vpermilvar_pd_512:
1546 // PERMV
1547 case Intrinsic::x86_avx2_permd:
1548 case Intrinsic::x86_avx2_permps: {
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001549 Value *Op1 = II->getArgOperand(1);
1550 TmpV = SimplifyDemandedVectorElts(Op1, DemandedElts, UndefElts,
1551 Depth + 1);
1552 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1553 break;
1554 }
1555
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001556 // SSE4A instructions leave the upper 64-bits of the 128-bit result
1557 // in an undefined state.
1558 case Intrinsic::x86_sse4a_extrq:
1559 case Intrinsic::x86_sse4a_extrqi:
1560 case Intrinsic::x86_sse4a_insertq:
1561 case Intrinsic::x86_sse4a_insertqi:
Craig Topper3a86a042017-03-19 05:49:16 +00001562 UndefElts.setHighBits(VWidth / 2);
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001563 break;
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001564 case Intrinsic::amdgcn_buffer_load:
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001565 case Intrinsic::amdgcn_buffer_load_format:
1566 case Intrinsic::amdgcn_image_sample:
1567 case Intrinsic::amdgcn_image_sample_cl:
1568 case Intrinsic::amdgcn_image_sample_d:
1569 case Intrinsic::amdgcn_image_sample_d_cl:
1570 case Intrinsic::amdgcn_image_sample_l:
1571 case Intrinsic::amdgcn_image_sample_b:
1572 case Intrinsic::amdgcn_image_sample_b_cl:
1573 case Intrinsic::amdgcn_image_sample_lz:
1574 case Intrinsic::amdgcn_image_sample_cd:
1575 case Intrinsic::amdgcn_image_sample_cd_cl:
1576
1577 case Intrinsic::amdgcn_image_sample_c:
1578 case Intrinsic::amdgcn_image_sample_c_cl:
1579 case Intrinsic::amdgcn_image_sample_c_d:
1580 case Intrinsic::amdgcn_image_sample_c_d_cl:
1581 case Intrinsic::amdgcn_image_sample_c_l:
1582 case Intrinsic::amdgcn_image_sample_c_b:
1583 case Intrinsic::amdgcn_image_sample_c_b_cl:
1584 case Intrinsic::amdgcn_image_sample_c_lz:
1585 case Intrinsic::amdgcn_image_sample_c_cd:
1586 case Intrinsic::amdgcn_image_sample_c_cd_cl:
1587
1588 case Intrinsic::amdgcn_image_sample_o:
1589 case Intrinsic::amdgcn_image_sample_cl_o:
1590 case Intrinsic::amdgcn_image_sample_d_o:
1591 case Intrinsic::amdgcn_image_sample_d_cl_o:
1592 case Intrinsic::amdgcn_image_sample_l_o:
1593 case Intrinsic::amdgcn_image_sample_b_o:
1594 case Intrinsic::amdgcn_image_sample_b_cl_o:
1595 case Intrinsic::amdgcn_image_sample_lz_o:
1596 case Intrinsic::amdgcn_image_sample_cd_o:
1597 case Intrinsic::amdgcn_image_sample_cd_cl_o:
1598
1599 case Intrinsic::amdgcn_image_sample_c_o:
1600 case Intrinsic::amdgcn_image_sample_c_cl_o:
1601 case Intrinsic::amdgcn_image_sample_c_d_o:
1602 case Intrinsic::amdgcn_image_sample_c_d_cl_o:
1603 case Intrinsic::amdgcn_image_sample_c_l_o:
1604 case Intrinsic::amdgcn_image_sample_c_b_o:
1605 case Intrinsic::amdgcn_image_sample_c_b_cl_o:
1606 case Intrinsic::amdgcn_image_sample_c_lz_o:
1607 case Intrinsic::amdgcn_image_sample_c_cd_o:
1608 case Intrinsic::amdgcn_image_sample_c_cd_cl_o:
1609
1610 case Intrinsic::amdgcn_image_getlod: {
Craig Topperd33ee1b2017-04-03 16:34:59 +00001611 if (VWidth == 1 || !DemandedElts.isMask())
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001612 return nullptr;
1613
1614 // TODO: Handle 3 vectors when supported in code gen.
1615 unsigned NewNumElts = PowerOf2Ceil(DemandedElts.countTrailingOnes());
1616 if (NewNumElts == VWidth)
1617 return nullptr;
1618
1619 Module *M = II->getParent()->getParent()->getParent();
1620 Type *EltTy = V->getType()->getVectorElementType();
1621
1622 Type *NewTy = (NewNumElts == 1) ? EltTy :
1623 VectorType::get(EltTy, NewNumElts);
1624
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001625 auto IID = II->getIntrinsicID();
1626
1627 bool IsBuffer = IID == Intrinsic::amdgcn_buffer_load ||
1628 IID == Intrinsic::amdgcn_buffer_load_format;
1629
1630 Function *NewIntrin = IsBuffer ?
1631 Intrinsic::getDeclaration(M, IID, NewTy) :
1632 // Samplers have 3 mangled types.
1633 Intrinsic::getDeclaration(M, IID,
1634 { NewTy, II->getArgOperand(0)->getType(),
1635 II->getArgOperand(1)->getType()});
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001636
1637 SmallVector<Value *, 5> Args;
1638 for (unsigned I = 0, E = II->getNumArgOperands(); I != E; ++I)
1639 Args.push_back(II->getArgOperand(I));
1640
Matt Arsenaulta3bdd8f2017-03-10 05:25:49 +00001641 IRBuilderBase::InsertPointGuard Guard(*Builder);
1642 Builder->SetInsertPoint(II);
1643
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001644 CallInst *NewCall = Builder->CreateCall(NewIntrin, Args);
1645 NewCall->takeName(II);
1646 NewCall->copyMetadata(*II);
Matt Arsenault7205f3c2017-04-17 15:12:44 +00001647
1648 if (!IsBuffer) {
1649 ConstantInt *DMask = dyn_cast<ConstantInt>(NewCall->getArgOperand(3));
1650 if (DMask) {
1651 unsigned DMaskVal = DMask->getZExtValue() & 0xf;
1652
1653 unsigned PopCnt = 0;
1654 unsigned NewDMask = 0;
1655 for (unsigned I = 0; I < 4; ++I) {
1656 const unsigned Bit = 1 << I;
1657 if (!!(DMaskVal & Bit)) {
1658 if (++PopCnt > NewNumElts)
1659 break;
1660
1661 NewDMask |= Bit;
1662 }
1663 }
1664
1665 NewCall->setArgOperand(3, ConstantInt::get(DMask->getType(), NewDMask));
1666 }
1667 }
1668
1669
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001670 if (NewNumElts == 1) {
1671 return Builder->CreateInsertElement(UndefValue::get(V->getType()),
1672 NewCall, static_cast<uint64_t>(0));
1673 }
1674
1675 SmallVector<uint32_t, 8> EltMask;
1676 for (unsigned I = 0; I < VWidth; ++I)
1677 EltMask.push_back(I);
1678
1679 Value *Shuffle = Builder->CreateShuffleVector(
1680 NewCall, UndefValue::get(NewTy), EltMask);
1681
1682 MadeChange = true;
1683 return Shuffle;
1684 }
Chris Lattner7e044912010-01-04 07:17:19 +00001685 }
1686 break;
1687 }
1688 }
Craig Topperf40110f2014-04-25 05:29:35 +00001689 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001690}