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 @@ -606,9 +606,12 @@ /// If CtxI and DT are specified this method performs flow-sensitive analysis /// and returns true if it is guaranteed to be never undef or poison /// immediately before the CtxI. + /// If IgnorePadding is true, V's paddings are not inspected. If false, + /// the paddings should be frozen as well. bool isGuaranteedNotToBeUndefOrPoison(const Value *V, const Instruction *CtxI = nullptr, const DominatorTree *DT = nullptr, + bool IgnorePadding = false, unsigned Depth = 0); /// Specific patterns of select instructions we can match. 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 @@ -4742,6 +4742,7 @@ bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V, const Instruction *CtxI, const DominatorTree *DT, + bool IgnorePadding, unsigned Depth) { if (Depth >= MaxDepth) return false; @@ -4750,13 +4751,16 @@ // be undef or poison. if (isa(V)) return true; - // TODO: Some instructions are guaranteed to return neither undef - // nor poison if their arguments are not poison/undef. + + if (auto *A = dyn_cast(V)) { + // NoUndef does not guarantee that paddings are not undef. + if (A->hasAttribute(Attribute::NoUndef) && + (IgnorePadding || !A->getType()->isAggregateType())) + return true; + } if (auto *C = dyn_cast(V)) { - // TODO: We can analyze ConstExpr by opcode to determine if there is any - // possibility of poison. - if (isa(C) || isa(C)) + if (isa(C)) return false; if (isa(C) || isa(C) || isa(V) || @@ -4765,9 +4769,6 @@ if (C->getType()->isVectorTy()) return !C->containsUndefElement() && !C->containsConstantExpression(); - - // TODO: Recursively analyze aggregates or other constants. - return false; } // Strip cast operations from a pointer value. @@ -4784,34 +4785,61 @@ return true; auto OpCheck = [&](const Value *V) { - return isGuaranteedNotToBeUndefOrPoison(V, CtxI, DT, Depth + 1); + return isGuaranteedNotToBeUndefOrPoison(V, CtxI, DT, IgnorePadding, + Depth + 1); }; - 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)) + if (auto *Opr = dyn_cast(V)) { + if (auto *OverflowOp = dyn_cast(V)) { + if (!OverflowOp->hasNoSignedWrap() && !OverflowOp->hasNoUnsignedWrap() && + OpCheck(OverflowOp->getOperand(0)) && + OpCheck(OverflowOp->getOperand(1))) + return true; + } + + if (auto *GEPOp = dyn_cast(V)) { + if (!GEPOp->isInBounds() && llvm::all_of(GEPOp->operands(), OpCheck)) return true; - break; } + + switch (Opr->getOpcode()) { + case Instruction::FNeg: + case Instruction::FAdd: + case Instruction::FSub: + case Instruction::FMul: + case Instruction::FDiv: + case Instruction::FRem: case Instruction::FCmp: { - auto *FI = dyn_cast(I); - if (FI->getFastMathFlags().none() && - llvm::all_of(FI->operands(), OpCheck)) + auto *FPOp = dyn_cast(Opr); + if (FPOp->getFastMathFlags().none() && + llvm::all_of(FPOp->operands(), OpCheck)) return true; break; } + case Instruction::ExtractValue: + if (isGuaranteedNotToBeUndefOrPoison(Opr->getOperand(0), CtxI, DT, true, + Depth + 1)) + return true; + break; + case Instruction::And: + case Instruction::Or: + case Instruction::Xor: case Instruction::BitCast: case Instruction::PHI: case Instruction::ICmp: - if (llvm::all_of(I->operands(), OpCheck)) + if (llvm::all_of(Opr->operands(), OpCheck)) return true; break; default: break; } + } + // Conservatively return false + if (isa(V)) + return false; + + if (auto *I = dyn_cast(V)) { if (programUndefinedIfPoison(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-noundef.ll b/llvm/test/Transforms/InstSimplify/freeze-noundef.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/InstSimplify/freeze-noundef.ll @@ -0,0 +1,83 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instsimplify -S | FileCheck %s + +define i8 @noundef(i8 noundef %x) { +; CHECK-LABEL: @noundef( +; CHECK-NEXT: ret i8 [[X:%.*]] +; + %y = freeze i8 %x + ret i8 %y +} + +define i1 @or(i1 noundef %x, i1 noundef %x2) { +; CHECK-LABEL: @or( +; CHECK-NEXT: [[Y:%.*]] = or i1 [[X:%.*]], [[X2:%.*]] +; CHECK-NEXT: ret i1 [[Y]] +; + %y = or i1 %x, %x2 + %z = freeze i1 %y + ret i1 %z +} + +define i1 @or2(i1 noundef %x, i1 %x2) { +; CHECK-LABEL: @or2( +; CHECK-NEXT: [[Y:%.*]] = or i1 [[X:%.*]], [[X2:%.*]] +; CHECK-NEXT: [[Z:%.*]] = freeze i1 [[Y]] +; CHECK-NEXT: ret i1 [[Z]] +; + %y = or i1 %x, %x2 + %z = freeze i1 %y + ret i1 %z +} + +define i8 @add(i8 noundef %x) { +; CHECK-LABEL: @add( +; CHECK-NEXT: [[Y:%.*]] = add i8 [[X:%.*]], 1 +; CHECK-NEXT: ret i8 [[Y]] +; + %y = add i8 %x, 1 + %z = freeze i8 %y + ret i8 %z +} + +define i8 @addnsw(i8 noundef %x) { +; CHECK-LABEL: @addnsw( +; CHECK-NEXT: [[Y:%.*]] = add nsw i8 [[X:%.*]], 1 +; CHECK-NEXT: [[Z:%.*]] = freeze i8 [[Y]] +; CHECK-NEXT: ret i8 [[Z]] +; + %y = add nsw i8 %x, 1 + %z = freeze i8 %y + ret i8 %z +} + +define {i8, i32} @aggr({i8, i32} noundef %x) { +; CHECK-LABEL: @aggr( +; CHECK-NEXT: [[Y:%.*]] = freeze { i8, i32 } [[X:%.*]] +; CHECK-NEXT: ret { i8, i32 } [[Y]] +; + %y = freeze {i8, i32} %x + ret {i8, i32} %y +} + +define i32 @extract({i8, i32} noundef %x) { +; CHECK-LABEL: @extract( +; CHECK-NEXT: [[Y:%.*]] = extractvalue { i8, i32 } [[X:%.*]], 1 +; CHECK-NEXT: ret i32 [[Y]] +; + %y = extractvalue {i8, i32} %x, 1 + %z = freeze i32 %y + ret i32 %z +} + +define i32 @extract2({i8, {i8, i32}} noundef %x) { +; CHECK-LABEL: @extract2( +; CHECK-NEXT: [[Y:%.*]] = extractvalue { i8, { i8, i32 } } [[X:%.*]], 1 +; CHECK-NEXT: [[Z:%.*]] = extractvalue { i8, i32 } [[Y]], 1 +; CHECK-NEXT: ret i32 [[Z]] +; + %y = extractvalue {i8, {i8, i32}} %x, 1 + %z = extractvalue {i8, i32} %y, 1 + %w = freeze i32 %z + ret i32 %w +} 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 @@ -123,6 +123,14 @@ ret float %r } +define i8* @constant_expr2() { +; CHECK-LABEL: @constant_expr2( +; CHECK-NEXT: ret i8* bitcast (i16* @g to i8*) +; + %r = freeze i8* bitcast (i16* @g to i8*) + ret i8* %r +} + ; Negative test define <2 x i31> @vector_element_constant_expr() {