[llvm] 3611119 - [EarlyCSE] Retain poison flags, if program is UB if poison.
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Sat Dec 11 07:12:21 PST 2021
Author: Florian Hahn
Date: 2021-12-11T15:11:44Z
New Revision: 361111906ba46494cbe888812ba7c0e500e2bb1e
URL: https://github.com/llvm/llvm-project/commit/361111906ba46494cbe888812ba7c0e500e2bb1e
DIFF: https://github.com/llvm/llvm-project/commit/361111906ba46494cbe888812ba7c0e500e2bb1e.diff
LOG: [EarlyCSE] Retain poison flags, if program is UB if poison.
Poison-generating flags can be retained during CSE on the earlier
instruction , *if* the earlier instruction being poison causes UB. For
now, always take AND for floating point instructions.
https://alive2.llvm.org/ce/z/4K3D7P
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D115247
Added:
Modified:
llvm/lib/Transforms/Scalar/EarlyCSE.cpp
llvm/test/Transforms/EarlyCSE/flags.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
index 90f71f7729a7c..a24997dd3fd4a 100644
--- a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
+++ b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
@@ -1366,8 +1366,16 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n");
continue;
}
- if (auto *I = dyn_cast<Instruction>(V))
- I->andIRFlags(&Inst);
+ if (auto *I = dyn_cast<Instruction>(V)) {
+ // If I being poison triggers UB, there is no need to drop those
+ // flags. Otherwise, only retain flags present on both I and Inst.
+ // TODO: Currently some fast-math flags are not treated as
+ // poison-generating even though they should. Until this is fixed,
+ // always retain flags present on both I and Inst for floating point
+ // instructions.
+ if (isa<FPMathOperator>(I) || (I->hasPoisonGeneratingFlags() && !programUndefinedIfPoison(I)))
+ I->andIRFlags(&Inst);
+ }
Inst.replaceAllUsesWith(V);
salvageKnowledge(&Inst, &AC);
removeMSSA(Inst);
diff --git a/llvm/test/Transforms/EarlyCSE/flags.ll b/llvm/test/Transforms/EarlyCSE/flags.ll
index 5434db94f2465..487ef5ef1a117 100644
--- a/llvm/test/Transforms/EarlyCSE/flags.ll
+++ b/llvm/test/Transforms/EarlyCSE/flags.ll
@@ -22,7 +22,7 @@ declare void @use.i8(i8*)
define void @test_inbounds_program_ub_if_first_gep_poison(i8* %ptr, i64 %n) {
; CHECK-LABEL: @test_inbounds_program_ub_if_first_gep_poison(
-; CHECK-NEXT: [[ADD_PTR_1:%.*]] = getelementptr i8, i8* [[PTR:%.*]], i64 [[N:%.*]]
+; CHECK-NEXT: [[ADD_PTR_1:%.*]] = getelementptr inbounds i8, i8* [[PTR:%.*]], i64 [[N:%.*]]
; CHECK-NEXT: call void @use.i8(i8* noundef [[ADD_PTR_1]])
; CHECK-NEXT: call void @use.i8(i8* [[ADD_PTR_1]])
; CHECK-NEXT: ret void
More information about the llvm-commits
mailing list