Skip to content

Commit 7fae11b

Browse files
author
Evan Cheng
committedDec 14, 2011
- Add MachineInstrBundle.h and MachineInstrBundle.cpp. This includes a function
to finalize MI bundles (i.e. add BUNDLE instruction and computing register def and use lists of the BUNDLE instruction) and a pass to unpack bundles. - Teach more of MachineBasic and MachineInstr methods to be bundle aware. - Switch Thumb2 IT block to MI bundles and delete the hazard recognizer hack to prevent IT blocks from being broken apart. llvm-svn: 146542
1 parent 87ebe63 commit 7fae11b

28 files changed

+653
-169
lines changed
 

‎llvm/include/llvm/CodeGen/MachineBasicBlock.h

+67-55
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,21 @@ class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
194194
return tmp;
195195
}
196196

197-
IterTy getInsnIterator() const {
197+
IterTy getInstrIterator() const {
198198
return MII;
199199
}
200200
};
201201

202-
typedef Instructions::iterator insn_iterator;
203-
typedef Instructions::const_iterator const_insn_iterator;
204-
typedef std::reverse_iterator<insn_iterator> reverse_insn_iterator;
202+
typedef Instructions::iterator instr_iterator;
203+
typedef Instructions::const_iterator const_instr_iterator;
204+
typedef std::reverse_iterator<instr_iterator> reverse_instr_iterator;
205205
typedef
206-
std::reverse_iterator<const_insn_iterator> const_reverse_insn_iterator;
206+
std::reverse_iterator<const_instr_iterator> const_reverse_instr_iterator;
207207

208208
typedef
209-
bundle_iterator<MachineInstr,insn_iterator> iterator;
209+
bundle_iterator<MachineInstr,instr_iterator> iterator;
210210
typedef
211-
bundle_iterator<const MachineInstr,const_insn_iterator> const_iterator;
211+
bundle_iterator<const MachineInstr,const_instr_iterator> const_iterator;
212212
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
213213
typedef std::reverse_iterator<iterator> reverse_iterator;
214214

@@ -221,44 +221,44 @@ class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
221221
const MachineInstr& front() const { return Insts.front(); }
222222
const MachineInstr& back() const { return Insts.back(); }
223223

224-
insn_iterator insn_begin() { return Insts.begin(); }
225-
const_insn_iterator insn_begin() const { return Insts.begin(); }
226-
insn_iterator insn_end() { return Insts.end(); }
227-
const_insn_iterator insn_end() const { return Insts.end(); }
228-
reverse_insn_iterator insn_rbegin() { return Insts.rbegin(); }
229-
const_reverse_insn_iterator insn_rbegin() const { return Insts.rbegin(); }
230-
reverse_insn_iterator insn_rend () { return Insts.rend(); }
231-
const_reverse_insn_iterator insn_rend () const { return Insts.rend(); }
224+
instr_iterator instr_begin() { return Insts.begin(); }
225+
const_instr_iterator instr_begin() const { return Insts.begin(); }
226+
instr_iterator instr_end() { return Insts.end(); }
227+
const_instr_iterator instr_end() const { return Insts.end(); }
228+
reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); }
229+
const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
230+
reverse_instr_iterator instr_rend () { return Insts.rend(); }
231+
const_reverse_instr_iterator instr_rend () const { return Insts.rend(); }
232232

