[PATCH] D94180: [SimplifyCFG] Optimize CFG when null is passed to a function with nonnull argument.
Dávid Bolvanský via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 6 09:14:14 PST 2021
xbolva00 created this revision.
xbolva00 added a reviewer: nikic.
Herald added a subscriber: hiraditya.
xbolva00 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Example:
__attribute__((nonnull,noinline)) char * pinc(char *p) {
return ++p;
}
char * foo(bool b, char *a) {
return pinc(b ? 0 : a);
}
optimize to
char * foo(bool b, char *a) {
return a;
}
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D94180
Files:
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
Index: llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
===================================================================
--- llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
+++ llvm/test/Transforms/SimplifyCFG/UnreachableEliminate.ll
@@ -211,8 +211,9 @@
;
; CHECK-LABEL: @test9(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[X:%.*]], i8* null, i8* [[Y:%.*]]
-; CHECK-NEXT: [[TMP0:%.*]] = call i8* @foo(i8* [[SPEC_SELECT]])
+; CHECK-NEXT: [[TMP0:%.*]] = xor i1 [[X:%.*]], true
+; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])
+; CHECK-NEXT: [[TMP1:%.*]] = call i8* @foo(i8* [[Y:%.*]])
; CHECK-NEXT: ret void
;
entry:
Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6587,10 +6587,19 @@
SI->getPointerAddressSpace())) &&
SI->getPointerOperand() == I;
- // A call to null is undefined.
- if (auto *CB = dyn_cast<CallBase>(Use))
- return !NullPointerIsDefined(CB->getFunction()) &&
- CB->getCalledOperand() == I;
+ if (auto *CB = dyn_cast<CallBase>(Use)) {
+ if (NullPointerIsDefined(CB->getFunction()))
+ return false;
+ // A call to null is undefined.
+ if (CB->getCalledOperand() == I)
+ return true;
+
+ // Passing null to a nonnnull argument is undefined.
+ for (const llvm::Use &Arg : CB->args())
+ if (Arg == I &&
+ CB->paramHasAttr(CB->getArgOperandNo(&Arg), Attribute::NonNull))
+ return true;
+ }
}
return false;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94180.314914.patch
Type: text/x-patch
Size: 1715 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210106/47321a0c/attachment.bin>
More information about the llvm-commits
mailing list