blob: 03b9c631578bcb7c3cbf0ca0d9806991fa3cee3b [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonCFGOptimizer.cpp - CFG optimizations -----------------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002// The LLVM Compiler Infrastructure
3//
4// This file is distributed under the University of Illinois Open Source
5// License. See LICENSE.TXT for details.
6//
7//===----------------------------------------------------------------------===//
8
Chandler Carruthed0881b2012-12-03 16:50:05 +00009#include "Hexagon.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000010#include "HexagonMachineFunctionInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "HexagonSubtarget.h"
12#include "HexagonTargetMachine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000013#include "llvm/CodeGen/MachineDominators.h"
14#include "llvm/CodeGen/MachineFunctionPass.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000015#include "llvm/CodeGen/MachineInstrBuilder.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000016#include "llvm/CodeGen/MachineLoopInfo.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000018#include "llvm/CodeGen/Passes.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000019#include "llvm/Support/Debug.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Target/TargetInstrInfo.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000024
25using namespace llvm;
26
Chandler Carruth84e68b22014-04-22 02:41:26 +000027#define DEBUG_TYPE "hexagon_cfg"
28
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000029namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000030 FunctionPass *createHexagonCFGOptimizer();
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000031 void initializeHexagonCFGOptimizerPass(PassRegistry&);
32}
33
34
Tony Linthicum1213a7a2011-12-12 21:14:40 +000035namespace {
36
37class HexagonCFGOptimizer : public MachineFunctionPass {
38
39private:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000040 void InvertAndChangeJumpTarget(MachineInstr &, MachineBasicBlock *);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000041
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000042public:
Tony Linthicum1213a7a2011-12-12 21:14:40 +000043 static char ID;
Eric Christopher5c3376a2015-02-02 18:46:27 +000044 HexagonCFGOptimizer() : MachineFunctionPass(ID) {
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000045 initializeHexagonCFGOptimizerPass(*PassRegistry::getPassRegistry());
46 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +000047
Craig Topper906c2cd2014-04-29 07:58:16 +000048 const char *getPassName() const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000049 return "Hexagon CFG Optimizer";
50 }
Craig Topper906c2cd2014-04-29 07:58:16 +000051 bool runOnMachineFunction(MachineFunction &Fn) override;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000052 MachineFunctionProperties getRequiredProperties() const override {
53 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000054 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000055 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +000056};
57
58
59char HexagonCFGOptimizer::ID = 0;
60
61static bool IsConditionalBranch(int Opc) {
Krzysztof Parzyszeka243adf2016-08-19 14:14:09 +000062 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;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000074}
75
76
77static bool IsUnconditionalJump(int Opc) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000078 return (Opc == Hexagon::J2_jump);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000079}
80
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000081void HexagonCFGOptimizer::InvertAndChangeJumpTarget(
82 MachineInstr &MI, MachineBasicBlock *NewTarget) {
Eric Christopher5c3376a2015-02-02 18:46:27 +000083 const TargetInstrInfo *TII =
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000084 MI.getParent()->getParent()->getSubtarget().getInstrInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +000085 int NewOpcode = 0;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000086 switch (MI.getOpcode()) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000087 case Hexagon::J2_jumpt:
88 NewOpcode = Hexagon::J2_jumpf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000089 break;
90
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000091 case Hexagon::J2_jumpf:
92 NewOpcode = Hexagon::J2_jumpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093 break;
94
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000095 case Hexagon::J2_jumptnewpt:
96 NewOpcode = Hexagon::J2_jumpfnewpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000097 break;
98
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000099 case Hexagon::J2_jumpfnewpt:
100 NewOpcode = Hexagon::J2_jumptnewpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000101 break;
102
103 default:
Craig Toppere55c5562012-02-07 02:50:20 +0000104 llvm_unreachable("Cannot handle this case");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105 }
106
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000107 MI.setDesc(TII->get(NewOpcode));
108 MI.getOperand(1).setMBB(NewTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109}
110
111
112bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000113 if (skipFunction(*Fn.getFunction()))
114 return false;
115
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000116 // Loop over all of the basic blocks.
117 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
118 MBBb != MBBe; ++MBBb) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000119 MachineBasicBlock *MBB = &*MBBb;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000120
121 // Traverse the basic block.
122 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
123 if (MII != MBB->end()) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000124 MachineInstr &MI = *MII;
125 int Opc = MI.getOpcode();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000126 if (IsConditionalBranch(Opc)) {
127
128 //
129 // (Case 1) Transform the code if the following condition occurs:
130 // BB1: if (p0) jump BB3
131 // ...falls-through to BB2 ...
132 // BB2: jump BB4
133 // ...next block in layout is BB3...
134 // BB3: ...
135 //
136 // Transform this to:
137 // BB1: if (!p0) jump BB4
138 // Remove BB2
139 // BB3: ...
140 //
141 // (Case 2) A variation occurs when BB3 contains a JMP to BB4:
142 // BB1: if (p0) jump BB3
143 // ...falls-through to BB2 ...
144 // BB2: jump BB4
145 // ...other basic blocks ...
146 // BB4:
147 // ...not a fall-thru
148 // BB3: ...
149 // jump BB4
150 //
151 // Transform this to:
152 // BB1: if (!p0) jump BB4
153 // Remove BB2
154 // BB3: ...
155 // BB4: ...
156 //
157 unsigned NumSuccs = MBB->succ_size();
158 MachineBasicBlock::succ_iterator SI = MBB->succ_begin();
159 MachineBasicBlock* FirstSucc = *SI;
160 MachineBasicBlock* SecondSucc = *(++SI);
Craig Topper062a2ba2014-04-25 05:30:21 +0000161 MachineBasicBlock* LayoutSucc = nullptr;
162 MachineBasicBlock* JumpAroundTarget = nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000163
164 if (MBB->isLayoutSuccessor(FirstSucc)) {
165 LayoutSucc = FirstSucc;
166 JumpAroundTarget = SecondSucc;
167 } else if (MBB->isLayoutSuccessor(SecondSucc)) {
168 LayoutSucc = SecondSucc;
169 JumpAroundTarget = FirstSucc;
170 } else {
171 // Odd case...cannot handle.
172 }
173
174 // The target of the unconditional branch must be JumpAroundTarget.
175 // TODO: If not, we should not invert the unconditional branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000176 MachineBasicBlock* CondBranchTarget = nullptr;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000177 if (MI.getOpcode() == Hexagon::J2_jumpt ||
178 MI.getOpcode() == Hexagon::J2_jumpf) {
179 CondBranchTarget = MI.getOperand(1).getMBB();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000180 }
181
182 if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) {
183 continue;
184 }
185
186 if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) {
187
188 // Ensure that BB2 has one instruction -- an unconditional jump.
189 if ((LayoutSucc->size() == 1) &&
190 IsUnconditionalJump(LayoutSucc->front().getOpcode())) {
Krzysztof Parzyszek89757432016-05-05 22:00:44 +0000191 assert(JumpAroundTarget && "jump target is needed to process second basic block");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000192 MachineBasicBlock* UncondTarget =
193 LayoutSucc->front().getOperand(0).getMBB();
194 // Check if the layout successor of BB2 is BB3.
195 bool case1 = LayoutSucc->isLayoutSuccessor(JumpAroundTarget);
196 bool case2 = JumpAroundTarget->isSuccessor(UncondTarget) &&
197 JumpAroundTarget->size() >= 1 &&
198 IsUnconditionalJump(JumpAroundTarget->back().getOpcode()) &&
199 JumpAroundTarget->pred_size() == 1 &&
200 JumpAroundTarget->succ_size() == 1;
201
202 if (case1 || case2) {
203 InvertAndChangeJumpTarget(MI, UncondTarget);
Cong Houd97c1002015-12-01 05:29:22 +0000204 MBB->replaceSuccessor(JumpAroundTarget, UncondTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000205
206 // Remove the unconditional branch in LayoutSucc.
207 LayoutSucc->erase(LayoutSucc->begin());
Cong Houd97c1002015-12-01 05:29:22 +0000208 LayoutSucc->replaceSuccessor(UncondTarget, JumpAroundTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000209
210 // This code performs the conversion for case 2, which moves
211 // the block to the fall-thru case (BB3 in the code above).
212 if (case2 && !case1) {
213 JumpAroundTarget->moveAfter(LayoutSucc);
214 // only move a block if it doesn't have a fall-thru. otherwise
215 // the CFG will be incorrect.
216 if (!UncondTarget->canFallThrough()) {
217 UncondTarget->moveAfter(JumpAroundTarget);
218 }
219 }
220
221 //
222 // Correct live-in information. Is used by post-RA scheduler
223 // The live-in to LayoutSucc is now all values live-in to
224 // JumpAroundTarget.
225 //
Matthias Braund9da1622015-09-09 18:08:03 +0000226 std::vector<MachineBasicBlock::RegisterMaskPair> OrigLiveIn(
227 LayoutSucc->livein_begin(), LayoutSucc->livein_end());
228 std::vector<MachineBasicBlock::RegisterMaskPair> NewLiveIn(
229 JumpAroundTarget->livein_begin(),
230 JumpAroundTarget->livein_end());
231 for (const auto &OrigLI : OrigLiveIn)
232 LayoutSucc->removeLiveIn(OrigLI.PhysReg);
233 for (const auto &NewLI : NewLiveIn)
234 LayoutSucc->addLiveIn(NewLI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000235 }
236 }
237 }
238 }
239 }
240 }
241 return true;
242}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000243}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000244
245
246//===----------------------------------------------------------------------===//
247// Public Constructor Functions
248//===----------------------------------------------------------------------===//
249
Chandler Carruthd4741442016-06-03 10:13:29 +0000250INITIALIZE_PASS(HexagonCFGOptimizer, "hexagon-cfg", "Hexagon CFG Optimizer",
251 false, false)
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +0000252
Eric Christopher5c3376a2015-02-02 18:46:27 +0000253FunctionPass *llvm::createHexagonCFGOptimizer() {
254 return new HexagonCFGOptimizer();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000255}