InstructionNamer.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //===- InstructionNamer.cpp - Give anonymous instructions names -----------===//
  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. //
  10. // This is a little utility pass that gives instructions names, this is mostly
  11. // useful when diffing the effect of an optimization because deleting an
  12. // unnamed instruction can change all other instruction numbering, making the
  13. // diff very noisy.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Transforms/Scalar.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/IR/Type.h"
  19. #include "llvm/Pass.h"
  20. using namespace llvm;
  21. namespace {
  22. struct InstNamer : public FunctionPass {
  23. static char ID; // Pass identification, replacement for typeid
  24. InstNamer() : FunctionPass(ID) {
  25. initializeInstNamerPass(*PassRegistry::getPassRegistry());
  26. }
  27. void getAnalysisUsage(AnalysisUsage &Info) const override {
  28. Info.setPreservesAll();
  29. }
  30. bool runOnFunction(Function &F) override {
  31. for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
  32. AI != AE; ++AI)
  33. if (!AI->hasName() && !AI->getType()->isVoidTy())
  34. AI->setName("arg");
  35. for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
  36. if (!BB->hasName())
  37. BB->setName("bb");
  38. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
  39. if (!I->hasName() && !I->getType()->isVoidTy())
  40. I->setName("tmp");
  41. }
  42. return true;
  43. }
  44. };
  45. char InstNamer::ID = 0;
  46. }
  47. INITIALIZE_PASS(InstNamer, "instnamer",
  48. "Assign names to anonymous instructions", false, false)
  49. char &llvm::InstructionNamerID = InstNamer::ID;
  50. //===----------------------------------------------------------------------===//
  51. //
  52. // InstructionNamer - Give any unnamed non-void instructions "tmp" names.
  53. //
  54. FunctionPass *llvm::createInstructionNamerPass() {
  55. return new InstNamer();
  56. }