[llvm] [Transforms] LoopIdiomRecognize recognize strlen and wcslen (PR #108985)
Michael Kruse via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 3 16:07:47 PST 2025
================
@@ -1524,6 +1551,276 @@ static Value *matchCondition(BranchInst *BI, BasicBlock *LoopEntry,
return nullptr;
}
+namespace {
+
+class StrlenVerifier {
+public:
+ explicit StrlenVerifier(const Loop *CurLoop, ScalarEvolution *SE,
+ const TargetLibraryInfo *TLI)
+ : CurLoop(CurLoop), SE(SE), TLI(TLI) {}
+
+ bool isValidStrlenIdiom() {
+ // Give up if the loop has multiple blocks, multiple backedges, or
+ // multiple exit blocks
+ if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1 ||
+ !CurLoop->getUniqueExitBlock())
+ return false;
+
+ // It should have a preheader and a branch instruction.
+ BasicBlock *Preheader = CurLoop->getLoopPreheader();
+ if (!Preheader)
+ return false;
+
+ BranchInst *EntryBI = dyn_cast<BranchInst>(Preheader->getTerminator());
+ if (!EntryBI)
+ return false;
+
+ // The loop exit must be conditioned on an icmp with 0 the null terminator.
+ // The icmp operand has to be a load on some SSA reg that increments
+ // by 1 in the loop.
+ BasicBlock *LoopBody = *CurLoop->block_begin();
+
+ // Skip if the body is too big as it most likely is not a strlen idiom.
+ if (!LoopBody || LoopBody->size() >= 15)
+ return false;
+
+ BranchInst *LoopTerm = dyn_cast<BranchInst>(LoopBody->getTerminator());
+ Value *LoopCond = matchCondition(LoopTerm, LoopBody);
+ if (!LoopCond)
+ return false;
+
+ LoadInst *LoopLoad = dyn_cast<LoadInst>(LoopCond);
+ if (!LoopLoad || LoopLoad->getPointerAddressSpace() != 0)
+ return false;
+
+ OperandType = LoopLoad->getType();
+ if (!OperandType || !OperandType->isIntegerTy())
+ return false;
+
+ // See if the pointer expression is an AddRec with constant step a of form
+ // ({n,+,a}) where a is the width of the char type.
+ Value *IncPtr = LoopLoad->getPointerOperand();
+ const SCEVAddRecExpr *LoadEv =
+ dyn_cast<SCEVAddRecExpr>(SE->getSCEV(IncPtr));
+ if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine())
+ return false;
+ LoadBaseEv = LoadEv->getStart();
+
+ LLVM_DEBUG({
+ dbgs() << "pointer load scev: ";
+ LoadEv->print(outs());
+ dbgs() << "\n";
+ });
+
+ const SCEVConstant *Step =
+ dyn_cast<SCEVConstant>(LoadEv->getStepRecurrence(*SE));
+ if (!Step)
+ return false;
+
+ unsigned StepSize = 0;
+ StepSizeCI = dyn_cast<ConstantInt>(Step->getValue());
+ if (!StepSizeCI)
+ return false;
+ StepSize = StepSizeCI->getZExtValue();
+
+ // Verify that StepSize is consistent with platform char width.
+ OpWidth = OperandType->getIntegerBitWidth();
+ unsigned WcharSize = TLI->getWCharSize(*LoopLoad->getModule());
+ if (OpWidth != StepSize * 8)
+ return false;
+ if (OpWidth != 8 && OpWidth != 16 && OpWidth != 32)
+ return false;
+ if (OpWidth >= 16)
+ if (OpWidth != WcharSize * 8)
+ return false;
+
+ // Scan every instruction in the loop to ensure there are no side effects.
+ for (Instruction &I : *LoopBody)
+ if (I.mayHaveSideEffects())
+ return false;
+
+ BasicBlock *LoopExitBB = CurLoop->getExitBlock();
+ if (!LoopExitBB)
+ return false;
+
+ for (PHINode &PN : LoopExitBB->phis()) {
+ if (!SE->isSCEVable(PN.getType()))
+ return false;
+
+ const SCEV *Ev = SE->getSCEV(&PN);
+ if (!Ev)
+ return false;
+
+ LLVM_DEBUG({
+ dbgs() << "loop exit phi scev: ";
+ Ev->print(dbgs());
+ dbgs() << "\n";
+ });
+
+ // Since we verified that the loop trip count will be a valid strlen
+ // idiom, we can expand all lcssa phi with {n,+,1} as (n + strlen) and use
+ // SCEVExpander materialize the loop output.
+ const SCEVAddRecExpr *AddRecEv = dyn_cast<SCEVAddRecExpr>(Ev);
+ if (!AddRecEv || !AddRecEv->isAffine())
+ return false;
+
+ // We only want RecAddExpr with recurrence step that are constant. This
+ // is good enough for all the idioms we want to recognize. Later we expand
+ // the recurrence as {base,+,a} -> (base + a * strlen) and materialize
+ if (!dyn_cast<SCEVConstant>(AddRecEv->getStepRecurrence(*SE)))
+ return false;
+ }
+
+ return true;
+ }
+
+public:
+ const Loop *CurLoop;
+ ScalarEvolution *SE;
+ const TargetLibraryInfo *TLI;
+
+ unsigned OpWidth;
+ ConstantInt *StepSizeCI;
+ const SCEV *LoadBaseEv;
+ Type *OperandType;
+};
+
+} // namespace
+
+/// The Strlen Idiom we are trying to detect has the following structure
+///
+/// preheader:
+/// ...
+/// br label %body, ...
+///
+/// body:
+/// ... ; %0 is incremented by a gep
+/// %1 = load i8, ptr %0, align 1
+/// %2 = icmp eq i8 %1, 0
+/// br i1 %2, label %exit, label %body
+///
+/// exit:
+/// %lcssa = phi [%0, %body], ...
+///
+/// We expect the strlen idiom to have a load of a character type that
+/// is compared against '\0', and such load pointer operand must have scev
+/// expression of the form {%str,+,c} where c is a ConstantInt of the
+/// appropiate character width for the idiom, and %str is the base of the string
+/// And, that all lcssa phis have the form {...,+,n} where n is a constant,
+///
+/// When transforming the output of the strlen idiom, the lccsa phi are
+/// expanded using SCEVExpander as {base scev,+,a} -> (base scev + a * strlen)
+/// and all subsequent uses are replaced. For example,
+///
+/// \code{.c}
+/// const char* base = str;
+/// while (*str != '\0')
+/// ++str;
+/// size_t result = str - base;
+/// \endcode
+///
+/// will be transformed as as follow: The idiom will be replaced by a strlen
----------------
Meinersbur wrote:
```suggestion
/// will be transformed as follows: The idiom will be replaced by a strlen
```
https://github.com/llvm/llvm-project/pull/108985
More information about the llvm-commits
mailing list