comparison lib/CodeGen/BranchRelaxation.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children 3a76565eade5
comparison
equal deleted inserted replaced
120:1172e4bd9c6f 121:803732b1fca8
1 //===-- BranchRelaxation.cpp ----------------------------------------------===// 1 //===- BranchRelaxation.cpp -----------------------------------------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 9
10 #include "llvm/CodeGen/Passes.h"
11 #include "llvm/ADT/SmallVector.h" 10 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/Statistic.h" 11 #include "llvm/ADT/Statistic.h"
12 #include "llvm/CodeGen/LivePhysRegs.h"
13 #include "llvm/CodeGen/MachineBasicBlock.h"
14 #include "llvm/CodeGen/MachineFunction.h"
13 #include "llvm/CodeGen/MachineFunctionPass.h" 15 #include "llvm/CodeGen/MachineFunctionPass.h"
16 #include "llvm/CodeGen/MachineInstr.h"
14 #include "llvm/CodeGen/RegisterScavenging.h" 17 #include "llvm/CodeGen/RegisterScavenging.h"
15 #include "llvm/Target/TargetInstrInfo.h" 18 #include "llvm/IR/DebugLoc.h"
16 #include "llvm/Target/TargetSubtargetInfo.h" 19 #include "llvm/Pass.h"
20 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Debug.h" 21 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/Format.h" 22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/MathExtras.h"
19 #include "llvm/Support/raw_ostream.h" 24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetInstrInfo.h"
26 #include "llvm/Target/TargetRegisterInfo.h"
27 #include "llvm/Target/TargetSubtargetInfo.h"
28 #include <cassert>
29 #include <cstdint>
30 #include <iterator>
31 #include <memory>
20 32
21 using namespace llvm; 33 using namespace llvm;
22 34
23 #define DEBUG_TYPE "branch-relaxation" 35 #define DEBUG_TYPE "branch-relaxation"
24 36
27 STATISTIC(NumUnconditionalRelaxed, "Number of unconditional branches relaxed"); 39 STATISTIC(NumUnconditionalRelaxed, "Number of unconditional branches relaxed");
28 40
29 #define BRANCH_RELAX_NAME "Branch relaxation pass" 41 #define BRANCH_RELAX_NAME "Branch relaxation pass"
30 42
31 namespace { 43 namespace {
44
32 class BranchRelaxation : public MachineFunctionPass { 45 class BranchRelaxation : public MachineFunctionPass {
33 /// BasicBlockInfo - Information about the offset and size of a single 46 /// BasicBlockInfo - Information about the offset and size of a single
34 /// basic block. 47 /// basic block.
35 struct BasicBlockInfo { 48 struct BasicBlockInfo {
36 /// Offset - Distance from the beginning of the function to the beginning 49 /// Offset - Distance from the beginning of the function to the beginning
37 /// of this basic block. 50 /// of this basic block.
38 /// 51 ///
39 /// The offset is always aligned as required by the basic block. 52 /// The offset is always aligned as required by the basic block.
40 unsigned Offset; 53 unsigned Offset = 0;
41 54
42 /// Size - Size of the basic block in bytes. If the block contains 55 /// Size - Size of the basic block in bytes. If the block contains
43 /// inline assembly, this is a worst case estimate. 56 /// inline assembly, this is a worst case estimate.
44 /// 57 ///
45 /// The size does not include any alignment padding whether from the 58 /// The size does not include any alignment padding whether from the
46 /// beginning of the block, or from an aligned jump table at the end. 59 /// beginning of the block, or from an aligned jump table at the end.
47 unsigned Size; 60 unsigned Size = 0;
48 61
49 BasicBlockInfo() : Offset(0), Size(0) {} 62 BasicBlockInfo() = default;
50 63
51 /// Compute the offset immediately following this block. \p MBB is the next 64 /// Compute the offset immediately following this block. \p MBB is the next
52 /// block. 65 /// block.
53 unsigned postOffset(const MachineBasicBlock &MBB) const { 66 unsigned postOffset(const MachineBasicBlock &MBB) const {
54 unsigned PO = Offset + Size; 67 unsigned PO = Offset + Size;
67 } 80 }
68 }; 81 };
69 82
70 SmallVector<BasicBlockInfo, 16> BlockInfo; 83 SmallVector<BasicBlockInfo, 16> BlockInfo;
71 std::unique_ptr<RegScavenger> RS; 84 std::unique_ptr<RegScavenger> RS;
85 LivePhysRegs LiveRegs;
72 86
73 MachineFunction *MF; 87 MachineFunction *MF;
88 const TargetRegisterInfo *TRI;
74 const TargetInstrInfo *TII; 89 const TargetInstrInfo *TII;
75 90
76 bool relaxBranchInstructions(); 91 bool relaxBranchInstructions();
77 void scanFunction(); 92 void scanFunction();
78 93
90 void dumpBBs(); 105 void dumpBBs();
91 void verify(); 106 void verify();
92 107
93 public: 108 public:
94 static char ID; 109 static char ID;
95 BranchRelaxation() : MachineFunctionPass(ID) { } 110
111 BranchRelaxation() : MachineFunctionPass(ID) {}
96 112
97 bool runOnMachineFunction(MachineFunction &MF) override; 113 bool runOnMachineFunction(MachineFunction &MF) override;
98 114
99 StringRef getPassName() const override { 115 StringRef getPassName() const override { return BRANCH_RELAX_NAME; }
100 return BRANCH_RELAX_NAME;
101 }
102 }; 116 };
103 117
104 } 118 } // end anonymous namespace
105 119
106 char BranchRelaxation::ID = 0; 120 char BranchRelaxation::ID = 0;
121
107 char &llvm::BranchRelaxationPassID = BranchRelaxation::ID; 122 char &llvm::BranchRelaxationPassID = BranchRelaxation::ID;
108 123
109 INITIALIZE_PASS(BranchRelaxation, DEBUG_TYPE, BRANCH_RELAX_NAME, false, false) 124 INITIALIZE_PASS(BranchRelaxation, DEBUG_TYPE, BRANCH_RELAX_NAME, false, false)
110 125
111 /// verify - check BBOffsets, BBSizes, alignment of islands 126 /// verify - check BBOffsets, BBSizes, alignment of islands
121 PrevNum = Num; 136 PrevNum = Num;
122 } 137 }
123 #endif 138 #endif
124 } 139 }
125 140
141 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
126 /// print block size and offset information - debugging 142 /// print block size and offset information - debugging
127 void BranchRelaxation::dumpBBs() { 143 LLVM_DUMP_METHOD void BranchRelaxation::dumpBBs() {
128 for (auto &MBB : *MF) { 144 for (auto &MBB : *MF) {
129 const BasicBlockInfo &BBI = BlockInfo[MBB.getNumber()]; 145 const BasicBlockInfo &BBI = BlockInfo[MBB.getNumber()];
130 dbgs() << format("BB#%u\toffset=%08x\t", MBB.getNumber(), BBI.Offset) 146 dbgs() << format("BB#%u\toffset=%08x\t", MBB.getNumber(), BBI.Offset)
131 << format("size=%#x\n", BBI.Size); 147 << format("size=%#x\n", BBI.Size);
132 } 148 }
133 } 149 }
150 #endif
134 151
135 /// scanFunction - Do the initial scan of the function, building up 152 /// scanFunction - Do the initial scan of the function, building up
136 /// information about each block. 153 /// information about each block.
137 void BranchRelaxation::scanFunction() { 154 void BranchRelaxation::scanFunction() {
138 BlockInfo.clear(); 155 BlockInfo.clear();
189 206
190 PrevNum = Num; 207 PrevNum = Num;
191 } 208 }
192 } 209 }
193 210
194 /// Insert a new empty basic block and insert it after \BB 211 /// Insert a new empty basic block and insert it after \BB
195 MachineBasicBlock *BranchRelaxation::createNewBlockAfter(MachineBasicBlock &BB) { 212 MachineBasicBlock *BranchRelaxation::createNewBlockAfter(MachineBasicBlock &BB) {
196 // Create a new MBB for the code after the OrigBB. 213 // Create a new MBB for the code after the OrigBB.
197 MachineBasicBlock *NewBB = 214 MachineBasicBlock *NewBB =
198 MF->CreateMachineBasicBlock(BB.getBasicBlock()); 215 MF->CreateMachineBasicBlock(BB.getBasicBlock());
199 MF->insert(++BB.getIterator(), NewBB); 216 MF->insert(++BB.getIterator(), NewBB);
226 TII->insertUnconditionalBranch(*OrigBB, NewBB, DebugLoc()); 243 TII->insertUnconditionalBranch(*OrigBB, NewBB, DebugLoc());
227 244
228 // Insert an entry into BlockInfo to align it properly with the block numbers. 245 // Insert an entry into BlockInfo to align it properly with the block numbers.
229 BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); 246 BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
230 247
231
232 NewBB->transferSuccessors(OrigBB); 248 NewBB->transferSuccessors(OrigBB);
233 OrigBB->addSuccessor(NewBB); 249 OrigBB->addSuccessor(NewBB);
234 OrigBB->addSuccessor(DestBB); 250 OrigBB->addSuccessor(DestBB);
235 251
236 // Cleanup potential unconditional branch to successor block. 252 // Cleanup potential unconditional branch to successor block.
249 // block, it may contain a tablejump. 265 // block, it may contain a tablejump.
250 BlockInfo[NewBB->getNumber()].Size = computeBlockSize(*NewBB); 266 BlockInfo[NewBB->getNumber()].Size = computeBlockSize(*NewBB);
251 267
252 // All BBOffsets following these blocks must be modified. 268 // All BBOffsets following these blocks must be modified.
253 adjustBlockOffsets(*OrigBB); 269 adjustBlockOffsets(*OrigBB);
270
271 // Need to fix live-in lists if we track liveness.
272 if (TRI->trackLivenessAfterRegAlloc(*MF))
273 computeAndAddLiveIns(LiveRegs, *NewBB);
254 274
255 ++NumSplit; 275 ++NumSplit;
256 276
257 return NewBB; 277 return NewBB;
258 } 278 }
334 354
335 // Update the successor lists according to the transformation to follow. 355 // Update the successor lists according to the transformation to follow.
336 // Do it here since if there's no split, no update is needed. 356 // Do it here since if there's no split, no update is needed.
337 MBB->replaceSuccessor(FBB, &NewBB); 357 MBB->replaceSuccessor(FBB, &NewBB);
338 NewBB.addSuccessor(FBB); 358 NewBB.addSuccessor(FBB);
359
360 // Need to fix live-in lists if we track liveness.
361 if (TRI->trackLivenessAfterRegAlloc(*MF))
362 computeAndAddLiveIns(LiveRegs, NewBB);
339 } 363 }
340 364
341 // We now have an appropriate fall-through block in place (either naturally or 365 // We now have an appropriate fall-through block in place (either naturally or
342 // just created), so we can invert the condition. 366 // just created), so we can invert the condition.
343 MachineBasicBlock &NextBB = *std::next(MachineFunction::iterator(MBB)); 367 MachineBasicBlock &NextBB = *std::next(MachineFunction::iterator(MBB));
409 // Relaxing branches involves creating new basic blocks, so re-eval 433 // Relaxing branches involves creating new basic blocks, so re-eval
410 // end() for termination. 434 // end() for termination.
411 for (MachineFunction::iterator I = MF->begin(); I != MF->end(); ++I) { 435 for (MachineFunction::iterator I = MF->begin(); I != MF->end(); ++I) {
412 MachineBasicBlock &MBB = *I; 436 MachineBasicBlock &MBB = *I;
413 437
414 auto Last = MBB.rbegin(); 438 // Empty block?
415 if (Last == MBB.rend()) // Empty block. 439 MachineBasicBlock::iterator Last = MBB.getLastNonDebugInstr();
440 if (Last == MBB.end())
416 continue; 441 continue;
417 442
418 // Expand the unconditional branch first if necessary. If there is a 443 // Expand the unconditional branch first if necessary. If there is a
419 // conditional branch, this will end up changing the branch destination of 444 // conditional branch, this will end up changing the branch destination of
420 // it to be over the newly inserted indirect branch block, which may avoid 445 // it to be over the newly inserted indirect branch block, which may avoid
471 DEBUG(dbgs() << "***** BranchRelaxation *****\n"); 496 DEBUG(dbgs() << "***** BranchRelaxation *****\n");
472 497
473 const TargetSubtargetInfo &ST = MF->getSubtarget(); 498 const TargetSubtargetInfo &ST = MF->getSubtarget();
474 TII = ST.getInstrInfo(); 499 TII = ST.getInstrInfo();
475 500
476 const TargetRegisterInfo *TRI = ST.getRegisterInfo(); 501 TRI = ST.getRegisterInfo();
477 if (TRI->trackLivenessAfterRegAlloc(*MF)) 502 if (TRI->trackLivenessAfterRegAlloc(*MF))
478 RS.reset(new RegScavenger()); 503 RS.reset(new RegScavenger());
479 504
480 // Renumber all of the machine basic blocks in the function, guaranteeing that 505 // Renumber all of the machine basic blocks in the function, guaranteeing that
481 // the numbers agree with the position of the block in the function. 506 // the numbers agree with the position of the block in the function.