Mercurial > hg > Members > tobaru > cbc > CbC_llvm
comparison lib/Transforms/IPO/IPConstantPropagation.cpp @ 80:67baa08a3894
update to LLVM 3.6
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 25 Sep 2014 16:56:18 +0900 |
parents | 54457678186b |
children | 1172e4bd9c6f |
comparison
equal
deleted
inserted
replaced
76:9e74acfe8c42 | 80:67baa08a3894 |
---|---|
13 // them. The existing dead argument elimination pass should be run after this | 13 // them. The existing dead argument elimination pass should be run after this |
14 // to clean up the mess. | 14 // to clean up the mess. |
15 // | 15 // |
16 //===----------------------------------------------------------------------===// | 16 //===----------------------------------------------------------------------===// |
17 | 17 |
18 #define DEBUG_TYPE "ipconstprop" | |
19 #include "llvm/Transforms/IPO.h" | 18 #include "llvm/Transforms/IPO.h" |
20 #include "llvm/ADT/SmallVector.h" | 19 #include "llvm/ADT/SmallVector.h" |
21 #include "llvm/ADT/Statistic.h" | 20 #include "llvm/ADT/Statistic.h" |
22 #include "llvm/Analysis/ValueTracking.h" | 21 #include "llvm/Analysis/ValueTracking.h" |
22 #include "llvm/IR/CallSite.h" | |
23 #include "llvm/IR/Constants.h" | 23 #include "llvm/IR/Constants.h" |
24 #include "llvm/IR/Instructions.h" | 24 #include "llvm/IR/Instructions.h" |
25 #include "llvm/IR/Module.h" | 25 #include "llvm/IR/Module.h" |
26 #include "llvm/Pass.h" | 26 #include "llvm/Pass.h" |
27 #include "llvm/Support/CallSite.h" | |
28 using namespace llvm; | 27 using namespace llvm; |
28 | |
29 #define DEBUG_TYPE "ipconstprop" | |
29 | 30 |
30 STATISTIC(NumArgumentsProped, "Number of args turned into constants"); | 31 STATISTIC(NumArgumentsProped, "Number of args turned into constants"); |
31 STATISTIC(NumReturnValProped, "Number of return values turned into constants"); | 32 STATISTIC(NumReturnValProped, "Number of return values turned into constants"); |
32 | 33 |
33 namespace { | 34 namespace { |
37 static char ID; // Pass identification, replacement for typeid | 38 static char ID; // Pass identification, replacement for typeid |
38 IPCP() : ModulePass(ID) { | 39 IPCP() : ModulePass(ID) { |
39 initializeIPCPPass(*PassRegistry::getPassRegistry()); | 40 initializeIPCPPass(*PassRegistry::getPassRegistry()); |
40 } | 41 } |
41 | 42 |
42 bool runOnModule(Module &M); | 43 bool runOnModule(Module &M) override; |
43 private: | 44 private: |
44 bool PropagateConstantsIntoArguments(Function &F); | 45 bool PropagateConstantsIntoArguments(Function &F); |
45 bool PropagateConstantReturn(Function &F); | 46 bool PropagateConstantReturn(Function &F); |
46 }; | 47 }; |
47 } | 48 } |
84 // constant or not. The bool is driven to true when found to be non-constant. | 85 // constant or not. The bool is driven to true when found to be non-constant. |
85 SmallVector<std::pair<Constant*, bool>, 16> ArgumentConstants; | 86 SmallVector<std::pair<Constant*, bool>, 16> ArgumentConstants; |
86 ArgumentConstants.resize(F.arg_size()); | 87 ArgumentConstants.resize(F.arg_size()); |
87 | 88 |
88 unsigned NumNonconstant = 0; | 89 unsigned NumNonconstant = 0; |
89 for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) { | 90 for (Use &U : F.uses()) { |
90 User *U = *UI; | 91 User *UR = U.getUser(); |
91 // Ignore blockaddress uses. | 92 // Ignore blockaddress uses. |
92 if (isa<BlockAddress>(U)) continue; | 93 if (isa<BlockAddress>(UR)) continue; |
93 | 94 |
94 // Used by a non-instruction, or not the callee of a function, do not | 95 // Used by a non-instruction, or not the callee of a function, do not |
95 // transform. | 96 // transform. |
96 if (!isa<CallInst>(U) && !isa<InvokeInst>(U)) | 97 if (!isa<CallInst>(UR) && !isa<InvokeInst>(UR)) |
97 return false; | 98 return false; |
98 | 99 |
99 CallSite CS(cast<Instruction>(U)); | 100 CallSite CS(cast<Instruction>(UR)); |
100 if (!CS.isCallee(UI)) | 101 if (!CS.isCallee(&U)) |
101 return false; | 102 return false; |
102 | 103 |
103 // Check out all of the potentially constant arguments. Note that we don't | 104 // Check out all of the potentially constant arguments. Note that we don't |
104 // inspect varargs here. | 105 // inspect varargs here. |
105 CallSite::arg_iterator AI = CS.arg_begin(); | 106 CallSite::arg_iterator AI = CS.arg_begin(); |
110 // If this argument is known non-constant, ignore it. | 111 // If this argument is known non-constant, ignore it. |
111 if (ArgumentConstants[i].second) | 112 if (ArgumentConstants[i].second) |
112 continue; | 113 continue; |
113 | 114 |
114 Constant *C = dyn_cast<Constant>(*AI); | 115 Constant *C = dyn_cast<Constant>(*AI); |
115 if (C && ArgumentConstants[i].first == 0) { | 116 if (C && ArgumentConstants[i].first == nullptr) { |
116 ArgumentConstants[i].first = C; // First constant seen. | 117 ArgumentConstants[i].first = C; // First constant seen. |
117 } else if (C && ArgumentConstants[i].first == C) { | 118 } else if (C && ArgumentConstants[i].first == C) { |
118 // Still the constant value we think it is. | 119 // Still the constant value we think it is. |
119 } else if (*AI == &*Arg) { | 120 } else if (*AI == &*Arg) { |
120 // Ignore recursive calls passing argument down. | 121 // Ignore recursive calls passing argument down. |
133 bool MadeChange = false; | 134 bool MadeChange = false; |
134 Function::arg_iterator AI = F.arg_begin(); | 135 Function::arg_iterator AI = F.arg_begin(); |
135 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) { | 136 for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) { |
136 // Do we have a constant argument? | 137 // Do we have a constant argument? |
137 if (ArgumentConstants[i].second || AI->use_empty() || | 138 if (ArgumentConstants[i].second || AI->use_empty() || |
138 (AI->hasByValAttr() && !F.onlyReadsMemory())) | 139 AI->hasInAllocaAttr() || (AI->hasByValAttr() && !F.onlyReadsMemory())) |
139 continue; | 140 continue; |
140 | 141 |
141 Value *V = ArgumentConstants[i].first; | 142 Value *V = ArgumentConstants[i].first; |
142 if (V == 0) V = UndefValue::get(AI->getType()); | 143 if (!V) V = UndefValue::get(AI->getType()); |
143 AI->replaceAllUsesWith(V); | 144 AI->replaceAllUsesWith(V); |
144 ++NumArgumentsProped; | 145 ++NumArgumentsProped; |
145 MadeChange = true; | 146 MadeChange = true; |
146 } | 147 } |
147 return MadeChange; | 148 return MadeChange; |
207 continue; | 208 continue; |
208 } | 209 } |
209 } | 210 } |
210 // Different or no known return value? Don't propagate this return | 211 // Different or no known return value? Don't propagate this return |
211 // value. | 212 // value. |
212 RetVals[i] = 0; | 213 RetVals[i] = nullptr; |
213 // All values non-constant? Stop looking. | 214 // All values non-constant? Stop looking. |
214 if (++NumNonConstant == RetVals.size()) | 215 if (++NumNonConstant == RetVals.size()) |
215 return false; | 216 return false; |
216 } | 217 } |
217 } | 218 } |
218 | 219 |
219 // If we got here, the function returns at least one constant value. Loop | 220 // If we got here, the function returns at least one constant value. Loop |
220 // over all users, replacing any uses of the return value with the returned | 221 // over all users, replacing any uses of the return value with the returned |
221 // constant. | 222 // constant. |
222 bool MadeChange = false; | 223 bool MadeChange = false; |
223 for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E; ++UI) { | 224 for (Use &U : F.uses()) { |
224 CallSite CS(*UI); | 225 CallSite CS(U.getUser()); |
225 Instruction* Call = CS.getInstruction(); | 226 Instruction* Call = CS.getInstruction(); |
226 | 227 |
227 // Not a call instruction or a call instruction that's not calling F | 228 // Not a call instruction or a call instruction that's not calling F |
228 // directly? | 229 // directly? |
229 if (!Call || !CS.isCallee(UI)) | 230 if (!Call || !CS.isCallee(&U)) |
230 continue; | 231 continue; |
231 | 232 |
232 // Call result not used? | 233 // Call result not used? |
233 if (Call->use_empty()) | 234 if (Call->use_empty()) |
234 continue; | 235 continue; |
235 | 236 |
236 MadeChange = true; | 237 MadeChange = true; |
237 | 238 |
238 if (STy == 0) { | 239 if (!STy) { |
239 Value* New = RetVals[0]; | 240 Value* New = RetVals[0]; |
240 if (Argument *A = dyn_cast<Argument>(New)) | 241 if (Argument *A = dyn_cast<Argument>(New)) |
241 // Was an argument returned? Then find the corresponding argument in | 242 // Was an argument returned? Then find the corresponding argument in |
242 // the call instruction and use that. | 243 // the call instruction and use that. |
243 New = CS.getArgument(A->getArgNo()); | 244 New = CS.getArgument(A->getArgNo()); |
244 Call->replaceAllUsesWith(New); | 245 Call->replaceAllUsesWith(New); |
245 continue; | 246 continue; |
246 } | 247 } |
247 | 248 |
248 for (Value::use_iterator I = Call->use_begin(), E = Call->use_end(); | 249 for (auto I = Call->user_begin(), E = Call->user_end(); I != E;) { |
249 I != E;) { | |
250 Instruction *Ins = cast<Instruction>(*I); | 250 Instruction *Ins = cast<Instruction>(*I); |
251 | 251 |
252 // Increment now, so we can remove the use | 252 // Increment now, so we can remove the use |
253 ++I; | 253 ++I; |
254 | 254 |