[PATCH] D18230: [SimplifyLibCalls] Simplify strlen to a subtraction for certain cases
Li Huang via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 5 12:18:04 PDT 2016
lihuang added a comment.
Sure, I will rebase the patch after 265431.
================
Comment at: lib/Transforms/Utils/SimplifyLibCalls.cpp:541-558
@@ +540,20 @@
+ // [0, strlen(s)] or the string has a single null terminator '\0' at the end.
+ if (GEPOperator *GEP = dyn_cast<GEPOperator>(Src)) {
+ if (GEP->getNumOperands() != 3)
+ return nullptr;
+
+ // We only try to simplify strlen when the pointer s points to an array
+ // of i8. Otherwise, we would need to scale the offset x before doing the
+ // subtraction. This will make the optimization more complex, and it's not
+ // very useful because calling strlen for a pointer of other types is
+ // very uncommon.
+ PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType());
+ ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType());
+ if (!AT || !AT->getElementType()->isIntegerTy(8))
+ return nullptr;
+
+ // First index should be 0.
+ const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
+ if (!FirstIdx || !FirstIdx->isZero())
+ return nullptr;
+
----------------
spatel wrote:
> These checks are included in getConstantStringInfo(). You do not need to repeat them here?
Yes these lines are the same lines in getConstantStringInfo(). The reason I didn't use getConstantStringInfo() here is it only works for GEP with a constant offset. In this case, the offset is a variable, and we need the same checks.
http://reviews.llvm.org/D18230
More information about the llvm-commits
mailing list