Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file exposes a function named BuildMI, which is useful for dramatically
11
// simplifying how MachineInstr's are created.  It allows use of code like this:
12
//
13
//   M = BuildMI(MBB, MI, DL, TII.get(X86::ADD8rr), Dst)
14
//           .addReg(argVal1)
15
//           .addReg(argVal2);
16
//
17
//===----------------------------------------------------------------------===//
18
19
#ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
20
#define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
21
22
#include "llvm/ADT/ArrayRef.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/IR/InstrTypes.h"
29
#include "llvm/IR/Intrinsics.h"
30
#include "llvm/Support/ErrorHandling.h"
31
#include <cassert>
32
#include <cstdint>
33
#include <utility>
34
35
namespace llvm {
36
37
class MCInstrDesc;
38
class MDNode;
39
40
namespace RegState {
41
42
  enum {
43
    Define         = 0x2,
44
    Implicit       = 0x4,
45
    Kill           = 0x8,
46
    Dead           = 0x10,
47
    Undef          = 0x20,
48
    EarlyClobber   = 0x40,
49
    Debug          = 0x80,
50
    InternalRead   = 0x100,
51
    DefineNoRead   = Define | Undef,
52
    ImplicitDefine = Implicit | Define,
53
    ImplicitKill   = Implicit | Kill
54
  };
55
56
} // end namespace RegState
57
58
class MachineInstrBuilder {
59
  MachineFunction *MF = nullptr;
60
  MachineInstr *MI = nullptr;
61
62
public:
63
717k
  MachineInstrBuilder() = default;
64
65
  /// Create a MachineInstrBuilder for manipulating an existing instruction.
66
  /// F must be the machine function that was used to allocate I.
67
92.1M
  MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
68
  MachineInstrBuilder(MachineFunction &F, MachineBasicBlock::iterator I)
69
419k
      : MF(&F), MI(&*I) {}
70
71
  /// Allow automatic conversion to the machine instruction we are working on.
72
47.5M
  operator MachineInstr*() const { return MI; }
73
36.1M
  MachineInstr *operator->() const { return MI; }
74
20.5k
  operator MachineBasicBlock::iterator() const { return MI; }
75
76
  /// If conversion operators fail, use this method to get the MachineInstr
77
  /// explicitly.
78
1.34M
  MachineInstr *getInstr() const { return MI; }
79
80
  /// Add a new virtual register operand.
81
  const MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
82
128M
                                    unsigned SubReg = 0) const {
83
128M
    assert((flags & 0x1) == 0 &&
84
128M
           "Passing in 'true' to addReg is forbidden! Use enums instead.");
85
128M
    MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
86
128M
                                               flags & RegState::Define,
87
128M
                                               flags & RegState::Implicit,
88
128M
                                               flags & RegState::Kill,
89
128M
                                               flags & RegState::Dead,
90
128M
                                               flags & RegState::Undef,
91
128M
                                               flags & RegState::EarlyClobber,
92
128M
                                               SubReg,
93
128M
                                               flags & RegState::Debug,
94
128M
                                               flags & RegState::InternalRead));
95
128M
    return *this;
96
128M
  }
97
98
  /// Add a virtual register definition operand.
99
  const MachineInstrBuilder &addDef(unsigned RegNo, unsigned Flags = 0,
100
16.0M
                                    unsigned SubReg = 0) const {
101
16.0M
    return addReg(RegNo, Flags | RegState::Define, SubReg);
102
16.0M
  }
103
104
  /// Add a virtual register use operand. It is an error for Flags to contain
105
  /// `RegState::Define` when calling this function.
106
  const MachineInstrBuilder &addUse(unsigned RegNo, unsigned Flags = 0,
107
24.0M
                                    unsigned SubReg = 0) const {
108
24.0M
    assert(!(Flags & RegState::Define) &&
109
24.0M
           "Misleading addUse defines register, use addReg instead.");
110
24.0M
    return addReg(RegNo, Flags, SubReg);
111
24.0M
  }
112
113
  /// Add a new immediate operand.
114
44.1M
  const MachineInstrBuilder &addImm(int64_t Val) const {
115
44.1M
    MI->addOperand(*MF, MachineOperand::CreateImm(Val));
116
44.1M
    return *this;
117
44.1M
  }
118
119
1.88M
  const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
120
1.88M
    MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
121
1.88M
    return *this;
122
1.88M
  }
123
124
26.1k
  const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
