[llvm] [GVN] Support rnflow pattern matching and transform (PR #162259)
Madhur Amilkanthwar via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 10 02:15:09 PST 2025
================
@@ -3330,6 +3334,128 @@ void GVNPass::assignValNumForDeadCode() {
}
}
+bool GVNPass::transformMinFindingSelectPattern(
+ Loop *L, Type *LoadType, BasicBlock *Preheader, BasicBlock *BB, Value *LHS,
+ Value *RHS, CmpInst *Comparison, SelectInst *Select, Value *BasePtr,
+ Value *IndexVal, Value *OffsetVal) {
+ // Hoist the chain of operations for the second load to preheader.
+ // %min.idx.ext = sext i32 %min.idx to i64
+ // %ptr.float.min = getelementptr float, ptr %0, i64 %min.idx.ext
+ // %ptr.second.load = getelementptr i8, ptr %ptr.float.min, i64 -4
+ // %val.current.min = load float, ptr %ptr.second.load, align 4
+ IRBuilder<> Builder(Preheader->getTerminator());
+
+ PHINode *Phi = dyn_cast<PHINode>(IndexVal);
+ if (!Phi) {
+ LLVM_DEBUG(dbgs() << "GVN: IndexVal is not a PHI node\n");
+ return false;
+ }
+
+ Value *InitialMinIndex = Phi->getIncomingValueForBlock(Preheader);
+
+ // Insert PHI node at the top of this block.
+ // This PHI node will be used to memoize the current minimum value so far.
+ PHINode *KnownMinPhi = PHINode::Create(LoadType, 2, "known_min", BB->begin());
+
+ // Hoist the load and build the necessary operations.
+ // 1. hoist_0 = sext i32 to i64
+ Value *HoistedSExt =
+ Builder.CreateSExt(InitialMinIndex, Builder.getInt64Ty(), "hoist_sext");
+
+ // 2. hoist_gep1 = getelementptr float, ptr BasePtr, i64 HoistedSExt
+ Value *HoistedGEP1 =
+ Builder.CreateGEP(LoadType, BasePtr, HoistedSExt, "hoist_gep1");
+
+ // 3. hoist_gep2 = getelementptr i8, ptr HoistedGEP1, i64 OffsetVal
+ Value *HoistedGEP2 = Builder.CreateGEP(Builder.getInt8Ty(), HoistedGEP1,
+ OffsetVal, "hoist_gep2");
+
+ // 4. hoisted_load = load float, ptr HoistedGEP2
+ LoadInst *NewLoad = Builder.CreateLoad(LoadType, HoistedGEP2, "hoisted_load");
----------------
madhur13490 wrote:
Done. Added the loop for checking clobbering.
https://github.com/llvm/llvm-project/pull/162259
More information about the llvm-commits
mailing list