Skip to content

Commit 86a20d9

Browse files
committed
Recommit "[SCCP] Do not replace deref'able ptr with un-deref'able one."
This version includes an small fix allowing function pointers to be unconditionally replaced for now. This reverts commit 4c5e4aa.
1 parent 4c19b89 commit 86a20d9

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

llvm/lib/Analysis/Loads.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,10 @@ bool llvm::canReplacePointersIfEqual(Value *A, Value *B, const DataLayout &DL,
510510
assert(Ty == B->getType() && Ty->isPointerTy() &&
511511
"values must have matching pointer types");
512512

513+
// Function pointers are not directly dereferenced using load/store
514+
// instructions. Allow any replacements for now.
515+
if (A->getType()->getPointerElementType()->isFunctionTy())
516+
return true;
513517
// NOTE: The checks in the function are incomplete and currently miss illegal
514518
// cases! The current implementation is a starting point and the
515519
// implementation should be made stricter over time.

llvm/lib/Transforms/Scalar/SCCP.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "llvm/Analysis/DomTreeUpdater.h"
3232
#include "llvm/Analysis/GlobalsModRef.h"
3333
#include "llvm/Analysis/InstructionSimplify.h"
34+
#include "llvm/Analysis/Loads.h"
3435
#include "llvm/Analysis/TargetLibraryInfo.h"
3536
#include "llvm/Analysis/ValueLattice.h"
3637
#include "llvm/Analysis/ValueLatticeUtils.h"
@@ -177,6 +178,8 @@ class SCCPSolver : public InstVisitor<SCCPSolver> {
177178
LLVMContext &Ctx;
178179

179180
public:
181+
const DataLayout &getDataLayout() const { return DL; }
182+
180183
void addAnalysis(Function &F, AnalysisResultsForFn A) {
181184
AnalysisResults.insert({&F, std::move(A)});
182185
}
@@ -1649,6 +1652,14 @@ static bool tryToReplaceWithConstant(SCCPSolver &Solver, Value *V) {
16491652
return false;
16501653
}
16511654

1655+
// Do not propagate equality of a un-dereferenceable pointer.
1656+
// FIXME: Currently this only treats pointers one past the last element
1657+
// for array types. Should probably be much stricter.
1658+
if (Const->getType()->isPointerTy() &&
1659+
!canReplacePointersIfEqual(V, Const, Solver.getDataLayout(),
1660+
dyn_cast<Instruction>(V)))
1661+
return false;
1662+
16521663
LLVM_DEBUG(dbgs() << " Constant: " << *Const << " = " << *V << '\n');
16531664

16541665
// Replaces all of the uses of a variable with uses of the constant.

llvm/test/Transforms/SCCP/apint-bigint2.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ define i101 @large_aggregate_2() {
5151
}
5252

5353
; CHECK-LABEL: @index_too_large
54-
; CHECK-NEXT: store i101* getelementptr (i101, i101* getelementptr ([6 x i101], [6 x i101]* @Y, i32 0, i32 -1), i101 9224497936761618431), i101** undef
55-
; CHECK-NEXT: ret void
54+
; CHECK-NEXT: %ptr1 = getelementptr [6 x i101], [6 x i101]* @Y, i32 0, i32 -1
55+
; CHECK-NEXT: %ptr2 = getelementptr i101, i101* %ptr1, i101 9224497936761618431
56+
; CHECK-NEXT: store i101* %ptr2, i101** undef
57+
; CHECK-NEXT: ret void
5658
define void @index_too_large() {
5759
%ptr1 = getelementptr [6 x i101], [6 x i101]* @Y, i32 0, i32 -1
5860
%ptr2 = getelementptr i101, i101* %ptr1, i101 9224497936761618431

llvm/test/Transforms/SCCP/indirectbr.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ BB1:
3131
define void @indbrtest2() {
3232
; CHECK-LABEL: @indbrtest2(
3333
; CHECK-NEXT: entry:
34-
; CHECK-NEXT: br label [[BB1:%.*]]
34+
; CHECK-NEXT: [[B:%.*]] = inttoptr i64 ptrtoint (i8* blockaddress(@indbrtest2, [[BB1:%.*]]) to i64) to i8*
35+
; CHECK-NEXT: [[C:%.*]] = bitcast i8* [[B]] to i8*
36+
; CHECK-NEXT: br label [[BB1]]
3537
; CHECK: BB1:
3638
; CHECK-NEXT: call void @BB1_f()
3739
; CHECK-NEXT: ret void

llvm/test/Transforms/SCCP/replace-dereferenceable-ptr-with-undereferenceable.ll

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ define i32 @eq_undereferenceable(i32* %p) {
1111
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32* [[P:%.*]], getelementptr inbounds (i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @x, i64 0, i64 0), i64 1)
1212
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
1313
; CHECK: if.then:
14-
; CHECK-NEXT: store i32 2, i32* getelementptr inbounds (i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @x, i64 0, i64 0), i64 1), align 4
14+
; CHECK-NEXT: store i32 2, i32* [[P]], align 4
1515
; CHECK-NEXT: br label [[IF_END]]
1616
; CHECK: if.end:
1717
; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @y, i64 0, i64 0), align 4
@@ -65,7 +65,7 @@ define i1 @eq_undereferenceable_cmp_simp(i32* %p) {
6565
; CHECK-NEXT: [[CMP_0:%.*]] = icmp eq i32* [[P:%.*]], getelementptr inbounds (i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @x, i64 0, i64 0), i64 1)
6666
; CHECK-NEXT: br i1 [[CMP_0]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
6767
; CHECK: if.then:
68-
; CHECK-NEXT: store i32 2, i32* getelementptr inbounds (i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @x, i64 0, i64 0), i64 1), align 4
68+
; CHECK-NEXT: store i32 2, i32* [[P]], align 4
6969
; CHECK-NEXT: ret i1 true
7070
; CHECK: if.end:
7171
; CHECK-NEXT: [[CMP_2:%.*]] = icmp eq i32* [[P]], getelementptr inbounds (i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @x, i64 0, i64 0), i64 1)
@@ -84,3 +84,22 @@ if.end: ; preds = %if.then, %entry
8484
%cmp.2 = icmp eq i32* %p, getelementptr inbounds (i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @x, i64 0, i64 0), i64 1)
8585
ret i1 %cmp.2
8686
}
87+
88+
89+
; Test cases with function pointers. Currently any replacements are allowed
90+
; for them.
91+
@FnGlobal1 = internal global void (i8*)* undef, align 8
92+
93+
define void (i8*)* @test_global_undef_fn_ptr() {
94+
%lv = load void (i8*)*, void (i8*)** @FnGlobal1, align 8
95+
ret void (i8*)* %lv
96+
}
97+
98+
declare void @foo(i8*)
99+
100+
@FnGlobal2 = internal global void (i8*)* @foo, align 8
101+
102+
define void (i8*)* @test_global_undef_fn_ptr2() {
103+
%lv = load void (i8*)*, void (i8*)** @FnGlobal2, align 8
104+
ret void (i8*)* %lv
105+
}

0 commit comments

Comments
 (0)