Jia Liu | 9f61011 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- MipsInstrInfo.cpp - Mips Instruction Information ------------------===// |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 7 | // |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file contains the Mips implementation of the TargetInstrInfo class. |
| 11 | // |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 13 | |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 14 | #include "MipsInstrInfo.h" |
Akira Hatanaka | 9c6028f | 2011-07-07 23:56:50 +0000 | [diff] [blame] | 15 | #include "InstPrinter/MipsInstPrinter.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "MipsMachineFunction.h" |
Eric Christopher | d8abc3a | 2015-01-08 18:18:54 +0000 | [diff] [blame] | 17 | #include "MipsSubtarget.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | d5ca7064 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 22 | #include "llvm/Support/TargetRegistry.h" |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 23 | |
Chandler Carruth | d174b72 | 2014-04-22 02:03:14 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 26 | #define GET_INSTRINFO_CTOR_DTOR |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 27 | #include "MipsGenInstrInfo.inc" |
| 28 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 29 | // Pin the vtable to this file. |
| 30 | void MipsInstrInfo::anchor() {} |
| 31 | |
Eric Christopher | 675cb4d | 2014-07-18 23:25:00 +0000 | [diff] [blame] | 32 | MipsInstrInfo::MipsInstrInfo(const MipsSubtarget &STI, unsigned UncondBr) |
| 33 | : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP), |
| 34 | Subtarget(STI), UncondBrOpc(UncondBr) {} |
Akira Hatanaka | 9c6028f | 2011-07-07 23:56:50 +0000 | [diff] [blame] | 35 | |
Eric Christopher | 675cb4d | 2014-07-18 23:25:00 +0000 | [diff] [blame] | 36 | const MipsInstrInfo *MipsInstrInfo::create(MipsSubtarget &STI) { |
| 37 | if (STI.inMips16Mode()) |
| 38 | return llvm::createMips16InstrInfo(STI); |
Akira Hatanaka | fab8929 | 2012-08-02 18:21:47 +0000 | [diff] [blame] | 39 | |
Eric Christopher | 675cb4d | 2014-07-18 23:25:00 +0000 | [diff] [blame] | 40 | return llvm::createMipsSEInstrInfo(STI); |
Akira Hatanaka | fab8929 | 2012-08-02 18:21:47 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Akira Hatanaka | b7fa3c9 | 2012-07-31 21:49:49 +0000 | [diff] [blame] | 43 | bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const { |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 44 | return op.isImm() && op.getImm() == 0; |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 47 | /// insertNoop - If data hazard condition is found insert the target nop |
| 48 | /// instruction. |
Simon Dardis | 9a3f32c | 2016-03-29 13:02:19 +0000 | [diff] [blame] | 49 | // FIXME: This appears to be dead code. |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 50 | void MipsInstrInfo:: |
Bruno Cardoso Lopes | ed874ef | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 51 | insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 52 | { |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 53 | DebugLoc DL; |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 54 | BuildMI(MBB, MI, DL, get(Mips::NOP)); |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Akira Hatanaka | b7fa3c9 | 2012-07-31 21:49:49 +0000 | [diff] [blame] | 57 | MachineMemOperand *MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI, |
| 58 | unsigned Flag) const { |
Akira Hatanaka | 1cf7576 | 2011-12-24 03:11:18 +0000 | [diff] [blame] | 59 | MachineFunction &MF = *MBB.getParent(); |
| 60 | MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 61 | unsigned Align = MFI.getObjectAlignment(FI); |
Jia Liu | f54f60f | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 62 | |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 63 | return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI), |
| 64 | Flag, MFI.getObjectSize(FI), Align); |
Akira Hatanaka | 1cf7576 | 2011-12-24 03:11:18 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 68 | // Branch Analysis |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 69 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 70 | |
Akira Hatanaka | b7fa3c9 | 2012-07-31 21:49:49 +0000 | [diff] [blame] | 71 | void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc, |
| 72 | MachineBasicBlock *&BB, |
| 73 | SmallVectorImpl<MachineOperand> &Cond) const { |
Akira Hatanaka | 067d815 | 2013-05-13 17:43:19 +0000 | [diff] [blame] | 74 | assert(getAnalyzableBrOpc(Opc) && "Not an analyzable branch"); |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 75 | int NumOp = Inst->getNumExplicitOperands(); |
Jia Liu | f54f60f | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 76 | |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 77 | // for both int and fp branches, the last explicit operand is the |
| 78 | // MBB. |
| 79 | BB = Inst->getOperand(NumOp-1).getMBB(); |
| 80 | Cond.push_back(MachineOperand::CreateImm(Opc)); |
Bruno Cardoso Lopes | bcaf6e5 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 81 | |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 82 | for (int i=0; i<NumOp-1; i++) |
| 83 | Cond.push_back(Inst->getOperand(i)); |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Bruno Cardoso Lopes | ed874ef | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 86 | bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 87 | MachineBasicBlock *&TBB, |
| 88 | MachineBasicBlock *&FBB, |
Evan Cheng | 64dfcac | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 89 | SmallVectorImpl<MachineOperand> &Cond, |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 90 | bool AllowModify) const { |
| 91 | SmallVector<MachineInstr*, 2> BranchInstrs; |
| 92 | BranchType BT = AnalyzeBranch(MBB, TBB, FBB, Cond, AllowModify, BranchInstrs); |
Akira Hatanaka | fcdd9b1 | 2012-09-13 17:12:37 +0000 | [diff] [blame] | 93 | |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 94 | return (BT == BT_None) || (BT == BT_Indirect); |
Jia Liu | f54f60f | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Eric Christopher | 754d54f | 2014-07-18 20:35:49 +0000 | [diff] [blame] | 97 | void |
| 98 | MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 99 | DebugLoc DL, ArrayRef<MachineOperand> Cond) const { |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 100 | unsigned Opc = Cond[0].getImm(); |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 101 | const MCInstrDesc &MCID = get(Opc); |
| 102 | MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID); |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 103 | |
Akira Hatanaka | fcdd9b1 | 2012-09-13 17:12:37 +0000 | [diff] [blame] | 104 | for (unsigned i = 1; i < Cond.size(); ++i) { |
| 105 | if (Cond[i].isReg()) |
| 106 | MIB.addReg(Cond[i].getReg()); |
| 107 | else if (Cond[i].isImm()) |
| 108 | MIB.addImm(Cond[i].getImm()); |
| 109 | else |
Craig Topper | beb77bd | 2016-04-24 04:38:29 +0000 | [diff] [blame] | 110 | assert(false && "Cannot copy operand"); |
Akira Hatanaka | fcdd9b1 | 2012-09-13 17:12:37 +0000 | [diff] [blame] | 111 | } |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 112 | MIB.addMBB(TBB); |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Eric Christopher | 754d54f | 2014-07-18 20:35:49 +0000 | [diff] [blame] | 115 | unsigned MipsInstrInfo::InsertBranch( |
| 116 | MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 117 | ArrayRef<MachineOperand> Cond, DebugLoc DL) const { |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 118 | // Shouldn't be a fall through. |
| 119 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 120 | |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 121 | // # of condition operands: |
| 122 | // Unconditional branches: 0 |
| 123 | // Floating point branches: 1 (opc) |
| 124 | // Int BranchZero: 2 (opc, reg) |
| 125 | // Int Branch: 3 (opc, reg0, reg1) |
| 126 | assert((Cond.size() <= 3) && |
| 127 | "# of Mips branch conditions must be <= 3!"); |
Bruno Cardoso Lopes | ed874ef | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 128 | |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 129 | // Two-way Conditional branch. |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 130 | if (FBB) { |
| 131 | BuildCondBr(MBB, TBB, DL, Cond); |
Akira Hatanaka | 5d5e0d8 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 132 | BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB); |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 133 | return 2; |
| 134 | } |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 135 | |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 136 | // One way branch. |
| 137 | // Unconditional branch. |
| 138 | if (Cond.empty()) |
Akira Hatanaka | 5d5e0d8 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 139 | BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB); |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 140 | else // Conditional branch. |
| 141 | BuildCondBr(MBB, TBB, DL, Cond); |
| 142 | return 1; |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 143 | } |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 144 | |
Eric Christopher | 754d54f | 2014-07-18 20:35:49 +0000 | [diff] [blame] | 145 | unsigned MipsInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 146 | MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend(); |
| 147 | MachineBasicBlock::reverse_iterator FirstBr; |
| 148 | unsigned removed; |
Bruno Cardoso Lopes | ed874ef | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 149 | |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 150 | // Skip all the debug instructions. |
| 151 | while (I != REnd && I->isDebugValue()) |
| 152 | ++I; |
Bruno Cardoso Lopes | ed874ef | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 153 | |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 154 | FirstBr = I; |
Bruno Cardoso Lopes | ed874ef | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 155 | |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 156 | // Up to 2 branches are removed. |
| 157 | // Note that indirect branches are not removed. |
Eric Christopher | 675cb4d | 2014-07-18 23:25:00 +0000 | [diff] [blame] | 158 | for (removed = 0; I != REnd && removed < 2; ++I, ++removed) |
Akira Hatanaka | 067d815 | 2013-05-13 17:43:19 +0000 | [diff] [blame] | 159 | if (!getAnalyzableBrOpc(I->getOpcode())) |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 160 | break; |
Bruno Cardoso Lopes | ed874ef | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 161 | |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 162 | MBB.erase(I.base(), FirstBr.base()); |
| 163 | |
| 164 | return removed; |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Bruno Cardoso Lopes | ed874ef | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 167 | /// ReverseBranchCondition - Return the inverse opcode of the |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 168 | /// specified Branch instruction. |
Eric Christopher | 754d54f | 2014-07-18 20:35:49 +0000 | [diff] [blame] | 169 | bool MipsInstrInfo::ReverseBranchCondition( |
| 170 | SmallVectorImpl<MachineOperand> &Cond) const { |
Akira Hatanaka | 93f898f | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 171 | assert( (Cond.size() && Cond.size() <= 3) && |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 172 | "Invalid Mips branch condition!"); |
Akira Hatanaka | 067d815 | 2013-05-13 17:43:19 +0000 | [diff] [blame] | 173 | Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm())); |
Bruno Cardoso Lopes | 7b616f5 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 174 | return false; |
| 175 | } |
Dan Gohman | d5ca7064 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 176 | |
Eric Christopher | 754d54f | 2014-07-18 20:35:49 +0000 | [diff] [blame] | 177 | MipsInstrInfo::BranchType MipsInstrInfo::AnalyzeBranch( |
| 178 | MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, |
| 179 | SmallVectorImpl<MachineOperand> &Cond, bool AllowModify, |
| 180 | SmallVectorImpl<MachineInstr *> &BranchInstrs) const { |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 181 | |
| 182 | MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend(); |
| 183 | |
| 184 | // Skip all the debug instructions. |
| 185 | while (I != REnd && I->isDebugValue()) |
| 186 | ++I; |
| 187 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 188 | if (I == REnd || !isUnpredicatedTerminator(*I)) { |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 189 | // This block ends with no branches (it just falls through to its succ). |
| 190 | // Leave TBB/FBB null. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 191 | TBB = FBB = nullptr; |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 192 | return BT_NoBranch; |
| 193 | } |
| 194 | |
| 195 | MachineInstr *LastInst = &*I; |
| 196 | unsigned LastOpc = LastInst->getOpcode(); |
| 197 | BranchInstrs.push_back(LastInst); |
| 198 | |
| 199 | // Not an analyzable branch (e.g., indirect jump). |
Akira Hatanaka | 067d815 | 2013-05-13 17:43:19 +0000 | [diff] [blame] | 200 | if (!getAnalyzableBrOpc(LastOpc)) |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 201 | return LastInst->isIndirectBranch() ? BT_Indirect : BT_None; |
| 202 | |
| 203 | // Get the second to last instruction in the block. |
| 204 | unsigned SecondLastOpc = 0; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 205 | MachineInstr *SecondLastInst = nullptr; |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 206 | |
| 207 | if (++I != REnd) { |
| 208 | SecondLastInst = &*I; |
Akira Hatanaka | 067d815 | 2013-05-13 17:43:19 +0000 | [diff] [blame] | 209 | SecondLastOpc = getAnalyzableBrOpc(SecondLastInst->getOpcode()); |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 210 | |
| 211 | // Not an analyzable branch (must be an indirect jump). |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 212 | if (isUnpredicatedTerminator(*SecondLastInst) && !SecondLastOpc) |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 213 | return BT_None; |
| 214 | } |
| 215 | |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 216 | // If there is only one terminator instruction, process it. |
| 217 | if (!SecondLastOpc) { |
Matheus Almeida | 6de62d3 | 2013-10-01 12:53:00 +0000 | [diff] [blame] | 218 | // Unconditional branch. |
Daniel Sanders | f9d8b8c | 2016-05-06 13:23:51 +0000 | [diff] [blame] | 219 | if (LastInst->isUnconditionalBranch()) { |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 220 | TBB = LastInst->getOperand(0).getMBB(); |
| 221 | return BT_Uncond; |
| 222 | } |
| 223 | |
| 224 | // Conditional branch |
| 225 | AnalyzeCondBr(LastInst, LastOpc, TBB, Cond); |
| 226 | return BT_Cond; |
| 227 | } |
| 228 | |
| 229 | // If we reached here, there are two branches. |
| 230 | // If there are three terminators, we don't know what sort of block this is. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 231 | if (++I != REnd && isUnpredicatedTerminator(*I)) |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 232 | return BT_None; |
| 233 | |
Akira Hatanaka | 28dc83c | 2013-03-01 01:22:26 +0000 | [diff] [blame] | 234 | BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst); |
| 235 | |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 236 | // If second to last instruction is an unconditional branch, |
| 237 | // analyze it and remove the last instruction. |
Daniel Sanders | f9d8b8c | 2016-05-06 13:23:51 +0000 | [diff] [blame] | 238 | if (SecondLastInst->isUnconditionalBranch()) { |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 239 | // Return if the last instruction cannot be removed. |
| 240 | if (!AllowModify) |
| 241 | return BT_None; |
| 242 | |
| 243 | TBB = SecondLastInst->getOperand(0).getMBB(); |
| 244 | LastInst->eraseFromParent(); |
| 245 | BranchInstrs.pop_back(); |
| 246 | return BT_Uncond; |
| 247 | } |
| 248 | |
| 249 | // Conditional branch followed by an unconditional branch. |
| 250 | // The last one must be unconditional. |
Daniel Sanders | f9d8b8c | 2016-05-06 13:23:51 +0000 | [diff] [blame] | 251 | if (!LastInst->isUnconditionalBranch()) |
Akira Hatanaka | 7320b23 | 2013-03-01 01:10:17 +0000 | [diff] [blame] | 252 | return BT_None; |
| 253 | |
| 254 | AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond); |
| 255 | FBB = LastInst->getOperand(0).getMBB(); |
| 256 | |
| 257 | return BT_CondUncond; |
| 258 | } |
| 259 | |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 260 | /// Return the corresponding compact (no delay slot) form of a branch. |
| 261 | unsigned MipsInstrInfo::getEquivalentCompactForm( |
| 262 | const MachineBasicBlock::iterator I) const { |
| 263 | unsigned Opcode = I->getOpcode(); |
Simon Dardis | d9d41f5 | 2016-04-05 12:50:29 +0000 | [diff] [blame] | 264 | bool canUseShortMicroMipsCTI = false; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 265 | |
Simon Dardis | d9d41f5 | 2016-04-05 12:50:29 +0000 | [diff] [blame] | 266 | if (Subtarget.inMicroMipsMode()) { |
| 267 | switch (Opcode) { |
| 268 | case Mips::BNE: |
| 269 | case Mips::BEQ: |
| 270 | // microMIPS has NE,EQ branches that do not have delay slots provided one |
| 271 | // of the operands is zero. |
| 272 | if (I->getOperand(1).getReg() == Subtarget.getABI().GetZeroReg()) |
| 273 | canUseShortMicroMipsCTI = true; |
| 274 | break; |
| 275 | // For microMIPS the PseudoReturn and PseudoIndirectBranch are always |
| 276 | // expanded to JR_MM, so they can be replaced with JRC16_MM. |
| 277 | case Mips::JR: |
| 278 | case Mips::PseudoReturn: |
| 279 | case Mips::PseudoIndirectBranch: |
| 280 | canUseShortMicroMipsCTI = true; |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
Simon Dardis | 669d8dd | 2016-05-18 10:38:01 +0000 | [diff] [blame] | 285 | // MIPSR6 forbids both operands being the zero register. |
| 286 | if (Subtarget.hasMips32r6() && (I->getNumOperands() > 1) && |
| 287 | (I->getOperand(0).isReg() && |
| 288 | (I->getOperand(0).getReg() == Mips::ZERO || |
| 289 | I->getOperand(0).getReg() == Mips::ZERO_64)) && |
| 290 | (I->getOperand(1).isReg() && |
| 291 | (I->getOperand(1).getReg() == Mips::ZERO || |
| 292 | I->getOperand(1).getReg() == Mips::ZERO_64))) |
| 293 | return 0; |
| 294 | |
Simon Dardis | d9d41f5 | 2016-04-05 12:50:29 +0000 | [diff] [blame] | 295 | if (Subtarget.hasMips32r6() || canUseShortMicroMipsCTI) { |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 296 | switch (Opcode) { |
| 297 | case Mips::B: |
| 298 | return Mips::BC; |
| 299 | case Mips::BAL: |
| 300 | return Mips::BALC; |
| 301 | case Mips::BEQ: |
Simon Dardis | d9d41f5 | 2016-04-05 12:50:29 +0000 | [diff] [blame] | 302 | if (canUseShortMicroMipsCTI) |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 303 | return Mips::BEQZC_MM; |
Simon Dardis | 03676dc | 2016-05-31 09:54:55 +0000 | [diff] [blame^] | 304 | else if (I->getOperand(0).getReg() == I->getOperand(1).getReg()) |
| 305 | return 0; |
| 306 | return Mips::BEQC; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 307 | case Mips::BNE: |
Simon Dardis | d9d41f5 | 2016-04-05 12:50:29 +0000 | [diff] [blame] | 308 | if (canUseShortMicroMipsCTI) |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 309 | return Mips::BNEZC_MM; |
Simon Dardis | 03676dc | 2016-05-31 09:54:55 +0000 | [diff] [blame^] | 310 | else if (I->getOperand(0).getReg() == I->getOperand(1).getReg()) |
| 311 | return 0; |
| 312 | return Mips::BNEC; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 313 | case Mips::BGE: |
Simon Dardis | 669d8dd | 2016-05-18 10:38:01 +0000 | [diff] [blame] | 314 | if (I->getOperand(0).getReg() == I->getOperand(1).getReg()) |
| 315 | return 0; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 316 | return Mips::BGEC; |
| 317 | case Mips::BGEU: |
Simon Dardis | 669d8dd | 2016-05-18 10:38:01 +0000 | [diff] [blame] | 318 | if (I->getOperand(0).getReg() == I->getOperand(1).getReg()) |
| 319 | return 0; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 320 | return Mips::BGEUC; |
| 321 | case Mips::BGEZ: |
| 322 | return Mips::BGEZC; |
| 323 | case Mips::BGTZ: |
| 324 | return Mips::BGTZC; |
| 325 | case Mips::BLEZ: |
| 326 | return Mips::BLEZC; |
| 327 | case Mips::BLT: |
Simon Dardis | 669d8dd | 2016-05-18 10:38:01 +0000 | [diff] [blame] | 328 | if (I->getOperand(0).getReg() == I->getOperand(1).getReg()) |
| 329 | return 0; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 330 | return Mips::BLTC; |
| 331 | case Mips::BLTU: |
Simon Dardis | 669d8dd | 2016-05-18 10:38:01 +0000 | [diff] [blame] | 332 | if (I->getOperand(0).getReg() == I->getOperand(1).getReg()) |
| 333 | return 0; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 334 | return Mips::BLTUC; |
| 335 | case Mips::BLTZ: |
| 336 | return Mips::BLTZC; |
Simon Dardis | d9d41f5 | 2016-04-05 12:50:29 +0000 | [diff] [blame] | 337 | // For MIPSR6, the instruction 'jic' can be used for these cases. Some |
| 338 | // tools will accept 'jrc reg' as an alias for 'jic 0, $reg'. |
| 339 | case Mips::JR: |
| 340 | case Mips::PseudoReturn: |
| 341 | case Mips::PseudoIndirectBranch: |
| 342 | if (canUseShortMicroMipsCTI) |
| 343 | return Mips::JRC16_MM; |
| 344 | return Mips::JIC; |
| 345 | case Mips::JALRPseudo: |
| 346 | return Mips::JIALC; |
| 347 | case Mips::JR64: |
| 348 | case Mips::PseudoReturn64: |
| 349 | case Mips::PseudoIndirectBranch64: |
| 350 | return Mips::JIC64; |
| 351 | case Mips::JALR64Pseudo: |
| 352 | return Mips::JIALC64; |
Simon Dardis | 669d8dd | 2016-05-18 10:38:01 +0000 | [diff] [blame] | 353 | default: |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | /// Predicate for distingushing between control transfer instructions and all |
| 362 | /// other instructions for handling forbidden slots. Consider inline assembly |
| 363 | /// as unsafe as well. |
| 364 | bool MipsInstrInfo::SafeInForbiddenSlot(const MachineInstr &MI) const { |
| 365 | if (MI.isInlineAsm()) |
| 366 | return false; |
| 367 | |
| 368 | return (MI.getDesc().TSFlags & MipsII::IsCTI) == 0; |
| 369 | |
| 370 | } |
| 371 | |
| 372 | /// Predicate for distingushing instructions that have forbidden slots. |
| 373 | bool MipsInstrInfo::HasForbiddenSlot(const MachineInstr &MI) const { |
| 374 | return (MI.getDesc().TSFlags & MipsII::HasForbiddenSlot) != 0; |
| 375 | } |
| 376 | |
Akira Hatanaka | acd1a7d | 2012-06-14 01:16:45 +0000 | [diff] [blame] | 377 | /// Return the number of bytes of code the specified instruction may be. |
| 378 | unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const { |
| 379 | switch (MI->getOpcode()) { |
| 380 | default: |
| 381 | return MI->getDesc().getSize(); |
| 382 | case TargetOpcode::INLINEASM: { // Inline Asm: Variable size. |
| 383 | const MachineFunction *MF = MI->getParent()->getParent(); |
| 384 | const char *AsmStr = MI->getOperand(0).getSymbolName(); |
| 385 | return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); |
| 386 | } |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 387 | case Mips::CONSTPOOL_ENTRY: |
| 388 | // If this machine instr is a constant pool entry, its size is recorded as |
| 389 | // operand #2. |
| 390 | return MI->getOperand(2).getImm(); |
Akira Hatanaka | acd1a7d | 2012-06-14 01:16:45 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
Akira Hatanaka | 310e26a | 2013-05-13 17:57:42 +0000 | [diff] [blame] | 393 | |
| 394 | MachineInstrBuilder |
| 395 | MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc, |
| 396 | MachineBasicBlock::iterator I) const { |
| 397 | MachineInstrBuilder MIB; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 398 | |
| 399 | // Certain branches have two forms: e.g beq $1, $zero, dst vs beqz $1, dest |
| 400 | // Pick the zero form of the branch for readable assembly and for greater |
| 401 | // branch distance in non-microMIPS mode. |
Simon Dardis | d9d41f5 | 2016-04-05 12:50:29 +0000 | [diff] [blame] | 402 | // FIXME: Certain atomic sequences on mips64 generate 32bit references to |
| 403 | // Mips::ZERO, which is incorrect. This test should be updated to use |
| 404 | // Subtarget.getABI().GetZeroReg() when those atomic sequences and others |
| 405 | // are fixed. |
| 406 | bool BranchWithZeroOperand = |
| 407 | (I->isBranch() && !I->isPseudo() && I->getOperand(1).isReg() && |
| 408 | (I->getOperand(1).getReg() == Mips::ZERO || |
| 409 | I->getOperand(1).getReg() == Mips::ZERO_64)); |
| 410 | |
| 411 | if (BranchWithZeroOperand) { |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 412 | switch (NewOpc) { |
| 413 | case Mips::BEQC: |
| 414 | NewOpc = Mips::BEQZC; |
| 415 | break; |
| 416 | case Mips::BNEC: |
| 417 | NewOpc = Mips::BNEZC; |
| 418 | break; |
| 419 | case Mips::BGEC: |
| 420 | NewOpc = Mips::BGEZC; |
| 421 | break; |
| 422 | case Mips::BLTC: |
| 423 | NewOpc = Mips::BLTZC; |
| 424 | break; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Akira Hatanaka | 310e26a | 2013-05-13 17:57:42 +0000 | [diff] [blame] | 428 | MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), get(NewOpc)); |
| 429 | |
Simon Dardis | d9d41f5 | 2016-04-05 12:50:29 +0000 | [diff] [blame] | 430 | // For MIPSR6 JI*C requires an immediate 0 as an operand, JIALC(64) an |
| 431 | // immediate 0 as an operand and requires the removal of it's %RA<imp-def> |
| 432 | // implicit operand as copying the implicit operations of the instructio we're |
| 433 | // looking at will give us the correct flags. |
| 434 | if (NewOpc == Mips::JIC || NewOpc == Mips::JIALC || NewOpc == Mips::JIC64 || |
| 435 | NewOpc == Mips::JIALC64) { |
| 436 | |
| 437 | if (NewOpc == Mips::JIALC || NewOpc == Mips::JIALC64) |
| 438 | MIB->RemoveOperand(0); |
| 439 | |
| 440 | for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J) { |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 441 | MIB.addOperand(I->getOperand(J)); |
Simon Dardis | d9d41f5 | 2016-04-05 12:50:29 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | MIB.addImm(0); |
| 445 | |
| 446 | } else if (BranchWithZeroOperand) { |
| 447 | // For MIPSR6 and microMIPS branches with an explicit zero operand, copy |
| 448 | // everything after the zero. |
| 449 | MIB.addOperand(I->getOperand(0)); |
| 450 | |
| 451 | for (unsigned J = 2, E = I->getDesc().getNumOperands(); J < E; ++J) { |
| 452 | MIB.addOperand(I->getOperand(J)); |
| 453 | } |
| 454 | } else { |
| 455 | // All other cases copy all other operands. |
| 456 | for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J) { |
| 457 | MIB.addOperand(I->getOperand(J)); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | MIB.copyImplicitOps(*I); |
Akira Hatanaka | 310e26a | 2013-05-13 17:57:42 +0000 | [diff] [blame] | 462 | |
| 463 | MIB.setMemRefs(I->memoperands_begin(), I->memoperands_end()); |
| 464 | return MIB; |
| 465 | } |