[llvm] e96ddad - [MemoryBuiltins] Avoid comparing against Type::getInt8PtrTy(0)
Alex Richardson via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 9 06:00:59 PST 2022
Author: Alex Richardson
Date: 2022-12-09T14:00:46Z
New Revision: e96ddad85ca03ddd76a7b0571771709fb1fd1b53
URL: https://github.com/llvm/llvm-project/commit/e96ddad85ca03ddd76a7b0571771709fb1fd1b53
DIFF: https://github.com/llvm/llvm-project/commit/e96ddad85ca03ddd76a7b0571771709fb1fd1b53.diff
LOG: [MemoryBuiltins] Avoid comparing against Type::getInt8PtrTy(0)
This does not make sense with opaque pointers, and also caused issues for
CHERI/Morello where hardcoding address-space zero prevented optimization.
Downstream change: https://git.morello-project.org/morello/llvm-project/-/merge_requests/180
Co-authored-by: Silviu Baranga <silviu.baranga at arm.com>
Reviewed By: lebedev.ri
Differential Revision: https://reviews.llvm.org/D139708
Added:
llvm/test/Transforms/InstCombine/malloc-free-addrspace.ll
Modified:
llvm/lib/Analysis/MemoryBuiltins.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp
index 3b17ea4b5a47e..51517833db9bd 100644
--- a/llvm/lib/Analysis/MemoryBuiltins.cpp
+++ b/llvm/lib/Analysis/MemoryBuiltins.cpp
@@ -192,7 +192,7 @@ getAllocationDataForFunction(const Function *Callee, AllocType AllocTy,
int SndParam = FnData->SndParam;
FunctionType *FTy = Callee->getFunctionType();
- if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
+ if (FTy->getReturnType()->isPointerTy() &&
FTy->getNumParams() == FnData->NumParams &&
(FstParam < 0 ||
(FTy->getParamType(FstParam)->isIntegerTy(32) ||
@@ -537,7 +537,7 @@ bool llvm::isLibFreeFunction(const Function *F, const LibFunc TLIFn) {
return false;
if (FTy->getNumParams() != FnData->NumParams)
return false;
- if (FTy->getParamType(0) != Type::getInt8PtrTy(F->getContext()))
+ if (!FTy->getParamType(0)->isPointerTy())
return false;
return true;
diff --git a/llvm/test/Transforms/InstCombine/malloc-free-addrspace.ll b/llvm/test/Transforms/InstCombine/malloc-free-addrspace.ll
new file mode 100644
index 0000000000000..7c42dce1802c5
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/malloc-free-addrspace.ll
@@ -0,0 +1,84 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature
+; RUN: opt -passes=instcombine -S < %s | FileCheck %s
+
+target datalayout = "e-m:e-p200:128:128:128:64-A200-P200-G200"
+
+define i64 @remove_malloc() addrspace(200) {
+; CHECK-LABEL: define {{[^@]+}}@remove_malloc() addrspace(200) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: ret i64 0
+;
+entry:
+ %call = call align 16 ptr addrspace(200) @malloc(i64 4)
+ call void @free(ptr addrspace(200) %call)
+ ret i64 0
+}
+
+define i64 @remove_calloc() addrspace(200) {
+; CHECK-LABEL: define {{[^@]+}}@remove_calloc() addrspace(200) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: ret i64 0
+;
+entry:
+ %call = call ptr addrspace(200) @calloc(i64 1, i64 4)
+ call void @free(ptr addrspace(200) %call)
+ ret i64 0
+}
+define i64 @remove_aligned_alloc() addrspace(200) {
+; CHECK-LABEL: define {{[^@]+}}@remove_aligned_alloc() addrspace(200) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: ret i64 0
+;
+entry:
+ %call = call align 4 ptr addrspace(200) @aligned_alloc(i64 4, i64 4)
+ call void @free(ptr addrspace(200) %call)
+ ret i64 0
+}
+
+define i64 @remove_strdup(ptr addrspace(200) %arg) addrspace(200) {
+; CHECK-LABEL: define {{[^@]+}}@remove_strdup
+; CHECK-SAME: (ptr addrspace(200) [[ARG:%.*]]) addrspace(200) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: ret i64 0
+;
+entry:
+ %call = call align 4 ptr addrspace(200) @strdup(ptr addrspace(200) %arg)
+ call void @free(ptr addrspace(200) %call)
+ ret i64 0
+}
+
+define i64 @remove_new(ptr addrspace(200) %arg) addrspace(200) {
+; CHECK-LABEL: define {{[^@]+}}@remove_new
+; CHECK-SAME: (ptr addrspace(200) [[ARG:%.*]]) addrspace(200) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: ret i64 0
+;
+entry:
+ %call = call align 4 ptr addrspace(200) @_Znwm(i64 4)
+ call void @_ZdlPv(ptr addrspace(200) %call)
+ ret i64 0
+}
+
+define i64 @remove_new_array(ptr addrspace(200) %arg) addrspace(200) {
+; CHECK-LABEL: define {{[^@]+}}@remove_new_array
+; CHECK-SAME: (ptr addrspace(200) [[ARG:%.*]]) addrspace(200) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: ret i64 0
+;
+entry:
+ %call = call align 4 ptr addrspace(200) @_Znam(i64 4)
+ call void @_ZdaPv(ptr addrspace(200) %call)
+ ret i64 0
+}
+
+declare noalias ptr addrspace(200) @calloc(i64, i64) addrspace(200) nounwind allockind("alloc,zeroed") allocsize(0,1) "alloc-family"="malloc"
+declare noalias ptr addrspace(200) @malloc(i64) addrspace(200) allockind("alloc,uninitialized") allocsize(0) "alloc-family"="malloc"
+declare noalias ptr addrspace(200) @aligned_alloc(i64, i64) addrspace(200) allockind("alloc,uninitialized,aligned") allocsize(1) "alloc-family"="malloc"
+declare noalias ptr addrspace(200) @strdup(ptr addrspace(200) %arg) addrspace(200)
+declare void @free(ptr addrspace(200)) addrspace(200) allockind("free") "alloc-family"="malloc"
+; new/delete
+declare noalias ptr addrspace(200) @_Znwm(i64) addrspace(200)
+declare void @_ZdlPv(ptr addrspace(200)) addrspace(200)
+; new[]/delete[]
+declare noalias ptr addrspace(200) @_Znam(i64) addrspace(200)
+declare void @_ZdaPv(ptr addrspace(200)) addrspace(200)
More information about the llvm-commits
mailing list