From ee9617e96b05de04b2f189596b86c6149c807764 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Wed, 29 Jul 2020 13:12:51 -0400 Subject: [PATCH] [InstSimplify] try constant folding intrinsics before general simplifications This matches the behavior of simplify calls for regular opcodes - rely on ConstantFolding before spending time on folds with variables. I am not aware of any diffs from this re-ordering currently, but there was potential for unintended behavior from the min/max intrinsics because that code is implicitly assuming that only 1 of the input operands is constant. --- llvm/lib/Analysis/InstructionSimplify.cpp | 48 +++++++++++++++++-------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index af487f9..de4e23a 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -5484,28 +5484,9 @@ static Value *simplifyIntrinsic(CallBase *Call, const SimplifyQuery &Q) { } } -Value *llvm::SimplifyCall(CallBase *Call, const SimplifyQuery &Q) { - Value *Callee = Call->getCalledOperand(); - - // musttail calls can only be simplified if they are also DCEd. - // As we can't guarantee this here, don't simplify them. - if (Call->isMustTailCall()) - return nullptr; - - // call undef -> undef - // call null -> undef - if (isa(Callee) || isa(Callee)) - return UndefValue::get(Call->getType()); - - Function *F = dyn_cast(Callee); - if (!F) - return nullptr; - - if (F->isIntrinsic()) - if (Value *Ret = simplifyIntrinsic(Call, Q)) - return Ret; - - if (!canConstantFoldCallTo(Call, F)) +static Value *tryConstantFoldCall(CallBase *Call, const SimplifyQuery &Q) { + auto *F = dyn_cast(Call->getCalledOperand()); + if (!F || !canConstantFoldCallTo(Call, F)) return nullptr; SmallVector ConstantArgs; @@ -5524,6 +5505,29 @@ Value *llvm::SimplifyCall(CallBase *Call, const SimplifyQuery &Q) { return ConstantFoldCall(Call, F, ConstantArgs, Q.TLI); } +Value *llvm::SimplifyCall(CallBase *Call, const SimplifyQuery &Q) { + // musttail calls can only be simplified if they are also DCEd. + // As we can't guarantee this here, don't simplify them. + if (Call->isMustTailCall()) + return nullptr; + + // call undef -> undef + // call null -> undef + Value *Callee = Call->getCalledOperand(); + if (isa(Callee) || isa(Callee)) + return UndefValue::get(Call->getType()); + + if (Value *V = tryConstantFoldCall(Call, Q)) + return V; + + auto *F = dyn_cast(Callee); + if (F && F->isIntrinsic()) + if (Value *Ret = simplifyIntrinsic(Call, Q)) + return Ret; + + return nullptr; +} + /// Given operands for a Freeze, see if we can fold the result. static Value *SimplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) { // Use a utility function defined in ValueTracking. -- 2.7.4