br i1 c, BB1, BB2: BB1: use1(c) BB2: use2(c)
In BB1 and BB2, c is never undef or poison because otherwise the branch would have triggered UB.
Checked with Alive2
Differential D75401
[ValueTracking] Let isGuaranteedNotToBeUndefOrPoison look into branch conditions of dominating blocks' terminators aqjune on Feb 28 2020, 10:45 PM. Authored by
Details br i1 c, BB1, BB2: BB1: use1(c) BB2: use2(c) In BB1 and BB2, c is never undef or poison because otherwise the branch would have triggered UB. Checked with Alive2
Diff Detail
Event TimelineComment Actions I agree this is correct, but this seems rather pessimistic. Comment Actions Wouldn't we want something like this instead: // Use a utility function defined in ValueTracking. if (llvm::isGuaranteedNotToBeUndefOrPoison(Op0)) return Op0; // Use a utility function defined in ValueTracking. if (llvm::isGuaranteedToBeUBIfUndefOrPoisonAt(Op0, Q.CtxI)) return Op0; And then centralized collect known UB cases? Comment Actions I don't remember this code very well but can there be an exit(0) between the freeze and branch? Comment Actions Never mind, the example looks correct so I think I'm just misremembering how this works. Comment Actions Hi, I guess the condition was needed for loop unswitching: for (..) { g(); if(b) { A } else { B } } When unswitching if(b), g() might exit or throw an exception, so b should be freezed. Comment Actions Let ValueTracking::isGuaranteedNotToBeUndefOrPoison look into branch conditions of dominating blocks' terminators Comment Actions You might need to check if the branch "must-be-executed" from the CtxI. See the example.
Comment Actions In the brcond_switch and brcond tests, freezes are moved into the block because simply InstSimplify is not recursively looking into operands of ret instructions. Even if freezes are outside the branches (as shown in the previous patch), the optimization is still correct. Comment Actions LGTM Long term, I think we'll want something more general, but for the moment, this is a lot better than nothing. Comment Actions Hi! Unfortunately I had to revert this patch, see my comment here: https://reviews.llvm.org/rG952ad4701cf0d8da79789f6b83ddaa386c60d535 Comment Actions Fixed ( https://github.com/llvm/llvm-project/commit/d7267ee1941668d7e985681e29d10983799811a0 ) |
If CtxI and DT are specified...