32 #define DEBUG_TYPE "correlated-value-propagation"
34 STATISTIC(NumPhis,
"Number of phis propagated");
35 STATISTIC(NumSelects,
"Number of selects propagated");
36 STATISTIC(NumMemAccess,
"Number of memory access targets propagated");
37 STATISTIC(NumCmps,
"Number of comparisons propagated");
38 STATISTIC(NumReturns,
"Number of return values propagated");
39 STATISTIC(NumDeadCases,
"Number of switch cases removed");
40 STATISTIC(NumSDivs,
"Number of sdiv converted to udiv");
41 STATISTIC(NumAShrs,
"Number of ashr converted to lshr");
42 STATISTIC(NumSRems,
"Number of srem converted to urem");
65 "Value Propagation",
false,
false)
72 return new CorrelatedValuePropagation();
77 if (isa<Constant>(S->
getOperand(0)))
return false;
83 if (!CI)
return false;
104 if (isa<Constant>(Incoming))
continue;
120 if (
C->isOneValue()) {
122 }
else if (
C->isZeroValue()) {
145 DEBUG(
dbgs() <<
"CVP: Threading PHI over " << *SI <<
'\n');
167 Value *Pointer =
nullptr;
169 Pointer =
L->getPointerOperand();
173 if (isa<Constant>(Pointer))
return false;
176 if (!C)
return false;
190 if (!Op1)
return false;
228 if (isa<Instruction>(Cond) && cast<Instruction>(Cond)->
getParent() == BB)
233 if (PB == PE)
return false;
237 bool Changed =
false;
265 if (Value != State) {
273 CI.getCaseSuccessor()->removePredecessor(BB);
311 if (Type && !CS.
paramHasAttr(ArgNo + 1, Attribute::NonNull) &&
381 BO->setIsExact(SDI->
isExact());
400 BO->setIsExact(SDI->
isExact());
432 auto LazyRRange = [&] () {
438 bool Changed =
false;
442 OBO::NoUnsignedWrap);
444 bool NewNUW = NUWRange.
contains(LazyRRange());
454 bool NewNSW = NSWRange.
contains(LazyRRange());
470 if (!
C)
return nullptr;
472 Value *Op0 =
C->getOperand(0);
474 if (!Op1)
return nullptr;
487 bool FnChanged =
false;
495 bool BBChanged =
false;
502 case Instruction::PHI:
503 BBChanged |=
processPHI(cast<PHINode>(II), LVI);
505 case Instruction::ICmp:
506 case Instruction::FCmp:
507 BBChanged |=
processCmp(cast<CmpInst>(II), LVI);
514 case Instruction::Invoke:
517 case Instruction::SRem:
518 BBChanged |=
processSRem(cast<BinaryOperator>(II), LVI);
520 case Instruction::SDiv:
521 BBChanged |=
processSDiv(cast<BinaryOperator>(II), LVI);
523 case Instruction::AShr:
524 BBChanged |=
processAShr(cast<BinaryOperator>(II), LVI);
527 BBChanged |=
processAdd(cast<BinaryOperator>(II), LVI);
538 auto *RI = cast<ReturnInst>(Term);
542 auto *RetVal = RI->getReturnValue();
544 if (isa<Constant>(RetVal))
break;
547 RI->replaceUsesOfWith(RetVal,
C);
553 FnChanged |= BBChanged;
559 bool CorrelatedValuePropagation::runOnFunction(
Function &
F) {
563 LazyValueInfo *LVI = &getAnalysis<LazyValueInfoWrapperPass>().getLVI();
571 bool Changed =
runImpl(F, LVI);
Legacy wrapper pass to provide the GlobalsAAResult object.
Pass interface - Implemented by all 'passes'.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
static ConstantInt * getFalse(LLVMContext &Context)
CaseIt case_end()
Returns a read/write iterator that points one past the last in the SwitchInst.
This class is the base class for the comparison instructions.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
ConstantRange getConstantRange(Value *V, BasicBlock *BB, Instruction *CxtI=nullptr)
Return the ConstantRange constraint that is known to hold for the specified value at the end of the s...
STATISTIC(NumFunctions,"Total number of functions")
Wrapper around LazyValueInfo.
This is the interface for a simple mod/ref and alias analysis over globals.
void setAttributes(AttributeSet PAL)
CaseIt case_begin()
Returns a read/write iterator that points to the first case in the SwitchInst.
An instruction for reading from memory.
unsigned arg_size() const
StringRef getName() const
Return a constant reference to the value's name.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Constant * getConstant(Value *V, BasicBlock *BB, Instruction *CxtI=nullptr)
Determine whether the specified value is known to be a constant at the end of the specified block...
static Value * getPointerOperand(Instruction &Inst)
This class represents the LLVM 'select' instruction.
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
LLVM_NODISCARD bool empty() const
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
const T & getValue() const LLVM_LVALUE_FUNCTION
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Class to represent pointers.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Pass * createCorrelatedValuePropagationPass()
initializer< Ty > init(const Ty &Val)
iterator_range< IterTy > args() const
A set of analyses that are preserved following a run of a transformation pass.
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Constant * getConstantOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB, Instruction *CxtI=nullptr)
Determine whether the specified value is known to be a constant on the specified edge.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
bool isExact() const
Determine whether the exact flag is set.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
bool isVectorTy() const
True if this is an instance of VectorType.
AttributeSet addAttribute(LLVMContext &C, unsigned Index, Attribute::AttrKind Kind) const
Add an attribute to the attribute set at the given index.
This is an important base class in LLVM.
const Value * getCondition() const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Represent the analysis usage information of a pass.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Analysis pass providing a never-invalidated alias analysis result.
Utility class for integer arithmetic operators which may exhibit overflow - Add, Sub, and Mul.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
Value * getOperand(unsigned i) const
Interval::pred_iterator pred_end(Interval *I)
Predicate getPredicate() const
Return the predicate for this instruction.
bool isEmptySet() const
Return true if this set contains no members.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
LLVMContext & getContext() const
All values hold a context through their type.
const Value * getTrueValue() const
Tristate
This is used to return true/false/dunno results.
Tristate getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, BasicBlock *FromBB, BasicBlock *ToBB, Instruction *CxtI=nullptr)
Determine whether the specified value comparison with a constant is known to be true or false on the ...
AttributeSet getAttributes() const
getAttributes/setAttributes - get or set the parameter attributes of the call.
Tristate getPredicateAt(unsigned Pred, Value *V, Constant *C, Instruction *CxtI)
Determine whether the specified value comparison with a constant is known to be true or false at the ...
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
Iterator for intrusive lists based on ilist_node.
This is the shared class of boolean and integer constants.
void initializeCorrelatedValuePropagationPass(PassRegistry &)
InstrTy * getInstruction() const
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
This class represents a range of values.
void invalidate(IRUnitT &IR)
Invalidate a specific analysis pass for an IR module.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
const BasicBlock & getEntryBlock() const
static ConstantInt * getTrue(LLVMContext &Context)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Value * getCondition() const
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
void setCondition(Value *V)
bool paramHasAttr(unsigned i, Attribute::AttrKind Kind) const
Return true if the call or the callee has the given attribute.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void preserve()
Mark an analysis as preserved.
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
void removeCase(CaseIt i)
This method removes the specified case and its successor from the switch instruction.
unsigned getNumCases() const
Return the number of 'cases' in this switch instruction, excluding the default case.
iterator_range< df_iterator< T > > depth_first(const T &G)
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
This pass computes, caches, and vends lazy value constraint information.
static ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp, const ConstantRange &Other, unsigned NoWrapKind)
Return the largest range containing all X such that "X BinOpC Y" is guaranteed not to wrap (overflow)...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
static const Function * getParent(const Value *V)
const Value * getFalseValue() const
A container for analyses that lazily runs them and caches their results.
Value * SimplifyInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
See if we can compute a simplified version of this instruction.
void setIncomingValue(unsigned i, Value *V)
const BasicBlock * getParent() const
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Analysis to compute lazy value information.