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/DerivedTypes.h"
14 #include "llvm/GlobalVariable.h"
15 #include "llvm/Intrinsics.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/Module.h"
18 #include "llvm/Analysis/ValueTracking.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/Support/Dwarf.h"
27 #include "llvm/Support/ErrorHandling.h"
29 using namespace llvm::dwarf;
31 // Handle the Pass registration stuff necessary to use TargetData's.
32 static RegisterPass<MachineModuleInfo>
33 X("machinemoduleinfo", "Machine Module Information");
34 char MachineModuleInfo::ID = 0;
36 // Out of line virtual method.
37 MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
40 class MMIAddrLabelMapCallbackPtr : CallbackVH {
43 MMIAddrLabelMapCallbackPtr() : Map(0) {}
44 MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V), Map(0) {}
46 void setMap(MMIAddrLabelMap *map) { Map = map; }
48 virtual void deleted();
49 virtual void allUsesReplacedWith(Value *V2);
52 class MMIAddrLabelMap {
54 struct AddrLabelSymEntry {
59 DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
61 std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
64 MMIAddrLabelMap(MCContext &context) : Context(context) {}
66 MCSymbol *getAddrLabelSymbol(BasicBlock *BB);
67 void UpdateForDeletedBlock(BasicBlock *BB);
68 void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
72 MCSymbol *MMIAddrLabelMap::getAddrLabelSymbol(BasicBlock *BB) {
73 assert(BB->hasAddressTaken() &&
74 "Shouldn't get label for block without address taken");
75 AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
77 // If we already had an entry for this block, just return it.
78 if (Entry.Sym) return Entry.Sym;
80 // Otherwise, this is a new entry, create a new symbol for it and add an
81 // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
82 BBCallbacks.push_back(BB);
83 BBCallbacks.back().setMap(this);
84 Entry.Index = BBCallbacks.size()-1;
85 return Entry.Sym = Context.CreateTempSymbol();
88 void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
89 // If the block got deleted, there is no need for the symbol. If the symbol
90 // was already emitted, we can just forget about it, otherwise we need to
91 // queue it up for later emission when the function is output.
92 AddrLabelSymEntry Entry = AddrLabelSymbols[BB];
93 AddrLabelSymbols.erase(BB);
94 assert(Entry.Sym && "Didn't have a symbol, why a callback?");
95 BBCallbacks[Entry.Index] = 0; // Clear the callback.
97 if (Entry.Sym->isDefined())
100 // If the block is not yet defined, we need to emit it at the end of the
102 assert(0 && "Case not handled yet!");
106 void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
107 // Get the entry for the RAUW'd block and remove it from our map.
108 AddrLabelSymEntry OldEntry = AddrLabelSymbols[Old];
109 AddrLabelSymbols.erase(Old);
110 assert(OldEntry.Sym && "Didn't have a symbol, why a callback?");
112 // If New is not address taken, just move our symbol over to it.
113 if (!AddrLabelSymbols.count(New)) {
114 BBCallbacks[OldEntry.Index] = New; // Update the callback.
115 AddrLabelSymbols[New] = OldEntry; // Set New's entry.
117 assert(0 && "Case not handled yet!");
123 void MMIAddrLabelMapCallbackPtr::deleted() {
124 Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
127 void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
128 Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
132 //===----------------------------------------------------------------------===//
134 MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI)
135 : ImmutablePass(&ID), Context(MAI),
137 CurCallSite(0), CallsEHReturn(0), CallsUnwindInit(0), DbgInfoAvailable(false){
138 // Always emit some info, by default "no personality" info.
139 Personalities.push_back(NULL);
140 AddrLabelSymbols = 0;
143 MachineModuleInfo::MachineModuleInfo()
144 : ImmutablePass(&ID), Context(*(MCAsmInfo*)0) {
145 assert(0 && "This MachineModuleInfo constructor should never be called, MMI "
146 "should always be explicitly constructed by LLVMTargetMachine");
150 MachineModuleInfo::~MachineModuleInfo() {
153 // FIXME: Why isn't doFinalization being called??
154 //assert(AddrLabelSymbols == 0 && "doFinalization not called");
155 delete AddrLabelSymbols;
156 AddrLabelSymbols = 0;
159 /// doInitialization - Initialize the state for a new module.
161 bool MachineModuleInfo::doInitialization() {
162 assert(AddrLabelSymbols == 0 && "Improperly initialized");
166 /// doFinalization - Tear down the state after completion of a module.
168 bool MachineModuleInfo::doFinalization() {
169 delete AddrLabelSymbols;
170 AddrLabelSymbols = 0;
174 /// EndFunction - Discard function meta information.
176 void MachineModuleInfo::EndFunction() {
177 // Clean up frame info.
180 // Clean up exception info.
188 VariableDbgInfo.clear();
191 /// AnalyzeModule - Scan the module for global debug information.
193 void MachineModuleInfo::AnalyzeModule(Module &M) {
194 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
196 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
197 if (!GV || !GV->hasInitializer()) return;
199 // Should be an array of 'i8*'.
200 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
201 if (InitList == 0) return;
203 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
205 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
206 UsedFunctions.insert(F);
209 //===- Address of Block Management ----------------------------------------===//
212 /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
213 /// block when its address is taken. This cannot be its normal LBB label
214 /// because the block may be accessed outside its containing function.
215 MCSymbol *MachineModuleInfo::getAddrLabelSymbol(const BasicBlock *BB) {
216 // Lazily create AddrLabelSymbols.
217 if (AddrLabelSymbols == 0)
218 AddrLabelSymbols = new MMIAddrLabelMap(Context);
219 return AddrLabelSymbols->getAddrLabelSymbol(const_cast<BasicBlock*>(BB));
223 //===- EH -----------------------------------------------------------------===//
225 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
226 /// specified MachineBasicBlock.
227 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
228 (MachineBasicBlock *LandingPad) {
229 unsigned N = LandingPads.size();
230 for (unsigned i = 0; i < N; ++i) {
231 LandingPadInfo &LP = LandingPads[i];
232 if (LP.LandingPadBlock == LandingPad)
236 LandingPads.push_back(LandingPadInfo(LandingPad));
237 return LandingPads[N];
240 /// addInvoke - Provide the begin and end labels of an invoke style call and
241 /// associate it with a try landing pad block.
242 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
243 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
244 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
245 LP.BeginLabels.push_back(BeginLabel);
246 LP.EndLabels.push_back(EndLabel);
249 /// addLandingPad - Provide the label of a try LandingPad block.
251 MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
252 MCSymbol *LandingPadLabel = Context.CreateTempSymbol();
253 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
254 LP.LandingPadLabel = LandingPadLabel;
255 return LandingPadLabel;
258 /// addPersonality - Provide the personality function for the exception
260 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
261 Function *Personality) {
262 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
263 LP.Personality = Personality;
265 for (unsigned i = 0; i < Personalities.size(); ++i)
266 if (Personalities[i] == Personality)
269 // If this is the first personality we're adding go
270 // ahead and add it at the beginning.
271 if (Personalities[0] == NULL)
272 Personalities[0] = Personality;
274 Personalities.push_back(Personality);
277 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
279 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
280 std::vector<GlobalVariable *> &TyInfo) {
281 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
282 for (unsigned N = TyInfo.size(); N; --N)
283 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
286 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
288 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
289 std::vector<GlobalVariable *> &TyInfo) {
290 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
291 std::vector<unsigned> IdsInFilter(TyInfo.size());
292 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
293 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
294 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
297 /// addCleanup - Add a cleanup action for a landing pad.
299 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
300 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
301 LP.TypeIds.push_back(0);
304 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
306 void MachineModuleInfo::TidyLandingPads() {
307 for (unsigned i = 0; i != LandingPads.size(); ) {
308 LandingPadInfo &LandingPad = LandingPads[i];
309 if (LandingPad.LandingPadLabel && !LandingPad.LandingPadLabel->isDefined())
310 LandingPad.LandingPadLabel = 0;
312 // Special case: we *should* emit LPs with null LP MBB. This indicates
314 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
315 LandingPads.erase(LandingPads.begin() + i);
319 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
320 MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
321 MCSymbol *EndLabel = LandingPad.EndLabels[j];
322 if (BeginLabel->isDefined() && EndLabel->isDefined()) continue;
324 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
325 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
329 // Remove landing pads with no try-ranges.
330 if (LandingPads[i].BeginLabels.empty()) {
331 LandingPads.erase(LandingPads.begin() + i);
335 // If there is no landing pad, ensure that the list of typeids is empty.
336 // If the only typeid is a cleanup, this is the same as having no typeids.
337 if (!LandingPad.LandingPadBlock ||
338 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
339 LandingPad.TypeIds.clear();
344 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
346 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
347 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
348 if (TypeInfos[i] == TI) return i + 1;
350 TypeInfos.push_back(TI);
351 return TypeInfos.size();
354 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
356 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
357 // If the new filter coincides with the tail of an existing filter, then
358 // re-use the existing filter. Folding filters more than this requires
359 // re-ordering filters and/or their elements - probably not worth it.
360 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
361 E = FilterEnds.end(); I != E; ++I) {
362 unsigned i = *I, j = TyIds.size();
365 if (FilterIds[--i] != TyIds[--j])
369 // The new filter coincides with range [i, end) of the existing filter.
375 // Add the new filter.
376 int FilterID = -(1 + FilterIds.size());
377 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
378 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
379 FilterIds.push_back(TyIds[I]);
380 FilterEnds.push_back(FilterIds.size());
381 FilterIds.push_back(0); // terminator
385 /// getPersonality - Return the personality function for the current function.
386 Function *MachineModuleInfo::getPersonality() const {
387 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
389 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
392 /// getPersonalityIndex - Return unique index for current personality
393 /// function. NULL/first personality function should always get zero index.
394 unsigned MachineModuleInfo::getPersonalityIndex() const {
395 const Function* Personality = NULL;
397 // Scan landing pads. If there is at least one non-NULL personality - use it.
398 for (unsigned i = 0; i != LandingPads.size(); ++i)
399 if (LandingPads[i].Personality) {
400 Personality = LandingPads[i].Personality;
404 for (unsigned i = 0; i < Personalities.size(); ++i) {
405 if (Personalities[i] == Personality)
409 // This will happen if the current personality function is
410 // in the zero index.