Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- CodeGen/MachineInstrBuilder.h - Simplify creation of MIs --*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file exposes a function named BuildMI, which is useful for dramatically |
| 10 | // simplifying how MachineInstr's are created. It allows use of code like this: |
| 11 | // |
| 12 | // M = BuildMI(MBB, MI, DL, TII.get(X86::ADD8rr), Dst) |
| 13 | // .addReg(argVal1) |
| 14 | // .addReg(argVal2); |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H |
| 19 | #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H |
| 20 | |
| 21 | #include "llvm/ADT/ArrayRef.h" |
| 22 | #include "llvm/CodeGen/GlobalISel/Utils.h" |
| 23 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 24 | #include "llvm/CodeGen/MachineFunction.h" |
| 25 | #include "llvm/CodeGen/MachineInstr.h" |
| 26 | #include "llvm/CodeGen/MachineInstrBundle.h" |
| 27 | #include "llvm/CodeGen/MachineOperand.h" |
| 28 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 29 | #include "llvm/IR/InstrTypes.h" |
| 30 | #include "llvm/IR/Intrinsics.h" |
| 31 | #include "llvm/Support/ErrorHandling.h" |
| 32 | #include <cassert> |
| 33 | #include <cstdint> |
| 34 | #include <utility> |
| 35 | |
| 36 | namespace llvm { |
| 37 | |
| 38 | class MCInstrDesc; |
| 39 | class MDNode; |
| 40 | |
| 41 | namespace RegState { |
| 42 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 43 | enum { |
| 44 | /// Register definition. |
| 45 | Define = 0x2, |
| 46 | /// Not emitted register (e.g. carry, or temporary result). |
| 47 | Implicit = 0x4, |
| 48 | /// The last use of a register. |
| 49 | Kill = 0x8, |
| 50 | /// Unused definition. |
| 51 | Dead = 0x10, |
| 52 | /// Value of the register doesn't matter. |
| 53 | Undef = 0x20, |
| 54 | /// Register definition happens before uses. |
| 55 | EarlyClobber = 0x40, |
| 56 | /// Register 'use' is for debugging purpose. |
| 57 | Debug = 0x80, |
| 58 | /// Register reads a value that is defined inside the same instruction or |
| 59 | /// bundle. |
| 60 | InternalRead = 0x100, |
| 61 | /// Register that may be renamed. |
| 62 | Renamable = 0x200, |
| 63 | DefineNoRead = Define | Undef, |
| 64 | ImplicitDefine = Implicit | Define, |
| 65 | ImplicitKill = Implicit | Kill |
| 66 | }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 67 | |
| 68 | } // end namespace RegState |
| 69 | |
| 70 | class MachineInstrBuilder { |
| 71 | MachineFunction *MF = nullptr; |
| 72 | MachineInstr *MI = nullptr; |
| 73 | |
| 74 | public: |
| 75 | MachineInstrBuilder() = default; |
| 76 | |
| 77 | /// Create a MachineInstrBuilder for manipulating an existing instruction. |
| 78 | /// F must be the machine function that was used to allocate I. |
| 79 | MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {} |
| 80 | MachineInstrBuilder(MachineFunction &F, MachineBasicBlock::iterator I) |
| 81 | : MF(&F), MI(&*I) {} |
| 82 | |
| 83 | /// Allow automatic conversion to the machine instruction we are working on. |
| 84 | operator MachineInstr*() const { return MI; } |
| 85 | MachineInstr *operator->() const { return MI; } |
| 86 | operator MachineBasicBlock::iterator() const { return MI; } |
| 87 | |
| 88 | /// If conversion operators fail, use this method to get the MachineInstr |
| 89 | /// explicitly. |
| 90 | MachineInstr *getInstr() const { return MI; } |
| 91 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 92 | /// Get the register for the operand index. |
| 93 | /// The operand at the index should be a register (asserted by |
| 94 | /// MachineOperand). |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 95 | Register getReg(unsigned Idx) const { return MI->getOperand(Idx).getReg(); } |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 96 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 97 | /// Add a new virtual register operand. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 98 | const MachineInstrBuilder &addReg(Register RegNo, unsigned flags = 0, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 99 | unsigned SubReg = 0) const { |
| 100 | assert((flags & 0x1) == 0 && |
| 101 | "Passing in 'true' to addReg is forbidden! Use enums instead."); |
| 102 | MI->addOperand(*MF, MachineOperand::CreateReg(RegNo, |
| 103 | flags & RegState::Define, |
| 104 | flags & RegState::Implicit, |
| 105 | flags & RegState::Kill, |
| 106 | flags & RegState::Dead, |
| 107 | flags & RegState::Undef, |
| 108 | flags & RegState::EarlyClobber, |
| 109 | SubReg, |
| 110 | flags & RegState::Debug, |
| 111 | flags & RegState::InternalRead, |
| 112 | flags & RegState::Renamable)); |
| 113 | return *this; |
| 114 | } |
| 115 | |
| 116 | /// Add a virtual register definition operand. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 117 | const MachineInstrBuilder &addDef(Register RegNo, unsigned Flags = 0, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 118 | unsigned SubReg = 0) const { |
| 119 | return addReg(RegNo, Flags | RegState::Define, SubReg); |
| 120 | } |
| 121 | |
| 122 | /// Add a virtual register use operand. It is an error for Flags to contain |
| 123 | /// `RegState::Define` when calling this function. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 124 | const MachineInstrBuilder &addUse(Register RegNo, unsigned Flags = 0, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 125 | unsigned SubReg = 0) const { |
| 126 | assert(!(Flags & RegState::Define) && |
| 127 | "Misleading addUse defines register, use addReg instead."); |
| 128 | return addReg(RegNo, Flags, SubReg); |
| 129 | } |
| 130 | |
| 131 | /// Add a new immediate operand. |
| 132 | const MachineInstrBuilder &addImm(int64_t Val) const { |
| 133 | MI->addOperand(*MF, MachineOperand::CreateImm(Val)); |
| 134 | return *this; |
| 135 | } |
| 136 | |
| 137 | const MachineInstrBuilder &addCImm(const ConstantInt *Val) const { |
| 138 | MI->addOperand(*MF, MachineOperand::CreateCImm(Val)); |
| 139 | return *this; |
| 140 | } |
| 141 | |
| 142 | const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const { |
| 143 | MI->addOperand(*MF, MachineOperand::CreateFPImm(Val)); |
| 144 | return *this; |
| 145 | } |
| 146 | |
| 147 | const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 148 | unsigned TargetFlags = 0) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 149 | MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags)); |
| 150 | return *this; |
| 151 | } |
| 152 | |
| 153 | const MachineInstrBuilder &addFrameIndex(int Idx) const { |
| 154 | MI->addOperand(*MF, MachineOperand::CreateFI(Idx)); |
| 155 | return *this; |
| 156 | } |
| 157 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 158 | const MachineInstrBuilder & |
| 159 | addConstantPoolIndex(unsigned Idx, int Offset = 0, |
| 160 | unsigned TargetFlags = 0) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 161 | MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags)); |
| 162 | return *this; |
| 163 | } |
| 164 | |
| 165 | const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 166 | unsigned TargetFlags = 0) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 167 | MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset, |
| 168 | TargetFlags)); |
| 169 | return *this; |
| 170 | } |
| 171 | |
| 172 | const MachineInstrBuilder &addJumpTableIndex(unsigned Idx, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 173 | unsigned TargetFlags = 0) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 174 | MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags)); |
| 175 | return *this; |
| 176 | } |
| 177 | |
| 178 | const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV, |
| 179 | int64_t Offset = 0, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 180 | unsigned TargetFlags = 0) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 181 | MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags)); |
| 182 | return *this; |
| 183 | } |
| 184 | |
| 185 | const MachineInstrBuilder &addExternalSymbol(const char *FnName, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 186 | unsigned TargetFlags = 0) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 187 | MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags)); |
| 188 | return *this; |
| 189 | } |
| 190 | |
| 191 | const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA, |
| 192 | int64_t Offset = 0, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 193 | unsigned TargetFlags = 0) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 194 | MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags)); |
| 195 | return *this; |
| 196 | } |
| 197 | |
| 198 | const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const { |
| 199 | MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask)); |
| 200 | return *this; |
| 201 | } |
| 202 | |
| 203 | const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const { |
| 204 | MI->addMemOperand(*MF, MMO); |
| 205 | return *this; |
| 206 | } |
| 207 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 208 | const MachineInstrBuilder & |
| 209 | setMemRefs(ArrayRef<MachineMemOperand *> MMOs) const { |
| 210 | MI->setMemRefs(*MF, MMOs); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 211 | return *this; |
| 212 | } |
| 213 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 214 | const MachineInstrBuilder &cloneMemRefs(const MachineInstr &OtherMI) const { |
| 215 | MI->cloneMemRefs(*MF, OtherMI); |
| 216 | return *this; |
| 217 | } |
| 218 | |
| 219 | const MachineInstrBuilder & |
| 220 | cloneMergedMemRefs(ArrayRef<const MachineInstr *> OtherMIs) const { |
| 221 | MI->cloneMergedMemRefs(*MF, OtherMIs); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 222 | return *this; |
| 223 | } |
| 224 | |
| 225 | const MachineInstrBuilder &add(const MachineOperand &MO) const { |
| 226 | MI->addOperand(*MF, MO); |
| 227 | return *this; |
| 228 | } |
| 229 | |
| 230 | const MachineInstrBuilder &add(ArrayRef<MachineOperand> MOs) const { |
| 231 | for (const MachineOperand &MO : MOs) { |
| 232 | MI->addOperand(*MF, MO); |
| 233 | } |
| 234 | return *this; |
| 235 | } |
| 236 | |
| 237 | const MachineInstrBuilder &addMetadata(const MDNode *MD) const { |
| 238 | MI->addOperand(*MF, MachineOperand::CreateMetadata(MD)); |
| 239 | assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable()) |
| 240 | : true) && |
| 241 | "first MDNode argument of a DBG_VALUE not a variable"); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 242 | assert((MI->isDebugLabel() ? static_cast<bool>(MI->getDebugLabel()) |
| 243 | : true) && |
| 244 | "first MDNode argument of a DBG_LABEL not a label"); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 245 | return *this; |
| 246 | } |
| 247 | |
| 248 | const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const { |
| 249 | MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex)); |
| 250 | return *this; |
| 251 | } |
| 252 | |
| 253 | const MachineInstrBuilder &addIntrinsicID(Intrinsic::ID ID) const { |
| 254 | MI->addOperand(*MF, MachineOperand::CreateIntrinsicID(ID)); |
| 255 | return *this; |
| 256 | } |
| 257 | |
| 258 | const MachineInstrBuilder &addPredicate(CmpInst::Predicate Pred) const { |
| 259 | MI->addOperand(*MF, MachineOperand::CreatePredicate(Pred)); |
| 260 | return *this; |
| 261 | } |
| 262 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 263 | const MachineInstrBuilder &addShuffleMask(ArrayRef<int> Val) const { |
| 264 | MI->addOperand(*MF, MachineOperand::CreateShuffleMask(Val)); |
| 265 | return *this; |
| 266 | } |
| 267 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 268 | const MachineInstrBuilder &addSym(MCSymbol *Sym, |
| 269 | unsigned char TargetFlags = 0) const { |
| 270 | MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags)); |
| 271 | return *this; |
| 272 | } |
| 273 | |
| 274 | const MachineInstrBuilder &setMIFlags(unsigned Flags) const { |
| 275 | MI->setFlags(Flags); |
| 276 | return *this; |
| 277 | } |
| 278 | |
| 279 | const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const { |
| 280 | MI->setFlag(Flag); |
| 281 | return *this; |
| 282 | } |
| 283 | |
| 284 | // Add a displacement from an existing MachineOperand with an added offset. |
| 285 | const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off, |
| 286 | unsigned char TargetFlags = 0) const { |
| 287 | // If caller specifies new TargetFlags then use it, otherwise the |
| 288 | // default behavior is to copy the target flags from the existing |
| 289 | // MachineOperand. This means if the caller wants to clear the |
| 290 | // target flags it needs to do so explicitly. |
| 291 | if (0 == TargetFlags) |
| 292 | TargetFlags = Disp.getTargetFlags(); |
| 293 | |
| 294 | switch (Disp.getType()) { |
| 295 | default: |
| 296 | llvm_unreachable("Unhandled operand type in addDisp()"); |
| 297 | case MachineOperand::MO_Immediate: |
| 298 | return addImm(Disp.getImm() + off); |
| 299 | case MachineOperand::MO_ConstantPoolIndex: |
| 300 | return addConstantPoolIndex(Disp.getIndex(), Disp.getOffset() + off, |
| 301 | TargetFlags); |
| 302 | case MachineOperand::MO_GlobalAddress: |
| 303 | return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off, |
| 304 | TargetFlags); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 305 | case MachineOperand::MO_BlockAddress: |
| 306 | return addBlockAddress(Disp.getBlockAddress(), Disp.getOffset() + off, |
| 307 | TargetFlags); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 308 | case MachineOperand::MO_JumpTableIndex: |
| 309 | assert(off == 0 && "cannot create offset into jump tables"); |
| 310 | return addJumpTableIndex(Disp.getIndex(), TargetFlags); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | /// Copy all the implicit operands from OtherMI onto this one. |
| 315 | const MachineInstrBuilder & |
| 316 | copyImplicitOps(const MachineInstr &OtherMI) const { |
| 317 | MI->copyImplicitOps(*MF, OtherMI); |
| 318 | return *this; |
| 319 | } |
| 320 | |
| 321 | bool constrainAllUses(const TargetInstrInfo &TII, |
| 322 | const TargetRegisterInfo &TRI, |
| 323 | const RegisterBankInfo &RBI) const { |
| 324 | return constrainSelectedInstRegOperands(*MI, TII, TRI, RBI); |
| 325 | } |
| 326 | }; |
| 327 | |
| 328 | /// Builder interface. Specify how to create the initial instruction itself. |
| 329 | inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, |
| 330 | const MCInstrDesc &MCID) { |
| 331 | return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL)); |
| 332 | } |
| 333 | |
| 334 | /// This version of the builder sets up the first operand as a |
| 335 | /// destination virtual register. |
| 336 | inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 337 | const MCInstrDesc &MCID, Register DestReg) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 338 | return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL)) |
| 339 | .addReg(DestReg, RegState::Define); |
| 340 | } |
| 341 | |
| 342 | /// This version of the builder inserts the newly-built instruction before |
| 343 | /// the given position in the given MachineBasicBlock, and sets up the first |
| 344 | /// operand as a destination virtual register. |
| 345 | inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, |
| 346 | MachineBasicBlock::iterator I, |
| 347 | const DebugLoc &DL, const MCInstrDesc &MCID, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 348 | Register DestReg) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 349 | MachineFunction &MF = *BB.getParent(); |
| 350 | MachineInstr *MI = MF.CreateMachineInstr(MCID, DL); |
| 351 | BB.insert(I, MI); |
| 352 | return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define); |
| 353 | } |
| 354 | |
| 355 | /// This version of the builder inserts the newly-built instruction before |
| 356 | /// the given position in the given MachineBasicBlock, and sets up the first |
| 357 | /// operand as a destination virtual register. |
| 358 | /// |
| 359 | /// If \c I is inside a bundle, then the newly inserted \a MachineInstr is |
| 360 | /// added to the same bundle. |
| 361 | inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, |
| 362 | MachineBasicBlock::instr_iterator I, |
| 363 | const DebugLoc &DL, const MCInstrDesc &MCID, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 364 | Register DestReg) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 365 | MachineFunction &MF = *BB.getParent(); |
| 366 | MachineInstr *MI = MF.CreateMachineInstr(MCID, DL); |
| 367 | BB.insert(I, MI); |
| 368 | return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define); |
| 369 | } |
| 370 | |
| 371 | inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I, |
| 372 | const DebugLoc &DL, const MCInstrDesc &MCID, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 373 | Register DestReg) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 374 | // Calling the overload for instr_iterator is always correct. However, the |
| 375 | // definition is not available in headers, so inline the check. |
| 376 | if (I.isInsideBundle()) |
| 377 | return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID, DestReg); |
| 378 | return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID, DestReg); |
| 379 | } |
| 380 | |
| 381 | inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I, |
| 382 | const DebugLoc &DL, const MCInstrDesc &MCID, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 383 | Register DestReg) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 384 | return BuildMI(BB, *I, DL, MCID, DestReg); |
| 385 | } |
| 386 | |
| 387 | /// This version of the builder inserts the newly-built instruction before the |
| 388 | /// given position in the given MachineBasicBlock, and does NOT take a |
| 389 | /// destination register. |
| 390 | inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, |
| 391 | MachineBasicBlock::iterator I, |
| 392 | const DebugLoc &DL, |
| 393 | const MCInstrDesc &MCID) { |
| 394 | MachineFunction &MF = *BB.getParent(); |
| 395 | MachineInstr *MI = MF.CreateMachineInstr(MCID, DL); |
| 396 | BB.insert(I, MI); |
| 397 | return MachineInstrBuilder(MF, MI); |
| 398 | } |
| 399 | |
| 400 | inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, |
| 401 | MachineBasicBlock::instr_iterator I, |
| 402 | const DebugLoc &DL, |
| 403 | const MCInstrDesc &MCID) { |
| 404 | MachineFunction &MF = *BB.getParent(); |
| 405 | MachineInstr *MI = MF.CreateMachineInstr(MCID, DL); |
| 406 | BB.insert(I, MI); |
| 407 | return MachineInstrBuilder(MF, MI); |
| 408 | } |
| 409 | |
| 410 | inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I, |
| 411 | const DebugLoc &DL, |
| 412 | const MCInstrDesc &MCID) { |
| 413 | // Calling the overload for instr_iterator is always correct. However, the |
| 414 | // definition is not available in headers, so inline the check. |
| 415 | if (I.isInsideBundle()) |
| 416 | return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID); |
| 417 | return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID); |
| 418 | } |
| 419 | |
| 420 | inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I, |
| 421 | const DebugLoc &DL, |
| 422 | const MCInstrDesc &MCID) { |
| 423 | return BuildMI(BB, *I, DL, MCID); |
| 424 | } |
| 425 | |
| 426 | /// This version of the builder inserts the newly-built instruction at the end |
| 427 | /// of the given MachineBasicBlock, and does NOT take a destination register. |
| 428 | inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL, |
| 429 | const MCInstrDesc &MCID) { |
| 430 | return BuildMI(*BB, BB->end(), DL, MCID); |
| 431 | } |
| 432 | |
| 433 | /// This version of the builder inserts the newly-built instruction at the |
| 434 | /// end of the given MachineBasicBlock, and sets up the first operand as a |
| 435 | /// destination virtual register. |
| 436 | inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 437 | const MCInstrDesc &MCID, Register DestReg) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 438 | return BuildMI(*BB, BB->end(), DL, MCID, DestReg); |
| 439 | } |
| 440 | |
| 441 | /// This version of the builder builds a DBG_VALUE intrinsic |
| 442 | /// for either a value in a register or a register-indirect |
| 443 | /// address. The convention is that a DBG_VALUE is indirect iff the |
| 444 | /// second operand is an immediate. |
| 445 | MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, |
| 446 | const MCInstrDesc &MCID, bool IsIndirect, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 447 | Register Reg, const MDNode *Variable, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 448 | const MDNode *Expr); |
| 449 | |
| 450 | /// This version of the builder builds a DBG_VALUE intrinsic |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 451 | /// for a MachineOperand. |
| 452 | MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, |
| 453 | const MCInstrDesc &MCID, bool IsIndirect, |
| 454 | MachineOperand &MO, const MDNode *Variable, |
| 455 | const MDNode *Expr); |
| 456 | |
| 457 | /// This version of the builder builds a DBG_VALUE intrinsic |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 458 | /// for either a value in a register or a register-indirect |
| 459 | /// address and inserts it at position I. |
| 460 | MachineInstrBuilder BuildMI(MachineBasicBlock &BB, |
| 461 | MachineBasicBlock::iterator I, const DebugLoc &DL, |
| 462 | const MCInstrDesc &MCID, bool IsIndirect, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 463 | Register Reg, const MDNode *Variable, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 464 | const MDNode *Expr); |
| 465 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 466 | /// This version of the builder builds a DBG_VALUE intrinsic |
| 467 | /// for a machine operand and inserts it at position I. |
| 468 | MachineInstrBuilder BuildMI(MachineBasicBlock &BB, |
| 469 | MachineBasicBlock::iterator I, const DebugLoc &DL, |
| 470 | const MCInstrDesc &MCID, bool IsIndirect, |
| 471 | MachineOperand &MO, const MDNode *Variable, |
| 472 | const MDNode *Expr); |
| 473 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 474 | /// Clone a DBG_VALUE whose value has been spilled to FrameIndex. |
| 475 | MachineInstr *buildDbgValueForSpill(MachineBasicBlock &BB, |
| 476 | MachineBasicBlock::iterator I, |
| 477 | const MachineInstr &Orig, int FrameIndex); |
| 478 | |
| 479 | /// Update a DBG_VALUE whose value has been spilled to FrameIndex. Useful when |
| 480 | /// modifying an instruction in place while iterating over a basic block. |
| 481 | void updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex); |
| 482 | |
| 483 | inline unsigned getDefRegState(bool B) { |
| 484 | return B ? RegState::Define : 0; |
| 485 | } |
| 486 | inline unsigned getImplRegState(bool B) { |
| 487 | return B ? RegState::Implicit : 0; |
| 488 | } |
| 489 | inline unsigned getKillRegState(bool B) { |
| 490 | return B ? RegState::Kill : 0; |
| 491 | } |
| 492 | inline unsigned getDeadRegState(bool B) { |
| 493 | return B ? RegState::Dead : 0; |
| 494 | } |
| 495 | inline unsigned getUndefRegState(bool B) { |
| 496 | return B ? RegState::Undef : 0; |
| 497 | } |
| 498 | inline unsigned getInternalReadRegState(bool B) { |
| 499 | return B ? RegState::InternalRead : 0; |
| 500 | } |
| 501 | inline unsigned getDebugRegState(bool B) { |
| 502 | return B ? RegState::Debug : 0; |
| 503 | } |
| 504 | inline unsigned getRenamableRegState(bool B) { |
| 505 | return B ? RegState::Renamable : 0; |
| 506 | } |
| 507 | |
| 508 | /// Get all register state flags from machine operand \p RegOp. |
| 509 | inline unsigned getRegState(const MachineOperand &RegOp) { |
| 510 | assert(RegOp.isReg() && "Not a register operand"); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 511 | return getDefRegState(RegOp.isDef()) | getImplRegState(RegOp.isImplicit()) | |
| 512 | getKillRegState(RegOp.isKill()) | getDeadRegState(RegOp.isDead()) | |
| 513 | getUndefRegState(RegOp.isUndef()) | |
| 514 | getInternalReadRegState(RegOp.isInternalRead()) | |
| 515 | getDebugRegState(RegOp.isDebug()) | |
| 516 | getRenamableRegState(Register::isPhysicalRegister(RegOp.getReg()) && |
| 517 | RegOp.isRenamable()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /// Helper class for constructing bundles of MachineInstrs. |
| 521 | /// |
| 522 | /// MIBundleBuilder can create a bundle from scratch by inserting new |
| 523 | /// MachineInstrs one at a time, or it can create a bundle from a sequence of |
| 524 | /// existing MachineInstrs in a basic block. |
| 525 | class MIBundleBuilder { |
| 526 | MachineBasicBlock &MBB; |
| 527 | MachineBasicBlock::instr_iterator Begin; |
| 528 | MachineBasicBlock::instr_iterator End; |
| 529 | |
| 530 | public: |
| 531 | /// Create an MIBundleBuilder that inserts instructions into a new bundle in |
| 532 | /// BB above the bundle or instruction at Pos. |
| 533 | MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos) |
| 534 | : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {} |
| 535 | |
| 536 | /// Create a bundle from the sequence of instructions between B and E. |
| 537 | MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator B, |
| 538 | MachineBasicBlock::iterator E) |
| 539 | : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) { |
| 540 | assert(B != E && "No instructions to bundle"); |
| 541 | ++B; |
| 542 | while (B != E) { |
| 543 | MachineInstr &MI = *B; |
| 544 | ++B; |
| 545 | MI.bundleWithPred(); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /// Create an MIBundleBuilder representing an existing instruction or bundle |
| 550 | /// that has MI as its head. |
| 551 | explicit MIBundleBuilder(MachineInstr *MI) |
| 552 | : MBB(*MI->getParent()), Begin(MI), |
| 553 | End(getBundleEnd(MI->getIterator())) {} |
| 554 | |
| 555 | /// Return a reference to the basic block containing this bundle. |
| 556 | MachineBasicBlock &getMBB() const { return MBB; } |
| 557 | |
| 558 | /// Return true if no instructions have been inserted in this bundle yet. |
| 559 | /// Empty bundles aren't representable in a MachineBasicBlock. |
| 560 | bool empty() const { return Begin == End; } |
| 561 | |
| 562 | /// Return an iterator to the first bundled instruction. |
| 563 | MachineBasicBlock::instr_iterator begin() const { return Begin; } |
| 564 | |
| 565 | /// Return an iterator beyond the last bundled instruction. |
| 566 | MachineBasicBlock::instr_iterator end() const { return End; } |
| 567 | |
| 568 | /// Insert MI into this bundle before I which must point to an instruction in |
| 569 | /// the bundle, or end(). |
| 570 | MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I, |
| 571 | MachineInstr *MI) { |
| 572 | MBB.insert(I, MI); |
| 573 | if (I == Begin) { |
| 574 | if (!empty()) |
| 575 | MI->bundleWithSucc(); |
| 576 | Begin = MI->getIterator(); |
| 577 | return *this; |
| 578 | } |
| 579 | if (I == End) { |
| 580 | MI->bundleWithPred(); |
| 581 | return *this; |
| 582 | } |
| 583 | // MI was inserted in the middle of the bundle, so its neighbors' flags are |
| 584 | // already fine. Update MI's bundle flags manually. |
| 585 | MI->setFlag(MachineInstr::BundledPred); |
| 586 | MI->setFlag(MachineInstr::BundledSucc); |
| 587 | return *this; |
| 588 | } |
| 589 | |
| 590 | /// Insert MI into MBB by prepending it to the instructions in the bundle. |
| 591 | /// MI will become the first instruction in the bundle. |
| 592 | MIBundleBuilder &prepend(MachineInstr *MI) { |
| 593 | return insert(begin(), MI); |
| 594 | } |
| 595 | |
| 596 | /// Insert MI into MBB by appending it to the instructions in the bundle. |
| 597 | /// MI will become the last instruction in the bundle. |
| 598 | MIBundleBuilder &append(MachineInstr *MI) { |
| 599 | return insert(end(), MI); |
| 600 | } |
| 601 | }; |
| 602 | |
| 603 | } // end namespace llvm |
| 604 | |
| 605 | #endif // LLVM_CODEGEN_MACHINEINSTRBUILDER_H |