It turns out most of the thumb2 instructions are not allowed to touch SP. The semanti...
[llvm/avr.git] / lib / CodeGen / MachineModuleInfo.cpp
blobdf9d7cab0954929c2ab389eb3c586e7cd11b674e
1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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/ErrorHandling.h"
27 #include "llvm/Support/Streams.h"
28 using namespace llvm;
29 using namespace llvm::dwarf;
31 // Handle the Pass registration stuff necessary to use TargetData's.
32 static RegisterPass<MachineModuleInfo>
33 X("machinemoduleinfo", "Module Information");
34 char MachineModuleInfo::ID = 0;
36 //===----------------------------------------------------------------------===//
38 MachineModuleInfo::MachineModuleInfo()
39 : ImmutablePass(&ID)
40 , LabelIDList()
41 , FrameMoves()
42 , LandingPads()
43 , Personalities()
44 , CallsEHReturn(0)
45 , CallsUnwindInit(0)
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.
56 ///
57 bool MachineModuleInfo::doInitialization() {
58 return false;
61 /// doFinalization - Tear down the state after completion of a module.
62 ///
63 bool MachineModuleInfo::doFinalization() {
64 return false;
67 /// BeginFunction - Begin gathering function meta information.
68 ///
69 void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
70 // Coming soon.
73 /// EndFunction - Discard function meta information.
74 ///
75 void MachineModuleInfo::EndFunction() {
76 // Clean up frame info.
77 FrameMoves.clear();
79 // Clean up exception info.
80 LandingPads.clear();
81 TypeInfos.clear();
82 FilterIds.clear();
83 FilterEnds.clear();
84 CallsEHReturn = 0;
85 CallsUnwindInit = 0;
88 /// AnalyzeModule - Scan the module for global debug information.
89 ///
90 void MachineModuleInfo::AnalyzeModule(Module &M) {
91 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
92 // UsedFunctions.
93 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
94 if (!GV || !GV->hasInitializer()) return;
96 // Should be an array of 'i8*'.
97 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
98 if (InitList == 0) return;
100 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);
106 //===-EH-------------------------------------------------------------------===//
108 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
109 /// specified MachineBasicBlock.
110 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
111 (MachineBasicBlock *LandingPad) {
112 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;
119 LandingPads.push_back(LandingPadInfo(LandingPad));
120 return LandingPads[N];
123 /// addInvoke - Provide the begin and end labels of an invoke style call and
124 /// associate it with a try landing pad block.
125 void 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);
132 /// addLandingPad - Provide the label of a try LandingPad block.
134 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
135 unsigned LandingPadLabel = NextLabelID();
136 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
137 LP.LandingPadLabel = LandingPadLabel;
138 return LandingPadLabel;
141 /// addPersonality - Provide the personality function for the exception
142 /// information.
143 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
144 Function *Personality) {
145 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
146 LP.Personality = Personality;
148 for (unsigned i = 0; i < Personalities.size(); ++i)
149 if (Personalities[i] == Personality)
150 return;
152 Personalities.push_back(Personality);
155 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
157 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
158 std::vector<GlobalVariable *> &TyInfo) {
159 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
160 for (unsigned N = TyInfo.size(); N; --N)
161 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
164 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
166 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
167 std::vector<GlobalVariable *> &TyInfo) {
168 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
169 std::vector<unsigned> IdsInFilter(TyInfo.size());
170 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
171 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
172 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
175 /// addCleanup - Add a cleanup action for a landing pad.
177 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
178 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
179 LP.TypeIds.push_back(0);
182 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
183 /// pads.
184 void MachineModuleInfo::TidyLandingPads() {
185 for (unsigned i = 0; i != LandingPads.size(); ) {
186 LandingPadInfo &LandingPad = LandingPads[i];
187 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
189 // Special case: we *should* emit LPs with null LP MBB. This indicates
190 // "nounwind" case.
191 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
192 LandingPads.erase(LandingPads.begin() + i);
193 continue;
196 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
197 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
198 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
200 if (!BeginLabel || !EndLabel) {
201 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
202 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
203 continue;
206 LandingPad.BeginLabels[j] = BeginLabel;
207 LandingPad.EndLabels[j] = EndLabel;
208 ++j;
211 // Remove landing pads with no try-ranges.
212 if (LandingPads[i].BeginLabels.empty()) {
213 LandingPads.erase(LandingPads.begin() + i);
214 continue;
217 // If there is no landing pad, ensure that the list of typeids is empty.
218 // If the only typeid is a cleanup, this is the same as having no typeids.
219 if (!LandingPad.LandingPadBlock ||
220 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
221 LandingPad.TypeIds.clear();
223 ++i;
227 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
228 /// function wide.
229 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
230 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
231 if (TypeInfos[i] == TI) return i + 1;
233 TypeInfos.push_back(TI);
234 return TypeInfos.size();
237 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
238 /// function wide.
239 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
240 // If the new filter coincides with the tail of an existing filter, then
241 // re-use the existing filter. Folding filters more than this requires
242 // re-ordering filters and/or their elements - probably not worth it.
243 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
244 E = FilterEnds.end(); I != E; ++I) {
245 unsigned i = *I, j = TyIds.size();
247 while (i && j)
248 if (FilterIds[--i] != TyIds[--j])
249 goto try_next;
251 if (!j)
252 // The new filter coincides with range [i, end) of the existing filter.
253 return -(1 + i);
255 try_next:;
258 // Add the new filter.
259 int FilterID = -(1 + FilterIds.size());
260 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
261 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
262 FilterIds.push_back(TyIds[I]);
263 FilterEnds.push_back(FilterIds.size());
264 FilterIds.push_back(0); // terminator
265 return FilterID;
268 /// getPersonality - Return the personality function for the current function.
269 Function *MachineModuleInfo::getPersonality() const {
270 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
271 // function
272 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
275 /// getPersonalityIndex - Return unique index for current personality
276 /// function. NULL personality function should always get zero index.
277 unsigned MachineModuleInfo::getPersonalityIndex() const {
278 const Function* Personality = NULL;
280 // Scan landing pads. If there is at least one non-NULL personality - use it.
281 for (unsigned i = 0; i != LandingPads.size(); ++i)
282 if (LandingPads[i].Personality) {
283 Personality = LandingPads[i].Personality;
284 break;
287 for (unsigned i = 0; i < Personalities.size(); ++i) {
288 if (Personalities[i] == Personality)
289 return i;
292 // This should never happen
293 llvm_unreachable("Personality function should be set!");
294 return 0;
297 //===----------------------------------------------------------------------===//
298 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
299 /// a info consumer to determine if the range of two labels is empty, by seeing
300 /// if the labels map to the same reduced label.
302 namespace llvm {
304 struct DebugLabelFolder : public MachineFunctionPass {
305 static char ID;
306 DebugLabelFolder() : MachineFunctionPass(&ID) {}
308 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
309 AU.setPreservesCFG();
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 = getAnalysisIfAvailable<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();
333 BB != E; ++BB) {
334 // Iterate through instructions.
335 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
336 // Is it a label.
337 if (I->isDebugLabel() && !MMI->isDbgLabelUsed(I->getOperand(0).getImm())){
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.
342 if (PriorLabel) {
343 // Remap the current label to prior label.
344 MMI->RemapLabel(NextLabel, PriorLabel);
345 // Delete the current label.
346 I = BB->erase(I);
347 // Indicate a change has been made.
348 MadeChange = true;
349 continue;
350 } else {
351 // Start a new round.
352 PriorLabel = NextLabel;
354 } else {
355 // No consecutive labels.
356 PriorLabel = 0;
359 ++I;
363 return MadeChange;
366 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }