MachineSSAUpdater.h revision 327952
1327952Sdim//===- 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 19327952Sdimclass MachineBasicBlock; 20327952Sdimclass MachineFunction; 21327952Sdimclass MachineInstr; 22327952Sdimclass MachineOperand; 23327952Sdimclass MachineRegisterInfo; 24327952Sdimclass TargetInstrInfo; 25327952Sdimclass TargetRegisterClass; 26327952Sdimtemplate<typename T> class SmallVectorImpl; 27327952Sdimtemplate<typename T> class SSAUpdaterTraits; 28327952Sdim 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; 40327952Sdim void *AV = nullptr; 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; 54327952Sdim 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, 59276479Sdim SmallVectorImpl<MachineInstr*> *InsertedPHIs = nullptr); 60327952Sdim MachineSSAUpdater(const MachineSSAUpdater &) = delete; 61327952Sdim MachineSSAUpdater &operator=(const MachineSSAUpdater &) = delete; 62200581Srdivacky ~MachineSSAUpdater(); 63200581Srdivacky 64200581Srdivacky /// Initialize - Reset this object to get ready for a new set of SSA 65200581Srdivacky /// updates. 66200581Srdivacky void Initialize(unsigned V); 67200581Srdivacky 68200581Srdivacky /// AddAvailableValue - Indicate that a rewritten value is available at the 69200581Srdivacky /// end of the specified block with the specified value. 70200581Srdivacky void AddAvailableValue(MachineBasicBlock *BB, unsigned V); 71200581Srdivacky 72200581Srdivacky /// HasValueForBlock - Return true if the MachineSSAUpdater already has a 73200581Srdivacky /// value for the specified block. 74200581Srdivacky bool HasValueForBlock(MachineBasicBlock *BB) const; 75200581Srdivacky 76200581Srdivacky /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is 77200581Srdivacky /// live at the end of the specified block. 78200581Srdivacky unsigned GetValueAtEndOfBlock(MachineBasicBlock *BB); 79200581Srdivacky 80200581Srdivacky /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that 81200581Srdivacky /// is live in the middle of the specified block. 82200581Srdivacky /// 83200581Srdivacky /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one 84200581Srdivacky /// important case: if there is a definition of the rewritten value after the 85200581Srdivacky /// 'use' in BB. Consider code like this: 86200581Srdivacky /// 87200581Srdivacky /// X1 = ... 88200581Srdivacky /// SomeBB: 89200581Srdivacky /// use(X) 90200581Srdivacky /// X2 = ... 91200581Srdivacky /// br Cond, SomeBB, OutBB 92200581Srdivacky /// 93200581Srdivacky /// In this case, there are two values (X1 and X2) added to the AvailableVals 94200581Srdivacky /// set by the client of the rewriter, and those values are both live out of 95200581Srdivacky /// their respective blocks. However, the use of X happens in the *middle* of 96200581Srdivacky /// a block. Because of this, we need to insert a new PHI node in SomeBB to 97200581Srdivacky /// merge the appropriate values, and this value isn't live out of the block. 98200581Srdivacky unsigned GetValueInMiddleOfBlock(MachineBasicBlock *BB); 99200581Srdivacky 100200581Srdivacky /// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes, 101200581Srdivacky /// which use their value in the corresponding predecessor. Note that this 102200581Srdivacky /// will not work if the use is supposed to be rewritten to a value defined in 103200581Srdivacky /// the same block as the use, but above it. Any 'AddAvailableValue's added 104200581Srdivacky /// for the use's block will be considered to be below it. 105200581Srdivacky void RewriteUse(MachineOperand &U); 106200581Srdivacky 107200581Srdivackyprivate: 108200581Srdivacky unsigned GetValueAtEndOfBlockInternal(MachineBasicBlock *BB); 109200581Srdivacky}; 110200581Srdivacky 111327952Sdim} // end namespace llvm 112200581Srdivacky 113327952Sdim#endif // LLVM_CODEGEN_MACHINESSAUPDATER_H 114