blob: 26a37316eb8b2d36fdde66b1cd866413f8488718 [file] [log] [blame]
Lang Hamesfae02a22009-07-21 23:47:33 +00001//===-- lib/CodeGen/PHIElimination.h ----------------------------*- 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#ifndef LLVM_CODEGEN_PHIELIMINATION_HPP
11#define LLVM_CODEGEN_PHIELIMINATION_HPP
12
13#include "llvm/ADT/SmallPtrSet.h"
14#include "llvm/CodeGen/MachineFunctionPass.h"
15#include "llvm/Target/TargetInstrInfo.h"
16
17#include <map>
18
19namespace llvm {
20
21 /// Lower PHI instructions to copies.
22 class PHIElimination : public MachineFunctionPass {
23 MachineRegisterInfo *MRI; // Machine register information
24
25 public:
26 static char ID; // Pass identification, replacement for typeid
27 PHIElimination() : MachineFunctionPass(&ID) {}
28
29 virtual bool runOnMachineFunction(MachineFunction &Fn);
30
31 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
32
33 private:
34 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
35 /// in predecessor basic blocks.
36 ///
37 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
38 void LowerAtomicPHINode(MachineBasicBlock &MBB,
39 MachineBasicBlock::iterator AfterPHIsIt);
40
41 /// analyzePHINodes - Gather information about the PHI nodes in
42 /// here. In particular, we want to map the number of uses of a virtual
43 /// register which is used in a PHI node. We map that to the BB the
44 /// vreg is coming from. This is used later to determine when the vreg
45 /// is killed in the BB.
46 ///
47 void analyzePHINodes(const MachineFunction& Fn);
48
49 // FindCopyInsertPoint - Find a safe place in MBB to insert a copy from
50 // SrcReg. This needs to be after any def or uses of SrcReg, but before
51 // any subsequent point where control flow might jump out of the basic
52 // block.
53 MachineBasicBlock::iterator FindCopyInsertPoint(MachineBasicBlock &MBB,
54 unsigned SrcReg);
55
56 // SkipPHIsAndLabels - Copies need to be inserted after phi nodes and
57 // also after any exception handling labels: in landing pads execution
58 // starts at the label, so any copies placed before it won't be executed!
59 MachineBasicBlock::iterator SkipPHIsAndLabels(MachineBasicBlock &MBB,
60 MachineBasicBlock::iterator I) {
61 // Rather than assuming that EH labels come before other kinds of labels,
62 // just skip all labels.
63 while (I != MBB.end() &&
64 (I->getOpcode() == TargetInstrInfo::PHI || I->isLabel()))
65 ++I;
66 return I;
67 }
68
69 typedef std::pair<const MachineBasicBlock*, unsigned> BBVRegPair;
70 typedef std::map<BBVRegPair, unsigned> VRegPHIUse;
71
72 VRegPHIUse VRegPHIUseCount;
73
74 // Defs of PHI sources which are implicit_def.
75 SmallPtrSet<MachineInstr*, 4> ImpDefs;
76 };
77
78}
79
80#endif /* PHIELIMINATION_H */