Skip to content

Commit 5a84296

Browse files
[Inline][Cloning] Drop incompatible attributes from NewFunc before instSimplify
1 parent 88e8eba commit 5a84296

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Analysis/DomTreeUpdater.h"
1919
#include "llvm/Analysis/InstructionSimplify.h"
2020
#include "llvm/Analysis/LoopInfo.h"
21+
#include "llvm/IR/AttributeMask.h"
2122
#include "llvm/IR/CFG.h"
2223
#include "llvm/IR/Constants.h"
2324
#include "llvm/IR/DebugInfo.h"
@@ -696,6 +697,16 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
696697
}
697698
}
698699

700+
// Drop all incompatible return attributes that cannot be applied to NewFunc
701+
// during cloning, so as to allow instruction simplification later to reason
702+
// on the old state of the function. The original attributes are restored
703+
// before returning.
704+
AttributeMask IncompatibleAttrs =
705+
AttributeFuncs::typeIncompatible(OldFunc->getReturnType());
706+
AttrBuilder RetAttrs(NewFunc->getContext(),
707+
NewFunc->getAttributes().getRetAttrs());
708+
NewFunc->removeRetAttrs(IncompatibleAttrs);
709+
699710
// Clone the entry block, and anything recursively reachable from it.
700711
std::vector<const BasicBlock *> CloneWorklist;
701712
PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
@@ -849,6 +860,9 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
849860
}
850861
}
851862

863+
// Restore attributes.
864+
NewFunc->addRetAttrs(RetAttrs);
865+
852866
// Remap debug intrinsic operands now that all values have been mapped.
853867
// Doing this now (late) preserves use-before-defs in debug intrinsics. If
854868
// we didn't do this, ValueAsMetadata(use-before-def) operands would be
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2+
; RUN: opt < %s -passes=inline -S | FileCheck %s
3+
; RUN: opt < %s -passes='cgscc(inline)' -S | FileCheck %s
4+
5+
define void @callee() {
6+
; CHECK-LABEL: define void @callee() {
7+
; CHECK-NEXT: entry:
8+
; CHECK-NEXT: [[VAL_PTR:%.*]] = load ptr, ptr null, align 8
9+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[VAL_PTR]], null
10+
; CHECK-NEXT: [[VAL:%.*]] = load i64, ptr null, align 8
11+
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i64 undef, i64 [[VAL]]
12+
; CHECK-NEXT: ret void
13+
;
14+
entry:
15+
%val_ptr = load ptr, ptr null, align 8
16+
%cmp = icmp eq ptr %val_ptr, null
17+
%val = load i64, ptr null, align 8
18+
%sel = select i1 %cmp, i64 undef, i64 %val
19+
ret void
20+
}
21+
22+
define noundef i1 @caller() {
23+
; CHECK-LABEL: define noundef i1 @caller() {
24+
; CHECK-NEXT: [[VAL_PTR_I:%.*]] = load ptr, ptr null, align 8
25+
; CHECK-NEXT: [[CMP_I:%.*]] = icmp eq ptr [[VAL_PTR_I]], null
26+
; CHECK-NEXT: [[VAL_I:%.*]] = load i64, ptr null, align 8
27+
; CHECK-NEXT: [[SEL_I:%.*]] = select i1 [[CMP_I]], i64 undef, i64 [[VAL_I]]
28+
; CHECK-NEXT: ret i1 false
29+
;
30+
call void @callee()
31+
ret i1 false
32+
}

0 commit comments

Comments
 (0)