Jia Liu | c570711 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- MipsInstrInfo.cpp - Mips Instruction Information ------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 7 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file contains the Mips implementation of the TargetInstrInfo class. |
| 11 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 13 | |
Akira Hatanaka | d4b48b2 | 2012-06-14 01:16:45 +0000 | [diff] [blame] | 14 | #include "MipsAnalyzeImmediate.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 15 | #include "MipsInstrInfo.h" |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 16 | #include "MipsTargetMachine.h" |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 17 | #include "MipsMachineFunction.h" |
Akira Hatanaka | 794bf17 | 2011-07-07 23:56:50 +0000 | [diff] [blame] | 18 | #include "InstPrinter/MipsInstPrinter.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 22 | #include "llvm/Support/TargetRegistry.h" |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Evan Cheng | 22fee2d | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 24 | |
Evan Cheng | 4db3cff | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 25 | #define GET_INSTRINFO_CTOR |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 26 | #include "MipsGenInstrInfo.inc" |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
Akira Hatanaka | 0bc1adb | 2012-07-31 21:49:49 +0000 | [diff] [blame] | 30 | MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm, unsigned UncondBr) |
Evan Cheng | 4db3cff | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 31 | : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP), |
Akira Hatanaka | 8589010 | 2012-07-31 23:41:32 +0000 | [diff] [blame] | 32 | TM(tm), UncondBrOpc(UncondBr) {} |
Akira Hatanaka | 794bf17 | 2011-07-07 23:56:50 +0000 | [diff] [blame] | 33 | |
Akira Hatanaka | af26626 | 2012-08-02 18:21:47 +0000 | [diff] [blame^] | 34 | const MipsInstrInfo *MipsInstrInfo::create(MipsTargetMachine &TM) { |
| 35 | if (TM.getSubtargetImpl()->inMips16Mode()) |
| 36 | return llvm::createMips16InstrInfo(TM); |
| 37 | |
| 38 | return llvm::createMipsSEInstrInfo(TM); |
| 39 | } |
| 40 | |
Akira Hatanaka | 0bc1adb | 2012-07-31 21:49:49 +0000 | [diff] [blame] | 41 | bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 42 | return op.isImm() && op.getImm() == 0; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 45 | /// insertNoop - If data hazard condition is found insert the target nop |
| 46 | /// instruction. |
| 47 | void MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 48 | insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 49 | { |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 50 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 51 | BuildMI(MBB, MI, DL, get(Mips::NOP)); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Akira Hatanaka | 0bc1adb | 2012-07-31 21:49:49 +0000 | [diff] [blame] | 54 | MachineMemOperand *MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI, |
| 55 | unsigned Flag) const { |
Akira Hatanaka | fd1d925 | 2011-12-24 03:11:18 +0000 | [diff] [blame] | 56 | MachineFunction &MF = *MBB.getParent(); |
| 57 | MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 58 | unsigned Align = MFI.getObjectAlignment(FI); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 59 | |
Akira Hatanaka | fd1d925 | 2011-12-24 03:11:18 +0000 | [diff] [blame] | 60 | return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), Flag, |
| 61 | MFI.getObjectSize(FI), Align); |
| 62 | } |
| 63 | |
Akira Hatanaka | c4f24eb | 2011-07-01 01:04:43 +0000 | [diff] [blame] | 64 | MachineInstr* |
| 65 | MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx, |
| 66 | uint64_t Offset, const MDNode *MDPtr, |
| 67 | DebugLoc DL) const { |
| 68 | MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE)) |
| 69 | .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr); |
| 70 | return &*MIB; |
| 71 | } |
| 72 | |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 73 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 74 | // Branch Analysis |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 75 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 76 | |
Akira Hatanaka | 0bc1adb | 2012-07-31 21:49:49 +0000 | [diff] [blame] | 77 | void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc, |
| 78 | MachineBasicBlock *&BB, |
| 79 | SmallVectorImpl<MachineOperand> &Cond) const { |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 80 | assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch"); |
| 81 | int NumOp = Inst->getNumExplicitOperands(); |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 82 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 83 | // for both int and fp branches, the last explicit operand is the |
| 84 | // MBB. |
| 85 | BB = Inst->getOperand(NumOp-1).getMBB(); |
| 86 | Cond.push_back(MachineOperand::CreateImm(Opc)); |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 87 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 88 | for (int i=0; i<NumOp-1; i++) |
| 89 | Cond.push_back(Inst->getOperand(i)); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 92 | bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 93 | MachineBasicBlock *&TBB, |
| 94 | MachineBasicBlock *&FBB, |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 95 | SmallVectorImpl<MachineOperand> &Cond, |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 96 | bool AllowModify) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 97 | { |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 98 | MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend(); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 99 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 100 | // Skip all the debug instructions. |
| 101 | while (I != REnd && I->isDebugValue()) |
| 102 | ++I; |
| 103 | |
| 104 | if (I == REnd || !isUnpredicatedTerminator(&*I)) { |
| 105 | // If this block ends with no branches (it just falls through to its succ) |
| 106 | // just return false, leaving TBB/FBB null. |
| 107 | TBB = FBB = NULL; |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | MachineInstr *LastInst = &*I; |
| 112 | unsigned LastOpc = LastInst->getOpcode(); |
| 113 | |
| 114 | // Not an analyzable branch (must be an indirect jump). |
| 115 | if (!GetAnalyzableBrOpc(LastOpc)) |
| 116 | return true; |
| 117 | |
| 118 | // Get the second to last instruction in the block. |
| 119 | unsigned SecondLastOpc = 0; |
| 120 | MachineInstr *SecondLastInst = NULL; |
| 121 | |
| 122 | if (++I != REnd) { |
| 123 | SecondLastInst = &*I; |
| 124 | SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode()); |
| 125 | |
| 126 | // Not an analyzable branch (must be an indirect jump). |
| 127 | if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc) |
| 128 | return true; |
| 129 | } |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 130 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 131 | // If there is only one terminator instruction, process it. |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 132 | if (!SecondLastOpc) { |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 133 | // Unconditional branch |
Akira Hatanaka | 6e55ff5 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 134 | if (LastOpc == UncondBrOpc) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 135 | TBB = LastInst->getOperand(0).getMBB(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 139 | // Conditional branch |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 140 | AnalyzeCondBr(LastInst, LastOpc, TBB, Cond); |
| 141 | return false; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 142 | } |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 143 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 144 | // If we reached here, there are two branches. |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 145 | // If there are three terminators, we don't know what sort of block this is. |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 146 | if (++I != REnd && isUnpredicatedTerminator(&*I)) |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 147 | return true; |
| 148 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 149 | // If second to last instruction is an unconditional branch, |
| 150 | // analyze it and remove the last instruction. |
Akira Hatanaka | 6e55ff5 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 151 | if (SecondLastOpc == UncondBrOpc) { |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 152 | // Return if the last instruction cannot be removed. |
| 153 | if (!AllowModify) |
| 154 | return true; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 156 | TBB = SecondLastInst->getOperand(0).getMBB(); |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 157 | LastInst->eraseFromParent(); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 161 | // Conditional branch followed by an unconditional branch. |
| 162 | // The last one must be unconditional. |
Akira Hatanaka | 6e55ff5 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 163 | if (LastOpc != UncondBrOpc) |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 164 | return true; |
| 165 | |
| 166 | AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond); |
| 167 | FBB = LastInst->getOperand(0).getMBB(); |
| 168 | |
| 169 | return false; |
Jia Liu | bb481f8 | 2012-02-28 07:46:26 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 172 | void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB, |
| 173 | MachineBasicBlock *TBB, DebugLoc DL, |
| 174 | const SmallVectorImpl<MachineOperand>& Cond) |
| 175 | const { |
| 176 | unsigned Opc = Cond[0].getImm(); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 177 | const MCInstrDesc &MCID = get(Opc); |
| 178 | MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID); |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 179 | |
| 180 | for (unsigned i = 1; i < Cond.size(); ++i) |
| 181 | MIB.addReg(Cond[i].getReg()); |
| 182 | |
| 183 | MIB.addMBB(TBB); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 186 | unsigned MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 187 | InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 188 | MachineBasicBlock *FBB, |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 189 | const SmallVectorImpl<MachineOperand> &Cond, |
| 190 | DebugLoc DL) const { |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 191 | // Shouldn't be a fall through. |
| 192 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 193 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 194 | // # of condition operands: |
| 195 | // Unconditional branches: 0 |
| 196 | // Floating point branches: 1 (opc) |
| 197 | // Int BranchZero: 2 (opc, reg) |
| 198 | // Int Branch: 3 (opc, reg0, reg1) |
| 199 | assert((Cond.size() <= 3) && |
| 200 | "# of Mips branch conditions must be <= 3!"); |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 201 | |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 202 | // Two-way Conditional branch. |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 203 | if (FBB) { |
| 204 | BuildCondBr(MBB, TBB, DL, Cond); |
Akira Hatanaka | 6e55ff5 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 205 | BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB); |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 206 | return 2; |
| 207 | } |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 208 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 209 | // One way branch. |
| 210 | // Unconditional branch. |
| 211 | if (Cond.empty()) |
Akira Hatanaka | 6e55ff5 | 2011-12-12 22:39:35 +0000 | [diff] [blame] | 212 | BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB); |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 213 | else // Conditional branch. |
| 214 | BuildCondBr(MBB, TBB, DL, Cond); |
| 215 | return 1; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 216 | } |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 217 | |
| 218 | unsigned MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 219 | RemoveBranch(MachineBasicBlock &MBB) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 220 | { |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 221 | MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend(); |
| 222 | MachineBasicBlock::reverse_iterator FirstBr; |
| 223 | unsigned removed; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 224 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 225 | // Skip all the debug instructions. |
| 226 | while (I != REnd && I->isDebugValue()) |
| 227 | ++I; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 228 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 229 | FirstBr = I; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 230 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 231 | // Up to 2 branches are removed. |
| 232 | // Note that indirect branches are not removed. |
| 233 | for(removed = 0; I != REnd && removed < 2; ++I, ++removed) |
| 234 | if (!GetAnalyzableBrOpc(I->getOpcode())) |
| 235 | break; |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 236 | |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 237 | MBB.erase(I.base(), FirstBr.base()); |
| 238 | |
| 239 | return removed; |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 242 | /// ReverseBranchCondition - Return the inverse opcode of the |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 243 | /// specified Branch instruction. |
| 244 | bool MipsInstrInfo:: |
Bruno Cardoso Lopes | 81092dc | 2011-03-04 17:51:39 +0000 | [diff] [blame] | 245 | ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 246 | { |
Akira Hatanaka | 20ada98 | 2011-04-01 17:39:08 +0000 | [diff] [blame] | 247 | assert( (Cond.size() && Cond.size() <= 3) && |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 248 | "Invalid Mips branch condition!"); |
Akira Hatanaka | 0bc1adb | 2012-07-31 21:49:49 +0000 | [diff] [blame] | 249 | Cond[0].setImm(GetOppositeBranchOpc(Cond[0].getImm())); |
Bruno Cardoso Lopes | 35d2a47 | 2007-08-18 01:56:48 +0000 | [diff] [blame] | 250 | return false; |
| 251 | } |
Dan Gohman | 9911405 | 2009-06-03 20:30:14 +0000 | [diff] [blame] | 252 | |
Akira Hatanaka | d4b48b2 | 2012-06-14 01:16:45 +0000 | [diff] [blame] | 253 | /// Return the number of bytes of code the specified instruction may be. |
| 254 | unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const { |
| 255 | switch (MI->getOpcode()) { |
| 256 | default: |
| 257 | return MI->getDesc().getSize(); |
| 258 | case TargetOpcode::INLINEASM: { // Inline Asm: Variable size. |
| 259 | const MachineFunction *MF = MI->getParent()->getParent(); |
| 260 | const char *AsmStr = MI->getOperand(0).getSymbolName(); |
| 261 | return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | unsigned |
| 267 | llvm::Mips::loadImmediate(int64_t Imm, bool IsN64, const TargetInstrInfo &TII, |
| 268 | MachineBasicBlock& MBB, |
| 269 | MachineBasicBlock::iterator II, DebugLoc DL, |
| 270 | bool LastInstrIsADDiu, |
| 271 | MipsAnalyzeImmediate::Inst *LastInst) { |
| 272 | MipsAnalyzeImmediate AnalyzeImm; |
| 273 | unsigned Size = IsN64 ? 64 : 32; |
| 274 | unsigned LUi = IsN64 ? Mips::LUi64 : Mips::LUi; |
| 275 | unsigned ZEROReg = IsN64 ? Mips::ZERO_64 : Mips::ZERO; |
| 276 | unsigned ATReg = IsN64 ? Mips::AT_64 : Mips::AT; |
| 277 | |
| 278 | const MipsAnalyzeImmediate::InstSeq &Seq = |
| 279 | AnalyzeImm.Analyze(Imm, Size, LastInstrIsADDiu); |
| 280 | MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin(); |
| 281 | |
| 282 | if (LastInst && (Seq.size() == 1)) { |
| 283 | *LastInst = *Inst; |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | // The first instruction can be a LUi, which is different from other |
| 288 | // instructions (ADDiu, ORI and SLL) in that it does not have a register |
| 289 | // operand. |
| 290 | if (Inst->Opc == LUi) |
| 291 | BuildMI(MBB, II, DL, TII.get(LUi), ATReg) |
| 292 | .addImm(SignExtend64<16>(Inst->ImmOpnd)); |
| 293 | else |
| 294 | BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ZEROReg) |
| 295 | .addImm(SignExtend64<16>(Inst->ImmOpnd)); |
| 296 | |
| 297 | // Build the remaining instructions in Seq. Skip the last instruction if |
| 298 | // LastInst is not 0. |
| 299 | for (++Inst; Inst != Seq.end() - !!LastInst; ++Inst) |
| 300 | BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ATReg) |
| 301 | .addImm(SignExtend64<16>(Inst->ImmOpnd)); |
| 302 | |
| 303 | if (LastInst) |
| 304 | *LastInst = *Inst; |
| 305 | |
| 306 | return Seq.size() - !!LastInst; |
| 307 | } |