Code refactoring.
authorEvan Cheng <evan.cheng@apple.com>
Thu, 7 May 2009 05:49:39 +0000 (05:49 +0000)
committerEvan Cheng <evan.cheng@apple.com>
Thu, 7 May 2009 05:49:39 +0000 (05:49 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71151 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CodePlacementOpt.cpp

index 54121afefda09bd8b3c1cbea9891cf59f0f638b2..924a96930b591f23be4de134d1229b7875b3c2b4 100644 (file)
@@ -24,6 +24,8 @@ using namespace llvm;
 
 namespace {
   class CodePlacementOpt : public MachineFunctionPass {
+    const MachineLoopInfo *MLI;
+
   public:
     static char ID;
     CodePlacementOpt() : MachineFunctionPass(&ID) {}
@@ -39,6 +41,9 @@ namespace {
       AU.addPreservedID(MachineDominatorsID);
       MachineFunctionPass::getAnalysisUsage(AU);
     }
+
+  private:
+    bool AlignLoops(MachineFunction &MF);
   };
 
   char CodePlacementOpt::ID = 0;
@@ -48,12 +53,9 @@ FunctionPass *llvm::createCodePlacementOptPass() {
   return new CodePlacementOpt();
 }
 
-bool CodePlacementOpt::runOnMachineFunction(MachineFunction &MF) {
-  const MachineLoopInfo *MLI = &getAnalysis<MachineLoopInfo>();
-
-  if (MLI->empty())
-    return false;  // No loops.
-
+/// AlignLoops - Align loop headers to target preferred alignments.
+///
+bool CodePlacementOpt::AlignLoops(MachineFunction &MF) {
   const TargetLowering *TLI = MF.getTarget().getTargetLowering();
   if (!TLI)
     return false;
@@ -66,6 +68,7 @@ bool CodePlacementOpt::runOnMachineFunction(MachineFunction &MF) {
   if (F->hasFnAttr(Attribute::OptimizeForSize))
     return false;
 
+  bool Changed = false;
   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
     MachineBasicBlock *MBB = I;
     if (MLI->isLoopHeader(MBB)) {
@@ -75,8 +78,20 @@ bool CodePlacementOpt::runOnMachineFunction(MachineFunction &MF) {
         // to prevent adding noop's inside a loop.
         continue;
       MBB->setAlignment(Align);
+      Changed = true;
     }
   }
 
-  return true;
+  return Changed;
+}
+
+bool CodePlacementOpt::runOnMachineFunction(MachineFunction &MF) {
+  MLI = &getAnalysis<MachineLoopInfo>();
+  if (MLI->empty())
+    return false;  // No loops.
+
+  bool Changed = false;
+  Changed |= AlignLoops(MF);
+
+  return Changed;
 }