/*========================== begin_copyright_notice ============================ Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. See https://llvm.org/LICENSE.txt for license information. SPDX-License-Identifier: Apache-2.0 with LLVM-exception ============================= end_copyright_notice ===========================*/ From 6ed8606c9b1c6d4150225da3da6399bcc9b749f8 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Fri, 28 Jan 2022 20:33:41 +0300 Subject: [PATCH] [PATCH] [IR] GetUnderlyingObject(), stripPointerCastsAndOffsets(): don't crash on `bitcast <1 x i8*> to i8*` I'm not sure how to write standalone tests for each of two changes here. If either one of these two fixes is missing, the test fill crash. --- llvm/lib/Analysis/ValueTracking.cpp | 2 ++ llvm/lib/IR/Value.cpp | 2 ++ llvm/test/Transforms/InstSimplify/icmp.ll | 14 ++++++++++++++ 3 files changed, 18 insertions(+) create mode 100644 llvm/test/Transforms/InstSimplify/icmp.ll diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index c70906dcc629..7ba473527f4e 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -3707,6 +3707,8 @@ Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL, } else if (Operator::getOpcode(V) == Instruction::BitCast || Operator::getOpcode(V) == Instruction::AddrSpaceCast) { V = cast(V)->getOperand(0); + if (!V->getType()->isPointerTy()) + return V; } else if (GlobalAlias *GA = dyn_cast(V)) { if (GA->isInterposable()) return V; diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp index b7f77dc3043e..e1d24af11d2b 100644 --- a/llvm/lib/IR/Value.cpp +++ b/llvm/lib/IR/Value.cpp @@ -498,6 +498,8 @@ static const Value *stripPointerCastsAndOffsets(const Value *V) { V = GEP->getPointerOperand(); } else if (Operator::getOpcode(V) == Instruction::BitCast) { V = cast(V)->getOperand(0); + if (!V->getType()->isPointerTy()) + return V; } else if (StripKind != PSK_ZeroIndicesAndAliasesSameRepresentation && Operator::getOpcode(V) == Instruction::AddrSpaceCast) { // TODO: If we know an address space cast will not change the diff --git a/llvm/test/Transforms/InstSimplify/icmp.ll b/llvm/test/Transforms/InstSimplify/icmp.ll new file mode 100644 index 000000000000..489f73c9495c --- /dev/null +++ b/llvm/test/Transforms/InstSimplify/icmp.ll @@ -0,0 +1,14 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instsimplify -S | FileCheck %s + +target datalayout = "e-p:64:64:64-p1:16:16:16-p2:32:32:32-p3:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" + +declare void @usei8ptr(i8* %ptr) + +; Ensure that we do not crash when looking at such a weird bitcast. +define i1 @bitcast_from_single_element_pointer_vector_to_pointer(<1 x i8*> %ptr1vec, i8* %ptr2) { + %ptr1 = bitcast <1 x i8*> %ptr1vec to i8* + call void @usei8ptr(i8* %ptr1) + %cmp = icmp eq i8* %ptr1, %ptr2 + ret i1 %cmp +} -- 2.25.1