blob: e326ecd75663f7f4e988410be516ded1087caeae [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"
Evan Cheng833501d2008-06-30 07:31:25 +000013#include "llvm/Analysis/ValueTracking.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000014#include "llvm/CodeGen/MachineFunctionPass.h"
15#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng1b42ac12008-09-22 22:21:38 +000016#include "llvm/CodeGen/Passes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#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"
Edwin Török675d5622009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027using namespace llvm;
28using namespace llvm::dwarf;
29
30// Handle the Pass registration stuff necessary to use TargetData's.
Dan Gohman089efff2008-05-13 00:00:25 +000031static RegisterPass<MachineModuleInfo>
Chris Lattnerf4853452010-03-13 20:55:24 +000032X("machinemoduleinfo", "Machine Module Information");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033char MachineModuleInfo::ID = 0;
34
Chris Lattner72ba6722009-09-15 22:44:26 +000035// Out of line virtual method.
36MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
37
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038//===----------------------------------------------------------------------===//
Eric Christopher84603632009-08-26 21:27:09 +000039
Chris Lattnerf4853452010-03-13 20:55:24 +000040MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI)
41: ImmutablePass(&ID), Context(MAI),
42 ObjFileMMI(0), CurCallSite(0), CallsEHReturn(0), CallsUnwindInit(0),
43 DbgInfoAvailable(false) {
Eric Christopherc87d5ae2009-08-26 21:30:49 +000044 // Always emit some info, by default "no personality" info.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 Personalities.push_back(NULL);
46}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
Chris Lattnerf4853452010-03-13 20:55:24 +000048MachineModuleInfo::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");
52 abort();
53}
54
Chris Lattner72ba6722009-09-15 22:44:26 +000055MachineModuleInfo::~MachineModuleInfo() {
Chris Lattner16fcdf92009-09-16 05:26:00 +000056 delete ObjFileMMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057}
58
59/// doInitialization - Initialize the state for a new module.
60///
61bool MachineModuleInfo::doInitialization() {
62 return false;
63}
64
65/// doFinalization - Tear down the state after completion of a module.
66///
67bool MachineModuleInfo::doFinalization() {
68 return false;
69}
70
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071/// EndFunction - Discard function meta information.
72///
73void MachineModuleInfo::EndFunction() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 // Clean up frame info.
75 FrameMoves.clear();
Eric Christopher84603632009-08-26 21:27:09 +000076
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 // Clean up exception info.
78 LandingPads.clear();
Jim Grosbach1db5d982010-01-28 01:45:32 +000079 CallSiteMap.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 TypeInfos.clear();
81 FilterIds.clear();
82 FilterEnds.clear();
83 CallsEHReturn = 0;
84 CallsUnwindInit = 0;
Devang Patelf57e1062009-10-08 20:41:17 +000085 VariableDbgInfo.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086}
87
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088/// AnalyzeModule - Scan the module for global debug information.
89///
90void MachineModuleInfo::AnalyzeModule(Module &M) {
Chris Lattner1e0e0d12009-07-20 06:14:25 +000091 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
92 // UsedFunctions.
Dale Johannesen3dadeb32008-01-16 19:59:28 +000093 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
94 if (!GV || !GV->hasInitializer()) return;
95
96 // Should be an array of 'i8*'.
97 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
98 if (InitList == 0) return;
99
Chris Lattner26165602009-07-20 06:05:50 +0000100 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
101 if (Function *F =
102 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
103 UsedFunctions.insert(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104}
105
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106//===-EH-------------------------------------------------------------------===//
107
108/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
109/// specified MachineBasicBlock.
Bill Wendling4de8de52008-07-03 22:53:42 +0000110LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
111 (MachineBasicBlock *LandingPad) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 unsigned N = LandingPads.size();
113 for (unsigned i = 0; i < N; ++i) {
114 LandingPadInfo &LP = LandingPads[i];
115 if (LP.LandingPadBlock == LandingPad)
116 return LP;
117 }
Eric Christopher84603632009-08-26 21:27:09 +0000118
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 LandingPads.push_back(LandingPadInfo(LandingPad));
120 return LandingPads[N];
121}
122
123/// addInvoke - Provide the begin and end labels of an invoke style call and
124/// associate it with a try landing pad block.
125void 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);
130}
131
132/// addLandingPad - Provide the label of a try LandingPad block.
133///
134unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
135 unsigned LandingPadLabel = NextLabelID();
136 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Eric Christopher84603632009-08-26 21:27:09 +0000137 LP.LandingPadLabel = LandingPadLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 return LandingPadLabel;
139}
140
141/// addPersonality - Provide the personality function for the exception
142/// information.
143void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
144 Function *Personality) {
145 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
146 LP.Personality = Personality;
147
Bill Wendling4de8de52008-07-03 22:53:42 +0000148 for (unsigned i = 0; i < Personalities.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 if (Personalities[i] == Personality)
150 return;
Eric Christopher84603632009-08-26 21:27:09 +0000151
Eric Christopherc87d5ae2009-08-26 21:30:49 +0000152 // 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;
156 else
157 Personalities.push_back(Personality);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158}
159
160/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
161///
162void 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]));
167}
168
169/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
170///
171void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
172 std::vector<GlobalVariable *> &TyInfo) {
173 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling993b3dd2008-07-07 21:41:57 +0000174 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling4de8de52008-07-03 22:53:42 +0000175 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
177 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
178}
179
Duncan Sands923fdb12007-08-27 15:47:50 +0000180/// addCleanup - Add a cleanup action for a landing pad.
181///
182void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
183 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
184 LP.TypeIds.push_back(0);
185}
186
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
188/// pads.
189void MachineModuleInfo::TidyLandingPads() {
190 for (unsigned i = 0; i != LandingPads.size(); ) {
191 LandingPadInfo &LandingPad = LandingPads[i];
Chris Lattner5e423432010-03-09 01:51:43 +0000192 if (isLabelDeleted(LandingPad.LandingPadLabel))
193 LandingPad.LandingPadLabel = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands4ff179f2007-12-19 07:36:31 +0000196 // "nounwind" case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
198 LandingPads.erase(LandingPads.begin() + i);
199 continue;
200 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000201
Bill Wendling4de8de52008-07-03 22:53:42 +0000202 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
Chris Lattner5e423432010-03-09 01:51:43 +0000203 unsigned BeginLabel = LandingPad.BeginLabels[j];
204 unsigned EndLabel = LandingPad.EndLabels[j];
205 if (isLabelDeleted(BeginLabel) || isLabelDeleted(EndLabel)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
207 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
208 continue;
209 }
210
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 ++j;
212 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000213
214 // Remove landing pads with no try-ranges.
Dan Gohman301f4052008-01-29 13:02:09 +0000215 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands241a0c92007-09-05 11:27:52 +0000216 LandingPads.erase(LandingPads.begin() + i);
217 continue;
218 }
219
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();
225
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 ++i;
227 }
228}
229
Eric Christopher84603632009-08-26 21:27:09 +0000230/// getTypeIDFor - Return the type id for the specified typeinfo. This is
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231/// function wide.
232unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling4de8de52008-07-03 22:53:42 +0000233 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
234 if (TypeInfos[i] == TI) return i + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235
236 TypeInfos.push_back(TI);
237 return TypeInfos.size();
238}
239
240/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
241/// function wide.
Bill Wendling0e679b82008-06-27 01:27:56 +0000242int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 // 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) {
Bill Wendling0e679b82008-06-27 01:27:56 +0000248 unsigned i = *I, j = TyIds.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249
250 while (i && j)
251 if (FilterIds[--i] != TyIds[--j])
252 goto try_next;
253
254 if (!j)
255 // The new filter coincides with range [i, end) of the existing filter.
256 return -(1 + i);
Bill Wendling0e679b82008-06-27 01:27:56 +0000257
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258try_next:;
259 }
260
261 // Add the new filter.
Bill Wendling0e679b82008-06-27 01:27:56 +0000262 int FilterID = -(1 + FilterIds.size());
263 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
264 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 FilterIds.push_back(TyIds[I]);
Bill Wendling0e679b82008-06-27 01:27:56 +0000266 FilterEnds.push_back(FilterIds.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 FilterIds.push_back(0); // terminator
268 return FilterID;
269}
270
271/// getPersonality - Return the personality function for the current function.
272Function *MachineModuleInfo::getPersonality() const {
273 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
274 // function
275 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
276}
277
278/// getPersonalityIndex - Return unique index for current personality
Eric Christophere92cc8d2009-08-26 21:44:57 +0000279/// function. NULL/first personality function should always get zero index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280unsigned MachineModuleInfo::getPersonalityIndex() const {
281 const Function* Personality = NULL;
Eric Christopher84603632009-08-26 21:27:09 +0000282
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling4de8de52008-07-03 22:53:42 +0000284 for (unsigned i = 0; i != LandingPads.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 if (LandingPads[i].Personality) {
286 Personality = LandingPads[i].Personality;
287 break;
288 }
Eric Christopher84603632009-08-26 21:27:09 +0000289
Bill Wendling4de8de52008-07-03 22:53:42 +0000290 for (unsigned i = 0; i < Personalities.size(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 if (Personalities[i] == Personality)
292 return i;
Bill Wendling4de8de52008-07-03 22:53:42 +0000293 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294
Eric Christophere92cc8d2009-08-26 21:44:57 +0000295 // This will happen if the current personality function is
296 // in the zero index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 return 0;
298}
299