[PATCH] D22938: AArch64: Consolidate branch inversion logic

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 28 14:35:51 PDT 2016


arsenm created this revision.
arsenm added a subscriber: llvm-commits.
Herald added subscribers: rengolin, aemerson.

https://reviews.llvm.org/D22938

Files:
  lib/Target/AArch64/AArch64BranchRelaxation.cpp

Index: lib/Target/AArch64/AArch64BranchRelaxation.cpp
===================================================================
--- lib/Target/AArch64/AArch64BranchRelaxation.cpp
+++ lib/Target/AArch64/AArch64BranchRelaxation.cpp
@@ -75,8 +75,10 @@
   MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI);
   void adjustBlockOffsets(MachineBasicBlock &MBB);
   bool isBlockInRange(const MachineInstr &MI, const MachineBasicBlock &BB) const;
-  void invertConditionalBranch(MachineInstr &MI) const;
-  unsigned insertInvertedConditionalBranch(MachineBasicBlock &MBB,
+
+  unsigned insertInvertedConditionalBranch(MachineBasicBlock &SrcBB,
+                                           MachineBasicBlock::iterator InsPt,
+                                           const DebugLoc &DL,
                                            const MachineInstr &OldBr,
                                            MachineBasicBlock &NewDestBB) const;
   unsigned insertUnconditionalBranch(MachineBasicBlock &MBB,
@@ -317,25 +319,19 @@
   CCOp.setImm(AArch64CC::getInvertedCondCode(CC));
 }
 
-/// Invert the branch condition of \p MI and change the destination to \p NewB
-void AArch64BranchRelaxation::invertConditionalBranch(MachineInstr &MI) const {
-  MI.setDesc(TII->get(getOppositeConditionOpcode(MI.getOpcode())));
-
-  if (MI.getOpcode() == AArch64::Bcc)
-    invertBccCondition(MI);
-}
-
 /// Insert a conditional branch at the end of \p MBB to \p NewDestBB, using the
 /// inverse condition of branch \p OldBr.
 /// \returns The number of bytes added to the block.
 unsigned AArch64BranchRelaxation::insertInvertedConditionalBranch(
-  MachineBasicBlock &MBB,
+  MachineBasicBlock &SrcMBB,
+  MachineBasicBlock::iterator InsPt,
+  const DebugLoc &DL,
   const MachineInstr &OldBr,
   MachineBasicBlock &NewDestBB) const {
   unsigned OppositeCondOpc = getOppositeConditionOpcode(OldBr.getOpcode());
 
   MachineInstrBuilder MIB =
-    BuildMI(&MBB, OldBr.getDebugLoc(), TII->get(OppositeCondOpc))
+    BuildMI(SrcMBB, InsPt, DL, TII->get(OppositeCondOpc))
     .addOperand(OldBr.getOperand(0));
 
   unsigned Opc = OldBr.getOpcode();
@@ -416,7 +412,14 @@
         DEBUG(dbgs() << "  Invert condition and swap its destination with "
                      << *BMI);
         changeBranchDestBlock(*BMI, *DestBB);
-        invertConditionalBranch(MI);
+
+        int NewSize =
+          insertInvertedConditionalBranch(*MBB, MI.getIterator(),
+                                          MI.getDebugLoc(), MI, *NewDest);
+        int OldSize = TII->getInstSizeInBytes(MI);
+        BlockInfo[MBB->getNumber()].Size += (NewSize - OldSize);
+        MI.eraseFromParent();
+
         changeBranchDestBlock(MI, *NewDest);
         return true;
       }
@@ -449,15 +452,16 @@
                << ", invert condition and change dest. to BB#"
                << NextBB.getNumber() << '\n');
 
+  unsigned &MBBSize = BlockInfo[MBB->getNumber()].Size;
+
   // Insert a new conditional branch and a new unconditional branch.
-  BlockInfo[MBB->getNumber()].Size
-    += insertInvertedConditionalBranch(*MBB, MI, NextBB);
+  MBBSize += insertInvertedConditionalBranch(*MBB, MBB->end(),
+                                             MI.getDebugLoc(), MI, NextBB);
 
-  BlockInfo[MBB->getNumber()].Size +=
-    insertUnconditionalBranch(*MBB, *DestBB, MI.getDebugLoc());
+  MBBSize += insertUnconditionalBranch(*MBB, *DestBB, MI.getDebugLoc());
 
   // Remove the old conditional branch.  It may or may not still be in MBB.
-  BlockInfo[MBB->getNumber()].Size -= TII->getInstSizeInBytes(MI);
+  MBBSize -= TII->getInstSizeInBytes(MI);
   MI.eraseFromParent();
 
   // Finally, keep the block offsets up to date.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22938.66009.patch
Type: text/x-patch
Size: 3685 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160728/3ee02287/attachment.bin>


More information about the llvm-commits mailing list