blob: 2f8fe6e087f587fe3d6716758d09eaa518128054 [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
Mehdi Amini117296c2016-10-01 02:56:57 +000048 StringRef getPassName() const override { return "Hexagon CFG Optimizer"; }
Craig Topper906c2cd2014-04-29 07:58:16 +000049 bool runOnMachineFunction(MachineFunction &Fn) override;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000050 MachineFunctionProperties getRequiredProperties() const override {
51 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000052 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000053 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +000054};
55
56
57char HexagonCFGOptimizer::ID = 0;
58
59static bool IsConditionalBranch(int Opc) {
Krzysztof Parzyszeka243adf2016-08-19 14:14:09 +000060 switch (Opc) {
61 case Hexagon::J2_jumpt:
62 case Hexagon::J2_jumptpt:
63 case Hexagon::J2_jumpf:
64 case Hexagon::J2_jumpfpt:
65 case Hexagon::J2_jumptnew:
66 case Hexagon::J2_jumpfnew:
67 case Hexagon::J2_jumptnewpt:
68 case Hexagon::J2_jumpfnewpt:
69 return true;
70 }
71 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000072}
73
74
75static bool IsUnconditionalJump(int Opc) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000076 return (Opc == Hexagon::J2_jump);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000077}
78
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000079void HexagonCFGOptimizer::InvertAndChangeJumpTarget(
80 MachineInstr &MI, MachineBasicBlock *NewTarget) {
Eric Christopher5c3376a2015-02-02 18:46:27 +000081 const TargetInstrInfo *TII =
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000082 MI.getParent()->getParent()->getSubtarget().getInstrInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +000083 int NewOpcode = 0;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000084 switch (MI.getOpcode()) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000085 case Hexagon::J2_jumpt:
86 NewOpcode = Hexagon::J2_jumpf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000087 break;
88
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000089 case Hexagon::J2_jumpf:
90 NewOpcode = Hexagon::J2_jumpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000091 break;
92
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000093 case Hexagon::J2_jumptnewpt:
94 NewOpcode = Hexagon::J2_jumpfnewpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000095 break;
96
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000097 case Hexagon::J2_jumpfnewpt:
98 NewOpcode = Hexagon::J2_jumptnewpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000099 break;
100
101 default:
Craig Toppere55c5562012-02-07 02:50:20 +0000102 llvm_unreachable("Cannot handle this case");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103 }
104
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000105 MI.setDesc(TII->get(NewOpcode));
106 MI.getOperand(1).setMBB(NewTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000107}
108
109
110bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000111 if (skipFunction(*Fn.getFunction()))
112 return false;
113
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000114 // Loop over all of the basic blocks.
115 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
116 MBBb != MBBe; ++MBBb) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000117 MachineBasicBlock *MBB = &*MBBb;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000118
119 // Traverse the basic block.
120 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
121 if (MII != MBB->end()) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000122 MachineInstr &MI = *MII;
123 int Opc = MI.getOpcode();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000124 if (IsConditionalBranch(Opc)) {
125
126 //
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 //
155 unsigned NumSuccs = MBB->succ_size();
156 MachineBasicBlock::succ_iterator SI = MBB->succ_begin();
157 MachineBasicBlock* FirstSucc = *SI;
158 MachineBasicBlock* SecondSucc = *(++SI);
Craig Topper062a2ba2014-04-25 05:30:21 +0000159 MachineBasicBlock* LayoutSucc = nullptr;
160 MachineBasicBlock* JumpAroundTarget = nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000161
162 if (MBB->isLayoutSuccessor(FirstSucc)) {
163 LayoutSucc = FirstSucc;
164 JumpAroundTarget = SecondSucc;
165 } else if (MBB->isLayoutSuccessor(SecondSucc)) {
166 LayoutSucc = SecondSucc;
167 JumpAroundTarget = FirstSucc;
168 } else {
169 // Odd case...cannot handle.
170 }
171
172 // The target of the unconditional branch must be JumpAroundTarget.
173 // TODO: If not, we should not invert the unconditional branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000174 MachineBasicBlock* CondBranchTarget = nullptr;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000175 if (MI.getOpcode() == Hexagon::J2_jumpt ||
176 MI.getOpcode() == Hexagon::J2_jumpf) {
177 CondBranchTarget = MI.getOperand(1).getMBB();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000178 }
179
180 if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) {
181 continue;
182 }
183
184 if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) {
185
186 // Ensure that BB2 has one instruction -- an unconditional jump.
187 if ((LayoutSucc->size() == 1) &&
188 IsUnconditionalJump(LayoutSucc->front().getOpcode())) {
Krzysztof Parzyszek89757432016-05-05 22:00:44 +0000189 assert(JumpAroundTarget && "jump target is needed to process second basic block");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000190 MachineBasicBlock* UncondTarget =
191 LayoutSucc->front().getOperand(0).getMBB();
192 // Check if the layout successor of BB2 is BB3.
193 bool case1 = LayoutSucc->isLayoutSuccessor(JumpAroundTarget);
194 bool case2 = JumpAroundTarget->isSuccessor(UncondTarget) &&
195 JumpAroundTarget->size() >= 1 &&
196 IsUnconditionalJump(JumpAroundTarget->back().getOpcode()) &&
197 JumpAroundTarget->pred_size() == 1 &&
198 JumpAroundTarget->succ_size() == 1;
199
200 if (case1 || case2) {
201 InvertAndChangeJumpTarget(MI, UncondTarget);
Cong Houd97c1002015-12-01 05:29:22 +0000202 MBB->replaceSuccessor(JumpAroundTarget, UncondTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000203
204 // Remove the unconditional branch in LayoutSucc.
205 LayoutSucc->erase(LayoutSucc->begin());
Cong Houd97c1002015-12-01 05:29:22 +0000206 LayoutSucc->replaceSuccessor(UncondTarget, JumpAroundTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000207
208 // This code performs the conversion for case 2, which moves
209 // the block to the fall-thru case (BB3 in the code above).
210 if (case2 && !case1) {
211 JumpAroundTarget->moveAfter(LayoutSucc);
212 // only move a block if it doesn't have a fall-thru. otherwise
213 // the CFG will be incorrect.
214 if (!UncondTarget->canFallThrough()) {
215 UncondTarget->moveAfter(JumpAroundTarget);
216 }
217 }
218
219 //
220 // Correct live-in information. Is used by post-RA scheduler
221 // The live-in to LayoutSucc is now all values live-in to
222 // JumpAroundTarget.
223 //
Matthias Braund9da1622015-09-09 18:08:03 +0000224 std::vector<MachineBasicBlock::RegisterMaskPair> OrigLiveIn(
225 LayoutSucc->livein_begin(), LayoutSucc->livein_end());
226 std::vector<MachineBasicBlock::RegisterMaskPair> NewLiveIn(
227 JumpAroundTarget->livein_begin(),
228 JumpAroundTarget->livein_end());
229 for (const auto &OrigLI : OrigLiveIn)
230 LayoutSucc->removeLiveIn(OrigLI.PhysReg);
231 for (const auto &NewLI : NewLiveIn)
232 LayoutSucc->addLiveIn(NewLI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000233 }
234 }
235 }
236 }
237 }
238 }
239 return true;
240}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000241}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000242
243
244//===----------------------------------------------------------------------===//
245// Public Constructor Functions
246//===----------------------------------------------------------------------===//
247
Chandler Carruthd4741442016-06-03 10:13:29 +0000248INITIALIZE_PASS(HexagonCFGOptimizer, "hexagon-cfg", "Hexagon CFG Optimizer",
249 false, false)
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +0000250
Eric Christopher5c3376a2015-02-02 18:46:27 +0000251FunctionPass *llvm::createHexagonCFGOptimizer() {
252 return new HexagonCFGOptimizer();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000253}