39#define DEBUG_TYPE "evaluator"
63 return !GV->hasDLLImportStorageClass() && !GV->isThreadLocal();
83 switch (CE->getOpcode()) {
84 case Instruction::BitCast:
88 case Instruction::IntToPtr:
89 case Instruction::PtrToInt:
92 if (
DL.getTypeSizeInBits(CE->getType()) !=
93 DL.getTypeSizeInBits(CE->getOperand(0)->getType()))
98 case Instruction::GetElementPtr:
99 for (
unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
104 case Instruction::Add:
118 if (!SimpleConstants.
insert(
C).second)
124void Evaluator::MutableValue::clear() {
131 const DataLayout &DL)
const {
132 TypeSize TySize = DL.getTypeStoreSize(Ty);
133 const MutableValue *
V =
this;
135 Type *AggTy = Agg->Ty;
136 std::optional<APInt>
Index = DL.getGEPIndexForOffset(AggTy,
Offset);
137 if (!Index ||
Index->uge(Agg->Elements.size()) ||
141 V = &Agg->Elements[
Index->getZExtValue()];
147bool Evaluator::MutableValue::makeMutable() {
149 Type *Ty =
C->getType();
150 unsigned NumElements;
152 NumElements = VT->getNumElements();
154 NumElements = AT->getNumElements();
156 NumElements =
ST->getNumElements();
160 MutableAggregate *MA =
new MutableAggregate(Ty);
161 MA->Elements.reserve(NumElements);
162 for (
unsigned I = 0;
I < NumElements; ++
I)
163 MA->Elements.push_back(
C->getAggregateElement(
I));
168bool Evaluator::MutableValue::write(Constant *
V, APInt
Offset,
169 const DataLayout &DL) {
170 Type *Ty =
V->getType();
171 TypeSize TySize = DL.getTypeStoreSize(Ty);
172 MutableValue *MV =
this;
179 Type *AggTy = Agg->Ty;
180 std::optional<APInt>
Index = DL.getGEPIndexForOffset(AggTy,
Offset);
181 if (!Index ||
Index->uge(Agg->Elements.size()) ||
185 MV = &Agg->Elements[
Index->getZExtValue()];
188 Type *MVType = MV->getType();
194 else if (Ty != MVType)
201Constant *Evaluator::MutableAggregate::toConstant()
const {
203 for (
const MutableValue &MV : Elements)
216Constant *Evaluator::ComputeLoadResult(Constant *
P,
Type *Ty) {
217 APInt
Offset(DL.getIndexTypeSizeInBits(
P->getType()), 0);
220 Offset =
Offset.sextOrTrunc(DL.getIndexTypeSizeInBits(
P->getType()));
222 return ComputeLoadResult(GV, Ty,
Offset);
226Constant *Evaluator::ComputeLoadResult(GlobalVariable *GV,
Type *Ty,
228 auto It = MutatedMemory.find(GV);
229 if (It != MutatedMemory.end())
230 return It->second.read(Ty,
Offset, DL);
248Evaluator::getCalleeWithFormalArgs(CallBase &CB,
249 SmallVectorImpl<Constant *> &Formals) {
252 return getFormalParams(CB, Fn, Formals) ? Fn :
nullptr;
256bool Evaluator::getFormalParams(CallBase &CB,
Function *
F,
257 SmallVectorImpl<Constant *> &Formals) {
258 auto *FTy =
F->getFunctionType();
267 constexpr uint64_t MaxByValCopySize = 256;
271 APInt
Base(DL.getIndexTypeSizeInBits(Arg->
getType()), 0);
274 if (!GV || !ParamAlign ||
Size > MaxByValCopySize) {
282 ComputeLoadResult(GV, I8Ty,
Base + APInt(
Base.getBitWidth(),
Off));
290 auto NewGV = std::make_unique<GlobalVariable>(
295 NewGV->setAlignment(*ParamAlign);
296 AllocaTmps.push_back(std::move(NewGV));
297 Arg = AllocaTmps.back().get();
309 bool &StrippedPointerCastsForAliasAnalysis) {
314 LLVM_DEBUG(
dbgs() <<
"Evaluating Instruction: " << *CurInst <<
"\n");
317 if (
SI->isVolatile()) {
323 if (Ptr != FoldedPtr) {
324 LLVM_DEBUG(
dbgs() <<
"Folding constant ptr expression: " << *Ptr);
341 LLVM_DEBUG(
dbgs() <<
"Store is not to global with unique initializer: "
350 LLVM_DEBUG(
dbgs() <<
"Store value is too complex to evaluate store. "
356 if (!Res.first->second.write(Val,
Offset, DL))
359 if (LI->isVolatile()) {
361 dbgs() <<
"Found a Load! Volatile load, can not evaluate.\n");
365 Constant *Ptr = getVal(LI->getOperand(0));
367 if (Ptr != FoldedPtr) {
369 LLVM_DEBUG(
dbgs() <<
"Found a constant pointer expression, constant "
373 InstResult = ComputeLoadResult(Ptr, LI->getType());
376 dbgs() <<
"Failed to compute load result. Can not evaluate load."
383 if (AI->isArrayAllocation()) {
384 LLVM_DEBUG(
dbgs() <<
"Found an array alloca. Can not evaluate.\n");
387 Type *Ty = AI->getAllocatedType();
388 AllocaTmps.push_back(std::make_unique<GlobalVariable>(
391 AI->getType()->getPointerAddressSpace()));
392 InstResult = AllocaTmps.back().get();
393 LLVM_DEBUG(
dbgs() <<
"Found an alloca. Result: " << *InstResult <<
"\n");
405 if (MSI->isVolatile()) {
417 Constant *Ptr = getVal(MSI->getDest());
427 Constant *Val = getVal(MSI->getValue());
430 if (!Val->
isNullValue() || MutatedMemory.contains(GV) ||
433 APInt
Len = LenC->getValue();
434 if (
Len.ugt(64 * 1024)) {
442 if (DestVal != Val) {
444 <<
Offset <<
" of " << *GV <<
".\n");
457 if (
II->isLifetimeStartOrEnd()) {
463 if (
II->getIntrinsicID() == Intrinsic::invariant_start) {
466 if (!
II->use_empty()) {
468 <<
"Found unused invariant_start. Can't evaluate.\n");
472 Value *PtrArg = getVal(
II->getArgOperand(1));
476 if (!
Size->isMinusOne() &&
477 Size->getValue().getLimitedValue() >= MinGVSize) {
478 Invariants.insert(GV);
483 <<
"Found a global var, but can not treat it as an "
490 }
else if (
II->getIntrinsicID() == Intrinsic::assume) {
494 }
else if (
II->getIntrinsicID() == Intrinsic::sideeffect) {
498 }
else if (
II->getIntrinsicID() == Intrinsic::pseudoprobe) {
503 Value *Stripped = CurInst->stripPointerCastsForAliasAnalysis();
507 if (Stripped != &*CurInst) {
508 InstResult = getVal(Stripped);
512 <<
"Stripped pointer casts for alias analysis for "
513 "intrinsic call.\n");
514 StrippedPointerCastsForAliasAnalysis =
true;
527 if (!Callee ||
Callee->isInterposable()) {
532 if (
Callee->isDeclaration()) {
537 << *InstResult <<
"\n");
543 if (
Callee->getFunctionType()->isVarArg()) {
545 <<
"Can not constant fold vararg function call.\n");
551 ValueStack.emplace_back();
556 ValueStack.pop_back();
560 << *InstResult <<
"\n\n");
563 <<
"Successfully evaluated function. Result: 0\n\n");
567 }
else if (CurInst->isTerminator()) {
571 NextBB = BI->getSuccessor(0);
576 NextBB = BI->getSuccessor(!
Cond->getZExtValue());
580 if (!Val)
return false;
581 NextBB =
SI->findCaseValue(Val)->getCaseSuccessor();
585 NextBB = BA->getBasicBlock();
601 for (
Value *
Op : CurInst->operands())
602 Ops.push_back(getVal(
Op));
605 LLVM_DEBUG(
dbgs() <<
"Cannot fold instruction: " << *CurInst <<
"\n");
609 << *InstResult <<
"\n");
612 if (!CurInst->use_empty()) {
614 setVal(&*CurInst, InstResult);
619 NextBB =
II->getNormalDest();
620 LLVM_DEBUG(
dbgs() <<
"Found an invoke instruction. Finished Block.\n\n");
634 assert(ActualArgs.
size() ==
F->arg_size() &&
"wrong number of arguments");
641 CallStack.push_back(
F);
645 setVal(&Arg, ActualArgs[ArgNo]);
659 LLVM_DEBUG(
dbgs() <<
"Trying to evaluate BB: " << *CurBB <<
"\n");
661 bool StrippedPointerCastsForAliasAnalysis =
false;
663 if (!EvaluateBlock(CurInst, NextBB, StrippedPointerCastsForAliasAnalysis))
676 if (StrippedPointerCastsForAliasAnalysis &&
682 CallStack.pop_back();
689 if (!ExecutedBlocks.
insert(NextBB).second)
696 for (CurInst = NextBB->
begin();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
static bool isSimpleEnoughValueToCommitHelper(Constant *C, SmallPtrSetImpl< Constant * > &SimpleConstants, const DataLayout &DL)
Return true if the specified constant can be handled by the code generator.
static bool isSimpleEnoughValueToCommit(Constant *C, SmallPtrSetImpl< Constant * > &SimpleConstants, const DataLayout &DL)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
uint64_t IntrinsicInst * II
SmallVector< MachineOperand, 4 > Cond
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static Function * getFunction(FunctionType *Ty, const Twine &Name, Module *M)
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
InstListType::iterator iterator
Instruction iterators...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
bool isInlineAsm() const
Check if this call is an inline asm statement.
bool isByValArgument(unsigned ArgNo) const
Determine whether this argument is passed by value.
MaybeAlign getParamAlign(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
Type * getParamByValType(unsigned ArgNo) const
Extract the byval type for a call or parameter.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
FunctionType * getFunctionType() const
unsigned arg_size() const
static LLVM_ABI bool isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, const DataLayout &DL)
Check whether a bitcast, inttoptr, or ptrtoint cast between these types is valid and a no-op.
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
A constant value that is initialized with an expression using other constant values.
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
const Constant * stripPointerCasts() const
A parsed version of the target data layout string in and methods for querying it.
LLVM_ABI bool EvaluateFunction(Function *F, Constant *&RetVal, const SmallVectorImpl< Constant * > &ActualArgs)
Evaluate a call to function F, returning true if successful, false if we can't evaluate it.
bool hasSection() const
Check if this global has a custom object file section.
@ InternalLinkage
Rename collisions when linking (static functions).
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ABI uint64_t getGlobalSize(const DataLayout &DL) const
Get the size of this global variable in bytes.
bool hasUniqueInitializer() const
hasUniqueInitializer - Whether the global variable has an initializer, and any changes made to the in...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
Value * getIncomingValueForBlock(const BasicBlock *BB) const
Return a value (possibly void), from a function.
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
bool isPointerTy() const
True if this is an instance of PointerType.
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
bool isVoidTy() const
Return true if this is 'void'.
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVMContext & getContext() const
All values hold a context through their type.
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
This is an optimization pass for GlobalISel generic memory operations.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
LLVM_ABI Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
LLVM_ABI Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
LLVM_ABI Constant * ConstantFoldInstOperands(const Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.