1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/CodeGen/MachineModuleInfo.h"
12 #include "llvm/Constants.h"
13 #include "llvm/Analysis/ValueTracking.h"
14 #include "llvm/CodeGen/MachineFunctionPass.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/Target/TargetInstrInfo.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Target/TargetOptions.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/GlobalVariable.h"
22 #include "llvm/Intrinsics.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/Module.h"
25 #include "llvm/Support/Dwarf.h"
26 #include "llvm/Support/ErrorHandling.h"
28 using namespace llvm::dwarf
;
30 // Handle the Pass registration stuff necessary to use TargetData's.
31 static RegisterPass
<MachineModuleInfo
>
32 X("machinemoduleinfo", "Module Information");
33 char MachineModuleInfo::ID
= 0;
35 //===----------------------------------------------------------------------===//
37 MachineModuleInfo::MachineModuleInfo()
45 , DbgInfoAvailable(false)
47 // Always emit some info, by default "no personality" info.
48 Personalities
.push_back(NULL
);
50 MachineModuleInfo::~MachineModuleInfo() {
54 /// doInitialization - Initialize the state for a new module.
56 bool MachineModuleInfo::doInitialization() {
60 /// doFinalization - Tear down the state after completion of a module.
62 bool MachineModuleInfo::doFinalization() {
66 /// BeginFunction - Begin gathering function meta information.
68 void MachineModuleInfo::BeginFunction(MachineFunction
*MF
) {
72 /// EndFunction - Discard function meta information.
74 void MachineModuleInfo::EndFunction() {
75 // Clean up frame info.
78 // Clean up exception info.
87 /// AnalyzeModule - Scan the module for global debug information.
89 void MachineModuleInfo::AnalyzeModule(Module
&M
) {
90 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
92 GlobalVariable
*GV
= M
.getGlobalVariable("llvm.used");
93 if (!GV
|| !GV
->hasInitializer()) return;
95 // Should be an array of 'i8*'.
96 ConstantArray
*InitList
= dyn_cast
<ConstantArray
>(GV
->getInitializer());
97 if (InitList
== 0) return;
99 for (unsigned i
= 0, e
= InitList
->getNumOperands(); i
!= e
; ++i
)
101 dyn_cast
<Function
>(InitList
->getOperand(i
)->stripPointerCasts()))
102 UsedFunctions
.insert(F
);
105 //===-EH-------------------------------------------------------------------===//
107 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
108 /// specified MachineBasicBlock.
109 LandingPadInfo
&MachineModuleInfo::getOrCreateLandingPadInfo
110 (MachineBasicBlock
*LandingPad
) {
111 unsigned N
= LandingPads
.size();
112 for (unsigned i
= 0; i
< N
; ++i
) {
113 LandingPadInfo
&LP
= LandingPads
[i
];
114 if (LP
.LandingPadBlock
== LandingPad
)
118 LandingPads
.push_back(LandingPadInfo(LandingPad
));
119 return LandingPads
[N
];
122 /// addInvoke - Provide the begin and end labels of an invoke style call and
123 /// associate it with a try landing pad block.
124 void MachineModuleInfo::addInvoke(MachineBasicBlock
*LandingPad
,
125 unsigned BeginLabel
, unsigned EndLabel
) {
126 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
127 LP
.BeginLabels
.push_back(BeginLabel
);
128 LP
.EndLabels
.push_back(EndLabel
);
131 /// addLandingPad - Provide the label of a try LandingPad block.
133 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock
*LandingPad
) {
134 unsigned LandingPadLabel
= NextLabelID();
135 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
136 LP
.LandingPadLabel
= LandingPadLabel
;
137 return LandingPadLabel
;
140 /// addPersonality - Provide the personality function for the exception
142 void MachineModuleInfo::addPersonality(MachineBasicBlock
*LandingPad
,
143 Function
*Personality
) {
144 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
145 LP
.Personality
= Personality
;
147 for (unsigned i
= 0; i
< Personalities
.size(); ++i
)
148 if (Personalities
[i
] == Personality
)
151 // If this is the first personality we're adding go
152 // ahead and add it at the beginning.
153 if (Personalities
[0] == NULL
)
154 Personalities
[0] = Personality
;
156 Personalities
.push_back(Personality
);
159 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
161 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock
*LandingPad
,
162 std::vector
<GlobalVariable
*> &TyInfo
) {
163 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
164 for (unsigned N
= TyInfo
.size(); N
; --N
)
165 LP
.TypeIds
.push_back(getTypeIDFor(TyInfo
[N
- 1]));
168 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
170 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock
*LandingPad
,
171 std::vector
<GlobalVariable
*> &TyInfo
) {
172 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
173 std::vector
<unsigned> IdsInFilter(TyInfo
.size());
174 for (unsigned I
= 0, E
= TyInfo
.size(); I
!= E
; ++I
)
175 IdsInFilter
[I
] = getTypeIDFor(TyInfo
[I
]);
176 LP
.TypeIds
.push_back(getFilterIDFor(IdsInFilter
));
179 /// addCleanup - Add a cleanup action for a landing pad.
181 void MachineModuleInfo::addCleanup(MachineBasicBlock
*LandingPad
) {
182 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
183 LP
.TypeIds
.push_back(0);
186 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
188 void MachineModuleInfo::TidyLandingPads() {
189 for (unsigned i
= 0; i
!= LandingPads
.size(); ) {
190 LandingPadInfo
&LandingPad
= LandingPads
[i
];
191 LandingPad
.LandingPadLabel
= MappedLabel(LandingPad
.LandingPadLabel
);
193 // Special case: we *should* emit LPs with null LP MBB. This indicates
195 if (!LandingPad
.LandingPadLabel
&& LandingPad
.LandingPadBlock
) {
196 LandingPads
.erase(LandingPads
.begin() + i
);
200 for (unsigned j
=0; j
!= LandingPads
[i
].BeginLabels
.size(); ) {
201 unsigned BeginLabel
= MappedLabel(LandingPad
.BeginLabels
[j
]);
202 unsigned EndLabel
= MappedLabel(LandingPad
.EndLabels
[j
]);
204 if (!BeginLabel
|| !EndLabel
) {
205 LandingPad
.BeginLabels
.erase(LandingPad
.BeginLabels
.begin() + j
);
206 LandingPad
.EndLabels
.erase(LandingPad
.EndLabels
.begin() + j
);
210 LandingPad
.BeginLabels
[j
] = BeginLabel
;
211 LandingPad
.EndLabels
[j
] = EndLabel
;
215 // Remove landing pads with no try-ranges.
216 if (LandingPads
[i
].BeginLabels
.empty()) {
217 LandingPads
.erase(LandingPads
.begin() + i
);
221 // If there is no landing pad, ensure that the list of typeids is empty.
222 // If the only typeid is a cleanup, this is the same as having no typeids.
223 if (!LandingPad
.LandingPadBlock
||
224 (LandingPad
.TypeIds
.size() == 1 && !LandingPad
.TypeIds
[0]))
225 LandingPad
.TypeIds
.clear();
231 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
233 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable
*TI
) {
234 for (unsigned i
= 0, N
= TypeInfos
.size(); i
!= N
; ++i
)
235 if (TypeInfos
[i
] == TI
) return i
+ 1;
237 TypeInfos
.push_back(TI
);
238 return TypeInfos
.size();
241 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
243 int MachineModuleInfo::getFilterIDFor(std::vector
<unsigned> &TyIds
) {
244 // If the new filter coincides with the tail of an existing filter, then
245 // re-use the existing filter. Folding filters more than this requires
246 // re-ordering filters and/or their elements - probably not worth it.
247 for (std::vector
<unsigned>::iterator I
= FilterEnds
.begin(),
248 E
= FilterEnds
.end(); I
!= E
; ++I
) {
249 unsigned i
= *I
, j
= TyIds
.size();
252 if (FilterIds
[--i
] != TyIds
[--j
])
256 // The new filter coincides with range [i, end) of the existing filter.
262 // Add the new filter.
263 int FilterID
= -(1 + FilterIds
.size());
264 FilterIds
.reserve(FilterIds
.size() + TyIds
.size() + 1);
265 for (unsigned I
= 0, N
= TyIds
.size(); I
!= N
; ++I
)
266 FilterIds
.push_back(TyIds
[I
]);
267 FilterEnds
.push_back(FilterIds
.size());
268 FilterIds
.push_back(0); // terminator
272 /// getPersonality - Return the personality function for the current function.
273 Function
*MachineModuleInfo::getPersonality() const {
274 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
276 return !LandingPads
.empty() ? LandingPads
[0].Personality
: NULL
;
279 /// getPersonalityIndex - Return unique index for current personality
280 /// function. NULL/first personality function should always get zero index.
281 unsigned MachineModuleInfo::getPersonalityIndex() const {
282 const Function
* Personality
= NULL
;
284 // Scan landing pads. If there is at least one non-NULL personality - use it.
285 for (unsigned i
= 0; i
!= LandingPads
.size(); ++i
)
286 if (LandingPads
[i
].Personality
) {
287 Personality
= LandingPads
[i
].Personality
;
291 for (unsigned i
= 0; i
< Personalities
.size(); ++i
) {
292 if (Personalities
[i
] == Personality
)
296 // This will happen if the current personality function is
297 // in the zero index.
301 //===----------------------------------------------------------------------===//
302 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
303 /// a info consumer to determine if the range of two labels is empty, by seeing
304 /// if the labels map to the same reduced label.
308 struct DebugLabelFolder
: public MachineFunctionPass
{
310 DebugLabelFolder() : MachineFunctionPass(&ID
) {}
312 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
313 AU
.setPreservesCFG();
314 AU
.addPreservedID(MachineLoopInfoID
);
315 AU
.addPreservedID(MachineDominatorsID
);
316 MachineFunctionPass::getAnalysisUsage(AU
);
319 virtual bool runOnMachineFunction(MachineFunction
&MF
);
320 virtual const char *getPassName() const { return "Label Folder"; }
323 char DebugLabelFolder::ID
= 0;
325 bool DebugLabelFolder::runOnMachineFunction(MachineFunction
&MF
) {
326 // Get machine module info.
327 MachineModuleInfo
*MMI
= getAnalysisIfAvailable
<MachineModuleInfo
>();
328 if (!MMI
) return false;
330 // Track if change is made.
331 bool MadeChange
= false;
332 // No prior label to begin.
333 unsigned PriorLabel
= 0;
335 // Iterate through basic blocks.
336 for (MachineFunction::iterator BB
= MF
.begin(), E
= MF
.end();
338 // Iterate through instructions.
339 for (MachineBasicBlock::iterator I
= BB
->begin(), E
= BB
->end(); I
!= E
; ) {
341 if (I
->isDebugLabel() && !MMI
->isDbgLabelUsed(I
->getOperand(0).getImm())){
342 // The label ID # is always operand #0, an immediate.
343 unsigned NextLabel
= I
->getOperand(0).getImm();
345 // If there was an immediate prior label.
347 // Remap the current label to prior label.
348 MMI
->RemapLabel(NextLabel
, PriorLabel
);
349 // Delete the current label.
351 // Indicate a change has been made.
355 // Start a new round.
356 PriorLabel
= NextLabel
;
359 // No consecutive labels.
370 FunctionPass
*createDebugLabelFoldingPass() { return new DebugLabelFolder(); }