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 // Out of line virtual method.
36 MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
38 //===----------------------------------------------------------------------===//
40 MachineModuleInfo::MachineModuleInfo()
45 , DbgInfoAvailable(false) {
46 // Always emit some info, by default "no personality" info.
47 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 /// EndFunction - Discard function meta information.
68 void MachineModuleInfo::EndFunction() {
69 // Clean up frame info.
72 // Clean up exception info.
81 /// AnalyzeModule - Scan the module for global debug information.
83 void MachineModuleInfo::AnalyzeModule(Module &M) {
84 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
86 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
87 if (!GV || !GV->hasInitializer()) return;
89 // Should be an array of 'i8*'.
90 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
91 if (InitList == 0) return;
93 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
95 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
96 UsedFunctions.insert(F);
99 //===-EH-------------------------------------------------------------------===//
101 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
102 /// specified MachineBasicBlock.
103 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
104 (MachineBasicBlock *LandingPad) {
105 unsigned N = LandingPads.size();
106 for (unsigned i = 0; i < N; ++i) {
107 LandingPadInfo &LP = LandingPads[i];
108 if (LP.LandingPadBlock == LandingPad)
112 LandingPads.push_back(LandingPadInfo(LandingPad));
113 return LandingPads[N];
116 /// addInvoke - Provide the begin and end labels of an invoke style call and
117 /// associate it with a try landing pad block.
118 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
119 unsigned BeginLabel, unsigned EndLabel) {
120 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
121 LP.BeginLabels.push_back(BeginLabel);
122 LP.EndLabels.push_back(EndLabel);
125 /// addLandingPad - Provide the label of a try LandingPad block.
127 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
128 unsigned LandingPadLabel = NextLabelID();
129 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
130 LP.LandingPadLabel = LandingPadLabel;
131 return LandingPadLabel;
134 /// addPersonality - Provide the personality function for the exception
136 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
137 Function *Personality) {
138 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
139 LP.Personality = Personality;
141 for (unsigned i = 0; i < Personalities.size(); ++i)
142 if (Personalities[i] == Personality)
145 // If this is the first personality we're adding go
146 // ahead and add it at the beginning.
147 if (Personalities[0] == NULL)
148 Personalities[0] = Personality;
150 Personalities.push_back(Personality);
153 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
155 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
156 std::vector<GlobalVariable *> &TyInfo) {
157 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
158 for (unsigned N = TyInfo.size(); N; --N)
159 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
162 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
164 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
165 std::vector<GlobalVariable *> &TyInfo) {
166 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
167 std::vector<unsigned> IdsInFilter(TyInfo.size());
168 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
169 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
170 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
173 /// addCleanup - Add a cleanup action for a landing pad.
175 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
176 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
177 LP.TypeIds.push_back(0);
180 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
182 void MachineModuleInfo::TidyLandingPads() {
183 for (unsigned i = 0; i != LandingPads.size(); ) {
184 LandingPadInfo &LandingPad = LandingPads[i];
185 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
187 // Special case: we *should* emit LPs with null LP MBB. This indicates
189 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
190 LandingPads.erase(LandingPads.begin() + i);
194 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
195 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
196 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
198 if (!BeginLabel || !EndLabel) {
199 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
200 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
204 LandingPad.BeginLabels[j] = BeginLabel;
205 LandingPad.EndLabels[j] = EndLabel;
209 // Remove landing pads with no try-ranges.
210 if (LandingPads[i].BeginLabels.empty()) {
211 LandingPads.erase(LandingPads.begin() + i);
215 // If there is no landing pad, ensure that the list of typeids is empty.
216 // If the only typeid is a cleanup, this is the same as having no typeids.
217 if (!LandingPad.LandingPadBlock ||
218 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
219 LandingPad.TypeIds.clear();
225 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
227 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
228 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
229 if (TypeInfos[i] == TI) return i + 1;
231 TypeInfos.push_back(TI);
232 return TypeInfos.size();
235 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
237 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
238 // If the new filter coincides with the tail of an existing filter, then
239 // re-use the existing filter. Folding filters more than this requires
240 // re-ordering filters and/or their elements - probably not worth it.
241 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
242 E = FilterEnds.end(); I != E; ++I) {
243 unsigned i = *I, j = TyIds.size();
246 if (FilterIds[--i] != TyIds[--j])
250 // The new filter coincides with range [i, end) of the existing filter.
256 // Add the new filter.
257 int FilterID = -(1 + FilterIds.size());
258 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
259 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
260 FilterIds.push_back(TyIds[I]);
261 FilterEnds.push_back(FilterIds.size());
262 FilterIds.push_back(0); // terminator
266 /// getPersonality - Return the personality function for the current function.
267 Function *MachineModuleInfo::getPersonality() const {
268 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
270 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
273 /// getPersonalityIndex - Return unique index for current personality
274 /// function. NULL/first personality function should always get zero index.
275 unsigned MachineModuleInfo::getPersonalityIndex() const {
276 const Function* Personality = NULL;
278 // Scan landing pads. If there is at least one non-NULL personality - use it.
279 for (unsigned i = 0; i != LandingPads.size(); ++i)
280 if (LandingPads[i].Personality) {
281 Personality = LandingPads[i].Personality;
285 for (unsigned i = 0; i < Personalities.size(); ++i) {
286 if (Personalities[i] == Personality)
290 // This will happen if the current personality function is
291 // in the zero index.
295 //===----------------------------------------------------------------------===//
296 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
297 /// a info consumer to determine if the range of two labels is empty, by seeing
298 /// if the labels map to the same reduced label.
302 struct DebugLabelFolder : public MachineFunctionPass {
304 DebugLabelFolder() : MachineFunctionPass(&ID) {}
306 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
307 AU.setPreservesCFG();
308 AU.addPreservedID(MachineLoopInfoID);
309 AU.addPreservedID(MachineDominatorsID);
310 MachineFunctionPass::getAnalysisUsage(AU);
313 virtual bool runOnMachineFunction(MachineFunction &MF);
314 virtual const char *getPassName() const { return "Label Folder"; }
317 char DebugLabelFolder::ID = 0;
319 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
320 // Get machine module info.
321 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
322 if (!MMI) return false;
324 // Track if change is made.
325 bool MadeChange = false;
326 // No prior label to begin.
327 unsigned PriorLabel = 0;
329 // Iterate through basic blocks.
330 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
332 // Iterate through instructions.
333 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
335 if (I->isDebugLabel() && !MMI->isDbgLabelUsed(I->getOperand(0).getImm())){
336 // The label ID # is always operand #0, an immediate.
337 unsigned NextLabel = I->getOperand(0).getImm();
339 // If there was an immediate prior label.
341 // Remap the current label to prior label.
342 MMI->RemapLabel(NextLabel, PriorLabel);
343 // Delete the current label.
345 // Indicate a change has been made.
349 // Start a new round.
350 PriorLabel = NextLabel;
353 // No consecutive labels.
364 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }