blob: b054732dace526972fc11894117342357e8ab5c8 [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
27 MachineLoopInfo *MLI;
Lang Hamesfae02a22009-07-21 23:47:33 +000028
29 public:
30 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000031 PHIElimination() : MachineFunctionPass(ID) {}
Lang Hamesfae02a22009-07-21 23:47:33 +000032
33 virtual bool runOnMachineFunction(MachineFunction &Fn);
34
35 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
36
37 private:
38 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
39 /// in predecessor basic blocks.
40 ///
41 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
42 void LowerAtomicPHINode(MachineBasicBlock &MBB,
43 MachineBasicBlock::iterator AfterPHIsIt);
44
45 /// analyzePHINodes - Gather information about the PHI nodes in
46 /// here. In particular, we want to map the number of uses of a virtual
47 /// register which is used in a PHI node. We map that to the BB the
48 /// vreg is coming from. This is used later to determine when the vreg
49 /// is killed in the BB.
50 ///
51 void analyzePHINodes(const MachineFunction& Fn);
52
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +000053 /// Split critical edges where necessary for good coalescer performance.
Jakob Stoklund Olesen0257dd32009-11-18 18:01:35 +000054 bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
55 LiveVariables &LV);
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +000056
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +000057 /// SplitCriticalEdge - Split a critical edge from A to B by
58 /// inserting a new MBB. Update branches in A and PHI instructions
59 /// in B. Return the new block.
60 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *A,
61 MachineBasicBlock *B);
62
Jakob Stoklund Olesen12222872009-11-13 21:56:15 +000063 /// FindCopyInsertPoint - Find a safe place in MBB to insert a copy from
64 /// SrcReg when following the CFG edge to SuccMBB. This needs to be after
65 /// any def of SrcReg, but before any subsequent point where control flow
66 /// might jump out of the basic block.
Lang Hamesfae02a22009-07-21 23:47:33 +000067 MachineBasicBlock::iterator FindCopyInsertPoint(MachineBasicBlock &MBB,
Jakob Stoklund Olesen12222872009-11-13 21:56:15 +000068 MachineBasicBlock &SuccMBB,
Lang Hamesfae02a22009-07-21 23:47:33 +000069 unsigned SrcReg);
70
71 // SkipPHIsAndLabels - Copies need to be inserted after phi nodes and
72 // also after any exception handling labels: in landing pads execution
73 // starts at the label, so any copies placed before it won't be executed!
Dale Johannesen6e303cb2010-02-16 01:57:28 +000074 // We also deal with DBG_VALUEs, which are a bit tricky:
75 // PHI
76 // DBG_VALUE
77 // LABEL
78 // Here the DBG_VALUE needs to be skipped, and if it refers to a PHI it
79 // needs to be annulled or, better, moved to follow the label, as well.
80 // PHI
81 // DBG_VALUE
82 // no label
83 // Here it is not a good idea to skip the DBG_VALUE.
84 // FIXME: For now we skip and annul all DBG_VALUEs, maximally simple and
85 // maximally stupid.
Lang Hamesfae02a22009-07-21 23:47:33 +000086 MachineBasicBlock::iterator SkipPHIsAndLabels(MachineBasicBlock &MBB,
87 MachineBasicBlock::iterator I) {
88 // Rather than assuming that EH labels come before other kinds of labels,
89 // just skip all labels.
Dale Johannesen6e303cb2010-02-16 01:57:28 +000090 while (I != MBB.end() &&
91 (I->isPHI() || I->isLabel() || I->isDebugValue())) {
92 if (I->isDebugValue() && I->getNumOperands()==3 &&
93 I->getOperand(0).isReg())
94 I->getOperand(0).setReg(0U);
Lang Hamesfae02a22009-07-21 23:47:33 +000095 ++I;
Dale Johannesen6e303cb2010-02-16 01:57:28 +000096 }
Lang Hamesfae02a22009-07-21 23:47:33 +000097 return I;
98 }
99
Jakob Stoklund Olesen74215fc2009-12-16 18:55:53 +0000100 typedef std::pair<unsigned, unsigned> BBVRegPair;
101 typedef DenseMap<BBVRegPair, unsigned> VRegPHIUse;
Lang Hamesfae02a22009-07-21 23:47:33 +0000102
103 VRegPHIUse VRegPHIUseCount;
Lang Hamesfae02a22009-07-21 23:47:33 +0000104
105 // Defs of PHI sources which are implicit_def.
106 SmallPtrSet<MachineInstr*, 4> ImpDefs;
Jakob Stoklund Olesen74215fc2009-12-16 18:55:53 +0000107
Jakob Stoklund Olesen74215fc2009-12-16 18:55:53 +0000108 // Map reusable lowered PHI node -> incoming join register.
Evan Chenga92dced2010-03-03 23:55:49 +0000109 typedef DenseMap<MachineInstr*, unsigned,
110 MachineInstrExpressionTrait> LoweredPHIMap;
Jakob Stoklund Olesen74215fc2009-12-16 18:55:53 +0000111 LoweredPHIMap LoweredPHIs;
Lang Hamesfae02a22009-07-21 23:47:33 +0000112 };
113
114}
115
Evan Chenge9390912009-09-04 07:46:30 +0000116#endif /* LLVM_CODEGEN_PHIELIMINATION_HPP */