blob: ae214d8bdf54a64130b968adb89da45941ec708d [file] [log] [blame]
Chris Lattner7e044912010-01-04 07:17:19 +00001//===- InstCombineSimplifyDemanded.cpp ------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains logic for simplifying instructions based on information
11// about how they are used.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carrutha9174582015-01-22 05:25:13 +000015#include "InstCombineInternal.h"
James Molloy2b21a7c2015-05-20 18:41:25 +000016#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000018#include "llvm/IR/PatternMatch.h"
Chris Lattner7e044912010-01-04 07:17:19 +000019
20using namespace llvm;
Shuxin Yang63e999e2012-12-04 00:04:54 +000021using namespace llvm::PatternMatch;
Chris Lattner7e044912010-01-04 07:17:19 +000022
Chandler Carruth964daaa2014-04-22 02:55:47 +000023#define DEBUG_TYPE "instcombine"
24
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000025/// Check to see if the specified operand of the specified instruction is a
26/// constant integer. If so, check to see if there are any bits set in the
27/// constant that are not demanded. If so, shrink the constant and return true.
Craig Topper4c947752012-12-22 18:09:02 +000028static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
Chris Lattner7e044912010-01-04 07:17:19 +000029 APInt Demanded) {
30 assert(I && "No instruction?");
31 assert(OpNo < I->getNumOperands() && "Operand index too large");
32
Sanjay Patelae3b43e2017-02-09 21:43:06 +000033 // The operand must be a constant integer or splat integer.
34 Value *Op = I->getOperand(OpNo);
35 const APInt *C;
36 if (!match(Op, m_APInt(C)))
37 return false;
Chris Lattner7e044912010-01-04 07:17:19 +000038
39 // If there are no bits set that aren't demanded, nothing to do.
Sanjay Patelae3b43e2017-02-09 21:43:06 +000040 Demanded = Demanded.zextOrTrunc(C->getBitWidth());
41 if ((~Demanded & *C) == 0)
Chris Lattner7e044912010-01-04 07:17:19 +000042 return false;
43
44 // This instruction is producing bits that are not demanded. Shrink the RHS.
Sanjay Patelae3b43e2017-02-09 21:43:06 +000045 Demanded &= *C;
46 I->setOperand(OpNo, ConstantInt::get(Op->getType(), Demanded));
David Majnemer42b83a52014-08-22 07:56:32 +000047
Chris Lattner7e044912010-01-04 07:17:19 +000048 return true;
49}
50
51
52
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000053/// Inst is an integer instruction that SimplifyDemandedBits knows about. See if
54/// the instruction has any properties that allow us to simplify its operands.
Chris Lattner7e044912010-01-04 07:17:19 +000055bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) {
56 unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
57 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
58 APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
Craig Topper4c947752012-12-22 18:09:02 +000059
Mehdi Aminia28d91d2015-03-10 02:37:25 +000060 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, KnownZero, KnownOne,
61 0, &Inst);
Craig Topperf40110f2014-04-25 05:29:35 +000062 if (!V) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000063 if (V == &Inst) return true;
Sanjay Patel4b198802016-02-01 22:23:39 +000064 replaceInstUsesWith(Inst, V);
Chris Lattner7e044912010-01-04 07:17:19 +000065 return true;
66}
67
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000068/// This form of SimplifyDemandedBits simplifies the specified instruction
69/// operand if possible, updating it in place. It returns true if it made any
70/// change and false otherwise.
Craig Topper47596dd2017-03-25 06:52:52 +000071bool InstCombiner::SimplifyDemandedBits(Instruction *I, unsigned OpNo,
72 const APInt &DemandedMask,
Chris Lattner7e044912010-01-04 07:17:19 +000073 APInt &KnownZero, APInt &KnownOne,
74 unsigned Depth) {
Craig Topper47596dd2017-03-25 06:52:52 +000075 Use &U = I->getOperandUse(OpNo);
David Majnemerfe58d132015-04-22 20:59:28 +000076 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, KnownZero,
Craig Topper47596dd2017-03-25 06:52:52 +000077 KnownOne, Depth, I);
Craig Topperf40110f2014-04-25 05:29:35 +000078 if (!NewVal) return false;
Chris Lattner7e044912010-01-04 07:17:19 +000079 U = NewVal;
80 return true;
81}
82
83
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +000084/// This function attempts to replace V with a simpler value based on the
85/// demanded bits. When this function is called, it is known that only the bits
86/// set in DemandedMask of the result of V are ever used downstream.
87/// Consequently, depending on the mask and V, it may be possible to replace V
88/// with a constant or one of its operands. In such cases, this function does
89/// the replacement and returns true. In all other cases, it returns false after
90/// analyzing the expression and setting KnownOne and known to be one in the
91/// expression. KnownZero contains all the bits that are known to be zero in the
92/// expression. These are provided to potentially allow the caller (which might
93/// recursively be SimplifyDemandedBits itself) to simplify the expression.
94/// KnownOne and KnownZero always follow the invariant that:
95/// KnownOne & KnownZero == 0.
96/// That is, a bit can't be both 1 and 0. Note that the bits in KnownOne and
97/// KnownZero may only be accurate for those bits set in DemandedMask. Note also
98/// that the bitwidth of V, DemandedMask, KnownZero and KnownOne must all be the
99/// same.
Chris Lattner7e044912010-01-04 07:17:19 +0000100///
101/// This returns null if it did not change anything and it permits no
102/// simplification. This returns V itself if it did some simplification of V's
103/// operands based on the information about what bits are demanded. This returns
104/// some other non-null value if it found out that V is equal to another value
105/// in the context where the specified bits are demanded, but not for all users.
106Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
107 APInt &KnownZero, APInt &KnownOne,
Hal Finkel60db0582014-09-07 18:57:58 +0000108 unsigned Depth,
109 Instruction *CxtI) {
Craig Toppere73658d2014-04-28 04:05:08 +0000110 assert(V != nullptr && "Null pointer of Value???");
Chris Lattner7e044912010-01-04 07:17:19 +0000111 assert(Depth <= 6 && "Limit Search Depth");
112 uint32_t BitWidth = DemandedMask.getBitWidth();
Chris Lattner229907c2011-07-18 04:54:35 +0000113 Type *VTy = V->getType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000114 assert(
115 (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
116 KnownZero.getBitWidth() == BitWidth &&
117 KnownOne.getBitWidth() == BitWidth &&
118 "Value *V, DemandedMask, KnownZero and KnownOne "
119 "must have same BitWidth");
Sanjay Patelae3b43e2017-02-09 21:43:06 +0000120 const APInt *C;
121 if (match(V, m_APInt(C))) {
122 // We know all of the bits for a scalar constant or a splat vector constant!
123 KnownOne = *C & DemandedMask;
Chris Lattner7e044912010-01-04 07:17:19 +0000124 KnownZero = ~KnownOne & DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000125 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000126 }
127 if (isa<ConstantPointerNull>(V)) {
128 // We know all of the bits for a constant!
Jay Foad25a5e4c2010-12-01 08:53:58 +0000129 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000130 KnownZero = DemandedMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000131 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000132 }
133
Jay Foad25a5e4c2010-12-01 08:53:58 +0000134 KnownZero.clearAllBits();
135 KnownOne.clearAllBits();
Chris Lattner7e044912010-01-04 07:17:19 +0000136 if (DemandedMask == 0) { // Not demanding any bits from V.
137 if (isa<UndefValue>(V))
Craig Topperf40110f2014-04-25 05:29:35 +0000138 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000139 return UndefValue::get(VTy);
140 }
Craig Topper4c947752012-12-22 18:09:02 +0000141
Chris Lattner7e044912010-01-04 07:17:19 +0000142 if (Depth == 6) // Limit search depth.
Craig Topperf40110f2014-04-25 05:29:35 +0000143 return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000144
Chris Lattner7e044912010-01-04 07:17:19 +0000145 Instruction *I = dyn_cast<Instruction>(V);
146 if (!I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000147 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Craig Topperf40110f2014-04-25 05:29:35 +0000148 return nullptr; // Only analyze instructions.
Chris Lattner7e044912010-01-04 07:17:19 +0000149 }
150
151 // If there are multiple uses of this value and we aren't at the root, then
152 // we can't do any simplifications of the operands, because DemandedMask
153 // only reflects the bits demanded by *one* of the users.
154 if (Depth != 0 && !I->hasOneUse()) {
Craig Topperb0076fe2017-04-12 18:05:21 +0000155 return SimplifyMultipleUseDemandedBits(I, DemandedMask, KnownZero, KnownOne,
156 Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000157 }
Craig Topper4c947752012-12-22 18:09:02 +0000158
Craig Topperb0076fe2017-04-12 18:05:21 +0000159 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
160 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
161
Chris Lattner7e044912010-01-04 07:17:19 +0000162 // If this is the root being simplified, allow it to have multiple uses,
163 // just set the DemandedMask to all bits so that we can try to simplify the
164 // operands. This allows visitTruncInst (for example) to simplify the
165 // operand of a trunc without duplicating all the logic below.
166 if (Depth == 0 && !V->hasOneUse())
Craig Toppere06b6bc2017-04-04 05:03:02 +0000167 DemandedMask.setAllBits();
Craig Topper4c947752012-12-22 18:09:02 +0000168
Chris Lattner7e044912010-01-04 07:17:19 +0000169 switch (I->getOpcode()) {
170 default:
Hal Finkel60db0582014-09-07 18:57:58 +0000171 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000172 break;
173 case Instruction::And:
174 // If either the LHS or the RHS are Zero, the result is zero.
Craig Topper47596dd2017-03-25 06:52:52 +0000175 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
176 Depth + 1) ||
177 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnownZero, LHSKnownZero,
178 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000179 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000180 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
181 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000182
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000183 // If the client is only demanding bits that we know, return the known
184 // constant.
185 if ((DemandedMask & ((RHSKnownZero | LHSKnownZero)|
186 (RHSKnownOne & LHSKnownOne))) == DemandedMask)
187 return Constant::getIntegerValue(VTy, RHSKnownOne & LHSKnownOne);
188
Chris Lattner7e044912010-01-04 07:17:19 +0000189 // If all of the demanded bits are known 1 on one side, return the other.
190 // These bits cannot contribute to the result of the 'and'.
Craig Topper4c947752012-12-22 18:09:02 +0000191 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000192 (DemandedMask & ~LHSKnownZero))
193 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000194 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000195 (DemandedMask & ~RHSKnownZero))
196 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000197
Chris Lattner7e044912010-01-04 07:17:19 +0000198 // If all of the demanded bits in the inputs are known zeros, return zero.
199 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
200 return Constant::getNullValue(VTy);
Craig Topper4c947752012-12-22 18:09:02 +0000201
Chris Lattner7e044912010-01-04 07:17:19 +0000202 // If the RHS is a constant, see if we can simplify it.
203 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
204 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000205
Chris Lattner7e044912010-01-04 07:17:19 +0000206 // Output known-1 bits are only known if set in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000207 KnownOne = RHSKnownOne & LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000208 // Output known-0 are known to be clear if zero in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000209 KnownZero = RHSKnownZero | LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000210 break;
211 case Instruction::Or:
212 // If either the LHS or the RHS are One, the result is One.
Craig Topper47596dd2017-03-25 06:52:52 +0000213 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
214 Depth + 1) ||
215 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnownOne, LHSKnownZero,
216 LHSKnownOne, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000217 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000218 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
219 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
220
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000221 // If the client is only demanding bits that we know, return the known
222 // constant.
223 if ((DemandedMask & ((RHSKnownZero & LHSKnownZero)|
224 (RHSKnownOne | LHSKnownOne))) == DemandedMask)
225 return Constant::getIntegerValue(VTy, RHSKnownOne | LHSKnownOne);
226
Chris Lattner7e044912010-01-04 07:17:19 +0000227 // If all of the demanded bits are known zero on one side, return the other.
228 // These bits cannot contribute to the result of the 'or'.
Craig Topper4c947752012-12-22 18:09:02 +0000229 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000230 (DemandedMask & ~LHSKnownOne))
231 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000232 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000233 (DemandedMask & ~RHSKnownOne))
234 return I->getOperand(1);
235
236 // If all of the potentially set bits on one side are known to be set on
237 // the other side, just use the 'other' side.
Craig Topper4c947752012-12-22 18:09:02 +0000238 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000239 (DemandedMask & (~RHSKnownZero)))
240 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000241 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
Chris Lattner7e044912010-01-04 07:17:19 +0000242 (DemandedMask & (~LHSKnownZero)))
243 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000244
Chris Lattner7e044912010-01-04 07:17:19 +0000245 // If the RHS is a constant, see if we can simplify it.
246 if (ShrinkDemandedConstant(I, 1, DemandedMask))
247 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000248
Chris Lattner7e044912010-01-04 07:17:19 +0000249 // Output known-0 bits are only known if clear in both the LHS & RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000250 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000251 // Output known-1 are known to be set if set in either the LHS | RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000252 KnownOne = RHSKnownOne | LHSKnownOne;
Chris Lattner7e044912010-01-04 07:17:19 +0000253 break;
254 case Instruction::Xor: {
Craig Topper47596dd2017-03-25 06:52:52 +0000255 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnownZero, RHSKnownOne,
256 Depth + 1) ||
257 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnownZero, LHSKnownOne,
258 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000259 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000260 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
261 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
262
Hal Finkel15aeaaf2014-09-07 19:21:07 +0000263 // Output known-0 bits are known if clear or set in both the LHS & RHS.
264 APInt IKnownZero = (RHSKnownZero & LHSKnownZero) |
265 (RHSKnownOne & LHSKnownOne);
266 // Output known-1 are known to be set if set in only one of the LHS, RHS.
267 APInt IKnownOne = (RHSKnownZero & LHSKnownOne) |
268 (RHSKnownOne & LHSKnownZero);
269
270 // If the client is only demanding bits that we know, return the known
271 // constant.
272 if ((DemandedMask & (IKnownZero|IKnownOne)) == DemandedMask)
273 return Constant::getIntegerValue(VTy, IKnownOne);
274
Chris Lattner7e044912010-01-04 07:17:19 +0000275 // If all of the demanded bits are known zero on one side, return the other.
276 // These bits cannot contribute to the result of the 'xor'.
277 if ((DemandedMask & RHSKnownZero) == DemandedMask)
278 return I->getOperand(0);
279 if ((DemandedMask & LHSKnownZero) == DemandedMask)
280 return I->getOperand(1);
Craig Topper4c947752012-12-22 18:09:02 +0000281
Chris Lattner7e044912010-01-04 07:17:19 +0000282 // If all of the demanded bits are known to be zero on one side or the
283 // other, turn this into an *inclusive* or.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000284 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
Chris Lattner7e044912010-01-04 07:17:19 +0000285 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
Craig Topper4c947752012-12-22 18:09:02 +0000286 Instruction *Or =
Chris Lattner7e044912010-01-04 07:17:19 +0000287 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
288 I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000289 return InsertNewInstWith(Or, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000290 }
Craig Topper4c947752012-12-22 18:09:02 +0000291
Chris Lattner7e044912010-01-04 07:17:19 +0000292 // If all of the demanded bits on one side are known, and all of the set
293 // bits on that side are also known to be set on the other side, turn this
294 // into an AND, as we know the bits will be cleared.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000295 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
Craig Topper4c947752012-12-22 18:09:02 +0000296 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
Chris Lattner7e044912010-01-04 07:17:19 +0000297 // all known
298 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
299 Constant *AndC = Constant::getIntegerValue(VTy,
300 ~RHSKnownOne & DemandedMask);
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000301 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000302 return InsertNewInstWith(And, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000303 }
304 }
Craig Topper4c947752012-12-22 18:09:02 +0000305
Chris Lattner7e044912010-01-04 07:17:19 +0000306 // If the RHS is a constant, see if we can simplify it.
307 // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
308 if (ShrinkDemandedConstant(I, 1, DemandedMask))
309 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000310
Chris Lattner7e044912010-01-04 07:17:19 +0000311 // If our LHS is an 'and' and if it has one use, and if any of the bits we
312 // are flipping are known to be set, then the xor is just resetting those
313 // bits to zero. We can just knock out bits from the 'and' and the 'xor',
314 // simplifying both of them.
315 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
316 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
317 isa<ConstantInt>(I->getOperand(1)) &&
318 isa<ConstantInt>(LHSInst->getOperand(1)) &&
319 (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
320 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
321 ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
322 APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
Craig Topper4c947752012-12-22 18:09:02 +0000323
Chris Lattner7e044912010-01-04 07:17:19 +0000324 Constant *AndC =
325 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000326 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000327 InsertNewInstWith(NewAnd, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000328
Chris Lattner7e044912010-01-04 07:17:19 +0000329 Constant *XorC =
330 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000331 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000332 return InsertNewInstWith(NewXor, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000333 }
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000334
335 // Output known-0 bits are known if clear or set in both the LHS & RHS.
336 KnownZero= (RHSKnownZero & LHSKnownZero) | (RHSKnownOne & LHSKnownOne);
337 // Output known-1 are known to be set if set in only one of the LHS, RHS.
338 KnownOne = (RHSKnownZero & LHSKnownOne) | (RHSKnownOne & LHSKnownZero);
Chris Lattner7e044912010-01-04 07:17:19 +0000339 break;
340 }
341 case Instruction::Select:
James Molloy2b21a7c2015-05-20 18:41:25 +0000342 // If this is a select as part of a min/max pattern, don't simplify any
343 // further in case we break the structure.
344 Value *LHS, *RHS;
James Molloy134bec22015-08-11 09:12:57 +0000345 if (matchSelectPattern(I, LHS, RHS).Flavor != SPF_UNKNOWN)
James Molloy2b21a7c2015-05-20 18:41:25 +0000346 return nullptr;
Simon Pilgrim61116dd2015-09-17 20:32:45 +0000347
Craig Topper47596dd2017-03-25 06:52:52 +0000348 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnownZero, RHSKnownOne,
349 Depth + 1) ||
350 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnownZero, LHSKnownOne,
351 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000352 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000353 assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
354 assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
355
Chris Lattner7e044912010-01-04 07:17:19 +0000356 // If the operands are constants, see if we can simplify them.
357 if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
358 ShrinkDemandedConstant(I, 2, DemandedMask))
359 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000360
Chris Lattner7e044912010-01-04 07:17:19 +0000361 // Only known if known in both the LHS and RHS.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000362 KnownOne = RHSKnownOne & LHSKnownOne;
363 KnownZero = RHSKnownZero & LHSKnownZero;
Chris Lattner7e044912010-01-04 07:17:19 +0000364 break;
365 case Instruction::Trunc: {
366 unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +0000367 DemandedMask = DemandedMask.zext(truncBf);
368 KnownZero = KnownZero.zext(truncBf);
369 KnownOne = KnownOne.zext(truncBf);
Craig Topper47596dd2017-03-25 06:52:52 +0000370 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
371 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000372 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000373 DemandedMask = DemandedMask.trunc(BitWidth);
374 KnownZero = KnownZero.trunc(BitWidth);
375 KnownOne = KnownOne.trunc(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000376 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000377 break;
378 }
379 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000380 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000381 return nullptr; // vector->int or fp->int?
Chris Lattner7e044912010-01-04 07:17:19 +0000382
Chris Lattner229907c2011-07-18 04:54:35 +0000383 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
384 if (VectorType *SrcVTy =
Chris Lattner7e044912010-01-04 07:17:19 +0000385 dyn_cast<VectorType>(I->getOperand(0)->getType())) {
386 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
387 // Don't touch a bitcast between vectors of different element counts.
Craig Topperf40110f2014-04-25 05:29:35 +0000388 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000389 } else
390 // Don't touch a scalar-to-vector bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000391 return nullptr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000392 } else if (I->getOperand(0)->getType()->isVectorTy())
Chris Lattner7e044912010-01-04 07:17:19 +0000393 // Don't touch a vector-to-scalar bitcast.
Craig Topperf40110f2014-04-25 05:29:35 +0000394 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000395
Craig Topper47596dd2017-03-25 06:52:52 +0000396 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
397 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000398 return I;
Craig Topper4c947752012-12-22 18:09:02 +0000399 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000400 break;
401 case Instruction::ZExt: {
402 // Compute the bits in the result that are not present in the input.
403 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000404
Jay Foad583abbc2010-12-07 08:25:19 +0000405 DemandedMask = DemandedMask.trunc(SrcBitWidth);
406 KnownZero = KnownZero.trunc(SrcBitWidth);
407 KnownOne = KnownOne.trunc(SrcBitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000408 if (SimplifyDemandedBits(I, 0, DemandedMask, KnownZero, KnownOne,
409 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000410 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000411 DemandedMask = DemandedMask.zext(BitWidth);
412 KnownZero = KnownZero.zext(BitWidth);
413 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000414 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000415 // The top bits are known to be zero.
Craig Topper3a86a042017-03-19 05:49:16 +0000416 KnownZero.setBitsFrom(SrcBitWidth);
Chris Lattner7e044912010-01-04 07:17:19 +0000417 break;
418 }
419 case Instruction::SExt: {
420 // Compute the bits in the result that are not present in the input.
421 unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
Craig Topper4c947752012-12-22 18:09:02 +0000422
423 APInt InputDemandedBits = DemandedMask &
Chris Lattner7e044912010-01-04 07:17:19 +0000424 APInt::getLowBitsSet(BitWidth, SrcBitWidth);
425
Craig Topper3a86a042017-03-19 05:49:16 +0000426 APInt NewBits(APInt::getBitsSetFrom(BitWidth, SrcBitWidth));
Chris Lattner7e044912010-01-04 07:17:19 +0000427 // If any of the sign extended bits are demanded, we know that the sign
428 // bit is demanded.
429 if ((NewBits & DemandedMask) != 0)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000430 InputDemandedBits.setBit(SrcBitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000431
Jay Foad583abbc2010-12-07 08:25:19 +0000432 InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
433 KnownZero = KnownZero.trunc(SrcBitWidth);
434 KnownOne = KnownOne.trunc(SrcBitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000435 if (SimplifyDemandedBits(I, 0, InputDemandedBits, KnownZero, KnownOne,
436 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000437 return I;
Jay Foad583abbc2010-12-07 08:25:19 +0000438 InputDemandedBits = InputDemandedBits.zext(BitWidth);
439 KnownZero = KnownZero.zext(BitWidth);
440 KnownOne = KnownOne.zext(BitWidth);
Craig Topper4c947752012-12-22 18:09:02 +0000441 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
442
Chris Lattner7e044912010-01-04 07:17:19 +0000443 // If the sign bit of the input is known set or clear, then we know the
444 // top bits of the result.
445
446 // If the input sign bit is known zero, or if the NewBits are not demanded
447 // convert this into a zero extension.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000448 if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
Chris Lattner7e044912010-01-04 07:17:19 +0000449 // Convert to ZExt cast
450 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000451 return InsertNewInstWith(NewCast, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000452 } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
453 KnownOne |= NewBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000454 }
455 break;
456 }
Matthias Braune48484c2015-04-30 22:05:30 +0000457 case Instruction::Add:
458 case Instruction::Sub: {
459 /// If the high-bits of an ADD/SUB are not demanded, then we do not care
460 /// about the high bits of the operands.
Chris Lattner7e044912010-01-04 07:17:19 +0000461 unsigned NLZ = DemandedMask.countLeadingZeros();
Matthias Braune48484c2015-04-30 22:05:30 +0000462 if (NLZ > 0) {
463 // Right fill the mask of bits for this ADD/SUB to demand the most
Chris Lattner7e044912010-01-04 07:17:19 +0000464 // significant bit and all those below it.
Chris Lattner7e044912010-01-04 07:17:19 +0000465 APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
Craig Topper07f29152017-03-22 04:03:53 +0000466 if (ShrinkDemandedConstant(I, 0, DemandedFromOps) ||
Craig Topper47596dd2017-03-25 06:52:52 +0000467 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnownZero, LHSKnownOne,
468 Depth + 1) ||
Matthias Braune48484c2015-04-30 22:05:30 +0000469 ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
Craig Topper845033a2017-04-12 16:49:59 +0000470 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnownZero, RHSKnownOne,
Craig Topper47596dd2017-03-25 06:52:52 +0000471 Depth + 1)) {
Matthias Braune48484c2015-04-30 22:05:30 +0000472 // Disable the nsw and nuw flags here: We can no longer guarantee that
473 // we won't wrap after simplification. Removing the nsw/nuw flags is
474 // legal here because the top bit is not demanded.
475 BinaryOperator &BinOP = *cast<BinaryOperator>(I);
476 BinOP.setHasNoSignedWrap(false);
477 BinOP.setHasNoUnsignedWrap(false);
Chris Lattner7e044912010-01-04 07:17:19 +0000478 return I;
David Majnemer7d0e99c2015-04-22 22:42:05 +0000479 }
Craig Topper845033a2017-04-12 16:49:59 +0000480
481 // If we are known to be adding/subtracting zeros to every bit below
482 // the highest demanded bit, we just return the other side.
483 if ((DemandedFromOps & RHSKnownZero) == DemandedFromOps)
484 return I->getOperand(0);
485 // We can't do this with the LHS for subtraction.
486 if (I->getOpcode() == Instruction::Add &&
487 (DemandedFromOps & LHSKnownZero) == DemandedFromOps)
488 return I->getOperand(1);
Chris Lattner7e044912010-01-04 07:17:19 +0000489 }
Benjamin Kramer010337c2011-12-24 17:31:38 +0000490
Craig Topper8fbb74b2017-03-24 22:12:10 +0000491 // Otherwise just hand the add/sub off to computeKnownBits to fill in
492 // the known zeros and ones.
493 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000494 break;
Matthias Braune48484c2015-04-30 22:05:30 +0000495 }
Chris Lattner7e044912010-01-04 07:17:19 +0000496 case Instruction::Shl:
497 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000498 {
499 Value *VarX; ConstantInt *C1;
500 if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
501 Instruction *Shr = cast<Instruction>(I->getOperand(0));
502 Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
503 KnownZero, KnownOne);
504 if (R)
505 return R;
506 }
507 }
508
Chris Lattner768003c2011-02-10 05:09:34 +0000509 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Chris Lattner7e044912010-01-04 07:17:19 +0000510 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000511
Chris Lattner768003c2011-02-10 05:09:34 +0000512 // If the shift is NUW/NSW, then it does demand the high bits.
513 ShlOperator *IOp = cast<ShlOperator>(I);
514 if (IOp->hasNoSignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000515 DemandedMaskIn.setHighBits(ShiftAmt+1);
Chris Lattner768003c2011-02-10 05:09:34 +0000516 else if (IOp->hasNoUnsignedWrap())
Craig Topper3a86a042017-03-19 05:49:16 +0000517 DemandedMaskIn.setHighBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000518
Craig Topper47596dd2017-03-25 06:52:52 +0000519 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
520 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000521 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000522 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
523 KnownZero <<= ShiftAmt;
524 KnownOne <<= ShiftAmt;
Chris Lattner7e044912010-01-04 07:17:19 +0000525 // low bits known zero.
526 if (ShiftAmt)
Craig Topper3a86a042017-03-19 05:49:16 +0000527 KnownZero.setLowBits(ShiftAmt);
Chris Lattner7e044912010-01-04 07:17:19 +0000528 }
529 break;
530 case Instruction::LShr:
531 // For a logical shift right
532 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000533 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000534
Chris Lattner7e044912010-01-04 07:17:19 +0000535 // Unsigned shift right.
536 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
Craig Topper4c947752012-12-22 18:09:02 +0000537
Chris Lattner768003c2011-02-10 05:09:34 +0000538 // If the shift is exact, then it does demand the low bits (and knows that
539 // they are zero).
540 if (cast<LShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000541 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000542
Craig Topper47596dd2017-03-25 06:52:52 +0000543 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
544 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000545 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000546 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Craig Topper885fa122017-03-31 20:01:16 +0000547 KnownZero = KnownZero.lshr(ShiftAmt);
548 KnownOne = KnownOne.lshr(ShiftAmt);
Craig Topper3a86a042017-03-19 05:49:16 +0000549 if (ShiftAmt)
550 KnownZero.setHighBits(ShiftAmt); // high bits known zero.
Chris Lattner7e044912010-01-04 07:17:19 +0000551 }
552 break;
553 case Instruction::AShr:
554 // If this is an arithmetic shift right and only the low-bit is set, we can
555 // always convert this into a logical shr, even if the shift amount is
556 // variable. The low bit of the shift cannot be an input sign bit unless
557 // the shift amount is >= the size of the datatype, which is undefined.
558 if (DemandedMask == 1) {
559 // Perform the logical shift right.
560 Instruction *NewVal = BinaryOperator::CreateLShr(
561 I->getOperand(0), I->getOperand(1), I->getName());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000562 return InsertNewInstWith(NewVal, *I);
Craig Topper4c947752012-12-22 18:09:02 +0000563 }
Chris Lattner7e044912010-01-04 07:17:19 +0000564
565 // If the sign bit is the only bit demanded by this ashr, then there is no
566 // need to do it, the shift doesn't change the high bit.
567 if (DemandedMask.isSignBit())
568 return I->getOperand(0);
Craig Topper4c947752012-12-22 18:09:02 +0000569
Chris Lattner7e044912010-01-04 07:17:19 +0000570 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner768003c2011-02-10 05:09:34 +0000571 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000572
Chris Lattner7e044912010-01-04 07:17:19 +0000573 // Signed shift right.
574 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
575 // If any of the "high bits" are demanded, we should set the sign bit as
576 // demanded.
577 if (DemandedMask.countLeadingZeros() <= ShiftAmt)
Jay Foad25a5e4c2010-12-01 08:53:58 +0000578 DemandedMaskIn.setBit(BitWidth-1);
Craig Topper4c947752012-12-22 18:09:02 +0000579
Chris Lattner768003c2011-02-10 05:09:34 +0000580 // If the shift is exact, then it does demand the low bits (and knows that
581 // they are zero).
582 if (cast<AShrOperator>(I)->isExact())
Craig Topper3a86a042017-03-19 05:49:16 +0000583 DemandedMaskIn.setLowBits(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000584
Craig Topper47596dd2017-03-25 06:52:52 +0000585 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, KnownZero, KnownOne,
586 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000587 return I;
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000588 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000589 // Compute the new bits that are at the top now.
590 APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
Craig Topper885fa122017-03-31 20:01:16 +0000591 KnownZero = KnownZero.lshr(ShiftAmt);
592 KnownOne = KnownOne.lshr(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000593
Chris Lattner7e044912010-01-04 07:17:19 +0000594 // Handle the sign bits.
595 APInt SignBit(APInt::getSignBit(BitWidth));
596 // Adjust to where it is now in the mask.
Craig Topper885fa122017-03-31 20:01:16 +0000597 SignBit = SignBit.lshr(ShiftAmt);
Craig Topper4c947752012-12-22 18:09:02 +0000598
Chris Lattner7e044912010-01-04 07:17:19 +0000599 // If the input sign bit is known to be zero, or if none of the top bits
600 // are demanded, turn this into an unsigned shift right.
Craig Topper4c947752012-12-22 18:09:02 +0000601 if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
Chris Lattner7e044912010-01-04 07:17:19 +0000602 (HighBits & ~DemandedMask) == HighBits) {
603 // Perform the logical shift right.
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000604 BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
605 SA, I->getName());
606 NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
Eli Friedman6efb64e2011-05-19 01:20:42 +0000607 return InsertNewInstWith(NewVal, *I);
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000608 } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
609 KnownOne |= HighBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000610 }
611 }
612 break;
613 case Instruction::SRem:
614 if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
Eli Friedmana81a82d2011-03-09 01:28:35 +0000615 // X % -1 demands all the bits because we don't want to introduce
616 // INT_MIN % -1 (== undef) by accident.
617 if (Rem->isAllOnesValue())
618 break;
Chris Lattner7e044912010-01-04 07:17:19 +0000619 APInt RA = Rem->getValue().abs();
620 if (RA.isPowerOf2()) {
621 if (DemandedMask.ult(RA)) // srem won't affect demanded bits
622 return I->getOperand(0);
623
624 APInt LowBits = RA - 1;
625 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000626 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnownZero, LHSKnownOne,
627 Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000628 return I;
629
Duncan Sands3a48b872010-01-28 17:22:42 +0000630 // The low bits of LHS are unchanged by the srem.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000631 KnownZero = LHSKnownZero & LowBits;
632 KnownOne = LHSKnownOne & LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000633
Duncan Sands3a48b872010-01-28 17:22:42 +0000634 // If LHS is non-negative or has all low bits zero, then the upper bits
635 // are all zero.
636 if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
637 KnownZero |= ~LowBits;
638
639 // If LHS is negative and not all low bits are zero, then the upper bits
640 // are all one.
641 if (LHSKnownOne[BitWidth-1] && ((LHSKnownOne & LowBits) != 0))
642 KnownOne |= ~LowBits;
Chris Lattner7e044912010-01-04 07:17:19 +0000643
Craig Topper4c947752012-12-22 18:09:02 +0000644 assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
Chris Lattner7e044912010-01-04 07:17:19 +0000645 }
646 }
Nick Lewyckye4679792011-03-07 01:50:10 +0000647
648 // The sign bit is the LHS's sign bit, except when the result of the
649 // remainder is zero.
650 if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
Nick Lewyckye4679792011-03-07 01:50:10 +0000651 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000652 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
Hal Finkel60db0582014-09-07 18:57:58 +0000653 CxtI);
Nick Lewyckye4679792011-03-07 01:50:10 +0000654 // If it's known zero, our sign bit is also zero.
655 if (LHSKnownZero.isNegative())
Craig Topper3a86a042017-03-19 05:49:16 +0000656 KnownZero.setSignBit();
Nick Lewyckye4679792011-03-07 01:50:10 +0000657 }
Chris Lattner7e044912010-01-04 07:17:19 +0000658 break;
659 case Instruction::URem: {
660 APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
661 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
Craig Topper47596dd2017-03-25 06:52:52 +0000662 if (SimplifyDemandedBits(I, 0, AllOnes, KnownZero2, KnownOne2, Depth + 1) ||
663 SimplifyDemandedBits(I, 1, AllOnes, KnownZero2, KnownOne2, Depth + 1))
Chris Lattner7e044912010-01-04 07:17:19 +0000664 return I;
665
666 unsigned Leaders = KnownZero2.countLeadingOnes();
Chris Lattner7e044912010-01-04 07:17:19 +0000667 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
668 break;
669 }
670 case Instruction::Call:
671 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
672 switch (II->getIntrinsicID()) {
673 default: break;
674 case Intrinsic::bswap: {
675 // If the only bits demanded come from one byte of the bswap result,
676 // just shift the input byte into position to eliminate the bswap.
677 unsigned NLZ = DemandedMask.countLeadingZeros();
678 unsigned NTZ = DemandedMask.countTrailingZeros();
Craig Topper4c947752012-12-22 18:09:02 +0000679
Chris Lattner7e044912010-01-04 07:17:19 +0000680 // Round NTZ down to the next byte. If we have 11 trailing zeros, then
681 // we need all the bits down to bit 8. Likewise, round NLZ. If we
682 // have 14 leading zeros, round to 8.
683 NLZ &= ~7;
684 NTZ &= ~7;
685 // If we need exactly one byte, we can do this transformation.
686 if (BitWidth-NLZ-NTZ == 8) {
687 unsigned ResultBit = NTZ;
688 unsigned InputBit = BitWidth-NTZ-8;
Craig Topper4c947752012-12-22 18:09:02 +0000689
Chris Lattner7e044912010-01-04 07:17:19 +0000690 // Replace this with either a left or right shift to get the byte into
691 // the right place.
692 Instruction *NewVal;
693 if (InputBit > ResultBit)
Gabor Greif79430172010-06-24 12:35:13 +0000694 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000695 ConstantInt::get(I->getType(), InputBit-ResultBit));
696 else
Gabor Greif79430172010-06-24 12:35:13 +0000697 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
Chris Lattner7e044912010-01-04 07:17:19 +0000698 ConstantInt::get(I->getType(), ResultBit-InputBit));
699 NewVal->takeName(I);
Eli Friedman6efb64e2011-05-19 01:20:42 +0000700 return InsertNewInstWith(NewVal, *I);
Chris Lattner7e044912010-01-04 07:17:19 +0000701 }
Craig Topper4c947752012-12-22 18:09:02 +0000702
Chris Lattner7e044912010-01-04 07:17:19 +0000703 // TODO: Could compute known zero/one bits based on the input.
704 break;
705 }
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000706 case Intrinsic::x86_mmx_pmovmskb:
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000707 case Intrinsic::x86_sse_movmsk_ps:
708 case Intrinsic::x86_sse2_movmsk_pd:
709 case Intrinsic::x86_sse2_pmovmskb_128:
710 case Intrinsic::x86_avx_movmsk_ps_256:
711 case Intrinsic::x86_avx_movmsk_pd_256:
712 case Intrinsic::x86_avx2_pmovmskb: {
713 // MOVMSK copies the vector elements' sign bits to the low bits
714 // and zeros the high bits.
Simon Pilgrimfda22d62016-06-04 13:42:46 +0000715 unsigned ArgWidth;
716 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
717 ArgWidth = 8; // Arg is x86_mmx, but treated as <8 x i8>.
718 } else {
719 auto Arg = II->getArgOperand(0);
720 auto ArgType = cast<VectorType>(Arg->getType());
721 ArgWidth = ArgType->getNumElements();
722 }
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000723
724 // If we don't need any of low bits then return zero,
725 // we know that DemandedMask is non-zero already.
726 APInt DemandedElts = DemandedMask.zextOrTrunc(ArgWidth);
727 if (DemandedElts == 0)
728 return ConstantInt::getNullValue(VTy);
729
Ahmed Bougacha17482a52016-04-28 14:36:07 +0000730 // We know that the upper bits are set to zero.
Craig Topper3a86a042017-03-19 05:49:16 +0000731 KnownZero.setBitsFrom(ArgWidth);
Simon Pilgrimbd4a3be2016-04-28 12:22:53 +0000732 return nullptr;
733 }
Chad Rosierb3628842011-05-26 23:13:19 +0000734 case Intrinsic::x86_sse42_crc32_64_64:
Craig Topper3a86a042017-03-19 05:49:16 +0000735 KnownZero.setBitsFrom(32);
Craig Topperf40110f2014-04-25 05:29:35 +0000736 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000737 }
738 }
Hal Finkel60db0582014-09-07 18:57:58 +0000739 computeKnownBits(V, KnownZero, KnownOne, Depth, CxtI);
Chris Lattner7e044912010-01-04 07:17:19 +0000740 break;
741 }
Craig Topper4c947752012-12-22 18:09:02 +0000742
Chris Lattner7e044912010-01-04 07:17:19 +0000743 // If the client is only demanding bits that we know, return the known
744 // constant.
Duncan Sandsc8a3e562010-01-29 06:18:46 +0000745 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
746 return Constant::getIntegerValue(VTy, KnownOne);
Craig Topperf40110f2014-04-25 05:29:35 +0000747 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000748}
749
Craig Topperb0076fe2017-04-12 18:05:21 +0000750/// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
751/// bits. It also tries to handle simplifications that can be done based on
752/// DemandedMask, but without modifying the Instruction.
753Value *InstCombiner::SimplifyMultipleUseDemandedBits(Instruction *I,
754 const APInt &DemandedMask,
755 APInt &KnownZero,
756 APInt &KnownOne,
757 unsigned Depth,
758 Instruction *CxtI) {
759 unsigned BitWidth = DemandedMask.getBitWidth();
760 Type *ITy = I->getType();
761
762 APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
763 APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
764
765 // Despite the fact that we can't simplify this instruction in all User's
766 // context, we can at least compute the knownzero/knownone bits, and we can
767 // do simplifications that apply to *just* the one user if we know that
768 // this instruction has a simpler value in that context.
Craig Topperf35a7f72017-04-12 18:25:25 +0000769 switch (I->getOpcode()) {
770 case Instruction::And:
Craig Topperb0076fe2017-04-12 18:05:21 +0000771 // If either the LHS or the RHS are Zero, the result is zero.
772 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
773 CxtI);
774 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
775 CxtI);
776
777 // If all of the demanded bits are known 1 on one side, return the other.
778 // These bits cannot contribute to the result of the 'and' in this
779 // context.
780 if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
781 (DemandedMask & ~LHSKnownZero))
782 return I->getOperand(0);
783 if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
784 (DemandedMask & ~RHSKnownZero))
785 return I->getOperand(1);
786
787 // If all of the demanded bits in the inputs are known zeros, return zero.
788 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
789 return Constant::getNullValue(ITy);
790
Craig Topperf35a7f72017-04-12 18:25:25 +0000791 break;
792
793 case Instruction::Or:
Craig Topperb0076fe2017-04-12 18:05:21 +0000794 // We can simplify (X|Y) -> X or Y in the user's context if we know that
795 // only bits from X or Y are demanded.
796
797 // If either the LHS or the RHS are One, the result is One.
798 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
799 CxtI);
800 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
801 CxtI);
802
803 // If all of the demanded bits are known zero on one side, return the
804 // other. These bits cannot contribute to the result of the 'or' in this
805 // context.
806 if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
807 (DemandedMask & ~LHSKnownOne))
808 return I->getOperand(0);
809 if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
810 (DemandedMask & ~RHSKnownOne))
811 return I->getOperand(1);
812
813 // If all of the potentially set bits on one side are known to be set on
814 // the other side, just use the 'other' side.
815 if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
816 (DemandedMask & (~RHSKnownZero)))
817 return I->getOperand(0);
818 if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
819 (DemandedMask & (~LHSKnownZero)))
820 return I->getOperand(1);
Craig Topperf35a7f72017-04-12 18:25:25 +0000821
822 break;
823
824 case Instruction::Xor:
Craig Topperb0076fe2017-04-12 18:05:21 +0000825 // We can simplify (X^Y) -> X or Y in the user's context if we know that
826 // only bits from X or Y are demanded.
827
828 computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth + 1,
829 CxtI);
830 computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth + 1,
831 CxtI);
832
833 // If all of the demanded bits are known zero on one side, return the
834 // other.
835 if ((DemandedMask & RHSKnownZero) == DemandedMask)
836 return I->getOperand(0);
837 if ((DemandedMask & LHSKnownZero) == DemandedMask)
838 return I->getOperand(1);
Craig Topperf35a7f72017-04-12 18:25:25 +0000839
840 break;
Craig Topperb0076fe2017-04-12 18:05:21 +0000841 }
842
843 // Compute the KnownZero/KnownOne bits to simplify things downstream.
844 computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI);
Craig Topper9a51c7f2017-04-12 18:17:46 +0000845
846 // If this user is only demanding bits that we know, return the known
847 // constant.
848 if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
849 return Constant::getIntegerValue(ITy, KnownOne);
850
Craig Topperb0076fe2017-04-12 18:05:21 +0000851 return nullptr;
852}
853
854
Shuxin Yang63e999e2012-12-04 00:04:54 +0000855/// Helper routine of SimplifyDemandedUseBits. It tries to simplify
856/// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into
857/// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
858/// of "C2-C1".
859///
860/// Suppose E1 and E2 are generally different in bits S={bm, bm+1,
861/// ..., bn}, without considering the specific value X is holding.
862/// This transformation is legal iff one of following conditions is hold:
863/// 1) All the bit in S are 0, in this case E1 == E2.
864/// 2) We don't care those bits in S, per the input DemandedMask.
865/// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the
866/// rest bits.
867///
868/// Currently we only test condition 2).
869///
870/// As with SimplifyDemandedUseBits, it returns NULL if the simplification was
871/// not successful.
872Value *InstCombiner::SimplifyShrShlDemandedBits(Instruction *Shr,
Benjamin Kramerc321e532016-06-08 19:09:22 +0000873 Instruction *Shl,
874 const APInt &DemandedMask,
875 APInt &KnownZero,
876 APInt &KnownOne) {
Shuxin Yang63e999e2012-12-04 00:04:54 +0000877
Benjamin Kramer010f1082013-08-30 14:35:35 +0000878 const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
879 const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
880 if (!ShlOp1 || !ShrOp1)
Craig Topperf40110f2014-04-25 05:29:35 +0000881 return nullptr; // Noop.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000882
883 Value *VarX = Shr->getOperand(0);
884 Type *Ty = VarX->getType();
885 unsigned BitWidth = Ty->getIntegerBitWidth();
886 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000887 return nullptr; // Undef.
Benjamin Kramer010f1082013-08-30 14:35:35 +0000888
889 unsigned ShlAmt = ShlOp1.getZExtValue();
890 unsigned ShrAmt = ShrOp1.getZExtValue();
Shuxin Yang63e999e2012-12-04 00:04:54 +0000891
892 KnownOne.clearAllBits();
Craig Topper3a86a042017-03-19 05:49:16 +0000893 KnownZero.setLowBits(ShlAmt - 1);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000894 KnownZero &= DemandedMask;
895
Benjamin Kramer010f1082013-08-30 14:35:35 +0000896 APInt BitMask1(APInt::getAllOnesValue(BitWidth));
897 APInt BitMask2(APInt::getAllOnesValue(BitWidth));
Shuxin Yang63e999e2012-12-04 00:04:54 +0000898
899 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
900 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
901 (BitMask1.ashr(ShrAmt) << ShlAmt);
902
903 if (ShrAmt <= ShlAmt) {
904 BitMask2 <<= (ShlAmt - ShrAmt);
905 } else {
906 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
907 BitMask2.ashr(ShrAmt - ShlAmt);
908 }
909
910 // Check if condition-2 (see the comment to this function) is satified.
911 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
912 if (ShrAmt == ShlAmt)
913 return VarX;
914
915 if (!Shr->hasOneUse())
Craig Topperf40110f2014-04-25 05:29:35 +0000916 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000917
918 BinaryOperator *New;
919 if (ShrAmt < ShlAmt) {
920 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
921 New = BinaryOperator::CreateShl(VarX, Amt);
922 BinaryOperator *Orig = cast<BinaryOperator>(Shl);
923 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
924 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
925 } else {
926 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
Shuxin Yang86c0e232012-12-04 03:28:32 +0000927 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
928 BinaryOperator::CreateAShr(VarX, Amt);
Shuxin Yang81b36782012-12-12 00:29:03 +0000929 if (cast<BinaryOperator>(Shr)->isExact())
930 New->setIsExact(true);
Shuxin Yang63e999e2012-12-04 00:04:54 +0000931 }
932
933 return InsertNewInstWith(New, *Shl);
934 }
935
Craig Topperf40110f2014-04-25 05:29:35 +0000936 return nullptr;
Shuxin Yang63e999e2012-12-04 00:04:54 +0000937}
Chris Lattner7e044912010-01-04 07:17:19 +0000938
Sanjay Patelbbbb3ce2016-07-14 20:54:43 +0000939/// The specified value produces a vector with any number of elements.
940/// DemandedElts contains the set of elements that are actually used by the
941/// caller. This method analyzes which elements of the operand are undef and
942/// returns that information in UndefElts.
Chris Lattner7e044912010-01-04 07:17:19 +0000943///
944/// If the information about demanded elements can be used to simplify the
945/// operation, the operation is simplified, then the resultant value is
946/// returned. This returns null if no change was made.
947Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
Chris Lattnerb22423c2010-02-08 23:56:03 +0000948 APInt &UndefElts,
Chris Lattner7e044912010-01-04 07:17:19 +0000949 unsigned Depth) {
Sanjay Patel9190b4a2016-04-29 20:54:56 +0000950 unsigned VWidth = V->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +0000951 APInt EltMask(APInt::getAllOnesValue(VWidth));
952 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
953
954 if (isa<UndefValue>(V)) {
955 // If the entire vector is undefined, just return this info.
956 UndefElts = EltMask;
Craig Topperf40110f2014-04-25 05:29:35 +0000957 return nullptr;
Chris Lattnerb22423c2010-02-08 23:56:03 +0000958 }
Craig Topper4c947752012-12-22 18:09:02 +0000959
Chris Lattnerb22423c2010-02-08 23:56:03 +0000960 if (DemandedElts == 0) { // If nothing is demanded, provide undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000961 UndefElts = EltMask;
962 return UndefValue::get(V->getType());
963 }
964
965 UndefElts = 0;
Craig Topper4c947752012-12-22 18:09:02 +0000966
Chris Lattner67058832012-01-25 06:48:06 +0000967 // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
968 if (Constant *C = dyn_cast<Constant>(V)) {
969 // Check if this is identity. If so, return 0 since we are not simplifying
970 // anything.
971 if (DemandedElts.isAllOnesValue())
Craig Topperf40110f2014-04-25 05:29:35 +0000972 return nullptr;
Chris Lattner67058832012-01-25 06:48:06 +0000973
Chris Lattner229907c2011-07-18 04:54:35 +0000974 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
Chris Lattner7e044912010-01-04 07:17:19 +0000975 Constant *Undef = UndefValue::get(EltTy);
Craig Topper4c947752012-12-22 18:09:02 +0000976
Chris Lattner67058832012-01-25 06:48:06 +0000977 SmallVector<Constant*, 16> Elts;
978 for (unsigned i = 0; i != VWidth; ++i) {
Chris Lattner7e044912010-01-04 07:17:19 +0000979 if (!DemandedElts[i]) { // If not demanded, set to undef.
980 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000981 UndefElts.setBit(i);
Chris Lattner67058832012-01-25 06:48:06 +0000982 continue;
983 }
Craig Topper4c947752012-12-22 18:09:02 +0000984
Chris Lattner67058832012-01-25 06:48:06 +0000985 Constant *Elt = C->getAggregateElement(i);
Craig Topperf40110f2014-04-25 05:29:35 +0000986 if (!Elt) return nullptr;
Craig Topper4c947752012-12-22 18:09:02 +0000987
Chris Lattner67058832012-01-25 06:48:06 +0000988 if (isa<UndefValue>(Elt)) { // Already undef.
Chris Lattner7e044912010-01-04 07:17:19 +0000989 Elts.push_back(Undef);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000990 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +0000991 } else { // Otherwise, defined.
Chris Lattner67058832012-01-25 06:48:06 +0000992 Elts.push_back(Elt);
Chris Lattner7e044912010-01-04 07:17:19 +0000993 }
Chris Lattner67058832012-01-25 06:48:06 +0000994 }
Craig Topper4c947752012-12-22 18:09:02 +0000995
Chris Lattner7e044912010-01-04 07:17:19 +0000996 // If we changed the constant, return it.
Chris Lattner47a86bd2012-01-25 06:02:56 +0000997 Constant *NewCV = ConstantVector::get(Elts);
Craig Topperf40110f2014-04-25 05:29:35 +0000998 return NewCV != C ? NewCV : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +0000999 }
Craig Topper4c947752012-12-22 18:09:02 +00001000
Chris Lattner7e044912010-01-04 07:17:19 +00001001 // Limit search depth.
1002 if (Depth == 10)
Craig Topperf40110f2014-04-25 05:29:35 +00001003 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001004
Stuart Hastings5bd18b62011-05-17 22:13:31 +00001005 // If multiple users are using the root value, proceed with
Chris Lattner7e044912010-01-04 07:17:19 +00001006 // simplification conservatively assuming that all elements
1007 // are needed.
1008 if (!V->hasOneUse()) {
1009 // Quit if we find multiple users of a non-root value though.
1010 // They'll be handled when it's their turn to be visited by
1011 // the main instcombine process.
1012 if (Depth != 0)
1013 // TODO: Just compute the UndefElts information recursively.
Craig Topperf40110f2014-04-25 05:29:35 +00001014 return nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001015
1016 // Conservatively assume that all elements are needed.
1017 DemandedElts = EltMask;
1018 }
Craig Topper4c947752012-12-22 18:09:02 +00001019
Chris Lattner7e044912010-01-04 07:17:19 +00001020 Instruction *I = dyn_cast<Instruction>(V);
Craig Topperf40110f2014-04-25 05:29:35 +00001021 if (!I) return nullptr; // Only analyze instructions.
Craig Topper4c947752012-12-22 18:09:02 +00001022
Chris Lattner7e044912010-01-04 07:17:19 +00001023 bool MadeChange = false;
1024 APInt UndefElts2(VWidth, 0);
Craig Topper23ebd952016-12-11 08:54:52 +00001025 APInt UndefElts3(VWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001026 Value *TmpV;
1027 switch (I->getOpcode()) {
1028 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001029
Chris Lattner7e044912010-01-04 07:17:19 +00001030 case Instruction::InsertElement: {
1031 // If this is a variable index, we don't know which element it overwrites.
1032 // demand exactly the same input as we produce.
1033 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
Craig Topperf40110f2014-04-25 05:29:35 +00001034 if (!Idx) {
Chris Lattner7e044912010-01-04 07:17:19 +00001035 // Note that we can't propagate undef elt info, because we don't know
1036 // which elt is getting updated.
1037 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001038 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001039 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1040 break;
1041 }
Craig Topper4c947752012-12-22 18:09:02 +00001042
Chris Lattner7e044912010-01-04 07:17:19 +00001043 // If this is inserting an element that isn't demanded, remove this
1044 // insertelement.
1045 unsigned IdxNo = Idx->getZExtValue();
1046 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1047 Worklist.Add(I);
1048 return I->getOperand(0);
1049 }
Craig Topper4c947752012-12-22 18:09:02 +00001050
Chris Lattner7e044912010-01-04 07:17:19 +00001051 // Otherwise, the element inserted overwrites whatever was there, so the
1052 // input demanded set is simpler than the output set.
1053 APInt DemandedElts2 = DemandedElts;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001054 DemandedElts2.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001055 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001056 UndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001057 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1058
1059 // The inserted element is defined.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001060 UndefElts.clearBit(IdxNo);
Chris Lattner7e044912010-01-04 07:17:19 +00001061 break;
1062 }
1063 case Instruction::ShuffleVector: {
1064 ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
Craig Topper2e18bcf2016-12-29 04:24:32 +00001065 unsigned LHSVWidth =
1066 Shuffle->getOperand(0)->getType()->getVectorNumElements();
Chris Lattner7e044912010-01-04 07:17:19 +00001067 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1068 for (unsigned i = 0; i < VWidth; i++) {
1069 if (DemandedElts[i]) {
1070 unsigned MaskVal = Shuffle->getMaskValue(i);
1071 if (MaskVal != -1u) {
1072 assert(MaskVal < LHSVWidth * 2 &&
1073 "shufflevector mask index out of range!");
1074 if (MaskVal < LHSVWidth)
Jay Foad25a5e4c2010-12-01 08:53:58 +00001075 LeftDemanded.setBit(MaskVal);
Chris Lattner7e044912010-01-04 07:17:19 +00001076 else
Jay Foad25a5e4c2010-12-01 08:53:58 +00001077 RightDemanded.setBit(MaskVal - LHSVWidth);
Chris Lattner7e044912010-01-04 07:17:19 +00001078 }
1079 }
1080 }
1081
Alexey Bataevfee90782016-09-23 09:14:08 +00001082 APInt LHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001083 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001084 LHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001085 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1086
Alexey Bataevfee90782016-09-23 09:14:08 +00001087 APInt RHSUndefElts(LHSVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001088 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
Alexey Bataevfee90782016-09-23 09:14:08 +00001089 RHSUndefElts, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001090 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1091
1092 bool NewUndefElts = false;
Alexey Bataev793c9462016-09-26 13:18:59 +00001093 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1094 unsigned RHSIdx = -1u, RHSValIdx = -1u;
Alexey Bataevfee90782016-09-23 09:14:08 +00001095 bool LHSUniform = true;
1096 bool RHSUniform = true;
Chris Lattner7e044912010-01-04 07:17:19 +00001097 for (unsigned i = 0; i < VWidth; i++) {
1098 unsigned MaskVal = Shuffle->getMaskValue(i);
1099 if (MaskVal == -1u) {
Jay Foad25a5e4c2010-12-01 08:53:58 +00001100 UndefElts.setBit(i);
Eli Friedman888bea02011-09-15 01:14:29 +00001101 } else if (!DemandedElts[i]) {
1102 NewUndefElts = true;
1103 UndefElts.setBit(i);
Chris Lattner7e044912010-01-04 07:17:19 +00001104 } else if (MaskVal < LHSVWidth) {
Alexey Bataevfee90782016-09-23 09:14:08 +00001105 if (LHSUndefElts[MaskVal]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001106 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001107 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001108 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001109 LHSIdx = LHSIdx == -1u ? i : LHSVWidth;
1110 LHSValIdx = LHSValIdx == -1u ? MaskVal : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001111 LHSUniform = LHSUniform && (MaskVal == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001112 }
1113 } else {
Alexey Bataevfee90782016-09-23 09:14:08 +00001114 if (RHSUndefElts[MaskVal - LHSVWidth]) {
Chris Lattner7e044912010-01-04 07:17:19 +00001115 NewUndefElts = true;
Jay Foad25a5e4c2010-12-01 08:53:58 +00001116 UndefElts.setBit(i);
Alexey Bataevfee90782016-09-23 09:14:08 +00001117 } else {
Alexey Bataev793c9462016-09-26 13:18:59 +00001118 RHSIdx = RHSIdx == -1u ? i : LHSVWidth;
1119 RHSValIdx = RHSValIdx == -1u ? MaskVal - LHSVWidth : LHSVWidth;
Alexey Bataevfee90782016-09-23 09:14:08 +00001120 RHSUniform = RHSUniform && (MaskVal - LHSVWidth == i);
Chris Lattner7e044912010-01-04 07:17:19 +00001121 }
1122 }
1123 }
1124
Alexey Bataevfee90782016-09-23 09:14:08 +00001125 // Try to transform shuffle with constant vector and single element from
1126 // this constant vector to single insertelement instruction.
1127 // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
1128 // insertelement V, C[ci], ci-n
1129 if (LHSVWidth == Shuffle->getType()->getNumElements()) {
1130 Value *Op = nullptr;
1131 Constant *Value = nullptr;
1132 unsigned Idx = -1u;
1133
Craig Topper62f06e22016-12-29 05:38:31 +00001134 // Find constant vector with the single element in shuffle (LHS or RHS).
Alexey Bataevfee90782016-09-23 09:14:08 +00001135 if (LHSIdx < LHSVWidth && RHSUniform) {
1136 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1137 Op = Shuffle->getOperand(1);
Alexey Bataev793c9462016-09-26 13:18:59 +00001138 Value = CV->getOperand(LHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001139 Idx = LHSIdx;
1140 }
1141 }
1142 if (RHSIdx < LHSVWidth && LHSUniform) {
1143 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1144 Op = Shuffle->getOperand(0);
Alexey Bataev793c9462016-09-26 13:18:59 +00001145 Value = CV->getOperand(RHSValIdx);
Alexey Bataevfee90782016-09-23 09:14:08 +00001146 Idx = RHSIdx;
1147 }
1148 }
1149 // Found constant vector with single element - convert to insertelement.
1150 if (Op && Value) {
1151 Instruction *New = InsertElementInst::Create(
1152 Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx),
1153 Shuffle->getName());
1154 InsertNewInstWith(New, *Shuffle);
1155 return New;
1156 }
1157 }
Chris Lattner7e044912010-01-04 07:17:19 +00001158 if (NewUndefElts) {
1159 // Add additional discovered undefs.
Chris Lattner0256be92012-01-27 03:08:05 +00001160 SmallVector<Constant*, 16> Elts;
Chris Lattner7e044912010-01-04 07:17:19 +00001161 for (unsigned i = 0; i < VWidth; ++i) {
1162 if (UndefElts[i])
1163 Elts.push_back(UndefValue::get(Type::getInt32Ty(I->getContext())));
1164 else
1165 Elts.push_back(ConstantInt::get(Type::getInt32Ty(I->getContext()),
1166 Shuffle->getMaskValue(i)));
1167 }
1168 I->setOperand(2, ConstantVector::get(Elts));
1169 MadeChange = true;
1170 }
1171 break;
1172 }
Pete Cooperabc13af2012-07-26 23:10:24 +00001173 case Instruction::Select: {
1174 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1175 if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1176 for (unsigned i = 0; i < VWidth; i++) {
Andrea Di Biagio40f59e42015-10-06 10:34:53 +00001177 Constant *CElt = CV->getAggregateElement(i);
1178 // Method isNullValue always returns false when called on a
1179 // ConstantExpr. If CElt is a ConstantExpr then skip it in order to
1180 // to avoid propagating incorrect information.
1181 if (isa<ConstantExpr>(CElt))
1182 continue;
1183 if (CElt->isNullValue())
Pete Cooperabc13af2012-07-26 23:10:24 +00001184 LeftDemanded.clearBit(i);
1185 else
1186 RightDemanded.clearBit(i);
1187 }
1188 }
1189
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001190 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded, UndefElts,
1191 Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001192 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1193
1194 TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001195 UndefElts2, Depth + 1);
Pete Cooperabc13af2012-07-26 23:10:24 +00001196 if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001197
Pete Cooperabc13af2012-07-26 23:10:24 +00001198 // Output elements are undefined if both are undefined.
1199 UndefElts &= UndefElts2;
1200 break;
1201 }
Chris Lattner7e044912010-01-04 07:17:19 +00001202 case Instruction::BitCast: {
1203 // Vector->vector casts only.
Chris Lattner229907c2011-07-18 04:54:35 +00001204 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
Chris Lattner7e044912010-01-04 07:17:19 +00001205 if (!VTy) break;
1206 unsigned InVWidth = VTy->getNumElements();
1207 APInt InputDemandedElts(InVWidth, 0);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001208 UndefElts2 = APInt(InVWidth, 0);
Chris Lattner7e044912010-01-04 07:17:19 +00001209 unsigned Ratio;
1210
1211 if (VWidth == InVWidth) {
1212 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1213 // elements as are demanded of us.
1214 Ratio = 1;
1215 InputDemandedElts = DemandedElts;
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001216 } else if ((VWidth % InVWidth) == 0) {
1217 // If the number of elements in the output is a multiple of the number of
1218 // elements in the input then an input element is live if any of the
1219 // corresponding output elements are live.
1220 Ratio = VWidth / InVWidth;
1221 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Chris Lattner7e044912010-01-04 07:17:19 +00001222 if (DemandedElts[OutIdx])
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001223 InputDemandedElts.setBit(OutIdx / Ratio);
1224 } else if ((InVWidth % VWidth) == 0) {
1225 // If the number of elements in the input is a multiple of the number of
1226 // elements in the output then an input element is live if the
1227 // corresponding output element is live.
1228 Ratio = InVWidth / VWidth;
Chris Lattner7e044912010-01-04 07:17:19 +00001229 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001230 if (DemandedElts[InIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001231 InputDemandedElts.setBit(InIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001232 } else {
1233 // Unsupported so far.
1234 break;
Chris Lattner7e044912010-01-04 07:17:19 +00001235 }
Craig Topper4c947752012-12-22 18:09:02 +00001236
Chris Lattner7e044912010-01-04 07:17:19 +00001237 // div/rem demand all inputs, because they don't want divide by zero.
1238 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001239 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001240 if (TmpV) {
1241 I->setOperand(0, TmpV);
1242 MadeChange = true;
1243 }
Craig Topper4c947752012-12-22 18:09:02 +00001244
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001245 if (VWidth == InVWidth) {
1246 UndefElts = UndefElts2;
1247 } else if ((VWidth % InVWidth) == 0) {
1248 // If the number of elements in the output is a multiple of the number of
1249 // elements in the input then an output element is undef if the
1250 // corresponding input element is undef.
Chris Lattner7e044912010-01-04 07:17:19 +00001251 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001252 if (UndefElts2[OutIdx / Ratio])
Jay Foad25a5e4c2010-12-01 08:53:58 +00001253 UndefElts.setBit(OutIdx);
Simon Pilgrim43f5e082015-09-29 08:19:11 +00001254 } else if ((InVWidth % VWidth) == 0) {
1255 // If the number of elements in the input is a multiple of the number of
1256 // elements in the output then an output element is undef if all of the
1257 // corresponding input elements are undef.
1258 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1259 APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio);
1260 if (SubUndef.countPopulation() == Ratio)
1261 UndefElts.setBit(OutIdx);
1262 }
1263 } else {
Chris Lattner7e044912010-01-04 07:17:19 +00001264 llvm_unreachable("Unimp");
Chris Lattner7e044912010-01-04 07:17:19 +00001265 }
1266 break;
1267 }
1268 case Instruction::And:
1269 case Instruction::Or:
1270 case Instruction::Xor:
1271 case Instruction::Add:
1272 case Instruction::Sub:
1273 case Instruction::Mul:
1274 // div/rem demand all inputs, because they don't want divide by zero.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001275 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1276 Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001277 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1278 TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001279 UndefElts2, Depth + 1);
Chris Lattner7e044912010-01-04 07:17:19 +00001280 if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
Craig Topper4c947752012-12-22 18:09:02 +00001281
Chris Lattner7e044912010-01-04 07:17:19 +00001282 // Output elements are undefined if both are undefined. Consider things
1283 // like undef&0. The result is known zero, not undef.
1284 UndefElts &= UndefElts2;
1285 break;
Pete Coopere807e452012-07-26 22:37:04 +00001286 case Instruction::FPTrunc:
1287 case Instruction::FPExt:
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001288 TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1289 Depth + 1);
Pete Coopere807e452012-07-26 22:37:04 +00001290 if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1291 break;
Craig Topper4c947752012-12-22 18:09:02 +00001292
Chris Lattner7e044912010-01-04 07:17:19 +00001293 case Instruction::Call: {
1294 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I);
1295 if (!II) break;
1296 switch (II->getIntrinsicID()) {
1297 default: break;
Craig Topper4c947752012-12-22 18:09:02 +00001298
Craig Topper7fc6d342016-12-11 22:32:38 +00001299 case Intrinsic::x86_xop_vfrcz_ss:
1300 case Intrinsic::x86_xop_vfrcz_sd:
1301 // The instructions for these intrinsics are speced to zero upper bits not
1302 // pass them through like other scalar intrinsics. So we shouldn't just
1303 // use Arg0 if DemandedElts[0] is clear like we do for other intrinsics.
1304 // Instead we should return a zero vector.
Craig Topper1a8a3372016-12-29 03:30:17 +00001305 if (!DemandedElts[0]) {
1306 Worklist.Add(II);
Craig Topper7fc6d342016-12-11 22:32:38 +00001307 return ConstantAggregateZero::get(II->getType());
Craig Topper1a8a3372016-12-29 03:30:17 +00001308 }
Craig Topper7fc6d342016-12-11 22:32:38 +00001309
Craig Topperac75bca2016-12-13 07:45:45 +00001310 // Only the lower element is used.
1311 DemandedElts = 1;
Craig Topper7fc6d342016-12-11 22:32:38 +00001312 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1313 UndefElts, Depth + 1);
1314 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperac75bca2016-12-13 07:45:45 +00001315
1316 // Only the lower element is undefined. The high elements are zero.
1317 UndefElts = UndefElts[0];
Craig Topper7fc6d342016-12-11 22:32:38 +00001318 break;
1319
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001320 // Unary scalar-as-vector operations that work column-wise.
Simon Pilgrim83020942016-04-24 18:23:14 +00001321 case Intrinsic::x86_sse_rcp_ss:
1322 case Intrinsic::x86_sse_rsqrt_ss:
1323 case Intrinsic::x86_sse_sqrt_ss:
1324 case Intrinsic::x86_sse2_sqrt_sd:
Simon Pilgrim83020942016-04-24 18:23:14 +00001325 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1326 UndefElts, Depth + 1);
1327 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1328
1329 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001330 if (!DemandedElts[0]) {
1331 Worklist.Add(II);
Simon Pilgrim83020942016-04-24 18:23:14 +00001332 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001333 }
Simon Pilgrim4c564ad2016-04-24 19:31:56 +00001334 // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions
1335 // checks).
Simon Pilgrim83020942016-04-24 18:23:14 +00001336 break;
1337
Craig Toppera0372de2016-12-14 03:17:27 +00001338 // Binary scalar-as-vector operations that work column-wise. The high
1339 // elements come from operand 0. The low element is a function of both
1340 // operands.
Chris Lattner7e044912010-01-04 07:17:19 +00001341 case Intrinsic::x86_sse_min_ss:
1342 case Intrinsic::x86_sse_max_ss:
Simon Pilgrim83020942016-04-24 18:23:14 +00001343 case Intrinsic::x86_sse_cmp_ss:
Chris Lattner7e044912010-01-04 07:17:19 +00001344 case Intrinsic::x86_sse2_min_sd:
1345 case Intrinsic::x86_sse2_max_sd:
Craig Toppera0372de2016-12-14 03:17:27 +00001346 case Intrinsic::x86_sse2_cmp_sd: {
1347 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1348 UndefElts, Depth + 1);
1349 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1350
1351 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001352 if (!DemandedElts[0]) {
1353 Worklist.Add(II);
Craig Toppera0372de2016-12-14 03:17:27 +00001354 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001355 }
Craig Toppera0372de2016-12-14 03:17:27 +00001356
1357 // Only lower element is used for operand 1.
1358 DemandedElts = 1;
1359 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1360 UndefElts2, Depth + 1);
1361 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1362
1363 // Lower element is undefined if both lower elements are undefined.
1364 // Consider things like undef&0. The result is known zero, not undef.
1365 if (!UndefElts2[0])
1366 UndefElts.clearBit(0);
1367
1368 break;
1369 }
1370
Craig Toppereb6a20e2016-12-14 03:17:30 +00001371 // Binary scalar-as-vector operations that work column-wise. The high
1372 // elements come from operand 0 and the low element comes from operand 1.
Simon Pilgrim83020942016-04-24 18:23:14 +00001373 case Intrinsic::x86_sse41_round_ss:
Craig Toppereb6a20e2016-12-14 03:17:30 +00001374 case Intrinsic::x86_sse41_round_sd: {
1375 // Don't use the low element of operand 0.
1376 APInt DemandedElts2 = DemandedElts;
1377 DemandedElts2.clearBit(0);
1378 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts2,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001379 UndefElts, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001380 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001381
1382 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001383 if (!DemandedElts[0]) {
1384 Worklist.Add(II);
Craig Toppereb6a20e2016-12-14 03:17:30 +00001385 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001386 }
Craig Toppereb6a20e2016-12-14 03:17:30 +00001387
1388 // Only lower element is used for operand 1.
1389 DemandedElts = 1;
Gabor Greife23efee2010-06-28 16:45:00 +00001390 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001391 UndefElts2, Depth + 1);
Gabor Greife23efee2010-06-28 16:45:00 +00001392 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
Chris Lattner7e044912010-01-04 07:17:19 +00001393
Craig Toppereb6a20e2016-12-14 03:17:30 +00001394 // Take the high undef elements from operand 0 and take the lower element
1395 // from operand 1.
1396 UndefElts.clearBit(0);
1397 UndefElts |= UndefElts2[0];
Chris Lattner7e044912010-01-04 07:17:19 +00001398 break;
Craig Toppereb6a20e2016-12-14 03:17:30 +00001399 }
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001400
Craig Topperdfd268d2016-12-14 05:43:05 +00001401 // Three input scalar-as-vector operations that work column-wise. The high
1402 // elements come from operand 0 and the low element is a function of all
1403 // three inputs.
Craig Topper268b3ab2016-12-14 06:06:58 +00001404 case Intrinsic::x86_avx512_mask_add_ss_round:
1405 case Intrinsic::x86_avx512_mask_div_ss_round:
1406 case Intrinsic::x86_avx512_mask_mul_ss_round:
1407 case Intrinsic::x86_avx512_mask_sub_ss_round:
1408 case Intrinsic::x86_avx512_mask_max_ss_round:
1409 case Intrinsic::x86_avx512_mask_min_ss_round:
1410 case Intrinsic::x86_avx512_mask_add_sd_round:
1411 case Intrinsic::x86_avx512_mask_div_sd_round:
1412 case Intrinsic::x86_avx512_mask_mul_sd_round:
1413 case Intrinsic::x86_avx512_mask_sub_sd_round:
1414 case Intrinsic::x86_avx512_mask_max_sd_round:
1415 case Intrinsic::x86_avx512_mask_min_sd_round:
Craig Topper23ebd952016-12-11 08:54:52 +00001416 case Intrinsic::x86_fma_vfmadd_ss:
1417 case Intrinsic::x86_fma_vfmsub_ss:
1418 case Intrinsic::x86_fma_vfnmadd_ss:
1419 case Intrinsic::x86_fma_vfnmsub_ss:
1420 case Intrinsic::x86_fma_vfmadd_sd:
1421 case Intrinsic::x86_fma_vfmsub_sd:
1422 case Intrinsic::x86_fma_vfnmadd_sd:
1423 case Intrinsic::x86_fma_vfnmsub_sd:
Craig Topperab5f3552016-12-15 03:49:45 +00001424 case Intrinsic::x86_avx512_mask_vfmadd_ss:
1425 case Intrinsic::x86_avx512_mask_vfmadd_sd:
1426 case Intrinsic::x86_avx512_maskz_vfmadd_ss:
1427 case Intrinsic::x86_avx512_maskz_vfmadd_sd:
Craig Topper23ebd952016-12-11 08:54:52 +00001428 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1429 UndefElts, Depth + 1);
1430 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
Craig Topperdfd268d2016-12-14 05:43:05 +00001431
1432 // If lowest element of a scalar op isn't used then use Arg0.
Craig Topper1a8a3372016-12-29 03:30:17 +00001433 if (!DemandedElts[0]) {
1434 Worklist.Add(II);
Craig Topperdfd268d2016-12-14 05:43:05 +00001435 return II->getArgOperand(0);
Craig Topper1a8a3372016-12-29 03:30:17 +00001436 }
Craig Topperdfd268d2016-12-14 05:43:05 +00001437
1438 // Only lower element is used for operand 1 and 2.
1439 DemandedElts = 1;
Craig Topper23ebd952016-12-11 08:54:52 +00001440 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1441 UndefElts2, Depth + 1);
1442 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1443 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1444 UndefElts3, Depth + 1);
1445 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1446
Craig Topperdfd268d2016-12-14 05:43:05 +00001447 // Lower element is undefined if all three lower elements are undefined.
1448 // Consider things like undef&0. The result is known zero, not undef.
1449 if (!UndefElts2[0] || !UndefElts3[0])
1450 UndefElts.clearBit(0);
Craig Topper23ebd952016-12-11 08:54:52 +00001451
Craig Topper23ebd952016-12-11 08:54:52 +00001452 break;
1453
Craig Topperab5f3552016-12-15 03:49:45 +00001454 case Intrinsic::x86_avx512_mask3_vfmadd_ss:
1455 case Intrinsic::x86_avx512_mask3_vfmadd_sd:
1456 case Intrinsic::x86_avx512_mask3_vfmsub_ss:
1457 case Intrinsic::x86_avx512_mask3_vfmsub_sd:
1458 case Intrinsic::x86_avx512_mask3_vfnmsub_ss:
1459 case Intrinsic::x86_avx512_mask3_vfnmsub_sd:
1460 // These intrinsics get the passthru bits from operand 2.
1461 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(2), DemandedElts,
1462 UndefElts, Depth + 1);
1463 if (TmpV) { II->setArgOperand(2, TmpV); MadeChange = true; }
1464
1465 // If lowest element of a scalar op isn't used then use Arg2.
Craig Topper1a8a3372016-12-29 03:30:17 +00001466 if (!DemandedElts[0]) {
1467 Worklist.Add(II);
Craig Topperab5f3552016-12-15 03:49:45 +00001468 return II->getArgOperand(2);
Craig Topper1a8a3372016-12-29 03:30:17 +00001469 }
Craig Topperab5f3552016-12-15 03:49:45 +00001470
1471 // Only lower element is used for operand 0 and 1.
1472 DemandedElts = 1;
1473 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1474 UndefElts2, Depth + 1);
1475 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1476 TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1477 UndefElts3, Depth + 1);
1478 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1479
1480 // Lower element is undefined if all three lower elements are undefined.
1481 // Consider things like undef&0. The result is known zero, not undef.
1482 if (!UndefElts2[0] || !UndefElts3[0])
1483 UndefElts.clearBit(0);
1484
1485 break;
1486
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001487 case Intrinsic::x86_sse2_pmulu_dq:
1488 case Intrinsic::x86_sse41_pmuldq:
1489 case Intrinsic::x86_avx2_pmul_dq:
Craig Topper72f2d4e2016-12-27 05:30:09 +00001490 case Intrinsic::x86_avx2_pmulu_dq:
1491 case Intrinsic::x86_avx512_pmul_dq_512:
1492 case Intrinsic::x86_avx512_pmulu_dq_512: {
Simon Pilgrimc9cf7fc2016-12-26 23:28:17 +00001493 Value *Op0 = II->getArgOperand(0);
1494 Value *Op1 = II->getArgOperand(1);
1495 unsigned InnerVWidth = Op0->getType()->getVectorNumElements();
1496 assert((VWidth * 2) == InnerVWidth && "Unexpected input size");
1497
1498 APInt InnerDemandedElts(InnerVWidth, 0);
1499 for (unsigned i = 0; i != VWidth; ++i)
1500 if (DemandedElts[i])
1501 InnerDemandedElts.setBit(i * 2);
1502
1503 UndefElts2 = APInt(InnerVWidth, 0);
1504 TmpV = SimplifyDemandedVectorElts(Op0, InnerDemandedElts, UndefElts2,
1505 Depth + 1);
1506 if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1507
1508 UndefElts3 = APInt(InnerVWidth, 0);
1509 TmpV = SimplifyDemandedVectorElts(Op1, InnerDemandedElts, UndefElts3,
1510 Depth + 1);
1511 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1512
1513 break;
1514 }
1515
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001516 case Intrinsic::x86_sse2_packssdw_128:
1517 case Intrinsic::x86_sse2_packsswb_128:
1518 case Intrinsic::x86_sse2_packuswb_128:
1519 case Intrinsic::x86_sse41_packusdw:
1520 case Intrinsic::x86_avx2_packssdw:
1521 case Intrinsic::x86_avx2_packsswb:
1522 case Intrinsic::x86_avx2_packusdw:
Craig Topper3731f4d2017-02-16 07:35:23 +00001523 case Intrinsic::x86_avx2_packuswb:
1524 case Intrinsic::x86_avx512_packssdw_512:
1525 case Intrinsic::x86_avx512_packsswb_512:
1526 case Intrinsic::x86_avx512_packusdw_512:
1527 case Intrinsic::x86_avx512_packuswb_512: {
Simon Pilgrim51b3b982017-01-20 09:28:21 +00001528 auto *Ty0 = II->getArgOperand(0)->getType();
1529 unsigned InnerVWidth = Ty0->getVectorNumElements();
1530 assert(VWidth == (InnerVWidth * 2) && "Unexpected input size");
1531
1532 unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128;
1533 unsigned VWidthPerLane = VWidth / NumLanes;
1534 unsigned InnerVWidthPerLane = InnerVWidth / NumLanes;
1535
1536 // Per lane, pack the elements of the first input and then the second.
1537 // e.g.
1538 // v8i16 PACK(v4i32 X, v4i32 Y) - (X[0..3],Y[0..3])
1539 // v32i8 PACK(v16i16 X, v16i16 Y) - (X[0..7],Y[0..7]),(X[8..15],Y[8..15])
1540 for (int OpNum = 0; OpNum != 2; ++OpNum) {
1541 APInt OpDemandedElts(InnerVWidth, 0);
1542 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1543 unsigned LaneIdx = Lane * VWidthPerLane;
1544 for (unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) {
1545 unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum;
1546 if (DemandedElts[Idx])
1547 OpDemandedElts.setBit((Lane * InnerVWidthPerLane) + Elt);
1548 }
1549 }
1550
1551 // Demand elements from the operand.
1552 auto *Op = II->getArgOperand(OpNum);
1553 APInt OpUndefElts(InnerVWidth, 0);
1554 TmpV = SimplifyDemandedVectorElts(Op, OpDemandedElts, OpUndefElts,
1555 Depth + 1);
1556 if (TmpV) {
1557 II->setArgOperand(OpNum, TmpV);
1558 MadeChange = true;
1559 }
1560
1561 // Pack the operand's UNDEF elements, one lane at a time.
1562 OpUndefElts = OpUndefElts.zext(VWidth);
1563 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1564 APInt LaneElts = OpUndefElts.lshr(InnerVWidthPerLane * Lane);
1565 LaneElts = LaneElts.getLoBits(InnerVWidthPerLane);
1566 LaneElts = LaneElts.shl(InnerVWidthPerLane * (2 * Lane + OpNum));
1567 UndefElts |= LaneElts;
1568 }
1569 }
1570 break;
1571 }
1572
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001573 // PSHUFB
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001574 case Intrinsic::x86_ssse3_pshuf_b_128:
1575 case Intrinsic::x86_avx2_pshuf_b:
Simon Pilgrimd4eb8002017-01-17 11:35:03 +00001576 case Intrinsic::x86_avx512_pshuf_b_512:
1577 // PERMILVAR
1578 case Intrinsic::x86_avx_vpermilvar_ps:
1579 case Intrinsic::x86_avx_vpermilvar_ps_256:
1580 case Intrinsic::x86_avx512_vpermilvar_ps_512:
1581 case Intrinsic::x86_avx_vpermilvar_pd:
1582 case Intrinsic::x86_avx_vpermilvar_pd_256:
Simon Pilgrimfe2c0ed2017-01-18 14:47:49 +00001583 case Intrinsic::x86_avx512_vpermilvar_pd_512:
1584 // PERMV
1585 case Intrinsic::x86_avx2_permd:
1586 case Intrinsic::x86_avx2_permps: {
Simon Pilgrim73a68c22017-01-16 11:30:41 +00001587 Value *Op1 = II->getArgOperand(1);
1588 TmpV = SimplifyDemandedVectorElts(Op1, DemandedElts, UndefElts,
1589 Depth + 1);
1590 if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1591 break;
1592 }
1593
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001594 // SSE4A instructions leave the upper 64-bits of the 128-bit result
1595 // in an undefined state.
1596 case Intrinsic::x86_sse4a_extrq:
1597 case Intrinsic::x86_sse4a_extrqi:
1598 case Intrinsic::x86_sse4a_insertq:
1599 case Intrinsic::x86_sse4a_insertqi:
Craig Topper3a86a042017-03-19 05:49:16 +00001600 UndefElts.setHighBits(VWidth / 2);
Simon Pilgrim61116dd2015-09-17 20:32:45 +00001601 break;
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001602 case Intrinsic::amdgcn_buffer_load:
1603 case Intrinsic::amdgcn_buffer_load_format: {
Craig Topperd33ee1b2017-04-03 16:34:59 +00001604 if (VWidth == 1 || !DemandedElts.isMask())
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001605 return nullptr;
1606
1607 // TODO: Handle 3 vectors when supported in code gen.
1608 unsigned NewNumElts = PowerOf2Ceil(DemandedElts.countTrailingOnes());
1609 if (NewNumElts == VWidth)
1610 return nullptr;
1611
1612 Module *M = II->getParent()->getParent()->getParent();
1613 Type *EltTy = V->getType()->getVectorElementType();
1614
1615 Type *NewTy = (NewNumElts == 1) ? EltTy :
1616 VectorType::get(EltTy, NewNumElts);
1617
1618 Function *NewIntrin = Intrinsic::getDeclaration(M, II->getIntrinsicID(),
1619 NewTy);
1620
1621 SmallVector<Value *, 5> Args;
1622 for (unsigned I = 0, E = II->getNumArgOperands(); I != E; ++I)
1623 Args.push_back(II->getArgOperand(I));
1624
Matt Arsenaulta3bdd8f2017-03-10 05:25:49 +00001625 IRBuilderBase::InsertPointGuard Guard(*Builder);
1626 Builder->SetInsertPoint(II);
1627
Matt Arsenaultefe949c2017-03-09 20:34:27 +00001628 CallInst *NewCall = Builder->CreateCall(NewIntrin, Args);
1629 NewCall->takeName(II);
1630 NewCall->copyMetadata(*II);
1631 if (NewNumElts == 1) {
1632 return Builder->CreateInsertElement(UndefValue::get(V->getType()),
1633 NewCall, static_cast<uint64_t>(0));
1634 }
1635
1636 SmallVector<uint32_t, 8> EltMask;
1637 for (unsigned I = 0; I < VWidth; ++I)
1638 EltMask.push_back(I);
1639
1640 Value *Shuffle = Builder->CreateShuffleVector(
1641 NewCall, UndefValue::get(NewTy), EltMask);
1642
1643 MadeChange = true;
1644 return Shuffle;
1645 }
Chris Lattner7e044912010-01-04 07:17:19 +00001646 }
1647 break;
1648 }
1649 }
Craig Topperf40110f2014-04-25 05:29:35 +00001650 return MadeChange ? I : nullptr;
Chris Lattner7e044912010-01-04 07:17:19 +00001651}