[llvm] [X86, SimplifyCFG] Support hoisting load/store with conditional faulting (Part I) (PR #96878)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 14 20:22:58 PDT 2024
================
@@ -3214,6 +3259,109 @@ bool SimplifyCFGOpt::speculativelyExecuteBB(BranchInst *BI,
BB->splice(BI->getIterator(), ThenBB, ThenBB->begin(),
std::prev(ThenBB->end()));
+ // If the target supports conditional faulting,
+ // we look for the following pattern:
+ // \code
+ // BB:
+ // ...
+ // %cond = icmp ult %x, %y
+ // br i1 %cond, label %TrueBB, label %FalseBB
+ // FalseBB:
+ // store i32 1, ptr %q, align 4
+ // ...
+ // TrueBB:
+ // %maskedloadstore = load i32, ptr %b, align 4
+ // store i32 %maskedloadstore, ptr %p, align 4
+ // ...
+ // \endcode
+ //
+ // and transform it into:
+ //
+ // \code
+ // BB:
+ // ...
+ // %cond = icmp ult %x, %y
+ // %maskedloadstore = cload i32, ptr %b, %cond
+ // cstore i32 %maskedloadstore, ptr %p, %cond
+ // cstore i32 1, ptr %q, ~%cond
+ // br i1 %cond, label %TrueBB, label %FalseBB
+ // FalseBB:
+ // ...
+ // TrueBB:
+ // ...
+ // \endcode
+ //
+ // where cload/cstore are represented by llvm.masked.load/store intrinsics,
+ // e.g.
+ //
+ // \code
+ // %vcond = bitcast i1 %cond to <1 x i1>
+ // %v0 = call <1 x i32> @llvm.masked.load.v1i32.p0
+ // (ptr %b, i32 4, <1 x i1> %vcond, <1 x i32> poison)
+ // %maskedloadstore = bitcast <1 x i32> %v0 to i32
+ // call void @llvm.masked.store.v1i32.p0
+ // (<1 x i32> %v0, ptr %p, i32 4, <1 x i1> %vcond)
+ // %cond.not = xor i1 %cond, true
+ // %vcond.not = bitcast i1 %cond.not to <1 x i>
+ // call void @llvm.masked.store.v1i32.p0
+ // (<1 x i32> <i32 1>, ptr %q, i32 4, <1x i1> %vcond.not)
+ // \endcode
+ //
+ // So we need to turn hoisted load/store into cload/cstore.
+ auto &Context = BI->getParent()->getContext();
+ auto *VCondTy = FixedVectorType::get(Type::getInt1Ty(Context), 1);
+ auto *Cond = BI->getOperand(0);
+ Value *Mask = nullptr;
+ // Construct the condition if needed.
+ if (!SpeculatedConditionalLoadsStores.empty()) {
+ IRBuilder<> Builder(SpeculatedConditionalLoadsStores.back());
+ if (Invert)
+ Mask = Builder.CreateBitCast(
+ Builder.CreateXor(Cond, ConstantInt::getTrue(Context)), VCondTy);
+ else
+ Mask = Builder.CreateBitCast(Cond, VCondTy);
----------------
DianQK wrote:
```suggestion
Mask = Builder.CreateBitCast(Invert ? Builder.CreateXor(Cond, ConstantInt::getTrue(Context)) : Cond, VCondTy);
```
Is it possible to avoid creating a null pointer?
https://github.com/llvm/llvm-project/pull/96878
More information about the llvm-commits
mailing list