LoopAligner.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //===-- LoopAligner.cpp - Loop aligner pass. ------------------------------===//
  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 file implements the pass that align loop headers to target specific
  11. // alignment boundary.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #define DEBUG_TYPE "loopalign"
  15. #include "llvm/CodeGen/MachineLoopInfo.h"
  16. #include "llvm/CodeGen/MachineFunctionPass.h"
  17. #include "llvm/CodeGen/Passes.h"
  18. #include "llvm/Target/TargetLowering.h"
  19. #include "llvm/Target/TargetMachine.h"
  20. #include "llvm/Support/Compiler.h"
  21. #include "llvm/Support/Debug.h"
  22. using namespace llvm;
  23. namespace {
  24. class LoopAligner : public MachineFunctionPass {
  25. public:
  26. static char ID;
  27. LoopAligner() : MachineFunctionPass(&ID) {}
  28. virtual bool runOnMachineFunction(MachineFunction &MF);
  29. virtual const char *getPassName() const { return "Loop aligner"; }
  30. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  31. AU.addRequired<MachineLoopInfo>();
  32. AU.addPreserved<MachineLoopInfo>();
  33. MachineFunctionPass::getAnalysisUsage(AU);
  34. }
  35. };
  36. char LoopAligner::ID = 0;
  37. } // end anonymous namespace
  38. FunctionPass *llvm::createLoopAlignerPass() { return new LoopAligner(); }
  39. bool LoopAligner::runOnMachineFunction(MachineFunction &MF) {
  40. const MachineLoopInfo *MLI = &getAnalysis<MachineLoopInfo>();
  41. if (MLI->empty())
  42. return false; // No loops.
  43. const TargetLowering *TLI = MF.getTarget().getTargetLowering();
  44. if (!TLI)
  45. return false;
  46. unsigned Align = TLI->getPrefLoopAlignment();
  47. if (!Align)
  48. return false; // Don't care about loop alignment.
  49. for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
  50. MachineBasicBlock *MBB = I;
  51. if (MLI->isLoopHeader(MBB))
  52. MBB->setAlignment(Align);
  53. }
  54. return true;
  55. }