annotate llvm/lib/Target/Hexagon/HexagonCFGOptimizer.cpp @ 207:2e18cbf3894f

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