125
26.1k
    MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
126
26.1k
    return *this;
127
26.1k
  }
128
129
  const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
130
22.9M
                                    unsigned char TargetFlags = 0) const {
131
22.9M
    MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
132
22.9M
    return *this;
133
22.9M
  }
134
135
1.32M
  const MachineInstrBuilder &addFrameIndex(int Idx) const {
136
1.32M
    MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
137
1.32M
    return *this;
138
1.32M
  }
139
140
  const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
141
                                                  int Offset = 0,
142
181k
                                          unsigned char TargetFlags = 0) const {
143
181k
    MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
144
181k
    return *this;
145
181k
  }
146
147
  const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
148
0
                                          unsigned char TargetFlags = 0) const {
149
0
    MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
150
0
                                                          TargetFlags));
151
0
    return *this;
152
0
  }
153
154
  const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
155
10.5k
                                          unsigned char TargetFlags = 0) const {
156
10.5k
    MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
157
10.5k
    return *this;
158
10.5k
  }
159
160
  const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
161
                                              int64_t Offset = 0,
162
4.93M
                                          unsigned char TargetFlags = 0) const {
163
4.93M
    MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
164
4.93M
    return *this;
165
4.93M
  }
166
167
  const MachineInstrBuilder &addExternalSymbol(const char *FnName,
168
52.6k
                                          unsigned char TargetFlags = 0) const {
169
52.6k
    MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
170
52.6k
    return *this;
171
52.6k
  }
172
173
  const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
174
                                             int64_t Offset = 0,
175
128
                                          unsigned char TargetFlags = 0) const {
176
128
    MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
177
128
    return *this;
178
128
  }
179
180
2.96M
  const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
181
2.96M
    MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
182
2.96M
    return *this;
183
2.96M
  }
184
185
6.52M
  const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
186
6.52M
    MI->addMemOperand(*MF, MMO);
187
6.52M
    return *this;
188
6.52M
  }
189
190
  const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
191
20.3M
                                        MachineInstr::mmo_iterator e) const {
192
20.3M
    MI->setMemRefs(b, e);
193
20.3M
    return *this;
194
20.3M
  }
195
196
  const MachineInstrBuilder &setMemRefs(std::pair<MachineInstr::mmo_iterator,
197
364k
                                        unsigned> MemOperandsRef) const {
198
364k
    MI->setMemRefs(MemOperandsRef);
199
364k
    return *this;
200
364k
  }
201
202
23.4M
  const MachineInstrBuilder &add(const MachineOperand &MO) const {
203
23.4M
    MI->addOperand(*MF, MO);
204
23.4M
    return *this;
205
23.4M
  }
206
207
154k
  const MachineInstrBuilder &add(ArrayRef<MachineOperand> MOs) const {
208
308k
    for (const MachineOperand &MO : MOs) {
209
308k
      MI->addOperand(*MF, MO);
210
308k
    }
211
154k
    return *this;
212
154k
  }
213
214
6.57k
  const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
215
6.57k
    MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
216
6.57k
    assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
217
6.57k
                               : true) &&
218
6.57k
           "first MDNode argument of a DBG_VALUE not a variable");
219
6.57k
    return *this;
220
6.57k
  }
221
222
368k
  const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
223
368k
    MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
224
368k
    return *this;
225
368k
  }
226
227
143k
  const MachineInstrBuilder &addIntrinsicID(Intrinsic::ID ID) const {
228
143k
    MI->addOperand(*MF, MachineOperand::CreateIntrinsicID(ID));
229
143k
    return *this;
230
143k
  }
231
232
1.52M
  const MachineInstrBuilder &addPredicate(CmpInst::Predicate Pred) const {
233
1.52M
    MI->addOperand(*MF, MachineOperand::CreatePredicate(Pred));
234
1.52M
    return *this;
235
1.52M
  }
236
237
  const MachineInstrBuilder &addSym(MCSymbol *Sym,
238
71.2k
                                    unsigned char TargetFlags = 0) const {
239
71.2k
    MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
240
71.2k
    return *this;
241
71.2k
  }
242
243
970k
  const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
244
970k
    MI->setFlags(Flags);
245
970k
    return *this;
246
970k
  }
247
248
2.78M
  const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
249
2.78M
    MI->setFlag(Flag);
250
2.78M
    return *this;
251
2.78M
  }
252
253
  // Add a displacement from an existing MachineOperand with an added offset.
