[PATCH] D18230: [SimplifyLibCalls] Simplify strlen to a subtraction for certain cases
Li Huang via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 17 11:13:33 PDT 2016
lihuang added inline comments.
================
Comment at: lib/Transforms/Utils/SimplifyLibCalls.cpp:538-558
@@ -537,1 +537,23 @@
+ // If s is a constant pointer pointing to a string literal, we can fold
+ // strlen(s + x) to strlen(s) - x, when x is known to be in the range
+ // [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;
+
----------------
majnemer wrote:
> Have you tried using something like `GetPointerBaseWithConstantOffset`? You might be able to replace this code with that.
The GEP in this case is not with a constant offset, as x is a variable. So GetPointerBaseWithConstantOffset doesn't help here.
This code actually duplicates part of the code in getConstantStringInfo, but I cannot just use getConstantStringInfo for GEP because it only works with constant offset. I need the same checks here to make sure that the GEP is pointing to a string and the first index is 0.
http://reviews.llvm.org/D18230
More information about the llvm-commits
mailing list