[llvm] r329768 - Simplification of libcall like printf->puts must check for RtLibUseGOT metadata.

Sriraman Tallam via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 10 16:32:37 PDT 2018


Author: tmsriram
Date: Tue Apr 10 16:32:36 2018
New Revision: 329768

URL: http://llvm.org/viewvc/llvm-project?rev=329768&view=rev
Log:
Simplification of libcall like printf->puts must check for RtLibUseGOT metadata.

With -fno-plt, for example, calls to printf when getting converted to puts
still use the PLT. This patch checks for the metadata "RtLibUseGOT" and
annotates the declaration with the right attributes.

Differential Revision: https://reviews.llvm.org/D45180

Added:
    llvm/trunk/test/CodeGen/X86/no-plt-libcalls.ll
Modified:
    llvm/trunk/lib/Transforms/Utils/BuildLibCalls.cpp

Modified: llvm/trunk/lib/Transforms/Utils/BuildLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/BuildLibCalls.cpp?rev=329768&r1=329767&r2=329768&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/BuildLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/BuildLibCalls.cpp Tue Apr 10 16:32:36 2018
@@ -105,12 +105,23 @@ static bool setRetNonNull(Function &F) {
   return true;
 }
 
+static bool setNonLazyBind(Function &F) {
+  if (F.hasFnAttribute(Attribute::NonLazyBind))
+    return false;
+  F.addFnAttr(Attribute::NonLazyBind);
+  return true;
+}
+
 bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
   LibFunc TheLibFunc;
   if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc)))
     return false;
 
   bool Changed = false;
+
+  if (F.getParent() != nullptr && F.getParent()->getRtLibUseGOT())
+    Changed |= setNonLazyBind(F);
+
   switch (TheLibFunc) {
   case LibFunc_strlen:
   case LibFunc_wcslen:

Added: llvm/trunk/test/CodeGen/X86/no-plt-libcalls.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/no-plt-libcalls.ll?rev=329768&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/no-plt-libcalls.ll (added)
+++ llvm/trunk/test/CodeGen/X86/no-plt-libcalls.ll Tue Apr 10 16:32:36 2018
@@ -0,0 +1,19 @@
+; Check if "RtLibUseGOT" works correctly when lib calls are simplified.
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+ at percent_s = constant [4 x i8] c"%s\0A\00"
+ at hello_world = constant [13 x i8] c"hello world\0A\00"
+declare i32 @printf(i8*, ...)
+define void @printf_call() {
+  %fmt = getelementptr [4 x i8], [4 x i8]* @percent_s, i32 0, i32 0
+  %str = getelementptr [13 x i8], [13 x i8]* @hello_world, i32 0, i32 0
+  call i32 (i8*, ...) @printf(i8* %fmt, i8* %str)
+; CHECK:  call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @hello_world, i64 0, i64 0))
+  ret void
+}
+
+; CHECK: Function Attrs: nounwind nonlazybind
+; CHECK-NEXT: declare i32 @puts(i8* nocapture readonly)
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 7, !"RtLibUseGOT", i32 1}




More information about the llvm-commits mailing list