@@ -2571,57 +2571,31 @@ static IntrinsicInst *findInitTrampoline(Value *Callee) {
2571
2571
}
2572
2572
2573
2573
void InstCombinerImpl::annotateAnyAllocSite (CallBase &Call, const TargetLibraryInfo *TLI) {
2574
- unsigned NumArgs = Call.arg_size ();
2575
- ConstantInt *Op0C = dyn_cast<ConstantInt>(Call.getOperand (0 ));
2576
- ConstantInt *Op1C =
2577
- (NumArgs == 1 ) ? nullptr : dyn_cast<ConstantInt>(Call.getOperand (1 ));
2578
- // Bail out if the allocation size is zero (or an invalid alignment of zero
2579
- // with aligned_alloc).
2580
- if ((Op0C && Op0C->isNullValue ()) || (Op1C && Op1C->isNullValue ()))
2581
- return ;
2582
2574
2583
- if (isMallocLikeFn (&Call, TLI) && Op0C) {
2575
+ uint64_t Size;
2576
+ ObjectSizeOpts Opts;
2577
+ if (getObjectSize (&Call, Size, DL, TLI, Opts) && Size > 0 ) {
2578
+ // TODO: should be annotating these nonnull
2584
2579
if (isOpNewLikeFn (&Call, TLI))
2585
2580
Call.addRetAttr (Attribute::getWithDereferenceableBytes (
2586
- Call.getContext (), Op0C-> getZExtValue () ));
2581
+ Call.getContext (), Size ));
2587
2582
else
2588
2583
Call.addRetAttr (Attribute::getWithDereferenceableOrNullBytes (
2589
- Call.getContext (), Op0C->getZExtValue ()));
2590
- } else if (isAlignedAllocLikeFn (&Call, TLI)) {
2591
- if (Op1C)
2592
- Call.addRetAttr (Attribute::getWithDereferenceableOrNullBytes (
2593
- Call.getContext (), Op1C->getZExtValue ()));
2594
- // Add alignment attribute if alignment is a power of two constant.
2595
- if (Op0C && Op0C->getValue ().ult (llvm::Value::MaximumAlignment) &&
2596
- isKnownNonZero (Call.getOperand (1 ), DL, 0 , &AC, &Call, &DT)) {
2597
- uint64_t AlignmentVal = Op0C->getZExtValue ();
2598
- if (llvm::isPowerOf2_64 (AlignmentVal)) {
2599
- Call.removeRetAttr (Attribute::Alignment);
2600
- Call.addRetAttr (Attribute::getWithAlignment (Call.getContext (),
2601
- Align (AlignmentVal)));
2602
- }
2603
- }
2604
- } else if (isReallocLikeFn (&Call, TLI) && Op1C) {
2605
- Call.addRetAttr (Attribute::getWithDereferenceableOrNullBytes (
2606
- Call.getContext (), Op1C->getZExtValue ()));
2607
- } else if (isCallocLikeFn (&Call, TLI) && Op0C && Op1C) {
2608
- bool Overflow;
2609
- const APInt &N = Op0C->getValue ();
2610
- APInt Size = N.umul_ov (Op1C->getValue (), Overflow);
2611
- if (!Overflow)
2612
- Call.addRetAttr (Attribute::getWithDereferenceableOrNullBytes (
2613
- Call.getContext (), Size.getZExtValue ()));
2614
- } else if (isStrdupLikeFn (&Call, TLI)) {
2615
- uint64_t Len = GetStringLength (Call.getOperand (0 ));
2616
- if (Len) {
2617
- // strdup
2618
- if (NumArgs == 1 )
2619
- Call.addRetAttr (Attribute::getWithDereferenceableOrNullBytes (
2620
- Call.getContext (), Len));
2621
- // strndup
2622
- else if (NumArgs == 2 && Op1C)
2623
- Call.addRetAttr (Attribute::getWithDereferenceableOrNullBytes (
2624
- Call.getContext (), std::min (Len, Op1C->getZExtValue () + 1 )));
2584
+ Call.getContext (), Size));
2585
+ }
2586
+
2587
+ // Add alignment attribute if alignment is a power of two constant.
2588
+ if (!isAlignedAllocLikeFn (&Call, TLI))
2589
+ return ;
2590
+
2591
+ ConstantInt *Op0C = dyn_cast<ConstantInt>(Call.getOperand (0 ));
2592
+ if (Op0C && Op0C->getValue ().ult (llvm::Value::MaximumAlignment) &&
2593
+ isKnownNonZero (Call.getOperand (1 ), DL, 0 , &AC, &Call, &DT)) {
2594
+ uint64_t AlignmentVal = Op0C->getZExtValue ();
2595
+ if (llvm::isPowerOf2_64 (AlignmentVal)) {
2596
+ Call.removeRetAttr (Attribute::Alignment);
2597
+ Call.addRetAttr (Attribute::getWithAlignment (Call.getContext (),
2598
+ Align (AlignmentVal)));
2625
2599
}
2626
2600
}
2627
2601
}
0 commit comments