Skip to content

Commit 322e131

Browse files
author
John Criswell
committed
Merged from mainline. This should fix 176.gcc.
llvm-svn: 15685
1 parent ab686a5 commit 322e131

File tree

1 file changed

+57
-25
lines changed

1 file changed

+57
-25
lines changed

llvm/lib/Transforms/IPO/InlineSimple.cpp

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ namespace {
3131
// FunctionInfo - For each function, calculate the size of it in blocks and
3232
// instructions.
3333
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+
3444
// NumInsts, NumBlocks - Keep track of how large each function is, which is
3545
// used to estimate the code size cost of inlining it.
3646
unsigned NumInsts, NumBlocks;
@@ -41,7 +51,11 @@ namespace {
4151
// entry here.
4252
std::vector<ArgInfo> ArgumentWeights;
4353

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);
4559
};
4660

4761
class SimpleInliner : public Inliner {
@@ -123,6 +137,41 @@ static unsigned CountCodeReductionForAlloca(Value *V) {
123137
return Reduction;
124138
}
125139

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+
126175
// getInlineCost - The heuristic used to determine if we should inline the
127176
// function call or not.
128177
//
@@ -149,31 +198,14 @@ int SimpleInliner::getInlineCost(CallSite CS) {
149198
// Get information about the callee...
150199
FunctionInfo &CalleeFI = CachedFunctionInfo[Callee];
151200

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);
176204

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;
177209

178210
// Add to the inline quality for properties that make the call valuable to
179211
// inline. This includes factors that indicate that the result of inlining

0 commit comments

Comments
 (0)