blob: a23cc1a8771a5aa5691dcc95c052f98901cc1936 [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
Chris Lattner44894142010-03-14 17:53:23 +0000107/// getAddrLabelSymbol - Return the symbol to be used for the specified basic
108/// block when its address is taken. This cannot be its normal LBB label
109/// because the block may be accessed outside its containing function.
110MCSymbol *MachineModuleInfo::getAddrLabelSymbol(const BasicBlock *BB) {
111 assert(BB->hasAddressTaken() &&
112 "Shouldn't get label for block without address taken");
113 MCSymbol *&Entry = AddrLabelSymbols[const_cast<BasicBlock*>(BB)];
114 if (Entry) return Entry;
115 return Entry = Context.CreateTempSymbol();
116}
117
118
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119//===-EH-------------------------------------------------------------------===//
120
121/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
122/// specified MachineBasicBlock.
Bill Wendling4de8de52008-07-03 22:53:42 +0000123LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
124 (MachineBasicBlock *LandingPad) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 unsigned N = LandingPads.size();
126 for (unsigned i = 0; i < N; ++i) {
127 LandingPadInfo &LP = LandingPads[i];
128 if (LP.LandingPadBlock == LandingPad)
129 return LP;
130 }
Eric Christopher84603632009-08-26 21:27:09 +0000131
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 LandingPads.push_back(LandingPadInfo(LandingPad));
133 return LandingPads[N];
134}
135
136/// addInvoke - Provide the begin and end labels of an invoke style call and
137/// associate it with a try landing pad block.
138void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
Chris Lattnere3330172010-03-14 01:41:15 +0000139 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
141 LP.BeginLabels.push_back(BeginLabel);
142 LP.EndLabels.push_back(EndLabel);
143}
144
145/// addLandingPad - Provide the label of a try LandingPad block.
146///
Chris Lattner8574ddd2010-03-14 02:33:54 +0000147MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
Chris Lattner54e56f22010-03-14 08:36:50 +0000148 MCSymbol *LandingPadLabel = Context.CreateTempSymbol();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Eric Christopher84603632009-08-26 21:27:09 +0000150 LP.LandingPadLabel = LandingPadLabel;
Chris Lattner8574ddd2010-03-14 02:33:54 +0000151 return LandingPadLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152}
153
154/// addPersonality - Provide the personality function for the exception
155/// information.
156void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
157 Function *Personality) {
158 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
159 LP.Personality = Personality;
160
Bill Wendling4de8de52008-07-03 22:53:42 +0000161 for (unsigned i = 0; i < Personalities.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 if (Personalities[i] == Personality)
163 return;
Eric Christopher84603632009-08-26 21:27:09 +0000164
Eric Christopherc87d5ae2009-08-26 21:30:49 +0000165 // If this is the first personality we're adding go
166 // ahead and add it at the beginning.
167 if (Personalities[0] == NULL)
168 Personalities[0] = Personality;
169 else
170 Personalities.push_back(Personality);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171}
172
173/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
174///
175void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
176 std::vector<GlobalVariable *> &TyInfo) {
177 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
178 for (unsigned N = TyInfo.size(); N; --N)
179 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
180}
181
182/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
183///
184void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
185 std::vector<GlobalVariable *> &TyInfo) {
186 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling993b3dd2008-07-07 21:41:57 +0000187 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling4de8de52008-07-03 22:53:42 +0000188 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
190 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
191}
192
Duncan Sands923fdb12007-08-27 15:47:50 +0000193/// addCleanup - Add a cleanup action for a landing pad.
194///
195void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
196 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
197 LP.TypeIds.push_back(0);
198}
199
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
201/// pads.
202void MachineModuleInfo::TidyLandingPads() {
203 for (unsigned i = 0; i != LandingPads.size(); ) {
204 LandingPadInfo &LandingPad = LandingPads[i];
Chris Lattnere3330172010-03-14 01:41:15 +0000205 if (LandingPad.LandingPadLabel && !LandingPad.LandingPadLabel->isDefined())
Chris Lattner5e423432010-03-09 01:51:43 +0000206 LandingPad.LandingPadLabel = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands4ff179f2007-12-19 07:36:31 +0000209 // "nounwind" case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
211 LandingPads.erase(LandingPads.begin() + i);
212 continue;
213 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000214
Chris Lattnere3330172010-03-14 01:41:15 +0000215 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
216 MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
217 MCSymbol *EndLabel = LandingPad.EndLabels[j];
218 if (BeginLabel->isDefined() && EndLabel->isDefined()) continue;
219
220 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
221 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
222 --j, --e;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000224
225 // Remove landing pads with no try-ranges.
Dan Gohman301f4052008-01-29 13:02:09 +0000226 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands241a0c92007-09-05 11:27:52 +0000227 LandingPads.erase(LandingPads.begin() + i);
228 continue;
229 }
230
231 // If there is no landing pad, ensure that the list of typeids is empty.
232 // If the only typeid is a cleanup, this is the same as having no typeids.
233 if (!LandingPad.LandingPadBlock ||
234 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
235 LandingPad.TypeIds.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 ++i;
237 }
238}
239
Eric Christopher84603632009-08-26 21:27:09 +0000240/// getTypeIDFor - Return the type id for the specified typeinfo. This is
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241/// function wide.
242unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling4de8de52008-07-03 22:53:42 +0000243 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
244 if (TypeInfos[i] == TI) return i + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245
246 TypeInfos.push_back(TI);
247 return TypeInfos.size();
248}
249
250/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
251/// function wide.
Bill Wendling0e679b82008-06-27 01:27:56 +0000252int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 // If the new filter coincides with the tail of an existing filter, then
254 // re-use the existing filter. Folding filters more than this requires
255 // re-ordering filters and/or their elements - probably not worth it.
256 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
257 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling0e679b82008-06-27 01:27:56 +0000258 unsigned i = *I, j = TyIds.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259
260 while (i && j)
261 if (FilterIds[--i] != TyIds[--j])
262 goto try_next;
263
264 if (!j)
265 // The new filter coincides with range [i, end) of the existing filter.
266 return -(1 + i);
Bill Wendling0e679b82008-06-27 01:27:56 +0000267
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268try_next:;
269 }
270
271 // Add the new filter.
Bill Wendling0e679b82008-06-27 01:27:56 +0000272 int FilterID = -(1 + FilterIds.size());
273 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
274 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 FilterIds.push_back(TyIds[I]);
Bill Wendling0e679b82008-06-27 01:27:56 +0000276 FilterEnds.push_back(FilterIds.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 FilterIds.push_back(0); // terminator
278 return FilterID;
279}
280
281/// getPersonality - Return the personality function for the current function.
282Function *MachineModuleInfo::getPersonality() const {
283 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
284 // function
285 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
286}
287
288/// getPersonalityIndex - Return unique index for current personality
Eric Christophere92cc8d2009-08-26 21:44:57 +0000289/// function. NULL/first personality function should always get zero index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290unsigned MachineModuleInfo::getPersonalityIndex() const {
291 const Function* Personality = NULL;
Eric Christopher84603632009-08-26 21:27:09 +0000292
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling4de8de52008-07-03 22:53:42 +0000294 for (unsigned i = 0; i != LandingPads.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 if (LandingPads[i].Personality) {
296 Personality = LandingPads[i].Personality;
297 break;
298 }
Eric Christopher84603632009-08-26 21:27:09 +0000299
Bill Wendling4de8de52008-07-03 22:53:42 +0000300 for (unsigned i = 0; i < Personalities.size(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 if (Personalities[i] == Personality)
302 return i;
Bill Wendling4de8de52008-07-03 22:53:42 +0000303 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304
Eric Christophere92cc8d2009-08-26 21:44:57 +0000305 // This will happen if the current personality function is
306 // in the zero index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 return 0;
308}
309