MachineSSAUpdater.h revision 208599
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; 26208599Srdivacky template<typename T> class SSAUpdaterTraits; 27207618Srdivacky class BumpPtrAllocator; 28200581Srdivacky 29200581Srdivacky/// MachineSSAUpdater - This class updates SSA form for a set of virtual 30200581Srdivacky/// registers defined in multiple blocks. This is used when code duplication 31200581Srdivacky/// or another unstructured transformation wants to rewrite a set of uses of one 32200581Srdivacky/// vreg with uses of a set of vregs. 33200581Srdivackyclass MachineSSAUpdater { 34208599Srdivacky friend class SSAUpdaterTraits<MachineSSAUpdater>; 35207618Srdivacky 36207618Srdivackyprivate: 37200581Srdivacky /// AvailableVals - This keeps track of which value to use on a per-block 38200581Srdivacky /// basis. When we insert PHI nodes, we keep track of them here. 39200581Srdivacky //typedef DenseMap<MachineBasicBlock*, unsigned > AvailableValsTy; 40200581Srdivacky void *AV; 41200581Srdivacky 42200581Srdivacky /// VR - Current virtual register whose uses are being updated. 43200581Srdivacky unsigned VR; 44200581Srdivacky 45200581Srdivacky /// VRC - Register class of the current virtual register. 46200581Srdivacky const TargetRegisterClass *VRC; 47200581Srdivacky 48200581Srdivacky /// InsertedPHIs - If this is non-null, the MachineSSAUpdater adds all PHI 49200581Srdivacky /// nodes that it creates to the vector. 50200581Srdivacky SmallVectorImpl<MachineInstr*> *InsertedPHIs; 51200581Srdivacky 52200581Srdivacky const TargetInstrInfo *TII; 53200581Srdivacky MachineRegisterInfo *MRI; 54200581Srdivackypublic: 55200581Srdivacky /// MachineSSAUpdater constructor. If InsertedPHIs is specified, it will be 56200581Srdivacky /// filled in with all PHI Nodes created by rewriting. 57200581Srdivacky explicit MachineSSAUpdater(MachineFunction &MF, 58200581Srdivacky SmallVectorImpl<MachineInstr*> *InsertedPHIs = 0); 59200581Srdivacky ~MachineSSAUpdater(); 60200581Srdivacky 61200581Srdivacky /// Initialize - Reset this object to get ready for a new set of SSA 62200581Srdivacky /// updates. 63200581Srdivacky void Initialize(unsigned V); 64200581Srdivacky 65200581Srdivacky /// AddAvailableValue - Indicate that a rewritten value is available at the 66200581Srdivacky /// end of the specified block with the specified value. 67200581Srdivacky void AddAvailableValue(MachineBasicBlock *BB, unsigned V); 68200581Srdivacky 69200581Srdivacky /// HasValueForBlock - Return true if the MachineSSAUpdater already has a 70200581Srdivacky /// value for the specified block. 71200581Srdivacky bool HasValueForBlock(MachineBasicBlock *BB) const; 72200581Srdivacky 73200581Srdivacky /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is 74200581Srdivacky /// live at the end of the specified block. 75200581Srdivacky unsigned GetValueAtEndOfBlock(MachineBasicBlock *BB); 76200581Srdivacky 77200581Srdivacky /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that 78200581Srdivacky /// is live in the middle of the specified block. 79200581Srdivacky /// 80200581Srdivacky /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one 81200581Srdivacky /// important case: if there is a definition of the rewritten value after the 82200581Srdivacky /// 'use' in BB. Consider code like this: 83200581Srdivacky /// 84200581Srdivacky /// X1 = ... 85200581Srdivacky /// SomeBB: 86200581Srdivacky /// use(X) 87200581Srdivacky /// X2 = ... 88200581Srdivacky /// br Cond, SomeBB, OutBB 89200581Srdivacky /// 90200581Srdivacky /// In this case, there are two values (X1 and X2) added to the AvailableVals 91200581Srdivacky /// set by the client of the rewriter, and those values are both live out of 92200581Srdivacky /// their respective blocks. However, the use of X happens in the *middle* of 93200581Srdivacky /// a block. Because of this, we need to insert a new PHI node in SomeBB to 94200581Srdivacky /// merge the appropriate values, and this value isn't live out of the block. 95200581Srdivacky /// 96200581Srdivacky unsigned GetValueInMiddleOfBlock(MachineBasicBlock *BB); 97200581Srdivacky 98200581Srdivacky /// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes, 99200581Srdivacky /// which use their value in the corresponding predecessor. Note that this 100200581Srdivacky /// will not work if the use is supposed to be rewritten to a value defined in 101200581Srdivacky /// the same block as the use, but above it. Any 'AddAvailableValue's added 102200581Srdivacky /// for the use's block will be considered to be below it. 103200581Srdivacky void RewriteUse(MachineOperand &U); 104200581Srdivacky 105200581Srdivackyprivate: 106200581Srdivacky void ReplaceRegWith(unsigned OldReg, unsigned NewReg); 107200581Srdivacky unsigned GetValueAtEndOfBlockInternal(MachineBasicBlock *BB); 108207618Srdivacky 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