Coverage Report

Created: 2019-02-20 00:17

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