[PATCH] D18230: [SimplifyLibCalls] Simplify strlen to a subtraction for certain cases

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 5 11:24:55 PDT 2016


spatel added inline comments.

================
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;
+
----------------
These checks are included in getConstantStringInfo(). You do not need to repeat them here?


http://reviews.llvm.org/D18230





More information about the llvm-commits mailing list