[Evaluator] Simplify handling of bitcasted calls

When fetching the function, strip casts. When casting the result,
use the call result type. Don't actually inspect the bitcast.
This commit is contained in:
Nikita Popov 2022-01-25 14:18:05 +01:00
parent 4100cf2e92
commit 6a008de82a
2 changed files with 10 additions and 21 deletions

View File

@ -127,7 +127,7 @@ private:
}
/// Casts call result to a type of bitcast call expression
Constant *castCallResultIfNeeded(Value *CallExpr, Constant *RV);
Constant *castCallResultIfNeeded(Type *ReturnType, Constant *RV);
/// Given call site return callee and list of its formal arguments
Function *getCalleeWithFormalArgs(CallBase &CB,

View File

@ -245,17 +245,10 @@ static Function *getFunction(Constant *C) {
Function *
Evaluator::getCalleeWithFormalArgs(CallBase &CB,
SmallVectorImpl<Constant *> &Formals) {
auto *V = CB.getCalledOperand();
auto *V = CB.getCalledOperand()->stripPointerCasts();
if (auto *Fn = getFunction(getVal(V)))
return getFormalParams(CB, Fn, Formals) ? Fn : nullptr;
auto *CE = dyn_cast<ConstantExpr>(V);
if (!CE || CE->getOpcode() != Instruction::BitCast ||
!getFormalParams(CB, getFunction(CE->getOperand(0)), Formals))
return nullptr;
return dyn_cast<Function>(
ConstantFoldLoadThroughBitcast(CE, CE->getOperand(0)->getType(), DL));
return nullptr;
}
bool Evaluator::getFormalParams(CallBase &CB, Function *F,
@ -284,17 +277,13 @@ bool Evaluator::getFormalParams(CallBase &CB, Function *F,
/// If call expression contains bitcast then we may need to cast
/// evaluated return value to a type of the call expression.
Constant *Evaluator::castCallResultIfNeeded(Value *CallExpr, Constant *RV) {
ConstantExpr *CE = dyn_cast<ConstantExpr>(CallExpr);
if (!RV || !CE || CE->getOpcode() != Instruction::BitCast)
Constant *Evaluator::castCallResultIfNeeded(Type *ReturnType, Constant *RV) {
if (!RV || RV->getType() == ReturnType)
return RV;
if (auto *FT =
dyn_cast<FunctionType>(CE->getType()->getPointerElementType())) {
RV = ConstantFoldLoadThroughBitcast(RV, FT->getReturnType(), DL);
if (!RV)
LLVM_DEBUG(dbgs() << "Failed to fold bitcast call expr\n");
}
RV = ConstantFoldLoadThroughBitcast(RV, ReturnType, DL);
if (!RV)
LLVM_DEBUG(dbgs() << "Failed to fold bitcast call expr\n");
return RV;
}
@ -540,7 +529,7 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB,
if (Callee->isDeclaration()) {
// If this is a function we can constant fold, do it.
if (Constant *C = ConstantFoldCall(&CB, Callee, Formals, TLI)) {
InstResult = castCallResultIfNeeded(CB.getCalledOperand(), C);
InstResult = castCallResultIfNeeded(CB.getType(), C);
if (!InstResult)
return false;
LLVM_DEBUG(dbgs() << "Constant folded function call. Result: "
@ -564,7 +553,7 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB,
return false;
}
ValueStack.pop_back();
InstResult = castCallResultIfNeeded(CB.getCalledOperand(), RetVal);
InstResult = castCallResultIfNeeded(CB.getType(), RetVal);
if (RetVal && !InstResult)
return false;