1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/CodeGen/MachineModuleInfo.h"
12 #include "llvm/Constants.h"
13 #include "llvm/Analysis/ValueTracking.h"
14 #include "llvm/CodeGen/MachineFunctionPass.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/Passes.h"
17 #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"
26 #include "llvm/Support/Streams.h"
28 using namespace llvm::dwarf;
30 // Handle the Pass registration stuff necessary to use TargetData's.
31 static RegisterPass<MachineModuleInfo>
32 X("machinemoduleinfo", "Module Information");
33 char MachineModuleInfo::ID = 0;
35 //===----------------------------------------------------------------------===//
37 MachineModuleInfo::MachineModuleInfo()
46 , DbgInfoAvailable(false)
48 // Always emit "no personality" info
49 Personalities.push_back(NULL);
51 MachineModuleInfo::~MachineModuleInfo() {
55 /// doInitialization - Initialize the state for a new module.
57 bool MachineModuleInfo::doInitialization() {
61 /// doFinalization - Tear down the state after completion of a module.
63 bool MachineModuleInfo::doFinalization() {
67 /// BeginFunction - Begin gathering function meta information.
69 void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
73 /// EndFunction - Discard function meta information.
75 void MachineModuleInfo::EndFunction() {
76 // Clean up frame info.
79 // Clean up exception info.
88 /// AnalyzeModule - Scan the module for global debug information.
90 void MachineModuleInfo::AnalyzeModule(Module &M) {
91 // Insert functions in the llvm.used array into UsedFunctions.
92 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
93 if (!GV || !GV->hasInitializer()) return;
95 // Should be an array of 'i8*'.
96 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
97 if (InitList == 0) return;
99 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
100 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
101 if (CE->getOpcode() == Instruction::BitCast)
102 if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
103 UsedFunctions.insert(F);
107 //===-EH-------------------------------------------------------------------===//
109 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
110 /// specified MachineBasicBlock.
111 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
112 (MachineBasicBlock *LandingPad) {
113 unsigned N = LandingPads.size();
114 for (unsigned i = 0; i < N; ++i) {
115 LandingPadInfo &LP = LandingPads[i];
116 if (LP.LandingPadBlock == LandingPad)
120 LandingPads.push_back(LandingPadInfo(LandingPad));
121 return LandingPads[N];
124 /// addInvoke - Provide the begin and end labels of an invoke style call and
125 /// associate it with a try landing pad block.
126 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
127 unsigned BeginLabel, unsigned EndLabel) {
128 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
129 LP.BeginLabels.push_back(BeginLabel);
130 LP.EndLabels.push_back(EndLabel);
133 /// addLandingPad - Provide the label of a try LandingPad block.
135 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
136 unsigned LandingPadLabel = NextLabelID();
137 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
138 LP.LandingPadLabel = LandingPadLabel;
139 return LandingPadLabel;
142 /// addPersonality - Provide the personality function for the exception
144 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
145 Function *Personality) {
146 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
147 LP.Personality = Personality;
149 for (unsigned i = 0; i < Personalities.size(); ++i)
150 if (Personalities[i] == Personality)
153 Personalities.push_back(Personality);
156 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
158 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
159 std::vector<GlobalVariable *> &TyInfo) {
160 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
161 for (unsigned N = TyInfo.size(); N; --N)
162 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
165 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
167 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
168 std::vector<GlobalVariable *> &TyInfo) {
169 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
170 std::vector<unsigned> IdsInFilter(TyInfo.size());
171 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
172 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
173 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
176 /// addCleanup - Add a cleanup action for a landing pad.
178 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
179 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
180 LP.TypeIds.push_back(0);
183 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
185 void MachineModuleInfo::TidyLandingPads() {
186 for (unsigned i = 0; i != LandingPads.size(); ) {
187 LandingPadInfo &LandingPad = LandingPads[i];
188 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
190 // Special case: we *should* emit LPs with null LP MBB. This indicates
192 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
193 LandingPads.erase(LandingPads.begin() + i);
197 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
198 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
199 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
201 if (!BeginLabel || !EndLabel) {
202 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
203 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
207 LandingPad.BeginLabels[j] = BeginLabel;
208 LandingPad.EndLabels[j] = EndLabel;
212 // Remove landing pads with no try-ranges.
213 if (LandingPads[i].BeginLabels.empty()) {
214 LandingPads.erase(LandingPads.begin() + i);
218 // If there is no landing pad, ensure that the list of typeids is empty.
219 // If the only typeid is a cleanup, this is the same as having no typeids.
220 if (!LandingPad.LandingPadBlock ||
221 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
222 LandingPad.TypeIds.clear();
228 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
230 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
231 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
232 if (TypeInfos[i] == TI) return i + 1;
234 TypeInfos.push_back(TI);
235 return TypeInfos.size();
238 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
240 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
241 // 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) {
246 unsigned i = *I, j = TyIds.size();
249 if (FilterIds[--i] != TyIds[--j])
253 // The new filter coincides with range [i, end) of the existing filter.
259 // Add the new filter.
260 int FilterID = -(1 + FilterIds.size());
261 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
262 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
263 FilterIds.push_back(TyIds[I]);
264 FilterEnds.push_back(FilterIds.size());
265 FilterIds.push_back(0); // terminator
269 /// getPersonality - Return the personality function for the current function.
270 Function *MachineModuleInfo::getPersonality() const {
271 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
273 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
276 /// getPersonalityIndex - Return unique index for current personality
277 /// function. NULL personality function should always get zero index.
278 unsigned MachineModuleInfo::getPersonalityIndex() const {
279 const Function* Personality = NULL;
281 // Scan landing pads. If there is at least one non-NULL personality - use it.
282 for (unsigned i = 0; i != LandingPads.size(); ++i)
283 if (LandingPads[i].Personality) {
284 Personality = LandingPads[i].Personality;
288 for (unsigned i = 0; i < Personalities.size(); ++i) {
289 if (Personalities[i] == Personality)
293 // This should never happen
294 assert(0 && "Personality function should be set!");
298 //===----------------------------------------------------------------------===//
299 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
300 /// a info consumer to determine if the range of two labels is empty, by seeing
301 /// if the labels map to the same reduced label.
305 struct DebugLabelFolder : public MachineFunctionPass {
307 DebugLabelFolder() : MachineFunctionPass(&ID) {}
309 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
310 AU.addPreservedID(MachineLoopInfoID);
311 AU.addPreservedID(MachineDominatorsID);
312 MachineFunctionPass::getAnalysisUsage(AU);
315 virtual bool runOnMachineFunction(MachineFunction &MF);
316 virtual const char *getPassName() const { return "Label Folder"; }
319 char DebugLabelFolder::ID = 0;
321 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
322 // Get machine module info.
323 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
324 if (!MMI) return false;
326 // Track if change is made.
327 bool MadeChange = false;
328 // No prior label to begin.
329 unsigned PriorLabel = 0;
331 // Iterate through basic blocks.
332 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
334 // Iterate through instructions.
335 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
337 if (I->isDebugLabel()) {
338 // The label ID # is always operand #0, an immediate.
339 unsigned NextLabel = I->getOperand(0).getImm();
341 // If there was an immediate prior label.
343 // Remap the current label to prior label.
344 MMI->RemapLabel(NextLabel, PriorLabel);
345 // Delete the current label.
347 // Indicate a change has been made.
351 // Start a new round.
352 PriorLabel = NextLabel;
355 // No consecutive labels.
366 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }