2009-11-11 01:22:30 +01:00
|
|
|
//===- LazyValueInfo.h - Value constraint analysis --------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-11-11 01:22:30 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the interface for lazy computation of value constraint
|
|
|
|
// information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-25 21:28:39 +02:00
|
|
|
#ifndef LLVM_ANALYSIS_LAZYVALUEINFO_H
|
|
|
|
#define LLVM_ANALYSIS_LAZYVALUEINFO_H
|
2009-11-11 01:22:30 +01:00
|
|
|
|
2016-06-14 00:01:25 +02:00
|
|
|
#include "llvm/IR/PassManager.h"
|
2009-11-11 01:22:30 +01:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
2016-12-19 09:22:17 +01:00
|
|
|
class AssumptionCache;
|
2009-11-11 03:08:33 +01:00
|
|
|
class Constant;
|
2016-05-02 21:58:00 +02:00
|
|
|
class ConstantRange;
|
2012-10-08 18:38:25 +02:00
|
|
|
class DataLayout;
|
2014-09-07 22:29:59 +02:00
|
|
|
class DominatorTree;
|
|
|
|
class Instruction;
|
2011-12-02 02:26:24 +01:00
|
|
|
class TargetLibraryInfo;
|
2009-11-11 03:08:33 +01:00
|
|
|
class Value;
|
2015-07-28 17:53:21 +02:00
|
|
|
|
2015-01-08 23:36:56 +01:00
|
|
|
/// This pass computes, caches, and vends lazy value constraint information.
|
2016-06-14 00:01:25 +02:00
|
|
|
class LazyValueInfo {
|
|
|
|
friend class LazyValueInfoWrapperPass;
|
2016-12-19 09:22:17 +01:00
|
|
|
AssumptionCache *AC = nullptr;
|
2017-03-12 15:06:41 +01:00
|
|
|
const DataLayout *DL = nullptr;
|
2016-06-14 00:01:25 +02:00
|
|
|
class TargetLibraryInfo *TLI = nullptr;
|
|
|
|
void *PImpl = nullptr;
|
2015-02-15 23:54:22 +01:00
|
|
|
LazyValueInfo(const LazyValueInfo&) = delete;
|
|
|
|
void operator=(const LazyValueInfo&) = delete;
|
2009-11-11 01:22:30 +01:00
|
|
|
public:
|
2016-06-14 00:01:25 +02:00
|
|
|
~LazyValueInfo();
|
|
|
|
LazyValueInfo() {}
|
2020-03-25 20:02:07 +01:00
|
|
|
LazyValueInfo(AssumptionCache *AC_, const DataLayout *DL_,
|
|
|
|
TargetLibraryInfo *TLI_)
|
|
|
|
: AC(AC_), DL(DL_), TLI(TLI_) {}
|
2016-06-14 00:01:25 +02:00
|
|
|
LazyValueInfo(LazyValueInfo &&Arg)
|
2020-03-25 20:02:07 +01:00
|
|
|
: AC(Arg.AC), DL(Arg.DL), TLI(Arg.TLI), PImpl(Arg.PImpl) {
|
2016-06-14 00:01:25 +02:00
|
|
|
Arg.PImpl = nullptr;
|
|
|
|
}
|
|
|
|
LazyValueInfo &operator=(LazyValueInfo &&Arg) {
|
|
|
|
releaseMemory();
|
2016-12-19 09:22:17 +01:00
|
|
|
AC = Arg.AC;
|
2017-03-12 15:06:41 +01:00
|
|
|
DL = Arg.DL;
|
2016-06-14 00:01:25 +02:00
|
|
|
TLI = Arg.TLI;
|
|
|
|
PImpl = Arg.PImpl;
|
|
|
|
Arg.PImpl = nullptr;
|
|
|
|
return *this;
|
2010-10-19 19:21:58 +02:00
|
|
|
}
|
2009-11-11 03:08:33 +01:00
|
|
|
|
2015-01-08 23:36:56 +01:00
|
|
|
/// This is used to return true/false/dunno results.
|
2009-11-11 03:08:33 +01:00
|
|
|
enum Tristate {
|
2009-11-12 05:36:58 +01:00
|
|
|
Unknown = -1, False = 0, True = 1
|
2009-11-11 03:08:33 +01:00
|
|
|
};
|
2015-07-28 17:53:21 +02:00
|
|
|
|
2009-11-11 03:08:33 +01:00
|
|
|
// Public query interface.
|
2015-07-28 17:53:21 +02:00
|
|
|
|
2015-01-08 23:36:56 +01:00
|
|
|
/// Determine whether the specified value comparison with a constant is known
|
|
|
|
/// to be true or false on the specified CFG edge.
|
2009-11-12 05:36:58 +01:00
|
|
|
/// Pred is a CmpInst predicate.
|
|
|
|
Tristate getPredicateOnEdge(unsigned Pred, Value *V, Constant *C,
|
2014-09-07 22:29:59 +02:00
|
|
|
BasicBlock *FromBB, BasicBlock *ToBB,
|
|
|
|
Instruction *CxtI = nullptr);
|
2015-07-28 17:53:21 +02:00
|
|
|
|
2015-01-08 23:36:56 +01:00
|
|
|
/// Determine whether the specified value comparison with a constant is known
|
2020-08-30 16:20:12 +02:00
|
|
|
/// to be true or false at the specified instruction.
|
|
|
|
/// \p Pred is a CmpInst predicate. If \p UseBlockValue is true, the block
|
|
|
|
/// value is also taken into account.
|
2014-09-07 22:29:59 +02:00
|
|
|
Tristate getPredicateAt(unsigned Pred, Value *V, Constant *C,
|
2021-04-10 19:45:37 +02:00
|
|
|
Instruction *CxtI, bool UseBlockValue);
|
2015-07-28 17:53:21 +02:00
|
|
|
|
2021-04-10 23:23:27 +02:00
|
|
|
/// Determine whether the specified value comparison is known to be true
|
|
|
|
/// or false at the specified instruction. While this takes two Value's,
|
|
|
|
/// it still requires that one of them is a constant.
|
|
|
|
/// \p Pred is a CmpInst predicate.
|
|
|
|
/// If \p UseBlockValue is true, the block value is also taken into account.
|
|
|
|
Tristate getPredicateAt(unsigned Pred, Value *LHS, Value *RHS,
|
|
|
|
Instruction *CxtI, bool UseBlockValue);
|
|
|
|
|
2020-09-27 17:41:39 +02:00
|
|
|
/// Determine whether the specified value is known to be a constant at the
|
|
|
|
/// specified instruction. Return null if not.
|
|
|
|
Constant *getConstant(Value *V, Instruction *CxtI);
|
2009-11-12 02:29:10 +01:00
|
|
|
|
2016-05-02 21:58:00 +02:00
|
|
|
/// Return the ConstantRange constraint that is known to hold for the
|
2020-09-27 17:41:39 +02:00
|
|
|
/// specified value at the specified instruction. This may only be called
|
2016-05-02 21:58:00 +02:00
|
|
|
/// on integer-typed Values.
|
2020-09-27 17:41:39 +02:00
|
|
|
ConstantRange getConstantRange(Value *V, Instruction *CxtI,
|
[ValueLattice] Distinguish between constant ranges with/without undef.
This patch updates ValueLattice to distinguish between ranges that are
guaranteed to not include undef and ranges that may include undef.
A constant range guaranteed to not contain undef can be used to simplify
instructions to arbitrary values. A constant range that may contain
undef can only be used to simplify to a constant. If the value can be
undef, it might take a value outside the range. For example, consider
the snipped below
define i32 @f(i32 %a, i1 %c) {
br i1 %c, label %true, label %false
true:
%a.255 = and i32 %a, 255
br label %exit
false:
br label %exit
exit:
%p = phi i32 [ %a.255, %true ], [ undef, %false ]
%f.1 = icmp eq i32 %p, 300
call void @use(i1 %f.1)
%res = and i32 %p, 255
ret i32 %res
}
In the exit block, %p would be a constant range [0, 256) including undef as
%p could be undef. We can use the range information to replace %f.1 with
false because we remove the compare, effectively forcing the use of the
constant to be != 300. We cannot replace %res with %p however, because
if %a would be undef %cond may be true but the second use might not be
< 256.
Currently LazyValueInfo uses the new behavior just when simplifying AND
instructions and does not distinguish between constant ranges with and
without undef otherwise. I think we should address the remaining issues
in LVI incrementally.
Reviewers: efriedma, reames, aqjune, jdoerfert, sstefan1
Reviewed By: efriedma
Differential Revision: https://reviews.llvm.org/D76931
2020-03-31 12:10:00 +02:00
|
|
|
bool UndefAllowed = true);
|
2016-05-02 21:58:00 +02:00
|
|
|
|
2015-01-08 23:36:56 +01:00
|
|
|
/// Determine whether the specified value is known to be a
|
2009-11-12 02:29:10 +01:00
|
|
|
/// constant on the specified edge. Return null if not.
|
2014-09-07 22:29:59 +02:00
|
|
|
Constant *getConstantOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
|
|
|
|
Instruction *CxtI = nullptr);
|
2015-07-28 17:53:21 +02:00
|
|
|
|
2017-06-23 07:41:35 +02:00
|
|
|
/// Return the ConstantRage constraint that is known to hold for the
|
|
|
|
/// specified value on the specified edge. This may be only be called
|
|
|
|
/// on integer-typed Values.
|
|
|
|
ConstantRange getConstantRangeOnEdge(Value *V, BasicBlock *FromBB,
|
|
|
|
BasicBlock *ToBB,
|
|
|
|
Instruction *CxtI = nullptr);
|
|
|
|
|
2015-01-08 23:36:56 +01:00
|
|
|
/// Inform the analysis cache that we have threaded an edge from
|
2010-07-26 20:48:03 +02:00
|
|
|
/// PredBB to OldSucc to be from PredBB to NewSucc instead.
|
|
|
|
void threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, BasicBlock *NewSucc);
|
2015-07-28 17:53:21 +02:00
|
|
|
|
2015-01-08 23:36:56 +01:00
|
|
|
/// Inform the analysis cache that we have erased a block.
|
2010-08-18 20:39:01 +02:00
|
|
|
void eraseBlock(BasicBlock *BB);
|
2015-07-28 17:53:21 +02:00
|
|
|
|
2017-06-06 21:25:31 +02:00
|
|
|
/// Print the \LazyValueInfo Analysis.
|
|
|
|
/// We pass in the DTree that is required for identifying which basic blocks
|
2020-03-25 20:02:07 +01:00
|
|
|
/// we can solve/print for, in the LVIPrinter.
|
2017-06-06 21:25:31 +02:00
|
|
|
void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS);
|
2017-03-22 20:27:12 +01:00
|
|
|
|
2016-06-14 00:01:25 +02:00
|
|
|
// For old PM pass. Delete once LazyValueInfoWrapperPass is gone.
|
|
|
|
void releaseMemory();
|
2017-01-23 07:35:12 +01:00
|
|
|
|
|
|
|
/// Handle invalidation events in the new pass manager.
|
|
|
|
bool invalidate(Function &F, const PreservedAnalyses &PA,
|
|
|
|
FunctionAnalysisManager::Invalidator &Inv);
|
2016-06-14 00:01:25 +02:00
|
|
|
};
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Analysis to compute lazy value information.
|
2016-06-14 00:01:25 +02:00
|
|
|
class LazyValueAnalysis : public AnalysisInfoMixin<LazyValueAnalysis> {
|
|
|
|
public:
|
|
|
|
typedef LazyValueInfo Result;
|
|
|
|
Result run(Function &F, FunctionAnalysisManager &FAM);
|
|
|
|
|
|
|
|
private:
|
2016-11-23 18:53:26 +01:00
|
|
|
static AnalysisKey Key;
|
2016-06-14 00:01:25 +02:00
|
|
|
friend struct AnalysisInfoMixin<LazyValueAnalysis>;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Wrapper around LazyValueInfo.
|
|
|
|
class LazyValueInfoWrapperPass : public FunctionPass {
|
|
|
|
LazyValueInfoWrapperPass(const LazyValueInfoWrapperPass&) = delete;
|
|
|
|
void operator=(const LazyValueInfoWrapperPass&) = delete;
|
|
|
|
public:
|
|
|
|
static char ID;
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 22:15:01 +01:00
|
|
|
LazyValueInfoWrapperPass();
|
2016-06-14 00:01:25 +02:00
|
|
|
~LazyValueInfoWrapperPass() override {
|
|
|
|
assert(!Info.PImpl && "releaseMemory not called");
|
|
|
|
}
|
|
|
|
|
|
|
|
LazyValueInfo &getLVI();
|
2014-03-05 08:30:04 +01:00
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
void releaseMemory() override;
|
|
|
|
bool runOnFunction(Function &F) override;
|
2016-06-14 00:01:25 +02:00
|
|
|
private:
|
|
|
|
LazyValueInfo Info;
|
2009-11-11 01:22:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|