diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h --- a/llvm/include/llvm/Analysis/ValueTracking.h +++ b/llvm/include/llvm/Analysis/ValueTracking.h @@ -600,7 +600,8 @@ /// immediately before the CtxI. bool isGuaranteedNotToBeUndefOrPoison(const Value *V, const Instruction *CtxI = nullptr, - const DominatorTree *DT = nullptr); + const DominatorTree *DT = nullptr, + unsigned Depth = 0); /// Specific patterns of select instructions we can match. enum SelectPatternFlavor { diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -4602,7 +4602,11 @@ bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V, const Instruction *CtxI, - const DominatorTree *DT) { + const DominatorTree *DT, + unsigned Depth) { + if (Depth >= MaxDepth) + return false; + // If the value is a freeze instruction, then it can never // be undef or poison. if (isa(V)) @@ -4610,25 +4614,53 @@ // TODO: Some instructions are guaranteed to return neither undef // nor poison if their arguments are not poison/undef. + auto OpCheck = [&](const Value *V) { + return isGuaranteedNotToBeUndefOrPoison(V, CtxI, DT, Depth + 1); + }; + // TODO: Deal with other Constant subclasses. - if (isa(V) || isa(V)) + if (isa(V) || isa(V)) return true; - if (auto PN = dyn_cast(V)) { - if (llvm::all_of(PN->incoming_values(), [](const Use &U) { - return isa(U.get()); - })) - return true; + // Strip cast operations from a pointer value. + // Note that stripPointerCastsSameRepresentation can strip off getelementptr + // inbounds with zero offset. To guarantee that the result isn't poison, the + // stripped pointer should be checked whether it is inbounds. + // It can strip off addrspacecast that do not change bit representation as + // well. We believe that such addrspacecast is equivalent to no-op. + auto *StrippedV = V->stripPointerCastsSameRepresentation(); + if (isa(StrippedV) || isa(StrippedV) || + isa(StrippedV) || isa(StrippedV)) { + // ConstantPointerNull is allowed here, because 'gep inbounds null, 0' is + // also null, not poison. + return true; } - if (auto II = dyn_cast(V)) { - if (llvm::all_of(II->operands(), [](const Value *V) { - return isGuaranteedNotToBeUndefOrPoison(V); - })) - return true; - } + if (auto *I = dyn_cast(V)) { + switch (I->getOpcode()) { + case Instruction::GetElementPtr: { + auto *GEPI = dyn_cast(I); + if (!GEPI->isInBounds() && llvm::all_of(GEPI->operands(), OpCheck)) + return true; + break; + } + case Instruction::FCmp: { + auto *FI = dyn_cast(I); + if (FI->getFastMathFlags().none() && + llvm::all_of(FI->operands(), OpCheck)) + return true; + break; + } + case Instruction::BitCast: + case Instruction::PHI: + case Instruction::ICmp: + if (llvm::all_of(I->operands(), OpCheck)) + return true; + break; + default: + break; + } - if (auto I = dyn_cast(V)) { if (programUndefinedIfFullPoison(I) && I->getType()->isIntegerTy(1)) // Note: once we have an agreement that poison is a value-wise concept, // we can remove the isIntegerTy(1) constraint. diff --git a/llvm/test/Transforms/InstSimplify/freeze.ll b/llvm/test/Transforms/InstSimplify/freeze.ll --- a/llvm/test/Transforms/InstSimplify/freeze.ll +++ b/llvm/test/Transforms/InstSimplify/freeze.ll @@ -21,8 +21,7 @@ define float @make_const2() { ; CHECK-LABEL: @make_const2( -; CHECK-NEXT: [[X:%.*]] = freeze float 1.000000e+01 -; CHECK-NEXT: ret float [[X]] +; CHECK-NEXT: ret float 1.000000e+01 ; %x = freeze float 10.0 ret float %x @@ -40,8 +39,7 @@ define i32()* @make_const_fn() { ; CHECK-LABEL: @make_const_fn( -; CHECK-NEXT: [[K:%.*]] = freeze i32 ()* @make_const -; CHECK-NEXT: ret i32 ()* [[K]] +; CHECK-NEXT: ret i32 ()* @make_const ; %k = freeze i32()* @make_const ret i32()* %k @@ -49,8 +47,7 @@ define i32* @make_const_null() { ; CHECK-LABEL: @make_const_null( -; CHECK-NEXT: [[K:%.*]] = freeze i32* null -; CHECK-NEXT: ret i32* [[K]] +; CHECK-NEXT: ret i32* null ; %k = freeze i32* null ret i32* %k @@ -77,8 +74,7 @@ define void @alloca() { ; CHECK-LABEL: @alloca( ; CHECK-NEXT: [[P:%.*]] = alloca i8 -; CHECK-NEXT: [[Y:%.*]] = freeze i8* [[P]] -; CHECK-NEXT: call void @f3(i8* [[Y]]) +; CHECK-NEXT: call void @f3(i8* [[P]]) ; CHECK-NEXT: ret void ; %p = alloca i8 @@ -91,8 +87,7 @@ ; CHECK-LABEL: @gep( ; CHECK-NEXT: [[P:%.*]] = alloca [4 x i8] ; CHECK-NEXT: [[Q:%.*]] = getelementptr [4 x i8], [4 x i8]* [[P]], i32 0, i32 6 -; CHECK-NEXT: [[Q2:%.*]] = freeze i8* [[Q]] -; CHECK-NEXT: ret i8* [[Q2]] +; CHECK-NEXT: ret i8* [[Q]] ; %p = alloca [4 x i8] %q = getelementptr [4 x i8], [4 x i8]* %p, i32 0, i32 6 @@ -115,8 +110,7 @@ ; CHECK-LABEL: @gep_inbounds( ; CHECK-NEXT: [[P:%.*]] = alloca [4 x i8] ; CHECK-NEXT: [[Q:%.*]] = getelementptr inbounds [4 x i8], [4 x i8]* [[P]], i32 0, i32 0 -; CHECK-NEXT: [[Q2:%.*]] = freeze i8* [[Q]] -; CHECK-NEXT: ret i8* [[Q2]] +; CHECK-NEXT: ret i8* [[Q]] ; %p = alloca [4 x i8] %q = getelementptr inbounds [4 x i8], [4 x i8]* %p, i32 0, i32 0 @@ -139,8 +133,7 @@ define i32* @gep_inbounds_null() { ; CHECK-LABEL: @gep_inbounds_null( -; CHECK-NEXT: [[K:%.*]] = freeze i32* null -; CHECK-NEXT: ret i32* [[K]] +; CHECK-NEXT: ret i32* null ; %p = getelementptr inbounds i32, i32* null, i32 0 %k = freeze i32* %p @@ -187,8 +180,7 @@ ; CHECK-NEXT: [[FX:%.*]] = freeze float [[X:%.*]] ; CHECK-NEXT: [[FY:%.*]] = freeze float [[Y:%.*]] ; CHECK-NEXT: [[C:%.*]] = fcmp oeq float [[FX]], [[FY]] -; CHECK-NEXT: [[FC:%.*]] = freeze i1 [[C]] -; CHECK-NEXT: ret i1 [[FC]] +; CHECK-NEXT: ret i1 [[C]] ; %fx = freeze float %x %fy = freeze float %y @@ -246,8 +238,7 @@ ; CHECK-NEXT: br label [[EXIT]] ; CHECK: EXIT: ; CHECK-NEXT: [[PHI2:%.*]] = phi i32 [ [[A0_FR]], [[BB1]] ], [ [[PHI1]], [[BB2]] ] -; CHECK-NEXT: [[PHI2_FR:%.*]] = freeze i32 [[PHI2]] -; CHECK-NEXT: ret i32 [[PHI2_FR]] +; CHECK-NEXT: ret i32 [[PHI2]] ; ENTRY: %a0.fr = freeze i32 %a0