llvm.org GIT mirror llvm / 49c14f5
InstSimplify: Exact shifts of X by Y are X if X has the lsb set Exact shifts may not shift out any non-zero bits. Use computeKnownBits to determine when this occurs and just return the left hand side. This fixes PR21477. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221325 91177308-0d34-0410-b5e6-96231b3b80d8 David Majnemer 5 years ago
2 changed file(s) with 45 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
13271327 return nullptr;
13281328 }
13291329
1330 /// \brief Given operands for an Shl, LShr or AShr, see if we can
1331 /// fold the result. If not, this returns null.
1332 static Value *SimplifyRightShift(unsigned Opcode, Value *Op0, Value *Op1,
1333 bool isExact, const Query &Q,
1334 unsigned MaxRecurse) {
1335 if (Value *V = SimplifyShift(Opcode, Op0, Op1, Q, MaxRecurse))
1336 return V;
1337
1338 // X >> X -> 0
1339 if (Op0 == Op1)
1340 return Constant::getNullValue(Op0->getType());
1341
1342 // The low bit cannot be shifted out of an exact shift if it is set.
1343 if (isExact) {
1344 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
1345 APInt Op0KnownZero(BitWidth, 0);
1346 APInt Op0KnownOne(BitWidth, 0);
1347 computeKnownBits(Op0, Op0KnownZero, Op0KnownOne, Q.DL, /*Depth=*/0, Q.AT, Q.CxtI,
1348 Q.DT);
1349 if (Op0KnownOne[0])
1350 return Op0;
1351 }
1352
1353 return nullptr;
1354 }
1355
13301356 /// SimplifyShlInst - Given operands for an Shl, see if we can
13311357 /// fold the result. If not, this returns null.
13321358 static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
13571383 /// fold the result. If not, this returns null.
13581384 static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
13591385 const Query &Q, unsigned MaxRecurse) {
1360 if (Value *V = SimplifyShift(Instruction::LShr, Op0, Op1, Q, MaxRecurse))
1361 return V;
1362
1363 // X >> X -> 0
1364 if (Op0 == Op1)
1365 return Constant::getNullValue(Op0->getType());
1386 if (Value *V = SimplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q,
1387 MaxRecurse))
1388 return V;
13661389
13671390 // undef >>l X -> 0
13681391 if (match(Op0, m_Undef()))
13901413 /// fold the result. If not, this returns null.
13911414 static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
13921415 const Query &Q, unsigned MaxRecurse) {
1393 if (Value *V = SimplifyShift(Instruction::AShr, Op0, Op1, Q, MaxRecurse))
1416 if (Value *V = SimplifyRightShift(Instruction::AShr, Op0, Op1, isExact, Q,
1417 MaxRecurse))
13941418 return V;
1395
1396 // X >> X -> 0
1397 if (Op0 == Op1)
1398 return Constant::getNullValue(Op0->getType());
13991419
14001420 // all ones >>a X -> all ones
14011421 if (match(Op0, m_AllOnes()))
329329 %cmp = icmp ne i8 %shr, 30
330330 ret i1 %cmp
331331 }
332
333 ; CHECK-LABEL: @exact_lshr_lowbit
334 ; CHECK-NEXT: ret i32 7
335 define i32 @exact_lshr_lowbit(i32 %shiftval) {
336 %shr = lshr exact i32 7, %shiftval
337 ret i32 %shr
338 }
339
340 ; CHECK-LABEL: @exact_ashr_lowbit
341 ; CHECK-NEXT: ret i32 7
342 define i32 @exact_ashr_lowbit(i32 %shiftval) {
343 %shr = ashr exact i32 7, %shiftval
344 ret i32 %shr
345 }