blob: 7dedf0318a8a67ca296044054a3d12bb437892cf [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"
16#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner518bb532010-02-09 19:54:29 +000017#include "llvm/CodeGen/MachineRegisterInfo.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;
21
Lang Hamesfae02a22009-07-21 23:47:33 +000022 /// Lower PHI instructions to copies.
23 class PHIElimination : public MachineFunctionPass {
24 MachineRegisterInfo *MRI; // Machine register information
25
26 public:
27 static char ID; // Pass identification, replacement for typeid
28 PHIElimination() : MachineFunctionPass(&ID) {}
29
30 virtual bool runOnMachineFunction(MachineFunction &Fn);
31
32 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
33
34 private:
35 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
36 /// in predecessor basic blocks.
37 ///
38 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
39 void LowerAtomicPHINode(MachineBasicBlock &MBB,
40 MachineBasicBlock::iterator AfterPHIsIt);
41
42 /// analyzePHINodes - Gather information about the PHI nodes in
43 /// here. In particular, we want to map the number of uses of a virtual
44 /// register which is used in a PHI node. We map that to the BB the
45 /// vreg is coming from. This is used later to determine when the vreg
46 /// is killed in the BB.
47 ///
48 void analyzePHINodes(const MachineFunction& Fn);
49
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +000050 /// Split critical edges where necessary for good coalescer performance.
Jakob Stoklund Olesen0257dd32009-11-18 18:01:35 +000051 bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
52 LiveVariables &LV);
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +000053
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +000054 /// SplitCriticalEdge - Split a critical edge from A to B by
55 /// inserting a new MBB. Update branches in A and PHI instructions
56 /// in B. Return the new block.
57 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *A,
58 MachineBasicBlock *B);
59
Jakob Stoklund Olesen12222872009-11-13 21:56:15 +000060 /// FindCopyInsertPoint - Find a safe place in MBB to insert a copy from
61 /// SrcReg when following the CFG edge to SuccMBB. This needs to be after
62 /// any def of SrcReg, but before any subsequent point where control flow
63 /// might jump out of the basic block.
Lang Hamesfae02a22009-07-21 23:47:33 +000064 MachineBasicBlock::iterator FindCopyInsertPoint(MachineBasicBlock &MBB,
Jakob Stoklund Olesen12222872009-11-13 21:56:15 +000065 MachineBasicBlock &SuccMBB,
Lang Hamesfae02a22009-07-21 23:47:33 +000066 unsigned SrcReg);
67
68 // SkipPHIsAndLabels - Copies need to be inserted after phi nodes and
69 // also after any exception handling labels: in landing pads execution
70 // starts at the label, so any copies placed before it won't be executed!
Dale Johannesen6e303cb2010-02-16 01:57:28 +000071 // We also deal with DBG_VALUEs, which are a bit tricky:
72 // PHI
73 // DBG_VALUE
74 // LABEL
75 // Here the DBG_VALUE needs to be skipped, and if it refers to a PHI it
76 // needs to be annulled or, better, moved to follow the label, as well.
77 // PHI
78 // DBG_VALUE
79 // no label
80 // Here it is not a good idea to skip the DBG_VALUE.
81 // FIXME: For now we skip and annul all DBG_VALUEs, maximally simple and
82 // maximally stupid.
Lang Hamesfae02a22009-07-21 23:47:33 +000083 MachineBasicBlock::iterator SkipPHIsAndLabels(MachineBasicBlock &MBB,
84 MachineBasicBlock::iterator I) {
85 // Rather than assuming that EH labels come before other kinds of labels,
86 // just skip all labels.
Dale Johannesen6e303cb2010-02-16 01:57:28 +000087 while (I != MBB.end() &&
88 (I->isPHI() || I->isLabel() || I->isDebugValue())) {
89 if (I->isDebugValue() && I->getNumOperands()==3 &&
90 I->getOperand(0).isReg())
91 I->getOperand(0).setReg(0U);
Lang Hamesfae02a22009-07-21 23:47:33 +000092 ++I;
Dale Johannesen6e303cb2010-02-16 01:57:28 +000093 }
Lang Hamesfae02a22009-07-21 23:47:33 +000094 return I;
95 }
96
Jakob Stoklund Olesen74215fc2009-12-16 18:55:53 +000097 typedef std::pair<unsigned, unsigned> BBVRegPair;
98 typedef DenseMap<BBVRegPair, unsigned> VRegPHIUse;
Lang Hamesfae02a22009-07-21 23:47:33 +000099
100 VRegPHIUse VRegPHIUseCount;
Lang Hamesfae02a22009-07-21 23:47:33 +0000101
102 // Defs of PHI sources which are implicit_def.
103 SmallPtrSet<MachineInstr*, 4> ImpDefs;
Jakob Stoklund Olesen74215fc2009-12-16 18:55:53 +0000104
Jakob Stoklund Olesen74215fc2009-12-16 18:55:53 +0000105 // Map reusable lowered PHI node -> incoming join register.
Evan Chenga92dced2010-03-03 23:55:49 +0000106 typedef DenseMap<MachineInstr*, unsigned,
107 MachineInstrExpressionTrait> LoweredPHIMap;
Jakob Stoklund Olesen74215fc2009-12-16 18:55:53 +0000108 LoweredPHIMap LoweredPHIs;
Lang Hamesfae02a22009-07-21 23:47:33 +0000109 };
110
111}
112
Evan Chenge9390912009-09-04 07:46:30 +0000113#endif /* LLVM_CODEGEN_PHIELIMINATION_HPP */