254
  const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
255
39
                                     unsigned char TargetFlags = 0) const {
256
39
    // If caller specifies new TargetFlags then use it, otherwise the
257
39
    // default behavior is to copy the target flags from the existing
258
39
    // MachineOperand. This means if the caller wants to clear the
259
39
    // target flags it needs to do so explicitly.
260
39
    if (0 == TargetFlags)
261
39
      TargetFlags = Disp.getTargetFlags();
262
39
263
39
    switch (Disp.getType()) {
264
0
      default:
265
0
        llvm_unreachable("Unhandled operand type in addDisp()");
266
18
      case MachineOperand::MO_Immediate:
267
18
        return addImm(Disp.getImm() + off);
268
6
      case MachineOperand::MO_ConstantPoolIndex:
269
6
        return addConstantPoolIndex(Disp.getIndex(), Disp.getOffset() + off,
270
6
                                    TargetFlags);
271
15
      case MachineOperand::MO_GlobalAddress:
272
15
        return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
273
15
                                TargetFlags);
274
39
    }
275
39
  }
276
277
  /// Copy all the implicit operands from OtherMI onto this one.
278
  const MachineInstrBuilder &
279
6.51k
  copyImplicitOps(const MachineInstr &OtherMI) const {
280
6.51k
    MI->copyImplicitOps(*MF, OtherMI);
281
6.51k
    return *this;
282
6.51k
  }
283
};
284
285
/// Builder interface. Specify how to create the initial instruction itself.
286
inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
287
41.0M
                                   const MCInstrDesc &MCID) {
288
41.0M
  return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
289
41.0M
}
290
291
/// This version of the builder sets up the first operand as a
292
/// destination virtual register.
293
inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
294
869k
                                   const MCInstrDesc &MCID, unsigned DestReg) {
295
869k
  return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
296
869k
           .addReg(DestReg, RegState::Define);
297
869k
}
298
299
/// This version of the builder inserts the newly-built instruction before
300
/// the given position in the given MachineBasicBlock, and sets up the first
301
/// operand as a destination virtual register.
302
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
303
                                   MachineBasicBlock::iterator I,
304
                                   const DebugLoc &DL, const MCInstrDesc &MCID,
305
24.6M
                                   unsigned DestReg) {
306
24.6M
  MachineFunction &MF = *BB.getParent();
307
24.6M
  MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
308
24.6M
  BB.insert(I, MI);
309
24.6M
  return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
310
24.6M
}
311
312
/// This version of the builder inserts the newly-built instruction before
313
/// the given position in the given MachineBasicBlock, and sets up the first
314
/// operand as a destination virtual register.
315
///
316
/// If \c I is inside a bundle, then the newly inserted \a MachineInstr is
317
/// added to the same bundle.
318
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
319
                                   MachineBasicBlock::instr_iterator I,
320
                                   const DebugLoc &DL, const MCInstrDesc &MCID,
321
8.31k
                                   unsigned DestReg) {
322
8.31k
  MachineFunction &MF = *BB.getParent();
323
8.31k
  MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
324
8.31k
  BB.insert(I, MI);
325
8.31k
  return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
326
8.31k
}
327
328
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
329
                                   const DebugLoc &DL, const MCInstrDesc &MCID,
330
337k
                                   unsigned DestReg) {
331
337k
  // Calling the overload for instr_iterator is always correct.  However, the
332
337k
  // definition is not available in headers, so inline the check.
333
337k
  if (I.isInsideBundle())
334
0
    return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID, DestReg);
335
337k
  return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID, DestReg);
336
337k
}
337
338
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
339
                                   const DebugLoc &DL, const MCInstrDesc &MCID,
340
284k
                                   unsigned DestReg) {
341
284k
  return BuildMI(BB, *I, DL, MCID, DestReg);
342
284k
}
343
344
/// This version of the builder inserts the newly-built instruction before the
345
/// given position in the given MachineBasicBlock, and does NOT take a
346
/// destination register.
347
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
348
                                   MachineBasicBlock::iterator I,
349
                                   const DebugLoc &DL,
350
22.5M
                                   const MCInstrDesc &MCID) {
351
22.5M
  MachineFunction &MF = *BB.getParent();
352
22.5M
  MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
353
22.5M
  BB.insert(I, MI);
354
22.5M
  return MachineInstrBuilder(MF, MI);
355
22.5M
}
356
357
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
358
                                   MachineBasicBlock::instr_iterator I,
