blob: efafdd0072896a00145fe3bd429fbff27e1ae8d6 [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/Compiler.h"
20#include "llvm/Support/Debug.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000021#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025
26using namespace llvm;
27
Chandler Carruth84e68b22014-04-22 02:41:26 +000028#define DEBUG_TYPE "hexagon_cfg"
29
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000030namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000031 FunctionPass *createHexagonCFGOptimizer();
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000032 void initializeHexagonCFGOptimizerPass(PassRegistry&);
33}
34
35
Tony Linthicum1213a7a2011-12-12 21:14:40 +000036namespace {
37
38class HexagonCFGOptimizer : public MachineFunctionPass {
39
40private:
Tony Linthicum1213a7a2011-12-12 21:14:40 +000041 void InvertAndChangeJumpTarget(MachineInstr*, MachineBasicBlock*);
42
43 public:
44 static char ID;
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
Craig Topper906c2cd2014-04-29 07:58:16 +000049 const char *getPassName() const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000050 return "Hexagon CFG Optimizer";
51 }
Craig Topper906c2cd2014-04-29 07:58:16 +000052 bool runOnMachineFunction(MachineFunction &Fn) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053};
54
55
56char HexagonCFGOptimizer::ID = 0;
57
58static bool IsConditionalBranch(int Opc) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000059 return (Opc == Hexagon::J2_jumpt) || (Opc == Hexagon::J2_jumpf)
60 || (Opc == Hexagon::J2_jumptnewpt) || (Opc == Hexagon::J2_jumpfnewpt);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000061}
62
63
64static bool IsUnconditionalJump(int Opc) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000065 return (Opc == Hexagon::J2_jump);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000066}
67
68
69void
70HexagonCFGOptimizer::InvertAndChangeJumpTarget(MachineInstr* MI,
71 MachineBasicBlock* NewTarget) {
Eric Christopher5c3376a2015-02-02 18:46:27 +000072 const TargetInstrInfo *TII =
73 MI->getParent()->getParent()->getSubtarget().getInstrInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +000074 int NewOpcode = 0;
75 switch(MI->getOpcode()) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000076 case Hexagon::J2_jumpt:
77 NewOpcode = Hexagon::J2_jumpf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000078 break;
79
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000080 case Hexagon::J2_jumpf:
81 NewOpcode = Hexagon::J2_jumpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000082 break;
83
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000084 case Hexagon::J2_jumptnewpt:
85 NewOpcode = Hexagon::J2_jumpfnewpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000086 break;
87
Colin LeMahieudb0b13c2014-12-10 21:24:10 +000088 case Hexagon::J2_jumpfnewpt:
89 NewOpcode = Hexagon::J2_jumptnewpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000090 break;
91
92 default:
Craig Toppere55c5562012-02-07 02:50:20 +000093 llvm_unreachable("Cannot handle this case");
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094 }
95
Eric Christopher5c3376a2015-02-02 18:46:27 +000096 MI->setDesc(TII->get(NewOpcode));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000097 MI->getOperand(1).setMBB(NewTarget);
98}
99
100
101bool HexagonCFGOptimizer::runOnMachineFunction(MachineFunction &Fn) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000102 // Loop over all of the basic blocks.
103 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
104 MBBb != MBBe; ++MBBb) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000105 MachineBasicBlock *MBB = &*MBBb;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000106
107 // Traverse the basic block.
108 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
109 if (MII != MBB->end()) {
110 MachineInstr *MI = MII;
111 int Opc = MI->getOpcode();
112 if (IsConditionalBranch(Opc)) {
113
114 //
115 // (Case 1) Transform the code if the following condition occurs:
116 // BB1: if (p0) jump BB3
117 // ...falls-through to BB2 ...
118 // BB2: jump BB4
119 // ...next block in layout is BB3...
120 // BB3: ...
121 //
122 // Transform this to:
123 // BB1: if (!p0) jump BB4
124 // Remove BB2
125 // BB3: ...
126 //
127 // (Case 2) A variation occurs when BB3 contains a JMP to BB4:
128 // BB1: if (p0) jump BB3
129 // ...falls-through to BB2 ...
130 // BB2: jump BB4
131 // ...other basic blocks ...
132 // BB4:
133 // ...not a fall-thru
134 // BB3: ...
135 // jump BB4
136 //
137 // Transform this to:
138 // BB1: if (!p0) jump BB4
139 // Remove BB2
140 // BB3: ...
141 // BB4: ...
142 //
143 unsigned NumSuccs = MBB->succ_size();
144 MachineBasicBlock::succ_iterator SI = MBB->succ_begin();
145 MachineBasicBlock* FirstSucc = *SI;
146 MachineBasicBlock* SecondSucc = *(++SI);
Craig Topper062a2ba2014-04-25 05:30:21 +0000147 MachineBasicBlock* LayoutSucc = nullptr;
148 MachineBasicBlock* JumpAroundTarget = nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000149
150 if (MBB->isLayoutSuccessor(FirstSucc)) {
151 LayoutSucc = FirstSucc;
152 JumpAroundTarget = SecondSucc;
153 } else if (MBB->isLayoutSuccessor(SecondSucc)) {
154 LayoutSucc = SecondSucc;
155 JumpAroundTarget = FirstSucc;
156 } else {
157 // Odd case...cannot handle.
158 }
159
160 // The target of the unconditional branch must be JumpAroundTarget.
161 // TODO: If not, we should not invert the unconditional branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000162 MachineBasicBlock* CondBranchTarget = nullptr;
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000163 if ((MI->getOpcode() == Hexagon::J2_jumpt) ||
164 (MI->getOpcode() == Hexagon::J2_jumpf)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000165 CondBranchTarget = MI->getOperand(1).getMBB();
166 }
167
168 if (!LayoutSucc || (CondBranchTarget != JumpAroundTarget)) {
169 continue;
170 }
171
172 if ((NumSuccs == 2) && LayoutSucc && (LayoutSucc->pred_size() == 1)) {
173
174 // Ensure that BB2 has one instruction -- an unconditional jump.
175 if ((LayoutSucc->size() == 1) &&
176 IsUnconditionalJump(LayoutSucc->front().getOpcode())) {
177 MachineBasicBlock* UncondTarget =
178 LayoutSucc->front().getOperand(0).getMBB();
179 // Check if the layout successor of BB2 is BB3.
180 bool case1 = LayoutSucc->isLayoutSuccessor(JumpAroundTarget);
181 bool case2 = JumpAroundTarget->isSuccessor(UncondTarget) &&
182 JumpAroundTarget->size() >= 1 &&
183 IsUnconditionalJump(JumpAroundTarget->back().getOpcode()) &&
184 JumpAroundTarget->pred_size() == 1 &&
185 JumpAroundTarget->succ_size() == 1;
186
187 if (case1 || case2) {
188 InvertAndChangeJumpTarget(MI, UncondTarget);
Cong Houd97c1002015-12-01 05:29:22 +0000189 MBB->replaceSuccessor(JumpAroundTarget, UncondTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000190
191 // Remove the unconditional branch in LayoutSucc.
192 LayoutSucc->erase(LayoutSucc->begin());
Cong Houd97c1002015-12-01 05:29:22 +0000193 LayoutSucc->replaceSuccessor(UncondTarget, JumpAroundTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000194
195 // This code performs the conversion for case 2, which moves
196 // the block to the fall-thru case (BB3 in the code above).
197 if (case2 && !case1) {
198 JumpAroundTarget->moveAfter(LayoutSucc);
199 // only move a block if it doesn't have a fall-thru. otherwise
200 // the CFG will be incorrect.
201 if (!UncondTarget->canFallThrough()) {
202 UncondTarget->moveAfter(JumpAroundTarget);
203 }
204 }
205
206 //
207 // Correct live-in information. Is used by post-RA scheduler
208 // The live-in to LayoutSucc is now all values live-in to
209 // JumpAroundTarget.
210 //
Matthias Braund9da1622015-09-09 18:08:03 +0000211 std::vector<MachineBasicBlock::RegisterMaskPair> OrigLiveIn(
212 LayoutSucc->livein_begin(), LayoutSucc->livein_end());
213 std::vector<MachineBasicBlock::RegisterMaskPair> NewLiveIn(
214 JumpAroundTarget->livein_begin(),
215 JumpAroundTarget->livein_end());
216 for (const auto &OrigLI : OrigLiveIn)
217 LayoutSucc->removeLiveIn(OrigLI.PhysReg);
218 for (const auto &NewLI : NewLiveIn)
219 LayoutSucc->addLiveIn(NewLI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000220 }
221 }
222 }
223 }
224 }
225 }
226 return true;
227}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000228}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000229
230
231//===----------------------------------------------------------------------===//
232// Public Constructor Functions
233//===----------------------------------------------------------------------===//
234
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +0000235static void initializePassOnce(PassRegistry &Registry) {
236 PassInfo *PI = new PassInfo("Hexagon CFG Optimizer", "hexagon-cfg",
Craig Topper062a2ba2014-04-25 05:30:21 +0000237 &HexagonCFGOptimizer::ID, nullptr, false, false);
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +0000238 Registry.registerPass(*PI, true);
239}
240
241void llvm::initializeHexagonCFGOptimizerPass(PassRegistry &Registry) {
242 CALL_ONCE_INITIALIZATION(initializePassOnce)
243}
244
Eric Christopher5c3376a2015-02-02 18:46:27 +0000245FunctionPass *llvm::createHexagonCFGOptimizer() {
246 return new HexagonCFGOptimizer();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000247}