[PATCH] D85332: [SCCP] Do not replace deref'able ptr with un-deref'able one.
Florian Hahn via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 7 07:26:28 PDT 2020
fhahn updated this revision to Diff 283906.
fhahn added a comment.
Updated to use canReplacePointersIfEqual, only guard the actual replacement, which still allows for simplification of conditionals based on equality.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D85332/new/
https://reviews.llvm.org/D85332
Files:
llvm/lib/Transforms/Scalar/SCCP.cpp
llvm/test/Transforms/SCCP/apint-bigint2.ll
llvm/test/Transforms/SCCP/indirectbr.ll
llvm/test/Transforms/SCCP/replace-dereferenceable-ptr-with-undereferenceable.ll
Index: llvm/test/Transforms/SCCP/replace-dereferenceable-ptr-with-undereferenceable.ll
===================================================================
--- llvm/test/Transforms/SCCP/replace-dereferenceable-ptr-with-undereferenceable.ll
+++ llvm/test/Transforms/SCCP/replace-dereferenceable-ptr-with-undereferenceable.ll
@@ -11,7 +11,7 @@
; 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)
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK: if.then:
-; 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
+; CHECK-NEXT: store i32 2, i32* [[P]], align 4
; CHECK-NEXT: br label [[IF_END]]
; CHECK: if.end:
; 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 @@
; 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)
; CHECK-NEXT: br i1 [[CMP_0]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK: if.then:
-; 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
+; CHECK-NEXT: store i32 2, i32* [[P]], align 4
; CHECK-NEXT: ret i1 true
; CHECK: if.end:
; 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)
Index: llvm/test/Transforms/SCCP/indirectbr.ll
===================================================================
--- llvm/test/Transforms/SCCP/indirectbr.ll
+++ llvm/test/Transforms/SCCP/indirectbr.ll
@@ -31,7 +31,9 @@
define void @indbrtest2() {
; CHECK-LABEL: @indbrtest2(
; CHECK-NEXT: entry:
-; CHECK-NEXT: br label [[BB1:%.*]]
+; CHECK-NEXT: [[B:%.*]] = inttoptr i64 ptrtoint (i8* blockaddress(@indbrtest2, [[BB1:%.*]]) to i64) to i8*
+; CHECK-NEXT: [[C:%.*]] = bitcast i8* [[B]] to i8*
+; CHECK-NEXT: br label [[BB1]]
; CHECK: BB1:
; CHECK-NEXT: call void @BB1_f()
; CHECK-NEXT: ret void
Index: llvm/test/Transforms/SCCP/apint-bigint2.ll
===================================================================
--- llvm/test/Transforms/SCCP/apint-bigint2.ll
+++ llvm/test/Transforms/SCCP/apint-bigint2.ll
@@ -51,8 +51,10 @@
}
; CHECK-LABEL: @index_too_large
-; CHECK-NEXT: store i101* getelementptr (i101, i101* getelementptr ([6 x i101], [6 x i101]* @Y, i32 0, i32 -1), i101 9224497936761618431), i101** undef
-; CHECK-NEXT: ret void
+; CHECK-NEXT: %ptr1 = getelementptr [6 x i101], [6 x i101]* @Y, i32 0, i32 -1
+; CHECK-NEXT: %ptr2 = getelementptr i101, i101* %ptr1, i101 9224497936761618431
+; CHECK-NEXT: store i101* %ptr2, i101** undef
+; CHECK-NEXT: ret void
define void @index_too_large() {
%ptr1 = getelementptr [6 x i101], [6 x i101]* @Y, i32 0, i32 -1
%ptr2 = getelementptr i101, i101* %ptr1, i101 9224497936761618431
Index: llvm/lib/Transforms/Scalar/SCCP.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/SCCP.cpp
+++ llvm/lib/Transforms/Scalar/SCCP.cpp
@@ -30,6 +30,7 @@
#include "llvm/Analysis/DomTreeUpdater.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/InstructionSimplify.h"
+#include "llvm/Analysis/Loads.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/ValueLattice.h"
#include "llvm/Analysis/ValueLatticeUtils.h"
@@ -176,6 +177,8 @@
LLVMContext &Ctx;
public:
+ const DataLayout &getDataLayout() const { return DL; }
+
void addAnalysis(Function &F, AnalysisResultsForFn A) {
AnalysisResults.insert({&F, std::move(A)});
}
@@ -1629,6 +1632,14 @@
return false;
}
+ // Do not propagate equality of a un-dereferenceable pointer.
+ // FIXME: Currently this only treats pointers one past the last element
+ // for array types. Should probably be much stricter.
+ if (Const->getType()->isPointerTy() &&
+ !canReplacePointersIfEqual(V, Const, Solver.getDataLayout(),
+ dyn_cast<Instruction>(V)))
+ return false;
+
LLVM_DEBUG(dbgs() << " Constant: " << *Const << " = " << *V << '\n');
// Replaces all of the uses of a variable with uses of the constant.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85332.283906.patch
Type: text/x-patch
Size: 4498 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200807/1909a8e9/attachment.bin>
More information about the llvm-commits
mailing list