blob: 45a97182e71c52c8c3ede2cbb614391eb3dce6c6 [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
Lang Hames287b8b02009-07-23 04:34:03 +000013#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/SmallSet.h"
Lang Hamesfae02a22009-07-21 23:47:33 +000015#include "llvm/ADT/SmallPtrSet.h"
Evan Chenge0083842010-08-17 17:43:50 +000016#include "llvm/CodeGen/MachineBasicBlock.h"
Lang Hamesfae02a22009-07-21 23:47:33 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
Lang Hamesfae02a22009-07-21 23:47:33 +000018
Lang Hamesfae02a22009-07-21 23:47:33 +000019namespace llvm {
Chris Lattner518bb532010-02-09 19:54:29 +000020 class LiveVariables;
Evan Chenge0083842010-08-17 17:43:50 +000021 class MachineRegisterInfo;
22 class MachineLoopInfo;
Chris Lattner518bb532010-02-09 19:54:29 +000023
Lang Hamesfae02a22009-07-21 23:47:33 +000024 /// Lower PHI instructions to copies.
25 class PHIElimination : public MachineFunctionPass {
Evan Chenge0083842010-08-17 17:43:50 +000026 MachineRegisterInfo *MRI; // Machine register information
Lang Hamesfae02a22009-07-21 23:47:33 +000027
28 public:
29 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000030 PHIElimination() : MachineFunctionPass(ID) {}
Lang Hamesfae02a22009-07-21 23:47:33 +000031
32 virtual bool runOnMachineFunction(MachineFunction &Fn);
33
34 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
35
36 private:
37 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
38 /// in predecessor basic blocks.
39 ///
40 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
41 void LowerAtomicPHINode(MachineBasicBlock &MBB,
42 MachineBasicBlock::iterator AfterPHIsIt);
43
44 /// analyzePHINodes - Gather information about the PHI nodes in
45 /// here. In particular, we want to map the number of uses of a virtual
46 /// register which is used in a PHI node. We map that to the BB the
47 /// vreg is coming from. This is used later to determine when the vreg
48 /// is killed in the BB.
49 ///
50 void analyzePHINodes(const MachineFunction& Fn);
51
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +000052 /// Split critical edges where necessary for good coalescer performance.
Jakob Stoklund Olesen0257dd32009-11-18 18:01:35 +000053 bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
Evan Cheng148341c2010-08-17 21:00:37 +000054 LiveVariables &LV, MachineLoopInfo *MLI);
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +000055
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +000056 /// SplitCriticalEdge - Split a critical edge from A to B by
57 /// inserting a new MBB. Update branches in A and PHI instructions
58 /// in B. Return the new block.
59 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *A,
60 MachineBasicBlock *B);
61
Jakob Stoklund Olesen12222872009-11-13 21:56:15 +000062 /// FindCopyInsertPoint - Find a safe place in MBB to insert a copy from
63 /// SrcReg when following the CFG edge to SuccMBB. This needs to be after
64 /// any def of SrcReg, but before any subsequent point where control flow
65 /// might jump out of the basic block.
Lang Hamesfae02a22009-07-21 23:47:33 +000066 MachineBasicBlock::iterator FindCopyInsertPoint(MachineBasicBlock &MBB,
Jakob Stoklund Olesen12222872009-11-13 21:56:15 +000067 MachineBasicBlock &SuccMBB,
Lang Hamesfae02a22009-07-21 23:47:33 +000068 unsigned SrcReg);
69
70 // SkipPHIsAndLabels - Copies need to be inserted after phi nodes and
71 // also after any exception handling labels: in landing pads execution
72 // starts at the label, so any copies placed before it won't be executed!
Dale Johannesen6e303cb2010-02-16 01:57:28 +000073 // We also deal with DBG_VALUEs, which are a bit tricky:
74 // PHI
75 // DBG_VALUE
76 // LABEL
77 // Here the DBG_VALUE needs to be skipped, and if it refers to a PHI it
78 // needs to be annulled or, better, moved to follow the label, as well.
79 // PHI
80 // DBG_VALUE
81 // no label
82 // Here it is not a good idea to skip the DBG_VALUE.
83 // FIXME: For now we skip and annul all DBG_VALUEs, maximally simple and
84 // maximally stupid.
Lang Hamesfae02a22009-07-21 23:47:33 +000085 MachineBasicBlock::iterator SkipPHIsAndLabels(MachineBasicBlock &MBB,
86 MachineBasicBlock::iterator I) {
87 // Rather than assuming that EH labels come before other kinds of labels,
88 // just skip all labels.
Dale Johannesen6e303cb2010-02-16 01:57:28 +000089 while (I != MBB.end() &&
90 (I->isPHI() || I->isLabel() || I->isDebugValue())) {
91 if (I->isDebugValue() && I->getNumOperands()==3 &&
92 I->getOperand(0).isReg())
93 I->getOperand(0).setReg(0U);
Lang Hamesfae02a22009-07-21 23:47:33 +000094 ++I;
Dale Johannesen6e303cb2010-02-16 01:57:28 +000095 }
Lang Hamesfae02a22009-07-21 23:47:33 +000096 return I;
97 }
98
Jakob Stoklund Olesen74215fc2009-12-16 18:55:53 +000099 typedef std::pair<unsigned, unsigned> BBVRegPair;
100 typedef DenseMap<BBVRegPair, unsigned> VRegPHIUse;
Lang Hamesfae02a22009-07-21 23:47:33 +0000101
102 VRegPHIUse VRegPHIUseCount;
Lang Hamesfae02a22009-07-21 23:47:33 +0000103
104 // Defs of PHI sources which are implicit_def.
105 SmallPtrSet<MachineInstr*, 4> ImpDefs;
Jakob Stoklund Olesen74215fc2009-12-16 18:55:53 +0000106
Jakob Stoklund Olesen74215fc2009-12-16 18:55:53 +0000107 // Map reusable lowered PHI node -> incoming join register.
Evan Chenga92dced2010-03-03 23:55:49 +0000108 typedef DenseMap<MachineInstr*, unsigned,
109 MachineInstrExpressionTrait> LoweredPHIMap;
Jakob Stoklund Olesen74215fc2009-12-16 18:55:53 +0000110 LoweredPHIMap LoweredPHIs;
Lang Hamesfae02a22009-07-21 23:47:33 +0000111 };
112
113}
114
Evan Chenge9390912009-09-04 07:46:30 +0000115#endif /* LLVM_CODEGEN_PHIELIMINATION_HPP */