diff options
| author | David Bolvansky <david.bolvansky@gmail.com> | 2019-08-28 08:28:20 +0000 | 
|---|---|---|
| committer | David Bolvansky <david.bolvansky@gmail.com> | 2019-08-28 08:28:20 +0000 | 
| commit | 05bda8b4e5dc340274aaccf1cdf0ac73d1180bda (patch) | |
| tree | f651be6e273cc176328d5f7ffa45191e35555204 /llvm/lib/Transforms | |
| parent | b9d87b95280231f5176c1ab35fd380faf16f427d (diff) | |
| download | bcm5719-llvm-05bda8b4e5dc340274aaccf1cdf0ac73d1180bda.tar.gz bcm5719-llvm-05bda8b4e5dc340274aaccf1cdf0ac73d1180bda.zip | |
Annotate return values of allocation functions with dereferenceable_or_null
Summary:
Example
define dso_local noalias i8* @_Z6maixxnv() local_unnamed_addr #0 {
entry:
  %call = tail call noalias dereferenceable_or_null(64) i8* @malloc(i64 64) #6
  ret i8* %call
}
Reviewers: jdoerfert
Reviewed By: jdoerfert
Subscribers: aaron.ballman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66651
llvm-svn: 370168
Diffstat (limited to 'llvm/lib/Transforms')
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp | 33 | 
1 files changed, 33 insertions, 0 deletions
| diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 1802e8c8780..c3be8c4e9a9 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -4178,8 +4178,41 @@ static IntrinsicInst *findInitTrampoline(Value *Callee) {    return nullptr;  } +static void annotateAnyAllocSite(CallBase &Call, const TargetLibraryInfo *TLI) { +  ConstantInt *Op0C = dyn_cast<ConstantInt>(Call.getOperand(0)); +  ConstantInt *Op1C = (Call.getNumArgOperands() == 1) +                          ? nullptr +                          : dyn_cast<ConstantInt>(Call.getOperand(1)); +  if ((Op0C && Op0C->isNullValue()) || (Op1C && Op1C->isNullValue())) +    return; +  if (isMallocLikeFn(&Call, TLI) && Op0C) { +    Call.addAttribute(AttributeList::ReturnIndex, +                      Attribute::getWithDereferenceableOrNullBytes( +                          Call.getContext(), Op0C->getZExtValue())); +  } else if (isOpNewLikeFn(&Call, TLI) && Op0C) { +    Call.addAttribute(AttributeList::ReturnIndex, +                      Attribute::getWithDereferenceableBytes( +                          Call.getContext(), Op0C->getZExtValue())); +  } else if (isReallocLikeFn(&Call, TLI) && Op1C) { +    Call.addAttribute(AttributeList::ReturnIndex, +                      Attribute::getWithDereferenceableOrNullBytes( +                          Call.getContext(), Op1C->getZExtValue())); +  } else if (isCallocLikeFn(&Call, TLI) && Op0C && Op1C) { +    bool Overflow; +    const APInt &N = Op0C->getValue(); +    APInt Size = N.umul_ov(Op1C->getValue(), Overflow); +    if (!Overflow) +      Call.addAttribute(AttributeList::ReturnIndex, +                        Attribute::getWithDereferenceableOrNullBytes( +                            Call.getContext(), Size.getZExtValue())); +  } +} +  /// Improvements for call, callbr and invoke instructions.  Instruction *InstCombiner::visitCallBase(CallBase &Call) { +  if (isAllocationFn(&Call, &TLI)) +    annotateAnyAllocSite(Call, &TLI); +    if (isAllocLikeFn(&Call, &TLI))      return visitAllocSite(Call); | 

