2010-01-05 15:44:46 +08:00
|
|
|
//===- InstCombineShifts.cpp ----------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-01-05 15:44:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the visitShl, visitLShr, and visitAShr functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-01-22 13:25:13 +08:00
|
|
|
#include "InstCombineInternal.h"
|
2011-07-21 05:57:23 +08:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2011-01-14 08:37:45 +08:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2014-03-04 19:08:18 +08:00
|
|
|
#include "llvm/IR/PatternMatch.h"
|
2010-01-05 15:44:46 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace PatternMatch;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "instcombine"
|
|
|
|
|
2019-06-29 19:51:50 +08:00
|
|
|
// Given pattern:
|
|
|
|
// (x shiftopcode Q) shiftopcode K
|
|
|
|
// we should rewrite it as
|
|
|
|
// x shiftopcode (Q+K) iff (Q+K) u< bitwidth(x)
|
|
|
|
// This is valid for any shift, but they must be identical.
|
|
|
|
static Instruction *
|
|
|
|
reassociateShiftAmtsOfTwoSameDirectionShifts(BinaryOperator *Sh0,
|
2019-08-07 01:03:40 +08:00
|
|
|
const SimplifyQuery &SQ,
|
|
|
|
InstCombiner::BuilderTy &Builder) {
|
|
|
|
// Look for a shift of some instruction, ignore zext of shift amount if any.
|
|
|
|
Instruction *Sh0Op0;
|
|
|
|
Value *ShAmt0;
|
|
|
|
if (!match(Sh0,
|
|
|
|
m_Shift(m_Instruction(Sh0Op0), m_ZExtOrSelf(m_Value(ShAmt0)))))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// If there is a truncation between the two shifts, we must make note of it
|
|
|
|
// and look through it. The truncation imposes additional constraints on the
|
|
|
|
// transform.
|
2019-07-02 20:54:48 +08:00
|
|
|
Instruction *Sh1;
|
2019-08-07 01:03:40 +08:00
|
|
|
Value *Trunc = nullptr;
|
|
|
|
match(Sh0Op0,
|
|
|
|
m_CombineOr(m_CombineAnd(m_Trunc(m_Instruction(Sh1)), m_Value(Trunc)),
|
|
|
|
m_Instruction(Sh1)));
|
|
|
|
|
|
|
|
// Inner shift: (x shiftopcode ShAmt1)
|
|
|
|
Value *X, *ShAmt1;
|
|
|
|
if (!match(Sh1, m_Shift(m_Value(X), m_ZExtOrSelf(m_Value(ShAmt1)))))
|
2019-06-29 19:51:50 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// The shift opcodes must be identical.
|
|
|
|
Instruction::BinaryOps ShiftOpcode = Sh0->getOpcode();
|
|
|
|
if (ShiftOpcode != Sh1->getOpcode())
|
|
|
|
return nullptr;
|
2019-08-07 01:03:40 +08:00
|
|
|
|
|
|
|
// Did we match a pattern with truncation ?
|
|
|
|
if (Trunc) {
|
|
|
|
// For right-shifts we can't do any such simplifications. Leave as-is.
|
|
|
|
if (ShiftOpcode != Instruction::BinaryOps::Shl)
|
|
|
|
return nullptr; // FIXME: still could perform constant-folding.
|
|
|
|
// If we saw truncation, we'll need to produce extra instruction,
|
|
|
|
// and for that one of the operands of the shift must be one-use.
|
|
|
|
if (!match(Sh0, m_c_BinOp(m_OneUse(m_Value()), m_Value())))
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:51:50 +08:00
|
|
|
// Can we fold (ShAmt0+ShAmt1) ?
|
2019-08-07 01:03:40 +08:00
|
|
|
auto *NewShAmt = dyn_cast_or_null<Constant>(
|
|
|
|
SimplifyAddInst(ShAmt0, ShAmt1, /*isNSW=*/false, /*isNUW=*/false,
|
|
|
|
SQ.getWithInstruction(Sh0)));
|
2019-06-29 19:51:50 +08:00
|
|
|
if (!NewShAmt)
|
|
|
|
return nullptr; // Did not simplify.
|
2019-08-07 01:03:40 +08:00
|
|
|
// Is the new shift amount smaller than the bit width of inner shift?
|
|
|
|
if (!match(NewShAmt, m_SpecificInt_ICMP(
|
|
|
|
ICmpInst::Predicate::ICMP_ULT,
|
|
|
|
APInt(NewShAmt->getType()->getScalarSizeInBits(),
|
|
|
|
X->getType()->getScalarSizeInBits()))))
|
|
|
|
return nullptr; // FIXME: could perform constant-folding.
|
|
|
|
|
2019-06-29 19:51:50 +08:00
|
|
|
// All good, we can do this fold.
|
2019-08-07 01:03:40 +08:00
|
|
|
NewShAmt = ConstantExpr::getZExtOrBitCast(NewShAmt, X->getType());
|
|
|
|
|
2019-06-29 19:51:50 +08:00
|
|
|
BinaryOperator *NewShift = BinaryOperator::Create(ShiftOpcode, X, NewShAmt);
|
2019-08-07 01:03:40 +08:00
|
|
|
|
|
|
|
// The flags can only be propagated if there wasn't a trunc.
|
|
|
|
if (!Trunc) {
|
|
|
|
// If the pattern did not involve trunc, and both of the original shifts
|
|
|
|
// had the same flag set, preserve the flag.
|
|
|
|
if (ShiftOpcode == Instruction::BinaryOps::Shl) {
|
|
|
|
NewShift->setHasNoUnsignedWrap(Sh0->hasNoUnsignedWrap() &&
|
|
|
|
Sh1->hasNoUnsignedWrap());
|
|
|
|
NewShift->setHasNoSignedWrap(Sh0->hasNoSignedWrap() &&
|
|
|
|
Sh1->hasNoSignedWrap());
|
|
|
|
} else {
|
|
|
|
NewShift->setIsExact(Sh0->isExact() && Sh1->isExact());
|
|
|
|
}
|
2019-06-29 19:51:50 +08:00
|
|
|
}
|
2019-08-07 01:03:40 +08:00
|
|
|
|
|
|
|
Instruction *Ret = NewShift;
|
|
|
|
if (Trunc) {
|
|
|
|
Builder.Insert(NewShift);
|
|
|
|
Ret = CastInst::Create(Instruction::Trunc, NewShift, Sh0->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ret;
|
2019-06-29 19:51:50 +08:00
|
|
|
}
|
|
|
|
|
[InstCombine] Dropping redundant masking before left-shift [0/5] (PR42563)
Summary:
If we have some pattern that leaves only some low bits set, and then performs
left-shift of those bits, if none of the bits that are left after the final
shift are modified by the mask, we can omit the mask.
There are many variants to this pattern:
a. `(x & ((1 << MaskShAmt) - 1)) << ShiftShAmt`
All these patterns can be simplified to just:
`x << ShiftShAmt`
iff:
a. `(MaskShAmt+ShiftShAmt) u>= bitwidth(x)`
alive proof:
a: https://rise4fun.com/Alive/wi9
Indeed, not all of these patterns are canonical.
But since this fold will only produce a single instruction
i'm really interested in handling even uncanonical patterns,
since i have this general kind of pattern in hotpaths,
and it is not totally outlandish for bit-twiddling code.
For now let's start with patterns where both shift amounts are variable,
with trivial constant "offset" between them, since i believe this is
both simplest to handle and i think this is most common.
But again, there are likely other variants where we could use
ValueTracking/ConstantRange to handle more cases.
https://bugs.llvm.org/show_bug.cgi?id=42563
Reviewers: spatel, nikic, huihuiz, xbolva00
Reviewed By: xbolva00
Subscribers: efriedma, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64512
llvm-svn: 366535
2019-07-19 16:25:43 +08:00
|
|
|
// If we have some pattern that leaves only some low bits set, and then performs
|
|
|
|
// left-shift of those bits, if none of the bits that are left after the final
|
|
|
|
// shift are modified by the mask, we can omit the mask.
|
|
|
|
//
|
|
|
|
// There are many variants to this pattern:
|
|
|
|
// a) (x & ((1 << MaskShAmt) - 1)) << ShiftShAmt
|
2019-07-19 16:26:13 +08:00
|
|
|
// b) (x & (~(-1 << MaskShAmt))) << ShiftShAmt
|
2019-07-19 16:26:25 +08:00
|
|
|
// c) (x & (-1 >> MaskShAmt)) << ShiftShAmt
|
2019-07-19 16:26:37 +08:00
|
|
|
// d) (x & ((-1 << MaskShAmt) >> MaskShAmt)) << ShiftShAmt
|
2019-07-19 16:26:47 +08:00
|
|
|
// e) ((x << MaskShAmt) l>> MaskShAmt) << ShiftShAmt
|
2019-07-19 16:26:58 +08:00
|
|
|
// f) ((x << MaskShAmt) a>> MaskShAmt) << ShiftShAmt
|
[InstCombine] Dropping redundant masking before left-shift [0/5] (PR42563)
Summary:
If we have some pattern that leaves only some low bits set, and then performs
left-shift of those bits, if none of the bits that are left after the final
shift are modified by the mask, we can omit the mask.
There are many variants to this pattern:
a. `(x & ((1 << MaskShAmt) - 1)) << ShiftShAmt`
All these patterns can be simplified to just:
`x << ShiftShAmt`
iff:
a. `(MaskShAmt+ShiftShAmt) u>= bitwidth(x)`
alive proof:
a: https://rise4fun.com/Alive/wi9
Indeed, not all of these patterns are canonical.
But since this fold will only produce a single instruction
i'm really interested in handling even uncanonical patterns,
since i have this general kind of pattern in hotpaths,
and it is not totally outlandish for bit-twiddling code.
For now let's start with patterns where both shift amounts are variable,
with trivial constant "offset" between them, since i believe this is
both simplest to handle and i think this is most common.
But again, there are likely other variants where we could use
ValueTracking/ConstantRange to handle more cases.
https://bugs.llvm.org/show_bug.cgi?id=42563
Reviewers: spatel, nikic, huihuiz, xbolva00
Reviewed By: xbolva00
Subscribers: efriedma, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64512
llvm-svn: 366535
2019-07-19 16:25:43 +08:00
|
|
|
// All these patterns can be simplified to just:
|
|
|
|
// x << ShiftShAmt
|
|
|
|
// iff:
|
2019-07-19 16:26:58 +08:00
|
|
|
// a,b) (MaskShAmt+ShiftShAmt) u>= bitwidth(x)
|
|
|
|
// c,d,e,f) (ShiftShAmt-MaskShAmt) s>= 0 (i.e. ShiftShAmt u>= MaskShAmt)
|
[InstCombine] Dropping redundant masking before left-shift [0/5] (PR42563)
Summary:
If we have some pattern that leaves only some low bits set, and then performs
left-shift of those bits, if none of the bits that are left after the final
shift are modified by the mask, we can omit the mask.
There are many variants to this pattern:
a. `(x & ((1 << MaskShAmt) - 1)) << ShiftShAmt`
All these patterns can be simplified to just:
`x << ShiftShAmt`
iff:
a. `(MaskShAmt+ShiftShAmt) u>= bitwidth(x)`
alive proof:
a: https://rise4fun.com/Alive/wi9
Indeed, not all of these patterns are canonical.
But since this fold will only produce a single instruction
i'm really interested in handling even uncanonical patterns,
since i have this general kind of pattern in hotpaths,
and it is not totally outlandish for bit-twiddling code.
For now let's start with patterns where both shift amounts are variable,
with trivial constant "offset" between them, since i believe this is
both simplest to handle and i think this is most common.
But again, there are likely other variants where we could use
ValueTracking/ConstantRange to handle more cases.
https://bugs.llvm.org/show_bug.cgi?id=42563
Reviewers: spatel, nikic, huihuiz, xbolva00
Reviewed By: xbolva00
Subscribers: efriedma, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64512
llvm-svn: 366535
2019-07-19 16:25:43 +08:00
|
|
|
static Instruction *
|
|
|
|
dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
|
|
|
|
const SimplifyQuery &SQ) {
|
|
|
|
assert(OuterShift->getOpcode() == Instruction::BinaryOps::Shl &&
|
|
|
|
"The input must be 'shl'!");
|
|
|
|
|
|
|
|
Value *Masked = OuterShift->getOperand(0);
|
|
|
|
Value *ShiftShAmt = OuterShift->getOperand(1);
|
|
|
|
|
|
|
|
Value *MaskShAmt;
|
|
|
|
|
|
|
|
// ((1 << MaskShAmt) - 1)
|
|
|
|
auto MaskA = m_Add(m_Shl(m_One(), m_Value(MaskShAmt)), m_AllOnes());
|
2019-07-19 16:26:13 +08:00
|
|
|
// (~(-1 << maskNbits))
|
|
|
|
auto MaskB = m_Xor(m_Shl(m_AllOnes(), m_Value(MaskShAmt)), m_AllOnes());
|
2019-07-19 16:26:25 +08:00
|
|
|
// (-1 >> MaskShAmt)
|
|
|
|
auto MaskC = m_Shr(m_AllOnes(), m_Value(MaskShAmt));
|
2019-07-19 16:26:37 +08:00
|
|
|
// ((-1 << MaskShAmt) >> MaskShAmt)
|
|
|
|
auto MaskD =
|
|
|
|
m_Shr(m_Shl(m_AllOnes(), m_Value(MaskShAmt)), m_Deferred(MaskShAmt));
|
[InstCombine] Dropping redundant masking before left-shift [0/5] (PR42563)
Summary:
If we have some pattern that leaves only some low bits set, and then performs
left-shift of those bits, if none of the bits that are left after the final
shift are modified by the mask, we can omit the mask.
There are many variants to this pattern:
a. `(x & ((1 << MaskShAmt) - 1)) << ShiftShAmt`
All these patterns can be simplified to just:
`x << ShiftShAmt`
iff:
a. `(MaskShAmt+ShiftShAmt) u>= bitwidth(x)`
alive proof:
a: https://rise4fun.com/Alive/wi9
Indeed, not all of these patterns are canonical.
But since this fold will only produce a single instruction
i'm really interested in handling even uncanonical patterns,
since i have this general kind of pattern in hotpaths,
and it is not totally outlandish for bit-twiddling code.
For now let's start with patterns where both shift amounts are variable,
with trivial constant "offset" between them, since i believe this is
both simplest to handle and i think this is most common.
But again, there are likely other variants where we could use
ValueTracking/ConstantRange to handle more cases.
https://bugs.llvm.org/show_bug.cgi?id=42563
Reviewers: spatel, nikic, huihuiz, xbolva00
Reviewed By: xbolva00
Subscribers: efriedma, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64512
llvm-svn: 366535
2019-07-19 16:25:43 +08:00
|
|
|
|
|
|
|
Value *X;
|
2019-07-19 16:26:25 +08:00
|
|
|
if (match(Masked, m_c_And(m_CombineOr(MaskA, MaskB), m_Value(X)))) {
|
|
|
|
// Can we simplify (MaskShAmt+ShiftShAmt) ?
|
|
|
|
Value *SumOfShAmts =
|
|
|
|
SimplifyAddInst(MaskShAmt, ShiftShAmt, /*IsNSW=*/false, /*IsNUW=*/false,
|
|
|
|
SQ.getWithInstruction(OuterShift));
|
|
|
|
if (!SumOfShAmts)
|
|
|
|
return nullptr; // Did not simplify.
|
|
|
|
// Is the total shift amount *not* smaller than the bit width?
|
|
|
|
// FIXME: could also rely on ConstantRange.
|
|
|
|
unsigned BitWidth = X->getType()->getScalarSizeInBits();
|
|
|
|
if (!match(SumOfShAmts, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE,
|
|
|
|
APInt(BitWidth, BitWidth))))
|
|
|
|
return nullptr;
|
|
|
|
// All good, we can do this fold.
|
2019-07-19 16:26:47 +08:00
|
|
|
} else if (match(Masked, m_c_And(m_CombineOr(MaskC, MaskD), m_Value(X))) ||
|
2019-07-19 16:26:58 +08:00
|
|
|
match(Masked, m_Shr(m_Shl(m_Value(X), m_Value(MaskShAmt)),
|
|
|
|
m_Deferred(MaskShAmt)))) {
|
2019-07-19 16:26:25 +08:00
|
|
|
// Can we simplify (ShiftShAmt-MaskShAmt) ?
|
|
|
|
Value *ShAmtsDiff =
|
|
|
|
SimplifySubInst(ShiftShAmt, MaskShAmt, /*IsNSW=*/false, /*IsNUW=*/false,
|
|
|
|
SQ.getWithInstruction(OuterShift));
|
|
|
|
if (!ShAmtsDiff)
|
|
|
|
return nullptr; // Did not simplify.
|
|
|
|
// Is the difference non-negative? (is ShiftShAmt u>= MaskShAmt ?)
|
|
|
|
// FIXME: could also rely on ConstantRange.
|
|
|
|
if (!match(ShAmtsDiff, m_NonNegative()))
|
|
|
|
return nullptr;
|
|
|
|
// All good, we can do this fold.
|
|
|
|
} else
|
|
|
|
return nullptr; // Don't know anything about this pattern.
|
[InstCombine] Dropping redundant masking before left-shift [0/5] (PR42563)
Summary:
If we have some pattern that leaves only some low bits set, and then performs
left-shift of those bits, if none of the bits that are left after the final
shift are modified by the mask, we can omit the mask.
There are many variants to this pattern:
a. `(x & ((1 << MaskShAmt) - 1)) << ShiftShAmt`
All these patterns can be simplified to just:
`x << ShiftShAmt`
iff:
a. `(MaskShAmt+ShiftShAmt) u>= bitwidth(x)`
alive proof:
a: https://rise4fun.com/Alive/wi9
Indeed, not all of these patterns are canonical.
But since this fold will only produce a single instruction
i'm really interested in handling even uncanonical patterns,
since i have this general kind of pattern in hotpaths,
and it is not totally outlandish for bit-twiddling code.
For now let's start with patterns where both shift amounts are variable,
with trivial constant "offset" between them, since i believe this is
both simplest to handle and i think this is most common.
But again, there are likely other variants where we could use
ValueTracking/ConstantRange to handle more cases.
https://bugs.llvm.org/show_bug.cgi?id=42563
Reviewers: spatel, nikic, huihuiz, xbolva00
Reviewed By: xbolva00
Subscribers: efriedma, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64512
llvm-svn: 366535
2019-07-19 16:25:43 +08:00
|
|
|
|
|
|
|
// No 'NUW'/'NSW'!
|
|
|
|
// We no longer know that we won't shift-out non-0 bits.
|
|
|
|
return BinaryOperator::Create(OuterShift->getOpcode(), X, ShiftShAmt);
|
|
|
|
}
|
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
|
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2017-01-14 02:08:25 +08:00
|
|
|
assert(Op0->getType() == Op1->getType());
|
2010-01-05 15:44:46 +08:00
|
|
|
|
|
|
|
// See if we can fold away this shift.
|
|
|
|
if (SimplifyDemandedInstructionBits(I))
|
|
|
|
return &I;
|
|
|
|
|
|
|
|
// Try to fold constant and into select arguments.
|
|
|
|
if (isa<Constant>(Op0))
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
|
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI))
|
|
|
|
return R;
|
|
|
|
|
2014-04-15 05:50:37 +08:00
|
|
|
if (Constant *CUI = dyn_cast<Constant>(Op1))
|
2010-01-05 15:44:46 +08:00
|
|
|
if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
|
|
|
|
return Res;
|
2010-11-24 02:52:42 +08:00
|
|
|
|
2019-06-29 19:51:50 +08:00
|
|
|
if (Instruction *NewShift =
|
2019-08-07 01:03:40 +08:00
|
|
|
reassociateShiftAmtsOfTwoSameDirectionShifts(&I, SQ, Builder))
|
2019-06-29 19:51:50 +08:00
|
|
|
return NewShift;
|
|
|
|
|
2016-11-01 23:40:30 +08:00
|
|
|
// (C1 shift (A add C2)) -> (C1 shift C2) shift A)
|
|
|
|
// iff A and C2 are both positive.
|
|
|
|
Value *A;
|
|
|
|
Constant *C;
|
|
|
|
if (match(Op0, m_Constant()) && match(Op1, m_Add(m_Value(A), m_Constant(C))))
|
2017-05-27 02:23:57 +08:00
|
|
|
if (isKnownNonNegative(A, DL, 0, &AC, &I, &DT) &&
|
|
|
|
isKnownNonNegative(C, DL, 0, &AC, &I, &DT))
|
2016-11-01 23:40:30 +08:00
|
|
|
return BinaryOperator::Create(
|
2017-07-08 07:16:26 +08:00
|
|
|
I.getOpcode(), Builder.CreateBinOp(I.getOpcode(), Op0, C), A);
|
2016-11-01 23:40:30 +08:00
|
|
|
|
2012-09-27 18:14:43 +08:00
|
|
|
// X shift (A srem B) -> X shift (A and B-1) iff B is a power of 2.
|
2011-02-10 13:36:31 +08:00
|
|
|
// Because shifts by negative values (which could occur if A were negative)
|
|
|
|
// are undefined.
|
2016-11-01 23:40:30 +08:00
|
|
|
const APInt *B;
|
2011-02-10 13:36:31 +08:00
|
|
|
if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Power2(B)))) {
|
|
|
|
// FIXME: Should this get moved into SimplifyDemandedBits by saying we don't
|
|
|
|
// demand the sign bit (and many others) here??
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *Rem = Builder.CreateAnd(A, ConstantInt::get(I.getType(), *B - 1),
|
|
|
|
Op1->getName());
|
2011-02-10 13:36:31 +08:00
|
|
|
I.setOperand(1, Rem);
|
|
|
|
return &I;
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
|
|
|
|
2016-04-11 23:43:41 +08:00
|
|
|
/// Return true if we can simplify two logical (either left or right) shifts
|
2017-01-17 04:05:26 +08:00
|
|
|
/// that have constant shift amounts: OuterShift (InnerShift X, C1), C2.
|
|
|
|
static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
|
|
|
|
Instruction *InnerShift, InstCombiner &IC,
|
2016-04-11 23:43:41 +08:00
|
|
|
Instruction *CxtI) {
|
2017-01-17 04:05:26 +08:00
|
|
|
assert(InnerShift->isLogicalShift() && "Unexpected instruction type");
|
2016-04-12 01:35:57 +08:00
|
|
|
|
2017-01-17 03:35:45 +08:00
|
|
|
// We need constant scalar or constant splat shifts.
|
2017-01-17 04:05:26 +08:00
|
|
|
const APInt *InnerShiftConst;
|
|
|
|
if (!match(InnerShift->getOperand(1), m_APInt(InnerShiftConst)))
|
2016-04-11 23:43:41 +08:00
|
|
|
return false;
|
|
|
|
|
2017-01-17 04:05:26 +08:00
|
|
|
// Two logical shifts in the same direction:
|
|
|
|
// shl (shl X, C1), C2 --> shl X, C1 + C2
|
|
|
|
// lshr (lshr X, C1), C2 --> lshr X, C1 + C2
|
|
|
|
bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
|
|
|
|
if (IsInnerShl == IsOuterShl)
|
2016-04-11 23:43:41 +08:00
|
|
|
return true;
|
|
|
|
|
2017-01-17 04:05:26 +08:00
|
|
|
// Equal shift amounts in opposite directions become bitwise 'and':
|
|
|
|
// lshr (shl X, C), C --> and X, C'
|
|
|
|
// shl (lshr X, C), C --> and X, C'
|
2018-01-04 02:28:20 +08:00
|
|
|
if (*InnerShiftConst == OuterShAmt)
|
2016-04-11 23:43:41 +08:00
|
|
|
return true;
|
|
|
|
|
2016-04-12 00:11:07 +08:00
|
|
|
// If the 2nd shift is bigger than the 1st, we can fold:
|
2017-01-17 04:05:26 +08:00
|
|
|
// lshr (shl X, C1), C2 --> and (shl X, C1 - C2), C3
|
|
|
|
// shl (lshr X, C1), C2 --> and (lshr X, C1 - C2), C3
|
2016-04-12 00:11:07 +08:00
|
|
|
// but it isn't profitable unless we know the and'd out bits are already zero.
|
2017-01-17 04:05:26 +08:00
|
|
|
// Also, check that the inner shift is valid (less than the type width) or
|
|
|
|
// we'll crash trying to produce the bit mask for the 'and'.
|
|
|
|
unsigned TypeWidth = InnerShift->getType()->getScalarSizeInBits();
|
2018-01-04 02:28:20 +08:00
|
|
|
if (InnerShiftConst->ugt(OuterShAmt) && InnerShiftConst->ult(TypeWidth)) {
|
|
|
|
unsigned InnerShAmt = InnerShiftConst->getZExtValue();
|
2017-01-17 04:05:26 +08:00
|
|
|
unsigned MaskShift =
|
|
|
|
IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt;
|
|
|
|
APInt Mask = APInt::getLowBitsSet(TypeWidth, OuterShAmt) << MaskShift;
|
|
|
|
if (IC.MaskedValueIsZero(InnerShift->getOperand(0), Mask, 0, CxtI))
|
2016-04-11 23:43:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-16 01:55:35 +08:00
|
|
|
/// See if we can compute the specified value, but shifted logically to the left
|
|
|
|
/// or right by some number of bits. This should return true if the expression
|
|
|
|
/// can be computed for the same cost as the current expression tree. This is
|
|
|
|
/// used to eliminate extraneous shifting from things like:
|
2010-08-28 06:24:38 +08:00
|
|
|
/// %C = shl i128 %A, 64
|
|
|
|
/// %D = shl i128 %B, 96
|
|
|
|
/// %E = or i128 %C, %D
|
|
|
|
/// %F = lshr i128 %E, 64
|
2017-01-16 01:55:35 +08:00
|
|
|
/// where the client will ask if E can be computed shifted right by 64-bits. If
|
|
|
|
/// this succeeds, getShiftedValue() will be called to produce the value.
|
|
|
|
static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
llvm-svn: 217342
2014-09-08 02:57:58 +08:00
|
|
|
InstCombiner &IC, Instruction *CxtI) {
|
2010-08-28 06:24:38 +08:00
|
|
|
// We can always evaluate constants shifted.
|
|
|
|
if (isa<Constant>(V))
|
|
|
|
return true;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
|
|
|
if (!I) return false;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
// If this is the opposite shift, we can directly reuse the input of the shift
|
|
|
|
// if the needed bits are already zero in the input. This allows us to reuse
|
|
|
|
// the value which means that we don't care if the shift has multiple uses.
|
|
|
|
// TODO: Handle opposite shift by exact value.
|
2014-04-25 13:29:35 +08:00
|
|
|
ConstantInt *CI = nullptr;
|
2016-04-12 01:25:23 +08:00
|
|
|
if ((IsLeftShift && match(I, m_LShr(m_Value(), m_ConstantInt(CI)))) ||
|
|
|
|
(!IsLeftShift && match(I, m_Shl(m_Value(), m_ConstantInt(CI))))) {
|
2018-01-04 02:28:20 +08:00
|
|
|
if (CI->getValue() == NumBits) {
|
2010-08-28 06:24:38 +08:00
|
|
|
// TODO: Check that the input bits are already zero with MaskedValueIsZero
|
|
|
|
#if 0
|
|
|
|
// If this is a truncate of a logical shr, we can truncate it to a smaller
|
2012-09-27 18:14:43 +08:00
|
|
|
// lshr iff we know that the bits we would otherwise be shifting in are
|
2010-08-28 06:24:38 +08:00
|
|
|
// already zeros.
|
|
|
|
uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
|
|
|
|
uint32_t BitWidth = Ty->getScalarSizeInBits();
|
|
|
|
if (MaskedValueIsZero(I->getOperand(0),
|
|
|
|
APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
|
|
|
|
CI->getLimitedValue(BitWidth) < BitWidth) {
|
|
|
|
return CanEvaluateTruncated(I->getOperand(0), Ty);
|
|
|
|
}
|
|
|
|
#endif
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
}
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
// We can't mutate something that has multiple uses: doing so would
|
|
|
|
// require duplicating the instruction in general, which isn't profitable.
|
|
|
|
if (!I->hasOneUse()) return false;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
switch (I->getOpcode()) {
|
|
|
|
default: return false;
|
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
|
|
|
// Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
|
2017-01-16 01:55:35 +08:00
|
|
|
return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) &&
|
|
|
|
canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2016-04-11 23:43:41 +08:00
|
|
|
case Instruction::Shl:
|
2016-04-12 01:11:55 +08:00
|
|
|
case Instruction::LShr:
|
2016-04-12 01:25:23 +08:00
|
|
|
return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
case Instruction::Select: {
|
|
|
|
SelectInst *SI = cast<SelectInst>(I);
|
2016-02-01 00:34:11 +08:00
|
|
|
Value *TrueVal = SI->getTrueValue();
|
|
|
|
Value *FalseVal = SI->getFalseValue();
|
2017-01-16 01:55:35 +08:00
|
|
|
return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) &&
|
|
|
|
canEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI);
|
2010-08-28 06:24:38 +08:00
|
|
|
}
|
|
|
|
case Instruction::PHI: {
|
|
|
|
// We can change a phi if we can change all operands. Note that we never
|
|
|
|
// get into trouble with cyclic PHIs here because we only consider
|
|
|
|
// instructions with a single use.
|
|
|
|
PHINode *PN = cast<PHINode>(I);
|
2015-05-13 04:05:31 +08:00
|
|
|
for (Value *IncValue : PN->incoming_values())
|
2017-01-16 01:55:35 +08:00
|
|
|
if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN))
|
2010-08-28 06:24:38 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
}
|
2010-08-28 06:24:38 +08:00
|
|
|
}
|
|
|
|
|
2017-01-17 01:27:50 +08:00
|
|
|
/// Fold OuterShift (InnerShift X, C1), C2.
|
|
|
|
/// See canEvaluateShiftedShift() for the constraints on these instructions.
|
|
|
|
static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
|
|
|
|
bool IsOuterShl,
|
|
|
|
InstCombiner::BuilderTy &Builder) {
|
|
|
|
bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
|
|
|
|
Type *ShType = InnerShift->getType();
|
|
|
|
unsigned TypeWidth = ShType->getScalarSizeInBits();
|
|
|
|
|
|
|
|
// We only accept shifts-by-a-constant in canEvaluateShifted().
|
2017-01-17 03:35:45 +08:00
|
|
|
const APInt *C1;
|
|
|
|
match(InnerShift->getOperand(1), m_APInt(C1));
|
2017-01-17 01:27:50 +08:00
|
|
|
unsigned InnerShAmt = C1->getZExtValue();
|
|
|
|
|
|
|
|
// Change the shift amount and clear the appropriate IR flags.
|
|
|
|
auto NewInnerShift = [&](unsigned ShAmt) {
|
|
|
|
InnerShift->setOperand(1, ConstantInt::get(ShType, ShAmt));
|
|
|
|
if (IsInnerShl) {
|
|
|
|
InnerShift->setHasNoUnsignedWrap(false);
|
|
|
|
InnerShift->setHasNoSignedWrap(false);
|
|
|
|
} else {
|
|
|
|
InnerShift->setIsExact(false);
|
|
|
|
}
|
|
|
|
return InnerShift;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Two logical shifts in the same direction:
|
|
|
|
// shl (shl X, C1), C2 --> shl X, C1 + C2
|
|
|
|
// lshr (lshr X, C1), C2 --> lshr X, C1 + C2
|
|
|
|
if (IsInnerShl == IsOuterShl) {
|
|
|
|
// If this is an oversized composite shift, then unsigned shifts get 0.
|
|
|
|
if (InnerShAmt + OuterShAmt >= TypeWidth)
|
|
|
|
return Constant::getNullValue(ShType);
|
|
|
|
|
|
|
|
return NewInnerShift(InnerShAmt + OuterShAmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal shift amounts in opposite directions become bitwise 'and':
|
|
|
|
// lshr (shl X, C), C --> and X, C'
|
|
|
|
// shl (lshr X, C), C --> and X, C'
|
|
|
|
if (InnerShAmt == OuterShAmt) {
|
|
|
|
APInt Mask = IsInnerShl
|
|
|
|
? APInt::getLowBitsSet(TypeWidth, TypeWidth - OuterShAmt)
|
|
|
|
: APInt::getHighBitsSet(TypeWidth, TypeWidth - OuterShAmt);
|
|
|
|
Value *And = Builder.CreateAnd(InnerShift->getOperand(0),
|
|
|
|
ConstantInt::get(ShType, Mask));
|
|
|
|
if (auto *AndI = dyn_cast<Instruction>(And)) {
|
|
|
|
AndI->moveBefore(InnerShift);
|
|
|
|
AndI->takeName(InnerShift);
|
|
|
|
}
|
|
|
|
return And;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(InnerShAmt > OuterShAmt &&
|
|
|
|
"Unexpected opposite direction logical shift pair");
|
|
|
|
|
|
|
|
// In general, we would need an 'and' for this transform, but
|
|
|
|
// canEvaluateShiftedShift() guarantees that the masked-off bits are not used.
|
|
|
|
// lshr (shl X, C1), C2 --> shl X, C1 - C2
|
|
|
|
// shl (lshr X, C1), C2 --> lshr X, C1 - C2
|
|
|
|
return NewInnerShift(InnerShAmt - OuterShAmt);
|
|
|
|
}
|
|
|
|
|
2017-01-16 01:55:35 +08:00
|
|
|
/// When canEvaluateShifted() returns true for an expression, this function
|
|
|
|
/// inserts the new computation that produces the shifted value.
|
|
|
|
static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
|
2015-03-10 10:37:25 +08:00
|
|
|
InstCombiner &IC, const DataLayout &DL) {
|
2010-08-28 06:24:38 +08:00
|
|
|
// We can always evaluate constants shifted.
|
|
|
|
if (Constant *C = dyn_cast<Constant>(V)) {
|
|
|
|
if (isLeftShift)
|
2017-07-08 07:16:26 +08:00
|
|
|
V = IC.Builder.CreateShl(C, NumBits);
|
2010-08-28 06:24:38 +08:00
|
|
|
else
|
2017-07-08 07:16:26 +08:00
|
|
|
V = IC.Builder.CreateLShr(C, NumBits);
|
2010-08-28 06:24:38 +08:00
|
|
|
// If we got a constantexpr back, try to simplify it with TD info.
|
2016-07-29 11:27:26 +08:00
|
|
|
if (auto *C = dyn_cast<Constant>(V))
|
|
|
|
if (auto *FoldedC =
|
2016-08-05 09:06:44 +08:00
|
|
|
ConstantFoldConstant(C, DL, &IC.getTargetLibraryInfo()))
|
2016-07-29 11:27:26 +08:00
|
|
|
V = FoldedC;
|
2010-08-28 06:24:38 +08:00
|
|
|
return V;
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
Instruction *I = cast<Instruction>(V);
|
|
|
|
IC.Worklist.Add(I);
|
|
|
|
|
|
|
|
switch (I->getOpcode()) {
|
2012-02-07 13:05:23 +08:00
|
|
|
default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
|
2010-08-28 06:24:38 +08:00
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
|
|
|
// Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
|
2015-03-10 10:37:25 +08:00
|
|
|
I->setOperand(
|
2017-01-16 01:55:35 +08:00
|
|
|
0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
|
2015-03-10 10:37:25 +08:00
|
|
|
I->setOperand(
|
2017-01-16 01:55:35 +08:00
|
|
|
1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
|
2010-08-28 06:24:38 +08:00
|
|
|
return I;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2017-01-17 01:27:50 +08:00
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::LShr:
|
|
|
|
return foldShiftedShift(cast<BinaryOperator>(I), NumBits, isLeftShift,
|
2017-07-08 07:16:26 +08:00
|
|
|
IC.Builder);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
case Instruction::Select:
|
2015-03-10 10:37:25 +08:00
|
|
|
I->setOperand(
|
2017-01-16 01:55:35 +08:00
|
|
|
1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
|
2015-03-10 10:37:25 +08:00
|
|
|
I->setOperand(
|
2017-01-16 01:55:35 +08:00
|
|
|
2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL));
|
2010-08-28 06:24:38 +08:00
|
|
|
return I;
|
|
|
|
case Instruction::PHI: {
|
|
|
|
// We can change a phi if we can change all operands. Note that we never
|
|
|
|
// get into trouble with cyclic PHIs here because we only consider
|
|
|
|
// instructions with a single use.
|
|
|
|
PHINode *PN = cast<PHINode>(I);
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
2017-01-16 01:55:35 +08:00
|
|
|
PN->setIncomingValue(i, getShiftedValue(PN->getIncomingValue(i), NumBits,
|
2015-03-10 10:37:25 +08:00
|
|
|
isLeftShift, IC, DL));
|
2010-08-28 06:24:38 +08:00
|
|
|
return PN;
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
}
|
2010-08-28 06:24:38 +08:00
|
|
|
}
|
|
|
|
|
2017-11-08 02:47:24 +08:00
|
|
|
// If this is a bitwise operator or add with a constant RHS we might be able
|
|
|
|
// to pull it through a shift.
|
|
|
|
static bool canShiftBinOpWithConstantRHS(BinaryOperator &Shift,
|
[InstCombine] canShiftBinOpWithConstantRHS(): drop bogus signbit check
Summary:
In D61918 i was looking at dropping it in DAGCombiner `visitShiftByConstant()`,
but as @craig.topper pointed out, it was copied from here.
That check claims that the transform is illegal otherwise.
That isn't true:
1. For `ISD::ADD`, we only process `ISD::SHL` outer shift => sign bit does not matter
https://rise4fun.com/Alive/K4A
2. For `ISD::AND`, there is no restriction on constants:
https://rise4fun.com/Alive/Wy3
3. For `ISD::OR`, there is no restriction on constants:
https://rise4fun.com/Alive/GOH
3. For `ISD::XOR`, there is no restriction on constants:
https://rise4fun.com/Alive/ml6
So, why is it there then?
As far as i can tell, it dates all the way back to original check-in rL7793.
I think we should just drop it.
Reviewers: spatel, craig.topper, efriedma, majnemer
Reviewed By: spatel
Subscribers: llvm-commits, craig.topper
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61938
llvm-svn: 361043
2019-05-17 23:52:49 +08:00
|
|
|
BinaryOperator *BO) {
|
2017-11-08 02:47:24 +08:00
|
|
|
switch (BO->getOpcode()) {
|
[InstCombine] canShiftBinOpWithConstantRHS(): drop bogus signbit check
Summary:
In D61918 i was looking at dropping it in DAGCombiner `visitShiftByConstant()`,
but as @craig.topper pointed out, it was copied from here.
That check claims that the transform is illegal otherwise.
That isn't true:
1. For `ISD::ADD`, we only process `ISD::SHL` outer shift => sign bit does not matter
https://rise4fun.com/Alive/K4A
2. For `ISD::AND`, there is no restriction on constants:
https://rise4fun.com/Alive/Wy3
3. For `ISD::OR`, there is no restriction on constants:
https://rise4fun.com/Alive/GOH
3. For `ISD::XOR`, there is no restriction on constants:
https://rise4fun.com/Alive/ml6
So, why is it there then?
As far as i can tell, it dates all the way back to original check-in rL7793.
I think we should just drop it.
Reviewers: spatel, craig.topper, efriedma, majnemer
Reviewed By: spatel
Subscribers: llvm-commits, craig.topper
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61938
llvm-svn: 361043
2019-05-17 23:52:49 +08:00
|
|
|
default:
|
|
|
|
return false; // Do not perform transform!
|
2017-11-08 02:47:24 +08:00
|
|
|
case Instruction::Add:
|
[InstCombine] canShiftBinOpWithConstantRHS(): drop bogus signbit check
Summary:
In D61918 i was looking at dropping it in DAGCombiner `visitShiftByConstant()`,
but as @craig.topper pointed out, it was copied from here.
That check claims that the transform is illegal otherwise.
That isn't true:
1. For `ISD::ADD`, we only process `ISD::SHL` outer shift => sign bit does not matter
https://rise4fun.com/Alive/K4A
2. For `ISD::AND`, there is no restriction on constants:
https://rise4fun.com/Alive/Wy3
3. For `ISD::OR`, there is no restriction on constants:
https://rise4fun.com/Alive/GOH
3. For `ISD::XOR`, there is no restriction on constants:
https://rise4fun.com/Alive/ml6
So, why is it there then?
As far as i can tell, it dates all the way back to original check-in rL7793.
I think we should just drop it.
Reviewers: spatel, craig.topper, efriedma, majnemer
Reviewed By: spatel
Subscribers: llvm-commits, craig.topper
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61938
llvm-svn: 361043
2019-05-17 23:52:49 +08:00
|
|
|
return Shift.getOpcode() == Instruction::Shl;
|
2017-11-08 02:47:24 +08:00
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
|
|
|
case Instruction::And:
|
[InstCombine] canShiftBinOpWithConstantRHS(): drop bogus signbit check
Summary:
In D61918 i was looking at dropping it in DAGCombiner `visitShiftByConstant()`,
but as @craig.topper pointed out, it was copied from here.
That check claims that the transform is illegal otherwise.
That isn't true:
1. For `ISD::ADD`, we only process `ISD::SHL` outer shift => sign bit does not matter
https://rise4fun.com/Alive/K4A
2. For `ISD::AND`, there is no restriction on constants:
https://rise4fun.com/Alive/Wy3
3. For `ISD::OR`, there is no restriction on constants:
https://rise4fun.com/Alive/GOH
3. For `ISD::XOR`, there is no restriction on constants:
https://rise4fun.com/Alive/ml6
So, why is it there then?
As far as i can tell, it dates all the way back to original check-in rL7793.
I think we should just drop it.
Reviewers: spatel, craig.topper, efriedma, majnemer
Reviewed By: spatel
Subscribers: llvm-commits, craig.topper
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61938
llvm-svn: 361043
2019-05-17 23:52:49 +08:00
|
|
|
return true;
|
2017-11-08 02:47:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 05:50:37 +08:00
|
|
|
Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, Constant *Op1,
|
2010-01-05 15:44:46 +08:00
|
|
|
BinaryOperator &I) {
|
|
|
|
bool isLeftShift = I.getOpcode() == Instruction::Shl;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2017-01-17 05:24:41 +08:00
|
|
|
const APInt *Op1C;
|
|
|
|
if (!match(Op1, m_APInt(Op1C)))
|
2014-04-15 05:50:37 +08:00
|
|
|
return nullptr;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
// See if we can propagate this shift into the input, this covers the trivial
|
|
|
|
// cast of lshr(shl(x,c1),c2) as well as other more complex cases.
|
|
|
|
if (I.getOpcode() != Instruction::AShr &&
|
2017-01-17 05:24:41 +08:00
|
|
|
canEvaluateShifted(Op0, Op1C->getZExtValue(), isLeftShift, *this, &I)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "ICE: GetShiftedValue propagating shift through expression"
|
|
|
|
" to eliminate shift:\n IN: "
|
|
|
|
<< *Op0 << "\n SH: " << I << "\n");
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(
|
2017-01-17 05:24:41 +08:00
|
|
|
I, getShiftedValue(Op0, Op1C->getZExtValue(), isLeftShift, *this, DL));
|
2010-08-28 06:24:38 +08:00
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
|
|
|
// See if we can simplify any instructions used by the instruction whose sole
|
2010-01-05 15:44:46 +08:00
|
|
|
// purpose is to compute bits we don't care about.
|
2017-01-17 05:24:41 +08:00
|
|
|
unsigned TypeBits = Op0->getType()->getScalarSizeInBits();
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2017-01-17 05:24:41 +08:00
|
|
|
assert(!Op1C->uge(TypeBits) &&
|
2014-04-24 00:48:40 +08:00
|
|
|
"Shift over the type width should have been removed already");
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2018-03-01 00:36:24 +08:00
|
|
|
if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I))
|
2017-01-11 07:49:07 +08:00
|
|
|
return FoldedShift;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
// Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
|
|
|
|
if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
|
|
|
|
Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
|
|
|
|
// If 'shift2' is an ashr, we would have to get the sign bit into a funny
|
|
|
|
// place. Don't try to do this transformation in this case. Also, we
|
|
|
|
// require that the input operand is a shift-by-constant so that we have
|
|
|
|
// confidence that the shifts will get folded together. We could do this
|
|
|
|
// xform in more cases, but it is unlikely to be profitable.
|
2012-12-09 23:37:46 +08:00
|
|
|
if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
|
2010-01-05 15:44:46 +08:00
|
|
|
isa<ConstantInt>(TrOp->getOperand(1))) {
|
|
|
|
// Okay, we'll do this xform. Make the shift of shift.
|
2017-01-17 05:24:41 +08:00
|
|
|
Constant *ShAmt =
|
|
|
|
ConstantExpr::getZExt(cast<Constant>(Op1), TrOp->getType());
|
2010-01-05 15:44:46 +08:00
|
|
|
// (shift2 (shift1 & 0x00FF), c2)
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *NSh = Builder.CreateBinOp(I.getOpcode(), TrOp, ShAmt, I.getName());
|
2010-01-05 15:44:46 +08:00
|
|
|
|
|
|
|
// For logical shifts, the truncation has the effect of making the high
|
|
|
|
// part of the register be zeros. Emulate this by inserting an AND to
|
|
|
|
// clear the top bits as needed. This 'and' will usually be zapped by
|
|
|
|
// other xforms later if dead.
|
|
|
|
unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
|
|
|
|
unsigned DstSize = TI->getType()->getScalarSizeInBits();
|
|
|
|
APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
// The mask we constructed says what the trunc would do if occurring
|
|
|
|
// between the shifts. We want to know the effect *after* the second
|
|
|
|
// shift. We know that it is a logical shift by a constant, so adjust the
|
|
|
|
// mask as appropriate.
|
|
|
|
if (I.getOpcode() == Instruction::Shl)
|
2017-01-17 05:24:41 +08:00
|
|
|
MaskV <<= Op1C->getZExtValue();
|
2010-01-05 15:44:46 +08:00
|
|
|
else {
|
|
|
|
assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
|
2017-04-19 01:14:21 +08:00
|
|
|
MaskV.lshrInPlace(Op1C->getZExtValue());
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// shift1 & 0x00FF
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *And = Builder.CreateAnd(NSh,
|
|
|
|
ConstantInt::get(I.getContext(), MaskV),
|
|
|
|
TI->getName());
|
2010-01-05 15:44:46 +08:00
|
|
|
|
|
|
|
// Return the value truncated to the interesting size.
|
|
|
|
return new TruncInst(And, I.getType());
|
|
|
|
}
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
if (Op0->hasOneUse()) {
|
|
|
|
if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
|
|
|
|
// Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
|
|
|
|
Value *V1, *V2;
|
|
|
|
ConstantInt *CC;
|
|
|
|
switch (Op0BO->getOpcode()) {
|
2010-01-10 14:59:55 +08:00
|
|
|
default: break;
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor: {
|
|
|
|
// These operators commute.
|
|
|
|
// Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
|
|
|
|
if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
|
|
|
|
match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
|
|
|
|
m_Specific(Op1)))) {
|
|
|
|
Value *YS = // (Y << C)
|
2017-07-08 07:16:26 +08:00
|
|
|
Builder.CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
|
2010-01-10 14:59:55 +08:00
|
|
|
// (X + (Y << C))
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *X = Builder.CreateBinOp(Op0BO->getOpcode(), YS, V1,
|
|
|
|
Op0BO->getOperand(1)->getName());
|
2017-01-17 05:24:41 +08:00
|
|
|
unsigned Op1Val = Op1C->getLimitedValue(TypeBits);
|
2014-04-15 05:50:37 +08:00
|
|
|
|
|
|
|
APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
|
|
|
|
Constant *Mask = ConstantInt::get(I.getContext(), Bits);
|
|
|
|
if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
|
|
|
|
Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
|
|
|
|
return BinaryOperator::CreateAnd(X, Mask);
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-10 14:59:55 +08:00
|
|
|
// Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
|
|
|
|
Value *Op0BOOp1 = Op0BO->getOperand(1);
|
|
|
|
if (isLeftShift && Op0BOOp1->hasOneUse() &&
|
2012-12-09 23:37:46 +08:00
|
|
|
match(Op0BOOp1,
|
2012-12-10 00:06:44 +08:00
|
|
|
m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
|
|
|
|
m_ConstantInt(CC)))) {
|
2010-01-10 14:59:55 +08:00
|
|
|
Value *YS = // (Y << C)
|
2017-07-08 07:16:26 +08:00
|
|
|
Builder.CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
|
2010-01-10 14:59:55 +08:00
|
|
|
// X & (CC << C)
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *XM = Builder.CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
|
|
|
|
V1->getName()+".mask");
|
2010-01-10 14:59:55 +08:00
|
|
|
return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
|
|
|
|
}
|
2016-08-18 04:30:52 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2010-01-10 14:59:55 +08:00
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-10 14:59:55 +08:00
|
|
|
case Instruction::Sub: {
|
|
|
|
// Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
|
|
|
|
if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
|
|
|
|
match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
|
|
|
|
m_Specific(Op1)))) {
|
|
|
|
Value *YS = // (Y << C)
|
2017-07-08 07:16:26 +08:00
|
|
|
Builder.CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
|
2010-01-10 14:59:55 +08:00
|
|
|
// (X + (Y << C))
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *X = Builder.CreateBinOp(Op0BO->getOpcode(), V1, YS,
|
|
|
|
Op0BO->getOperand(0)->getName());
|
2017-01-17 05:24:41 +08:00
|
|
|
unsigned Op1Val = Op1C->getLimitedValue(TypeBits);
|
2014-04-15 05:50:37 +08:00
|
|
|
|
|
|
|
APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
|
|
|
|
Constant *Mask = ConstantInt::get(I.getContext(), Bits);
|
|
|
|
if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
|
|
|
|
Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
|
|
|
|
return BinaryOperator::CreateAnd(X, Mask);
|
2010-01-10 14:59:55 +08:00
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-10 14:59:55 +08:00
|
|
|
// Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
|
|
|
|
if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
|
|
|
|
match(Op0BO->getOperand(0),
|
2012-12-10 00:06:44 +08:00
|
|
|
m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))),
|
|
|
|
m_ConstantInt(CC))) && V2 == Op1) {
|
2010-01-10 14:59:55 +08:00
|
|
|
Value *YS = // (Y << C)
|
2017-07-08 07:16:26 +08:00
|
|
|
Builder.CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
|
2010-01-10 14:59:55 +08:00
|
|
|
// X & (CC << C)
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *XM = Builder.CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
|
|
|
|
V1->getName()+".mask");
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-10 14:59:55 +08:00
|
|
|
return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-10 14:59:55 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
|
|
|
|
2014-07-22 12:57:06 +08:00
|
|
|
// If the operand is a bitwise operator with a constant RHS, and the
|
2010-01-05 15:44:46 +08:00
|
|
|
// shift is the only use, we can pull it out of the shift.
|
2017-08-06 04:00:42 +08:00
|
|
|
const APInt *Op0C;
|
|
|
|
if (match(Op0BO->getOperand(1), m_APInt(Op0C))) {
|
[InstCombine] canShiftBinOpWithConstantRHS(): drop bogus signbit check
Summary:
In D61918 i was looking at dropping it in DAGCombiner `visitShiftByConstant()`,
but as @craig.topper pointed out, it was copied from here.
That check claims that the transform is illegal otherwise.
That isn't true:
1. For `ISD::ADD`, we only process `ISD::SHL` outer shift => sign bit does not matter
https://rise4fun.com/Alive/K4A
2. For `ISD::AND`, there is no restriction on constants:
https://rise4fun.com/Alive/Wy3
3. For `ISD::OR`, there is no restriction on constants:
https://rise4fun.com/Alive/GOH
3. For `ISD::XOR`, there is no restriction on constants:
https://rise4fun.com/Alive/ml6
So, why is it there then?
As far as i can tell, it dates all the way back to original check-in rL7793.
I think we should just drop it.
Reviewers: spatel, craig.topper, efriedma, majnemer
Reviewed By: spatel
Subscribers: llvm-commits, craig.topper
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61938
llvm-svn: 361043
2019-05-17 23:52:49 +08:00
|
|
|
if (canShiftBinOpWithConstantRHS(I, Op0BO)) {
|
2017-08-06 04:00:42 +08:00
|
|
|
Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
|
|
|
|
cast<Constant>(Op0BO->getOperand(1)), Op1);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
Value *NewShift =
|
2017-07-08 07:16:26 +08:00
|
|
|
Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
|
2010-01-05 15:44:46 +08:00
|
|
|
NewShift->takeName(Op0BO);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
|
|
|
|
NewRHS);
|
|
|
|
}
|
|
|
|
}
|
2017-08-09 04:14:11 +08:00
|
|
|
|
|
|
|
// If the operand is a subtract with a constant LHS, and the shift
|
|
|
|
// is the only use, we can pull it out of the shift.
|
|
|
|
// This folds (shl (sub C1, X), C2) -> (sub (C1 << C2), (shl X, C2))
|
|
|
|
if (isLeftShift && Op0BO->getOpcode() == Instruction::Sub &&
|
|
|
|
match(Op0BO->getOperand(0), m_APInt(Op0C))) {
|
|
|
|
Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
|
|
|
|
cast<Constant>(Op0BO->getOperand(0)), Op1);
|
|
|
|
|
|
|
|
Value *NewShift = Builder.CreateShl(Op0BO->getOperand(1), Op1);
|
|
|
|
NewShift->takeName(Op0BO);
|
|
|
|
|
|
|
|
return BinaryOperator::CreateSub(NewRHS, NewShift);
|
|
|
|
}
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
2017-11-08 02:47:24 +08:00
|
|
|
|
|
|
|
// If we have a select that conditionally executes some binary operator,
|
|
|
|
// see if we can pull it the select and operator through the shift.
|
|
|
|
//
|
|
|
|
// For example, turning:
|
|
|
|
// shl (select C, (add X, C1), X), C2
|
|
|
|
// Into:
|
|
|
|
// Y = shl X, C2
|
|
|
|
// select C, (add Y, C1 << C2), Y
|
|
|
|
Value *Cond;
|
|
|
|
BinaryOperator *TBO;
|
|
|
|
Value *FalseVal;
|
|
|
|
if (match(Op0, m_Select(m_Value(Cond), m_OneUse(m_BinOp(TBO)),
|
|
|
|
m_Value(FalseVal)))) {
|
|
|
|
const APInt *C;
|
|
|
|
if (!isa<Constant>(FalseVal) && TBO->getOperand(0) == FalseVal &&
|
|
|
|
match(TBO->getOperand(1), m_APInt(C)) &&
|
[InstCombine] canShiftBinOpWithConstantRHS(): drop bogus signbit check
Summary:
In D61918 i was looking at dropping it in DAGCombiner `visitShiftByConstant()`,
but as @craig.topper pointed out, it was copied from here.
That check claims that the transform is illegal otherwise.
That isn't true:
1. For `ISD::ADD`, we only process `ISD::SHL` outer shift => sign bit does not matter
https://rise4fun.com/Alive/K4A
2. For `ISD::AND`, there is no restriction on constants:
https://rise4fun.com/Alive/Wy3
3. For `ISD::OR`, there is no restriction on constants:
https://rise4fun.com/Alive/GOH
3. For `ISD::XOR`, there is no restriction on constants:
https://rise4fun.com/Alive/ml6
So, why is it there then?
As far as i can tell, it dates all the way back to original check-in rL7793.
I think we should just drop it.
Reviewers: spatel, craig.topper, efriedma, majnemer
Reviewed By: spatel
Subscribers: llvm-commits, craig.topper
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61938
llvm-svn: 361043
2019-05-17 23:52:49 +08:00
|
|
|
canShiftBinOpWithConstantRHS(I, TBO)) {
|
2017-11-08 02:47:24 +08:00
|
|
|
Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
|
|
|
|
cast<Constant>(TBO->getOperand(1)), Op1);
|
|
|
|
|
|
|
|
Value *NewShift =
|
|
|
|
Builder.CreateBinOp(I.getOpcode(), FalseVal, Op1);
|
|
|
|
Value *NewOp = Builder.CreateBinOp(TBO->getOpcode(), NewShift,
|
|
|
|
NewRHS);
|
|
|
|
return SelectInst::Create(Cond, NewOp, NewShift);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryOperator *FBO;
|
|
|
|
Value *TrueVal;
|
|
|
|
if (match(Op0, m_Select(m_Value(Cond), m_Value(TrueVal),
|
|
|
|
m_OneUse(m_BinOp(FBO))))) {
|
|
|
|
const APInt *C;
|
|
|
|
if (!isa<Constant>(TrueVal) && FBO->getOperand(0) == TrueVal &&
|
2018-07-31 03:41:25 +08:00
|
|
|
match(FBO->getOperand(1), m_APInt(C)) &&
|
[InstCombine] canShiftBinOpWithConstantRHS(): drop bogus signbit check
Summary:
In D61918 i was looking at dropping it in DAGCombiner `visitShiftByConstant()`,
but as @craig.topper pointed out, it was copied from here.
That check claims that the transform is illegal otherwise.
That isn't true:
1. For `ISD::ADD`, we only process `ISD::SHL` outer shift => sign bit does not matter
https://rise4fun.com/Alive/K4A
2. For `ISD::AND`, there is no restriction on constants:
https://rise4fun.com/Alive/Wy3
3. For `ISD::OR`, there is no restriction on constants:
https://rise4fun.com/Alive/GOH
3. For `ISD::XOR`, there is no restriction on constants:
https://rise4fun.com/Alive/ml6
So, why is it there then?
As far as i can tell, it dates all the way back to original check-in rL7793.
I think we should just drop it.
Reviewers: spatel, craig.topper, efriedma, majnemer
Reviewed By: spatel
Subscribers: llvm-commits, craig.topper
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61938
llvm-svn: 361043
2019-05-17 23:52:49 +08:00
|
|
|
canShiftBinOpWithConstantRHS(I, FBO)) {
|
2017-11-08 02:47:24 +08:00
|
|
|
Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
|
|
|
|
cast<Constant>(FBO->getOperand(1)), Op1);
|
|
|
|
|
|
|
|
Value *NewShift =
|
|
|
|
Builder.CreateBinOp(I.getOpcode(), TrueVal, Op1);
|
|
|
|
Value *NewOp = Builder.CreateBinOp(FBO->getOpcode(), NewShift,
|
|
|
|
NewRHS);
|
|
|
|
return SelectInst::Create(Cond, NewShift, NewOp);
|
|
|
|
}
|
|
|
|
}
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitShl(BinaryOperator &I) {
|
2018-06-22 01:06:36 +08:00
|
|
|
if (Value *V = SimplifyShlInst(I.getOperand(0), I.getOperand(1),
|
|
|
|
I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
|
|
|
|
SQ.getWithInstruction(&I)))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2018-10-03 23:20:58 +08:00
|
|
|
if (Instruction *X = foldVectorBinop(I))
|
2018-06-03 00:27:44 +08:00
|
|
|
return X;
|
|
|
|
|
2011-02-10 13:36:31 +08:00
|
|
|
if (Instruction *V = commonShiftTransforms(I))
|
|
|
|
return V;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
[InstCombine] Dropping redundant masking before left-shift [0/5] (PR42563)
Summary:
If we have some pattern that leaves only some low bits set, and then performs
left-shift of those bits, if none of the bits that are left after the final
shift are modified by the mask, we can omit the mask.
There are many variants to this pattern:
a. `(x & ((1 << MaskShAmt) - 1)) << ShiftShAmt`
All these patterns can be simplified to just:
`x << ShiftShAmt`
iff:
a. `(MaskShAmt+ShiftShAmt) u>= bitwidth(x)`
alive proof:
a: https://rise4fun.com/Alive/wi9
Indeed, not all of these patterns are canonical.
But since this fold will only produce a single instruction
i'm really interested in handling even uncanonical patterns,
since i have this general kind of pattern in hotpaths,
and it is not totally outlandish for bit-twiddling code.
For now let's start with patterns where both shift amounts are variable,
with trivial constant "offset" between them, since i believe this is
both simplest to handle and i think this is most common.
But again, there are likely other variants where we could use
ValueTracking/ConstantRange to handle more cases.
https://bugs.llvm.org/show_bug.cgi?id=42563
Reviewers: spatel, nikic, huihuiz, xbolva00
Reviewed By: xbolva00
Subscribers: efriedma, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64512
llvm-svn: 366535
2019-07-19 16:25:43 +08:00
|
|
|
if (Instruction *V = dropRedundantMaskingOfLeftShiftInput(&I, SQ))
|
|
|
|
return V;
|
|
|
|
|
2018-06-22 01:06:36 +08:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2018-06-11 04:10:13 +08:00
|
|
|
Type *Ty = I.getType();
|
2019-06-22 00:25:32 +08:00
|
|
|
unsigned BitWidth = Ty->getScalarSizeInBits();
|
|
|
|
|
2017-01-14 02:39:09 +08:00
|
|
|
const APInt *ShAmtAPInt;
|
|
|
|
if (match(Op1, m_APInt(ShAmtAPInt))) {
|
|
|
|
unsigned ShAmt = ShAmtAPInt->getZExtValue();
|
2018-06-22 01:06:36 +08:00
|
|
|
unsigned BitWidth = Ty->getScalarSizeInBits();
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2017-01-14 02:52:10 +08:00
|
|
|
Value *X;
|
2019-08-06 00:04:07 +08:00
|
|
|
if (match(Op0, m_OneUse(m_ZExt(m_Value(X))))) {
|
2017-01-14 02:52:10 +08:00
|
|
|
unsigned SrcWidth = X->getType()->getScalarSizeInBits();
|
2019-08-06 00:59:58 +08:00
|
|
|
// shl (zext X), ShAmt --> zext (shl X, ShAmt)
|
|
|
|
// This is only valid if X would have zeros shifted out.
|
2017-01-14 02:52:10 +08:00
|
|
|
if (ShAmt < SrcWidth &&
|
|
|
|
MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmt), 0, &I))
|
2017-07-08 07:16:26 +08:00
|
|
|
return new ZExtInst(Builder.CreateShl(X, ShAmt), Ty);
|
2019-08-06 00:59:58 +08:00
|
|
|
|
|
|
|
// shl (zext (mul MulOp, C2)), ShAmt --> mul (zext MulOp), (C2 << ShAmt)
|
|
|
|
// This is valid if the high bits of the wider multiply are shifted out.
|
|
|
|
Value *MulOp;
|
|
|
|
const APInt *C2;
|
|
|
|
if (ShAmt >= (BitWidth - SrcWidth) &&
|
|
|
|
match(X, m_Mul(m_Value(MulOp), m_APInt(C2)))) {
|
|
|
|
Value *Zext = Builder.CreateZExt(MulOp, Ty);
|
|
|
|
Constant *NewMulC = ConstantInt::get(Ty, C2->zext(BitWidth).shl(ShAmt));
|
|
|
|
return BinaryOperator::CreateMul(Zext, NewMulC);
|
|
|
|
}
|
2017-01-04 10:21:34 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 03:33:14 +08:00
|
|
|
// (X >> C) << C --> X & (-1 << C)
|
|
|
|
if (match(Op0, m_Shr(m_Value(X), m_Specific(Op1)))) {
|
2017-01-27 06:08:10 +08:00
|
|
|
APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmt));
|
2017-01-30 01:11:18 +08:00
|
|
|
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
|
|
|
|
}
|
|
|
|
|
[NFC][SCEV] Add tests related to bit masking (PR37793)
Summary:
Related to https://bugs.llvm.org/show_bug.cgi?id=37793, https://reviews.llvm.org/D46760#1127287
We'd like to do this canonicalization https://rise4fun.com/Alive/Gmc
But it is currently restricted by rL155136 / rL155362, which says:
```
// This is a constant shift of a constant shift. Be careful about hiding
// shl instructions behind bit masks. They are used to represent multiplies
// by a constant, and it is important that simple arithmetic expressions
// are still recognizable by scalar evolution.
//
// The transforms applied to shl are very similar to the transforms applied
// to mul by constant. We can be more aggressive about optimizing right
// shifts.
//
// Combinations of right and left shifts will still be optimized in
// DAGCombine where scalar evolution no longer applies.
```
I think these tests show that for *constants*, SCEV has no issues with that canonicalization.
Reviewers: mkazantsev, spatel, efriedma, sanjoy
Reviewed By: mkazantsev
Subscribers: sanjoy, javed.absar, llvm-commits, stoklund, bixia
Differential Revision: https://reviews.llvm.org/D48229
llvm-svn: 335101
2018-06-20 15:54:11 +08:00
|
|
|
// FIXME: we do not yet transform non-exact shr's. The backend (DAGCombine)
|
|
|
|
// needs a few fixes for the rotate pattern recognition first.
|
2017-02-02 05:31:34 +08:00
|
|
|
const APInt *ShOp1;
|
2017-06-24 14:24:04 +08:00
|
|
|
if (match(Op0, m_Exact(m_Shr(m_Value(X), m_APInt(ShOp1))))) {
|
2017-02-02 05:31:34 +08:00
|
|
|
unsigned ShrAmt = ShOp1->getZExtValue();
|
2017-01-31 02:40:23 +08:00
|
|
|
if (ShrAmt < ShAmt) {
|
|
|
|
// If C1 < C2: (X >>?,exact C1) << C2 --> X << (C2 - C1)
|
|
|
|
Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShrAmt);
|
|
|
|
auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
|
|
|
|
NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
|
|
|
|
NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
|
|
|
|
return NewShl;
|
|
|
|
}
|
|
|
|
if (ShrAmt > ShAmt) {
|
|
|
|
// If C1 > C2: (X >>?exact C1) << C2 --> X >>?exact (C1 - C2)
|
|
|
|
Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmt);
|
|
|
|
auto *NewShr = BinaryOperator::Create(
|
|
|
|
cast<BinaryOperator>(Op0)->getOpcode(), X, ShiftDiff);
|
|
|
|
NewShr->setIsExact(true);
|
|
|
|
return NewShr;
|
|
|
|
}
|
2017-01-27 06:08:10 +08:00
|
|
|
}
|
|
|
|
|
2017-02-02 05:31:34 +08:00
|
|
|
if (match(Op0, m_Shl(m_Value(X), m_APInt(ShOp1)))) {
|
|
|
|
unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
|
|
|
|
// Oversized shifts are simplified to zero in InstSimplify.
|
|
|
|
if (AmtSum < BitWidth)
|
|
|
|
// (X << C1) << C2 --> X << (C1 + C2)
|
|
|
|
return BinaryOperator::CreateShl(X, ConstantInt::get(Ty, AmtSum));
|
|
|
|
}
|
|
|
|
|
2011-02-10 13:36:31 +08:00
|
|
|
// If the shifted-out value is known-zero, then this is a NUW shift.
|
2012-12-09 23:37:46 +08:00
|
|
|
if (!I.hasNoUnsignedWrap() &&
|
2017-01-27 06:08:10 +08:00
|
|
|
MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, ShAmt), 0, &I)) {
|
2016-02-01 00:34:11 +08:00
|
|
|
I.setHasNoUnsignedWrap();
|
|
|
|
return &I;
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2017-01-14 02:08:25 +08:00
|
|
|
// If the shifted-out value is all signbits, then this is a NSW shift.
|
|
|
|
if (!I.hasNoSignedWrap() && ComputeNumSignBits(Op0, 0, &I) > ShAmt) {
|
2011-02-10 13:36:31 +08:00
|
|
|
I.setHasNoSignedWrap();
|
|
|
|
return &I;
|
|
|
|
}
|
|
|
|
}
|
2011-04-29 16:15:41 +08:00
|
|
|
|
2018-06-11 04:10:13 +08:00
|
|
|
// Transform (x >> y) << y to x & (-1 << y)
|
|
|
|
// Valid for any type of right-shift.
|
|
|
|
Value *X;
|
|
|
|
if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) {
|
|
|
|
Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
|
|
|
|
Value *Mask = Builder.CreateShl(AllOnes, Op1);
|
|
|
|
return BinaryOperator::CreateAnd(Mask, X);
|
|
|
|
}
|
|
|
|
|
2017-02-10 07:13:04 +08:00
|
|
|
Constant *C1;
|
|
|
|
if (match(Op1, m_Constant(C1))) {
|
|
|
|
Constant *C2;
|
|
|
|
Value *X;
|
|
|
|
// (C2 << X) << C1 --> (C2 << C1) << X
|
|
|
|
if (match(Op0, m_OneUse(m_Shl(m_Constant(C2), m_Value(X)))))
|
|
|
|
return BinaryOperator::CreateShl(ConstantExpr::getShl(C2, C1), X);
|
|
|
|
|
|
|
|
// (X * C2) << C1 --> X * (C2 << C1)
|
|
|
|
if (match(Op0, m_Mul(m_Value(X), m_Constant(C2))))
|
|
|
|
return BinaryOperator::CreateMul(X, ConstantExpr::getShl(C2, C1));
|
|
|
|
}
|
2011-04-29 16:15:41 +08:00
|
|
|
|
2019-06-22 00:25:32 +08:00
|
|
|
// (1 << (C - x)) -> ((1 << C) >> x) if C is bitwidth - 1
|
|
|
|
if (match(Op0, m_One()) &&
|
|
|
|
match(Op1, m_Sub(m_SpecificInt(BitWidth - 1), m_Value(X))))
|
|
|
|
return BinaryOperator::CreateLShr(
|
|
|
|
ConstantInt::get(Ty, APInt::getSignMask(BitWidth)), X);
|
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
|
2018-06-22 01:06:36 +08:00
|
|
|
if (Value *V = SimplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
|
|
|
|
SQ.getWithInstruction(&I)))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2011-01-14 08:37:45 +08:00
|
|
|
|
2018-10-03 23:20:58 +08:00
|
|
|
if (Instruction *X = foldVectorBinop(I))
|
2018-06-03 00:27:44 +08:00
|
|
|
return X;
|
|
|
|
|
2010-01-24 02:49:30 +08:00
|
|
|
if (Instruction *R = commonShiftTransforms(I))
|
|
|
|
return R;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2018-06-22 01:06:36 +08:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2017-01-31 00:11:40 +08:00
|
|
|
Type *Ty = I.getType();
|
2017-01-14 07:04:10 +08:00
|
|
|
const APInt *ShAmtAPInt;
|
|
|
|
if (match(Op1, m_APInt(ShAmtAPInt))) {
|
|
|
|
unsigned ShAmt = ShAmtAPInt->getZExtValue();
|
2017-01-31 00:11:40 +08:00
|
|
|
unsigned BitWidth = Ty->getScalarSizeInBits();
|
2017-01-14 07:04:10 +08:00
|
|
|
auto *II = dyn_cast<IntrinsicInst>(Op0);
|
|
|
|
if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt &&
|
|
|
|
(II->getIntrinsicID() == Intrinsic::ctlz ||
|
|
|
|
II->getIntrinsicID() == Intrinsic::cttz ||
|
|
|
|
II->getIntrinsicID() == Intrinsic::ctpop)) {
|
2010-01-24 02:49:30 +08:00
|
|
|
// ctlz.i32(x)>>5 --> zext(x == 0)
|
|
|
|
// cttz.i32(x)>>5 --> zext(x == 0)
|
|
|
|
// ctpop.i32(x)>>5 --> zext(x == -1)
|
2017-01-14 07:04:10 +08:00
|
|
|
bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop;
|
2017-01-31 00:11:40 +08:00
|
|
|
Constant *RHS = ConstantInt::getSigned(Ty, IsPop ? -1 : 0);
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *Cmp = Builder.CreateICmpEQ(II->getArgOperand(0), RHS);
|
2017-01-31 00:11:40 +08:00
|
|
|
return new ZExtInst(Cmp, Ty);
|
2010-01-24 02:49:30 +08:00
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2017-01-27 06:08:10 +08:00
|
|
|
Value *X;
|
2017-02-02 05:31:34 +08:00
|
|
|
const APInt *ShOp1;
|
2018-11-06 19:28:22 +08:00
|
|
|
if (match(Op0, m_Shl(m_Value(X), m_APInt(ShOp1))) && ShOp1->ult(BitWidth)) {
|
|
|
|
if (ShOp1->ult(ShAmt)) {
|
|
|
|
unsigned ShlAmt = ShOp1->getZExtValue();
|
2017-01-31 00:11:40 +08:00
|
|
|
Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
|
|
|
|
if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
|
|
|
|
// (X <<nuw C1) >>u C2 --> X >>u (C2 - C1)
|
2017-01-31 01:38:55 +08:00
|
|
|
auto *NewLShr = BinaryOperator::CreateLShr(X, ShiftDiff);
|
2017-01-31 00:11:40 +08:00
|
|
|
NewLShr->setIsExact(I.isExact());
|
|
|
|
return NewLShr;
|
|
|
|
}
|
|
|
|
// (X << C1) >>u C2 --> (X >>u (C2 - C1)) & (-1 >> C2)
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *NewLShr = Builder.CreateLShr(X, ShiftDiff, "", I.isExact());
|
2017-01-31 00:11:40 +08:00
|
|
|
APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
|
|
|
|
return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask));
|
|
|
|
}
|
2018-11-06 19:28:22 +08:00
|
|
|
if (ShOp1->ugt(ShAmt)) {
|
|
|
|
unsigned ShlAmt = ShOp1->getZExtValue();
|
2017-01-31 07:01:05 +08:00
|
|
|
Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
|
|
|
|
if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
|
|
|
|
// (X <<nuw C1) >>u C2 --> X <<nuw (C1 - C2)
|
|
|
|
auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
|
|
|
|
NewShl->setHasNoUnsignedWrap(true);
|
|
|
|
return NewShl;
|
|
|
|
}
|
|
|
|
// (X << C1) >>u C2 --> X << (C1 - C2) & (-1 >> C2)
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *NewShl = Builder.CreateShl(X, ShiftDiff);
|
2017-01-31 07:01:05 +08:00
|
|
|
APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
|
|
|
|
return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
|
|
|
|
}
|
2018-11-06 19:28:22 +08:00
|
|
|
assert(*ShOp1 == ShAmt);
|
2017-01-31 07:01:05 +08:00
|
|
|
// (X << C) >>u C --> X & (-1 >>u C)
|
|
|
|
APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
|
|
|
|
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
|
2017-01-27 06:08:10 +08:00
|
|
|
}
|
|
|
|
|
2017-08-04 23:42:47 +08:00
|
|
|
if (match(Op0, m_OneUse(m_ZExt(m_Value(X)))) &&
|
|
|
|
(!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
|
2017-08-05 00:08:41 +08:00
|
|
|
assert(ShAmt < X->getType()->getScalarSizeInBits() &&
|
|
|
|
"Big shift not simplified to zero?");
|
2017-08-04 23:42:47 +08:00
|
|
|
// lshr (zext iM X to iN), C --> zext (lshr X, C) to iN
|
|
|
|
Value *NewLShr = Builder.CreateLShr(X, ShAmt);
|
|
|
|
return new ZExtInst(NewLShr, Ty);
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:23:43 +08:00
|
|
|
if (match(Op0, m_SExt(m_Value(X))) &&
|
|
|
|
(!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
|
2017-06-08 04:32:08 +08:00
|
|
|
// Are we moving the sign bit to the low bit and widening with high zeros?
|
|
|
|
unsigned SrcTyBitWidth = X->getType()->getScalarSizeInBits();
|
2017-06-12 22:23:43 +08:00
|
|
|
if (ShAmt == BitWidth - 1) {
|
2017-06-08 04:32:08 +08:00
|
|
|
// lshr (sext i1 X to iN), N-1 --> zext X to iN
|
|
|
|
if (SrcTyBitWidth == 1)
|
|
|
|
return new ZExtInst(X, Ty);
|
|
|
|
|
|
|
|
// lshr (sext iM X to iN), N-1 --> zext (lshr X, M-1) to iN
|
|
|
|
if (Op0->hasOneUse()) {
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *NewLShr = Builder.CreateLShr(X, SrcTyBitWidth - 1);
|
2017-06-08 04:32:08 +08:00
|
|
|
return new ZExtInst(NewLShr, Ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:23:43 +08:00
|
|
|
// lshr (sext iM X to iN), N-M --> zext (ashr X, min(N-M, M-1)) to iN
|
|
|
|
if (ShAmt == BitWidth - SrcTyBitWidth && Op0->hasOneUse()) {
|
|
|
|
// The new shift amount can't be more than the narrow source type.
|
|
|
|
unsigned NewShAmt = std::min(ShAmt, SrcTyBitWidth - 1);
|
2017-07-08 07:16:26 +08:00
|
|
|
Value *AShr = Builder.CreateAShr(X, NewShAmt);
|
2017-06-12 22:23:43 +08:00
|
|
|
return new ZExtInst(AShr, Ty);
|
|
|
|
}
|
2017-06-08 04:32:08 +08:00
|
|
|
}
|
|
|
|
|
2017-02-02 05:31:34 +08:00
|
|
|
if (match(Op0, m_LShr(m_Value(X), m_APInt(ShOp1)))) {
|
|
|
|
unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
|
|
|
|
// Oversized shifts are simplified to zero in InstSimplify.
|
|
|
|
if (AmtSum < BitWidth)
|
|
|
|
// (X >>u C1) >>u C2 --> X >>u (C1 + C2)
|
|
|
|
return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
|
|
|
|
}
|
|
|
|
|
2011-02-10 13:36:31 +08:00
|
|
|
// If the shifted-out value is known-zero, then this is an exact shift.
|
2012-12-09 23:37:46 +08:00
|
|
|
if (!I.isExact() &&
|
2017-01-14 07:04:10 +08:00
|
|
|
MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
|
2011-02-10 13:36:31 +08:00
|
|
|
I.setIsExact();
|
|
|
|
return &I;
|
2012-12-09 23:37:46 +08:00
|
|
|
}
|
2011-02-10 13:36:31 +08:00
|
|
|
}
|
[InstCombine] Recommit: Fold (x << y) >> y -> x & (-1 >> y)
Summary:
We already do it for splat constants, but not just values.
Also, undef cases are mostly non-functional.
The original commit was reverted because
it broke tests for amdgpu backend, which i didn't check.
Now, the backed was updated to recognize these new
patterns, so we are good.
https://bugs.llvm.org/show_bug.cgi?id=37603
https://rise4fun.com/Alive/cplX
Reviewers: spatel, craig.topper, mareko, bogner, rampitec, nhaehnle, arsenm
Reviewed By: spatel, rampitec, nhaehnle
Subscribers: wdng, nhaehnle, llvm-commits
Differential Revision: https://reviews.llvm.org/D47980
llvm-svn: 334818
2018-06-15 17:56:52 +08:00
|
|
|
|
|
|
|
// Transform (x << y) >> y to x & (-1 >> y)
|
|
|
|
Value *X;
|
|
|
|
if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))))) {
|
|
|
|
Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
|
|
|
|
Value *Mask = Builder.CreateLShr(AllOnes, Op1);
|
|
|
|
return BinaryOperator::CreateAnd(Mask, X);
|
|
|
|
}
|
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
|
2018-06-22 01:06:36 +08:00
|
|
|
if (Value *V = SimplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
|
|
|
|
SQ.getWithInstruction(&I)))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2011-01-14 08:37:45 +08:00
|
|
|
|
2018-10-03 23:20:58 +08:00
|
|
|
if (Instruction *X = foldVectorBinop(I))
|
2018-06-03 00:27:44 +08:00
|
|
|
return X;
|
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
if (Instruction *R = commonShiftTransforms(I))
|
|
|
|
return R;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2018-06-22 01:06:36 +08:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2017-01-31 01:19:32 +08:00
|
|
|
Type *Ty = I.getType();
|
|
|
|
unsigned BitWidth = Ty->getScalarSizeInBits();
|
2017-01-16 00:38:19 +08:00
|
|
|
const APInt *ShAmtAPInt;
|
2018-01-09 22:23:46 +08:00
|
|
|
if (match(Op1, m_APInt(ShAmtAPInt)) && ShAmtAPInt->ult(BitWidth)) {
|
2017-01-16 00:38:19 +08:00
|
|
|
unsigned ShAmt = ShAmtAPInt->getZExtValue();
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2017-01-15 07:13:50 +08:00
|
|
|
// If the shift amount equals the difference in width of the destination
|
2017-01-16 00:38:19 +08:00
|
|
|
// and source scalar types:
|
2017-01-15 07:13:50 +08:00
|
|
|
// ashr (shl (zext X), C), C --> sext X
|
2010-01-09 03:04:21 +08:00
|
|
|
Value *X;
|
2017-01-15 07:13:50 +08:00
|
|
|
if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) &&
|
|
|
|
ShAmt == BitWidth - X->getType()->getScalarSizeInBits())
|
2017-01-31 01:19:32 +08:00
|
|
|
return new SExtInst(X, Ty);
|
|
|
|
|
|
|
|
// We can't handle (X << C1) >>s C2. It shifts arbitrary bits in. However,
|
|
|
|
// we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
|
2017-02-02 05:31:34 +08:00
|
|
|
const APInt *ShOp1;
|
2018-01-09 22:23:46 +08:00
|
|
|
if (match(Op0, m_NSWShl(m_Value(X), m_APInt(ShOp1))) &&
|
|
|
|
ShOp1->ult(BitWidth)) {
|
2017-02-02 05:31:34 +08:00
|
|
|
unsigned ShlAmt = ShOp1->getZExtValue();
|
2017-01-31 07:35:52 +08:00
|
|
|
if (ShlAmt < ShAmt) {
|
|
|
|
// (X <<nsw C1) >>s C2 --> X >>s (C2 - C1)
|
|
|
|
Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
|
|
|
|
auto *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff);
|
|
|
|
NewAShr->setIsExact(I.isExact());
|
|
|
|
return NewAShr;
|
|
|
|
}
|
|
|
|
if (ShlAmt > ShAmt) {
|
|
|
|
// (X <<nsw C1) >>s C2 --> X <<nsw (C1 - C2)
|
|
|
|
Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
|
|
|
|
auto *NewShl = BinaryOperator::Create(Instruction::Shl, X, ShiftDiff);
|
|
|
|
NewShl->setHasNoSignedWrap(true);
|
|
|
|
return NewShl;
|
|
|
|
}
|
2017-01-31 01:19:32 +08:00
|
|
|
}
|
2011-02-10 13:36:31 +08:00
|
|
|
|
2018-01-09 22:23:46 +08:00
|
|
|
if (match(Op0, m_AShr(m_Value(X), m_APInt(ShOp1))) &&
|
|
|
|
ShOp1->ult(BitWidth)) {
|
2017-02-02 05:31:34 +08:00
|
|
|
unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
|
|
|
|
// Oversized arithmetic shifts replicate the sign bit.
|
|
|
|
AmtSum = std::min(AmtSum, BitWidth - 1);
|
|
|
|
// (X >>s C1) >>s C2 --> X >>s (C1 + C2)
|
|
|
|
return BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
|
|
|
|
}
|
|
|
|
|
[InstCombine] sink sext after ashr
Narrow ops are better for bit-tracking, and in the case of vectors,
may enable better codegen.
As the trunc test shows, this can allow follow-on simplifications.
There's a block of code in visitTrunc that deals with shifted ops
with FIXME comments. It may be possible to remove some of that now,
but I want to make sure there are no problems with this step first.
http://rise4fun.com/Alive/Y3a
Name: hoist_ashr_ahead_of_sext_1
%s = sext i8 %x to i32
%r = ashr i32 %s, 3 ; shift value is < than source bit width
=>
%a = ashr i8 %x, 3
%r = sext i8 %a to i32
Name: hoist_ashr_ahead_of_sext_2
%s = sext i8 %x to i32
%r = ashr i32 %s, 8 ; shift value is >= than source bit width
=>
%a = ashr i8 %x, 7 ; so clamp this shift value
%r = sext i8 %a to i32
Name: junc_the_trunc
%a = sext i16 %v to i32
%s = ashr i32 %a, 18
%t = trunc i32 %s to i16
=>
%t = ashr i16 %v, 15
llvm-svn: 310942
2017-08-16 02:25:52 +08:00
|
|
|
if (match(Op0, m_OneUse(m_SExt(m_Value(X)))) &&
|
|
|
|
(Ty->isVectorTy() || shouldChangeType(Ty, X->getType()))) {
|
|
|
|
// ashr (sext X), C --> sext (ashr X, C')
|
|
|
|
Type *SrcTy = X->getType();
|
|
|
|
ShAmt = std::min(ShAmt, SrcTy->getScalarSizeInBits() - 1);
|
|
|
|
Value *NewSh = Builder.CreateAShr(X, ConstantInt::get(SrcTy, ShAmt));
|
|
|
|
return new SExtInst(NewSh, Ty);
|
|
|
|
}
|
|
|
|
|
2011-02-10 13:36:31 +08:00
|
|
|
// If the shifted-out value is known-zero, then this is an exact shift.
|
2012-12-09 23:37:46 +08:00
|
|
|
if (!I.isExact() &&
|
2017-01-15 07:13:50 +08:00
|
|
|
MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
|
2011-02-10 13:36:31 +08:00
|
|
|
I.setIsExact();
|
|
|
|
return &I;
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
}
|
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
// See if we can turn a signed shr into an unsigned shr.
|
2017-04-21 00:56:25 +08:00
|
|
|
if (MaskedValueIsZero(Op0, APInt::getSignMask(BitWidth), 0, &I))
|
2010-01-09 03:04:21 +08:00
|
|
|
return BinaryOperator::CreateLShr(Op0, Op1);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|