[llvm] [LICM] Promote conditional, loop-invariant memory accesses to scalars with intrinsic (PR #93999)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri May 31 14:26:51 PDT 2024


================
@@ -0,0 +1,115 @@
+//===- LowerConditionalStoreIntrinsic.cpp -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Transforms/Scalar/LowerConditionalStoreIntrinsic.h"
+#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/Intrinsics.h"
+#include "llvm/InitializePasses.h"
+#include "llvm/Pass.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Transforms/Scalar.h"
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "lower-cond-store-intrinsic"
+
+// Conditional store intrinsic removal:
+// block:
+//  llvm.conditional.store.*(Val, Ptr, Condition)
+//                         |
+//                         V
+// block:
+//  br %i1 Condition, label cond.store, label block.remaining
+// cond.tore:
+//  store * Val, Ptr
+//  br label block.remaining
+// block.remaining:
+
+static bool isCondStoreIntr(Instruction &Instr) {
+  CallInst *CI = dyn_cast<CallInst>(&Instr);
+  if (!CI)
+    return false;
+
+  Function *Fn = CI->getCalledFunction();
+  if (Fn && Fn->getIntrinsicID() == Intrinsic::conditional_store)
+    return true;
+  return false;
+}
+
+static void lowerCondStoreIntr(Instruction &Instr, BasicBlock &BB) {
+  LLVM_DEBUG(dbgs() << "Found basic with conditional store: " << BB.getName()
+                    << "\n");
+  auto *Val = Instr.getOperand(0);
+  auto *Ptr = Instr.getOperand(1);
+  auto *AlignmentVal = dyn_cast<ConstantInt>(Instr.getOperand(2));
+  auto Alignment = MaybeAlign(AlignmentVal->getValue().getLimitedValue());
+  assert(AlignmentVal && "Invalid intrinsic operands");
----------------
arsenm wrote:

cast instead of dyn_cast and assert (but this doesn't matter if the align just comes from the attributes) 

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


More information about the llvm-commits mailing list