MachineSSAUpdater.h revision 200581
1200581Srdivacky//===-- MachineSSAUpdater.h - Unstructured SSA Update Tool ------*- C++ -*-===// 2200581Srdivacky// 3200581Srdivacky// The LLVM Compiler Infrastructure 4200581Srdivacky// 5200581Srdivacky// This file is distributed under the University of Illinois Open Source 6200581Srdivacky// License. See LICENSE.TXT for details. 7200581Srdivacky// 8200581Srdivacky//===----------------------------------------------------------------------===// 9200581Srdivacky// 10200581Srdivacky// This file declares the MachineSSAUpdater class. 11200581Srdivacky// 12200581Srdivacky//===----------------------------------------------------------------------===// 13200581Srdivacky 14200581Srdivacky#ifndef LLVM_CODEGEN_MACHINESSAUPDATER_H 15200581Srdivacky#define LLVM_CODEGEN_MACHINESSAUPDATER_H 16200581Srdivacky 17200581Srdivackynamespace llvm { 18200581Srdivacky class MachineBasicBlock; 19200581Srdivacky class MachineFunction; 20200581Srdivacky class MachineInstr; 21200581Srdivacky class MachineOperand; 22200581Srdivacky class MachineRegisterInfo; 23200581Srdivacky class TargetInstrInfo; 24200581Srdivacky class TargetRegisterClass; 25200581Srdivacky template<typename T> class SmallVectorImpl; 26200581Srdivacky 27200581Srdivacky/// MachineSSAUpdater - This class updates SSA form for a set of virtual 28200581Srdivacky/// registers defined in multiple blocks. This is used when code duplication 29200581Srdivacky/// or another unstructured transformation wants to rewrite a set of uses of one 30200581Srdivacky/// vreg with uses of a set of vregs. 31200581Srdivackyclass MachineSSAUpdater { 32200581Srdivacky /// AvailableVals - This keeps track of which value to use on a per-block 33200581Srdivacky /// basis. When we insert PHI nodes, we keep track of them here. 34200581Srdivacky //typedef DenseMap<MachineBasicBlock*, unsigned > AvailableValsTy; 35200581Srdivacky void *AV; 36200581Srdivacky 37200581Srdivacky /// IncomingPredInfo - We use this as scratch space when doing our recursive 38200581Srdivacky /// walk. This should only be used in GetValueInBlockInternal, normally it 39200581Srdivacky /// should be empty. 40200581Srdivacky //std::vector<std::pair<MachineBasicBlock*, unsigned > > IncomingPredInfo; 41200581Srdivacky void *IPI; 42200581Srdivacky 43200581Srdivacky /// VR - Current virtual register whose uses are being updated. 44200581Srdivacky unsigned VR; 45200581Srdivacky 46200581Srdivacky /// VRC - Register class of the current virtual register. 47200581Srdivacky const TargetRegisterClass *VRC; 48200581Srdivacky 49200581Srdivacky /// InsertedPHIs - If this is non-null, the MachineSSAUpdater adds all PHI 50200581Srdivacky /// nodes that it creates to the vector. 51200581Srdivacky SmallVectorImpl<MachineInstr*> *InsertedPHIs; 52200581Srdivacky 53200581Srdivacky const TargetInstrInfo *TII; 54200581Srdivacky MachineRegisterInfo *MRI; 55200581Srdivackypublic: 56200581Srdivacky /// MachineSSAUpdater constructor. If InsertedPHIs is specified, it will be 57200581Srdivacky /// filled in with all PHI Nodes created by rewriting. 58200581Srdivacky explicit MachineSSAUpdater(MachineFunction &MF, 59200581Srdivacky SmallVectorImpl<MachineInstr*> *InsertedPHIs = 0); 60200581Srdivacky ~MachineSSAUpdater(); 61200581Srdivacky 62200581Srdivacky /// Initialize - Reset this object to get ready for a new set of SSA 63200581Srdivacky /// updates. 64200581Srdivacky void Initialize(unsigned V); 65200581Srdivacky 66200581Srdivacky /// AddAvailableValue - Indicate that a rewritten value is available at the 67200581Srdivacky /// end of the specified block with the specified value. 68200581Srdivacky void AddAvailableValue(MachineBasicBlock *BB, unsigned V); 69200581Srdivacky 70200581Srdivacky /// HasValueForBlock - Return true if the MachineSSAUpdater already has a 71200581Srdivacky /// value for the specified block. 72200581Srdivacky bool HasValueForBlock(MachineBasicBlock *BB) const; 73200581Srdivacky 74200581Srdivacky /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is 75200581Srdivacky /// live at the end of the specified block. 76200581Srdivacky unsigned GetValueAtEndOfBlock(MachineBasicBlock *BB); 77200581Srdivacky 78200581Srdivacky /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that 79200581Srdivacky /// is live in the middle of the specified block. 80200581Srdivacky /// 81200581Srdivacky /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one 82200581Srdivacky /// important case: if there is a definition of the rewritten value after the 83200581Srdivacky /// 'use' in BB. Consider code like this: 84200581Srdivacky /// 85200581Srdivacky /// X1 = ... 86200581Srdivacky /// SomeBB: 87200581Srdivacky /// use(X) 88200581Srdivacky /// X2 = ... 89200581Srdivacky /// br Cond, SomeBB, OutBB 90200581Srdivacky /// 91200581Srdivacky /// In this case, there are two values (X1 and X2) added to the AvailableVals 92200581Srdivacky /// set by the client of the rewriter, and those values are both live out of 93200581Srdivacky /// their respective blocks. However, the use of X happens in the *middle* of 94200581Srdivacky /// a block. Because of this, we need to insert a new PHI node in SomeBB to 95200581Srdivacky /// merge the appropriate values, and this value isn't live out of the block. 96200581Srdivacky /// 97200581Srdivacky unsigned GetValueInMiddleOfBlock(MachineBasicBlock *BB); 98200581Srdivacky 99200581Srdivacky /// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes, 100200581Srdivacky /// which use their value in the corresponding predecessor. Note that this 101200581Srdivacky /// will not work if the use is supposed to be rewritten to a value defined in 102200581Srdivacky /// the same block as the use, but above it. Any 'AddAvailableValue's added 103200581Srdivacky /// for the use's block will be considered to be below it. 104200581Srdivacky void RewriteUse(MachineOperand &U); 105200581Srdivacky 106200581Srdivackyprivate: 107200581Srdivacky void ReplaceRegWith(unsigned OldReg, unsigned NewReg); 108200581Srdivacky unsigned GetValueAtEndOfBlockInternal(MachineBasicBlock *BB); 109200581Srdivacky void operator=(const MachineSSAUpdater&); // DO NOT IMPLEMENT 110200581Srdivacky MachineSSAUpdater(const MachineSSAUpdater&); // DO NOT IMPLEMENT 111200581Srdivacky}; 112200581Srdivacky 113200581Srdivacky} // End llvm namespace 114200581Srdivacky 115200581Srdivacky#endif 116