xref: /llvm-project/llvm/lib/Target/Hexagon/HexagonCFGOptimizer.cpp (revision 7e8bc5cf77bdda9e32b984b3fa91953361f24abb)
1 //===- HexagonCFGOptimizer.cpp - CFG optimizations ------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Hexagon.h"
10 #include "MCTargetDesc/HexagonMCTargetDesc.h"
11 #include "llvm/CodeGen/MachineBasicBlock.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"
20 #include <cassert>
21 #include <vector>
22 
23 using namespace llvm;
24 
25 #define DEBUG_TYPE "hexagon_cfg"
26 
27 namespace llvm {
28 
29 FunctionPass *createHexagonCFGOptimizer();
30 void initializeHexagonCFGOptimizerPass(PassRegistry&);
31 
32 } // end namespace llvm
33 
34 namespace {
35 
36 class HexagonCFGOptimizer : public MachineFunctionPass {
37 private:
38   void InvertAndChangeJumpTarget(MachineInstr &, MachineBasicBlock *);
39   bool isOnFallThroughPath(MachineBasicBlock *MBB);
40 
41 public:
42   static char ID;
43 
44   HexagonCFGOptimizer() : MachineFunctionPass(ID) {
45     initializeHexagonCFGOptimizerPass(*PassRegistry::getPassRegistry());
46   }
47 
48   StringRef getPassName() const override { return "Hexagon CFG Optimizer"; }
49   bool runOnMachineFunction(MachineFunction &Fn) override;
50 
51   MachineFunctionProperties getRequiredProperties() const override {
52     return MachineFunctionProperties().set(
53         MachineFunctionProperties::Property::NoVRegs);
54   }
55 };
56 
57 } // end anonymous namespace
58 
59 char HexagonCFGOptimizer::ID = 0;
60 
61 static bool IsConditionalBranch(int Opc) {
62   switch (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:
71       return true;
72   }
73   return false;
74 }
75 
76 static bool IsUnconditionalJump(int Opc) {
77   return (Opc == Hexagon::J2_jump);
78 }
79 
80 void HexagonCFGOptimizer::InvertAndChangeJumpTarget(
81     MachineInstr &MI, MachineBasicBlock *NewTarget) {
82   const TargetInstrInfo *TII =
83       MI.getParent()->getParent()->getSubtarget().getInstrInfo();
84   int NewOpcode = 0;
85   switch (MI.getOpcode()) {
86   case Hexagon::J2_jumpt:
87     NewOpcode = Hexagon::J2_jumpf;
88     break;
89   case Hexagon::J2_jumpf:
90     NewOpcode = Hexagon::J2_jumpt;
91     break;
92   case Hexagon::J2_jumptnewpt:
93     NewOpcode = Hexagon::J2_jumpfnewpt;
94     break;
95   case Hexagon::J2_jumpfnewpt:
96     NewOpcode = Hexagon::J2_jumptnewpt;
97     break;
98   default:
99     llvm_unreachable("Cannot handle this case");
100   }
101 
102   MI.setDesc(TII->get(NewOpcode));
103   MI.getOperand(1).setMBB(NewTarget);
104 }
105 
106 bool HexagonCFGOptimizer::isOnFallThroughPath(MachineBasicBlock *MBB) {
107   if (MBB->canFallThrough())
108     return true;
109   for (MachineBasicBlock *PB : MBB->predecessors())
110     if (PB->isLayoutSuccessor(MBB) && PB->canFallThrough())
111       return true;
112   return false;
113 }
114 
115 bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) {
116   if (skipFunction(Fn.getFunction()))
117     return false;
118 
119   // Loop over all of the basic blocks.
120   for (MachineBasicBlock &MBB : Fn) {
121     // Traverse the basic block.
122     MachineBasicBlock::iterator MII = MBB.getFirstTerminator();
123     if (MII != MBB.end()) {
124       MachineInstr &MI = *MII;
125       int Opc = MI.getOpcode();
126       if (IsConditionalBranch(Opc)) {
127         // (Case 1) Transform the code if the following condition occurs:
128         //   BB1: if (p0) jump BB3
129         //   ...falls-through to BB2 ...
130         //   BB2: jump BB4
131         //   ...next block in layout is BB3...
132         //   BB3: ...
133         //
134         //  Transform this to:
135         //  BB1: if (!p0) jump BB4
136         //  Remove BB2
137         //  BB3: ...
138         //
139         // (Case 2) A variation occurs when BB3 contains a JMP to BB4:
140         //   BB1: if (p0) jump BB3
141         //   ...falls-through to BB2 ...
142         //   BB2: jump BB4
143         //   ...other basic blocks ...
144         //   BB4:
145         //   ...not a fall-thru
146         //   BB3: ...
147         //     jump BB4
148         //
149         // Transform this to:
150         //   BB1: if (!p0) jump BB4
151         //   Remove BB2
152         //   BB3: ...
153         //   BB4: ...
154         unsigned NumSuccs = MBB.succ_size();
155         MachineBasicBlock::succ_iterator SI = MBB.succ_begin();
156         MachineBasicBlock* FirstSucc = *SI;
157         MachineBasicBlock* SecondSucc = *(++SI);
158         MachineBasicBlock* LayoutSucc = nullptr;
159         MachineBasicBlock* JumpAroundTarget = nullptr;
160 
161         if (MBB.isLayoutSuccessor(FirstSucc)) {
162           LayoutSucc = FirstSucc;
163           JumpAroundTarget = SecondSucc;
164         } else if (MBB.isLayoutSuccessor(SecondSucc)) {
165           LayoutSucc = SecondSucc;
166           JumpAroundTarget = FirstSucc;
167         } else {
168           // Odd case...cannot handle.
169         }
170 
171         // The target of the unconditional branch must be JumpAroundTarget.
172         // TODO: If not, we should not invert the unconditional branch.
173         MachineBasicBlock* CondBranchTarget = nullptr;
174         if (MI.getOpcode() == Hexagon::J2_jumpt ||
175             MI.getOpcode() == Hexagon::J2_jumpf) {
176           CondBranchTarget = MI.getOperand(1).getMBB();
177         }
178 
179         if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) {
180           continue;
181         }
182 
183         if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) {
184           // Ensure that BB2 has one instruction -- an unconditional jump.
185           if ((LayoutSucc->size() == 1) &&
186               IsUnconditionalJump(LayoutSucc->front().getOpcode())) {
187             assert(JumpAroundTarget && "jump target is needed to process second basic block");
188             MachineBasicBlock* UncondTarget =
189               LayoutSucc->front().getOperand(0).getMBB();
190             // Check if the layout successor of BB2 is BB3.
191             bool case1 = LayoutSucc->isLayoutSuccessor(JumpAroundTarget);
192             bool case2 = JumpAroundTarget->isSuccessor(UncondTarget) &&
193               !JumpAroundTarget->empty() &&
194               IsUnconditionalJump(JumpAroundTarget->back().getOpcode()) &&
195               JumpAroundTarget->pred_size() == 1 &&
196               JumpAroundTarget->succ_size() == 1;
197 
198             if (case1 || case2) {
199               InvertAndChangeJumpTarget(MI, UncondTarget);
200               MBB.replaceSuccessor(JumpAroundTarget, UncondTarget);
201 
202               // Remove the unconditional branch in LayoutSucc.
203               LayoutSucc->erase(LayoutSucc->begin());
204               LayoutSucc->replaceSuccessor(UncondTarget, JumpAroundTarget);
205 
206               // This code performs the conversion for case 2, which moves
207               // the block to the fall-thru case (BB3 in the code above).
208               if (case2 && !case1) {
209                 JumpAroundTarget->moveAfter(LayoutSucc);
210                 // only move a block if it doesn't have a fall-thru. otherwise
211                 // the CFG will be incorrect.
212                 if (!isOnFallThroughPath(UncondTarget))
213                   UncondTarget->moveAfter(JumpAroundTarget);
214               }
215 
216               // Correct live-in information. Is used by post-RA scheduler
217               // The live-in to LayoutSucc is now all values live-in to
218               // JumpAroundTarget.
219               std::vector<MachineBasicBlock::RegisterMaskPair> OrigLiveIn(
220                   LayoutSucc->livein_begin(), LayoutSucc->livein_end());
221               std::vector<MachineBasicBlock::RegisterMaskPair> NewLiveIn(
222                   JumpAroundTarget->livein_begin(),
223                   JumpAroundTarget->livein_end());
224               for (const auto &OrigLI : OrigLiveIn)
225                 LayoutSucc->removeLiveIn(OrigLI.PhysReg);
226               for (const auto &NewLI : NewLiveIn)
227                 LayoutSucc->addLiveIn(NewLI);
228             }
229           }
230         }
231       }
232     }
233   }
234   return true;
235 }
236 
237 //===----------------------------------------------------------------------===//
238 //                         Public Constructor Functions
239 //===----------------------------------------------------------------------===//
240 
241 INITIALIZE_PASS(HexagonCFGOptimizer, "hexagon-cfg", "Hexagon CFG Optimizer",
242                 false, false)
243 
244 FunctionPass *llvm::createHexagonCFGOptimizer() {
245   return new HexagonCFGOptimizer();
246 }
247