Get rid of one more non-DebugLoc getNode and
[oota-llvm.git] / lib / CodeGen / LoopAligner.cpp
index a40bb50565d3863bc520d726ea4bab6e06beaf9a..b67f5c3bf9192e0a9a444a813a4331d26a316102 100644 (file)
@@ -24,11 +24,9 @@ using namespace llvm;
 
 namespace {
   class LoopAligner : public MachineFunctionPass {
-    const TargetLowering *TLI;
-
   public:
     static char ID;
-    LoopAligner() : MachineFunctionPass((intptr_t)&ID) {}
+    LoopAligner() : MachineFunctionPass(&ID) {}
 
     virtual bool runOnMachineFunction(MachineFunction &MF);
     virtual const char *getPassName() const { return "Loop aligner"; }
@@ -36,6 +34,7 @@ namespace {
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.addRequired<MachineLoopInfo>();
       AU.addPreserved<MachineLoopInfo>();
+      AU.addPreservedID(MachineDominatorsID);
       MachineFunctionPass::getAnalysisUsage(AU);
     }
   };
@@ -48,17 +47,31 @@ FunctionPass *llvm::createLoopAlignerPass() { return new LoopAligner(); }
 bool LoopAligner::runOnMachineFunction(MachineFunction &MF) {
   const MachineLoopInfo *MLI = &getAnalysis<MachineLoopInfo>();
 
-  if (MLI->begin() == MLI->end())
+  if (MLI->empty())
     return false;  // No loops.
 
-  unsigned Align = MF.getTarget().getTargetLowering()->getPrefLoopAlignment();
+  const TargetLowering *TLI = MF.getTarget().getTargetLowering();
+  if (!TLI)
+    return false;
+
+  unsigned Align = TLI->getPrefLoopAlignment();
   if (!Align)
     return false;  // Don't care about loop alignment.
 
+  const Function *F = MF.getFunction();
+  if (F->hasFnAttr(Attribute::OptimizeForSize))
+    return false;
+
   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
     MachineBasicBlock *MBB = I;
-    if (MLI->isLoopHeader(MBB))
+    if (MLI->isLoopHeader(MBB)) {
+      MachineBasicBlock *PredBB = prior(I);
+      if (MLI->getLoopFor(MBB) == MLI->getLoopFor(PredBB))
+        // If previously BB is in the same loop, don't align this BB. We want
+        // to prevent adding noop's inside a loop.
+        continue;
       MBB->setAlignment(Align);
+    }
   }
 
   return true;