Skip to content

Commit

Permalink
Revert "[ValueTracking] Let isGuaranteedNotToBeUndefOrPoison look int…
Browse files Browse the repository at this point in the history
…o branch conditions of dominating blocks' terminators"

That commit causes SIGSEGV on some simple tests.
This reverts commit 952ad47.
  • Loading branch information
DaniilSuchkov committed Mar 5, 2020
1 parent 5abfe64 commit 3db48f9
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 100 deletions.
8 changes: 1 addition & 7 deletions llvm/include/llvm/Analysis/ValueTracking.h
Expand Up @@ -569,13 +569,7 @@ class Value;

/// Return true if this function can prove that V is never undef value
/// or poison value.
//
/// 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.
bool isGuaranteedNotToBeUndefOrPoison(const Value *V,
const Instruction *CtxI = nullptr,
const DominatorTree *DT = nullptr);
bool isGuaranteedNotToBeUndefOrPoison(const Value *V);

/// Specific patterns of select instructions we can match.
enum SelectPatternFlavor {
Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Expand Up @@ -5360,17 +5360,16 @@ Value *llvm::SimplifyCall(CallBase *Call, const SimplifyQuery &Q) {
}

/// Given operands for a Freeze, see if we can fold the result.
static Value *SimplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
static Value *SimplifyFreezeInst(Value *Op0) {
// Use a utility function defined in ValueTracking.
if (llvm::isGuaranteedNotToBeUndefOrPoison(Op0, Q.CxtI, Q.DT))
if (llvm::isGuaranteedNotToBeUndefOrPoison(Op0))
return Op0;

// We have room for improvement.
return nullptr;
}

Value *llvm::SimplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
return ::SimplifyFreezeInst(Op0, Q);
return ::SimplifyFreezeInst(Op0);
}

/// See if we can compute a simplified version of this instruction.
Expand Down
27 changes: 1 addition & 26 deletions llvm/lib/Analysis/ValueTracking.cpp
Expand Up @@ -4525,9 +4525,7 @@ bool llvm::isOverflowIntrinsicNoWrap(const WithOverflowInst *WO,
return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
}

bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V,
const Instruction *CtxI,
const DominatorTree *DT) {
bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V) {
// If the value is a freeze instruction, then it can never
// be undef or poison.
if (isa<FreezeInst>(V))
Expand Down Expand Up @@ -4560,29 +4558,6 @@ bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V,
return true;
}

if (!CtxI || !DT)
return false;

// If V is used as a branch condition before reaching CtxI, V cannot be
// undef or poison.
// br V, BB1, BB2
// BB1:
// CtxI ; V cannot be undef or poison here
auto Dominator = DT->getNode(CtxI->getParent())->getIDom();
while (Dominator) {
auto *TI = Dominator->getBlock()->getTerminator();

if (auto BI = dyn_cast<BranchInst>(TI)) {
if (BI->isConditional() && BI->getCondition() == V)
return true;
} else if (auto SI = dyn_cast<SwitchInst>(TI)) {
if (SI->getCondition() == V)
return true;
}

Dominator = Dominator->getIDom();
}

return false;
}

Expand Down
63 changes: 0 additions & 63 deletions llvm/test/Transforms/InstSimplify/freeze.ll
Expand Up @@ -18,66 +18,3 @@ define i32 @make_const() {
%x = freeze i32 10
ret i32 %x
}

define i1 @brcond(i1 %c, i1 %c2) {
; CHECK-LABEL: @brcond(
; CHECK-NEXT: br i1 [[C:%.*]], label [[A:%.*]], label [[B:%.*]]
; CHECK: A:
; CHECK-NEXT: br i1 [[C2:%.*]], label [[A2:%.*]], label [[B]]
; CHECK: A2:
; CHECK-NEXT: ret i1 [[C]]
; CHECK: B:
; CHECK-NEXT: ret i1 [[C]]
;
br i1 %c, label %A, label %B
A:
br i1 %c2, label %A2, label %B
A2:
%f1 = freeze i1 %c
ret i1 %f1
B:
%f2 = freeze i1 %c
ret i1 %f2
}

define i32 @brcond_switch(i32 %x) {
; CHECK-LABEL: @brcond_switch(
; CHECK-NEXT: switch i32 [[X:%.*]], label [[EXIT:%.*]] [
; CHECK-NEXT: i32 0, label [[A:%.*]]
; CHECK-NEXT: ]
; CHECK: A:
; CHECK-NEXT: ret i32 [[X]]
; CHECK: EXIT:
; CHECK-NEXT: ret i32 [[X]]
;
switch i32 %x, label %EXIT [ i32 0, label %A ]
A:
%fr1 = freeze i32 %x
ret i32 %fr1
EXIT:
%fr2 = freeze i32 %x
ret i32 %fr2
}

define i1 @brcond_noopt(i1 %c, i1 %c2) {
; CHECK-LABEL: @brcond_noopt(
; CHECK-NEXT: [[F:%.*]] = freeze i1 [[C:%.*]]
; CHECK-NEXT: call void @f1(i1 [[F]])
; CHECK-NEXT: call void @f2()
; CHECK-NEXT: br i1 [[C]], label [[A:%.*]], label [[B:%.*]]
; CHECK: A:
; CHECK-NEXT: ret i1 false
; CHECK: B:
; CHECK-NEXT: ret i1 true
;
%f = freeze i1 %c
call void @f1(i1 %f) ; cannot optimize i1 %f to %c
call void @f2() ; .. because if f2() exits, `br %c` cannot be reached
br i1 %c, label %A, label %B
A:
ret i1 0
B:
ret i1 1
}
declare void @f1(i1)
declare void @f2()

0 comments on commit 3db48f9

Please sign in to comment.