blob: 8661b9ee132bdaf4c8bca4f0021143b47487faca [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
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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040MachineModuleInfo::MachineModuleInfo()
Dan Gohman26f8c272008-09-04 17:05:41 +000041: ImmutablePass(&ID)
Chris Lattner16fcdf92009-09-16 05:26:00 +000042, ObjFileMMI(0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043, CallsEHReturn(0)
44, CallsUnwindInit(0)
Chris Lattner72ba6722009-09-15 22:44:26 +000045, DbgInfoAvailable(false) {
Eric Christopherc87d5ae2009-08-26 21:30:49 +000046 // Always emit some info, by default "no personality" info.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 Personalities.push_back(NULL);
48}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049
Chris Lattner72ba6722009-09-15 22:44:26 +000050MachineModuleInfo::~MachineModuleInfo() {
Chris Lattner16fcdf92009-09-16 05:26:00 +000051 delete ObjFileMMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052}
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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066/// EndFunction - Discard function meta information.
67///
68void MachineModuleInfo::EndFunction() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 // Clean up frame info.
70 FrameMoves.clear();
Eric Christopher84603632009-08-26 21:27:09 +000071
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 // Clean up exception info.
73 LandingPads.clear();
74 TypeInfos.clear();
75 FilterIds.clear();
76 FilterEnds.clear();
77 CallsEHReturn = 0;
78 CallsUnwindInit = 0;
79}
80
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081/// AnalyzeModule - Scan the module for global debug information.
82///
83void MachineModuleInfo::AnalyzeModule(Module &M) {
Chris Lattner1e0e0d12009-07-20 06:14:25 +000084 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
85 // UsedFunctions.
Dale Johannesen3dadeb32008-01-16 19:59:28 +000086 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
87 if (!GV || !GV->hasInitializer()) return;
88
89 // Should be an array of 'i8*'.
90 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
91 if (InitList == 0) return;
92
Chris Lattner26165602009-07-20 06:05:50 +000093 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
94 if (Function *F =
95 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
96 UsedFunctions.insert(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097}
98
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099//===-EH-------------------------------------------------------------------===//
100
101/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
102/// specified MachineBasicBlock.
Bill Wendling4de8de52008-07-03 22:53:42 +0000103LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
104 (MachineBasicBlock *LandingPad) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 unsigned N = LandingPads.size();
106 for (unsigned i = 0; i < N; ++i) {
107 LandingPadInfo &LP = LandingPads[i];
108 if (LP.LandingPadBlock == LandingPad)
109 return LP;
110 }
Eric Christopher84603632009-08-26 21:27:09 +0000111
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 LandingPads.push_back(LandingPadInfo(LandingPad));
113 return LandingPads[N];
114}
115
116/// addInvoke - Provide the begin and end labels of an invoke style call and
117/// associate it with a try landing pad block.
118void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
119 unsigned BeginLabel, unsigned EndLabel) {
120 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
121 LP.BeginLabels.push_back(BeginLabel);
122 LP.EndLabels.push_back(EndLabel);
123}
124
125/// addLandingPad - Provide the label of a try LandingPad block.
126///
127unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
128 unsigned LandingPadLabel = NextLabelID();
129 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Eric Christopher84603632009-08-26 21:27:09 +0000130 LP.LandingPadLabel = LandingPadLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 return LandingPadLabel;
132}
133
134/// addPersonality - Provide the personality function for the exception
135/// information.
136void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
137 Function *Personality) {
138 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
139 LP.Personality = Personality;
140
Bill Wendling4de8de52008-07-03 22:53:42 +0000141 for (unsigned i = 0; i < Personalities.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 if (Personalities[i] == Personality)
143 return;
Eric Christopher84603632009-08-26 21:27:09 +0000144
Eric Christopherc87d5ae2009-08-26 21:30:49 +0000145 // If this is the first personality we're adding go
146 // ahead and add it at the beginning.
147 if (Personalities[0] == NULL)
148 Personalities[0] = Personality;
149 else
150 Personalities.push_back(Personality);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151}
152
153/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
154///
155void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
156 std::vector<GlobalVariable *> &TyInfo) {
157 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
158 for (unsigned N = TyInfo.size(); N; --N)
159 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
160}
161
162/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
163///
164void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
165 std::vector<GlobalVariable *> &TyInfo) {
166 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling993b3dd2008-07-07 21:41:57 +0000167 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling4de8de52008-07-03 22:53:42 +0000168 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
170 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
171}
172
Duncan Sands923fdb12007-08-27 15:47:50 +0000173/// addCleanup - Add a cleanup action for a landing pad.
174///
175void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
176 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
177 LP.TypeIds.push_back(0);
178}
179
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
181/// pads.
182void MachineModuleInfo::TidyLandingPads() {
183 for (unsigned i = 0; i != LandingPads.size(); ) {
184 LandingPadInfo &LandingPad = LandingPads[i];
185 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
186
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands4ff179f2007-12-19 07:36:31 +0000188 // "nounwind" case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
190 LandingPads.erase(LandingPads.begin() + i);
191 continue;
192 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000193
Bill Wendling4de8de52008-07-03 22:53:42 +0000194 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
196 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands241a0c92007-09-05 11:27:52 +0000197
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 if (!BeginLabel || !EndLabel) {
199 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
200 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
201 continue;
202 }
203
204 LandingPad.BeginLabels[j] = BeginLabel;
205 LandingPad.EndLabels[j] = EndLabel;
206 ++j;
207 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000208
209 // Remove landing pads with no try-ranges.
Dan Gohman301f4052008-01-29 13:02:09 +0000210 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands241a0c92007-09-05 11:27:52 +0000211 LandingPads.erase(LandingPads.begin() + i);
212 continue;
213 }
214
215 // If there is no landing pad, ensure that the list of typeids is empty.
216 // If the only typeid is a cleanup, this is the same as having no typeids.
217 if (!LandingPad.LandingPadBlock ||
218 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
219 LandingPad.TypeIds.clear();
220
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 ++i;
222 }
223}
224
Eric Christopher84603632009-08-26 21:27:09 +0000225/// getTypeIDFor - Return the type id for the specified typeinfo. This is
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226/// function wide.
227unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling4de8de52008-07-03 22:53:42 +0000228 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
229 if (TypeInfos[i] == TI) return i + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230
231 TypeInfos.push_back(TI);
232 return TypeInfos.size();
233}
234
235/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
236/// function wide.
Bill Wendling0e679b82008-06-27 01:27:56 +0000237int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 // If the new filter coincides with the tail of an existing filter, then
239 // re-use the existing filter. Folding filters more than this requires
240 // re-ordering filters and/or their elements - probably not worth it.
241 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
242 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling0e679b82008-06-27 01:27:56 +0000243 unsigned i = *I, j = TyIds.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244
245 while (i && j)
246 if (FilterIds[--i] != TyIds[--j])
247 goto try_next;
248
249 if (!j)
250 // The new filter coincides with range [i, end) of the existing filter.
251 return -(1 + i);
Bill Wendling0e679b82008-06-27 01:27:56 +0000252
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253try_next:;
254 }
255
256 // Add the new filter.
Bill Wendling0e679b82008-06-27 01:27:56 +0000257 int FilterID = -(1 + FilterIds.size());
258 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
259 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 FilterIds.push_back(TyIds[I]);
Bill Wendling0e679b82008-06-27 01:27:56 +0000261 FilterEnds.push_back(FilterIds.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 FilterIds.push_back(0); // terminator
263 return FilterID;
264}
265
266/// getPersonality - Return the personality function for the current function.
267Function *MachineModuleInfo::getPersonality() const {
268 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
269 // function
270 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
271}
272
273/// getPersonalityIndex - Return unique index for current personality
Eric Christophere92cc8d2009-08-26 21:44:57 +0000274/// function. NULL/first personality function should always get zero index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275unsigned MachineModuleInfo::getPersonalityIndex() const {
276 const Function* Personality = NULL;
Eric Christopher84603632009-08-26 21:27:09 +0000277
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling4de8de52008-07-03 22:53:42 +0000279 for (unsigned i = 0; i != LandingPads.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 if (LandingPads[i].Personality) {
281 Personality = LandingPads[i].Personality;
282 break;
283 }
Eric Christopher84603632009-08-26 21:27:09 +0000284
Bill Wendling4de8de52008-07-03 22:53:42 +0000285 for (unsigned i = 0; i < Personalities.size(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 if (Personalities[i] == Personality)
287 return i;
Bill Wendling4de8de52008-07-03 22:53:42 +0000288 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289
Eric Christophere92cc8d2009-08-26 21:44:57 +0000290 // This will happen if the current personality function is
291 // in the zero index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 return 0;
293}
294
295//===----------------------------------------------------------------------===//
296/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
297/// a info consumer to determine if the range of two labels is empty, by seeing
298/// if the labels map to the same reduced label.
299
300namespace llvm {
301
302struct DebugLabelFolder : public MachineFunctionPass {
303 static char ID;
Dan Gohman26f8c272008-09-04 17:05:41 +0000304 DebugLabelFolder() : MachineFunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305
Evan Cheng465a66e2008-09-22 20:58:04 +0000306 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanecb436f2009-07-31 23:37:33 +0000307 AU.setPreservesCFG();
Evan Cheng1b42ac12008-09-22 22:21:38 +0000308 AU.addPreservedID(MachineLoopInfoID);
309 AU.addPreservedID(MachineDominatorsID);
Evan Cheng465a66e2008-09-22 20:58:04 +0000310 MachineFunctionPass::getAnalysisUsage(AU);
311 }
312
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 virtual bool runOnMachineFunction(MachineFunction &MF);
314 virtual const char *getPassName() const { return "Label Folder"; }
315};
316
317char DebugLabelFolder::ID = 0;
318
319bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
320 // Get machine module info.
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000321 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 if (!MMI) return false;
Eric Christopher84603632009-08-26 21:27:09 +0000323
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 // Track if change is made.
325 bool MadeChange = false;
326 // No prior label to begin.
327 unsigned PriorLabel = 0;
Eric Christopher84603632009-08-26 21:27:09 +0000328
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 // Iterate through basic blocks.
330 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
331 BB != E; ++BB) {
332 // Iterate through instructions.
333 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
334 // Is it a label.
Devang Patel9a727412009-04-10 18:58:59 +0000335 if (I->isDebugLabel() && !MMI->isDbgLabelUsed(I->getOperand(0).getImm())){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 // The label ID # is always operand #0, an immediate.
337 unsigned NextLabel = I->getOperand(0).getImm();
Eric Christopher84603632009-08-26 21:27:09 +0000338
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 // If there was an immediate prior label.
340 if (PriorLabel) {
341 // Remap the current label to prior label.
342 MMI->RemapLabel(NextLabel, PriorLabel);
343 // Delete the current label.
344 I = BB->erase(I);
345 // Indicate a change has been made.
346 MadeChange = true;
347 continue;
348 } else {
349 // Start a new round.
350 PriorLabel = NextLabel;
351 }
352 } else {
353 // No consecutive labels.
354 PriorLabel = 0;
355 }
Eric Christopher84603632009-08-26 21:27:09 +0000356
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 ++I;
358 }
359 }
Eric Christopher84603632009-08-26 21:27:09 +0000360
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 return MadeChange;
362}
363
364FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
365
366}