1f4a2713aSLionel Sambuc //===-- IPConstantPropagation.cpp - Propagate constants through calls -----===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This pass implements an _extremely_ simple interprocedural constant
11f4a2713aSLionel Sambuc // propagation pass. It could certainly be improved in many different ways,
12f4a2713aSLionel Sambuc // like using a worklist. This pass makes arguments dead, but does not remove
13f4a2713aSLionel Sambuc // them. The existing dead argument elimination pass should be run after this
14f4a2713aSLionel Sambuc // to clean up the mess.
15f4a2713aSLionel Sambuc //
16f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
17f4a2713aSLionel Sambuc
18f4a2713aSLionel Sambuc #include "llvm/Transforms/IPO.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
20f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
21f4a2713aSLionel Sambuc #include "llvm/Analysis/ValueTracking.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/IR/CallSite.h"
23f4a2713aSLionel Sambuc #include "llvm/IR/Constants.h"
24f4a2713aSLionel Sambuc #include "llvm/IR/Instructions.h"
25f4a2713aSLionel Sambuc #include "llvm/IR/Module.h"
26f4a2713aSLionel Sambuc #include "llvm/Pass.h"
27f4a2713aSLionel Sambuc using namespace llvm;
28f4a2713aSLionel Sambuc
29*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "ipconstprop"
30*0a6a1f1dSLionel Sambuc
31f4a2713aSLionel Sambuc STATISTIC(NumArgumentsProped, "Number of args turned into constants");
32f4a2713aSLionel Sambuc STATISTIC(NumReturnValProped, "Number of return values turned into constants");
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc namespace {
35f4a2713aSLionel Sambuc /// IPCP - The interprocedural constant propagation pass
36f4a2713aSLionel Sambuc ///
37f4a2713aSLionel Sambuc struct IPCP : public ModulePass {
38f4a2713aSLionel Sambuc static char ID; // Pass identification, replacement for typeid
IPCP__anone4b0d59f0111::IPCP39f4a2713aSLionel Sambuc IPCP() : ModulePass(ID) {
40f4a2713aSLionel Sambuc initializeIPCPPass(*PassRegistry::getPassRegistry());
41f4a2713aSLionel Sambuc }
42f4a2713aSLionel Sambuc
43*0a6a1f1dSLionel Sambuc bool runOnModule(Module &M) override;
44f4a2713aSLionel Sambuc private:
45f4a2713aSLionel Sambuc bool PropagateConstantsIntoArguments(Function &F);
46f4a2713aSLionel Sambuc bool PropagateConstantReturn(Function &F);
47f4a2713aSLionel Sambuc };
48f4a2713aSLionel Sambuc }
49f4a2713aSLionel Sambuc
50f4a2713aSLionel Sambuc char IPCP::ID = 0;
51f4a2713aSLionel Sambuc INITIALIZE_PASS(IPCP, "ipconstprop",
52f4a2713aSLionel Sambuc "Interprocedural constant propagation", false, false)
53f4a2713aSLionel Sambuc
createIPConstantPropagationPass()54f4a2713aSLionel Sambuc ModulePass *llvm::createIPConstantPropagationPass() { return new IPCP(); }
55f4a2713aSLionel Sambuc
runOnModule(Module & M)56f4a2713aSLionel Sambuc bool IPCP::runOnModule(Module &M) {
57f4a2713aSLionel Sambuc bool Changed = false;
58f4a2713aSLionel Sambuc bool LocalChange = true;
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc // FIXME: instead of using smart algorithms, we just iterate until we stop
61f4a2713aSLionel Sambuc // making changes.
62f4a2713aSLionel Sambuc while (LocalChange) {
63f4a2713aSLionel Sambuc LocalChange = false;
64f4a2713aSLionel Sambuc for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
65f4a2713aSLionel Sambuc if (!I->isDeclaration()) {
66f4a2713aSLionel Sambuc // Delete any klingons.
67f4a2713aSLionel Sambuc I->removeDeadConstantUsers();
68f4a2713aSLionel Sambuc if (I->hasLocalLinkage())
69f4a2713aSLionel Sambuc LocalChange |= PropagateConstantsIntoArguments(*I);
70f4a2713aSLionel Sambuc Changed |= PropagateConstantReturn(*I);
71f4a2713aSLionel Sambuc }
72f4a2713aSLionel Sambuc Changed |= LocalChange;
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc return Changed;
75f4a2713aSLionel Sambuc }
76f4a2713aSLionel Sambuc
77f4a2713aSLionel Sambuc /// PropagateConstantsIntoArguments - Look at all uses of the specified
78f4a2713aSLionel Sambuc /// function. If all uses are direct call sites, and all pass a particular
79f4a2713aSLionel Sambuc /// constant in for an argument, propagate that constant in as the argument.
80f4a2713aSLionel Sambuc ///
PropagateConstantsIntoArguments(Function & F)81f4a2713aSLionel Sambuc bool IPCP::PropagateConstantsIntoArguments(Function &F) {
82f4a2713aSLionel Sambuc if (F.arg_empty() || F.use_empty()) return false; // No arguments? Early exit.
83f4a2713aSLionel Sambuc
84f4a2713aSLionel Sambuc // For each argument, keep track of its constant value and whether it is a
85f4a2713aSLionel Sambuc // constant or not. The bool is driven to true when found to be non-constant.
86f4a2713aSLionel Sambuc SmallVector<std::pair<Constant*, bool>, 16> ArgumentConstants;
87f4a2713aSLionel Sambuc ArgumentConstants.resize(F.arg_size());
88f4a2713aSLionel Sambuc
89f4a2713aSLionel Sambuc unsigned NumNonconstant = 0;
90*0a6a1f1dSLionel Sambuc for (Use &U : F.uses()) {
91*0a6a1f1dSLionel Sambuc User *UR = U.getUser();
92f4a2713aSLionel Sambuc // Ignore blockaddress uses.
93*0a6a1f1dSLionel Sambuc if (isa<BlockAddress>(UR)) continue;
94f4a2713aSLionel Sambuc
95f4a2713aSLionel Sambuc // Used by a non-instruction, or not the callee of a function, do not
96f4a2713aSLionel Sambuc // transform.
97*0a6a1f1dSLionel Sambuc if (!isa<CallInst>(UR) && !isa<InvokeInst>(UR))
98f4a2713aSLionel Sambuc return false;
99f4a2713aSLionel Sambuc
100*0a6a1f1dSLionel Sambuc CallSite CS(cast<Instruction>(UR));
101*0a6a1f1dSLionel Sambuc if (!CS.isCallee(&U))
102f4a2713aSLionel Sambuc return false;
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc // Check out all of the potentially constant arguments. Note that we don't
105f4a2713aSLionel Sambuc // inspect varargs here.
106f4a2713aSLionel Sambuc CallSite::arg_iterator AI = CS.arg_begin();
107f4a2713aSLionel Sambuc Function::arg_iterator Arg = F.arg_begin();
108f4a2713aSLionel Sambuc for (unsigned i = 0, e = ArgumentConstants.size(); i != e;
109f4a2713aSLionel Sambuc ++i, ++AI, ++Arg) {
110f4a2713aSLionel Sambuc
111f4a2713aSLionel Sambuc // If this argument is known non-constant, ignore it.
112f4a2713aSLionel Sambuc if (ArgumentConstants[i].second)
113f4a2713aSLionel Sambuc continue;
114f4a2713aSLionel Sambuc
115f4a2713aSLionel Sambuc Constant *C = dyn_cast<Constant>(*AI);
116*0a6a1f1dSLionel Sambuc if (C && ArgumentConstants[i].first == nullptr) {
117f4a2713aSLionel Sambuc ArgumentConstants[i].first = C; // First constant seen.
118f4a2713aSLionel Sambuc } else if (C && ArgumentConstants[i].first == C) {
119f4a2713aSLionel Sambuc // Still the constant value we think it is.
120f4a2713aSLionel Sambuc } else if (*AI == &*Arg) {
121f4a2713aSLionel Sambuc // Ignore recursive calls passing argument down.
122f4a2713aSLionel Sambuc } else {
123f4a2713aSLionel Sambuc // Argument became non-constant. If all arguments are non-constant now,
124f4a2713aSLionel Sambuc // give up on this function.
125f4a2713aSLionel Sambuc if (++NumNonconstant == ArgumentConstants.size())
126f4a2713aSLionel Sambuc return false;
127f4a2713aSLionel Sambuc ArgumentConstants[i].second = true;
128f4a2713aSLionel Sambuc }
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc }
131f4a2713aSLionel Sambuc
132f4a2713aSLionel Sambuc // If we got to this point, there is a constant argument!
133f4a2713aSLionel Sambuc assert(NumNonconstant != ArgumentConstants.size());
134f4a2713aSLionel Sambuc bool MadeChange = false;
135f4a2713aSLionel Sambuc Function::arg_iterator AI = F.arg_begin();
136f4a2713aSLionel Sambuc for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) {
137f4a2713aSLionel Sambuc // Do we have a constant argument?
138f4a2713aSLionel Sambuc if (ArgumentConstants[i].second || AI->use_empty() ||
139*0a6a1f1dSLionel Sambuc AI->hasInAllocaAttr() || (AI->hasByValAttr() && !F.onlyReadsMemory()))
140f4a2713aSLionel Sambuc continue;
141f4a2713aSLionel Sambuc
142f4a2713aSLionel Sambuc Value *V = ArgumentConstants[i].first;
143*0a6a1f1dSLionel Sambuc if (!V) V = UndefValue::get(AI->getType());
144f4a2713aSLionel Sambuc AI->replaceAllUsesWith(V);
145f4a2713aSLionel Sambuc ++NumArgumentsProped;
146f4a2713aSLionel Sambuc MadeChange = true;
147f4a2713aSLionel Sambuc }
148f4a2713aSLionel Sambuc return MadeChange;
149f4a2713aSLionel Sambuc }
150f4a2713aSLionel Sambuc
151f4a2713aSLionel Sambuc
152f4a2713aSLionel Sambuc // Check to see if this function returns one or more constants. If so, replace
153f4a2713aSLionel Sambuc // all callers that use those return values with the constant value. This will
154f4a2713aSLionel Sambuc // leave in the actual return values and instructions, but deadargelim will
155f4a2713aSLionel Sambuc // clean that up.
156f4a2713aSLionel Sambuc //
157f4a2713aSLionel Sambuc // Additionally if a function always returns one of its arguments directly,
158f4a2713aSLionel Sambuc // callers will be updated to use the value they pass in directly instead of
159f4a2713aSLionel Sambuc // using the return value.
PropagateConstantReturn(Function & F)160f4a2713aSLionel Sambuc bool IPCP::PropagateConstantReturn(Function &F) {
161f4a2713aSLionel Sambuc if (F.getReturnType()->isVoidTy())
162f4a2713aSLionel Sambuc return false; // No return value.
163f4a2713aSLionel Sambuc
164f4a2713aSLionel Sambuc // If this function could be overridden later in the link stage, we can't
165f4a2713aSLionel Sambuc // propagate information about its results into callers.
166f4a2713aSLionel Sambuc if (F.mayBeOverridden())
167f4a2713aSLionel Sambuc return false;
168f4a2713aSLionel Sambuc
169f4a2713aSLionel Sambuc // Check to see if this function returns a constant.
170f4a2713aSLionel Sambuc SmallVector<Value *,4> RetVals;
171f4a2713aSLionel Sambuc StructType *STy = dyn_cast<StructType>(F.getReturnType());
172f4a2713aSLionel Sambuc if (STy)
173f4a2713aSLionel Sambuc for (unsigned i = 0, e = STy->getNumElements(); i < e; ++i)
174f4a2713aSLionel Sambuc RetVals.push_back(UndefValue::get(STy->getElementType(i)));
175f4a2713aSLionel Sambuc else
176f4a2713aSLionel Sambuc RetVals.push_back(UndefValue::get(F.getReturnType()));
177f4a2713aSLionel Sambuc
178f4a2713aSLionel Sambuc unsigned NumNonConstant = 0;
179f4a2713aSLionel Sambuc for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
180f4a2713aSLionel Sambuc if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
181f4a2713aSLionel Sambuc for (unsigned i = 0, e = RetVals.size(); i != e; ++i) {
182f4a2713aSLionel Sambuc // Already found conflicting return values?
183f4a2713aSLionel Sambuc Value *RV = RetVals[i];
184f4a2713aSLionel Sambuc if (!RV)
185f4a2713aSLionel Sambuc continue;
186f4a2713aSLionel Sambuc
187f4a2713aSLionel Sambuc // Find the returned value
188f4a2713aSLionel Sambuc Value *V;
189f4a2713aSLionel Sambuc if (!STy)
190f4a2713aSLionel Sambuc V = RI->getOperand(0);
191f4a2713aSLionel Sambuc else
192f4a2713aSLionel Sambuc V = FindInsertedValue(RI->getOperand(0), i);
193f4a2713aSLionel Sambuc
194f4a2713aSLionel Sambuc if (V) {
195f4a2713aSLionel Sambuc // Ignore undefs, we can change them into anything
196f4a2713aSLionel Sambuc if (isa<UndefValue>(V))
197f4a2713aSLionel Sambuc continue;
198f4a2713aSLionel Sambuc
199f4a2713aSLionel Sambuc // Try to see if all the rets return the same constant or argument.
200f4a2713aSLionel Sambuc if (isa<Constant>(V) || isa<Argument>(V)) {
201f4a2713aSLionel Sambuc if (isa<UndefValue>(RV)) {
202f4a2713aSLionel Sambuc // No value found yet? Try the current one.
203f4a2713aSLionel Sambuc RetVals[i] = V;
204f4a2713aSLionel Sambuc continue;
205f4a2713aSLionel Sambuc }
206f4a2713aSLionel Sambuc // Returning the same value? Good.
207f4a2713aSLionel Sambuc if (RV == V)
208f4a2713aSLionel Sambuc continue;
209f4a2713aSLionel Sambuc }
210f4a2713aSLionel Sambuc }
211f4a2713aSLionel Sambuc // Different or no known return value? Don't propagate this return
212f4a2713aSLionel Sambuc // value.
213*0a6a1f1dSLionel Sambuc RetVals[i] = nullptr;
214*0a6a1f1dSLionel Sambuc // All values non-constant? Stop looking.
215f4a2713aSLionel Sambuc if (++NumNonConstant == RetVals.size())
216f4a2713aSLionel Sambuc return false;
217f4a2713aSLionel Sambuc }
218f4a2713aSLionel Sambuc }
219f4a2713aSLionel Sambuc
220f4a2713aSLionel Sambuc // If we got here, the function returns at least one constant value. Loop
221f4a2713aSLionel Sambuc // over all users, replacing any uses of the return value with the returned
222f4a2713aSLionel Sambuc // constant.
223f4a2713aSLionel Sambuc bool MadeChange = false;
224*0a6a1f1dSLionel Sambuc for (Use &U : F.uses()) {
225*0a6a1f1dSLionel Sambuc CallSite CS(U.getUser());
226f4a2713aSLionel Sambuc Instruction* Call = CS.getInstruction();
227f4a2713aSLionel Sambuc
228f4a2713aSLionel Sambuc // Not a call instruction or a call instruction that's not calling F
229f4a2713aSLionel Sambuc // directly?
230*0a6a1f1dSLionel Sambuc if (!Call || !CS.isCallee(&U))
231f4a2713aSLionel Sambuc continue;
232f4a2713aSLionel Sambuc
233f4a2713aSLionel Sambuc // Call result not used?
234f4a2713aSLionel Sambuc if (Call->use_empty())
235f4a2713aSLionel Sambuc continue;
236f4a2713aSLionel Sambuc
237f4a2713aSLionel Sambuc MadeChange = true;
238f4a2713aSLionel Sambuc
239*0a6a1f1dSLionel Sambuc if (!STy) {
240f4a2713aSLionel Sambuc Value* New = RetVals[0];
241f4a2713aSLionel Sambuc if (Argument *A = dyn_cast<Argument>(New))
242f4a2713aSLionel Sambuc // Was an argument returned? Then find the corresponding argument in
243f4a2713aSLionel Sambuc // the call instruction and use that.
244f4a2713aSLionel Sambuc New = CS.getArgument(A->getArgNo());
245f4a2713aSLionel Sambuc Call->replaceAllUsesWith(New);
246f4a2713aSLionel Sambuc continue;
247f4a2713aSLionel Sambuc }
248f4a2713aSLionel Sambuc
249*0a6a1f1dSLionel Sambuc for (auto I = Call->user_begin(), E = Call->user_end(); I != E;) {
250f4a2713aSLionel Sambuc Instruction *Ins = cast<Instruction>(*I);
251f4a2713aSLionel Sambuc
252f4a2713aSLionel Sambuc // Increment now, so we can remove the use
253f4a2713aSLionel Sambuc ++I;
254f4a2713aSLionel Sambuc
255f4a2713aSLionel Sambuc // Find the index of the retval to replace with
256f4a2713aSLionel Sambuc int index = -1;
257f4a2713aSLionel Sambuc if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Ins))
258f4a2713aSLionel Sambuc if (EV->hasIndices())
259f4a2713aSLionel Sambuc index = *EV->idx_begin();
260f4a2713aSLionel Sambuc
261f4a2713aSLionel Sambuc // If this use uses a specific return value, and we have a replacement,
262f4a2713aSLionel Sambuc // replace it.
263f4a2713aSLionel Sambuc if (index != -1) {
264f4a2713aSLionel Sambuc Value *New = RetVals[index];
265f4a2713aSLionel Sambuc if (New) {
266f4a2713aSLionel Sambuc if (Argument *A = dyn_cast<Argument>(New))
267f4a2713aSLionel Sambuc // Was an argument returned? Then find the corresponding argument in
268f4a2713aSLionel Sambuc // the call instruction and use that.
269f4a2713aSLionel Sambuc New = CS.getArgument(A->getArgNo());
270f4a2713aSLionel Sambuc Ins->replaceAllUsesWith(New);
271f4a2713aSLionel Sambuc Ins->eraseFromParent();
272f4a2713aSLionel Sambuc }
273f4a2713aSLionel Sambuc }
274f4a2713aSLionel Sambuc }
275f4a2713aSLionel Sambuc }
276f4a2713aSLionel Sambuc
277f4a2713aSLionel Sambuc if (MadeChange) ++NumReturnValProped;
278f4a2713aSLionel Sambuc return MadeChange;
279f4a2713aSLionel Sambuc }
280