[llvm-dev] Today is the last day of mediocre library call optimizations

Johannes Doerfert via llvm-dev llvm-dev at lists.llvm.org
Mon Apr 1 00:06:29 PDT 2019

To this day, we still do too little when it comes to optimizing well
known library calls.

Given that various libraries, especially the C/C++ standard library, are
relatively clear defined, and LLVM has all the infrastructure already in
place, e.g., SimplifyLibCalls.cpp, it seems odd that we still miss so
many optimization opportunities.

To show how easy it is, and how much impact it can have, I crafted a
very simple, yet very effective patch which will hopefully only be the
first of its kind.

The attached twelve line addition to SimplifyLibCalls.cpp provides
subsequent passes with a better understanding of the "getenv" function.

This function was chosen to showcase the enormous impact well placed
information can have on real world software, including LLVM itself.

While you could take my word for it, I would encourage you to try it out
yourself. Apply the patch and create *a new build* of LLVM to compare
against your existing one. I firmly believe the result will be rather
surprising for most.

With a bit of luck we may even see more patches like this one today.
Even if you are all too busy today, there is always next year I guess.

Thanks in advance,
-------------- next part --------------
From 0f9896144f5f45153c57db7ecac1d48d5401005c Mon Sep 17 00:00:00 2001
From: Johannes Doerfert <johannes at jdoerfert.de>
Date: Mon, 1 Apr 2019 00:54:39 -0500
Subject: [PATCH] [LibCalls] Improve optimizations for getenv library calls

 llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 3920736a8d5..31d70476d72 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -1910,6 +1910,16 @@ Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilder<> &B) {
                      ConstantInt::get(CI->getType(), 0x7F));
+static Value *optimizeGetenv(CallInst *CI, IRBuilder<> &B) {
+  IntegerType *IT = Type::getInt64Ty(CI->getContext());
+  Metadata *LowAndHigh[] = {
+      ConstantAsMetadata::get(ConstantInt::get(IT, 0)),
+      ConstantAsMetadata::get(ConstantInt::get(IT, ~0))};
+  CI->setMetadata(LLVMContext::MD_range,
+                  MDNode::get(CI->getContext(), LowAndHigh));
+  return CI;
 Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilder<> &B) {
   StringRef Str;
   if (!getConstantStringInfo(CI->getArgOperand(0), Str))
@@ -2658,6 +2668,8 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
       return optimizeIsAscii(CI, Builder);
     case LibFunc_toascii:
       return optimizeToAscii(CI, Builder);
+    case LibFunc_getenv:
+      return optimizeGetenv(CI, Builder);
     case LibFunc_atoi:
     case LibFunc_atol:
     case LibFunc_atoll:

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190401/b946cd44/attachment.sig>

More information about the llvm-dev mailing list