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"
|
2016-12-19 16:22:17 +08:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2017-04-27 04:56:07 +08:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.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"
|
2017-04-27 04:56:07 +08:00
|
|
|
#include "llvm/IR/DIBuilder.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"
|
2017-04-27 04:56:07 +08:00
|
|
|
#include "llvm/Support/Dwarf.h"
|
2015-01-24 12:19:17 +08:00
|
|
|
#include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
|
Salvage debug info from instructions about to be deleted
[Reapplies r297971 and punting on finding a better API for findDbgValues()]
This patch improves debug info quality in InstCombine by looking at
values that are about to be deleted, checking whether there are any
dbg.value instrinsics referring to them, and potentially encoding the
semantics of the deleted instruction into the dbg.value's
DIExpression.
In the example in the testcase (which was extracted from XNU) there is a sequence of
%4 = load %struct.entry*, %struct.entry** %next2, align 8, !dbg !41
%5 = bitcast %struct.entry* %4 to i8*, !dbg !42
%add.ptr4 = getelementptr inbounds i8, i8* %5, i64 -8, !dbg !43
%6 = bitcast i8* %add.ptr4 to %struct.entry*, !dbg !44
call void @llvm.dbg.value(metadata %struct.entry* %6, i64 0, metadata !20, metadata !21), !dbg 34
When these instructions are eliminated by instcombine one after
another, we can still salvage the otherwise dead debug info:
- Bitcasts have no effect, so have the dbg.value point to operand(0)
- Loads can be expressed via a DW_OP_deref
- Constant gep instructions can be replaced by DWARF expression arithmetic
The API introduced by this patch is not specific to instcombine and
can be useful in other places, too.
rdar://problem/30725338
Differential Revision: https://reviews.llvm.org/D30919
llvm-svn: 297994
2017-03-17 05:14:09 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.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
|
|
|
|
2017-02-04 05:43:34 +08:00
|
|
|
/// Assign a complexity or rank value to LLVM Values. This is used to reduce
|
|
|
|
/// the amount of pattern matching needed for compares and commutative
|
|
|
|
/// instructions. For example, if we have:
|
|
|
|
/// icmp ugt X, Constant
|
|
|
|
/// or
|
|
|
|
/// xor (add X, Constant), cast Z
|
|
|
|
///
|
|
|
|
/// We do not have to consider the commuted variants of these patterns because
|
|
|
|
/// canonicalization based on complexity guarantees the above ordering.
|
2015-01-21 03:27:58 +08:00
|
|
|
///
|
|
|
|
/// This routine maps IR values to various complexity ranks:
|
|
|
|
/// 0 -> undef
|
|
|
|
/// 1 -> Constants
|
|
|
|
/// 2 -> Other non-instructions
|
|
|
|
/// 3 -> Arguments
|
2017-02-04 05:43:34 +08:00
|
|
|
/// 4 -> Cast and (f)neg/not instructions
|
|
|
|
/// 5 -> Other instructions
|
2010-01-04 15:37:31 +08:00
|
|
|
static inline unsigned getComplexity(Value *V) {
|
|
|
|
if (isa<Instruction>(V)) {
|
2017-02-04 05:43:34 +08:00
|
|
|
if (isa<CastInst>(V) || BinaryOperator::isNeg(V) ||
|
|
|
|
BinaryOperator::isFNeg(V) || BinaryOperator::isNot(V))
|
|
|
|
return 4;
|
|
|
|
return 5;
|
2010-01-04 15:37:31 +08:00
|
|
|
}
|
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;
|
|
|
|
|
2016-10-28 01:30:50 +08:00
|
|
|
// A vector of constant integers can be inverted easily.
|
|
|
|
Constant *CV;
|
|
|
|
if (V->getType()->isVectorTy() && match(V, PatternMatch::m_Constant(CV))) {
|
|
|
|
unsigned NumElts = V->getType()->getVectorNumElements();
|
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
Constant *Elt = CV->getAggregateElement(i);
|
|
|
|
if (!Elt)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (isa<UndefValue>(Elt))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!isa<ConstantInt>(Elt))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-24 08:08:41 +08:00
|
|
|
// 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 The core instruction combiner logic.
|
|
|
|
///
|
|
|
|
/// This class provides both the logic to recursively visit instructions and
|
2016-12-28 11:12:42 +08:00
|
|
|
/// combine them.
|
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-08-05 07:41:01 +08:00
|
|
|
typedef IRBuilder<TargetFolder, IRBuilderCallbackInserter> 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.
|
2016-12-19 16:22:17 +08:00
|
|
|
AssumptionCache &AC;
|
2016-08-05 09:06:44 +08:00
|
|
|
TargetLibraryInfo &TLI;
|
|
|
|
DominatorTree &DT;
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL;
|
2017-04-27 04:56:07 +08:00
|
|
|
const SimplifyQuery SQ;
|
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,
|
2017-04-27 04:56:07 +08:00
|
|
|
AssumptionCache &AC, TargetLibraryInfo &TLI, DominatorTree &DT,
|
|
|
|
const DataLayout &DL, LoopInfo *LI)
|
2015-01-21 19:38:17 +08:00
|
|
|
: Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
|
2016-12-19 16:22:17 +08:00
|
|
|
ExpensiveCombines(ExpensiveCombines), AA(AA), AC(AC), TLI(TLI), DT(DT),
|
2017-04-27 04:56:07 +08:00
|
|
|
DL(DL), SQ(DL, &TLI, &DT, &AC), 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
|
|
|
|
2016-12-19 16:22:17 +08:00
|
|
|
AssumptionCache &getAssumptionCache() const { return AC; }
|
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &getDataLayout() const { return DL; }
|
2015-01-21 05:10:35 +08:00
|
|
|
|
2016-08-05 09:06:44 +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; }
|
|
|
|
|
2016-08-05 09:06:44 +08:00
|
|
|
TargetLibraryInfo &getTargetLibraryInfo() const { return TLI; }
|
2011-12-02 09:26:24 +08:00
|
|
|
|
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 *visitFCmpInst(FCmpInst &I);
|
|
|
|
Instruction *visitICmpInst(ICmpInst &I);
|
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);
|
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 *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);
|
2017-02-01 02:09:05 +08:00
|
|
|
Instruction *visitFenceInst(FenceInst &FI);
|
2010-01-04 15:12:23 +08:00
|
|
|
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);
|
2016-05-10 17:24:49 +08:00
|
|
|
Instruction *visitVAStartInst(VAStartInst &I);
|
|
|
|
Instruction *visitVACopyInst(VACopyInst &I);
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2016-09-12 22:25:46 +08:00
|
|
|
/// 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
|
|
|
|
2016-09-12 22:25:46 +08:00
|
|
|
/// True when DB dominates all uses of DI except UI.
|
|
|
|
/// UI must be in the same block as DI.
|
|
|
|
/// The routine checks that the DI parent and DB are different.
|
2014-11-22 07:36:44 +08:00
|
|
|
bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
|
|
|
|
const BasicBlock *DB) const;
|
|
|
|
|
2016-09-12 22:25:46 +08:00
|
|
|
/// Try to replace select with select operand SIOpd in SI-ICmp sequence.
|
2014-11-22 07:36:44 +08:00
|
|
|
bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
|
|
|
|
const unsigned SIOpd);
|
|
|
|
|
Fix invalid addrspacecast due to combining alloca with global var
For function-scope variables with large initialisation list, FE usually
generates a global variable to hold the initializer, then generates
memcpy intrinsic to initialize the alloca. InstCombiner::visitAllocaInst
identifies such allocas which are accessed only by reading and replaces
them with the global variable. This is done by casting the global variable
to the type of the alloca and replacing all references.
However, when the global variable is in a different address space which
is disjoint with addr space 0 (e.g. for IR generated from OpenCL,
global variable cannot be in private addr space i.e. addr space 0), casting
the global variable to addr space 0 results in invalid IR for certain
targets (e.g. amdgpu).
To fix this issue, when the global variable is not in addr space 0,
instead of casting it to addr space 0, this patch chases down the uses
of alloca until reaching the load instructions, then replaces load from
alloca with load from the global variable. If during the chasing
bitcast and GEP are encountered, new bitcast and GEP based on the global
variable are generated and used in the load instructions.
Differential Revision: https://reviews.llvm.org/D27283
llvm-svn: 294786
2017-02-11 05:46:07 +08:00
|
|
|
/// Try to replace instruction \p I with value \p V which are pointers
|
|
|
|
/// in different address space.
|
|
|
|
/// \return true if successful.
|
|
|
|
bool replacePointer(Instruction &I, Value *V);
|
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
private:
|
2017-02-01 01:25:42 +08:00
|
|
|
bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
|
|
|
|
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);
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2016-07-19 17:06:08 +08:00
|
|
|
/// Classify whether a cast is worth optimizing.
|
2015-01-21 03:27:58 +08:00
|
|
|
///
|
2016-07-19 17:06:08 +08:00
|
|
|
/// This is a helper to decide whether the simplification of
|
|
|
|
/// logic(cast(A), cast(B)) to cast(logic(A, B)) should be performed.
|
|
|
|
///
|
|
|
|
/// \param CI The cast we are interested in.
|
|
|
|
///
|
|
|
|
/// \return true if this cast actually results in any code being generated and
|
|
|
|
/// if it cannot already be eliminated by some other transformation.
|
|
|
|
bool shouldOptimizeCast(CastInst *CI);
|
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);
|
2016-07-19 17:06:08 +08:00
|
|
|
|
|
|
|
/// Transform (zext icmp) to bitwise / integer operations in order to
|
|
|
|
/// eliminate it.
|
|
|
|
///
|
|
|
|
/// \param ICI The icmp of the (zext icmp) pair we are interested in.
|
|
|
|
/// \parem CI The zext of the (zext icmp) pair we are interested in.
|
|
|
|
/// \param DoTransform Pass false to just test whether the given (zext icmp)
|
|
|
|
/// would be transformed. Pass true to actually perform the transformation.
|
|
|
|
///
|
|
|
|
/// \return null if the transformation cannot be performed. If the
|
|
|
|
/// transformation can be performed the new instruction that replaces the
|
|
|
|
/// (zext icmp) pair will be returned (if \p DoTransform is false the
|
|
|
|
/// unmodified \p ICI will be returned in this case).
|
|
|
|
Instruction *transformZExtICmp(ICmpInst *ICI, ZExtInst &CI,
|
|
|
|
bool DoTransform = 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);
|
2016-12-01 04:48:54 +08:00
|
|
|
Instruction *shrinkBitwiseLogic(TruncInst &Trunc);
|
2016-10-26 04:43:42 +08:00
|
|
|
Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2016-07-19 17:06:08 +08:00
|
|
|
/// Determine if a pair of casts can be replaced by a single cast.
|
|
|
|
///
|
|
|
|
/// \param CI1 The first of a pair of casts.
|
|
|
|
/// \param CI2 The second of a pair of casts.
|
|
|
|
///
|
|
|
|
/// \return 0 if the cast pair cannot be eliminated, otherwise returns an
|
|
|
|
/// Instruction::CastOps value for a cast that can replace the pair, casting
|
|
|
|
/// CI1->getSrcTy() to CI2->getDstTy().
|
|
|
|
///
|
|
|
|
/// \see CastInst::isEliminableCastPair
|
|
|
|
Instruction::CastOps isEliminableCastPair(const CastInst *CI1,
|
|
|
|
const CastInst *CI2);
|
|
|
|
|
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');
|
2017-03-17 04:11:54 +08:00
|
|
|
assert(I.use_empty() && "Cannot erase instruction that is used!");
|
Salvage debug info from instructions about to be deleted
[Reapplies r297971 and punting on finding a better API for findDbgValues()]
This patch improves debug info quality in InstCombine by looking at
values that are about to be deleted, checking whether there are any
dbg.value instrinsics referring to them, and potentially encoding the
semantics of the deleted instruction into the dbg.value's
DIExpression.
In the example in the testcase (which was extracted from XNU) there is a sequence of
%4 = load %struct.entry*, %struct.entry** %next2, align 8, !dbg !41
%5 = bitcast %struct.entry* %4 to i8*, !dbg !42
%add.ptr4 = getelementptr inbounds i8, i8* %5, i64 -8, !dbg !43
%6 = bitcast i8* %add.ptr4 to %struct.entry*, !dbg !44
call void @llvm.dbg.value(metadata %struct.entry* %6, i64 0, metadata !20, metadata !21), !dbg 34
When these instructions are eliminated by instcombine one after
another, we can still salvage the otherwise dead debug info:
- Bitcasts have no effect, so have the dbg.value point to operand(0)
- Loads can be expressed via a DW_OP_deref
- Constant gep instructions can be replaced by DWARF expression arithmetic
The API introduced by this patch is not specific to instcombine and
can be useful in other places, too.
rdar://problem/30725338
Differential Revision: https://reviews.llvm.org/D30919
llvm-svn: 297994
2017-03-17 05:14:09 +08:00
|
|
|
salvageDebugInfo(I);
|
|
|
|
|
2010-01-04 15:12:23 +08:00
|
|
|
// 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
|
|
|
|
2017-04-27 00:39:58 +08:00
|
|
|
void computeKnownBits(Value *V, KnownBits &Known,
|
2015-03-10 10:37:25 +08:00
|
|
|
unsigned Depth, Instruction *CxtI) const {
|
2017-04-27 00:39:58 +08:00
|
|
|
return llvm::computeKnownBits(V, Known, 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 {
|
2016-12-19 16:22:17 +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 {
|
2016-12-19 16:22:17 +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 {
|
2016-12-19 16:22:17 +08:00
|
|
|
return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, &AC, CxtI,
|
|
|
|
&DT);
|
2014-12-26 17:10:14 +08:00
|
|
|
}
|
2015-01-02 15:29:43 +08:00
|
|
|
OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
|
|
|
|
const Instruction *CxtI) {
|
2016-12-19 16:22:17 +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) {
|
2016-12-19 16:22:17 +08:00
|
|
|
return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
|
2015-01-07 08:39:50 +08:00
|
|
|
}
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2017-02-08 01:56:50 +08:00
|
|
|
/// Maximum size of array considered when transforming.
|
2017-02-08 02:58:17 +08:00
|
|
|
uint64_t MaxArraySizeForCombine;
|
2017-02-08 01:56:50 +08:00
|
|
|
|
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
|
|
|
|
2017-04-27 04:56:07 +08:00
|
|
|
/// This tries to simplify binary operations by factorizing out common terms
|
|
|
|
/// (e. g. "(A*B)+(A*C)" -> "A*(B+C)").
|
|
|
|
Value *tryFactorization(InstCombiner::BuilderTy *, BinaryOperator &,
|
|
|
|
Instruction::BinaryOps, Value *, Value *, Value *,
|
|
|
|
Value *);
|
|
|
|
|
2015-01-21 03:27:58 +08:00
|
|
|
/// \brief Attempts to replace V with a simpler value based on the demanded
|
|
|
|
/// bits.
|
2017-04-27 00:39:58 +08:00
|
|
|
Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, KnownBits &Known,
|
|
|
|
unsigned Depth, Instruction *CxtI);
|
2017-03-25 14:52:52 +08:00
|
|
|
bool SimplifyDemandedBits(Instruction *I, unsigned Op,
|
2017-04-27 00:39:58 +08:00
|
|
|
const APInt &DemandedMask, KnownBits &Known,
|
|
|
|
unsigned Depth = 0);
|
2017-04-13 02:05:21 +08:00
|
|
|
/// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
|
|
|
|
/// bits. It also tries to handle simplifications that can be done based on
|
|
|
|
/// DemandedMask, but without modifying the Instruction.
|
|
|
|
Value *SimplifyMultipleUseDemandedBits(Instruction *I,
|
|
|
|
const APInt &DemandedMask,
|
2017-04-27 00:39:58 +08:00
|
|
|
KnownBits &Known,
|
2017-04-13 02:05:21 +08:00
|
|
|
unsigned Depth, Instruction *CxtI);
|
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.
|
2017-04-21 06:37:01 +08:00
|
|
|
Value *simplifyShrShlDemandedBits(
|
2017-04-21 06:33:54 +08:00
|
|
|
Instruction *Shr, const APInt &ShrOp1, Instruction *Shl,
|
2017-04-27 00:39:58 +08:00
|
|
|
const APInt &ShlOp1, const APInt &DemandedMask, KnownBits &Known);
|
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
|
|
|
|
2017-01-11 07:49:07 +08:00
|
|
|
|
|
|
|
/// 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).
|
2017-04-15 03:20:12 +08:00
|
|
|
Instruction *foldOpIntoPhi(Instruction &I, PHINode *PN);
|
2010-01-04 15:12:23 +08:00
|
|
|
|
2017-01-11 07:49:07 +08:00
|
|
|
/// Given an instruction with a select as one operand and a constant as the
|
|
|
|
/// other operand, try to fold the binary operator into the select arguments.
|
|
|
|
/// This also works for Cast instructions, which obviously do not have a
|
|
|
|
/// second operand.
|
|
|
|
Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
|
|
|
|
|
|
|
|
/// This is a convenience wrapper function for the above two functions.
|
2017-04-03 15:08:08 +08:00
|
|
|
Instruction *foldOpWithConstantIntoOperand(BinaryOperator &I);
|
2017-01-11 07:49:07 +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
|
|
|
|
2016-12-15 01:49:19 +08:00
|
|
|
/// Helper function for FoldPHIArgXIntoPHI() to get debug location for the
|
|
|
|
/// folded operation.
|
|
|
|
DebugLoc PHIArgMergedDebugLoc(PHINode &PN);
|
|
|
|
|
2016-07-22 02:07:40 +08:00
|
|
|
Instruction *foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
|
|
|
|
ICmpInst::Predicate Cond, Instruction &I);
|
2016-08-13 01:13:28 +08:00
|
|
|
Instruction *foldAllocaCmp(ICmpInst &ICI, const AllocaInst *Alloca,
|
|
|
|
const Value *Other);
|
2016-07-22 02:07:40 +08:00
|
|
|
Instruction *foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
|
|
|
|
GlobalVariable *GV, CmpInst &ICI,
|
|
|
|
ConstantInt *AndCst = nullptr);
|
|
|
|
Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI,
|
|
|
|
Constant *RHSC);
|
|
|
|
Instruction *foldICmpAddOpConst(Instruction &ICI, Value *X, ConstantInt *CI,
|
|
|
|
ICmpInst::Predicate Pred);
|
|
|
|
Instruction *foldICmpWithCastAndCast(ICmpInst &ICI);
|
2016-09-10 23:03:44 +08:00
|
|
|
|
2016-09-12 23:24:31 +08:00
|
|
|
Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp);
|
2016-09-15 22:37:50 +08:00
|
|
|
Instruction *foldICmpWithConstant(ICmpInst &Cmp);
|
2016-09-10 23:03:44 +08:00
|
|
|
Instruction *foldICmpInstWithConstant(ICmpInst &Cmp);
|
2016-09-17 00:10:22 +08:00
|
|
|
Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp);
|
|
|
|
Instruction *foldICmpBinOp(ICmpInst &Cmp);
|
|
|
|
Instruction *foldICmpEquality(ICmpInst &Cmp);
|
2016-08-23 05:24:29 +08:00
|
|
|
|
|
|
|
Instruction *foldICmpTruncConstant(ICmpInst &Cmp, Instruction *Trunc,
|
|
|
|
const APInt *C);
|
|
|
|
Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And,
|
|
|
|
const APInt *C);
|
|
|
|
Instruction *foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor,
|
|
|
|
const APInt *C);
|
|
|
|
Instruction *foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or,
|
|
|
|
const APInt *C);
|
|
|
|
Instruction *foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul,
|
|
|
|
const APInt *C);
|
|
|
|
Instruction *foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl,
|
|
|
|
const APInt *C);
|
|
|
|
Instruction *foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr,
|
|
|
|
const APInt *C);
|
|
|
|
Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
|
|
|
|
const APInt *C);
|
|
|
|
Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div,
|
|
|
|
const APInt *C);
|
|
|
|
Instruction *foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub,
|
|
|
|
const APInt *C);
|
|
|
|
Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add,
|
|
|
|
const APInt *C);
|
2016-08-27 00:42:33 +08:00
|
|
|
Instruction *foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And,
|
2016-08-27 02:28:46 +08:00
|
|
|
const APInt *C1);
|
|
|
|
Instruction *foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And,
|
2016-09-08 06:33:03 +08:00
|
|
|
const APInt *C1, const APInt *C2);
|
2016-09-16 06:26:31 +08:00
|
|
|
Instruction *foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
|
|
|
|
const APInt &C2);
|
|
|
|
Instruction *foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
|
|
|
|
const APInt &C2);
|
2016-08-17 01:54:36 +08:00
|
|
|
|
2016-09-10 23:03:44 +08:00
|
|
|
Instruction *foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp,
|
|
|
|
BinaryOperator *BO,
|
|
|
|
const APInt *C);
|
|
|
|
Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, const APInt *C);
|
2016-07-22 02:07:40 +08:00
|
|
|
|
2016-09-30 06:18:30 +08:00
|
|
|
// Helpers of visitSelectInst().
|
2016-10-01 03:49:22 +08:00
|
|
|
Instruction *foldSelectExtConst(SelectInst &Sel);
|
2016-09-30 06:18:30 +08:00
|
|
|
Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
|
|
|
|
Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *);
|
|
|
|
Instruction *foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
|
|
|
|
Value *A, Value *B, Instruction &Outer,
|
|
|
|
SelectPatternFlavor SPF2, Value *C);
|
|
|
|
Instruction *foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
|
|
|
|
|
2017-04-03 01:57:30 +08:00
|
|
|
Instruction *OptAndOp(BinaryOperator *Op, ConstantInt *OpRHS,
|
2010-01-04 15:12:23 +08:00
|
|
|
ConstantInt *AndRHS, BinaryOperator &TheAnd);
|
2013-01-15 07:16:36 +08:00
|
|
|
|
2016-09-01 03:49:56 +08:00
|
|
|
Value *insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi,
|
|
|
|
bool isSigned, bool Inside);
|
2010-01-04 15:12:23 +08:00
|
|
|
Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
|
2016-05-26 00:22:14 +08:00
|
|
|
Instruction *MatchBSwap(BinaryOperator &I);
|
2010-01-04 15:12:23 +08:00
|
|
|
bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
|
2017-02-08 22:32:04 +08:00
|
|
|
|
|
|
|
Instruction *SimplifyElementAtomicMemCpy(ElementAtomicMemCpyInst *AMI);
|
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
|