comparison lib/Target/Mips/MipsLongBranch.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 //===-- MipsLongBranch.cpp - Emit long branches ---------------------------===// 1 //===- MipsLongBranch.cpp - Emit long branches ----------------------------===//
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.
11 // offset is too large to fit into its immediate field. 11 // offset is too large to fit into its immediate field.
12 // 12 //
13 // FIXME: Fix pc-region jump instructions which cross 256MB segment boundaries. 13 // FIXME: Fix pc-region jump instructions which cross 256MB segment boundaries.
14 //===----------------------------------------------------------------------===// 14 //===----------------------------------------------------------------------===//
15 15
16 #include "Mips.h" 16 #include "MCTargetDesc/MipsABIInfo.h"
17 #include "MCTargetDesc/MipsBaseInfo.h" 17 #include "MCTargetDesc/MipsBaseInfo.h"
18 #include "MCTargetDesc/MipsMCNaCl.h" 18 #include "MCTargetDesc/MipsMCNaCl.h"
19 #include "MCTargetDesc/MipsMCTargetDesc.h"
20 #include "Mips.h"
21 #include "MipsInstrInfo.h"
19 #include "MipsMachineFunction.h" 22 #include "MipsMachineFunction.h"
23 #include "MipsSubtarget.h"
20 #include "MipsTargetMachine.h" 24 #include "MipsTargetMachine.h"
25 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h" 26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/CodeGen/MachineBasicBlock.h"
29 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h" 30 #include "llvm/CodeGen/MachineFunctionPass.h"
31 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h" 32 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/IR/Function.h" 33 #include "llvm/CodeGen/MachineOperand.h"
34 #include "llvm/IR/DebugLoc.h"
25 #include "llvm/Support/CommandLine.h" 35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/MathExtras.h" 37 #include "llvm/Support/MathExtras.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Target/TargetMachine.h" 38 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetRegisterInfo.h" 39 #include "llvm/Target/TargetSubtargetInfo.h"
40 #include <cassert>
41 #include <cstdint>
42 #include <iterator>
30 43
31 using namespace llvm; 44 using namespace llvm;
32 45
33 #define DEBUG_TYPE "mips-long-branch" 46 #define DEBUG_TYPE "mips-long-branch"
34 47
45 cl::init(false), 58 cl::init(false),
46 cl::desc("MIPS: Expand all branches to long format."), 59 cl::desc("MIPS: Expand all branches to long format."),
47 cl::Hidden); 60 cl::Hidden);
48 61
49 namespace { 62 namespace {
50 typedef MachineBasicBlock::iterator Iter; 63
51 typedef MachineBasicBlock::reverse_iterator ReverseIter; 64 using Iter = MachineBasicBlock::iterator;
65 using ReverseIter = MachineBasicBlock::reverse_iterator;
52 66
53 struct MBBInfo { 67 struct MBBInfo {
54 uint64_t Size, Address; 68 uint64_t Size = 0;
55 bool HasLongBranch; 69 uint64_t Address;
56 MachineInstr *Br; 70 bool HasLongBranch = false;
57 71 MachineInstr *Br = nullptr;
58 MBBInfo() : Size(0), HasLongBranch(false), Br(nullptr) {} 72
73 MBBInfo() = default;
59 }; 74 };
60 75
61 class MipsLongBranch : public MachineFunctionPass { 76 class MipsLongBranch : public MachineFunctionPass {
62
63 public: 77 public:
64 static char ID; 78 static char ID;
65 MipsLongBranch(TargetMachine &tm) 79
66 : MachineFunctionPass(ID), TM(tm), IsPIC(TM.isPositionIndependent()), 80 MipsLongBranch()
67 ABI(static_cast<const MipsTargetMachine &>(TM).getABI()) {} 81 : MachineFunctionPass(ID), ABI(MipsABIInfo::Unknown()) {}
68 82
69 StringRef getPassName() const override { return "Mips Long Branch"; } 83 StringRef getPassName() const override { return "Mips Long Branch"; }
70 84
71 bool runOnMachineFunction(MachineFunction &F) override; 85 bool runOnMachineFunction(MachineFunction &F) override;
72 86
81 int64_t computeOffset(const MachineInstr *Br); 95 int64_t computeOffset(const MachineInstr *Br);
82 void replaceBranch(MachineBasicBlock &MBB, Iter Br, const DebugLoc &DL, 96 void replaceBranch(MachineBasicBlock &MBB, Iter Br, const DebugLoc &DL,
83 MachineBasicBlock *MBBOpnd); 97 MachineBasicBlock *MBBOpnd);
84 void expandToLongBranch(MBBInfo &Info); 98 void expandToLongBranch(MBBInfo &Info);
85 99
86 const TargetMachine &TM;
87 MachineFunction *MF; 100 MachineFunction *MF;
88 SmallVector<MBBInfo, 16> MBBInfos; 101 SmallVector<MBBInfo, 16> MBBInfos;
89 bool IsPIC; 102 bool IsPIC;
90 MipsABIInfo ABI; 103 MipsABIInfo ABI;
91 unsigned LongBranchSeqSize; 104 unsigned LongBranchSeqSize;
92 }; 105 };
93 106
94 char MipsLongBranch::ID = 0; 107 } // end anonymous namespace
95 } // end of anonymous namespace 108
96 109 char MipsLongBranch::ID = 0;
97 /// createMipsLongBranchPass - Returns a pass that converts branches to long
98 /// branches.
99 FunctionPass *llvm::createMipsLongBranchPass(MipsTargetMachine &tm) {
100 return new MipsLongBranch(tm);
101 }
102 110
103 /// Iterate over list of Br's operands and search for a MachineBasicBlock 111 /// Iterate over list of Br's operands and search for a MachineBasicBlock
104 /// operand. 112 /// operand.
105 static MachineBasicBlock *getTargetMBB(const MachineInstr &Br) { 113 static MachineBasicBlock *getTargetMBB(const MachineInstr &Br) {
106 for (unsigned I = 0, E = Br.getDesc().getNumOperands(); I < E; ++I) { 114 for (unsigned I = 0, E = Br.getDesc().getNumOperands(); I < E; ++I) {
459 bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) { 467 bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) {
460 const MipsSubtarget &STI = 468 const MipsSubtarget &STI =
461 static_cast<const MipsSubtarget &>(F.getSubtarget()); 469 static_cast<const MipsSubtarget &>(F.getSubtarget());
462 const MipsInstrInfo *TII = 470 const MipsInstrInfo *TII =
463 static_cast<const MipsInstrInfo *>(STI.getInstrInfo()); 471 static_cast<const MipsInstrInfo *>(STI.getInstrInfo());
472
473 const TargetMachine& TM = F.getTarget();
474 IsPIC = TM.isPositionIndependent();
475 ABI = static_cast<const MipsTargetMachine &>(TM).getABI();
476
464 LongBranchSeqSize = 477 LongBranchSeqSize =
465 !IsPIC ? 2 : (ABI.IsN64() ? 10 : (!STI.isTargetNaCl() ? 9 : 10)); 478 !IsPIC ? 2 : (ABI.IsN64() ? 10 : (!STI.isTargetNaCl() ? 9 : 10));
466 479
467 if (STI.inMips16Mode() || !STI.enableLongBranchPass()) 480 if (STI.inMips16Mode() || !STI.enableLongBranchPass())
468 return false; 481 return false;
528 541
529 MF->RenumberBlocks(); 542 MF->RenumberBlocks();
530 543
531 return true; 544 return true;
532 } 545 }
546
547 /// createMipsLongBranchPass - Returns a pass that converts branches to long
548 /// branches.
549 FunctionPass *llvm::createMipsLongBranchPass() { return new MipsLongBranch(); }