[PATCH] D125887: [CGP] Freeze condition when despeculating ctlz/cttz
Nikita Popov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed May 18 06:42:44 PDT 2022
nikic created this revision.
nikic added reviewers: spatel, fhahn, nlopes.
Herald added a subscriber: hiraditya.
Herald added a project: All.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Freeze the condition of the newly introduced conditional branch, to avoid immediate undefined behavior if the input to ctlz/cttz was originally poison.
https://reviews.llvm.org/D125887
Files:
llvm/lib/CodeGen/CodeGenPrepare.cpp
llvm/test/Transforms/CodeGenPrepare/X86/cttz-ctlz.ll
Index: llvm/test/Transforms/CodeGenPrepare/X86/cttz-ctlz.ll
===================================================================
--- llvm/test/Transforms/CodeGenPrepare/X86/cttz-ctlz.ll
+++ llvm/test/Transforms/CodeGenPrepare/X86/cttz-ctlz.ll
@@ -13,7 +13,8 @@
define i64 @cttz(i64 %A) {
; SLOW-LABEL: @cttz(
; SLOW-NEXT: entry:
-; SLOW-NEXT: [[CMPZ:%.*]] = icmp eq i64 [[A:%.*]], 0
+; SLOW-NEXT: [[TMP0:%.*]] = freeze i64 [[A:%.*]]
+; SLOW-NEXT: [[CMPZ:%.*]] = icmp eq i64 [[TMP0]], 0
; SLOW-NEXT: br i1 [[CMPZ]], label [[COND_END:%.*]], label [[COND_FALSE:%.*]]
; SLOW: cond.false:
; SLOW-NEXT: [[Z:%.*]] = call i64 @llvm.cttz.i64(i64 [[A]], i1 true)
@@ -29,7 +30,8 @@
;
; FAST_LZ-LABEL: @cttz(
; FAST_LZ-NEXT: entry:
-; FAST_LZ-NEXT: [[CMPZ:%.*]] = icmp eq i64 [[A:%.*]], 0
+; FAST_LZ-NEXT: [[TMP0:%.*]] = freeze i64 [[A:%.*]]
+; FAST_LZ-NEXT: [[CMPZ:%.*]] = icmp eq i64 [[TMP0]], 0
; FAST_LZ-NEXT: br i1 [[CMPZ]], label [[COND_END:%.*]], label [[COND_FALSE:%.*]]
; FAST_LZ: cond.false:
; FAST_LZ-NEXT: [[Z:%.*]] = call i64 @llvm.cttz.i64(i64 [[A]], i1 true)
@@ -46,7 +48,8 @@
define i64 @ctlz(i64 %A) {
; SLOW-LABEL: @ctlz(
; SLOW-NEXT: entry:
-; SLOW-NEXT: [[CMPZ:%.*]] = icmp eq i64 [[A:%.*]], 0
+; SLOW-NEXT: [[TMP0:%.*]] = freeze i64 [[A:%.*]]
+; SLOW-NEXT: [[CMPZ:%.*]] = icmp eq i64 [[TMP0]], 0
; SLOW-NEXT: br i1 [[CMPZ]], label [[COND_END:%.*]], label [[COND_FALSE:%.*]]
; SLOW: cond.false:
; SLOW-NEXT: [[Z:%.*]] = call i64 @llvm.ctlz.i64(i64 [[A]], i1 true)
@@ -57,7 +60,8 @@
;
; FAST_TZ-LABEL: @ctlz(
; FAST_TZ-NEXT: entry:
-; FAST_TZ-NEXT: [[CMPZ:%.*]] = icmp eq i64 [[A:%.*]], 0
+; FAST_TZ-NEXT: [[TMP0:%.*]] = freeze i64 [[A:%.*]]
+; FAST_TZ-NEXT: [[CMPZ:%.*]] = icmp eq i64 [[TMP0]], 0
; FAST_TZ-NEXT: br i1 [[CMPZ]], label [[COND_END:%.*]], label [[COND_FALSE:%.*]]
; FAST_TZ: cond.false:
; FAST_TZ-NEXT: [[Z:%.*]] = call i64 @llvm.ctlz.i64(i64 [[A]], i1 true)
Index: llvm/lib/CodeGen/CodeGenPrepare.cpp
===================================================================
--- llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -2038,7 +2038,8 @@
return false;
// Bail if the value is never zero.
- if (llvm::isKnownNonZero(CountZeros->getOperand(0), *DL))
+ Value *Op = CountZeros->getOperand(0);
+ if (llvm::isKnownNonZero(Op, *DL))
return false;
// The intrinsic will be sunk behind a compare against zero and branch.
@@ -2059,7 +2060,9 @@
// Replace the unconditional branch that was created by the first split with
// a compare against zero and a conditional branch.
Value *Zero = Constant::getNullValue(Ty);
- Value *Cmp = Builder.CreateICmpEQ(CountZeros->getOperand(0), Zero, "cmpz");
+ if (!isGuaranteedNotToBeUndefOrPoison(Op))
+ Op = Builder.CreateFreeze(Op); // Avoid introducing branch on poison.
+ Value *Cmp = Builder.CreateICmpEQ(Op, Zero, "cmpz");
Builder.CreateCondBr(Cmp, EndBlock, CallBlock);
StartBlock->getTerminator()->eraseFromParent();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125887.430360.patch
Type: text/x-patch
Size: 3064 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220518/f00db818/attachment.bin>
More information about the llvm-commits
mailing list