[llvm] r298659 - [MetaRenamer] Don't rename library functions.
Bryant Wong via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 23 16:21:07 PDT 2017
Author: bryant
Date: Thu Mar 23 18:21:07 2017
New Revision: 298659
URL: http://llvm.org/viewvc/llvm-project?rev=298659&view=rev
Log:
[MetaRenamer] Don't rename library functions.
Library functions can have specific semantics that affect the behavior of
certain passes. DSE, for instance, gives special treatment to malloc-ed pointers
but not to pointers returned from an equivalently typed (but differently named)
function.
MetaRenamer ought not to alter program semantics, so library functions must
remain untouched.
Reviewers: mehdi_amini, majnemer, chandlerc, davide
Reviewed By: davide
Subscribers: davide, llvm-commits
Differential Revision: https://reviews.llvm.org/D31304
Modified:
llvm/trunk/lib/Transforms/Utils/MetaRenamer.cpp
llvm/trunk/test/Transforms/MetaRenamer/metarenamer.ll
Modified: llvm/trunk/lib/Transforms/Utils/MetaRenamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/MetaRenamer.cpp?rev=298659&r1=298658&r2=298659&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/MetaRenamer.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/MetaRenamer.cpp Thu Mar 23 18:21:07 2017
@@ -16,6 +16,7 @@
#include "llvm/Transforms/IPO.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
+#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
@@ -67,6 +68,7 @@ namespace {
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
+ AU.addRequired<TargetLibraryInfoWrapperPass>();
AU.setPreservesAll();
}
@@ -110,9 +112,15 @@ namespace {
}
// Rename all functions
+ const TargetLibraryInfo &TLI =
+ getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
for (auto &F : M) {
StringRef Name = F.getName();
- if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
+ LibFunc Tmp;
+ // Leave library functions alone because their presence or absence could
+ // affect the behavior of other passes.
+ if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
+ TLI.getLibFunc(F, Tmp))
continue;
F.setName(renamer.newName());
@@ -139,8 +147,11 @@ namespace {
}
char MetaRenamer::ID = 0;
-INITIALIZE_PASS(MetaRenamer, "metarenamer",
- "Assign new names to everything", false, false)
+INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
+ "Assign new names to everything", false, false)
+INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
+INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
+ "Assign new names to everything", false, false)
//===----------------------------------------------------------------------===//
//
// MetaRenamer - Rename everything with metasyntactic names.
Modified: llvm/trunk/test/Transforms/MetaRenamer/metarenamer.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/MetaRenamer/metarenamer.ll?rev=298659&r1=298658&r2=298659&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/MetaRenamer/metarenamer.ll (original)
+++ llvm/trunk/test/Transforms/MetaRenamer/metarenamer.ll Thu Mar 23 18:21:07 2017
@@ -96,3 +96,18 @@ define i32 @varargs_func_6_xxx(i32 %arg_
store i32 %arg_2_xxx, i32* %2, align 4
ret i32 6
}
+
+declare noalias i8* @malloc(i32)
+declare void @free(i8* nocapture)
+
+define void @dont_rename_lib_funcs() {
+; CHECK-LABEL: @foo(
+; CHECK-NEXT: bb:
+; CHECK-NEXT: [[TMP:%.*]] = call i8* @malloc(i32 23)
+; CHECK-NEXT: call void @free(i8* [[TMP]])
+; CHECK-NEXT: ret void
+;
+ %x = call i8* @malloc(i32 23)
+ call void @free(i8* %x)
+ ret void
+}
More information about the llvm-commits
mailing list