233233
iterator begin() { return Insts.begin(); }
234234
const_iterator begin() const { return Insts.begin(); }
235235
iterator end() {
236-
insn_iterator II = insn_end();
237-
if (II != insn_begin()) {
236+
instr_iterator II = instr_end();
237+
if (II != instr_begin()) {
238238
while (II->isInsideBundle())
239239
--II;
240240
}
241241
return II;
242242
}
243243
const_iterator end() const {
244-
const_insn_iterator II = insn_end();
245-
if (II != insn_begin()) {
244+
const_instr_iterator II = instr_end();
245+
if (II != instr_begin()) {
246246
while (II->isInsideBundle())
247247
--II;
248248
}
249249
return II;
250250
}
251251
reverse_iterator rbegin() {
252-
reverse_insn_iterator II = insn_rbegin();
253-
if (II != insn_rend()) {
252+
reverse_instr_iterator II = instr_rbegin();
253+
if (II != instr_rend()) {
254254
while (II->isInsideBundle())
255255
++II;
256256
}
257257
return II;
258258
}
259259
const_reverse_iterator rbegin() const {
260-
const_reverse_insn_iterator II = insn_rbegin();
261-
if (II != insn_rend()) {
260+
const_reverse_instr_iterator II = instr_rbegin();
261+
if (II != instr_rend()) {
262262
while (II->isInsideBundle())
263263
++II;
264264
}
@@ -442,9 +442,9 @@ class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
442442
iterator getFirstTerminator();
443443
const_iterator getFirstTerminator() const;
444444

445-
/// getFirstInsnTerminator - Same getFirstTerminator but it ignores bundles
446-
/// and return an insn_iterator instead.
447-
insn_iterator getFirstInsnTerminator();
445+
/// getFirstInstrTerminator - Same getFirstTerminator but it ignores bundles
446+
/// and return an instr_iterator instead.
447+
instr_iterator getFirstInstrTerminator();
448448

449449
/// getLastNonDebugInstr - returns an iterator to the last non-debug
450450
/// instruction in the basic block, or end()
@@ -464,68 +464,80 @@ class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
464464
void push_back(MachineInstr *MI) { Insts.push_back(MI); }
465465

466466
template<typename IT>
467-
void insert(insn_iterator I, IT S, IT E) {
467+
void insert(instr_iterator I, IT S, IT E) {
468468
Insts.insert(I, S, E);
469469
}
470-
insn_iterator insert(insn_iterator I, MachineInstr *M) {
470+
instr_iterator insert(instr_iterator I, MachineInstr *M) {
471471
return Insts.insert(I, M);
472472
}
473-
insn_iterator insertAfter(insn_iterator I, MachineInstr *M) {
473+
instr_iterator insertAfter(instr_iterator I, MachineInstr *M) {
474474
return Insts.insertAfter(I, M);
475475
}
476476

477477
template<typename IT>
478478
void insert(iterator I, IT S, IT E) {
479-
Insts.insert(I.getInsnIterator(), S, E);
479+
Insts.insert(I.getInstrIterator(), S, E);
480480
}
481481
iterator insert(iterator I, MachineInstr *M) {
482-
return Insts.insert(I.getInsnIterator(), M);
482+
return Insts.insert(I.getInstrIterator(), M);
483483
}
484484
iterator insertAfter(iterator I, MachineInstr *M) {
485-
return Insts.insertAfter(I.getInsnIterator(), M);
485+
return Insts.insertAfter(I.getInstrIterator(), M);
486486
}
487487

488-
// erase - Remove the specified element or range from the instruction list.
489-
// These functions delete any instructions removed.
490-
//
491-
insn_iterator erase(insn_iterator I) {
488+
/// erase - Remove the specified element or range from the instruction list.
489+
/// These functions delete any instructions removed.
490+
///
491+
instr_iterator erase(instr_iterator I) {
492492
return Insts.erase(I);
493493
}
494-
insn_iterator erase(insn_iterator I, insn_iterator E) {
494+
instr_iterator erase(instr_iterator I, instr_iterator E) {
495495
return Insts.erase(I, E);
496496
}
497-
498-
iterator erase(iterator I) {
499-
return Insts.erase(I.getInsnIterator());
497+
instr_iterator erase_instr(MachineInstr *I) {
498+
instr_iterator MII(I);
499+
return erase(MII);
500500
}
501+
502+
iterator erase(iterator I);
501503
iterator erase(iterator I, iterator E) {
502-
return Insts.erase(I.getInsnIterator(), E.getInsnIterator());
504+
return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
505+
}
506+
iterator erase(MachineInstr *I) {
507+
iterator MII(I);
508+
return erase(MII);
503509
}
504510

505-
iterator erase(MachineInstr *I) { iterator MII(I); return erase(MII); }
506-
MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
507-
void clear() { Insts.clear(); }
511+
/// remove - Remove the instruction from the instruction list. This function
512+
/// does not delete the instruction. WARNING: Note, if the specified
513+
/// instruction is a bundle this function will remove all the bundled
514+
/// instructions as well. It is up to the caller to keep a list of the
515+
/// bundled instructions and re-insert them if desired. This function is
516+
/// *not recommended* for manipulating instructions with bundled. Use
517+
/// splice instead.
518+
MachineInstr *remove(MachineInstr *I);
519+
void clear() {
520+
Insts.clear();
521+
}
508522

509523
/// splice - Take an instruction from MBB 'Other' at the position From,
510524
/// and insert it into this MBB right before 'where'.
511-
void splice(insn_iterator where, MachineBasicBlock *Other,
512-
insn_iterator From) {
525+
void splice(instr_iterator where, MachineBasicBlock *Other,
526+
instr_iterator From) {
513527
Insts.splice(where, Other->Insts, From);
514528
}
515-
void splice(iterator where, MachineBasicBlock *Other, iterator From) {
516-
Insts.splice(where.getInsnIterator(), Other->Insts, From.getInsnIterator());
517-
}
529+
void splice(iterator where, MachineBasicBlock *Other, iterator From);
518530

519531
/// splice - Take a block of instructions from MBB 'Other' in the range [From,
520532
/// To), and insert them into this MBB right before 'where'.
521-
void splice(insn_iterator where, MachineBasicBlock *Other, insn_iterator From,
522-
insn_iterator To) {
533+
void splice(instr_iterator where, MachineBasicBlock *Other, instr_iterator From,
534+
instr_iterator To) {
523535
Insts.splice(where, Other->Insts, From, To);
524536
}
525537
void splice(iterator where, MachineBasicBlock *Other, iterator From,
526538
iterator To) {
527-
Insts.splice(where.getInsnIterator(), Other->Insts,
528-
From.getInsnIterator(), To.getInsnIterator());
539+
Insts.splice(where.getInstrIterator(), Other->Insts,
540+
From.getInstrIterator(), To.getInstrIterator());
529541
}
530542

531543
/// removeFromParent - This method unlinks 'this' from the containing
@@ -552,9 +564,9 @@ class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
552564

553565
/// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
554566
/// any DBG_VALUE instructions. Return UnknownLoc if there is none.
555-
DebugLoc findDebugLoc(insn_iterator MBBI);
567+
DebugLoc findDebugLoc(instr_iterator MBBI);
556568
DebugLoc findDebugLoc(iterator MBBI) {
557-
return findDebugLoc(MBBI.getInsnIterator());
569+
return findDebugLoc(MBBI.getInstrIterator());
558570
}
559571

560572
// Debugging methods.

‎llvm/include/llvm/CodeGen/MachineInstr.h

+20
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ class MachineInstr : public ilist_node<MachineInstr> {
175175
Flags = flags;
176176
}
177177

178+
/// clearFlag - Clear a MI flag.
179+
void clearFlag(MIFlag Flag) {
180+
Flags &= ~((uint8_t)Flag);
181+
}
182+
178183
/// isInsideBundle - Return true if MI is in a bundle (but not the first MI
179184
/// in a bundle).
180185
///
@@ -215,6 +220,15 @@ class MachineInstr : public ilist_node<MachineInstr> {
215220
return getFlag(InsideBundle);
216221
}
217222

223+
/// setIsInsideBundle - Set InsideBundle bit.
224+
///
225+
void setIsInsideBundle(bool Val = true) {
226+
if (Val)
227+
setFlag(InsideBundle);
228+
else
229+
clearFlag(InsideBundle);
230+
}
231+
218232
/// getDebugLoc - Returns the debug location id of this MachineInstr.
219233
///
220234
DebugLoc getDebugLoc() const { return debugLoc; }
@@ -589,6 +603,9 @@ class MachineInstr : public ilist_node<MachineInstr> {
589603
bool isRegSequence() const {
590604
return getOpcode() == TargetOpcode::REG_SEQUENCE;
591605
}
606+
bool isBundle() const {
607+
return getOpcode() == TargetOpcode::BUNDLE;
608+
}
592609
bool isCopy() const {
593610
return getOpcode() == TargetOpcode::COPY;
594611
}
@@ -608,6 +625,9 @@ class MachineInstr : public ilist_node<MachineInstr> {
608625
getOperand(0).getSubReg() == getOperand(1).getSubReg();
609626
}
610627

628+
/// getBundleSize - Return the number of instructions inside the MI bundle.
629+
unsigned getBundleSize() const;
630+
611631
/// readsRegister - Return true if the MachineInstr reads the specified
612632
/// register. If TargetRegisterInfo is passed, then it also checks if there
613633
/// is a read of a super-register.

‎llvm/include/llvm/CodeGen/MachineInstrBuilder.h

+46
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,30 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
209209
return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
210210
}
211211

212+
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
213+
MachineBasicBlock::instr_iterator I,
214+
DebugLoc DL,
215+
const MCInstrDesc &MCID,
216+
unsigned DestReg) {
217+
MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
218+
BB.insert(I, MI);
219+
return MachineInstrBuilder(MI).addReg(DestReg, RegState::Define);
220+
}
221+
222+
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
223+
MachineInstr *I,
224+
DebugLoc DL,
225+
const MCInstrDesc &MCID,
226+
unsigned DestReg) {
227+
if (I->isInsideBundle()) {
228+
MachineBasicBlock::instr_iterator MII = I;
229+
return BuildMI(BB, MII, DL, MCID, DestReg);
230+
}
231+
232+
MachineBasicBlock::iterator MII = I;
233+
return BuildMI(BB, MII, DL, MCID, DestReg);
234+
}
235+
212236
/// BuildMI - This version of the builder inserts the newly-built
213237
/// instruction before the given position in the given MachineBasicBlock, and
214238
/// does NOT take a destination register.
@@ -222,6 +246,28 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
222246
return MachineInstrBuilder(MI);
223247
}
224248

249+
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
250+
MachineBasicBlock::instr_iterator I,
251+
DebugLoc DL,
252+
const MCInstrDesc &MCID) {
253+
MachineInstr *MI = BB.getParent()->CreateMachineInstr(MCID, DL);
254+
BB.insert(I, MI);
255+
return MachineInstrBuilder(MI);
256+
}
257+
258+
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
259+
MachineInstr *I,
260+
DebugLoc DL,
261+
const MCInstrDesc &MCID) {
262+
if (I->isInsideBundle()) {
263+
MachineBasicBlock::instr_iterator MII = I;
264+
return BuildMI(BB, MII, DL, MCID);
265+
}
266+
267+
MachineBasicBlock::iterator MII = I;
268+
return BuildMI(BB, MII, DL, MCID);
269+
}
270+
225271
/// BuildMI - This version of the builder inserts the newly-built
226272
/// instruction at the end of the given MachineBasicBlock, and does NOT take a
227273
/// destination register.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===-- CodeGen/MachineInstBundle.h - MI bundle utilities -------*- 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 provide utility functions to manipulate machine instruction
11+
// bundles.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
16+
#define LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
17+
18+
#include "llvm/CodeGen/MachineBasicBlock.h"
19+
20+
namespace llvm {
21+
22+
/// FinalizeBundle - Finalize a machine instruction bundle which includes
23+
/// a sequence of instructions starting from FirstMI to LastMI (inclusive).
24+
/// This routine adds a BUNDLE instruction to represent the bundle, it adds
25+
/// IsInternalRead markers to MachineOperands which are defined inside the
26+
/// bundle, and it copies externally visible defs and uses to the BUNDLE
27+
/// instruction.
28+
void FinalizeBundle(MachineBasicBlock &MBB,
29+
MachineBasicBlock::instr_iterator FirstMI,
30+
MachineBasicBlock::instr_iterator LastMI);
31+
32+
} // End llvm namespace
33+
34+
#endif

‎llvm/include/llvm/CodeGen/Passes.h

+4
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ namespace llvm {
238238
///
239239
FunctionPass *createExecutionDependencyFixPass(const TargetRegisterClass *RC);
240240

241+
/// createUnpackMachineBundles - This pass unpack machine instruction bundles.
242+
///
243+
FunctionPass *createUnpackMachineBundlesPass();
244+
241245
} // End llvm namespace
242246

243247
#endif

‎llvm/include/llvm/InitializePasses.h

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ void initializeUnreachableMachineBlockElimPass(PassRegistry&);
231231
void initializeVerifierPass(PassRegistry&);
232232
void initializeVirtRegMapPass(PassRegistry&);
233233
void initializeInstSimplifierPass(PassRegistry&);
234+
void initializeUnpackMachineBundlesPass(PassRegistry&);
234235

235236
}
236237

‎llvm/lib/CodeGen/BranchFolding.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,9 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
926926
if (MergePotentials.size() >= 2)
927927
MadeChange |= TryTailMergeBlocks(IBB, PredBB);
928928
// Reinsert an unconditional branch if needed.
929-
// The 1 below can occur as a result of removing blocks in TryTailMergeBlocks.
930-
PredBB = prior(I); // this may have been changed in TryTailMergeBlocks
929+
// The 1 below can occur as a result of removing blocks in
930+
// TryTailMergeBlocks.
931+
PredBB = prior(I); // this may have been changed in TryTailMergeBlocks
931932
if (MergePotentials.size() == 1 &&
932933
MergePotentials.begin()->getBlock() != PredBB)
933934
FixTail(MergePotentials.begin()->getBlock(), IBB, TII);

0 commit comments

Comments
 (0)