Skip to content

Commit b896334

Browse files
committed
[ArgPromotion] Check dereferenceability on argument as well
Before walking all the callers, check whether we have a dereferenceable attribute directly on the argument. Also make it clearer that the code currently does not treat alignment correctly.
1 parent c2b4767 commit b896334

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

llvm/lib/Transforms/IPO/ArgumentPromotion.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -477,18 +477,20 @@ doPromotion(Function *F, SmallPtrSetImpl<Argument *> &ArgsToPromote,
477477
static bool allCallersPassValidPointerForArgument(Argument *Arg, Type *Ty) {
478478
Function *Callee = Arg->getParent();
479479
const DataLayout &DL = Callee->getParent()->getDataLayout();
480+
Align NeededAlign(1); // TODO: This is incorrect!
481+
APInt Bytes(64, DL.getTypeStoreSize(Ty));
480482

481-
unsigned ArgNo = Arg->getArgNo();
483+
// Check if the argument itself is marked dereferenceable and aligned.
484+
if (isDereferenceableAndAlignedPointer(Arg, NeededAlign, Bytes, DL))
485+
return true;
482486

483487
// Look at all call sites of the function. At this point we know we only have
484488
// direct callees.
485-
for (User *U : Callee->users()) {
489+
return all_of(Callee->users(), [&](User *U) {
486490
CallBase &CB = cast<CallBase>(*U);
487-
488-
if (!isDereferenceablePointer(CB.getArgOperand(ArgNo), Ty, DL))
489-
return false;
490-
}
491-
return true;
491+
return isDereferenceableAndAlignedPointer(
492+
CB.getArgOperand(Arg->getArgNo()), NeededAlign, Bytes, DL);
493+
});
492494
}
493495

494496
/// Returns true if Prefix is a prefix of longer. That means, Longer has a size

llvm/test/Transforms/ArgumentPromotion/align.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ define void @caller_guaranteed_aligned_1(i1 %c, i32* align 16 dereferenceable(4)
5353

5454
define internal i32 @callee_guaranteed_aligned_2(i1 %c, i32* align 16 dereferenceable(4) %p) {
5555
; CHECK-LABEL: define {{[^@]+}}@callee_guaranteed_aligned_2
56-
; CHECK-SAME: (i1 [[C:%.*]], i32* align 16 dereferenceable(4) [[P:%.*]]) {
56+
; CHECK-SAME: (i1 [[C:%.*]], i32 [[P_VAL:%.*]]) {
5757
; CHECK-NEXT: br i1 [[C]], label [[IF:%.*]], label [[ELSE:%.*]]
5858
; CHECK: if:
59-
; CHECK-NEXT: [[X:%.*]] = load i32, i32* [[P]], align 16
60-
; CHECK-NEXT: ret i32 [[X]]
59+
; CHECK-NEXT: ret i32 [[P_VAL]]
6160
; CHECK: else:
6261
; CHECK-NEXT: ret i32 -1
6362
;
@@ -74,7 +73,8 @@ else:
7473
define void @caller_guaranteed_aligned_2(i1 %c, i32* %p) {
7574
; CHECK-LABEL: define {{[^@]+}}@caller_guaranteed_aligned_2
7675
; CHECK-SAME: (i1 [[C:%.*]], i32* [[P:%.*]]) {
77-
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @callee_guaranteed_aligned_2(i1 [[C]], i32* [[P]])
76+
; CHECK-NEXT: [[P_VAL:%.*]] = load i32, i32* [[P]], align 16
77+
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @callee_guaranteed_aligned_2(i1 [[C]], i32 [[P_VAL]])
7878
; CHECK-NEXT: ret void
7979
;
8080
call i32 @callee_guaranteed_aligned_2(i1 %c, i32* %p)

0 commit comments

Comments
 (0)