blob: a22ac8c9fdf5296cbd572a34336a661fcbef062e [file] [log] [blame]
Eugene Zelenko3b873362017-09-28 22:27:31 +00001//===- HexagonCFGOptimizer.cpp - CFG optimizations ------------------------===//
2//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruthed0881b2012-12-03 16:50:05 +000010#include "Hexagon.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000011#include "llvm/CodeGen/MachineBasicBlock.h"
12#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
13#include "llvm/CodeGen/MachineFunction.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000014#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000015#include "llvm/CodeGen/MachineInstr.h"
16#include "llvm/CodeGen/MachineOperand.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000017#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000018#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000019#include "llvm/Pass.h"
20#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000021#include <cassert>
22#include <vector>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023
24using namespace llvm;
25
Chandler Carruth84e68b22014-04-22 02:41:26 +000026#define DEBUG_TYPE "hexagon_cfg"
27
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000028namespace llvm {
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000029
Eugene Zelenko3b873362017-09-28 22:27:31 +000030FunctionPass *createHexagonCFGOptimizer();
31void initializeHexagonCFGOptimizerPass(PassRegistry&);
32
33} // end namespace llvm
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000034
Tony Linthicum1213a7a2011-12-12 21:14:40 +000035namespace {
36
37class HexagonCFGOptimizer : public MachineFunctionPass {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000038private:
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000039 void InvertAndChangeJumpTarget(MachineInstr &, MachineBasicBlock *);
Krzysztof Parzyszek0b3acbb2017-04-28 21:54:11 +000040 bool isOnFallThroughPath(MachineBasicBlock *MBB);
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;
Eugene Zelenko3b873362017-09-28 22:27:31 +000044
Eric Christopher5c3376a2015-02-02 18:46:27 +000045 HexagonCFGOptimizer() : MachineFunctionPass(ID) {
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000046 initializeHexagonCFGOptimizerPass(*PassRegistry::getPassRegistry());
47 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +000048
Mehdi Amini117296c2016-10-01 02:56:57 +000049 StringRef getPassName() const override { return "Hexagon CFG Optimizer"; }
Craig Topper906c2cd2014-04-29 07:58:16 +000050 bool runOnMachineFunction(MachineFunction &Fn) override;
Eugene Zelenko3b873362017-09-28 22:27:31 +000051
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
Eugene Zelenko3b873362017-09-28 22:27:31 +000058} // end anonymous namespace
Tony Linthicum1213a7a2011-12-12 21:14:40 +000059
60char HexagonCFGOptimizer::ID = 0;
61
62static bool IsConditionalBranch(int Opc) {
Krzysztof Parzyszeka243adf2016-08-19 14:14:09 +000063 switch (Opc) {
64 case Hexagon::J2_jumpt:
65 case Hexagon::J2_jumptpt:
66 case Hexagon::J2_jumpf:
67 case Hexagon::J2_jumpfpt:
68 case Hexagon::J2_jumptnew:
69 case Hexagon::J2_jumpfnew:
70 case Hexagon::J2_jumptnewpt:
71 case Hexagon::J2_jumpfnewpt:
72 return true;
73 }
74 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000075}
76
Tony Linthicum1213a7a2011-12-12 21:14:40 +000077static 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;
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000090 case Hexagon::J2_jumpf:
91 NewOpcode = Hexagon::J2_jumpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000092 break;
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;
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000096 case Hexagon::J2_jumpfnewpt:
97 NewOpcode = Hexagon::J2_jumptnewpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000098 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000099 default:
Craig Toppere55c5562012-02-07 02:50:20 +0000100 llvm_unreachable("Cannot handle this case");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000101 }
102
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000103 MI.setDesc(TII->get(NewOpcode));
104 MI.getOperand(1).setMBB(NewTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105}
106
Krzysztof Parzyszek0b3acbb2017-04-28 21:54:11 +0000107bool HexagonCFGOptimizer::isOnFallThroughPath(MachineBasicBlock *MBB) {
108 if (MBB->canFallThrough())
109 return true;
110 for (MachineBasicBlock *PB : MBB->predecessors())
111 if (PB->isLayoutSuccessor(MBB) && PB->canFallThrough())
112 return true;
113 return false;
114}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000115
116bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000117 if (skipFunction(Fn.getFunction()))
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000118 return false;
119
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000120 // Loop over all of the basic blocks.
121 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
122 MBBb != MBBe; ++MBBb) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000123 MachineBasicBlock *MBB = &*MBBb;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000124
125 // Traverse the basic block.
126 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
127 if (MII != MBB->end()) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000128 MachineInstr &MI = *MII;
129 int Opc = MI.getOpcode();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000130 if (IsConditionalBranch(Opc)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000131 // (Case 1) Transform the code if the following condition occurs:
132 // BB1: if (p0) jump BB3
133 // ...falls-through to BB2 ...
134 // BB2: jump BB4
135 // ...next block in layout is BB3...
136 // BB3: ...
137 //
138 // Transform this to:
139 // BB1: if (!p0) jump BB4
140 // Remove BB2
141 // BB3: ...
142 //
143 // (Case 2) A variation occurs when BB3 contains a JMP to BB4:
144 // BB1: if (p0) jump BB3
145 // ...falls-through to BB2 ...
146 // BB2: jump BB4
147 // ...other basic blocks ...
148 // BB4:
149 // ...not a fall-thru
150 // BB3: ...
151 // jump BB4
152 //
153 // Transform this to:
154 // BB1: if (!p0) jump BB4
155 // Remove BB2
156 // BB3: ...
157 // BB4: ...
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000158 unsigned NumSuccs = MBB->succ_size();
159 MachineBasicBlock::succ_iterator SI = MBB->succ_begin();
160 MachineBasicBlock* FirstSucc = *SI;
161 MachineBasicBlock* SecondSucc = *(++SI);
Craig Topper062a2ba2014-04-25 05:30:21 +0000162 MachineBasicBlock* LayoutSucc = nullptr;
163 MachineBasicBlock* JumpAroundTarget = nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000164
165 if (MBB->isLayoutSuccessor(FirstSucc)) {
166 LayoutSucc = FirstSucc;
167 JumpAroundTarget = SecondSucc;
168 } else if (MBB->isLayoutSuccessor(SecondSucc)) {
169 LayoutSucc = SecondSucc;
170 JumpAroundTarget = FirstSucc;
171 } else {
172 // Odd case...cannot handle.
173 }
174
175 // The target of the unconditional branch must be JumpAroundTarget.
176 // TODO: If not, we should not invert the unconditional branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000177 MachineBasicBlock* CondBranchTarget = nullptr;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000178 if (MI.getOpcode() == Hexagon::J2_jumpt ||
179 MI.getOpcode() == Hexagon::J2_jumpf) {
180 CondBranchTarget = MI.getOperand(1).getMBB();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000181 }
182
183 if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) {
184 continue;
185 }
186
187 if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000188 // 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) &&
Eugene Zelenko3b873362017-09-28 22:27:31 +0000197 !JumpAroundTarget->empty() &&
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000198 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.
Krzysztof Parzyszek0b3acbb2017-04-28 21:54:11 +0000216 if (!isOnFallThroughPath(UncondTarget))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000217 UncondTarget->moveAfter(JumpAroundTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000218 }
219
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000220 // 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.
Matthias Braund9da1622015-09-09 18:08:03 +0000223 std::vector<MachineBasicBlock::RegisterMaskPair> OrigLiveIn(
224 LayoutSucc->livein_begin(), LayoutSucc->livein_end());
225 std::vector<MachineBasicBlock::RegisterMaskPair> NewLiveIn(
226 JumpAroundTarget->livein_begin(),
227 JumpAroundTarget->livein_end());
228 for (const auto &OrigLI : OrigLiveIn)
229 LayoutSucc->removeLiveIn(OrigLI.PhysReg);
230 for (const auto &NewLI : NewLiveIn)
231 LayoutSucc->addLiveIn(NewLI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000232 }
233 }
234 }
235 }
236 }
237 }
238 return true;
239}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000240
241//===----------------------------------------------------------------------===//
242// Public Constructor Functions
243//===----------------------------------------------------------------------===//
244
Chandler Carruthd4741442016-06-03 10:13:29 +0000245INITIALIZE_PASS(HexagonCFGOptimizer, "hexagon-cfg", "Hexagon CFG Optimizer",
246 false, false)
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +0000247
Eric Christopher5c3376a2015-02-02 18:46:27 +0000248FunctionPass *llvm::createHexagonCFGOptimizer() {
249 return new HexagonCFGOptimizer();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000250}