diff options
author | Matt Arsenault <Matthew.Arsenault@amd.com> | 2016-11-01 18:34:00 +0000 |
---|---|---|
committer | Matt Arsenault <Matthew.Arsenault@amd.com> | 2016-11-01 18:34:00 +0000 |
commit | cb578f84e091e9198b25c687d0f5f1dc558f27c5 (patch) | |
tree | eabbc54a43a78c21d23dcf5db4a55197ced67b22 /llvm/lib/CodeGen/BranchRelaxation.cpp | |
parent | 90c117a61b7e061c837cfb625fe0b1d1bc51707a (diff) | |
download | bcm5719-llvm-cb578f84e091e9198b25c687d0f5f1dc558f27c5.tar.gz bcm5719-llvm-cb578f84e091e9198b25c687d0f5f1dc558f27c5.zip |
BranchRelaxation: Expand unconditional branches first
It's likely if a conditional branch needs to be expanded, the following
unconditional branch will also need expansion. By expanding the
unconditional branch first, the conditional branch can be simply
inverted to jump over the inserted indirect branch block. If the
conditional branch is expanded first, it results in an additional
branch.
This avoids test regressions in future commits.
llvm-svn: 285722
Diffstat (limited to 'llvm/lib/CodeGen/BranchRelaxation.cpp')
-rw-r--r-- | llvm/lib/CodeGen/BranchRelaxation.cpp | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/llvm/lib/CodeGen/BranchRelaxation.cpp b/llvm/lib/CodeGen/BranchRelaxation.cpp index a36906540a2..ddf8797d479 100644 --- a/llvm/lib/CodeGen/BranchRelaxation.cpp +++ b/llvm/lib/CodeGen/BranchRelaxation.cpp @@ -404,6 +404,28 @@ bool BranchRelaxation::relaxBranchInstructions() { for (MachineFunction::iterator I = MF->begin(); I != MF->end(); ++I) { MachineBasicBlock &MBB = *I; + auto Last = MBB.rbegin(); + if (Last == MBB.rend()) // Empty block. + continue; + + // Expand the unconditional branch first if necessary. If there is a + // conditional branch, this will end up changing the branch destination of + // it to be over the newly inserted indirect branch block, which may avoid + // the need to try expanding the conditional branch first, saving an extra + // jump. + if (Last->isUnconditionalBranch()) { + // Unconditional branch destination might be unanalyzable, assume these + // are OK. + if (MachineBasicBlock *DestBB = TII->getBranchDestBlock(*Last)) { + if (!isBlockInRange(*Last, *DestBB)) { + fixupUnconditionalBranch(*Last); + ++NumUnconditionalRelaxed; + Changed = true; + } + } + } + + // Loop over the conditional branches. MachineBasicBlock::iterator Next; for (MachineBasicBlock::iterator J = MBB.getFirstTerminator(); J != MBB.end(); J = Next) { @@ -437,21 +459,6 @@ bool BranchRelaxation::relaxBranchInstructions() { Next = MBB.getFirstTerminator(); } } - - if (MI.isUnconditionalBranch()) { - // Unconditional branch destination might be unanalyzable, assume these - // are OK. - if (MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI)) { - if (!isBlockInRange(MI, *DestBB)) { - fixupUnconditionalBranch(MI); - ++NumUnconditionalRelaxed; - Changed = true; - } - } - - // Unconditional branch is the last terminator. - break; - } } } |