1 //===- HexagonCFGOptimizer.cpp - CFG optimizations ------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "llvm/CodeGen/MachineBasicBlock.h"
11 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
12 #include "llvm/CodeGen/MachineFunction.h"
13 #include "llvm/CodeGen/MachineFunctionPass.h"
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/CodeGen/MachineOperand.h"
16 #include "llvm/CodeGen/TargetInstrInfo.h"
17 #include "llvm/CodeGen/TargetSubtargetInfo.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/ErrorHandling.h"
25 #define DEBUG_TYPE "hexagon_cfg"
29 FunctionPass
*createHexagonCFGOptimizer();
30 void initializeHexagonCFGOptimizerPass(PassRegistry
&);
32 } // end namespace llvm
36 class HexagonCFGOptimizer
: public MachineFunctionPass
{
38 void InvertAndChangeJumpTarget(MachineInstr
&, MachineBasicBlock
*);
39 bool isOnFallThroughPath(MachineBasicBlock
*MBB
);
44 HexagonCFGOptimizer() : MachineFunctionPass(ID
) {
45 initializeHexagonCFGOptimizerPass(*PassRegistry::getPassRegistry());
48 StringRef
getPassName() const override
{ return "Hexagon CFG Optimizer"; }
49 bool runOnMachineFunction(MachineFunction
&Fn
) override
;
51 MachineFunctionProperties
getRequiredProperties() const override
{
52 return MachineFunctionProperties().set(
53 MachineFunctionProperties::Property::NoVRegs
);
57 } // end anonymous namespace
59 char HexagonCFGOptimizer::ID
= 0;
61 static bool IsConditionalBranch(int Opc
) {
63 case Hexagon::J2_jumpt
:
64 case Hexagon::J2_jumptpt
:
65 case Hexagon::J2_jumpf
:
66 case Hexagon::J2_jumpfpt
:
67 case Hexagon::J2_jumptnew
:
68 case Hexagon::J2_jumpfnew
:
69 case Hexagon::J2_jumptnewpt
:
70 case Hexagon::J2_jumpfnewpt
:
76 static bool IsUnconditionalJump(int Opc
) {
77 return (Opc
== Hexagon::J2_jump
);
80 void HexagonCFGOptimizer::InvertAndChangeJumpTarget(
81 MachineInstr
&MI
, MachineBasicBlock
*NewTarget
) {
82 const TargetInstrInfo
*TII
=
83 MI
.getParent()->getParent()->getSubtarget().getInstrInfo();
85 switch (MI
.getOpcode()) {
86 case Hexagon::J2_jumpt
:
87 NewOpcode
= Hexagon::J2_jumpf
;
89 case Hexagon::J2_jumpf
:
90 NewOpcode
= Hexagon::J2_jumpt
;
92 case Hexagon::J2_jumptnewpt
:
93 NewOpcode
= Hexagon::J2_jumpfnewpt
;
95 case Hexagon::J2_jumpfnewpt
:
96 NewOpcode
= Hexagon::J2_jumptnewpt
;
99 llvm_unreachable("Cannot handle this case");
102 MI
.setDesc(TII
->get(NewOpcode
));
103 MI
.getOperand(1).setMBB(NewTarget
);
106 bool HexagonCFGOptimizer::isOnFallThroughPath(MachineBasicBlock
*MBB
) {
107 if (MBB
->canFallThrough())
109 for (MachineBasicBlock
*PB
: MBB
->predecessors())
110 if (PB
->isLayoutSuccessor(MBB
) && PB
->canFallThrough())
115 bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction
&Fn
) {
116 if (skipFunction(Fn
.getFunction()))
119 // Loop over all of the basic blocks.
120 for (MachineFunction::iterator MBBb
= Fn
.begin(), MBBe
= Fn
.end();
121 MBBb
!= MBBe
; ++MBBb
) {
122 MachineBasicBlock
*MBB
= &*MBBb
;
124 // Traverse the basic block.
125 MachineBasicBlock::iterator MII
= MBB
->getFirstTerminator();
126 if (MII
!= MBB
->end()) {
127 MachineInstr
&MI
= *MII
;
128 int Opc
= MI
.getOpcode();
129 if (IsConditionalBranch(Opc
)) {
130 // (Case 1) Transform the code if the following condition occurs:
131 // BB1: if (p0) jump BB3
132 // ...falls-through to BB2 ...
134 // ...next block in layout is BB3...
137 // Transform this to:
138 // BB1: if (!p0) jump BB4
142 // (Case 2) A variation occurs when BB3 contains a JMP to BB4:
143 // BB1: if (p0) jump BB3
144 // ...falls-through to BB2 ...
146 // ...other basic blocks ...
148 // ...not a fall-thru
152 // Transform this to:
153 // BB1: if (!p0) jump BB4
157 unsigned NumSuccs
= MBB
->succ_size();
158 MachineBasicBlock::succ_iterator SI
= MBB
->succ_begin();
159 MachineBasicBlock
* FirstSucc
= *SI
;
160 MachineBasicBlock
* SecondSucc
= *(++SI
);
161 MachineBasicBlock
* LayoutSucc
= nullptr;
162 MachineBasicBlock
* JumpAroundTarget
= nullptr;
164 if (MBB
->isLayoutSuccessor(FirstSucc
)) {
165 LayoutSucc
= FirstSucc
;
166 JumpAroundTarget
= SecondSucc
;
167 } else if (MBB
->isLayoutSuccessor(SecondSucc
)) {
168 LayoutSucc
= SecondSucc
;
169 JumpAroundTarget
= FirstSucc
;
171 // Odd case...cannot handle.
174 // The target of the unconditional branch must be JumpAroundTarget.
175 // TODO: If not, we should not invert the unconditional branch.
176 MachineBasicBlock
* CondBranchTarget
= nullptr;
177 if (MI
.getOpcode() == Hexagon::J2_jumpt
||
178 MI
.getOpcode() == Hexagon::J2_jumpf
) {
179 CondBranchTarget
= MI
.getOperand(1).getMBB();
182 if (!LayoutSucc
|| (CondBranchTarget
!= JumpAroundTarget
)) {
186 if ((NumSuccs
== 2) && LayoutSucc
&& (LayoutSucc
->pred_size() == 1)) {
187 // Ensure that BB2 has one instruction -- an unconditional jump.
188 if ((LayoutSucc
->size() == 1) &&
189 IsUnconditionalJump(LayoutSucc
->front().getOpcode())) {
190 assert(JumpAroundTarget
&& "jump target is needed to process second basic block");
191 MachineBasicBlock
* UncondTarget
=
192 LayoutSucc
->front().getOperand(0).getMBB();
193 // Check if the layout successor of BB2 is BB3.
194 bool case1
= LayoutSucc
->isLayoutSuccessor(JumpAroundTarget
);
195 bool case2
= JumpAroundTarget
->isSuccessor(UncondTarget
) &&
196 !JumpAroundTarget
->empty() &&
197 IsUnconditionalJump(JumpAroundTarget
->back().getOpcode()) &&
198 JumpAroundTarget
->pred_size() == 1 &&
199 JumpAroundTarget
->succ_size() == 1;
201 if (case1
|| case2
) {
202 InvertAndChangeJumpTarget(MI
, UncondTarget
);
203 MBB
->replaceSuccessor(JumpAroundTarget
, UncondTarget
);
205 // Remove the unconditional branch in LayoutSucc.
206 LayoutSucc
->erase(LayoutSucc
->begin());
207 LayoutSucc
->replaceSuccessor(UncondTarget
, JumpAroundTarget
);
209 // This code performs the conversion for case 2, which moves
210 // the block to the fall-thru case (BB3 in the code above).
211 if (case2
&& !case1
) {
212 JumpAroundTarget
->moveAfter(LayoutSucc
);
213 // only move a block if it doesn't have a fall-thru. otherwise
214 // the CFG will be incorrect.
215 if (!isOnFallThroughPath(UncondTarget
))
216 UncondTarget
->moveAfter(JumpAroundTarget
);
219 // Correct live-in information. Is used by post-RA scheduler
220 // The live-in to LayoutSucc is now all values live-in to
222 std::vector
<MachineBasicBlock::RegisterMaskPair
> OrigLiveIn(
223 LayoutSucc
->livein_begin(), LayoutSucc
->livein_end());
224 std::vector
<MachineBasicBlock::RegisterMaskPair
> NewLiveIn(
225 JumpAroundTarget
->livein_begin(),
226 JumpAroundTarget
->livein_end());
227 for (const auto &OrigLI
: OrigLiveIn
)
228 LayoutSucc
->removeLiveIn(OrigLI
.PhysReg
);
229 for (const auto &NewLI
: NewLiveIn
)
230 LayoutSucc
->addLiveIn(NewLI
);
240 //===----------------------------------------------------------------------===//
241 // Public Constructor Functions
242 //===----------------------------------------------------------------------===//
244 INITIALIZE_PASS(HexagonCFGOptimizer
, "hexagon-cfg", "Hexagon CFG Optimizer",
247 FunctionPass
*llvm::createHexagonCFGOptimizer() {
248 return new HexagonCFGOptimizer();