From 4b9467daae8b47322a21180a5cd08493dfbc9271 Mon Sep 17 00:00:00 2001 From: Hyxogen <8938732+Hyxogen@users.noreply.github.com> Date: Tue, 13 Feb 2024 10:15:45 +0100 Subject: [PATCH] partialexecuter: check for division/remainder by zero Fixes: https://github.com/leaningtech/cheerp-meta/issues/139 Fixes: https://github.com/leaningtech/cheerp-meta/issues/140 --- llvm/lib/CheerpWriter/PartialExecuter.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/llvm/lib/CheerpWriter/PartialExecuter.cpp b/llvm/lib/CheerpWriter/PartialExecuter.cpp index 667cc2942091..a63f14280a13 100644 --- a/llvm/lib/CheerpWriter/PartialExecuter.cpp +++ b/llvm/lib/CheerpWriter/PartialExecuter.cpp @@ -463,9 +463,6 @@ class PartialInterpreter : public llvm::Interpreter { if (!areOperandsComputed(I)) return true; - if (isa(I) || isa(I)) - return false; - switch (I.getOpcode()) { case Instruction::Br: @@ -516,9 +513,21 @@ class PartialInterpreter : public llvm::Interpreter { // TODO(carlo): Possibly even Loads of Alloca might be proven valid break; } + case Instruction::SRem: + case Instruction::URem: + case Instruction::SDiv: + case Instruction::UDiv: + { + Value *Src2 = I.getOperand(1); + if (isValueComputed(Src2) && getOperandValue(Src2).IntVal.isZero()) + return true; // Division or remainder by zero + return false; // Is a binary operator + } default: { - //Default case is for Instruction to be skipped + if (isa(I) || isa(I)) + return false; + // Default case is for Instruction to be skipped return true; } }