@@ -31,6 +31,16 @@ namespace {
31
31
// FunctionInfo - For each function, calculate the size of it in blocks and
32
32
// instructions.
33
33
struct FunctionInfo {
34
+ // HasAllocas - Keep track of whether or not a function contains an alloca
35
+ // instruction that is not in the entry block of the function. Inlining
36
+ // this call could cause us to blow out the stack, because the stack memory
37
+ // would never be released.
38
+ //
39
+ // FIXME: LLVM needs a way of dealloca'ing memory, which would make this
40
+ // irrelevant!
41
+ //
42
+ bool HasAllocas;
43
+
34
44
// NumInsts, NumBlocks - Keep track of how large each function is, which is
35
45
// used to estimate the code size cost of inlining it.
36
46
unsigned NumInsts, NumBlocks;
@@ -41,7 +51,11 @@ namespace {
41
51
// entry here.
42
52
std::vector<ArgInfo> ArgumentWeights;
43
53
44
- FunctionInfo () : NumInsts(0 ), NumBlocks(0 ) {}
54
+ FunctionInfo () : HasAllocas(false ), NumInsts(0 ), NumBlocks(0 ) {}
55
+
56
+ // / analyzeFunction - Fill in the current structure with information gleaned
57
+ // / from the specified function.
58
+ void analyzeFunction (Function *F);
45
59
};
46
60
47
61
class SimpleInliner : public Inliner {
@@ -123,6 +137,41 @@ static unsigned CountCodeReductionForAlloca(Value *V) {
123
137
return Reduction;
124
138
}
125
139
140
+ // / analyzeFunction - Fill in the current structure with information gleaned
141
+ // / from the specified function.
142
+ void FunctionInfo::analyzeFunction (Function *F) {
143
+ unsigned NumInsts = 0 , NumBlocks = 0 ;
144
+
145
+ // Look at the size of the callee. Each basic block counts as 20 units, and
146
+ // each instruction counts as 10.
147
+ for (Function::const_iterator BB = F->begin (), E = F->end (); BB != E; ++BB) {
148
+ for (BasicBlock::const_iterator II = BB->begin (), E = BB->end ();
149
+ II != E; ++II) {
150
+ ++NumInsts;
151
+
152
+ // If there is an alloca in the body of the function, we cannot currently
153
+ // inline the function without the risk of exploding the stack.
154
+ if (isa<AllocaInst>(II) && BB != F->begin ()) {
155
+ HasAllocas = true ;
156
+ this ->NumBlocks = this ->NumInsts = 1 ;
157
+ return ;
158
+ }
159
+ }
160
+
161
+ ++NumBlocks;
162
+ }
163
+
164
+ this ->NumBlocks = NumBlocks;
165
+ this ->NumInsts = NumInsts;
166
+
167
+ // Check out all of the arguments to the function, figuring out how much
168
+ // code can be eliminated if one of the arguments is a constant.
169
+ for (Function::aiterator I = F->abegin (), E = F->aend (); I != E; ++I)
170
+ ArgumentWeights.push_back (ArgInfo (CountCodeReductionForConstant (I),
171
+ CountCodeReductionForAlloca (I)));
172
+ }
173
+
174
+
126
175
// getInlineCost - The heuristic used to determine if we should inline the
127
176
// function call or not.
128
177
//
@@ -149,31 +198,14 @@ int SimpleInliner::getInlineCost(CallSite CS) {
149
198
// Get information about the callee...
150
199
FunctionInfo &CalleeFI = CachedFunctionInfo[Callee];
151
200
152
- // If we haven't calculated this information yet...
153
- if (CalleeFI.NumBlocks == 0 ) {
154
- unsigned NumInsts = 0 , NumBlocks = 0 ;
155
-
156
- // Look at the size of the callee. Each basic block counts as 20 units, and
157
- // each instruction counts as 10.
158
- for (Function::const_iterator BB = Callee->begin (), E = Callee->end ();
159
- BB != E; ++BB) {
160
- NumInsts += BB->size ();
161
- NumBlocks++;
162
- }
163
-
164
- CalleeFI.NumBlocks = NumBlocks;
165
- CalleeFI.NumInsts = NumInsts;
166
-
167
- // Check out all of the arguments to the function, figuring out how much
168
- // code can be eliminated if one of the arguments is a constant.
169
- std::vector<ArgInfo> &ArgWeights = CalleeFI.ArgumentWeights ;
170
-
171
- for (Function::aiterator I = Callee->abegin (), E = Callee->aend ();
172
- I != E; ++I)
173
- ArgWeights.push_back (ArgInfo (CountCodeReductionForConstant (I),
174
- CountCodeReductionForAlloca (I)));
175
- }
201
+ // If we haven't calculated this information yet, do so now.
202
+ if (CalleeFI.NumBlocks == 0 )
203
+ CalleeFI.analyzeFunction (Callee);
176
204
205
+ // Don't inline calls to functions with allocas that are not in the entry
206
+ // block of the function.
207
+ if (CalleeFI.HasAllocas )
208
+ return 2000000000 ;
177
209
178
210
// Add to the inline quality for properties that make the call valuable to
179
211
// inline. This includes factors that indicate that the result of inlining
0 commit comments