annotate llvm/lib/Target/Hexagon/HexagonCFGOptimizer.cpp @ 152:e8a9b4f4d755

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