[llvm] [GVN] Fold value-preserving zext/sext of trunc during PHI translation (PR #204089)

Pengcheng Wang via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 17 05:41:47 PDT 2026


================
@@ -198,6 +198,25 @@ Value *PHITransAddr::translateSubExpr(Value *V, BasicBlock *CurBB,
       return addAsInput(V);
     }
 
+    // Fold a value-preserving zext/sext of a trunc back to the original value.
+    // For an index select coming from a narrower induction variable we often
+    // get e.g. `zext nneg (trunc nuw i64 %iv to i32) to i64`, which is exactly
+    // %iv.  Recognising this lets the translated address match an existing
+    // wider-typed address (`gep base, %iv`) used elsewhere in the loop.
+    if ((Cast->getOpcode() == Instruction::ZExt ||
----------------
wangpc-pp wrote:

Done in https://github.com/llvm/llvm-project/pull/204089/commits/af99c833bcea03042ac21657512a1256ac80a3ef.

https://github.com/llvm/llvm-project/pull/204089


More information about the llvm-commits mailing list