[llvm] [SimplifyCFG] Add foldCondStoreToSelect optimization (PR #207654)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 02:48:07 PDT 2026


================
@@ -4292,6 +4292,302 @@ static StoreInst *findUniqueStoreInBlocks(BasicBlock *BB1, BasicBlock *BB2) {
   return S;
 }
 
+/// Return true if two pointer-producing values refer to the same address.
+/// Strips pointer casts and matches identical GEPs with the same base and
+/// indices.
+static bool isSameGEPAddress(Value *A, Value *B) {
+  if (A == B)
+    return true;
+  A = A->stripPointerCasts();
+  B = B->stripPointerCasts();
+  if (A == B)
+    return true;
+  auto *GA = dyn_cast<GetElementPtrInst>(A);
+  auto *GB = dyn_cast<GetElementPtrInst>(B);
+  if (!GA || !GB)
+    return false;
+  if (GA->getPointerOperand()->stripPointerCasts() !=
+      GB->getPointerOperand()->stripPointerCasts())
+    return false;
+  if (GA->getSourceElementType() != GB->getSourceElementType())
+    return false;
+  if (GA->getNumIndices() != GB->getNumIndices())
+    return false;
+  return std::equal(GA->idx_begin(), GA->idx_end(), GB->idx_begin());
+}
+
+/// Find the unique simple store in \p BB. Returns nullptr if there is no
+/// store, if there are multiple stores, or if the store is not simple.
+/// Also rejects blocks containing non-store instructions that read/write
+/// memory or have side effects, as they cannot be safely speculated.
+static StoreInst *findUniqueSimpleStoreInBlock(BasicBlock *BB) {
+  StoreInst *Found = nullptr;
+  unsigned NumOther = 0;
+  for (Instruction &I : *BB) {
+    if (I.isTerminator())
+      continue;
+    auto *SI = dyn_cast<StoreInst>(&I);
+    if (SI) {
+      if (!SI->isSimple() || Found)
+        return nullptr;
+      Found = SI;
+      continue;
+    }
+    // Reject if any non-store instruction reads/writes memory or has
+    // side effects — it cannot be safely speculated.
+    if (I.mayReadOrWriteMemory() || I.mayHaveSideEffects())
----------------
artagnon wrote:

Use isSafeToSpeculate instead?

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


More information about the llvm-commits mailing list