Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1 | //===-- RISCVInstrInfo.cpp - RISCV Instruction Information ------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains the RISCV implementation of the TargetInstrInfo class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "RISCVInstrInfo.h" |
| 14 | #include "RISCV.h" |
| 15 | #include "RISCVSubtarget.h" |
| 16 | #include "RISCVTargetMachine.h" |
Shiva Chen | a49a16d | 2019-09-13 04:03:32 +0000 | [diff] [blame^] | 17 | #include "Utils/RISCVMatInt.h" |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/RegisterScavenging.h" |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/TargetRegistry.h" |
| 26 | |
| 27 | #define GET_INSTRINFO_CTOR_DTOR |
| 28 | #include "RISCVGenInstrInfo.inc" |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 32 | RISCVInstrInfo::RISCVInstrInfo() |
| 33 | : RISCVGenInstrInfo(RISCV::ADJCALLSTACKDOWN, RISCV::ADJCALLSTACKUP) {} |
Alex Bradbury | cfa6291 | 2017-11-08 12:20:01 +0000 | [diff] [blame] | 34 | |
Alex Bradbury | fda6037 | 2018-04-26 15:34:27 +0000 | [diff] [blame] | 35 | unsigned RISCVInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, |
| 36 | int &FrameIndex) const { |
| 37 | switch (MI.getOpcode()) { |
| 38 | default: |
| 39 | return 0; |
| 40 | case RISCV::LB: |
| 41 | case RISCV::LBU: |
| 42 | case RISCV::LH: |
| 43 | case RISCV::LHU: |
| 44 | case RISCV::LW: |
| 45 | case RISCV::FLW: |
| 46 | case RISCV::LWU: |
| 47 | case RISCV::LD: |
| 48 | case RISCV::FLD: |
| 49 | break; |
| 50 | } |
| 51 | |
| 52 | if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() && |
| 53 | MI.getOperand(2).getImm() == 0) { |
| 54 | FrameIndex = MI.getOperand(1).getIndex(); |
| 55 | return MI.getOperand(0).getReg(); |
| 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | unsigned RISCVInstrInfo::isStoreToStackSlot(const MachineInstr &MI, |
| 62 | int &FrameIndex) const { |
| 63 | switch (MI.getOpcode()) { |
| 64 | default: |
| 65 | return 0; |
| 66 | case RISCV::SB: |
| 67 | case RISCV::SH: |
| 68 | case RISCV::SW: |
| 69 | case RISCV::FSW: |
| 70 | case RISCV::SD: |
| 71 | case RISCV::FSD: |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | if (MI.getOperand(0).isFI() && MI.getOperand(1).isImm() && |
| 76 | MI.getOperand(1).getImm() == 0) { |
| 77 | FrameIndex = MI.getOperand(0).getIndex(); |
| 78 | return MI.getOperand(2).getReg(); |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Alex Bradbury | cfa6291 | 2017-11-08 12:20:01 +0000 | [diff] [blame] | 84 | void RISCVInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 85 | MachineBasicBlock::iterator MBBI, |
| 86 | const DebugLoc &DL, unsigned DstReg, |
| 87 | unsigned SrcReg, bool KillSrc) const { |
Alex Bradbury | 65d6ea5 | 2018-03-21 15:11:02 +0000 | [diff] [blame] | 88 | if (RISCV::GPRRegClass.contains(DstReg, SrcReg)) { |
| 89 | BuildMI(MBB, MBBI, DL, get(RISCV::ADDI), DstReg) |
| 90 | .addReg(SrcReg, getKillRegState(KillSrc)) |
| 91 | .addImm(0); |
| 92 | return; |
| 93 | } |
Alex Bradbury | cfa6291 | 2017-11-08 12:20:01 +0000 | [diff] [blame] | 94 | |
Alex Bradbury | 21d28fe | 2018-04-12 05:50:06 +0000 | [diff] [blame] | 95 | // FPR->FPR copies |
| 96 | unsigned Opc; |
| 97 | if (RISCV::FPR32RegClass.contains(DstReg, SrcReg)) |
| 98 | Opc = RISCV::FSGNJ_S; |
| 99 | else if (RISCV::FPR64RegClass.contains(DstReg, SrcReg)) |
| 100 | Opc = RISCV::FSGNJ_D; |
| 101 | else |
| 102 | llvm_unreachable("Impossible reg-to-reg copy"); |
Alex Bradbury | 65d6ea5 | 2018-03-21 15:11:02 +0000 | [diff] [blame] | 103 | |
Alex Bradbury | 21d28fe | 2018-04-12 05:50:06 +0000 | [diff] [blame] | 104 | BuildMI(MBB, MBBI, DL, get(Opc), DstReg) |
| 105 | .addReg(SrcReg, getKillRegState(KillSrc)) |
| 106 | .addReg(SrcReg, getKillRegState(KillSrc)); |
Alex Bradbury | cfa6291 | 2017-11-08 12:20:01 +0000 | [diff] [blame] | 107 | } |
Alex Bradbury | 74913e1 | 2017-11-08 13:31:40 +0000 | [diff] [blame] | 108 | |
| 109 | void RISCVInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
| 110 | MachineBasicBlock::iterator I, |
| 111 | unsigned SrcReg, bool IsKill, int FI, |
| 112 | const TargetRegisterClass *RC, |
| 113 | const TargetRegisterInfo *TRI) const { |
| 114 | DebugLoc DL; |
| 115 | if (I != MBB.end()) |
| 116 | DL = I->getDebugLoc(); |
| 117 | |
Alex Bradbury | 65d6ea5 | 2018-03-21 15:11:02 +0000 | [diff] [blame] | 118 | unsigned Opcode; |
| 119 | |
Alex Bradbury | 87a54d6 | 2017-12-07 12:45:05 +0000 | [diff] [blame] | 120 | if (RISCV::GPRRegClass.hasSubClassEq(RC)) |
Ana Pazos | 2e4106b | 2018-07-26 17:49:43 +0000 | [diff] [blame] | 121 | Opcode = TRI->getRegSizeInBits(RISCV::GPRRegClass) == 32 ? |
| 122 | RISCV::SW : RISCV::SD; |
Alex Bradbury | 65d6ea5 | 2018-03-21 15:11:02 +0000 | [diff] [blame] | 123 | else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) |
| 124 | Opcode = RISCV::FSW; |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 125 | else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) |
| 126 | Opcode = RISCV::FSD; |
Alex Bradbury | 74913e1 | 2017-11-08 13:31:40 +0000 | [diff] [blame] | 127 | else |
| 128 | llvm_unreachable("Can't store this register to stack slot"); |
Alex Bradbury | 65d6ea5 | 2018-03-21 15:11:02 +0000 | [diff] [blame] | 129 | |
| 130 | BuildMI(MBB, I, DL, get(Opcode)) |
| 131 | .addReg(SrcReg, getKillRegState(IsKill)) |
| 132 | .addFrameIndex(FI) |
| 133 | .addImm(0); |
Alex Bradbury | 74913e1 | 2017-11-08 13:31:40 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void RISCVInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
| 137 | MachineBasicBlock::iterator I, |
| 138 | unsigned DstReg, int FI, |
| 139 | const TargetRegisterClass *RC, |
| 140 | const TargetRegisterInfo *TRI) const { |
| 141 | DebugLoc DL; |
| 142 | if (I != MBB.end()) |
| 143 | DL = I->getDebugLoc(); |
| 144 | |
Alex Bradbury | 65d6ea5 | 2018-03-21 15:11:02 +0000 | [diff] [blame] | 145 | unsigned Opcode; |
| 146 | |
Alex Bradbury | 87a54d6 | 2017-12-07 12:45:05 +0000 | [diff] [blame] | 147 | if (RISCV::GPRRegClass.hasSubClassEq(RC)) |
Ana Pazos | 2e4106b | 2018-07-26 17:49:43 +0000 | [diff] [blame] | 148 | Opcode = TRI->getRegSizeInBits(RISCV::GPRRegClass) == 32 ? |
| 149 | RISCV::LW : RISCV::LD; |
Alex Bradbury | 65d6ea5 | 2018-03-21 15:11:02 +0000 | [diff] [blame] | 150 | else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) |
| 151 | Opcode = RISCV::FLW; |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 152 | else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) |
| 153 | Opcode = RISCV::FLD; |
Alex Bradbury | 74913e1 | 2017-11-08 13:31:40 +0000 | [diff] [blame] | 154 | else |
| 155 | llvm_unreachable("Can't load this register from stack slot"); |
Alex Bradbury | 65d6ea5 | 2018-03-21 15:11:02 +0000 | [diff] [blame] | 156 | |
| 157 | BuildMI(MBB, I, DL, get(Opcode), DstReg).addFrameIndex(FI).addImm(0); |
Alex Bradbury | 74913e1 | 2017-11-08 13:31:40 +0000 | [diff] [blame] | 158 | } |
Alex Bradbury | 9fea488 | 2018-01-10 19:53:46 +0000 | [diff] [blame] | 159 | |
Shiva Chen | a49a16d | 2019-09-13 04:03:32 +0000 | [diff] [blame^] | 160 | void RISCVInstrInfo::movImm(MachineBasicBlock &MBB, |
| 161 | MachineBasicBlock::iterator MBBI, |
| 162 | const DebugLoc &DL, Register DstReg, uint64_t Val, |
| 163 | MachineInstr::MIFlag Flag) const { |
| 164 | MachineFunction *MF = MBB.getParent(); |
| 165 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 166 | bool IsRV64 = MF->getSubtarget<RISCVSubtarget>().is64Bit(); |
| 167 | Register SrcReg = RISCV::X0; |
| 168 | Register Result = MRI.createVirtualRegister(&RISCV::GPRRegClass); |
| 169 | unsigned Num = 0; |
Alex Bradbury | 9fea488 | 2018-01-10 19:53:46 +0000 | [diff] [blame] | 170 | |
Shiva Chen | a49a16d | 2019-09-13 04:03:32 +0000 | [diff] [blame^] | 171 | if (!IsRV64 && !isInt<32>(Val)) |
| 172 | report_fatal_error("Should only materialize 32-bit constants for RV32"); |
Alex Bradbury | 099c720 | 2018-04-18 19:02:31 +0000 | [diff] [blame] | 173 | |
Shiva Chen | a49a16d | 2019-09-13 04:03:32 +0000 | [diff] [blame^] | 174 | RISCVMatInt::InstSeq Seq; |
| 175 | RISCVMatInt::generateInstSeq(Val, IsRV64, Seq); |
| 176 | assert(Seq.size() > 0); |
| 177 | |
| 178 | for (RISCVMatInt::Inst &Inst : Seq) { |
| 179 | // Write the final result to DstReg if it's the last instruction in the Seq. |
| 180 | // Otherwise, write the result to the temp register. |
| 181 | if (++Num == Seq.size()) |
| 182 | Result = DstReg; |
| 183 | |
| 184 | if (Inst.Opc == RISCV::LUI) { |
| 185 | BuildMI(MBB, MBBI, DL, get(RISCV::LUI), Result) |
| 186 | .addImm(Inst.Imm) |
| 187 | .setMIFlag(Flag); |
| 188 | } else { |
| 189 | BuildMI(MBB, MBBI, DL, get(Inst.Opc), Result) |
| 190 | .addReg(SrcReg, RegState::Kill) |
| 191 | .addImm(Inst.Imm) |
| 192 | .setMIFlag(Flag); |
| 193 | } |
| 194 | // Only the first instruction has X0 as its source. |
| 195 | SrcReg = Result; |
| 196 | } |
Alex Bradbury | 9fea488 | 2018-01-10 19:53:46 +0000 | [diff] [blame] | 197 | } |
Alex Bradbury | e027c93 | 2018-01-10 20:47:00 +0000 | [diff] [blame] | 198 | |
| 199 | // The contents of values added to Cond are not examined outside of |
| 200 | // RISCVInstrInfo, giving us flexibility in what to push to it. For RISCV, we |
| 201 | // push BranchOpcode, Reg1, Reg2. |
| 202 | static void parseCondBranch(MachineInstr &LastInst, MachineBasicBlock *&Target, |
| 203 | SmallVectorImpl<MachineOperand> &Cond) { |
| 204 | // Block ends with fall-through condbranch. |
| 205 | assert(LastInst.getDesc().isConditionalBranch() && |
| 206 | "Unknown conditional branch"); |
| 207 | Target = LastInst.getOperand(2).getMBB(); |
| 208 | Cond.push_back(MachineOperand::CreateImm(LastInst.getOpcode())); |
| 209 | Cond.push_back(LastInst.getOperand(0)); |
| 210 | Cond.push_back(LastInst.getOperand(1)); |
| 211 | } |
| 212 | |
| 213 | static unsigned getOppositeBranchOpcode(int Opc) { |
| 214 | switch (Opc) { |
| 215 | default: |
| 216 | llvm_unreachable("Unrecognized conditional branch"); |
| 217 | case RISCV::BEQ: |
| 218 | return RISCV::BNE; |
| 219 | case RISCV::BNE: |
| 220 | return RISCV::BEQ; |
| 221 | case RISCV::BLT: |
| 222 | return RISCV::BGE; |
| 223 | case RISCV::BGE: |
| 224 | return RISCV::BLT; |
| 225 | case RISCV::BLTU: |
| 226 | return RISCV::BGEU; |
| 227 | case RISCV::BGEU: |
| 228 | return RISCV::BLTU; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock &MBB, |
| 233 | MachineBasicBlock *&TBB, |
| 234 | MachineBasicBlock *&FBB, |
| 235 | SmallVectorImpl<MachineOperand> &Cond, |
| 236 | bool AllowModify) const { |
| 237 | TBB = FBB = nullptr; |
| 238 | Cond.clear(); |
| 239 | |
| 240 | // If the block has no terminators, it just falls into the block after it. |
| 241 | MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); |
| 242 | if (I == MBB.end() || !isUnpredicatedTerminator(*I)) |
| 243 | return false; |
| 244 | |
| 245 | // Count the number of terminators and find the first unconditional or |
| 246 | // indirect branch. |
| 247 | MachineBasicBlock::iterator FirstUncondOrIndirectBr = MBB.end(); |
| 248 | int NumTerminators = 0; |
| 249 | for (auto J = I.getReverse(); J != MBB.rend() && isUnpredicatedTerminator(*J); |
| 250 | J++) { |
| 251 | NumTerminators++; |
| 252 | if (J->getDesc().isUnconditionalBranch() || |
| 253 | J->getDesc().isIndirectBranch()) { |
| 254 | FirstUncondOrIndirectBr = J.getReverse(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // If AllowModify is true, we can erase any terminators after |
| 259 | // FirstUncondOrIndirectBR. |
| 260 | if (AllowModify && FirstUncondOrIndirectBr != MBB.end()) { |
| 261 | while (std::next(FirstUncondOrIndirectBr) != MBB.end()) { |
| 262 | std::next(FirstUncondOrIndirectBr)->eraseFromParent(); |
| 263 | NumTerminators--; |
| 264 | } |
| 265 | I = FirstUncondOrIndirectBr; |
| 266 | } |
| 267 | |
| 268 | // We can't handle blocks that end in an indirect branch. |
| 269 | if (I->getDesc().isIndirectBranch()) |
| 270 | return true; |
| 271 | |
| 272 | // We can't handle blocks with more than 2 terminators. |
| 273 | if (NumTerminators > 2) |
| 274 | return true; |
| 275 | |
| 276 | // Handle a single unconditional branch. |
| 277 | if (NumTerminators == 1 && I->getDesc().isUnconditionalBranch()) { |
| 278 | TBB = I->getOperand(0).getMBB(); |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | // Handle a single conditional branch. |
| 283 | if (NumTerminators == 1 && I->getDesc().isConditionalBranch()) { |
| 284 | parseCondBranch(*I, TBB, Cond); |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | // Handle a conditional branch followed by an unconditional branch. |
| 289 | if (NumTerminators == 2 && std::prev(I)->getDesc().isConditionalBranch() && |
| 290 | I->getDesc().isUnconditionalBranch()) { |
| 291 | parseCondBranch(*std::prev(I), TBB, Cond); |
| 292 | FBB = I->getOperand(0).getMBB(); |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | // Otherwise, we can't handle this. |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | unsigned RISCVInstrInfo::removeBranch(MachineBasicBlock &MBB, |
| 301 | int *BytesRemoved) const { |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 302 | if (BytesRemoved) |
| 303 | *BytesRemoved = 0; |
Alex Bradbury | e027c93 | 2018-01-10 20:47:00 +0000 | [diff] [blame] | 304 | MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); |
| 305 | if (I == MBB.end()) |
| 306 | return 0; |
| 307 | |
| 308 | if (!I->getDesc().isUnconditionalBranch() && |
| 309 | !I->getDesc().isConditionalBranch()) |
| 310 | return 0; |
| 311 | |
| 312 | // Remove the branch. |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 313 | if (BytesRemoved) |
| 314 | *BytesRemoved += getInstSizeInBytes(*I); |
Alex Bradbury | ad73a43 | 2019-07-18 03:23:47 +0000 | [diff] [blame] | 315 | I->eraseFromParent(); |
Alex Bradbury | e027c93 | 2018-01-10 20:47:00 +0000 | [diff] [blame] | 316 | |
| 317 | I = MBB.end(); |
| 318 | |
| 319 | if (I == MBB.begin()) |
| 320 | return 1; |
| 321 | --I; |
| 322 | if (!I->getDesc().isConditionalBranch()) |
| 323 | return 1; |
| 324 | |
| 325 | // Remove the branch. |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 326 | if (BytesRemoved) |
| 327 | *BytesRemoved += getInstSizeInBytes(*I); |
Alex Bradbury | ad73a43 | 2019-07-18 03:23:47 +0000 | [diff] [blame] | 328 | I->eraseFromParent(); |
Alex Bradbury | e027c93 | 2018-01-10 20:47:00 +0000 | [diff] [blame] | 329 | return 2; |
| 330 | } |
| 331 | |
| 332 | // Inserts a branch into the end of the specific MachineBasicBlock, returning |
| 333 | // the number of instructions inserted. |
| 334 | unsigned RISCVInstrInfo::insertBranch( |
| 335 | MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, |
| 336 | ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const { |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 337 | if (BytesAdded) |
| 338 | *BytesAdded = 0; |
Alex Bradbury | e027c93 | 2018-01-10 20:47:00 +0000 | [diff] [blame] | 339 | |
| 340 | // Shouldn't be a fall through. |
| 341 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
| 342 | assert((Cond.size() == 3 || Cond.size() == 0) && |
| 343 | "RISCV branch conditions have two components!"); |
| 344 | |
| 345 | // Unconditional branch. |
| 346 | if (Cond.empty()) { |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 347 | MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(TBB); |
| 348 | if (BytesAdded) |
| 349 | *BytesAdded += getInstSizeInBytes(MI); |
Alex Bradbury | e027c93 | 2018-01-10 20:47:00 +0000 | [diff] [blame] | 350 | return 1; |
| 351 | } |
| 352 | |
| 353 | // Either a one or two-way conditional branch. |
| 354 | unsigned Opc = Cond[0].getImm(); |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 355 | MachineInstr &CondMI = |
| 356 | *BuildMI(&MBB, DL, get(Opc)).add(Cond[1]).add(Cond[2]).addMBB(TBB); |
| 357 | if (BytesAdded) |
| 358 | *BytesAdded += getInstSizeInBytes(CondMI); |
Alex Bradbury | e027c93 | 2018-01-10 20:47:00 +0000 | [diff] [blame] | 359 | |
| 360 | // One-way conditional branch. |
| 361 | if (!FBB) |
| 362 | return 1; |
| 363 | |
| 364 | // Two-way conditional branch. |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 365 | MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(FBB); |
| 366 | if (BytesAdded) |
| 367 | *BytesAdded += getInstSizeInBytes(MI); |
Alex Bradbury | e027c93 | 2018-01-10 20:47:00 +0000 | [diff] [blame] | 368 | return 2; |
| 369 | } |
| 370 | |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 371 | unsigned RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB, |
| 372 | MachineBasicBlock &DestBB, |
| 373 | const DebugLoc &DL, |
| 374 | int64_t BrOffset, |
| 375 | RegScavenger *RS) const { |
| 376 | assert(RS && "RegScavenger required for long branching"); |
| 377 | assert(MBB.empty() && |
| 378 | "new block should be inserted for expanding unconditional branch"); |
| 379 | assert(MBB.pred_size() == 1); |
| 380 | |
| 381 | MachineFunction *MF = MBB.getParent(); |
| 382 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 383 | const auto &TM = static_cast<const RISCVTargetMachine &>(MF->getTarget()); |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 384 | |
Alex Bradbury | 5bf3b20 | 2018-10-04 14:30:03 +0000 | [diff] [blame] | 385 | if (TM.isPositionIndependent()) |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 386 | report_fatal_error("Unable to insert indirect branch"); |
| 387 | |
| 388 | if (!isInt<32>(BrOffset)) |
| 389 | report_fatal_error( |
| 390 | "Branch offsets outside of the signed 32-bit range not supported"); |
| 391 | |
| 392 | // FIXME: A virtual register must be used initially, as the register |
| 393 | // scavenger won't work with empty blocks (SIInstrInfo::insertIndirectBranch |
| 394 | // uses the same workaround). |
Daniel Sanders | 3836874 | 2019-08-12 22:41:02 +0000 | [diff] [blame] | 395 | Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass); |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 396 | auto II = MBB.end(); |
| 397 | |
| 398 | MachineInstr &LuiMI = *BuildMI(MBB, II, DL, get(RISCV::LUI), ScratchReg) |
| 399 | .addMBB(&DestBB, RISCVII::MO_HI); |
| 400 | BuildMI(MBB, II, DL, get(RISCV::PseudoBRIND)) |
| 401 | .addReg(ScratchReg, RegState::Kill) |
| 402 | .addMBB(&DestBB, RISCVII::MO_LO); |
| 403 | |
| 404 | RS->enterBasicBlockEnd(MBB); |
Alex Bradbury | 2c6c84e | 2019-03-11 20:43:29 +0000 | [diff] [blame] | 405 | unsigned Scav = RS->scavengeRegisterBackwards(RISCV::GPRRegClass, |
| 406 | LuiMI.getIterator(), false, 0); |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 407 | MRI.replaceRegWith(ScratchReg, Scav); |
| 408 | MRI.clearVirtRegs(); |
| 409 | RS->setRegUsed(Scav); |
| 410 | return 8; |
| 411 | } |
| 412 | |
Alex Bradbury | e027c93 | 2018-01-10 20:47:00 +0000 | [diff] [blame] | 413 | bool RISCVInstrInfo::reverseBranchCondition( |
| 414 | SmallVectorImpl<MachineOperand> &Cond) const { |
| 415 | assert((Cond.size() == 3) && "Invalid branch condition!"); |
| 416 | Cond[0].setImm(getOppositeBranchOpcode(Cond[0].getImm())); |
| 417 | return false; |
| 418 | } |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 419 | |
| 420 | MachineBasicBlock * |
| 421 | RISCVInstrInfo::getBranchDestBlock(const MachineInstr &MI) const { |
| 422 | assert(MI.getDesc().isBranch() && "Unexpected opcode!"); |
| 423 | // The branch target is always the last operand. |
| 424 | int NumOp = MI.getNumExplicitOperands(); |
| 425 | return MI.getOperand(NumOp - 1).getMBB(); |
| 426 | } |
| 427 | |
| 428 | bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp, |
| 429 | int64_t BrOffset) const { |
| 430 | // Ideally we could determine the supported branch offset from the |
| 431 | // RISCVII::FormMask, but this can't be used for Pseudo instructions like |
| 432 | // PseudoBR. |
| 433 | switch (BranchOp) { |
| 434 | default: |
| 435 | llvm_unreachable("Unexpected opcode!"); |
| 436 | case RISCV::BEQ: |
| 437 | case RISCV::BNE: |
| 438 | case RISCV::BLT: |
| 439 | case RISCV::BGE: |
| 440 | case RISCV::BLTU: |
| 441 | case RISCV::BGEU: |
| 442 | return isIntN(13, BrOffset); |
| 443 | case RISCV::JAL: |
| 444 | case RISCV::PseudoBR: |
| 445 | return isIntN(21, BrOffset); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | unsigned RISCVInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { |
| 450 | unsigned Opcode = MI.getOpcode(); |
| 451 | |
| 452 | switch (Opcode) { |
| 453 | default: { return get(Opcode).getSize(); } |
| 454 | case TargetOpcode::EH_LABEL: |
| 455 | case TargetOpcode::IMPLICIT_DEF: |
| 456 | case TargetOpcode::KILL: |
| 457 | case TargetOpcode::DBG_VALUE: |
| 458 | return 0; |
Lewis Revill | cf74881 | 2019-06-26 10:35:58 +0000 | [diff] [blame] | 459 | case RISCV::PseudoCALLReg: |
Shiva Chen | d58bd8d | 2018-04-25 14:19:12 +0000 | [diff] [blame] | 460 | case RISCV::PseudoCALL: |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 461 | case RISCV::PseudoTAIL: |
Alex Bradbury | da20f5c | 2019-04-01 14:42:56 +0000 | [diff] [blame] | 462 | case RISCV::PseudoLLA: |
Lewis Revill | a524036 | 2019-06-11 12:57:47 +0000 | [diff] [blame] | 463 | case RISCV::PseudoLA: |
Lewis Revill | 39263ac | 2019-06-19 08:40:59 +0000 | [diff] [blame] | 464 | case RISCV::PseudoLA_TLS_IE: |
| 465 | case RISCV::PseudoLA_TLS_GD: |
Shiva Chen | d58bd8d | 2018-04-25 14:19:12 +0000 | [diff] [blame] | 466 | return 8; |
Craig Topper | 784929d | 2019-02-08 20:48:56 +0000 | [diff] [blame] | 467 | case TargetOpcode::INLINEASM: |
| 468 | case TargetOpcode::INLINEASM_BR: { |
Alex Bradbury | 315cd3a | 2018-01-10 21:05:07 +0000 | [diff] [blame] | 469 | const MachineFunction &MF = *MI.getParent()->getParent(); |
| 470 | const auto &TM = static_cast<const RISCVTargetMachine &>(MF.getTarget()); |
| 471 | return getInlineAsmLength(MI.getOperand(0).getSymbolName(), |
| 472 | *TM.getMCAsmInfo()); |
| 473 | } |
| 474 | } |
| 475 | } |
Ana Pazos | 05a6064 | 2019-01-25 20:22:49 +0000 | [diff] [blame] | 476 | |
| 477 | bool RISCVInstrInfo::isAsCheapAsAMove(const MachineInstr &MI) const { |
| 478 | const unsigned Opcode = MI.getOpcode(); |
| 479 | switch(Opcode) { |
| 480 | default: |
| 481 | break; |
| 482 | case RISCV::ADDI: |
| 483 | case RISCV::ORI: |
| 484 | case RISCV::XORI: |
| 485 | return (MI.getOperand(1).isReg() && MI.getOperand(1).getReg() == RISCV::X0); |
| 486 | } |
| 487 | return MI.isAsCheapAsAMove(); |
| 488 | } |