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