359
                                   const DebugLoc &DL,
360
6.67k
                                   const MCInstrDesc &MCID) {
361
6.67k
  MachineFunction &MF = *BB.getParent();
362
6.67k
  MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
363
6.67k
  BB.insert(I, MI);
364
6.67k
  return MachineInstrBuilder(MF, MI);
365
6.67k
}
366
367
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
368
                                   const DebugLoc &DL,
369
1.44M
                                   const MCInstrDesc &MCID) {
370
1.44M
  // Calling the overload for instr_iterator is always correct.  However, the
371
1.44M
  // definition is not available in headers, so inline the check.
372
1.44M
  if (I.isInsideBundle())
373
5.03k
    return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID);
374
1.43M
  return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID);
375
1.44M
}
376
377
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
378
                                   const DebugLoc &DL,
379
562k
                                   const MCInstrDesc &MCID) {
380
562k
  return BuildMI(BB, *I, DL, MCID);
381
562k
}
382
383
/// This version of the builder inserts the newly-built instruction at the end
384
/// of the given MachineBasicBlock, and does NOT take a destination register.
385
inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
386
13.7M
                                   const MCInstrDesc &MCID) {
387
13.7M
  return BuildMI(*BB, BB->end(), DL, MCID);
388
13.7M
}
389
390
/// This version of the builder inserts the newly-built instruction at the
391
/// end of the given MachineBasicBlock, and sets up the first operand as a
392
/// destination virtual register.
393
inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
394
918k
                                   const MCInstrDesc &MCID, unsigned DestReg) {
395
918k
  return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
396
918k
}
397
398
/// This version of the builder builds a DBG_VALUE intrinsic
399
/// for either a value in a register or a register-indirect
400
/// address.  The convention is that a DBG_VALUE is indirect iff the
401
/// second operand is an immediate.
402
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
403
                            const MCInstrDesc &MCID, bool IsIndirect,
404
                            unsigned Reg, const MDNode *Variable,
405
                            const MDNode *Expr);
406
407
/// This version of the builder builds a DBG_VALUE intrinsic
408
/// for either a value in a register or a register-indirect
409
/// address and inserts it at position I.
410
MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
411
                            MachineBasicBlock::iterator I, const DebugLoc &DL,
412
                            const MCInstrDesc &MCID, bool IsIndirect,
413
                            unsigned Reg, const MDNode *Variable,
414
                            const MDNode *Expr);
415
416
/// Clone a DBG_VALUE whose value has been spilled to FrameIndex.
417
MachineInstr *buildDbgValueForSpill(MachineBasicBlock &BB,
418
                                    MachineBasicBlock::iterator I,
419
                                    const MachineInstr &Orig, int FrameIndex);
420
421
/// Update a DBG_VALUE whose value has been spilled to FrameIndex. Useful when
422
/// modifying an instruction in place while iterating over a basic block.
423
void updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex);
424
425
15.9M
inline unsigned getDefRegState(bool B) {
426
15.9M
  return B ? 
RegState::Define2.52M
:
013.3M
;
427
15.9M
}
428
5.69M
inline unsigned getImplRegState(bool B) {
429
5.69M
  return B ? 
RegState::Implicit4.39M
:
01.29M
;
430
5.69M
}
431
19.2M
inline unsigned getKillRegState(bool B) {
432
19.2M
  return B ? 
RegState::Kill7.77M
:
011.4M
;
433
19.2M
}
434
858k
inline unsigned getDeadRegState(bool B) {
435
858k
  return B ? 
RegState::Dead41.7k
:
0816k
;
436
858k
}
437
112k
inline unsigned getUndefRegState(bool B) {
438
112k
  return B ? 
RegState::Undef3.19k
:
0109k
;
439
112k
}
440
2.57k
inline unsigned getInternalReadRegState(bool B) {
441
2.57k
  return B ? 
RegState::InternalRead9
:
02.56k
;
442
2.57k
}
443
13.3M
inline unsigned getDebugRegState(bool B) {
444
13.3M
  return B ? 
RegState::Debug223
:
013.3M
;
445
13.3M
}
446
447
/// Get all register state flags from machine operand \p RegOp.
448
1.57k
inline unsigned getRegState(const MachineOperand &RegOp) {
449
1.57k
  assert(RegOp.isReg() && "Not a register operand");
450
1.57k
  return getDefRegState(RegOp.isDef())                    |
451
1.57k
         getImplRegState(RegOp.isImplicit())              |
452
1.57k
         getKillRegState(RegOp.isKill())                  |
453
1.57k
         getDeadRegState(RegOp.isDead())                  |
454
1.57k
         getUndefRegState(RegOp.isUndef())                |
455
1.57k
         getInternalReadRegState(RegOp.isInternalRead())  |
456
1.57k
         getDebugRegState(RegOp.isDebug());
457
1.57k
}
458
459
/// Helper class for constructing bundles of MachineInstrs.
460
///
461
/// MIBundleBuilder can create a bundle from scratch by inserting new
462
/// MachineInstrs one at a time, or it can create a bundle from a sequence of
463
/// existing MachineInstrs in a basic block.
464
class MIBundleBuilder {
465
  MachineBasicBlock &MBB;
466
  MachineBasicBlock::instr_iterator Begin;
467
  MachineBasicBlock::instr_iterator End;
468
469
public:
470
  /// Create an MIBundleBuilder that inserts instructions into a new bundle in
471
  /// BB above the bundle or instruction at Pos.
472
  MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos)
