2010-01-05 15:44:46 +08:00
|
|
|
//===- InstCombineShifts.cpp ----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
|
|
|
|
assert(I.getOperand(1)->getType() == I.getOperand(0)->getType());
|
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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.
|
|
|
|
Value *A; const APInt *B;
|
|
|
|
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??
|
|
|
|
Value *Rem = Builder->CreateAnd(A, ConstantInt::get(I.getType(), *B-1),
|
|
|
|
Op1->getName());
|
|
|
|
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
|
|
|
|
/// that have constant shift amounts.
|
2016-04-12 00:11:07 +08:00
|
|
|
static bool canEvaluateShiftedShift(unsigned FirstShiftAmt,
|
|
|
|
bool IsFirstShiftLeft,
|
|
|
|
Instruction *SecondShift, InstCombiner &IC,
|
2016-04-11 23:43:41 +08:00
|
|
|
Instruction *CxtI) {
|
2016-04-12 01:27:44 +08:00
|
|
|
assert(SecondShift->isLogicalShift() && "Unexpected instruction type");
|
2016-04-12 01:35:57 +08:00
|
|
|
|
2016-04-12 00:11:07 +08:00
|
|
|
// We need constant shifts.
|
|
|
|
auto *SecondShiftConst = dyn_cast<ConstantInt>(SecondShift->getOperand(1));
|
|
|
|
if (!SecondShiftConst)
|
2016-04-11 23:43:41 +08:00
|
|
|
return false;
|
|
|
|
|
2016-04-12 00:11:07 +08:00
|
|
|
unsigned SecondShiftAmt = SecondShiftConst->getZExtValue();
|
2016-04-12 01:11:55 +08:00
|
|
|
bool IsSecondShiftLeft = SecondShift->getOpcode() == Instruction::Shl;
|
2016-04-12 00:11:07 +08:00
|
|
|
|
2016-04-12 01:11:55 +08:00
|
|
|
// We can always fold shl(c1) + shl(c2) -> shl(c1+c2).
|
|
|
|
// We can always fold lshr(c1) + lshr(c2) -> lshr(c1+c2).
|
|
|
|
if (IsFirstShiftLeft == IsSecondShiftLeft)
|
2016-04-11 23:43:41 +08:00
|
|
|
return true;
|
|
|
|
|
2016-04-12 01:11:55 +08:00
|
|
|
// We can always fold lshr(c) + shl(c) -> and(c2).
|
|
|
|
// We can always fold shl(c) + lshr(c) -> and(c2).
|
|
|
|
if (FirstShiftAmt == SecondShiftAmt)
|
2016-04-11 23:43:41 +08:00
|
|
|
return true;
|
|
|
|
|
2016-04-12 00:11:07 +08:00
|
|
|
unsigned TypeWidth = SecondShift->getType()->getScalarSizeInBits();
|
2016-04-11 23:43:41 +08:00
|
|
|
|
2016-04-12 00:11:07 +08:00
|
|
|
// If the 2nd shift is bigger than the 1st, we can fold:
|
2016-04-12 01:11:55 +08:00
|
|
|
// lshr(c1) + shl(c2) -> shl(c3) + and(c4) or
|
|
|
|
// shl(c1) + lshr(c2) -> lshr(c3) + and(c4),
|
2016-04-12 00:11:07 +08:00
|
|
|
// but it isn't profitable unless we know the and'd out bits are already zero.
|
2016-04-12 00:50:32 +08:00
|
|
|
// Also check that the 2nd shift is valid (less than the type width) or we'll
|
|
|
|
// crash trying to produce the bit mask for the 'and'.
|
|
|
|
if (SecondShiftAmt > FirstShiftAmt && SecondShiftAmt < TypeWidth) {
|
2016-04-12 01:11:55 +08:00
|
|
|
unsigned MaskShift = IsSecondShiftLeft ? TypeWidth - SecondShiftAmt
|
|
|
|
: SecondShiftAmt - FirstShiftAmt;
|
2016-04-12 00:11:07 +08:00
|
|
|
APInt Mask = APInt::getLowBitsSet(TypeWidth, FirstShiftAmt) << MaskShift;
|
|
|
|
if (IC.MaskedValueIsZero(SecondShift->getOperand(0), Mask, 0, CxtI))
|
2016-04-11 23:43:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-03 06:34:55 +08:00
|
|
|
/// See if we can compute the specified value, but shifted
|
2010-08-28 06:24:38 +08:00
|
|
|
/// 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:
|
|
|
|
/// %C = shl i128 %A, 64
|
|
|
|
/// %D = shl i128 %B, 96
|
|
|
|
/// %E = or i128 %C, %D
|
|
|
|
/// %F = lshr i128 %E, 64
|
|
|
|
/// where the client will ask if E can be computed shifted right by 64-bits. If
|
|
|
|
/// this succeeds, the GetShiftedValue function will be called to produce the
|
|
|
|
/// value.
|
2016-04-12 01:25:23 +08:00
|
|
|
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))))) {
|
2010-08-28 06:24:38 +08:00
|
|
|
if (CI->getZExtValue() == NumBits) {
|
|
|
|
// 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.
|
2016-04-12 01:25:23 +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();
|
2016-04-12 01:25:23 +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())
|
2016-04-12 01:25:23 +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
|
|
|
}
|
|
|
|
|
2015-11-03 06:34:55 +08:00
|
|
|
/// When CanEvaluateShifted returned true for an expression,
|
2010-08-28 06:24:38 +08:00
|
|
|
/// this value 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)
|
|
|
|
V = IC.Builder->CreateShl(C, NumBits);
|
|
|
|
else
|
|
|
|
V = IC.Builder->CreateLShr(C, NumBits);
|
|
|
|
// 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(
|
|
|
|
0, GetShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
|
|
|
|
I->setOperand(
|
|
|
|
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
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
case Instruction::Shl: {
|
2011-07-29 08:18:19 +08:00
|
|
|
BinaryOperator *BO = cast<BinaryOperator>(I);
|
|
|
|
unsigned TypeWidth = BO->getType()->getScalarSizeInBits();
|
2010-08-28 06:24:38 +08:00
|
|
|
|
|
|
|
// We only accept shifts-by-a-constant in CanEvaluateShifted.
|
2011-07-29 08:18:19 +08:00
|
|
|
ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
|
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
// We can always fold shl(c1)+shl(c2) -> shl(c1+c2).
|
|
|
|
if (isLeftShift) {
|
|
|
|
// If this is oversized composite shift, then unsigned shifts get 0.
|
|
|
|
unsigned NewShAmt = NumBits+CI->getZExtValue();
|
|
|
|
if (NewShAmt >= TypeWidth)
|
|
|
|
return Constant::getNullValue(I->getType());
|
|
|
|
|
2011-07-29 08:18:19 +08:00
|
|
|
BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt));
|
|
|
|
BO->setHasNoUnsignedWrap(false);
|
|
|
|
BO->setHasNoSignedWrap(false);
|
2010-08-28 06:24:38 +08:00
|
|
|
return I;
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
// We turn shl(c)+lshr(c) -> and(c2) if the input doesn't already have
|
|
|
|
// zeros.
|
2010-08-28 06:53:44 +08:00
|
|
|
if (CI->getValue() == NumBits) {
|
|
|
|
APInt Mask(APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits));
|
2011-07-29 08:18:19 +08:00
|
|
|
V = IC.Builder->CreateAnd(BO->getOperand(0),
|
|
|
|
ConstantInt::get(BO->getContext(), Mask));
|
2010-08-28 06:53:44 +08:00
|
|
|
if (Instruction *VI = dyn_cast<Instruction>(V)) {
|
2011-07-29 08:18:19 +08:00
|
|
|
VI->moveBefore(BO);
|
|
|
|
VI->takeName(BO);
|
2010-08-28 06:53:44 +08:00
|
|
|
}
|
|
|
|
return V;
|
2010-08-28 06:24:38 +08:00
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:53:44 +08:00
|
|
|
// We turn shl(c1)+shr(c2) -> shl(c3)+and(c4), but only when we know that
|
|
|
|
// the and won't be needed.
|
|
|
|
assert(CI->getZExtValue() > NumBits);
|
2011-07-29 08:18:19 +08:00
|
|
|
BO->setOperand(1, ConstantInt::get(BO->getType(),
|
|
|
|
CI->getZExtValue() - NumBits));
|
|
|
|
BO->setHasNoUnsignedWrap(false);
|
|
|
|
BO->setHasNoSignedWrap(false);
|
|
|
|
return BO;
|
2010-08-28 06:24:38 +08:00
|
|
|
}
|
2016-04-12 01:35:57 +08:00
|
|
|
// FIXME: This is almost identical to the SHL case. Refactor both cases into
|
|
|
|
// a helper function.
|
2010-08-28 06:24:38 +08:00
|
|
|
case Instruction::LShr: {
|
2011-07-29 08:18:19 +08:00
|
|
|
BinaryOperator *BO = cast<BinaryOperator>(I);
|
|
|
|
unsigned TypeWidth = BO->getType()->getScalarSizeInBits();
|
2010-08-28 06:24:38 +08:00
|
|
|
// We only accept shifts-by-a-constant in CanEvaluateShifted.
|
2011-07-29 08:18:19 +08:00
|
|
|
ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
// We can always fold lshr(c1)+lshr(c2) -> lshr(c1+c2).
|
|
|
|
if (!isLeftShift) {
|
|
|
|
// If this is oversized composite shift, then unsigned shifts get 0.
|
|
|
|
unsigned NewShAmt = NumBits+CI->getZExtValue();
|
|
|
|
if (NewShAmt >= TypeWidth)
|
2011-07-29 08:18:19 +08:00
|
|
|
return Constant::getNullValue(BO->getType());
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2011-07-29 08:18:19 +08:00
|
|
|
BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt));
|
|
|
|
BO->setIsExact(false);
|
2010-08-28 06:24:38 +08:00
|
|
|
return I;
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:24:38 +08:00
|
|
|
// We turn lshr(c)+shl(c) -> and(c2) if the input doesn't already have
|
|
|
|
// zeros.
|
2010-08-28 06:53:44 +08:00
|
|
|
if (CI->getValue() == NumBits) {
|
|
|
|
APInt Mask(APInt::getHighBitsSet(TypeWidth, TypeWidth - NumBits));
|
|
|
|
V = IC.Builder->CreateAnd(I->getOperand(0),
|
2011-07-29 08:18:19 +08:00
|
|
|
ConstantInt::get(BO->getContext(), Mask));
|
2010-08-28 06:53:44 +08:00
|
|
|
if (Instruction *VI = dyn_cast<Instruction>(V)) {
|
|
|
|
VI->moveBefore(I);
|
|
|
|
VI->takeName(I);
|
|
|
|
}
|
|
|
|
return V;
|
2010-08-28 06:24:38 +08:00
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-08-28 06:53:44 +08:00
|
|
|
// We turn lshr(c1)+shl(c2) -> lshr(c3)+and(c4), but only when we know that
|
|
|
|
// the and won't be needed.
|
|
|
|
assert(CI->getZExtValue() > NumBits);
|
2011-07-29 08:18:19 +08:00
|
|
|
BO->setOperand(1, ConstantInt::get(BO->getType(),
|
|
|
|
CI->getZExtValue() - NumBits));
|
|
|
|
BO->setIsExact(false);
|
|
|
|
return BO;
|
2010-08-28 06:24:38 +08:00
|
|
|
}
|
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(
|
|
|
|
1, GetShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
|
|
|
|
I->setOperand(
|
|
|
|
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)
|
2015-03-10 10:37:25 +08:00
|
|
|
PN->setIncomingValue(i, GetShiftedValue(PN->getIncomingValue(i), NumBits,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-04-15 05:50:37 +08:00
|
|
|
ConstantInt *COp1 = nullptr;
|
|
|
|
if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(Op1))
|
|
|
|
COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
|
|
|
|
else if (ConstantVector *CV = dyn_cast<ConstantVector>(Op1))
|
|
|
|
COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
|
|
|
|
else
|
|
|
|
COp1 = dyn_cast<ConstantInt>(Op1);
|
|
|
|
|
|
|
|
if (!COp1)
|
|
|
|
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 &&
|
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
|
|
|
CanEvaluateShifted(Op0, COp1->getZExtValue(), isLeftShift, *this, &I)) {
|
optimize bitcasts from large integers to vector into vector
element insertion from the pieces that feed into the vector.
This handles a pattern that occurs frequently due to code
generated for the x86-64 abi. We now compile something like
this:
struct S { float A, B, C, D; };
struct S g;
struct S bar() {
struct S A = g;
++A.A;
++A.C;
return A;
}
into all nice vector operations:
_bar: ## @bar
## BB#0: ## %entry
movq _g@GOTPCREL(%rip), %rax
movss LCPI1_0(%rip), %xmm1
movss (%rax), %xmm0
addss %xmm1, %xmm0
pshufd $16, %xmm0, %xmm0
movss 4(%rax), %xmm2
movss 12(%rax), %xmm3
pshufd $16, %xmm2, %xmm2
unpcklps %xmm2, %xmm0
addss 8(%rax), %xmm1
pshufd $16, %xmm1, %xmm1
pshufd $16, %xmm3, %xmm2
unpcklps %xmm2, %xmm1
ret
instead of icky integer operations:
_bar: ## @bar
movq _g@GOTPCREL(%rip), %rax
movss LCPI1_0(%rip), %xmm1
movss (%rax), %xmm0
addss %xmm1, %xmm0
movd %xmm0, %ecx
movl 4(%rax), %edx
movl 12(%rax), %esi
shlq $32, %rdx
addq %rcx, %rdx
movd %rdx, %xmm0
addss 8(%rax), %xmm1
movd %xmm1, %eax
shlq $32, %rsi
addq %rax, %rsi
movd %rsi, %xmm1
ret
This resolves rdar://8360454
llvm-svn: 112343
2010-08-28 09:20:38 +08:00
|
|
|
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(
|
2015-03-10 10:37:25 +08:00
|
|
|
I, GetShiftedValue(Op0, COp1->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.
|
|
|
|
uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2014-04-24 00:48:40 +08:00
|
|
|
assert(!COp1->uge(TypeBits) &&
|
|
|
|
"Shift over the type width should have been removed already");
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
// ((X*C1) << C2) == (X * (C1 << C2))
|
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
|
|
|
|
if (BO->getOpcode() == Instruction::Mul && isLeftShift)
|
|
|
|
if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
|
|
|
|
return BinaryOperator::CreateMul(BO->getOperand(0),
|
2016-02-01 00:34:11 +08:00
|
|
|
ConstantExpr::getShl(BOOp, Op1));
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
// Try to fold constant and into select arguments.
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
|
|
|
|
if (Instruction *R = FoldOpIntoSelect(I, SI))
|
|
|
|
return R;
|
|
|
|
if (isa<PHINode>(Op0))
|
|
|
|
if (Instruction *NV = FoldOpIntoPhi(I))
|
|
|
|
return NV;
|
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.
|
2014-04-15 05:50:37 +08:00
|
|
|
Constant *ShAmt = ConstantExpr::getZExt(COp1, TrOp->getType());
|
2010-01-05 15:44:46 +08:00
|
|
|
// (shift2 (shift1 & 0x00FF), c2)
|
|
|
|
Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName());
|
|
|
|
|
|
|
|
// 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)
|
2014-04-15 05:50:37 +08:00
|
|
|
MaskV <<= COp1->getZExtValue();
|
2010-01-05 15:44:46 +08:00
|
|
|
else {
|
|
|
|
assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
|
2014-04-15 05:50:37 +08:00
|
|
|
MaskV = MaskV.lshr(COp1->getZExtValue());
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// shift1 & 0x00FF
|
|
|
|
Value *And = Builder->CreateAnd(NSh,
|
|
|
|
ConstantInt::get(I.getContext(), MaskV),
|
|
|
|
TI->getName());
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
|
|
|
|
// (X + (Y << C))
|
|
|
|
Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1,
|
|
|
|
Op0BO->getOperand(1)->getName());
|
2014-04-15 05:50:37 +08:00
|
|
|
uint32_t Op1Val = COp1->getLimitedValue(TypeBits);
|
|
|
|
|
|
|
|
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)
|
|
|
|
Builder->CreateShl(Op0BO->getOperand(0), Op1,
|
|
|
|
Op0BO->getName());
|
|
|
|
// X & (CC << C)
|
|
|
|
Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
|
|
|
|
V1->getName()+".mask");
|
|
|
|
return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
|
|
|
|
}
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-10 14:59:55 +08:00
|
|
|
// FALL THROUGH.
|
|
|
|
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)
|
|
|
|
Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
|
|
|
|
// (X + (Y << C))
|
|
|
|
Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS,
|
|
|
|
Op0BO->getOperand(0)->getName());
|
2014-04-15 05:50:37 +08:00
|
|
|
uint32_t Op1Val = COp1->getLimitedValue(TypeBits);
|
|
|
|
|
|
|
|
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)
|
|
|
|
Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
|
|
|
|
// X & (CC << C)
|
|
|
|
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.
|
|
|
|
if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
|
|
|
|
bool isValid = true; // Valid only for And, Or, Xor
|
|
|
|
bool highBitSet = false; // Transform if high bit of constant set?
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
switch (Op0BO->getOpcode()) {
|
2010-01-10 14:59:55 +08:00
|
|
|
default: isValid = false; break; // Do not perform transform!
|
|
|
|
case Instruction::Add:
|
|
|
|
isValid = isLeftShift;
|
|
|
|
break;
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
|
|
|
highBitSet = false;
|
|
|
|
break;
|
|
|
|
case Instruction::And:
|
|
|
|
highBitSet = true;
|
|
|
|
break;
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
// If this is a signed shift right, and the high bit is modified
|
|
|
|
// by the logical operation, do not perform the transformation.
|
|
|
|
// The highBitSet boolean indicates the value of the high bit of
|
|
|
|
// the constant which would cause it to be modified for this
|
|
|
|
// operation.
|
|
|
|
//
|
|
|
|
if (isValid && I.getOpcode() == Instruction::AShr)
|
|
|
|
isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
if (isValid) {
|
|
|
|
Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
Value *NewShift =
|
|
|
|
Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
// Find out if this is a shift of a shift by a constant.
|
|
|
|
BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
|
|
|
|
if (ShiftOp && !ShiftOp->isShift())
|
2014-04-25 13:29:35 +08:00
|
|
|
ShiftOp = nullptr;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) {
|
Reapply r155136 after fixing PR12599.
Original commit message:
Defer some shl transforms to DAGCombine.
The shl instruction is used to represent multiplication by a constant
power of two as well as bitwise left shifts. Some InstCombine
transformations would turn an shl instruction into a bit mask operation,
making it difficult for later analysis passes to recognize the
constsnt multiplication.
Disable those shl transformations, deferring them to DAGCombine time.
An 'shl X, C' instruction is now treated mostly the same was as 'mul X, C'.
These transformations are deferred:
(X >>? C) << C --> X & (-1 << C) (When X >> C has multiple uses)
(X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) (When C2 > C1)
(X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) (When C1 > C2)
The corresponding exact transformations are preserved, just like
div-exact + mul:
(X >>?,exact C) << C --> X
(X >>?,exact C1) << C2 --> X << (C2-C1)
(X >>?,exact C1) << C2 --> X >>?,exact (C1-C2)
The disabled transformations could also prevent the instruction selector
from recognizing rotate patterns in hash functions and cryptographic
primitives. I have a test case for that, but it is too fragile.
llvm-svn: 155362
2012-04-24 01:39:52 +08:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
|
|
|
|
uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
|
2014-04-15 05:50:37 +08:00
|
|
|
uint32_t ShiftAmt2 = COp1->getLimitedValue(TypeBits);
|
2010-01-05 15:44:46 +08:00
|
|
|
assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
|
2014-04-25 13:29:35 +08:00
|
|
|
if (ShiftAmt1 == 0) return nullptr; // Will be simplified in the future.
|
2010-01-05 15:44:46 +08:00
|
|
|
Value *X = ShiftOp->getOperand(0);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
IntegerType *Ty = cast<IntegerType>(I.getType());
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
// Check for (X << c1) << c2 and (X >> c1) >> c2
|
|
|
|
if (I.getOpcode() == ShiftOp->getOpcode()) {
|
2012-01-01 05:30:22 +08:00
|
|
|
uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift.
|
2010-01-05 15:44:46 +08:00
|
|
|
// If this is oversized composite shift, then unsigned shifts get 0, ashr
|
|
|
|
// saturates.
|
|
|
|
if (AmtSum >= TypeBits) {
|
|
|
|
if (I.getOpcode() != Instruction::AShr)
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, Constant::getNullValue(I.getType()));
|
2010-01-05 15:44:46 +08:00
|
|
|
AmtSum = TypeBits-1; // Saturate to 31 for i32 ashr.
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
return BinaryOperator::Create(I.getOpcode(), X,
|
|
|
|
ConstantInt::get(Ty, AmtSum));
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
if (ShiftAmt1 == ShiftAmt2) {
|
|
|
|
// If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
|
2010-08-28 05:04:34 +08:00
|
|
|
if (I.getOpcode() == Instruction::LShr &&
|
|
|
|
ShiftOp->getOpcode() == Instruction::Shl) {
|
2010-01-05 15:44:46 +08:00
|
|
|
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
|
|
|
|
return BinaryOperator::CreateAnd(X,
|
|
|
|
ConstantInt::get(I.getContext(), Mask));
|
|
|
|
}
|
|
|
|
} else if (ShiftAmt1 < ShiftAmt2) {
|
|
|
|
uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
|
Reapply r155136 after fixing PR12599.
Original commit message:
Defer some shl transforms to DAGCombine.
The shl instruction is used to represent multiplication by a constant
power of two as well as bitwise left shifts. Some InstCombine
transformations would turn an shl instruction into a bit mask operation,
making it difficult for later analysis passes to recognize the
constsnt multiplication.
Disable those shl transformations, deferring them to DAGCombine time.
An 'shl X, C' instruction is now treated mostly the same was as 'mul X, C'.
These transformations are deferred:
(X >>? C) << C --> X & (-1 << C) (When X >> C has multiple uses)
(X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) (When C2 > C1)
(X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) (When C1 > C2)
The corresponding exact transformations are preserved, just like
div-exact + mul:
(X >>?,exact C) << C --> X
(X >>?,exact C1) << C2 --> X << (C2-C1)
(X >>?,exact C1) << C2 --> X >>?,exact (C1-C2)
The disabled transformations could also prevent the instruction selector
from recognizing rotate patterns in hash functions and cryptographic
primitives. I have a test case for that, but it is too fragile.
llvm-svn: 155362
2012-04-24 01:39:52 +08:00
|
|
|
|
|
|
|
// (X >>?,exact C1) << C2 --> X << (C2-C1)
|
|
|
|
// The inexact version is deferred to DAGCombine so we don't hide shl
|
|
|
|
// behind a bit mask.
|
2010-08-28 05:04:34 +08:00
|
|
|
if (I.getOpcode() == Instruction::Shl &&
|
Reapply r155136 after fixing PR12599.
Original commit message:
Defer some shl transforms to DAGCombine.
The shl instruction is used to represent multiplication by a constant
power of two as well as bitwise left shifts. Some InstCombine
transformations would turn an shl instruction into a bit mask operation,
making it difficult for later analysis passes to recognize the
constsnt multiplication.
Disable those shl transformations, deferring them to DAGCombine time.
An 'shl X, C' instruction is now treated mostly the same was as 'mul X, C'.
These transformations are deferred:
(X >>? C) << C --> X & (-1 << C) (When X >> C has multiple uses)
(X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) (When C2 > C1)
(X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) (When C1 > C2)
The corresponding exact transformations are preserved, just like
div-exact + mul:
(X >>?,exact C) << C --> X
(X >>?,exact C1) << C2 --> X << (C2-C1)
(X >>?,exact C1) << C2 --> X >>?,exact (C1-C2)
The disabled transformations could also prevent the instruction selector
from recognizing rotate patterns in hash functions and cryptographic
primitives. I have a test case for that, but it is too fragile.
llvm-svn: 155362
2012-04-24 01:39:52 +08:00
|
|
|
ShiftOp->getOpcode() != Instruction::Shl &&
|
|
|
|
ShiftOp->isExact()) {
|
2010-01-05 15:44:46 +08:00
|
|
|
assert(ShiftOp->getOpcode() == Instruction::LShr ||
|
|
|
|
ShiftOp->getOpcode() == Instruction::AShr);
|
2012-01-04 17:28:29 +08:00
|
|
|
ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
|
Reapply r155136 after fixing PR12599.
Original commit message:
Defer some shl transforms to DAGCombine.
The shl instruction is used to represent multiplication by a constant
power of two as well as bitwise left shifts. Some InstCombine
transformations would turn an shl instruction into a bit mask operation,
making it difficult for later analysis passes to recognize the
constsnt multiplication.
Disable those shl transformations, deferring them to DAGCombine time.
An 'shl X, C' instruction is now treated mostly the same was as 'mul X, C'.
These transformations are deferred:
(X >>? C) << C --> X & (-1 << C) (When X >> C has multiple uses)
(X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) (When C2 > C1)
(X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) (When C1 > C2)
The corresponding exact transformations are preserved, just like
div-exact + mul:
(X >>?,exact C) << C --> X
(X >>?,exact C1) << C2 --> X << (C2-C1)
(X >>?,exact C1) << C2 --> X >>?,exact (C1-C2)
The disabled transformations could also prevent the instruction selector
from recognizing rotate patterns in hash functions and cryptographic
primitives. I have a test case for that, but it is too fragile.
llvm-svn: 155362
2012-04-24 01:39:52 +08:00
|
|
|
BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
|
|
|
|
X, ShiftDiffCst);
|
|
|
|
NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
|
|
|
|
NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
|
|
|
|
return NewShl;
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
Reapply r155136 after fixing PR12599.
Original commit message:
Defer some shl transforms to DAGCombine.
The shl instruction is used to represent multiplication by a constant
power of two as well as bitwise left shifts. Some InstCombine
transformations would turn an shl instruction into a bit mask operation,
making it difficult for later analysis passes to recognize the
constsnt multiplication.
Disable those shl transformations, deferring them to DAGCombine time.
An 'shl X, C' instruction is now treated mostly the same was as 'mul X, C'.
These transformations are deferred:
(X >>? C) << C --> X & (-1 << C) (When X >> C has multiple uses)
(X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) (When C2 > C1)
(X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) (When C1 > C2)
The corresponding exact transformations are preserved, just like
div-exact + mul:
(X >>?,exact C) << C --> X
(X >>?,exact C1) << C2 --> X << (C2-C1)
(X >>?,exact C1) << C2 --> X >>?,exact (C1-C2)
The disabled transformations could also prevent the instruction selector
from recognizing rotate patterns in hash functions and cryptographic
primitives. I have a test case for that, but it is too fragile.
llvm-svn: 155362
2012-04-24 01:39:52 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
// (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2)
|
2010-08-28 05:04:34 +08:00
|
|
|
if (I.getOpcode() == Instruction::LShr &&
|
|
|
|
ShiftOp->getOpcode() == Instruction::Shl) {
|
2012-01-04 17:28:29 +08:00
|
|
|
ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
|
|
|
|
// (X <<nuw C1) >>u C2 --> X >>u (C2-C1)
|
|
|
|
if (ShiftOp->hasNoUnsignedWrap()) {
|
|
|
|
BinaryOperator *NewLShr = BinaryOperator::Create(Instruction::LShr,
|
|
|
|
X, ShiftDiffCst);
|
|
|
|
NewLShr->setIsExact(I.isExact());
|
|
|
|
return NewLShr;
|
|
|
|
}
|
|
|
|
Value *Shift = Builder->CreateLShr(X, ShiftDiffCst);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
|
|
|
|
return BinaryOperator::CreateAnd(Shift,
|
|
|
|
ConstantInt::get(I.getContext(),Mask));
|
|
|
|
}
|
2012-01-04 17:28:29 +08:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (I.getOpcode() == Instruction::AShr &&
|
|
|
|
ShiftOp->getOpcode() == Instruction::Shl) {
|
|
|
|
if (ShiftOp->hasNoSignedWrap()) {
|
|
|
|
// (X <<nsw C1) >>s C2 --> X >>s (C2-C1)
|
|
|
|
ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
|
|
|
|
BinaryOperator *NewAShr = BinaryOperator::Create(Instruction::AShr,
|
|
|
|
X, ShiftDiffCst);
|
|
|
|
NewAShr->setIsExact(I.isExact());
|
|
|
|
return NewAShr;
|
|
|
|
}
|
|
|
|
}
|
2010-01-05 15:44:46 +08:00
|
|
|
} else {
|
|
|
|
assert(ShiftAmt2 < ShiftAmt1);
|
|
|
|
uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
|
|
|
|
|
Reapply r155136 after fixing PR12599.
Original commit message:
Defer some shl transforms to DAGCombine.
The shl instruction is used to represent multiplication by a constant
power of two as well as bitwise left shifts. Some InstCombine
transformations would turn an shl instruction into a bit mask operation,
making it difficult for later analysis passes to recognize the
constsnt multiplication.
Disable those shl transformations, deferring them to DAGCombine time.
An 'shl X, C' instruction is now treated mostly the same was as 'mul X, C'.
These transformations are deferred:
(X >>? C) << C --> X & (-1 << C) (When X >> C has multiple uses)
(X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) (When C2 > C1)
(X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) (When C1 > C2)
The corresponding exact transformations are preserved, just like
div-exact + mul:
(X >>?,exact C) << C --> X
(X >>?,exact C1) << C2 --> X << (C2-C1)
(X >>?,exact C1) << C2 --> X >>?,exact (C1-C2)
The disabled transformations could also prevent the instruction selector
from recognizing rotate patterns in hash functions and cryptographic
primitives. I have a test case for that, but it is too fragile.
llvm-svn: 155362
2012-04-24 01:39:52 +08:00
|
|
|
// (X >>?exact C1) << C2 --> X >>?exact (C1-C2)
|
|
|
|
// The inexact version is deferred to DAGCombine so we don't hide shl
|
|
|
|
// behind a bit mask.
|
2010-08-28 05:04:34 +08:00
|
|
|
if (I.getOpcode() == Instruction::Shl &&
|
Reapply r155136 after fixing PR12599.
Original commit message:
Defer some shl transforms to DAGCombine.
The shl instruction is used to represent multiplication by a constant
power of two as well as bitwise left shifts. Some InstCombine
transformations would turn an shl instruction into a bit mask operation,
making it difficult for later analysis passes to recognize the
constsnt multiplication.
Disable those shl transformations, deferring them to DAGCombine time.
An 'shl X, C' instruction is now treated mostly the same was as 'mul X, C'.
These transformations are deferred:
(X >>? C) << C --> X & (-1 << C) (When X >> C has multiple uses)
(X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) (When C2 > C1)
(X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) (When C1 > C2)
The corresponding exact transformations are preserved, just like
div-exact + mul:
(X >>?,exact C) << C --> X
(X >>?,exact C1) << C2 --> X << (C2-C1)
(X >>?,exact C1) << C2 --> X >>?,exact (C1-C2)
The disabled transformations could also prevent the instruction selector
from recognizing rotate patterns in hash functions and cryptographic
primitives. I have a test case for that, but it is too fragile.
llvm-svn: 155362
2012-04-24 01:39:52 +08:00
|
|
|
ShiftOp->getOpcode() != Instruction::Shl &&
|
|
|
|
ShiftOp->isExact()) {
|
2012-01-01 05:30:22 +08:00
|
|
|
ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
|
Reapply r155136 after fixing PR12599.
Original commit message:
Defer some shl transforms to DAGCombine.
The shl instruction is used to represent multiplication by a constant
power of two as well as bitwise left shifts. Some InstCombine
transformations would turn an shl instruction into a bit mask operation,
making it difficult for later analysis passes to recognize the
constsnt multiplication.
Disable those shl transformations, deferring them to DAGCombine time.
An 'shl X, C' instruction is now treated mostly the same was as 'mul X, C'.
These transformations are deferred:
(X >>? C) << C --> X & (-1 << C) (When X >> C has multiple uses)
(X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) (When C2 > C1)
(X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) (When C1 > C2)
The corresponding exact transformations are preserved, just like
div-exact + mul:
(X >>?,exact C) << C --> X
(X >>?,exact C1) << C2 --> X << (C2-C1)
(X >>?,exact C1) << C2 --> X >>?,exact (C1-C2)
The disabled transformations could also prevent the instruction selector
from recognizing rotate patterns in hash functions and cryptographic
primitives. I have a test case for that, but it is too fragile.
llvm-svn: 155362
2012-04-24 01:39:52 +08:00
|
|
|
BinaryOperator *NewShr = BinaryOperator::Create(ShiftOp->getOpcode(),
|
|
|
|
X, ShiftDiffCst);
|
|
|
|
NewShr->setIsExact(true);
|
|
|
|
return NewShr;
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
Reapply r155136 after fixing PR12599.
Original commit message:
Defer some shl transforms to DAGCombine.
The shl instruction is used to represent multiplication by a constant
power of two as well as bitwise left shifts. Some InstCombine
transformations would turn an shl instruction into a bit mask operation,
making it difficult for later analysis passes to recognize the
constsnt multiplication.
Disable those shl transformations, deferring them to DAGCombine time.
An 'shl X, C' instruction is now treated mostly the same was as 'mul X, C'.
These transformations are deferred:
(X >>? C) << C --> X & (-1 << C) (When X >> C has multiple uses)
(X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) (When C2 > C1)
(X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) (When C1 > C2)
The corresponding exact transformations are preserved, just like
div-exact + mul:
(X >>?,exact C) << C --> X
(X >>?,exact C1) << C2 --> X << (C2-C1)
(X >>?,exact C1) << C2 --> X >>?,exact (C1-C2)
The disabled transformations could also prevent the instruction selector
from recognizing rotate patterns in hash functions and cryptographic
primitives. I have a test case for that, but it is too fragile.
llvm-svn: 155362
2012-04-24 01:39:52 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
// (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2)
|
2010-08-28 05:04:34 +08:00
|
|
|
if (I.getOpcode() == Instruction::LShr &&
|
|
|
|
ShiftOp->getOpcode() == Instruction::Shl) {
|
2012-01-04 17:28:29 +08:00
|
|
|
ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
|
|
|
|
if (ShiftOp->hasNoUnsignedWrap()) {
|
|
|
|
// (X <<nuw C1) >>u C2 --> X <<nuw (C1-C2)
|
|
|
|
BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
|
|
|
|
X, ShiftDiffCst);
|
|
|
|
NewShl->setHasNoUnsignedWrap(true);
|
|
|
|
return NewShl;
|
|
|
|
}
|
|
|
|
Value *Shift = Builder->CreateShl(X, ShiftDiffCst);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
|
|
|
|
return BinaryOperator::CreateAnd(Shift,
|
|
|
|
ConstantInt::get(I.getContext(),Mask));
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2012-01-04 17:28:29 +08:00
|
|
|
// 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.
|
|
|
|
if (I.getOpcode() == Instruction::AShr &&
|
|
|
|
ShiftOp->getOpcode() == Instruction::Shl) {
|
|
|
|
if (ShiftOp->hasNoSignedWrap()) {
|
|
|
|
// (X <<nsw C1) >>s C2 --> X <<nsw (C1-C2)
|
|
|
|
ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
|
|
|
|
BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
|
|
|
|
X, ShiftDiffCst);
|
|
|
|
NewShl->setHasNoSignedWrap(true);
|
|
|
|
return NewShl;
|
|
|
|
}
|
|
|
|
}
|
2010-01-05 15:44: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) {
|
2014-05-11 16:46:12 +08:00
|
|
|
if (Value *V = SimplifyVectorOp(I))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2014-05-11 16:46:12 +08:00
|
|
|
|
2015-01-04 20:03:27 +08:00
|
|
|
if (Value *V =
|
|
|
|
SimplifyShlInst(I.getOperand(0), I.getOperand(1), I.hasNoSignedWrap(),
|
2016-08-05 09:06:44 +08:00
|
|
|
I.hasNoUnsignedWrap(), DL, &TLI, &DT, &AC))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2011-02-10 13:36:31 +08:00
|
|
|
if (Instruction *V = commonShiftTransforms(I))
|
|
|
|
return V;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2011-02-10 13:36:31 +08:00
|
|
|
if (ConstantInt *Op1C = dyn_cast<ConstantInt>(I.getOperand(1))) {
|
|
|
|
unsigned ShAmt = Op1C->getZExtValue();
|
2012-12-09 23:37:46 +08:00
|
|
|
|
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() &&
|
2011-02-10 13:36:31 +08:00
|
|
|
MaskedValueIsZero(I.getOperand(0),
|
2016-02-01 00:34:11 +08:00
|
|
|
APInt::getHighBitsSet(Op1C->getBitWidth(), ShAmt), 0,
|
|
|
|
&I)) {
|
|
|
|
I.setHasNoUnsignedWrap();
|
|
|
|
return &I;
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2011-02-10 13:36:31 +08:00
|
|
|
// If the shifted out value is all signbits, this is a NSW shift.
|
|
|
|
if (!I.hasNoSignedWrap() &&
|
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
|
|
|
ComputeNumSignBits(I.getOperand(0), 0, &I) > ShAmt) {
|
2011-02-10 13:36:31 +08:00
|
|
|
I.setHasNoSignedWrap();
|
|
|
|
return &I;
|
|
|
|
}
|
|
|
|
}
|
2011-04-29 16:15:41 +08:00
|
|
|
|
2011-04-29 16:41:23 +08:00
|
|
|
// (C1 << A) << C2 -> (C1 << C2) << A
|
2011-04-29 16:15:41 +08:00
|
|
|
Constant *C1, *C2;
|
|
|
|
Value *A;
|
|
|
|
if (match(I.getOperand(0), m_OneUse(m_Shl(m_Constant(C1), m_Value(A)))) &&
|
|
|
|
match(I.getOperand(1), m_Constant(C2)))
|
|
|
|
return BinaryOperator::CreateShl(ConstantExpr::getShl(C1, C2), A);
|
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2010-01-05 15:44:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
|
2014-05-11 16:46:12 +08:00
|
|
|
if (Value *V = SimplifyVectorOp(I))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2014-05-11 16:46:12 +08:00
|
|
|
|
2015-01-04 20:03:27 +08:00
|
|
|
if (Value *V = SimplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
|
2016-08-05 09:06:44 +08:00
|
|
|
DL, &TLI, &DT, &AC))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2011-01-14 08:37:45 +08:00
|
|
|
|
2010-01-24 02:49:30 +08:00
|
|
|
if (Instruction *R = commonShiftTransforms(I))
|
|
|
|
return R;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-24 02:49:30 +08:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2011-02-10 13:36:31 +08:00
|
|
|
if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
|
|
|
|
unsigned ShAmt = Op1C->getZExtValue();
|
|
|
|
|
2010-01-24 02:49:30 +08:00
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) {
|
2010-01-24 07:31:46 +08:00
|
|
|
unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
|
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)
|
|
|
|
if ((II->getIntrinsicID() == Intrinsic::ctlz ||
|
|
|
|
II->getIntrinsicID() == Intrinsic::cttz ||
|
|
|
|
II->getIntrinsicID() == Intrinsic::ctpop) &&
|
2011-02-10 13:36:31 +08:00
|
|
|
isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt) {
|
2010-01-24 02:49:30 +08:00
|
|
|
bool isCtPop = II->getIntrinsicID() == Intrinsic::ctpop;
|
2010-01-24 07:31:46 +08:00
|
|
|
Constant *RHS = ConstantInt::getSigned(Op0->getType(), isCtPop ? -1:0);
|
2010-06-24 08:44:01 +08:00
|
|
|
Value *Cmp = Builder->CreateICmpEQ(II->getArgOperand(0), RHS);
|
2010-01-24 02:49:30 +08:00
|
|
|
return new ZExtInst(Cmp, II->getType());
|
|
|
|
}
|
|
|
|
}
|
2012-12-09 23:37:46 +08:00
|
|
|
|
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() &&
|
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
|
|
|
MaskedValueIsZero(Op0, APInt::getLowBitsSet(Op1C->getBitWidth(), 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
|
|
|
}
|
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::visitAShr(BinaryOperator &I) {
|
2014-05-11 16:46:12 +08:00
|
|
|
if (Value *V = SimplifyVectorOp(I))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2014-05-11 16:46:12 +08:00
|
|
|
|
2015-01-04 20:03:27 +08:00
|
|
|
if (Value *V = SimplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
|
2016-08-05 09:06:44 +08:00
|
|
|
DL, &TLI, &DT, &AC))
|
2016-02-02 06:23:39 +08:00
|
|
|
return replaceInstUsesWith(I, V);
|
2011-01-14 08:37:45 +08:00
|
|
|
|
2010-01-05 15:44:46 +08:00
|
|
|
if (Instruction *R = commonShiftTransforms(I))
|
|
|
|
return R;
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-09 03:04:21 +08:00
|
|
|
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
|
2011-01-14 08:37:45 +08:00
|
|
|
|
2010-01-09 03:04:21 +08:00
|
|
|
if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
|
2011-02-10 13:36:31 +08:00
|
|
|
unsigned ShAmt = Op1C->getZExtValue();
|
2012-12-09 23:37:46 +08:00
|
|
|
|
2010-01-09 03:04:21 +08:00
|
|
|
// If the input is a SHL by the same constant (ashr (shl X, C), C), then we
|
2010-01-19 06:19:16 +08:00
|
|
|
// have a sign-extend idiom.
|
2010-01-09 03:04:21 +08:00
|
|
|
Value *X;
|
2010-01-19 06:19:16 +08:00
|
|
|
if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1)))) {
|
|
|
|
// If the input is an extension from the shifted amount value, e.g.
|
|
|
|
// %x = zext i8 %A to i32
|
|
|
|
// %y = shl i32 %x, 24
|
|
|
|
// %z = ashr %y, 24
|
|
|
|
// then turn this into "z = sext i8 A to i32".
|
|
|
|
if (ZExtInst *ZI = dyn_cast<ZExtInst>(X)) {
|
|
|
|
uint32_t SrcBits = ZI->getOperand(0)->getType()->getScalarSizeInBits();
|
|
|
|
uint32_t DestBits = ZI->getType()->getScalarSizeInBits();
|
|
|
|
if (Op1C->getZExtValue() == DestBits-SrcBits)
|
|
|
|
return new SExtInst(ZI->getOperand(0), ZI->getType());
|
|
|
|
}
|
|
|
|
}
|
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() &&
|
2016-02-01 00:34:11 +08:00
|
|
|
MaskedValueIsZero(Op0, APInt::getLowBitsSet(Op1C->getBitWidth(), 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.
|
|
|
|
if (MaskedValueIsZero(Op0,
|
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
|
|
|
APInt::getSignBit(I.getType()->getScalarSizeInBits()),
|
|
|
|
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
|
|
|
}
|