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", "Machine Module Information");
33 char MachineModuleInfo::ID = 0;
35 // Out of line virtual method.
36 MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
38 //===----------------------------------------------------------------------===//
40 MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI)
41 : ImmutablePass(&ID), Context(MAI),
42 ObjFileMMI(0), CurCallSite(0), CallsEHReturn(0), CallsUnwindInit(0),
43 DbgInfoAvailable(false) {
44 // Always emit some info, by default "no personality" info.
45 Personalities.push_back(NULL);
48 MachineModuleInfo::MachineModuleInfo()
49 : ImmutablePass(&ID), Context(*(MCAsmInfo*)0) {
50 assert(0 && "This MachineModuleInfo constructor should never be called, MMI "
51 "should always be explicitly constructed by LLVMTargetMachine");
55 MachineModuleInfo::~MachineModuleInfo() {
59 /// doInitialization - Initialize the state for a new module.
61 bool MachineModuleInfo::doInitialization() {
65 /// doFinalization - Tear down the state after completion of a module.
67 bool MachineModuleInfo::doFinalization() {
71 /// EndFunction - Discard function meta information.
73 void MachineModuleInfo::EndFunction() {
74 // Clean up frame info.
77 // Clean up exception info.
85 VariableDbgInfo.clear();
88 /// AnalyzeModule - Scan the module for global debug information.
90 void MachineModuleInfo::AnalyzeModule(Module &M) {
91 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
93 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
94 if (!GV || !GV->hasInitializer()) return;
96 // Should be an array of 'i8*'.
97 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
98 if (InitList == 0) return;
100 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
102 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
103 UsedFunctions.insert(F);
106 //===-EH-------------------------------------------------------------------===//
108 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
109 /// specified MachineBasicBlock.
110 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
111 (MachineBasicBlock *LandingPad) {
112 unsigned N = LandingPads.size();
113 for (unsigned i = 0; i < N; ++i) {
114 LandingPadInfo &LP = LandingPads[i];
115 if (LP.LandingPadBlock == LandingPad)
119 LandingPads.push_back(LandingPadInfo(LandingPad));
120 return LandingPads[N];
123 /// addInvoke - Provide the begin and end labels of an invoke style call and
124 /// associate it with a try landing pad block.
125 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
126 unsigned BeginLabel, unsigned EndLabel) {
127 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
128 LP.BeginLabels.push_back(BeginLabel);
129 LP.EndLabels.push_back(EndLabel);
132 /// addLandingPad - Provide the label of a try LandingPad block.
134 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
135 unsigned LandingPadLabel = NextLabelID();
136 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
137 LP.LandingPadLabel = LandingPadLabel;
138 return LandingPadLabel;
141 /// addPersonality - Provide the personality function for the exception
143 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
144 Function *Personality) {
145 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
146 LP.Personality = Personality;
148 for (unsigned i = 0; i < Personalities.size(); ++i)
149 if (Personalities[i] == Personality)
152 // If this is the first personality we're adding go
153 // ahead and add it at the beginning.
154 if (Personalities[0] == NULL)
155 Personalities[0] = Personality;
157 Personalities.push_back(Personality);
160 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
162 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
163 std::vector<GlobalVariable *> &TyInfo) {
164 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
165 for (unsigned N = TyInfo.size(); N; --N)
166 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
169 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
171 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
172 std::vector<GlobalVariable *> &TyInfo) {
173 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
174 std::vector<unsigned> IdsInFilter(TyInfo.size());
175 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
176 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
177 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
180 /// addCleanup - Add a cleanup action for a landing pad.
182 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
183 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
184 LP.TypeIds.push_back(0);
187 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
189 void MachineModuleInfo::TidyLandingPads() {
190 for (unsigned i = 0; i != LandingPads.size(); ) {
191 LandingPadInfo &LandingPad = LandingPads[i];
192 if (isLabelDeleted(LandingPad.LandingPadLabel))
193 LandingPad.LandingPadLabel = 0;
195 // Special case: we *should* emit LPs with null LP MBB. This indicates
197 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
198 LandingPads.erase(LandingPads.begin() + i);
202 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
203 unsigned BeginLabel = LandingPad.BeginLabels[j];
204 unsigned EndLabel = LandingPad.EndLabels[j];
205 if (isLabelDeleted(BeginLabel) || isLabelDeleted(EndLabel)) {
206 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
207 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
214 // Remove landing pads with no try-ranges.
215 if (LandingPads[i].BeginLabels.empty()) {
216 LandingPads.erase(LandingPads.begin() + i);
220 // If there is no landing pad, ensure that the list of typeids is empty.
221 // If the only typeid is a cleanup, this is the same as having no typeids.
222 if (!LandingPad.LandingPadBlock ||
223 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
224 LandingPad.TypeIds.clear();
230 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
232 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
233 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
234 if (TypeInfos[i] == TI) return i + 1;
236 TypeInfos.push_back(TI);
237 return TypeInfos.size();
240 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
242 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
243 // If the new filter coincides with the tail of an existing filter, then
244 // re-use the existing filter. Folding filters more than this requires
245 // re-ordering filters and/or their elements - probably not worth it.
246 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
247 E = FilterEnds.end(); I != E; ++I) {
248 unsigned i = *I, j = TyIds.size();
251 if (FilterIds[--i] != TyIds[--j])
255 // The new filter coincides with range [i, end) of the existing filter.
261 // Add the new filter.
262 int FilterID = -(1 + FilterIds.size());
263 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
264 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
265 FilterIds.push_back(TyIds[I]);
266 FilterEnds.push_back(FilterIds.size());
267 FilterIds.push_back(0); // terminator
271 /// getPersonality - Return the personality function for the current function.
272 Function *MachineModuleInfo::getPersonality() const {
273 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
275 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
278 /// getPersonalityIndex - Return unique index for current personality
279 /// function. NULL/first personality function should always get zero index.
280 unsigned MachineModuleInfo::getPersonalityIndex() const {
281 const Function* Personality = NULL;
283 // Scan landing pads. If there is at least one non-NULL personality - use it.
284 for (unsigned i = 0; i != LandingPads.size(); ++i)
285 if (LandingPads[i].Personality) {
286 Personality = LandingPads[i].Personality;
290 for (unsigned i = 0; i < Personalities.size(); ++i) {
291 if (Personalities[i] == Personality)
295 // This will happen if the current personality function is
296 // in the zero index.