[llvm] [IR] Initial introduction of memset_pattern (PR #97583)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 14 05:13:56 PDT 2024


================
@@ -456,6 +456,56 @@ static void createMemMoveLoop(Instruction *InsertBefore, Value *SrcAddr,
   ElseTerm->eraseFromParent();
 }
 
+static void createMemSetPatternLoop(Instruction *InsertBefore, Value *DstAddr,
+                                    Value *Count, Value *SetValue,
+                                    Align DstAlign, bool IsVolatile) {
+  BasicBlock *OrigBB = InsertBefore->getParent();
+  Function *F = OrigBB->getParent();
+  const DataLayout &DL = F->getDataLayout();
+
+  if (DL.isBigEndian())
+    report_fatal_error("memset.pattern expansion not currently "
+                       "implemented for big-endian targets",
+                       false);
+
+  if (!isPowerOf2_32(SetValue->getType()->getScalarSizeInBits()))
+    report_fatal_error("Pattern width for memset_pattern must be a power of 2",
+                       false);
+
+  Type *TypeOfCount = Count->getType();
+
+  BasicBlock *NewBB = OrigBB->splitBasicBlock(InsertBefore, "split");
+  BasicBlock *LoopBB =
+      BasicBlock::Create(F->getContext(), "storeloop", F, NewBB);
+  IRBuilder<> Builder(OrigBB->getTerminator());
+
+  Builder.CreateCondBr(
+      Builder.CreateICmpEQ(ConstantInt::get(TypeOfCount, 0), Count), NewBB,
+      LoopBB);
+  OrigBB->getTerminator()->eraseFromParent();
+
+  IRBuilder<> LoopBuilder(LoopBB);
+  PHINode *CurrentDst = LoopBuilder.CreatePHI(DstAddr->getType(), 0);
+  CurrentDst->addIncoming(DstAddr, OrigBB);
+  PHINode *LoopCount = LoopBuilder.CreatePHI(TypeOfCount, 0);
+  LoopCount->addIncoming(Count, OrigBB);
+
+  // Create the store instruction for the pattern
+  LoopBuilder.CreateAlignedStore(SetValue, CurrentDst, DstAlign, IsVolatile);
----------------
nikic wrote:

Doesn't this also need the "PartAlign" logic? It's DstAlign on the first iteration, but afterwards its the common alignment of DstAlign and the stride.

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


More information about the llvm-commits mailing list