1 //===-- lib/CodeGen/GlobalISel/GICombiner.cpp -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file constains common code to combine machine functions at generic
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/GlobalISel/Combiner.h"
15 #include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
16 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
17 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
18 #include "llvm/CodeGen/GlobalISel/GISelWorkList.h"
19 #include "llvm/ADT/PostOrderIterator.h"
20 #include "llvm/CodeGen/GlobalISel/Utils.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/Support/Debug.h"
24 #define DEBUG_TYPE "gi-combiner"
28 Combiner::Combiner(CombinerInfo &Info, const TargetPassConfig *TPC)
29 : CInfo(Info), TPC(TPC) {
30 (void)this->TPC; // FIXME: Remove when used.
33 bool Combiner::combineMachineInstrs(MachineFunction &MF) {
34 // If the ISel pipeline failed, do not bother running this pass.
35 // FIXME: Should this be here or in individual combiner passes.
36 if (MF.getProperties().hasProperty(
37 MachineFunctionProperties::Property::FailedISel))
40 MRI = &MF.getRegInfo();
43 LLVM_DEBUG(dbgs() << "Generic MI Combiner for: " << MF.getName() << '\n');
45 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
47 bool MFChanged = false;
51 // Collect all instructions. Do a post order traversal for basic blocks and
52 // insert with list bottom up, so while we pop_back_val, we'll traverse top
55 GISelWorkList<512> WorkList;
56 for (MachineBasicBlock *MBB : post_order(&MF)) {
59 for (auto MII = MBB->rbegin(), MIE = MBB->rend(); MII != MIE;) {
60 MachineInstr *CurMI = &*MII;
62 // Erase dead insts before even adding to the list.
63 if (isTriviallyDead(*CurMI, *MRI)) {
64 LLVM_DEBUG(dbgs() << *CurMI << "Is dead; erasing.\n");
65 CurMI->eraseFromParentAndMarkDBGValuesForRemoval();
68 WorkList.insert(CurMI);
71 // Main Loop. Process the instructions here.
72 while (!WorkList.empty()) {
73 MachineInstr *CurrInst = WorkList.pop_back_val();
74 LLVM_DEBUG(dbgs() << "Try combining " << *CurrInst << "\n";);
75 Changed |= CInfo.combine(*CurrInst, Builder);