[llvm] r372647 - [InstCombine] Annotate strndup calls with dereferenceable_or_null
David Bolvansky via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 23 12:55:45 PDT 2019
Author: xbolva00
Date: Mon Sep 23 12:55:45 2019
New Revision: 372647
URL: http://llvm.org/viewvc/llvm-project?rev=372647&view=rev
Log:
[InstCombine] Annotate strndup calls with dereferenceable_or_null
"Implementations are free to malloc() a buffer containing either (size + 1) bytes or (strnlen(s, size) + 1) bytes. Applications should not assume that strndup() will allocate (size + 1) bytes when strlen(s) is smaller than size."
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/trunk/test/Transforms/InstCombine/objsize.ll
llvm/trunk/test/Transforms/InstCombine/strndup.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp?rev=372647&r1=372646&r2=372647&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp Mon Sep 23 12:55:45 2019
@@ -4179,10 +4179,10 @@ static IntrinsicInst *findInitTrampoline
}
static void annotateAnyAllocSite(CallBase &Call, const TargetLibraryInfo *TLI) {
+ unsigned NumArgs = Call.getNumArgOperands();
ConstantInt *Op0C = dyn_cast<ConstantInt>(Call.getOperand(0));
- ConstantInt *Op1C = (Call.getNumArgOperands() == 1)
- ? nullptr
- : dyn_cast<ConstantInt>(Call.getOperand(1));
+ ConstantInt *Op1C =
+ (NumArgs == 1) ? nullptr : dyn_cast<ConstantInt>(Call.getOperand(1));
// Bail out if the allocation size is zero.
if ((Op0C && Op0C->isNullValue()) || (Op1C && Op1C->isNullValue()))
return;
@@ -4208,12 +4208,21 @@ static void annotateAnyAllocSite(CallBas
Call.addAttribute(AttributeList::ReturnIndex,
Attribute::getWithDereferenceableOrNullBytes(
Call.getContext(), Size.getZExtValue()));
- } else if (isStrdupLikeFn(&Call, TLI) && Call.getNumArgOperands() == 1) {
- // TODO: handle strndup
- if (uint64_t Len = GetStringLength(Call.getOperand(0)))
- Call.addAttribute(
- AttributeList::ReturnIndex,
- Attribute::getWithDereferenceableOrNullBytes(Call.getContext(), Len));
+ } else if (isStrdupLikeFn(&Call, TLI)) {
+ uint64_t Len = GetStringLength(Call.getOperand(0));
+ if (Len) {
+ // strdup
+ if (NumArgs == 1)
+ Call.addAttribute(AttributeList::ReturnIndex,
+ Attribute::getWithDereferenceableOrNullBytes(
+ Call.getContext(), Len));
+ // strndup
+ else if (NumArgs == 2 && Op1C)
+ Call.addAttribute(
+ AttributeList::ReturnIndex,
+ Attribute::getWithDereferenceableOrNullBytes(
+ Call.getContext(), std::min(Len, Op1C->getZExtValue() + 1)));
+ }
}
}
Modified: llvm/trunk/test/Transforms/InstCombine/objsize.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/objsize.ll?rev=372647&r1=372646&r2=372647&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/objsize.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/objsize.ll Mon Sep 23 12:55:45 2019
@@ -239,7 +239,7 @@ define i32 @test9(i8** %esc) {
define i32 @test10(i8** %esc) {
; CHECK-LABEL: @test10(
-; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @strndup(i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i32 3) #0
+; CHECK-NEXT: [[CALL:%.*]] = tail call dereferenceable_or_null(4) i8* @strndup(i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i32 3) #0
; CHECK-NEXT: store i8* [[CALL]], i8** [[ESC:%.*]], align 8
; CHECK-NEXT: ret i32 4
;
Modified: llvm/trunk/test/Transforms/InstCombine/strndup.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/strndup.ll?rev=372647&r1=372646&r2=372647&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/strndup.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/strndup.ll Mon Sep 23 12:55:45 2019
@@ -18,7 +18,7 @@ define i8* @test1() {
define i8* @test2() {
; CHECK-LABEL: @test2(
-; CHECK-NEXT: [[RET:%.*]] = call i8* @strndup(i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i64 0, i64 0), i32 4)
+; CHECK-NEXT: [[RET:%.*]] = call dereferenceable_or_null(5) i8* @strndup(i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i64 0, i64 0), i32 4)
; CHECK-NEXT: ret i8* [[RET]]
;
%src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
More information about the llvm-commits
mailing list