Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,7 @@ static bool isSafeAndProfitableToSinkLoad(LoadInst *L) {
Instruction *InstCombinerImpl::foldPHIArgLoadIntoPHI(PHINode &PN) {
LoadInst *FirstLI = cast<LoadInst>(PN.getIncomingValue(0));

// Can't forward swifterror through a phi.
if (FirstLI->getOperand(0)->isSwiftError())
if (!canReplaceOperandWithVariable(FirstLI, 0))
return nullptr;

// FIXME: This is overconservative; this transform is allowed in some cases
Expand Down Expand Up @@ -738,8 +737,7 @@ Instruction *InstCombinerImpl::foldPHIArgLoadIntoPHI(PHINode &PN) {
LI->getPointerAddressSpace() != LoadAddrSpace)
return nullptr;

// Can't forward swifterror through a phi.
if (LI->getOperand(0)->isSwiftError())
if (!canReplaceOperandWithVariable(LI, 0))
return nullptr;

// We can't sink the load if the loaded value could be modified between
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/Transforms/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3884,6 +3884,12 @@ bool llvm::canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx) {
if (Op->isSwiftError())
return false;

// Protected pointer field loads/stores should be paired with the intrinsic
// to avoid unnecessary address escapes.
if (auto *II = dyn_cast<IntrinsicInst>(Op))
if (II->getIntrinsicID() == Intrinsic::protected_field_ptr)
return false;

// Cannot replace alloca argument with phi/select.
if (I->isLifetimeStartOrEnd())
return false;
Expand Down
38 changes: 38 additions & 0 deletions llvm/test/Transforms/PhaseOrdering/phi-protected-field-ptr.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -O2 -S < %s | FileCheck %s

; Test that no optimization run at -O2 moves the loads into the exit block,
; as this causes unnecessary address escapes with pointer field protection.

define ptr @phi_prot_ptr(i1 %sel, ptr %p1, ptr %p2) {
; CHECK-LABEL: define ptr @phi_prot_ptr(
; CHECK-SAME: i1 [[SEL:%.*]], ptr readonly [[P1:%.*]], ptr readonly [[P2:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: br i1 [[SEL]], label %[[T:.*]], label %[[F:.*]]
; CHECK: [[T]]:
; CHECK-NEXT: [[PROTP1:%.*]] = tail call ptr @llvm.protected.field.ptr.p0(ptr [[P1]], i64 1, i1 true)
; CHECK-NEXT: [[LOAD1:%.*]] = load ptr, ptr [[PROTP1]], align 8
; CHECK-NEXT: br label %[[EXIT:.*]]
; CHECK: [[F]]:
; CHECK-NEXT: [[PROTP2:%.*]] = tail call ptr @llvm.protected.field.ptr.p0(ptr [[P2]], i64 2, i1 true)
; CHECK-NEXT: [[LOAD2:%.*]] = load ptr, ptr [[PROTP2]], align 8
; CHECK-NEXT: br label %[[EXIT]]
; CHECK: [[EXIT]]:
; CHECK-NEXT: [[RETVAL:%.*]] = phi ptr [ [[LOAD1]], %[[T]] ], [ [[LOAD2]], %[[F]] ]
; CHECK-NEXT: ret ptr [[RETVAL]]
;
br i1 %sel, label %t, label %f

t:
%protp1 = call ptr @llvm.protected.field.ptr.p0(ptr %p1, i64 1, i1 true)
%load1 = load ptr, ptr %protp1
br label %exit

f:
%protp2 = call ptr @llvm.protected.field.ptr.p0(ptr %p2, i64 2, i1 true)
%load2 = load ptr, ptr %protp2
br label %exit

exit:
%retval = phi ptr [ %load1, %t ], [ %load2, %f ]
ret ptr %retval
}
Loading