473
491
      : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
474
475
  /// Create a bundle from the sequence of instructions between B and E.
476
  MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator B,
477
                  MachineBasicBlock::iterator E)
478
38.8k
      : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
479
38.8k
    assert(B != E && "No instructions to bundle");
480
38.8k
    ++B;
481
91.2k
    while (
B != E91.2k
) {
482
52.3k
      MachineInstr &MI = *B;
483
52.3k
      ++B;
484
52.3k
      MI.bundleWithPred();
485
52.3k
    }
486
38.8k
  }
487
488
  /// Create an MIBundleBuilder representing an existing instruction or bundle
489
  /// that has MI as its head.
490
  explicit MIBundleBuilder(MachineInstr *MI)
491
      : MBB(*MI->getParent()), Begin(MI),
492
49
        End(getBundleEnd(MI->getIterator())) {}
493
494
  /// Return a reference to the basic block containing this bundle.
495
0
  MachineBasicBlock &getMBB() const { return MBB; }
496
497
  /// Return true if no instructions have been inserted in this bundle yet.
498
  /// Empty bundles aren't representable in a MachineBasicBlock.
499
24.7k
  bool empty() const { return Begin == End; }
500
501
  /// Return an iterator to the first bundled instruction.
502
24.7k
  MachineBasicBlock::instr_iterator begin() const { return Begin; }
503
504
  /// Return an iterator beyond the last bundled instruction.
505
1.51k
  MachineBasicBlock::instr_iterator end() const { return End; }
506
507
  /// Insert MI into this bundle before I which must point to an instruction in
508
  /// the bundle, or end().
509
  MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
510
25.7k
                          MachineInstr *MI) {
511
25.7k
    MBB.insert(I, MI);
512
25.7k
    if (
I == Begin25.7k
) {
513
24.7k
      if (!empty())
514
24.2k
        MI->bundleWithSucc();
515
24.7k
      Begin = MI->getIterator();
516
24.7k
      return *this;
517
24.7k
    }
518
1.02k
    
if (1.02k
I == End1.02k
) {
519
1.02k
      MI->bundleWithPred();
520
1.02k
      return *this;
521
1.02k
    }
522
1.02k
    // MI was inserted in the middle of the bundle, so its neighbors' flags are
523
1.02k
    // already fine. Update MI's bundle flags manually.
524
0
    MI->setFlag(MachineInstr::BundledPred);
525
0
    MI->setFlag(MachineInstr::BundledSucc);
526
0
    return *this;
527
25.7k
  }
528
529
  /// Insert MI into MBB by prepending it to the instructions in the bundle.
530
  /// MI will become the first instruction in the bundle.
531
24.2k
  MIBundleBuilder &prepend(MachineInstr *MI) {
532
24.2k
    return insert(begin(), MI);
533
24.2k
  }
534
535
  /// Insert MI into MBB by appending it to the instructions in the bundle.
536
  /// MI will become the last instruction in the bundle.
537
1.51k
  MIBundleBuilder &append(MachineInstr *MI) {
538
1.51k
    return insert(end(), MI);
539
1.51k
  }
540
};
541
542
} // end namespace llvm
543
544
#endif // LLVM_CODEGEN_MACHINEINSTRBUILDER_H