blob: fdbda8f715fa6f1b6f2aef14c2fad2905eb30cca [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/CodeGen/MachineModuleInfo.h"
11
12#include "llvm/Constants.h"
Chris Lattnere3330172010-03-14 01:41:15 +000013#include "llvm/DerivedTypes.h"
14#include "llvm/GlobalVariable.h"
15#include "llvm/Intrinsics.h"
16#include "llvm/Instructions.h"
17#include "llvm/Module.h"
Evan Cheng833501d2008-06-30 07:31:25 +000018#include "llvm/Analysis/ValueTracking.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng1b42ac12008-09-22 22:21:38 +000021#include "llvm/CodeGen/Passes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetOptions.h"
Chris Lattnere3330172010-03-14 01:41:15 +000025#include "llvm/MC/MCSymbol.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Support/Dwarf.h"
Edwin Török675d5622009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028using namespace llvm;
29using namespace llvm::dwarf;
30
31// Handle the Pass registration stuff necessary to use TargetData's.
Dan Gohman089efff2008-05-13 00:00:25 +000032static RegisterPass<MachineModuleInfo>
Chris Lattnerf4853452010-03-13 20:55:24 +000033X("machinemoduleinfo", "Machine Module Information");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034char MachineModuleInfo::ID = 0;
35
Chris Lattner72ba6722009-09-15 22:44:26 +000036// Out of line virtual method.
37MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
38
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039//===----------------------------------------------------------------------===//
Eric Christopher84603632009-08-26 21:27:09 +000040
Chris Lattnerf4853452010-03-13 20:55:24 +000041MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI)
42: ImmutablePass(&ID), Context(MAI),
Chris Lattner54e56f22010-03-14 08:36:50 +000043 ObjFileMMI(0),
Chris Lattner7e655952010-03-14 02:24:55 +000044 CurCallSite(0), CallsEHReturn(0), CallsUnwindInit(0), DbgInfoAvailable(false){
Eric Christopherc87d5ae2009-08-26 21:30:49 +000045 // Always emit some info, by default "no personality" info.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 Personalities.push_back(NULL);
47}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048
Chris Lattnerf4853452010-03-13 20:55:24 +000049MachineModuleInfo::MachineModuleInfo()
50: ImmutablePass(&ID), Context(*(MCAsmInfo*)0) {
51 assert(0 && "This MachineModuleInfo constructor should never be called, MMI "
52 "should always be explicitly constructed by LLVMTargetMachine");
53 abort();
54}
55
Chris Lattner72ba6722009-09-15 22:44:26 +000056MachineModuleInfo::~MachineModuleInfo() {
Chris Lattner16fcdf92009-09-16 05:26:00 +000057 delete ObjFileMMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058}
59
60/// doInitialization - Initialize the state for a new module.
61///
62bool MachineModuleInfo::doInitialization() {
63 return false;
64}
65
66/// doFinalization - Tear down the state after completion of a module.
67///
68bool MachineModuleInfo::doFinalization() {
69 return false;
70}
71
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072/// EndFunction - Discard function meta information.
73///
74void MachineModuleInfo::EndFunction() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 // Clean up frame info.
76 FrameMoves.clear();
Eric Christopher84603632009-08-26 21:27:09 +000077
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 // Clean up exception info.
79 LandingPads.clear();
Jim Grosbach1db5d982010-01-28 01:45:32 +000080 CallSiteMap.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 TypeInfos.clear();
82 FilterIds.clear();
83 FilterEnds.clear();
84 CallsEHReturn = 0;
85 CallsUnwindInit = 0;
Devang Patelf57e1062009-10-08 20:41:17 +000086 VariableDbgInfo.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087}
88
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089/// AnalyzeModule - Scan the module for global debug information.
90///
91void MachineModuleInfo::AnalyzeModule(Module &M) {
Chris Lattner1e0e0d12009-07-20 06:14:25 +000092 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
93 // UsedFunctions.
Dale Johannesen3dadeb32008-01-16 19:59:28 +000094 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
95 if (!GV || !GV->hasInitializer()) return;
96
97 // Should be an array of 'i8*'.
98 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
99 if (InitList == 0) return;
100
Chris Lattner26165602009-07-20 06:05:50 +0000101 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
102 if (Function *F =
103 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
104 UsedFunctions.insert(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105}
106
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107//===-EH-------------------------------------------------------------------===//
108
109/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
110/// specified MachineBasicBlock.
Bill Wendling4de8de52008-07-03 22:53:42 +0000111LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
112 (MachineBasicBlock *LandingPad) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 unsigned N = LandingPads.size();
114 for (unsigned i = 0; i < N; ++i) {
115 LandingPadInfo &LP = LandingPads[i];
116 if (LP.LandingPadBlock == LandingPad)
117 return LP;
118 }
Eric Christopher84603632009-08-26 21:27:09 +0000119
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 LandingPads.push_back(LandingPadInfo(LandingPad));
121 return LandingPads[N];
122}
123
124/// addInvoke - Provide the begin and end labels of an invoke style call and
125/// associate it with a try landing pad block.
126void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
Chris Lattnere3330172010-03-14 01:41:15 +0000127 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
129 LP.BeginLabels.push_back(BeginLabel);
130 LP.EndLabels.push_back(EndLabel);
131}
132
133/// addLandingPad - Provide the label of a try LandingPad block.
134///
Chris Lattner8574ddd2010-03-14 02:33:54 +0000135MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
Chris Lattner54e56f22010-03-14 08:36:50 +0000136 MCSymbol *LandingPadLabel = Context.CreateTempSymbol();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Eric Christopher84603632009-08-26 21:27:09 +0000138 LP.LandingPadLabel = LandingPadLabel;
Chris Lattner8574ddd2010-03-14 02:33:54 +0000139 return LandingPadLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140}
141
142/// addPersonality - Provide the personality function for the exception
143/// information.
144void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
145 Function *Personality) {
146 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
147 LP.Personality = Personality;
148
Bill Wendling4de8de52008-07-03 22:53:42 +0000149 for (unsigned i = 0; i < Personalities.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 if (Personalities[i] == Personality)
151 return;
Eric Christopher84603632009-08-26 21:27:09 +0000152
Eric Christopherc87d5ae2009-08-26 21:30:49 +0000153 // If this is the first personality we're adding go
154 // ahead and add it at the beginning.
155 if (Personalities[0] == NULL)
156 Personalities[0] = Personality;
157 else
158 Personalities.push_back(Personality);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159}
160
161/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
162///
163void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
164 std::vector<GlobalVariable *> &TyInfo) {
165 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
166 for (unsigned N = TyInfo.size(); N; --N)
167 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
168}
169
170/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
171///
172void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
173 std::vector<GlobalVariable *> &TyInfo) {
174 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling993b3dd2008-07-07 21:41:57 +0000175 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling4de8de52008-07-03 22:53:42 +0000176 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
178 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
179}
180
Duncan Sands923fdb12007-08-27 15:47:50 +0000181/// addCleanup - Add a cleanup action for a landing pad.
182///
183void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
184 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
185 LP.TypeIds.push_back(0);
186}
187
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
189/// pads.
190void MachineModuleInfo::TidyLandingPads() {
191 for (unsigned i = 0; i != LandingPads.size(); ) {
192 LandingPadInfo &LandingPad = LandingPads[i];
Chris Lattnere3330172010-03-14 01:41:15 +0000193 if (LandingPad.LandingPadLabel && !LandingPad.LandingPadLabel->isDefined())
Chris Lattner5e423432010-03-09 01:51:43 +0000194 LandingPad.LandingPadLabel = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands4ff179f2007-12-19 07:36:31 +0000197 // "nounwind" case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
199 LandingPads.erase(LandingPads.begin() + i);
200 continue;
201 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000202
Chris Lattnere3330172010-03-14 01:41:15 +0000203 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
204 MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
205 MCSymbol *EndLabel = LandingPad.EndLabels[j];
206 if (BeginLabel->isDefined() && EndLabel->isDefined()) continue;
207
208 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
209 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
210 --j, --e;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000212
213 // Remove landing pads with no try-ranges.
Dan Gohman301f4052008-01-29 13:02:09 +0000214 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands241a0c92007-09-05 11:27:52 +0000215 LandingPads.erase(LandingPads.begin() + i);
216 continue;
217 }
218
219 // If there is no landing pad, ensure that the list of typeids is empty.
220 // If the only typeid is a cleanup, this is the same as having no typeids.
221 if (!LandingPad.LandingPadBlock ||
222 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
223 LandingPad.TypeIds.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 ++i;
225 }
226}
227
Eric Christopher84603632009-08-26 21:27:09 +0000228/// getTypeIDFor - Return the type id for the specified typeinfo. This is
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229/// function wide.
230unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling4de8de52008-07-03 22:53:42 +0000231 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
232 if (TypeInfos[i] == TI) return i + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233
234 TypeInfos.push_back(TI);
235 return TypeInfos.size();
236}
237
238/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
239/// function wide.
Bill Wendling0e679b82008-06-27 01:27:56 +0000240int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 // If the new filter coincides with the tail of an existing filter, then
242 // re-use the existing filter. Folding filters more than this requires
243 // re-ordering filters and/or their elements - probably not worth it.
244 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
245 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling0e679b82008-06-27 01:27:56 +0000246 unsigned i = *I, j = TyIds.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247
248 while (i && j)
249 if (FilterIds[--i] != TyIds[--j])
250 goto try_next;
251
252 if (!j)
253 // The new filter coincides with range [i, end) of the existing filter.
254 return -(1 + i);
Bill Wendling0e679b82008-06-27 01:27:56 +0000255
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256try_next:;
257 }
258
259 // Add the new filter.
Bill Wendling0e679b82008-06-27 01:27:56 +0000260 int FilterID = -(1 + FilterIds.size());
261 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
262 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 FilterIds.push_back(TyIds[I]);
Bill Wendling0e679b82008-06-27 01:27:56 +0000264 FilterEnds.push_back(FilterIds.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 FilterIds.push_back(0); // terminator
266 return FilterID;
267}
268
269/// getPersonality - Return the personality function for the current function.
270Function *MachineModuleInfo::getPersonality() const {
271 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
272 // function
273 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
274}
275
276/// getPersonalityIndex - Return unique index for current personality
Eric Christophere92cc8d2009-08-26 21:44:57 +0000277/// function. NULL/first personality function should always get zero index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278unsigned MachineModuleInfo::getPersonalityIndex() const {
279 const Function* Personality = NULL;
Eric Christopher84603632009-08-26 21:27:09 +0000280
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling4de8de52008-07-03 22:53:42 +0000282 for (unsigned i = 0; i != LandingPads.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 if (LandingPads[i].Personality) {
284 Personality = LandingPads[i].Personality;
285 break;
286 }
Eric Christopher84603632009-08-26 21:27:09 +0000287
Bill Wendling4de8de52008-07-03 22:53:42 +0000288 for (unsigned i = 0; i < Personalities.size(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 if (Personalities[i] == Personality)
290 return i;
Bill Wendling4de8de52008-07-03 22:53:42 +0000291 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292
Eric Christophere92cc8d2009-08-26 21:44:57 +0000293 // This will happen if the current personality function is
294 // in the zero index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 return 0;
296}
297