blob: 08b126a13b19e47ba9220652444812f5653b6530 [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>
32X("machinemoduleinfo", "Module Information");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033char MachineModuleInfo::ID = 0;
34
35//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037MachineModuleInfo::MachineModuleInfo()
Dan Gohman26f8c272008-09-04 17:05:41 +000038: ImmutablePass(&ID)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039, LabelIDList()
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040, FrameMoves()
41, LandingPads()
42, Personalities()
43, CallsEHReturn(0)
44, CallsUnwindInit(0)
Devang Patel86cfa402009-01-13 23:02:17 +000045, DbgInfoAvailable(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046{
47 // Always emit "no personality" info
48 Personalities.push_back(NULL);
49}
50MachineModuleInfo::~MachineModuleInfo() {
51
52}
53
54/// doInitialization - Initialize the state for a new module.
55///
56bool MachineModuleInfo::doInitialization() {
57 return false;
58}
59
60/// doFinalization - Tear down the state after completion of a module.
61///
62bool MachineModuleInfo::doFinalization() {
63 return false;
64}
65
66/// BeginFunction - Begin gathering function meta information.
67///
68void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
69 // Coming soon.
70}
71
72/// EndFunction - Discard function meta information.
73///
74void MachineModuleInfo::EndFunction() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 // Clean up frame info.
76 FrameMoves.clear();
77
78 // Clean up exception info.
79 LandingPads.clear();
80 TypeInfos.clear();
81 FilterIds.clear();
82 FilterEnds.clear();
83 CallsEHReturn = 0;
84 CallsUnwindInit = 0;
85}
86
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087/// AnalyzeModule - Scan the module for global debug information.
88///
89void MachineModuleInfo::AnalyzeModule(Module &M) {
Chris Lattner1e0e0d12009-07-20 06:14:25 +000090 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
91 // UsedFunctions.
Dale Johannesen3dadeb32008-01-16 19:59:28 +000092 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
93 if (!GV || !GV->hasInitializer()) return;
94
95 // Should be an array of 'i8*'.
96 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
97 if (InitList == 0) return;
98
Chris Lattner26165602009-07-20 06:05:50 +000099 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
100 if (Function *F =
101 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
102 UsedFunctions.insert(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103}
104
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105//===-EH-------------------------------------------------------------------===//
106
107/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
108/// specified MachineBasicBlock.
Bill Wendling4de8de52008-07-03 22:53:42 +0000109LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
110 (MachineBasicBlock *LandingPad) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 unsigned N = LandingPads.size();
112 for (unsigned i = 0; i < N; ++i) {
113 LandingPadInfo &LP = LandingPads[i];
114 if (LP.LandingPadBlock == LandingPad)
115 return LP;
116 }
117
118 LandingPads.push_back(LandingPadInfo(LandingPad));
119 return LandingPads[N];
120}
121
122/// addInvoke - Provide the begin and end labels of an invoke style call and
123/// associate it with a try landing pad block.
124void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
125 unsigned BeginLabel, unsigned EndLabel) {
126 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
127 LP.BeginLabels.push_back(BeginLabel);
128 LP.EndLabels.push_back(EndLabel);
129}
130
131/// addLandingPad - Provide the label of a try LandingPad block.
132///
133unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
134 unsigned LandingPadLabel = NextLabelID();
135 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
136 LP.LandingPadLabel = LandingPadLabel;
137 return LandingPadLabel;
138}
139
140/// addPersonality - Provide the personality function for the exception
141/// information.
142void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
143 Function *Personality) {
144 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
145 LP.Personality = Personality;
146
Bill Wendling4de8de52008-07-03 22:53:42 +0000147 for (unsigned i = 0; i < Personalities.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 if (Personalities[i] == Personality)
149 return;
150
151 Personalities.push_back(Personality);
152}
153
154/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
155///
156void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
157 std::vector<GlobalVariable *> &TyInfo) {
158 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
159 for (unsigned N = TyInfo.size(); N; --N)
160 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
161}
162
163/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
164///
165void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
166 std::vector<GlobalVariable *> &TyInfo) {
167 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling993b3dd2008-07-07 21:41:57 +0000168 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling4de8de52008-07-03 22:53:42 +0000169 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
171 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
172}
173
Duncan Sands923fdb12007-08-27 15:47:50 +0000174/// addCleanup - Add a cleanup action for a landing pad.
175///
176void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
177 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
178 LP.TypeIds.push_back(0);
179}
180
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
182/// pads.
183void MachineModuleInfo::TidyLandingPads() {
184 for (unsigned i = 0; i != LandingPads.size(); ) {
185 LandingPadInfo &LandingPad = LandingPads[i];
186 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
187
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands4ff179f2007-12-19 07:36:31 +0000189 // "nounwind" case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
191 LandingPads.erase(LandingPads.begin() + i);
192 continue;
193 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000194
Bill Wendling4de8de52008-07-03 22:53:42 +0000195 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
197 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands241a0c92007-09-05 11:27:52 +0000198
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 if (!BeginLabel || !EndLabel) {
200 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
201 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
202 continue;
203 }
204
205 LandingPad.BeginLabels[j] = BeginLabel;
206 LandingPad.EndLabels[j] = EndLabel;
207 ++j;
208 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000209
210 // Remove landing pads with no try-ranges.
Dan Gohman301f4052008-01-29 13:02:09 +0000211 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands241a0c92007-09-05 11:27:52 +0000212 LandingPads.erase(LandingPads.begin() + i);
213 continue;
214 }
215
216 // If there is no landing pad, ensure that the list of typeids is empty.
217 // If the only typeid is a cleanup, this is the same as having no typeids.
218 if (!LandingPad.LandingPadBlock ||
219 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
220 LandingPad.TypeIds.clear();
221
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 ++i;
223 }
224}
225
226/// getTypeIDFor - Return the type id for the specified typeinfo. This is
227/// function wide.
228unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling4de8de52008-07-03 22:53:42 +0000229 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
230 if (TypeInfos[i] == TI) return i + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231
232 TypeInfos.push_back(TI);
233 return TypeInfos.size();
234}
235
236/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
237/// function wide.
Bill Wendling0e679b82008-06-27 01:27:56 +0000238int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 // If the new filter coincides with the tail of an existing filter, then
240 // re-use the existing filter. Folding filters more than this requires
241 // re-ordering filters and/or their elements - probably not worth it.
242 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
243 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling0e679b82008-06-27 01:27:56 +0000244 unsigned i = *I, j = TyIds.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245
246 while (i && j)
247 if (FilterIds[--i] != TyIds[--j])
248 goto try_next;
249
250 if (!j)
251 // The new filter coincides with range [i, end) of the existing filter.
252 return -(1 + i);
Bill Wendling0e679b82008-06-27 01:27:56 +0000253
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254try_next:;
255 }
256
257 // Add the new filter.
Bill Wendling0e679b82008-06-27 01:27:56 +0000258 int FilterID = -(1 + FilterIds.size());
259 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
260 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 FilterIds.push_back(TyIds[I]);
Bill Wendling0e679b82008-06-27 01:27:56 +0000262 FilterEnds.push_back(FilterIds.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 FilterIds.push_back(0); // terminator
264 return FilterID;
265}
266
267/// getPersonality - Return the personality function for the current function.
268Function *MachineModuleInfo::getPersonality() const {
269 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
270 // function
271 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
272}
273
274/// getPersonalityIndex - Return unique index for current personality
275/// function. NULL personality function should always get zero index.
276unsigned MachineModuleInfo::getPersonalityIndex() const {
277 const Function* Personality = NULL;
278
279 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling4de8de52008-07-03 22:53:42 +0000280 for (unsigned i = 0; i != LandingPads.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 if (LandingPads[i].Personality) {
282 Personality = LandingPads[i].Personality;
283 break;
284 }
285
Bill Wendling4de8de52008-07-03 22:53:42 +0000286 for (unsigned i = 0; i < Personalities.size(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 if (Personalities[i] == Personality)
288 return i;
Bill Wendling4de8de52008-07-03 22:53:42 +0000289 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290
291 // This should never happen
Edwin Törökbd448e32009-07-14 16:55:14 +0000292 llvm_unreachable("Personality function should be set!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 return 0;
294}
295
296//===----------------------------------------------------------------------===//
297/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
298/// a info consumer to determine if the range of two labels is empty, by seeing
299/// if the labels map to the same reduced label.
300
301namespace llvm {
302
303struct DebugLabelFolder : public MachineFunctionPass {
304 static char ID;
Dan Gohman26f8c272008-09-04 17:05:41 +0000305 DebugLabelFolder() : MachineFunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306
Evan Cheng465a66e2008-09-22 20:58:04 +0000307 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanecb436f2009-07-31 23:37:33 +0000308 AU.setPreservesCFG();
Evan Cheng1b42ac12008-09-22 22:21:38 +0000309 AU.addPreservedID(MachineLoopInfoID);
310 AU.addPreservedID(MachineDominatorsID);
Evan Cheng465a66e2008-09-22 20:58:04 +0000311 MachineFunctionPass::getAnalysisUsage(AU);
312 }
313
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 virtual bool runOnMachineFunction(MachineFunction &MF);
315 virtual const char *getPassName() const { return "Label Folder"; }
316};
317
318char DebugLabelFolder::ID = 0;
319
320bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
321 // Get machine module info.
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000322 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 if (!MMI) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324
325 // Track if change is made.
326 bool MadeChange = false;
327 // No prior label to begin.
328 unsigned PriorLabel = 0;
329
330 // Iterate through basic blocks.
331 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
332 BB != E; ++BB) {
333 // Iterate through instructions.
334 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
335 // Is it a label.
Devang Patel9a727412009-04-10 18:58:59 +0000336 if (I->isDebugLabel() && !MMI->isDbgLabelUsed(I->getOperand(0).getImm())){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 // The label ID # is always operand #0, an immediate.
338 unsigned NextLabel = I->getOperand(0).getImm();
339
340 // If there was an immediate prior label.
341 if (PriorLabel) {
342 // Remap the current label to prior label.
343 MMI->RemapLabel(NextLabel, PriorLabel);
344 // Delete the current label.
345 I = BB->erase(I);
346 // Indicate a change has been made.
347 MadeChange = true;
348 continue;
349 } else {
350 // Start a new round.
351 PriorLabel = NextLabel;
352 }
353 } else {
354 // No consecutive labels.
355 PriorLabel = 0;
356 }
357
358 ++I;
359 }
360 }
361
362 return MadeChange;
363}
364
365FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
366
367}
Bill Wendling4de8de52008-07-03 22:53:42 +0000368