blob: cd7bb650548c0412496dfd45fad93594c50f548c [file] [log] [blame]
Reid Spencer9bbaa2a2005-04-25 03:59:26 +00001//===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
Reid Spencer39a762d2005-04-25 02:53:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer9bbaa2a2005-04-25 03:59:26 +00005// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Reid Spencer39a762d2005-04-25 02:53:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a variety of small optimizations for calls to specific
11// well-known (e.g. runtime library) function calls. For example, a call to the
12// function "exit(3)" that occurs within the main() function can be transformed
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000013// into a simple "return 3" instruction. Any optimization that takes this form
14// (replace call to library function with simpler code that provides same
15// result) belongs in this file.
Reid Spencer39a762d2005-04-25 02:53:12 +000016//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Transforms/IPO.h"
20#include "llvm/Module.h"
21#include "llvm/Pass.h"
22#include "llvm/Instructions.h"
23#include "llvm/ADT/Statistic.h"
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000024#include "llvm/ADT/hash_map"
Reid Spencer39a762d2005-04-25 02:53:12 +000025using namespace llvm;
26
27namespace {
28 Statistic<> SimplifiedLibCalls("simplified-lib-calls",
29 "Number of well-known library calls simplified");
30
31 /// This class is the base class for a set of small but important
32 /// optimizations of calls to well-known functions, such as those in the c
33 /// library. This class provides the basic infrastructure for handling
34 /// runOnModule. Subclasses register themselves and provide two methods:
35 /// RecognizeCall and OptimizeCall. Whenever this class finds a function call,
36 /// it asks the subclasses to recognize the call. If it is recognized, then
37 /// the OptimizeCall method is called on that subclass instance. In this way
38 /// the subclasses implement the calling conditions on which they trigger and
39 /// the action to perform, making it easy to add new optimizations of this
40 /// form.
41 /// @brief A ModulePass for optimizing well-known function calls
42 struct SimplifyLibCalls : public ModulePass {
43
44
45 /// For this pass, process all of the function calls in the module, calling
46 /// RecognizeCall and OptimizeCall as appropriate.
47 virtual bool runOnModule(Module &M);
48
49 };
50
51 RegisterOpt<SimplifyLibCalls>
52 X("simplify-libcalls","Simplify well-known library calls");
53
54 struct CallOptimizer
55 {
56 /// @brief Constructor that registers the optimization
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000057 CallOptimizer(const char * fname );
Reid Spencer39a762d2005-04-25 02:53:12 +000058
59 virtual ~CallOptimizer();
60
61 /// The implementations of this function in subclasses is the heart of the
62 /// SimplifyLibCalls algorithm. Sublcasses of this class implement
63 /// OptimizeCall to determine if (a) the conditions are right for optimizing
64 /// the call and (b) to perform the optimization. If an action is taken
65 /// against ci, the subclass is responsible for returning true and ensuring
66 /// that ci is erased from its parent.
67 /// @param ci the call instruction under consideration
68 /// @param f the function that ci calls.
69 /// @brief Optimize a call, if possible.
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000070 virtual bool OptimizeCall(CallInst* ci) const = 0;
71
72 const std::string& getFunctionName() const { return func_name; }
73 private:
74 std::string func_name;
Reid Spencer39a762d2005-04-25 02:53:12 +000075 };
76
77 /// @brief The list of optimizations deriving from CallOptimizer
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000078 hash_map<std::string,CallOptimizer*> optlist;
Reid Spencer39a762d2005-04-25 02:53:12 +000079
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000080 CallOptimizer::CallOptimizer(const char* fname)
81 : func_name(fname)
Reid Spencer39a762d2005-04-25 02:53:12 +000082 {
83 // Register this call optimizer
Reid Spencer9bbaa2a2005-04-25 03:59:26 +000084 optlist[func_name] = this;
Reid Spencer39a762d2005-04-25 02:53:12 +000085 }
86
87 /// Make sure we get our virtual table in this file.
88 CallOptimizer::~CallOptimizer() {}
89}
90
91ModulePass *llvm::createSimplifyLibCallsPass()
92{
93 return new SimplifyLibCalls();
94}
95
96bool SimplifyLibCalls::runOnModule(Module &M)
97{
98 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
99 {
100 // All the "well-known" functions are external because they live in a
101 // runtime library somewhere and were (probably) not compiled by LLVM.
102 // So, we only act on external functions that have non-empty uses.
103 if (FI->isExternal() && !FI->use_empty())
104 {
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000105 // Get the optimization class that pertains to this function
106 if (CallOptimizer* CO = optlist[FI->getName()] )
Reid Spencer39a762d2005-04-25 02:53:12 +0000107 {
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000108 // Loop over each of the uses of the function
109 for (Value::use_iterator UI = FI->use_begin(), UE = FI->use_end();
110 UI != UE ; )
Reid Spencer39a762d2005-04-25 02:53:12 +0000111 {
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000112 // If the use of the function is a call instruction
113 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
Reid Spencer39a762d2005-04-25 02:53:12 +0000114 {
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000115 // Do the optimization on the CallOptimizer we found earlier.
116 if (CO->OptimizeCall(CI))
Reid Spencer39a762d2005-04-25 02:53:12 +0000117 {
118 ++SimplifiedLibCalls;
119 break;
120 }
121 }
122 }
123 }
124 }
125 }
126 return true;
127}
128
129namespace {
130
131/// This CallOptimizer will find instances of a call to "exit" that occurs
132/// within the "main" function and change it to a simple "ret" instruction with
133/// the same value as passed to the exit function. It assumes that the
134/// instructions after the call to exit(3) can be deleted since they are
135/// unreachable anyway.
136/// @brief Replace calls to exit in main with a simple return
137struct ExitInMainOptimization : public CallOptimizer
138{
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000139 ExitInMainOptimization() : CallOptimizer("exit") {}
140 virtual ~ExitInMainOptimization() {}
141 virtual bool OptimizeCall(CallInst* ci) const
142 {
143 // If the call isn't coming from main or main doesn't have external linkage
144 // or the return type of main is not the same as the type of the exit(3)
145 // argument then we don't act
146 if (const Function* f = ci->getParent()->getParent())
147 if (!(f->hasExternalLinkage() &&
148 (f->getReturnType() == ci->getOperand(1)->getType()) &&
149 (f->getName() == "main")))
150 return false;
Reid Spencer39a762d2005-04-25 02:53:12 +0000151
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000152 // Okay, time to replace it. Get the basic block of the call instruction
153 BasicBlock* bb = ci->getParent();
Reid Spencer39a762d2005-04-25 02:53:12 +0000154
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000155 // Create a return instruction that we'll replace the call with. Note that
156 // the argument of the return is the argument of the call instruction.
157 ReturnInst* ri = new ReturnInst(ci->getOperand(1), ci);
Reid Spencer39a762d2005-04-25 02:53:12 +0000158
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000159 // Erase everything from the call instruction to the end of the block. There
160 // really shouldn't be anything other than the call instruction, but just in
161 // case there is we delete it all because its now dead.
162 bb->getInstList().erase(ci, bb->end());
Reid Spencer39a762d2005-04-25 02:53:12 +0000163
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000164 return true;
165 }
Reid Spencer39a762d2005-04-25 02:53:12 +0000166} ExitInMainOptimizer;
167
Reid Spencer9bbaa2a2005-04-25 03:59:26 +0000168/// This CallOptimizer will find instances of a call to "exit" that occurs
169/// within the "main" function and change it to a simple "ret" instruction with
170/// the same value as passed to the exit function. It assumes that the
171/// instructions after the call to exit(3) can be deleted since they are
172/// unreachable anyway.
173/// @brief Replace calls to exit in main with a simple return
174struct StrCatOptimization : public CallOptimizer
175{
176 StrCatOptimization() : CallOptimizer("strcat") {}
177 virtual ~StrCatOptimization() {}
178 virtual bool OptimizeCall(CallInst* ci) const
179 {
180 return false;
181 }
182} StrCatOptimizer;
183
Reid Spencer39a762d2005-04-25 02:53:12 +0000184}