2015-01-22 13:25:13 +08:00
|
|
|
//===- InstCombineInternal.h - InstCombine pass internals -------*- C++ -*-===//
|
2010-01-04 15:12:23 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-01-22 13:25:13 +08:00
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// This file provides internal interfaces used to implement the InstCombine.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2015-01-22 13:25:13 +08:00
|
|
|
#ifndef LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
|
|
|
|
#define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2015-07-10 14:55:49 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2015-01-04 20:03:27 +08:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2015-01-20 16:35:24 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2014-03-04 19:59:06 +08:00
|
|
|
#include "llvm/Analysis/TargetFolder.h"
|
2012-12-04 15:12:27 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2014-11-22 07:36:44 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2014-03-06 11:23:41 +08:00
|
|
|
#include "llvm/IR/InstVisitor.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Operator.h"
|
2014-09-07 20:44:26 +08:00
|
|
|
#include "llvm/IR/PatternMatch.h"
|
2010-01-04 15:12:23 +08:00
|
|
|
#include "llvm/Pass.h"
|
2015-01-24 12:19:17 +08:00
|
|
|
#include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "instcombine"
|
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
namespace llvm {
|
2014-05-08 01:36:59 +08:00
|
|
|
class CallSite;
|
|
|
|
class DataLayout;
|
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
|
|
|
class DominatorTree;
|
2014-05-08 01:36:59 +08:00
|
|
|
class TargetLibraryInfo;
|
|
|
|
class DbgDeclareInst;
|
|
|
|
class MemIntrinsic;
|
|
|
|
class MemSetInst;
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Assign a complexity or rank value to LLVM Values.
|
|
|
|
///
|
|
|
|
/// This routine maps IR values to various complexity ranks:
|
|
|
|
/// 0 -> undef
|
|
|
|
/// 1 -> Constants
|
|
|
|
/// 2 -> Other non-instructions
|
|
|
|
/// 3 -> Arguments
|
|
|
|
/// 3 -> Unary operations
|
|
|
|
/// 4 -> Other instructions
|
2010-01-04 15:37:31 +08:00
|
|
|
static inline unsigned getComplexity(Value *V) {
|
|
|
|
if (isa<Instruction>(V)) {
|
2014-05-08 01:36:59 +08:00
|
|
|
if (BinaryOperator::isNeg(V) || BinaryOperator::isFNeg(V) ||
|
2010-01-04 15:37:31 +08:00
|
|
|
BinaryOperator::isNot(V))
|
|
|
|
return 3;
|
|
|
|
return 4;
|
|
|
|
}
|
2014-05-08 01:36:59 +08:00
|
|
|
if (isa<Argument>(V))
|
|
|
|
return 3;
|
2010-01-04 15:37:31 +08:00
|
|
|
return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
|
|
|
|
}
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Add one to a Constant
|
2014-01-20 00:56:10 +08:00
|
|
|
static inline Constant *AddOne(Constant *C) {
|
|
|
|
return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
|
|
|
|
}
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Subtract one from a Constant
|
2014-01-20 00:56:10 +08:00
|
|
|
static inline Constant *SubOne(Constant *C) {
|
|
|
|
return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
|
|
|
|
}
|
|
|
|
|
2015-02-24 08:08:41 +08:00
|
|
|
/// \brief Return true if the specified value is free to invert (apply ~ to).
|
|
|
|
/// This happens in cases where the ~ can be eliminated. If WillInvertAllUses
|
|
|
|
/// is true, work under the assumption that the caller intends to remove all
|
|
|
|
/// uses of V and only keep uses of ~V.
|
|
|
|
///
|
|
|
|
static inline bool IsFreeToInvert(Value *V, bool WillInvertAllUses) {
|
|
|
|
// ~(~(X)) -> X.
|
|
|
|
if (BinaryOperator::isNot(V))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Constants can be considered to be not'ed values.
|
|
|
|
if (isa<ConstantInt>(V))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Compares can be inverted if all of their uses are being modified to use the
|
|
|
|
// ~V.
|
|
|
|
if (isa<CmpInst>(V))
|
|
|
|
return WillInvertAllUses;
|
|
|
|
|
|
|
|
// If `V` is of the form `A + Constant` then `-1 - V` can be folded into `(-1
|
|
|
|
// - Constant) - A` if we are willing to invert all of the uses.
|
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
|
|
|
|
if (BO->getOpcode() == Instruction::Add ||
|
|
|
|
BO->getOpcode() == Instruction::Sub)
|
|
|
|
if (isa<Constant>(BO->getOperand(0)) || isa<Constant>(BO->getOperand(1)))
|
|
|
|
return WillInvertAllUses;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:27:22 +08:00
|
|
|
|
|
|
|
/// \brief Specific patterns of overflow check idioms that we match.
|
|
|
|
enum OverflowCheckFlavor {
|
|
|
|
OCF_UNSIGNED_ADD,
|
|
|
|
OCF_SIGNED_ADD,
|
|
|
|
OCF_UNSIGNED_SUB,
|
|
|
|
OCF_SIGNED_SUB,
|
|
|
|
OCF_UNSIGNED_MUL,
|
|
|
|
OCF_SIGNED_MUL,
|
|
|
|
|
|
|
|
OCF_INVALID
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Returns the OverflowCheckFlavor corresponding to a overflow_with_op
|
|
|
|
/// intrinsic.
|
|
|
|
static inline OverflowCheckFlavor
|
|
|
|
IntrinsicIDToOverflowCheckFlavor(unsigned ID) {
|
|
|
|
switch (ID) {
|
|
|
|
default:
|
|
|
|
return OCF_INVALID;
|
|
|
|
case Intrinsic::uadd_with_overflow:
|
|
|
|
return OCF_UNSIGNED_ADD;
|
|
|
|
case Intrinsic::sadd_with_overflow:
|
|
|
|
return OCF_SIGNED_ADD;
|
|
|
|
case Intrinsic::usub_with_overflow:
|
|
|
|
return OCF_UNSIGNED_SUB;
|
|
|
|
case Intrinsic::ssub_with_overflow:
|
|
|
|
return OCF_SIGNED_SUB;
|
|
|
|
case Intrinsic::umul_with_overflow:
|
|
|
|
return OCF_UNSIGNED_MUL;
|
|
|
|
case Intrinsic::smul_with_overflow:
|
|
|
|
return OCF_SIGNED_MUL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief An IRBuilder inserter that adds new instructions to the instcombine
|
|
|
|
/// worklist.
|
2013-01-15 07:16:36 +08:00
|
|
|
class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter
|
2016-03-12 01:15:50 +08:00
|
|
|
: public IRBuilderDefaultInserter {
|
2010-01-04 15:12:23 +08:00
|
|
|
InstCombineWorklist &Worklist;
|
2015-01-04 20:03:27 +08:00
|
|
|
AssumptionCache *AC;
|
2014-05-08 01:36:59 +08:00
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
public:
|
2015-01-04 20:03:27 +08:00
|
|
|
InstCombineIRInserter(InstCombineWorklist &WL, AssumptionCache *AC)
|
|
|
|
: Worklist(WL), AC(AC) {}
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2014-05-08 01:36:59 +08:00
|
|
|
void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB,
|
|
|
|
BasicBlock::iterator InsertPt) const {
|
2016-03-12 01:15:50 +08:00
|
|
|
IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
|
2010-01-04 15:12:23 +08:00
|
|
|
Worklist.Add(I);
|
2014-09-07 20:44:26 +08:00
|
|
|
|
|
|
|
using namespace llvm::PatternMatch;
|
2014-10-26 02:09:01 +08:00
|
|
|
if (match(I, m_Intrinsic<Intrinsic::assume>()))
|
2015-01-04 20:03:27 +08:00
|
|
|
AC->registerAssumption(cast<CallInst>(I));
|
2010-01-04 15:12:23 +08:00
|
|
|
}
|
|
|
|
};
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief The core instruction combiner logic.
|
|
|
|
///
|
|
|
|
/// This class provides both the logic to recursively visit instructions and
|
|
|
|
/// combine them, as well as the pass infrastructure for running this as part
|
|
|
|
/// of the LLVM pass pipeline.
|
2010-05-12 04:16:09 +08:00
|
|
|
class LLVM_LIBRARY_VISIBILITY InstCombiner
|
2015-01-21 06:44:35 +08:00
|
|
|
: public InstVisitor<InstCombiner, Instruction *> {
|
2015-01-21 19:38:17 +08:00
|
|
|
// FIXME: These members shouldn't be public.
|
2010-01-04 15:12:23 +08:00
|
|
|
public:
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief A worklist of the instructions that need to be simplified.
|
2015-01-21 19:38:17 +08:00
|
|
|
InstCombineWorklist &Worklist;
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief An IRBuilder that automatically inserts new instructions into the
|
|
|
|
/// worklist.
|
2016-03-12 01:15:50 +08:00
|
|
|
typedef IRBuilder<TargetFolder, InstCombineIRInserter> BuilderTy;
|
2010-01-04 15:12:23 +08:00
|
|
|
BuilderTy *Builder;
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2015-01-21 19:38:17 +08:00
|
|
|
private:
|
|
|
|
// Mode in which we are running the combiner.
|
|
|
|
const bool MinimizeSize;
|
2016-03-10 02:47:11 +08:00
|
|
|
/// Enable combines that trigger rarely but are costly in compiletime.
|
|
|
|
const bool ExpensiveCombines;
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2015-07-10 14:55:49 +08:00
|
|
|
AliasAnalysis *AA;
|
|
|
|
|
2015-01-21 19:38:17 +08:00
|
|
|
// Required analyses.
|
|
|
|
// FIXME: These can never be null and should be references.
|
|
|
|
AssumptionCache *AC;
|
|
|
|
TargetLibraryInfo *TLI;
|
|
|
|
DominatorTree *DT;
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL;
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2015-01-21 19:38:17 +08:00
|
|
|
// Optional analyses. When non-null, these can both be used to do better
|
|
|
|
// combining and will be updated to reflect any changes.
|
|
|
|
LoopInfo *LI;
|
|
|
|
|
|
|
|
bool MadeIRChange;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder,
|
2016-03-10 02:47:11 +08:00
|
|
|
bool MinimizeSize, bool ExpensiveCombines, AliasAnalysis *AA,
|
2015-07-10 14:55:49 +08:00
|
|
|
AssumptionCache *AC, TargetLibraryInfo *TLI,
|
2015-03-10 10:37:25 +08:00
|
|
|
DominatorTree *DT, const DataLayout &DL, LoopInfo *LI)
|
2015-01-21 19:38:17 +08:00
|
|
|
: Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
|
2016-03-10 02:47:11 +08:00
|
|
|
ExpensiveCombines(ExpensiveCombines), AA(AA), AC(AC), TLI(TLI), DT(DT),
|
|
|
|
DL(DL), LI(LI), MadeIRChange(false) {}
|
2015-01-21 19:38:17 +08:00
|
|
|
|
|
|
|
/// \brief Run the combiner over the entire worklist until it is empty.
|
|
|
|
///
|
|
|
|
/// \returns true if the IR is changed.
|
|
|
|
bool run();
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2015-01-04 20:03:27 +08:00
|
|
|
AssumptionCache *getAssumptionCache() const { return AC; }
|
2014-09-07 20:44:26 +08:00
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &getDataLayout() const { return DL; }
|
2015-01-21 05:10:35 +08:00
|
|
|
|
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
|
|
|
DominatorTree *getDominatorTree() const { return DT; }
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2015-01-20 16:35:24 +08:00
|
|
|
LoopInfo *getLoopInfo() const { return LI; }
|
|
|
|
|
2011-12-02 09:26:24 +08:00
|
|
|
TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
|
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
// Visitation implementation - Implement instruction combining for different
|
|
|
|
// instruction types. The semantics are as follows:
|
|
|
|
// Return Value:
|
|
|
|
// null - No change was made
|
|
|
|
// I - Change was made, I is still valid, I may be dead though
|
|
|
|
// otherwise - Change was made, replace I with returned instruction
|
|
|
|
//
|
|
|
|
Instruction *visitAdd(BinaryOperator &I);
|
|
|
|
Instruction *visitFAdd(BinaryOperator &I);
|
2011-07-18 12:54:35 +08:00
|
|
|
Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *visitSub(BinaryOperator &I);
|
|
|
|
Instruction *visitFSub(BinaryOperator &I);
|
|
|
|
Instruction *visitMul(BinaryOperator &I);
|
2014-01-19 21:36:27 +08:00
|
|
|
Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
|
2013-01-08 05:39:23 +08:00
|
|
|
Instruction *InsertBefore);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *visitFMul(BinaryOperator &I);
|
|
|
|
Instruction *visitURem(BinaryOperator &I);
|
|
|
|
Instruction *visitSRem(BinaryOperator &I);
|
|
|
|
Instruction *visitFRem(BinaryOperator &I);
|
|
|
|
bool SimplifyDivRemOfSelect(BinaryOperator &I);
|
|
|
|
Instruction *commonRemTransforms(BinaryOperator &I);
|
|
|
|
Instruction *commonIRemTransforms(BinaryOperator &I);
|
|
|
|
Instruction *commonDivTransforms(BinaryOperator &I);
|
|
|
|
Instruction *commonIDivTransforms(BinaryOperator &I);
|
|
|
|
Instruction *visitUDiv(BinaryOperator &I);
|
|
|
|
Instruction *visitSDiv(BinaryOperator &I);
|
2011-01-30 01:50:27 +08:00
|
|
|
Instruction *visitFDiv(BinaryOperator &I);
|
2014-12-03 18:39:15 +08:00
|
|
|
Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
|
2010-03-05 16:46:26 +08:00
|
|
|
Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
|
|
|
|
Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *visitAnd(BinaryOperator &I);
|
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
|
|
|
Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction *CxtI);
|
2010-03-05 16:46:26 +08:00
|
|
|
Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
|
2014-05-08 01:36:59 +08:00
|
|
|
Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A,
|
|
|
|
Value *B, Value *C);
|
2014-08-21 13:14:48 +08:00
|
|
|
Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A,
|
|
|
|
Value *B, Value *C);
|
2014-05-08 01:36:59 +08:00
|
|
|
Instruction *visitOr(BinaryOperator &I);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *visitXor(BinaryOperator &I);
|
|
|
|
Instruction *visitShl(BinaryOperator &I);
|
|
|
|
Instruction *visitAShr(BinaryOperator &I);
|
|
|
|
Instruction *visitLShr(BinaryOperator &I);
|
|
|
|
Instruction *commonShiftTransforms(BinaryOperator &I);
|
|
|
|
Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
|
|
|
|
Constant *RHSC);
|
|
|
|
Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
|
|
|
|
GlobalVariable *GV, CmpInst &ICI,
|
2014-04-28 12:05:08 +08:00
|
|
|
ConstantInt *AndCst = nullptr);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *visitFCmpInst(FCmpInst &I);
|
|
|
|
Instruction *visitICmpInst(ICmpInst &I);
|
|
|
|
Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
|
2014-05-08 01:36:59 +08:00
|
|
|
Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS,
|
2010-01-04 15:12:23 +08:00
|
|
|
ConstantInt *RHS);
|
|
|
|
Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
|
|
|
|
ConstantInt *DivRHS);
|
2011-02-13 15:43:07 +08:00
|
|
|
Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI,
|
|
|
|
ConstantInt *DivRHS);
|
2014-07-23 03:19:36 +08:00
|
|
|
Instruction *FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A,
|
|
|
|
ConstantInt *CI1, ConstantInt *CI2);
|
2014-10-19 16:23:08 +08:00
|
|
|
Instruction *FoldICmpCstShlCst(ICmpInst &I, Value *Op, Value *A,
|
|
|
|
ConstantInt *CI1, ConstantInt *CI2);
|
2013-09-21 06:12:42 +08:00
|
|
|
Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI,
|
|
|
|
ICmpInst::Predicate Pred);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
|
|
|
|
ICmpInst::Predicate Cond, Instruction &I);
|
2015-10-07 08:20:07 +08:00
|
|
|
Instruction *FoldAllocaCmp(ICmpInst &ICI, AllocaInst *Alloca, Value *Other);
|
2014-04-15 05:50:37 +08:00
|
|
|
Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
|
2010-01-04 15:12:23 +08:00
|
|
|
BinaryOperator &I);
|
|
|
|
Instruction *commonCastTransforms(CastInst &CI);
|
|
|
|
Instruction *commonPointerCastTransforms(CastInst &CI);
|
|
|
|
Instruction *visitTrunc(TruncInst &CI);
|
|
|
|
Instruction *visitZExt(ZExtInst &CI);
|
|
|
|
Instruction *visitSExt(SExtInst &CI);
|
|
|
|
Instruction *visitFPTrunc(FPTruncInst &CI);
|
|
|
|
Instruction *visitFPExt(CastInst &CI);
|
|
|
|
Instruction *visitFPToUI(FPToUIInst &FI);
|
|
|
|
Instruction *visitFPToSI(FPToSIInst &FI);
|
|
|
|
Instruction *visitUIToFP(CastInst &CI);
|
|
|
|
Instruction *visitSIToFP(CastInst &CI);
|
|
|
|
Instruction *visitPtrToInt(PtrToIntInst &CI);
|
|
|
|
Instruction *visitIntToPtr(IntToPtrInst &CI);
|
|
|
|
Instruction *visitBitCast(BitCastInst &CI);
|
2013-11-15 13:45:08 +08:00
|
|
|
Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
|
2014-05-08 01:36:59 +08:00
|
|
|
Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
|
|
|
|
Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
|
|
|
|
Value *A, Value *B, Instruction &Outer,
|
|
|
|
SelectPatternFlavor SPF2, Value *C);
|
2015-02-17 05:47:54 +08:00
|
|
|
Instruction *FoldItoFPtoI(Instruction &FI);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *visitSelectInst(SelectInst &SI);
|
|
|
|
Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
|
|
|
|
Instruction *visitCallInst(CallInst &CI);
|
|
|
|
Instruction *visitInvokeInst(InvokeInst &II);
|
|
|
|
|
|
|
|
Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
|
|
|
|
Instruction *visitPHINode(PHINode &PN);
|
|
|
|
Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
|
|
|
|
Instruction *visitAllocaInst(AllocaInst &AI);
|
2012-07-10 02:38:20 +08:00
|
|
|
Instruction *visitAllocSite(Instruction &FI);
|
2010-06-24 20:21:15 +08:00
|
|
|
Instruction *visitFree(CallInst &FI);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *visitLoadInst(LoadInst &LI);
|
|
|
|
Instruction *visitStoreInst(StoreInst &SI);
|
|
|
|
Instruction *visitBranchInst(BranchInst &BI);
|
|
|
|
Instruction *visitSwitchInst(SwitchInst &SI);
|
2014-09-08 05:28:34 +08:00
|
|
|
Instruction *visitReturnInst(ReturnInst &RI);
|
2014-05-07 22:30:18 +08:00
|
|
|
Instruction *visitInsertValueInst(InsertValueInst &IV);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *visitInsertElementInst(InsertElementInst &IE);
|
|
|
|
Instruction *visitExtractElementInst(ExtractElementInst &EI);
|
|
|
|
Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
|
|
|
|
Instruction *visitExtractValueInst(ExtractValueInst &EV);
|
2011-09-30 21:12:16 +08:00
|
|
|
Instruction *visitLandingPadInst(LandingPadInst &LI);
|
2010-01-04 15:12:23 +08:00
|
|
|
|
|
|
|
// visitInstruction - Specify what to return for unhandled instructions...
|
2014-04-28 12:05:08 +08:00
|
|
|
Instruction *visitInstruction(Instruction &I) { return nullptr; }
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2014-11-22 07:36:44 +08:00
|
|
|
// True when DB dominates all uses of DI execpt UI.
|
|
|
|
// UI must be in the same block as DI.
|
|
|
|
// The routine checks that the DI parent and DB are different.
|
|
|
|
bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
|
|
|
|
const BasicBlock *DB) const;
|
|
|
|
|
|
|
|
// Replace select with select operand SIOpd in SI-ICmp sequence when possible
|
|
|
|
bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
|
|
|
|
const unsigned SIOpd);
|
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
private:
|
2015-09-22 00:09:37 +08:00
|
|
|
bool ShouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
|
2011-07-18 12:54:35 +08:00
|
|
|
bool ShouldChangeType(Type *From, Type *To) const;
|
2010-01-04 15:37:31 +08:00
|
|
|
Value *dyn_castNegVal(Value *V) const;
|
2014-05-08 01:36:59 +08:00
|
|
|
Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
|
2015-03-28 04:56:11 +08:00
|
|
|
Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset,
|
2014-05-08 01:36:59 +08:00
|
|
|
SmallVectorImpl<Value *> &NewIndices);
|
2010-01-04 15:53:58 +08:00
|
|
|
Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Classify whether a cast is worth optimizing.
|
|
|
|
///
|
|
|
|
/// Returns true if the cast from "V to Ty" actually results in any code
|
|
|
|
/// being generated and is interesting to optimize out. If the cast can be
|
|
|
|
/// eliminated by some other simple transformation, we prefer to do the
|
|
|
|
/// simplification first.
|
2014-05-08 01:36:59 +08:00
|
|
|
bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V,
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *Ty);
|
2010-01-04 15:37:31 +08:00
|
|
|
|
2015-04-08 12:27:22 +08:00
|
|
|
/// \brief Try to optimize a sequence of instructions checking if an operation
|
|
|
|
/// on LHS and RHS overflows.
|
|
|
|
///
|
2015-08-29 03:09:31 +08:00
|
|
|
/// If this overflow check is done via one of the overflow check intrinsics,
|
|
|
|
/// then CtxI has to be the call instruction calling that intrinsic. If this
|
|
|
|
/// overflow check is done by arithmetic followed by a compare, then CtxI has
|
|
|
|
/// to be the arithmetic instruction.
|
|
|
|
///
|
2015-04-08 12:27:22 +08:00
|
|
|
/// If a simplification is possible, stores the simplified result of the
|
|
|
|
/// operation in OperationResult and result of the overflow check in
|
|
|
|
/// OverflowResult, and return true. If no simplification is possible,
|
|
|
|
/// returns false.
|
|
|
|
bool OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS, Value *RHS,
|
|
|
|
Instruction &CtxI, Value *&OperationResult,
|
|
|
|
Constant *&OverflowResult);
|
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *visitCallSite(CallSite CS);
|
2015-03-10 10:37:25 +08:00
|
|
|
Instruction *tryOptimizeCall(CallInst *CI);
|
2010-01-04 15:12:23 +08:00
|
|
|
bool transformConstExprCastCall(CallSite CS);
|
2011-09-06 21:37:06 +08:00
|
|
|
Instruction *transformCallThroughTrampoline(CallSite CS,
|
|
|
|
IntrinsicInst *Tramp);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
|
|
|
|
bool DoXform = true);
|
2011-04-02 04:09:03 +08:00
|
|
|
Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
|
2015-03-10 10:37:25 +08:00
|
|
|
bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS, Instruction &CxtI);
|
|
|
|
bool WillNotOverflowSignedSub(Value *LHS, Value *RHS, Instruction &CxtI);
|
|
|
|
bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, Instruction &CxtI);
|
|
|
|
bool WillNotOverflowSignedMul(Value *LHS, Value *RHS, Instruction &CxtI);
|
2012-05-23 01:19:09 +08:00
|
|
|
Value *EmitGEPOffset(User *GEP);
|
2013-04-19 03:35:39 +08:00
|
|
|
Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
|
2013-05-31 08:59:42 +08:00
|
|
|
Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
|
2016-02-24 00:36:07 +08:00
|
|
|
Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
|
2010-01-04 15:12:23 +08:00
|
|
|
|
|
|
|
public:
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Inserts an instruction \p New before instruction \p Old
|
|
|
|
///
|
|
|
|
/// Also adds the new instruction to the worklist and returns \p New so that
|
|
|
|
/// it is suitable for use as the return from the visitation patterns.
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
|
2014-04-28 12:05:08 +08:00
|
|
|
assert(New && !New->getParent() &&
|
2010-01-04 15:12:23 +08:00
|
|
|
"New instruction already inserted into a basic block!");
|
|
|
|
BasicBlock *BB = Old.getParent();
|
2015-10-14 00:59:33 +08:00
|
|
|
BB->getInstList().insert(Old.getIterator(), New); // Insert inst
|
2010-01-04 15:12:23 +08:00
|
|
|
Worklist.Add(New);
|
|
|
|
return New;
|
|
|
|
}
|
2011-05-19 09:20:42 +08:00
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Same as InsertNewInstBefore, but also sets the debug loc.
|
2011-05-19 09:20:42 +08:00
|
|
|
Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
|
|
|
|
New->setDebugLoc(Old.getDebugLoc());
|
|
|
|
return InsertNewInstBefore(New, Old);
|
|
|
|
}
|
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief A combiner-aware RAUW-like routine.
|
|
|
|
///
|
|
|
|
/// This method is to be used when an instruction is found to be dead,
|
2016-01-06 08:23:12 +08:00
|
|
|
/// replaceable with another preexisting expression. Here we add all uses of
|
2015-01-21 03:27:58 +08:00
|
|
|
/// I to the worklist, replace all uses of I with the new value, then return
|
|
|
|
/// I, so that the inst combiner will know that I was modified.
|
2016-02-02 06:23:39 +08:00
|
|
|
Instruction *replaceInstUsesWith(Instruction &I, Value *V) {
|
2015-03-10 13:13:47 +08:00
|
|
|
// If there are no uses to replace, then we return nullptr to indicate that
|
|
|
|
// no changes were made to the program.
|
|
|
|
if (I.use_empty()) return nullptr;
|
|
|
|
|
2014-05-08 01:36:59 +08:00
|
|
|
Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
// If we are replacing the instruction with itself, this must be in a
|
|
|
|
// segment of unreachable code, so just clobber the instruction.
|
2013-01-15 07:16:36 +08:00
|
|
|
if (&I == V)
|
2010-01-04 15:12:23 +08:00
|
|
|
V = UndefValue::get(I.getType());
|
2011-03-28 07:32:31 +08:00
|
|
|
|
2013-09-06 03:48:28 +08:00
|
|
|
DEBUG(dbgs() << "IC: Replacing " << I << "\n"
|
2015-01-21 05:10:35 +08:00
|
|
|
<< " with " << *V << '\n');
|
2011-03-28 07:32:31 +08:00
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
I.replaceAllUsesWith(V);
|
|
|
|
return &I;
|
|
|
|
}
|
|
|
|
|
2014-12-11 16:02:30 +08:00
|
|
|
/// Creates a result tuple for an overflow intrinsic \p II with a given
|
2015-04-08 12:27:22 +08:00
|
|
|
/// \p Result and a constant \p Overflow value.
|
2014-12-11 16:02:30 +08:00
|
|
|
Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result,
|
2015-04-08 12:27:22 +08:00
|
|
|
Constant *Overflow) {
|
|
|
|
Constant *V[] = {UndefValue::get(Result->getType()), Overflow};
|
2014-12-11 16:02:30 +08:00
|
|
|
StructType *ST = cast<StructType>(II->getType());
|
|
|
|
Constant *Struct = ConstantStruct::get(ST, V);
|
|
|
|
return InsertValueInst::Create(Struct, Result, 0);
|
|
|
|
}
|
2015-01-21 05:10:35 +08:00
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Combiner aware instruction erasure.
|
|
|
|
///
|
|
|
|
/// When dealing with an instruction that has side effects or produces a void
|
|
|
|
/// value, we can't rely on DCE to delete the instruction. Instead, visit
|
|
|
|
/// methods should return the value returned by this function.
|
2016-02-02 06:23:39 +08:00
|
|
|
Instruction *eraseInstFromFunction(Instruction &I) {
|
2013-09-06 03:48:28 +08:00
|
|
|
DEBUG(dbgs() << "IC: ERASE " << I << '\n');
|
2010-01-04 15:12:23 +08:00
|
|
|
|
|
|
|
assert(I.use_empty() && "Cannot erase instruction that is used!");
|
|
|
|
// Make sure that we reprocess all operands now that we reduced their
|
|
|
|
// use counts.
|
|
|
|
if (I.getNumOperands() < 8) {
|
2016-02-01 00:34:48 +08:00
|
|
|
for (Use &Operand : I.operands())
|
|
|
|
if (auto *Inst = dyn_cast<Instruction>(Operand))
|
|
|
|
Worklist.Add(Inst);
|
2010-01-04 15:12:23 +08:00
|
|
|
}
|
|
|
|
Worklist.Remove(&I);
|
|
|
|
I.eraseFromParent();
|
|
|
|
MadeIRChange = true;
|
2014-05-08 01:36:59 +08:00
|
|
|
return nullptr; // Don't do anything with FI
|
2010-01-04 15:12:23 +08:00
|
|
|
}
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2014-05-15 05:14:37 +08:00
|
|
|
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
|
2015-03-10 10:37:25 +08:00
|
|
|
unsigned Depth, Instruction *CxtI) const {
|
2015-01-04 20:03:27 +08:00
|
|
|
return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
|
|
|
|
DT);
|
2010-01-04 15:12:23 +08:00
|
|
|
}
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2015-01-21 05:10:35 +08:00
|
|
|
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth = 0,
|
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
|
|
|
Instruction *CxtI = nullptr) const {
|
2015-01-04 20:03:27 +08:00
|
|
|
return llvm::MaskedValueIsZero(V, Mask, DL, Depth, AC, CxtI, DT);
|
2010-01-04 15:12:23 +08:00
|
|
|
}
|
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
|
|
|
unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0,
|
|
|
|
Instruction *CxtI = nullptr) const {
|
2015-01-04 20:03:27 +08:00
|
|
|
return llvm::ComputeNumSignBits(Op, DL, Depth, AC, CxtI, DT);
|
2010-01-04 15:12:23 +08:00
|
|
|
}
|
2014-12-26 17:10:14 +08:00
|
|
|
void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
|
|
|
|
unsigned Depth = 0, Instruction *CxtI = nullptr) const {
|
2015-01-04 20:03:27 +08:00
|
|
|
return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
|
2014-12-26 17:10:14 +08:00
|
|
|
DT);
|
|
|
|
}
|
2015-01-02 15:29:43 +08:00
|
|
|
OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
|
|
|
|
const Instruction *CxtI) {
|
2015-01-04 20:03:27 +08:00
|
|
|
return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, AC, CxtI, DT);
|
2015-01-02 15:29:43 +08:00
|
|
|
}
|
2015-01-07 08:39:50 +08:00
|
|
|
OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
|
|
|
|
const Instruction *CxtI) {
|
|
|
|
return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, AC, CxtI, DT);
|
|
|
|
}
|
2010-01-04 15:12:23 +08:00
|
|
|
|
|
|
|
private:
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Performs a few simplifications for operators which are associative
|
|
|
|
/// or commutative.
|
2010-11-13 23:10:37 +08:00
|
|
|
bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2015-01-21 05:10:35 +08:00
|
|
|
/// \brief Tries to simplify binary operations which some other binary
|
|
|
|
/// operation distributes over.
|
2015-01-21 03:27:58 +08:00
|
|
|
///
|
|
|
|
/// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
|
|
|
|
/// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
|
|
|
|
/// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
|
|
|
|
/// value, or null if it didn't simplify.
|
2010-12-22 21:36:08 +08:00
|
|
|
Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
|
2010-11-23 22:23:47 +08:00
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Attempts to replace V with a simpler value based on the demanded
|
|
|
|
/// bits.
|
2014-05-08 01:36:59 +08:00
|
|
|
Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero,
|
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 &KnownOne, unsigned Depth,
|
2015-03-10 10:37:25 +08:00
|
|
|
Instruction *CxtI);
|
2014-05-08 01:36:59 +08:00
|
|
|
bool SimplifyDemandedBits(Use &U, APInt DemandedMask, APInt &KnownZero,
|
|
|
|
APInt &KnownOne, unsigned Depth = 0);
|
2012-12-04 08:04:54 +08:00
|
|
|
/// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
|
|
|
|
/// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
|
|
|
|
Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
|
|
|
|
APInt DemandedMask, APInt &KnownZero,
|
|
|
|
APInt &KnownOne);
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Tries to simplify operands to an integer instruction based on its
|
|
|
|
/// demanded bits.
|
2010-01-04 15:12:23 +08:00
|
|
|
bool SimplifyDemandedInstructionBits(Instruction &Inst);
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
|
2014-05-08 01:36:59 +08:00
|
|
|
APInt &UndefElts, unsigned Depth = 0);
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2014-05-11 16:46:12 +08:00
|
|
|
Value *SimplifyVectorOp(BinaryOperator &Inst);
|
2014-12-04 17:44:01 +08:00
|
|
|
Value *SimplifyBSwap(BinaryOperator &Inst);
|
2014-05-11 16:46:12 +08:00
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
// FoldOpIntoPhi - Given a binary operator, cast instruction, or select
|
|
|
|
// which has a PHI node as operand #0, see if we can fold the instruction
|
|
|
|
// into the PHI (which is only possible if all operands to the PHI are
|
|
|
|
// constants).
|
|
|
|
//
|
2011-01-16 13:14:26 +08:00
|
|
|
Instruction *FoldOpIntoPhi(Instruction &I);
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Try to rotate an operation below a PHI node, using PHI nodes for
|
|
|
|
/// its operands.
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
|
|
|
|
Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
|
|
|
|
Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
|
|
|
|
Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
|
2015-09-28 04:34:31 +08:00
|
|
|
Instruction *FoldPHIArgZextsIntoPHI(PHINode &PN);
|
2010-01-04 15:12:23 +08:00
|
|
|
|
|
|
|
Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
|
|
|
|
ConstantInt *AndRHS, BinaryOperator &TheAnd);
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
|
|
|
|
bool isSub, Instruction &I);
|
2014-05-08 01:36:59 +08:00
|
|
|
Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned,
|
|
|
|
bool Inside);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
|
2015-12-11 18:04:51 +08:00
|
|
|
Instruction *MatchBSwapOrBitReverse(BinaryOperator &I);
|
2010-01-04 15:12:23 +08:00
|
|
|
bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
|
2015-11-19 13:56:52 +08:00
|
|
|
Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *SimplifyMemSet(MemSetInst *MI);
|
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
|
2012-10-23 16:28:26 +08:00
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Returns a value X such that Val = X * Scale, or null if none.
|
|
|
|
///
|
|
|
|
/// If the multiplication is known not to overflow then NoSignedWrap is set.
|
2012-10-23 16:28:26 +08:00
|
|
|
Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
|
2010-01-04 15:12:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm.
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#undef DEBUG_TYPE
|
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
#endif
|