MachineModuleInfo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. //===----------------------------------------------------------------------===//
  9. #include "llvm/CodeGen/MachineModuleInfo.h"
  10. #include "llvm/Constants.h"
  11. #include "llvm/Analysis/ValueTracking.h"
  12. #include "llvm/CodeGen/MachineFunctionPass.h"
  13. #include "llvm/CodeGen/MachineFunction.h"
  14. #include "llvm/CodeGen/Passes.h"
  15. #include "llvm/Target/TargetInstrInfo.h"
  16. #include "llvm/Target/TargetMachine.h"
  17. #include "llvm/Target/TargetOptions.h"
  18. #include "llvm/DerivedTypes.h"
  19. #include "llvm/GlobalVariable.h"
  20. #include "llvm/Intrinsics.h"
  21. #include "llvm/Instructions.h"
  22. #include "llvm/Module.h"
  23. #include "llvm/Support/Dwarf.h"
  24. #include "llvm/Support/ErrorHandling.h"
  25. #include "llvm/Support/Streams.h"
  26. using namespace llvm;
  27. using namespace llvm::dwarf;
  28. // Handle the Pass registration stuff necessary to use TargetData's.
  29. static RegisterPass<MachineModuleInfo>
  30. X("machinemoduleinfo", "Module Information");
  31. char MachineModuleInfo::ID = 0;
  32. //===----------------------------------------------------------------------===//
  33. MachineModuleInfo::MachineModuleInfo()
  34. : ImmutablePass(&ID)
  35. , LabelIDList()
  36. , FrameMoves()
  37. , LandingPads()
  38. , Personalities()
  39. , CallsEHReturn(0)
  40. , CallsUnwindInit(0)
  41. , DbgInfoAvailable(false)
  42. {
  43. // Always emit "no personality" info
  44. Personalities.push_back(NULL);
  45. }
  46. MachineModuleInfo::~MachineModuleInfo() {
  47. }
  48. /// doInitialization - Initialize the state for a new module.
  49. ///
  50. bool MachineModuleInfo::doInitialization() {
  51. return false;
  52. }
  53. /// doFinalization - Tear down the state after completion of a module.
  54. ///
  55. bool MachineModuleInfo::doFinalization() {
  56. return false;
  57. }
  58. /// BeginFunction - Begin gathering function meta information.
  59. ///
  60. void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
  61. // Coming soon.
  62. }
  63. /// EndFunction - Discard function meta information.
  64. ///
  65. void MachineModuleInfo::EndFunction() {
  66. // Clean up frame info.
  67. FrameMoves.clear();
  68. // Clean up exception info.
  69. LandingPads.clear();
  70. TypeInfos.clear();
  71. FilterIds.clear();
  72. FilterEnds.clear();
  73. CallsEHReturn = 0;
  74. CallsUnwindInit = 0;
  75. }
  76. /// AnalyzeModule - Scan the module for global debug information.
  77. ///
  78. void MachineModuleInfo::AnalyzeModule(Module &M) {
  79. // Insert functions in the llvm.used array into UsedFunctions.
  80. GlobalVariable *GV = M.getGlobalVariable("llvm.used");
  81. if (!GV || !GV->hasInitializer()) return;
  82. // Should be an array of 'i8*'.
  83. ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
  84. if (InitList == 0) return;
  85. for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
  86. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
  87. if (CE->getOpcode() == Instruction::BitCast)
  88. if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
  89. UsedFunctions.insert(F);
  90. }
  91. }
  92. //===-EH-------------------------------------------------------------------===//
  93. /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
  94. /// specified MachineBasicBlock.
  95. LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
  96. (MachineBasicBlock *LandingPad) {
  97. unsigned N = LandingPads.size();
  98. for (unsigned i = 0; i < N; ++i) {
  99. LandingPadInfo &LP = LandingPads[i];
  100. if (LP.LandingPadBlock == LandingPad)
  101. return LP;
  102. }
  103. LandingPads.push_back(LandingPadInfo(LandingPad));
  104. return LandingPads[N];
  105. }
  106. /// addInvoke - Provide the begin and end labels of an invoke style call and
  107. /// associate it with a try landing pad block.
  108. void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
  109. unsigned BeginLabel, unsigned EndLabel) {
  110. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  111. LP.BeginLabels.push_back(BeginLabel);
  112. LP.EndLabels.push_back(EndLabel);
  113. }
  114. /// addLandingPad - Provide the label of a try LandingPad block.
  115. ///
  116. unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
  117. unsigned LandingPadLabel = NextLabelID();
  118. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  119. LP.LandingPadLabel = LandingPadLabel;
  120. return LandingPadLabel;
  121. }
  122. /// addPersonality - Provide the personality function for the exception
  123. /// information.
  124. void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
  125. Function *Personality) {
  126. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  127. LP.Personality = Personality;
  128. for (unsigned i = 0; i < Personalities.size(); ++i)
  129. if (Personalities[i] == Personality)
  130. return;
  131. Personalities.push_back(Personality);
  132. }
  133. /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
  134. ///
  135. void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
  136. std::vector<GlobalVariable *> &TyInfo) {
  137. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  138. for (unsigned N = TyInfo.size(); N; --N)
  139. LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
  140. }
  141. /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
  142. ///
  143. void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
  144. std::vector<GlobalVariable *> &TyInfo) {
  145. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  146. std::vector<unsigned> IdsInFilter(TyInfo.size());
  147. for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
  148. IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
  149. LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
  150. }
  151. /// addCleanup - Add a cleanup action for a landing pad.
  152. ///
  153. void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
  154. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  155. LP.TypeIds.push_back(0);
  156. }
  157. /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
  158. /// pads.
  159. void MachineModuleInfo::TidyLandingPads() {
  160. for (unsigned i = 0; i != LandingPads.size(); ) {
  161. LandingPadInfo &LandingPad = LandingPads[i];
  162. LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
  163. // Special case: we *should* emit LPs with null LP MBB. This indicates
  164. // "nounwind" case.
  165. if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
  166. LandingPads.erase(LandingPads.begin() + i);
  167. continue;
  168. }
  169. for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
  170. unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
  171. unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
  172. if (!BeginLabel || !EndLabel) {
  173. LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
  174. LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
  175. continue;
  176. }
  177. LandingPad.BeginLabels[j] = BeginLabel;
  178. LandingPad.EndLabels[j] = EndLabel;
  179. ++j;
  180. }
  181. // Remove landing pads with no try-ranges.
  182. if (LandingPads[i].BeginLabels.empty()) {
  183. LandingPads.erase(LandingPads.begin() + i);
  184. continue;
  185. }
  186. // If there is no landing pad, ensure that the list of typeids is empty.
  187. // If the only typeid is a cleanup, this is the same as having no typeids.
  188. if (!LandingPad.LandingPadBlock ||
  189. (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
  190. LandingPad.TypeIds.clear();
  191. ++i;
  192. }
  193. }
  194. /// getTypeIDFor - Return the type id for the specified typeinfo. This is
  195. /// function wide.
  196. unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
  197. for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
  198. if (TypeInfos[i] == TI) return i + 1;
  199. TypeInfos.push_back(TI);
  200. return TypeInfos.size();
  201. }
  202. /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
  203. /// function wide.
  204. int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
  205. // If the new filter coincides with the tail of an existing filter, then
  206. // re-use the existing filter. Folding filters more than this requires
  207. // re-ordering filters and/or their elements - probably not worth it.
  208. for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
  209. E = FilterEnds.end(); I != E; ++I) {
  210. unsigned i = *I, j = TyIds.size();
  211. while (i && j)
  212. if (FilterIds[--i] != TyIds[--j])
  213. goto try_next;
  214. if (!j)
  215. // The new filter coincides with range [i, end) of the existing filter.
  216. return -(1 + i);
  217. try_next:;
  218. }
  219. // Add the new filter.
  220. int FilterID = -(1 + FilterIds.size());
  221. FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
  222. for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
  223. FilterIds.push_back(TyIds[I]);
  224. FilterEnds.push_back(FilterIds.size());
  225. FilterIds.push_back(0); // terminator
  226. return FilterID;
  227. }
  228. /// getPersonality - Return the personality function for the current function.
  229. Function *MachineModuleInfo::getPersonality() const {
  230. // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
  231. // function
  232. return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
  233. }
  234. /// getPersonalityIndex - Return unique index for current personality
  235. /// function. NULL personality function should always get zero index.
  236. unsigned MachineModuleInfo::getPersonalityIndex() const {
  237. const Function* Personality = NULL;
  238. // Scan landing pads. If there is at least one non-NULL personality - use it.
  239. for (unsigned i = 0; i != LandingPads.size(); ++i)
  240. if (LandingPads[i].Personality) {
  241. Personality = LandingPads[i].Personality;
  242. break;
  243. }
  244. for (unsigned i = 0; i < Personalities.size(); ++i) {
  245. if (Personalities[i] == Personality)
  246. return i;
  247. }
  248. // This should never happen
  249. LLVM_UNREACHABLE("Personality function should be set!");
  250. return 0;
  251. }
  252. //===----------------------------------------------------------------------===//
  253. /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
  254. /// a info consumer to determine if the range of two labels is empty, by seeing
  255. /// if the labels map to the same reduced label.
  256. namespace llvm {
  257. struct DebugLabelFolder : public MachineFunctionPass {
  258. static char ID;
  259. DebugLabelFolder() : MachineFunctionPass(&ID) {}
  260. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  261. AU.addPreservedID(MachineLoopInfoID);
  262. AU.addPreservedID(MachineDominatorsID);
  263. MachineFunctionPass::getAnalysisUsage(AU);
  264. }
  265. virtual bool runOnMachineFunction(MachineFunction &MF);
  266. virtual const char *getPassName() const { return "Label Folder"; }
  267. };
  268. char DebugLabelFolder::ID = 0;
  269. bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
  270. // Get machine module info.
  271. MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
  272. if (!MMI) return false;
  273. // Track if change is made.
  274. bool MadeChange = false;
  275. // No prior label to begin.
  276. unsigned PriorLabel = 0;
  277. // Iterate through basic blocks.
  278. for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
  279. BB != E; ++BB) {
  280. // Iterate through instructions.
  281. for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
  282. // Is it a label.
  283. if (I->isDebugLabel() && !MMI->isDbgLabelUsed(I->getOperand(0).getImm())){
  284. // The label ID # is always operand #0, an immediate.
  285. unsigned NextLabel = I->getOperand(0).getImm();
  286. // If there was an immediate prior label.
  287. if (PriorLabel) {
  288. // Remap the current label to prior label.
  289. MMI->RemapLabel(NextLabel, PriorLabel);
  290. // Delete the current label.
  291. I = BB->erase(I);
  292. // Indicate a change has been made.
  293. MadeChange = true;
  294. continue;
  295. } else {
  296. // Start a new round.
  297. PriorLabel = NextLabel;
  298. }
  299. } else {
  300. // No consecutive labels.
  301. PriorLabel = 0;
  302. }
  303. ++I;
  304. }
  305. }
  306. return MadeChange;
  307. }
  308. FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
  309. }