Skip to content

Commit

Permalink
[SystemZ] Fix check for zero size when lowering memcmp.
Browse files Browse the repository at this point in the history
During lowering of memcmp/bcmp, the check for a size of 0 is done
in 2 different ways. In rare cases this can lead to a crash in
SystemZSelectionDAGInfo::EmitTargetCodeForMemcmp(). The root cause
is that SelectionDAGBuilder::visitMemCmpBCmpCall() checks for a
constant int value which is not yet evaluated. When the value is
turned into a SDValue, then the evaluation is done and results in
a ConstantSDNode. But EmitTargetCodeForMemcmp() expects the special
case of 0 length to be handled, which results in an assertion.

The fix is to turn the value into a SDValue, so that both functions
use the same check.

Reviewed By: uweigand

Differential Revision: https://reviews.llvm.org/D126900
  • Loading branch information
redstar committed Jun 8, 2022
1 parent 4636b93 commit d897a14
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Expand Up @@ -7902,7 +7902,7 @@ void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
const Value *Size = I.getArgOperand(2);
const ConstantInt *CSize = dyn_cast<ConstantInt>(Size);
const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
if (CSize && CSize->getZExtValue() == 0) {
EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
I.getType(), true);
Expand Down
20 changes: 20 additions & 0 deletions llvm/test/CodeGen/SystemZ/memcmp-03.ll
@@ -0,0 +1,20 @@
; Test memcmp with 0 size.

; RUN: llc < %s -mtriple=s390x-linux-gnu | FileCheck %s
; REQUIRES: asserts

declare i32 @memcmp(i8* nocapture, i8* nocapture, i64)

define hidden void @fun() {
; CHECK-LABEL: fun
entry:
%len = extractvalue [2 x i64] zeroinitializer, 1
br i1 undef, label %end, label %call

call:
%res = tail call signext i32 @memcmp(i8* noundef undef, i8* noundef undef, i64 noundef %len)
unreachable

end:
unreachable
}

0 comments on commit d897a14

Please sign in to comment.