Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 1 | //===- MachineSSAUpdater.cpp - Unstructured SSA Update Tool ---------------===// |
| 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 | // |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 10 | // This file implements the MachineSSAUpdater class. It's based on SSAUpdater |
| 11 | // class in lib/Transforms/Utils. |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/CodeGen/MachineSSAUpdater.h" |
| 16 | #include "llvm/CodeGen/MachineInstr.h" |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 19 | #include "llvm/Target/TargetInstrInfo.h" |
| 20 | #include "llvm/Target/TargetMachine.h" |
| 21 | #include "llvm/Target/TargetRegisterInfo.h" |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 3143e90 | 2010-02-10 01:17:36 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 24 | #include "llvm/Support/AlignOf.h" |
| 25 | #include "llvm/Support/Allocator.h" |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/ErrorHandling.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 31 | /// BBInfo - Per-basic block information used internally by MachineSSAUpdater. |
| 32 | class MachineSSAUpdater::BBInfo { |
| 33 | public: |
| 34 | MachineBasicBlock *BB; // Back-pointer to the corresponding block. |
| 35 | unsigned AvailableVal; // Value to use in this block. |
| 36 | BBInfo *DefBB; // Block that defines the available value. |
| 37 | int BlkNum; // Postorder number. |
| 38 | BBInfo *IDom; // Immediate dominator. |
| 39 | unsigned NumPreds; // Number of predecessor blocks. |
| 40 | BBInfo **Preds; // Array[NumPreds] of predecessor blocks. |
| 41 | MachineInstr *PHITag; // Marker for existing PHIs that match. |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 42 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 43 | BBInfo(MachineBasicBlock *ThisBB, unsigned V) |
| 44 | : BB(ThisBB), AvailableVal(V), DefBB(V ? this : 0), BlkNum(0), IDom(0), |
| 45 | NumPreds(0), Preds(0), PHITag(0) { } |
| 46 | }; |
| 47 | |
| 48 | typedef DenseMap<MachineBasicBlock*, MachineSSAUpdater::BBInfo*> BBMapTy; |
| 49 | |
| 50 | typedef DenseMap<MachineBasicBlock*, unsigned> AvailableValsTy; |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 51 | static AvailableValsTy &getAvailableVals(void *AV) { |
| 52 | return *static_cast<AvailableValsTy*>(AV); |
| 53 | } |
| 54 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 55 | static BBMapTy *getBBMap(void *BM) { |
| 56 | return static_cast<BBMapTy*>(BM); |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 59 | MachineSSAUpdater::MachineSSAUpdater(MachineFunction &MF, |
| 60 | SmallVectorImpl<MachineInstr*> *NewPHI) |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 61 | : AV(0), BM(0), InsertedPHIs(NewPHI) { |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 62 | TII = MF.getTarget().getInstrInfo(); |
| 63 | MRI = &MF.getRegInfo(); |
| 64 | } |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 65 | |
| 66 | MachineSSAUpdater::~MachineSSAUpdater() { |
| 67 | delete &getAvailableVals(AV); |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /// Initialize - Reset this object to get ready for a new set of SSA |
| 71 | /// updates. ProtoValue is the value used to name PHI nodes. |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 72 | void MachineSSAUpdater::Initialize(unsigned V) { |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 73 | if (AV == 0) |
| 74 | AV = new AvailableValsTy(); |
| 75 | else |
| 76 | getAvailableVals(AV).clear(); |
| 77 | |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 78 | VR = V; |
| 79 | VRC = MRI->getRegClass(VR); |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /// HasValueForBlock - Return true if the MachineSSAUpdater already has a value for |
| 83 | /// the specified block. |
| 84 | bool MachineSSAUpdater::HasValueForBlock(MachineBasicBlock *BB) const { |
| 85 | return getAvailableVals(AV).count(BB); |
| 86 | } |
| 87 | |
| 88 | /// AddAvailableValue - Indicate that a rewritten value is available in the |
| 89 | /// specified block with the specified value. |
| 90 | void MachineSSAUpdater::AddAvailableValue(MachineBasicBlock *BB, unsigned V) { |
| 91 | getAvailableVals(AV)[BB] = V; |
| 92 | } |
| 93 | |
| 94 | /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is |
| 95 | /// live at the end of the specified block. |
| 96 | unsigned MachineSSAUpdater::GetValueAtEndOfBlock(MachineBasicBlock *BB) { |
| 97 | return GetValueAtEndOfBlockInternal(BB); |
| 98 | } |
| 99 | |
Evan Cheng | 3a41ddb | 2009-12-07 23:11:03 +0000 | [diff] [blame] | 100 | static |
| 101 | unsigned LookForIdenticalPHI(MachineBasicBlock *BB, |
| 102 | SmallVector<std::pair<MachineBasicBlock*, unsigned>, 8> &PredValues) { |
| 103 | if (BB->empty()) |
| 104 | return 0; |
| 105 | |
| 106 | MachineBasicBlock::iterator I = BB->front(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 107 | if (!I->isPHI()) |
Evan Cheng | 3a41ddb | 2009-12-07 23:11:03 +0000 | [diff] [blame] | 108 | return 0; |
| 109 | |
| 110 | AvailableValsTy AVals; |
| 111 | for (unsigned i = 0, e = PredValues.size(); i != e; ++i) |
| 112 | AVals[PredValues[i].first] = PredValues[i].second; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 113 | while (I != BB->end() && I->isPHI()) { |
Evan Cheng | 3a41ddb | 2009-12-07 23:11:03 +0000 | [diff] [blame] | 114 | bool Same = true; |
| 115 | for (unsigned i = 1, e = I->getNumOperands(); i != e; i += 2) { |
| 116 | unsigned SrcReg = I->getOperand(i).getReg(); |
| 117 | MachineBasicBlock *SrcBB = I->getOperand(i+1).getMBB(); |
| 118 | if (AVals[SrcBB] != SrcReg) { |
| 119 | Same = false; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | if (Same) |
| 124 | return I->getOperand(0).getReg(); |
| 125 | ++I; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
Evan Cheng | 01306ca | 2009-12-03 21:51:55 +0000 | [diff] [blame] | 130 | /// InsertNewDef - Insert an empty PHI or IMPLICIT_DEF instruction which define |
| 131 | /// a value of the given register class at the start of the specified basic |
| 132 | /// block. It returns the virtual register defined by the instruction. |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 133 | static |
Evan Cheng | 01306ca | 2009-12-03 21:51:55 +0000 | [diff] [blame] | 134 | MachineInstr *InsertNewDef(unsigned Opcode, |
| 135 | MachineBasicBlock *BB, MachineBasicBlock::iterator I, |
| 136 | const TargetRegisterClass *RC, |
| 137 | MachineRegisterInfo *MRI, const TargetInstrInfo *TII) { |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 138 | unsigned NewVR = MRI->createVirtualRegister(RC); |
Chris Lattner | a4f2bb0 | 2010-04-02 20:17:23 +0000 | [diff] [blame] | 139 | return BuildMI(*BB, I, DebugLoc(), TII->get(Opcode), NewVR); |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 140 | } |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 141 | |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 142 | /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that |
| 143 | /// is live in the middle of the specified block. |
| 144 | /// |
| 145 | /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one |
| 146 | /// important case: if there is a definition of the rewritten value after the |
| 147 | /// 'use' in BB. Consider code like this: |
| 148 | /// |
| 149 | /// X1 = ... |
| 150 | /// SomeBB: |
| 151 | /// use(X) |
| 152 | /// X2 = ... |
| 153 | /// br Cond, SomeBB, OutBB |
| 154 | /// |
| 155 | /// In this case, there are two values (X1 and X2) added to the AvailableVals |
| 156 | /// set by the client of the rewriter, and those values are both live out of |
| 157 | /// their respective blocks. However, the use of X happens in the *middle* of |
| 158 | /// a block. Because of this, we need to insert a new PHI node in SomeBB to |
| 159 | /// merge the appropriate values, and this value isn't live out of the block. |
| 160 | /// |
| 161 | unsigned MachineSSAUpdater::GetValueInMiddleOfBlock(MachineBasicBlock *BB) { |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 162 | // If there is no definition of the renamed variable in this block, just use |
| 163 | // GetValueAtEndOfBlock to do our work. |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 164 | if (!HasValueForBlock(BB)) |
Evan Cheng | 3a41ddb | 2009-12-07 23:11:03 +0000 | [diff] [blame] | 165 | return GetValueAtEndOfBlockInternal(BB); |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 166 | |
Evan Cheng | 01306ca | 2009-12-03 21:51:55 +0000 | [diff] [blame] | 167 | // If there are no predecessors, just return undef. |
Evan Cheng | 9d0f8bb | 2009-12-04 09:23:37 +0000 | [diff] [blame] | 168 | if (BB->pred_empty()) { |
| 169 | // Insert an implicit_def to represent an undef value. |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 170 | MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF, |
Evan Cheng | 9d0f8bb | 2009-12-04 09:23:37 +0000 | [diff] [blame] | 171 | BB, BB->getFirstTerminator(), |
| 172 | VRC, MRI, TII); |
| 173 | return NewDef->getOperand(0).getReg(); |
| 174 | } |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 175 | |
| 176 | // Otherwise, we have the hard case. Get the live-in values for each |
| 177 | // predecessor. |
| 178 | SmallVector<std::pair<MachineBasicBlock*, unsigned>, 8> PredValues; |
| 179 | unsigned SingularValue = 0; |
| 180 | |
| 181 | bool isFirstPred = true; |
| 182 | for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(), |
| 183 | E = BB->pred_end(); PI != E; ++PI) { |
| 184 | MachineBasicBlock *PredBB = *PI; |
| 185 | unsigned PredVal = GetValueAtEndOfBlockInternal(PredBB); |
| 186 | PredValues.push_back(std::make_pair(PredBB, PredVal)); |
| 187 | |
| 188 | // Compute SingularValue. |
| 189 | if (isFirstPred) { |
| 190 | SingularValue = PredVal; |
| 191 | isFirstPred = false; |
| 192 | } else if (PredVal != SingularValue) |
| 193 | SingularValue = 0; |
| 194 | } |
| 195 | |
| 196 | // Otherwise, if all the merged values are the same, just use it. |
| 197 | if (SingularValue != 0) |
| 198 | return SingularValue; |
| 199 | |
Evan Cheng | 3a41ddb | 2009-12-07 23:11:03 +0000 | [diff] [blame] | 200 | // If an identical PHI is already in BB, just reuse it. |
| 201 | unsigned DupPHI = LookForIdenticalPHI(BB, PredValues); |
| 202 | if (DupPHI) |
| 203 | return DupPHI; |
| 204 | |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 205 | // Otherwise, we do need a PHI: insert one now. |
Evan Cheng | 7e572eb | 2009-12-07 03:07:01 +0000 | [diff] [blame] | 206 | MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->front(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 207 | MachineInstr *InsertedPHI = InsertNewDef(TargetOpcode::PHI, BB, |
Evan Cheng | 7e572eb | 2009-12-07 03:07:01 +0000 | [diff] [blame] | 208 | Loc, VRC, MRI, TII); |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 209 | |
| 210 | // Fill in all the predecessors of the PHI. |
| 211 | MachineInstrBuilder MIB(InsertedPHI); |
| 212 | for (unsigned i = 0, e = PredValues.size(); i != e; ++i) |
| 213 | MIB.addReg(PredValues[i].second).addMBB(PredValues[i].first); |
| 214 | |
| 215 | // See if the PHI node can be merged to a single value. This can happen in |
| 216 | // loop cases when we get a PHI of itself and one other value. |
| 217 | if (unsigned ConstVal = InsertedPHI->isConstantValuePHI()) { |
| 218 | InsertedPHI->eraseFromParent(); |
| 219 | return ConstVal; |
| 220 | } |
| 221 | |
| 222 | // If the client wants to know about all new instructions, tell it. |
| 223 | if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI); |
| 224 | |
David Greene | 6bb93ef | 2010-01-05 00:10:05 +0000 | [diff] [blame] | 225 | DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n"); |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 226 | return InsertedPHI->getOperand(0).getReg(); |
| 227 | } |
| 228 | |
| 229 | static |
| 230 | MachineBasicBlock *findCorrespondingPred(const MachineInstr *MI, |
| 231 | MachineOperand *U) { |
| 232 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) { |
| 233 | if (&MI->getOperand(i) == U) |
| 234 | return MI->getOperand(i+1).getMBB(); |
| 235 | } |
| 236 | |
| 237 | llvm_unreachable("MachineOperand::getParent() failure?"); |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | /// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes, |
| 242 | /// which use their value in the corresponding predecessor. |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 243 | void MachineSSAUpdater::RewriteUse(MachineOperand &U) { |
| 244 | MachineInstr *UseMI = U.getParent(); |
| 245 | unsigned NewVR = 0; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 246 | if (UseMI->isPHI()) { |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 247 | MachineBasicBlock *SourceBB = findCorrespondingPred(UseMI, &U); |
Evan Cheng | 3a41ddb | 2009-12-07 23:11:03 +0000 | [diff] [blame] | 248 | NewVR = GetValueAtEndOfBlockInternal(SourceBB); |
Evan Cheng | 2e65c29 | 2009-12-04 00:09:05 +0000 | [diff] [blame] | 249 | } else { |
| 250 | NewVR = GetValueInMiddleOfBlock(UseMI->getParent()); |
Evan Cheng | 01306ca | 2009-12-03 21:51:55 +0000 | [diff] [blame] | 251 | } |
Evan Cheng | 2e65c29 | 2009-12-04 00:09:05 +0000 | [diff] [blame] | 252 | |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 253 | U.setReg(NewVR); |
| 254 | } |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 255 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 256 | void MachineSSAUpdater::ReplaceRegWith(unsigned OldReg, unsigned NewReg) { |
| 257 | MRI->replaceRegWith(OldReg, NewReg); |
| 258 | |
| 259 | AvailableValsTy &AvailableVals = getAvailableVals(AV); |
| 260 | for (DenseMap<MachineBasicBlock*, unsigned>::iterator |
| 261 | I = AvailableVals.begin(), E = AvailableVals.end(); I != E; ++I) |
| 262 | if (I->second == OldReg) |
| 263 | I->second = NewReg; |
| 264 | } |
| 265 | |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 266 | /// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry |
| 267 | /// for the specified BB and if so, return it. If not, construct SSA form by |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 268 | /// first calculating the required placement of PHIs and then inserting new |
| 269 | /// PHIs where needed. |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 270 | unsigned MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){ |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 271 | AvailableValsTy &AvailableVals = getAvailableVals(AV); |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 272 | if (unsigned V = AvailableVals[BB]) |
| 273 | return V; |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 274 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 275 | // Pool allocation used internally by GetValueAtEndOfBlock. |
| 276 | BumpPtrAllocator Allocator; |
| 277 | BBMapTy BBMapObj; |
| 278 | BM = &BBMapObj; |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 279 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 280 | SmallVector<BBInfo*, 100> BlockList; |
| 281 | BuildBlockList(BB, &BlockList, &Allocator); |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 282 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 283 | // Special case: bail out if BB is unreachable. |
| 284 | if (BlockList.size() == 0) { |
| 285 | BM = 0; |
Evan Cheng | 9d0f8bb | 2009-12-04 09:23:37 +0000 | [diff] [blame] | 286 | // Insert an implicit_def to represent an undef value. |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 287 | MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF, |
Evan Cheng | 9d0f8bb | 2009-12-04 09:23:37 +0000 | [diff] [blame] | 288 | BB, BB->getFirstTerminator(), |
| 289 | VRC, MRI, TII); |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 290 | unsigned V = NewDef->getOperand(0).getReg(); |
| 291 | AvailableVals[BB] = V; |
| 292 | return V; |
Evan Cheng | 9d0f8bb | 2009-12-04 09:23:37 +0000 | [diff] [blame] | 293 | } |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 294 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 295 | FindDominators(&BlockList); |
| 296 | FindPHIPlacement(&BlockList); |
| 297 | FindAvailableVals(&BlockList); |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 298 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 299 | BM = 0; |
| 300 | return BBMapObj[BB]->DefBB->AvailableVal; |
| 301 | } |
| 302 | |
| 303 | /// FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds |
| 304 | /// vector, set Info->NumPreds, and allocate space in Info->Preds. |
| 305 | static void FindPredecessorBlocks(MachineSSAUpdater::BBInfo *Info, |
| 306 | SmallVectorImpl<MachineBasicBlock*> *Preds, |
| 307 | BumpPtrAllocator *Allocator) { |
| 308 | MachineBasicBlock *BB = Info->BB; |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 309 | for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(), |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 310 | E = BB->pred_end(); PI != E; ++PI) |
| 311 | Preds->push_back(*PI); |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 312 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 313 | Info->NumPreds = Preds->size(); |
| 314 | Info->Preds = static_cast<MachineSSAUpdater::BBInfo**> |
| 315 | (Allocator->Allocate(Info->NumPreds * sizeof(MachineSSAUpdater::BBInfo*), |
| 316 | AlignOf<MachineSSAUpdater::BBInfo*>::Alignment)); |
| 317 | } |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 318 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 319 | /// BuildBlockList - Starting from the specified basic block, traverse back |
| 320 | /// through its predecessors until reaching blocks with known values. Create |
| 321 | /// BBInfo structures for the blocks and append them to the block list. |
| 322 | void MachineSSAUpdater::BuildBlockList(MachineBasicBlock *BB, |
| 323 | BlockListTy *BlockList, |
| 324 | BumpPtrAllocator *Allocator) { |
| 325 | AvailableValsTy &AvailableVals = getAvailableVals(AV); |
| 326 | BBMapTy *BBMap = getBBMap(BM); |
| 327 | SmallVector<BBInfo*, 10> RootList; |
| 328 | SmallVector<BBInfo*, 64> WorkList; |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 329 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 330 | BBInfo *Info = new (*Allocator) BBInfo(BB, 0); |
| 331 | (*BBMap)[BB] = Info; |
| 332 | WorkList.push_back(Info); |
| 333 | |
| 334 | // Search backward from BB, creating BBInfos along the way and stopping when |
| 335 | // reaching blocks that define the value. Record those defining blocks on |
| 336 | // the RootList. |
| 337 | SmallVector<MachineBasicBlock*, 10> Preds; |
| 338 | while (!WorkList.empty()) { |
| 339 | Info = WorkList.pop_back_val(); |
| 340 | Preds.clear(); |
| 341 | FindPredecessorBlocks(Info, &Preds, Allocator); |
| 342 | |
| 343 | // Treat an unreachable predecessor as a definition with 'undef'. |
| 344 | if (Info->NumPreds == 0) { |
| 345 | // Insert an implicit_def to represent an undef value. |
| 346 | MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF, |
| 347 | Info->BB, |
| 348 | Info->BB->getFirstTerminator(), |
| 349 | VRC, MRI, TII); |
| 350 | Info->AvailableVal = NewDef->getOperand(0).getReg(); |
| 351 | Info->DefBB = Info; |
| 352 | RootList.push_back(Info); |
| 353 | continue; |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 356 | for (unsigned p = 0; p != Info->NumPreds; ++p) { |
| 357 | MachineBasicBlock *Pred = Preds[p]; |
| 358 | // Check if BBMap already has a BBInfo for the predecessor block. |
| 359 | BBMapTy::value_type &BBMapBucket = BBMap->FindAndConstruct(Pred); |
| 360 | if (BBMapBucket.second) { |
| 361 | Info->Preds[p] = BBMapBucket.second; |
| 362 | continue; |
| 363 | } |
Evan Cheng | 2e65c29 | 2009-12-04 00:09:05 +0000 | [diff] [blame] | 364 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 365 | // Create a new BBInfo for the predecessor. |
| 366 | unsigned PredVal = AvailableVals.lookup(Pred); |
| 367 | BBInfo *PredInfo = new (*Allocator) BBInfo(Pred, PredVal); |
| 368 | BBMapBucket.second = PredInfo; |
| 369 | Info->Preds[p] = PredInfo; |
| 370 | |
| 371 | if (PredInfo->AvailableVal) { |
| 372 | RootList.push_back(PredInfo); |
| 373 | continue; |
| 374 | } |
| 375 | WorkList.push_back(PredInfo); |
| 376 | } |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 379 | // Now that we know what blocks are backwards-reachable from the starting |
| 380 | // block, do a forward depth-first traversal to assign postorder numbers |
| 381 | // to those blocks. |
| 382 | BBInfo *PseudoEntry = new (*Allocator) BBInfo(0, 0); |
| 383 | unsigned BlkNum = 1; |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 384 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 385 | // Initialize the worklist with the roots from the backward traversal. |
| 386 | while (!RootList.empty()) { |
| 387 | Info = RootList.pop_back_val(); |
| 388 | Info->IDom = PseudoEntry; |
| 389 | Info->BlkNum = -1; |
| 390 | WorkList.push_back(Info); |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 393 | while (!WorkList.empty()) { |
| 394 | Info = WorkList.back(); |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 395 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 396 | if (Info->BlkNum == -2) { |
| 397 | // All the successors have been handled; assign the postorder number. |
| 398 | Info->BlkNum = BlkNum++; |
| 399 | // If not a root, put it on the BlockList. |
| 400 | if (!Info->AvailableVal) |
| 401 | BlockList->push_back(Info); |
| 402 | WorkList.pop_back(); |
| 403 | continue; |
| 404 | } |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 405 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 406 | // Leave this entry on the worklist, but set its BlkNum to mark that its |
| 407 | // successors have been put on the worklist. When it returns to the top |
| 408 | // the list, after handling its successors, it will be assigned a number. |
| 409 | Info->BlkNum = -2; |
| 410 | |
| 411 | // Add unvisited successors to the work list. |
| 412 | for (MachineBasicBlock::succ_iterator SI = Info->BB->succ_begin(), |
| 413 | E = Info->BB->succ_end(); SI != E; ++SI) { |
| 414 | BBInfo *SuccInfo = (*BBMap)[*SI]; |
| 415 | if (!SuccInfo || SuccInfo->BlkNum) |
| 416 | continue; |
| 417 | SuccInfo->BlkNum = -1; |
| 418 | WorkList.push_back(SuccInfo); |
| 419 | } |
| 420 | } |
| 421 | PseudoEntry->BlkNum = BlkNum; |
| 422 | } |
| 423 | |
| 424 | /// IntersectDominators - This is the dataflow lattice "meet" operation for |
| 425 | /// finding dominators. Given two basic blocks, it walks up the dominator |
| 426 | /// tree until it finds a common dominator of both. It uses the postorder |
| 427 | /// number of the blocks to determine how to do that. |
| 428 | static MachineSSAUpdater::BBInfo * |
| 429 | IntersectDominators(MachineSSAUpdater::BBInfo *Blk1, |
| 430 | MachineSSAUpdater::BBInfo *Blk2) { |
| 431 | while (Blk1 != Blk2) { |
| 432 | while (Blk1->BlkNum < Blk2->BlkNum) { |
| 433 | Blk1 = Blk1->IDom; |
| 434 | if (!Blk1) |
| 435 | return Blk2; |
| 436 | } |
| 437 | while (Blk2->BlkNum < Blk1->BlkNum) { |
| 438 | Blk2 = Blk2->IDom; |
| 439 | if (!Blk2) |
| 440 | return Blk1; |
| 441 | } |
| 442 | } |
| 443 | return Blk1; |
| 444 | } |
| 445 | |
| 446 | /// FindDominators - Calculate the dominator tree for the subset of the CFG |
| 447 | /// corresponding to the basic blocks on the BlockList. This uses the |
| 448 | /// algorithm from: "A Simple, Fast Dominance Algorithm" by Cooper, Harvey and |
| 449 | /// Kennedy, published in Software--Practice and Experience, 2001, 4:1-10. |
| 450 | /// Because the CFG subset does not include any edges leading into blocks that |
| 451 | /// define the value, the results are not the usual dominator tree. The CFG |
| 452 | /// subset has a single pseudo-entry node with edges to a set of root nodes |
| 453 | /// for blocks that define the value. The dominators for this subset CFG are |
| 454 | /// not the standard dominators but they are adequate for placing PHIs within |
| 455 | /// the subset CFG. |
| 456 | void MachineSSAUpdater::FindDominators(BlockListTy *BlockList) { |
| 457 | bool Changed; |
| 458 | do { |
| 459 | Changed = false; |
| 460 | // Iterate over the list in reverse order, i.e., forward on CFG edges. |
| 461 | for (BlockListTy::reverse_iterator I = BlockList->rbegin(), |
| 462 | E = BlockList->rend(); I != E; ++I) { |
| 463 | BBInfo *Info = *I; |
| 464 | |
| 465 | // Start with the first predecessor. |
| 466 | assert(Info->NumPreds > 0 && "unreachable block"); |
| 467 | BBInfo *NewIDom = Info->Preds[0]; |
| 468 | |
| 469 | // Iterate through the block's other predecessors. |
| 470 | for (unsigned p = 1; p != Info->NumPreds; ++p) { |
| 471 | BBInfo *Pred = Info->Preds[p]; |
| 472 | NewIDom = IntersectDominators(NewIDom, Pred); |
| 473 | } |
| 474 | |
| 475 | // Check if the IDom value has changed. |
| 476 | if (NewIDom != Info->IDom) { |
| 477 | Info->IDom = NewIDom; |
| 478 | Changed = true; |
| 479 | } |
| 480 | } |
| 481 | } while (Changed); |
| 482 | } |
| 483 | |
| 484 | /// IsDefInDomFrontier - Search up the dominator tree from Pred to IDom for |
| 485 | /// any blocks containing definitions of the value. If one is found, then the |
| 486 | /// successor of Pred is in the dominance frontier for the definition, and |
| 487 | /// this function returns true. |
| 488 | static bool IsDefInDomFrontier(const MachineSSAUpdater::BBInfo *Pred, |
| 489 | const MachineSSAUpdater::BBInfo *IDom) { |
| 490 | for (; Pred != IDom; Pred = Pred->IDom) { |
| 491 | if (Pred->DefBB == Pred) |
| 492 | return true; |
| 493 | } |
| 494 | return false; |
| 495 | } |
| 496 | |
| 497 | /// FindPHIPlacement - PHIs are needed in the iterated dominance frontiers of |
| 498 | /// the known definitions. Iteratively add PHIs in the dom frontiers until |
| 499 | /// nothing changes. Along the way, keep track of the nearest dominating |
| 500 | /// definitions for non-PHI blocks. |
| 501 | void MachineSSAUpdater::FindPHIPlacement(BlockListTy *BlockList) { |
| 502 | bool Changed; |
| 503 | do { |
| 504 | Changed = false; |
| 505 | // Iterate over the list in reverse order, i.e., forward on CFG edges. |
| 506 | for (BlockListTy::reverse_iterator I = BlockList->rbegin(), |
| 507 | E = BlockList->rend(); I != E; ++I) { |
| 508 | BBInfo *Info = *I; |
| 509 | |
| 510 | // If this block already needs a PHI, there is nothing to do here. |
| 511 | if (Info->DefBB == Info) |
| 512 | continue; |
| 513 | |
| 514 | // Default to use the same def as the immediate dominator. |
| 515 | BBInfo *NewDefBB = Info->IDom->DefBB; |
| 516 | for (unsigned p = 0; p != Info->NumPreds; ++p) { |
| 517 | if (IsDefInDomFrontier(Info->Preds[p], Info->IDom)) { |
| 518 | // Need a PHI here. |
| 519 | NewDefBB = Info; |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Check if anything changed. |
| 525 | if (NewDefBB != Info->DefBB) { |
| 526 | Info->DefBB = NewDefBB; |
| 527 | Changed = true; |
| 528 | } |
| 529 | } |
| 530 | } while (Changed); |
| 531 | } |
| 532 | |
| 533 | /// FindAvailableVal - If this block requires a PHI, first check if an existing |
| 534 | /// PHI matches the PHI placement and reaching definitions computed earlier, |
| 535 | /// and if not, create a new PHI. Visit all the block's predecessors to |
| 536 | /// calculate the available value for each one and fill in the incoming values |
| 537 | /// for a new PHI. |
| 538 | void MachineSSAUpdater::FindAvailableVals(BlockListTy *BlockList) { |
| 539 | AvailableValsTy &AvailableVals = getAvailableVals(AV); |
| 540 | |
| 541 | // Go through the worklist in forward order (i.e., backward through the CFG) |
| 542 | // and check if existing PHIs can be used. If not, create empty PHIs where |
| 543 | // they are needed. |
| 544 | for (BlockListTy::iterator I = BlockList->begin(), E = BlockList->end(); |
| 545 | I != E; ++I) { |
| 546 | BBInfo *Info = *I; |
| 547 | // Check if there needs to be a PHI in BB. |
| 548 | if (Info->DefBB != Info) |
| 549 | continue; |
| 550 | |
| 551 | // Look for an existing PHI. |
| 552 | FindExistingPHI(Info->BB, BlockList); |
| 553 | if (Info->AvailableVal) |
| 554 | continue; |
| 555 | |
| 556 | MachineBasicBlock::iterator Loc = |
| 557 | Info->BB->empty() ? Info->BB->end() : Info->BB->front(); |
| 558 | MachineInstr *InsertedPHI = InsertNewDef(TargetOpcode::PHI, Info->BB, Loc, |
| 559 | VRC, MRI, TII); |
| 560 | unsigned PHI = InsertedPHI->getOperand(0).getReg(); |
| 561 | Info->AvailableVal = PHI; |
| 562 | AvailableVals[Info->BB] = PHI; |
| 563 | } |
| 564 | |
| 565 | // Now go back through the worklist in reverse order to fill in the arguments |
| 566 | // for any new PHIs added in the forward traversal. |
| 567 | for (BlockListTy::reverse_iterator I = BlockList->rbegin(), |
| 568 | E = BlockList->rend(); I != E; ++I) { |
| 569 | BBInfo *Info = *I; |
| 570 | |
| 571 | if (Info->DefBB != Info) { |
| 572 | // Record the available value at join nodes to speed up subsequent |
| 573 | // uses of this SSAUpdater for the same value. |
| 574 | if (Info->NumPreds > 1) |
| 575 | AvailableVals[Info->BB] = Info->DefBB->AvailableVal; |
| 576 | continue; |
| 577 | } |
| 578 | |
| 579 | // Check if this block contains a newly added PHI. |
| 580 | unsigned PHI = Info->AvailableVal; |
| 581 | MachineInstr *InsertedPHI = MRI->getVRegDef(PHI); |
| 582 | if (!InsertedPHI->isPHI() || InsertedPHI->getNumOperands() > 1) |
| 583 | continue; |
| 584 | |
| 585 | // Iterate through the block's predecessors. |
| 586 | MachineInstrBuilder MIB(InsertedPHI); |
| 587 | for (unsigned p = 0; p != Info->NumPreds; ++p) { |
| 588 | BBInfo *PredInfo = Info->Preds[p]; |
| 589 | MachineBasicBlock *Pred = PredInfo->BB; |
| 590 | // Skip to the nearest preceding definition. |
| 591 | if (PredInfo->DefBB != PredInfo) |
| 592 | PredInfo = PredInfo->DefBB; |
| 593 | MIB.addReg(PredInfo->AvailableVal).addMBB(Pred); |
| 594 | } |
| 595 | |
David Greene | 6bb93ef | 2010-01-05 00:10:05 +0000 | [diff] [blame] | 596 | DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n"); |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 597 | |
| 598 | // If the client wants to know about all new instructions, tell it. |
| 599 | if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI); |
| 600 | } |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 601 | } |
Evan Cheng | 229694f | 2009-12-03 02:31:43 +0000 | [diff] [blame] | 602 | |
Bob Wilson | 211678a | 2010-04-26 17:40:49 +0000 | [diff] [blame^] | 603 | /// FindExistingPHI - Look through the PHI nodes in a block to see if any of |
| 604 | /// them match what is needed. |
| 605 | void MachineSSAUpdater::FindExistingPHI(MachineBasicBlock *BB, |
| 606 | BlockListTy *BlockList) { |
| 607 | for (MachineBasicBlock::iterator BBI = BB->begin(), BBE = BB->end(); |
| 608 | BBI != BBE && BBI->isPHI(); ++BBI) { |
| 609 | if (CheckIfPHIMatches(BBI)) { |
| 610 | RecordMatchingPHI(BBI); |
| 611 | break; |
| 612 | } |
| 613 | // Match failed: clear all the PHITag values. |
| 614 | for (BlockListTy::iterator I = BlockList->begin(), E = BlockList->end(); |
| 615 | I != E; ++I) |
| 616 | (*I)->PHITag = 0; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /// CheckIfPHIMatches - Check if a PHI node matches the placement and values |
| 621 | /// in the BBMap. |
| 622 | bool MachineSSAUpdater::CheckIfPHIMatches(MachineInstr *PHI) { |
| 623 | BBMapTy *BBMap = getBBMap(BM); |
| 624 | SmallVector<MachineInstr*, 20> WorkList; |
| 625 | WorkList.push_back(PHI); |
| 626 | |
| 627 | // Mark that the block containing this PHI has been visited. |
| 628 | (*BBMap)[PHI->getParent()]->PHITag = PHI; |
| 629 | |
| 630 | while (!WorkList.empty()) { |
| 631 | PHI = WorkList.pop_back_val(); |
| 632 | |
| 633 | // Iterate through the PHI's incoming values. |
| 634 | for (unsigned i = 1, e = PHI->getNumOperands(); i != e; i += 2) { |
| 635 | unsigned IncomingVal = PHI->getOperand(i).getReg(); |
| 636 | BBInfo *PredInfo = (*BBMap)[PHI->getOperand(i+1).getMBB()]; |
| 637 | // Skip to the nearest preceding definition. |
| 638 | if (PredInfo->DefBB != PredInfo) |
| 639 | PredInfo = PredInfo->DefBB; |
| 640 | |
| 641 | // Check if it matches the expected value. |
| 642 | if (PredInfo->AvailableVal) { |
| 643 | if (IncomingVal == PredInfo->AvailableVal) |
| 644 | continue; |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | // Check if the value is a PHI in the correct block. |
| 649 | MachineInstr *IncomingPHIVal = MRI->getVRegDef(IncomingVal); |
| 650 | if (!IncomingPHIVal->isPHI() || |
| 651 | IncomingPHIVal->getParent() != PredInfo->BB) |
| 652 | return false; |
| 653 | |
| 654 | // If this block has already been visited, check if this PHI matches. |
| 655 | if (PredInfo->PHITag) { |
| 656 | if (IncomingPHIVal == PredInfo->PHITag) |
| 657 | continue; |
| 658 | return false; |
| 659 | } |
| 660 | PredInfo->PHITag = IncomingPHIVal; |
| 661 | |
| 662 | WorkList.push_back(IncomingPHIVal); |
| 663 | } |
| 664 | } |
| 665 | return true; |
| 666 | } |
| 667 | |
| 668 | /// RecordMatchingPHI - For a PHI node that matches, record it and its input |
| 669 | /// PHIs in both the BBMap and the AvailableVals mapping. |
| 670 | void MachineSSAUpdater::RecordMatchingPHI(MachineInstr *PHI) { |
| 671 | BBMapTy *BBMap = getBBMap(BM); |
| 672 | AvailableValsTy &AvailableVals = getAvailableVals(AV); |
| 673 | SmallVector<MachineInstr*, 20> WorkList; |
| 674 | WorkList.push_back(PHI); |
| 675 | |
| 676 | // Record this PHI. |
| 677 | MachineBasicBlock *BB = PHI->getParent(); |
| 678 | AvailableVals[BB] = PHI->getOperand(0).getReg(); |
| 679 | (*BBMap)[BB]->AvailableVal = PHI->getOperand(0).getReg(); |
| 680 | |
| 681 | while (!WorkList.empty()) { |
| 682 | PHI = WorkList.pop_back_val(); |
| 683 | |
| 684 | // Iterate through the PHI's incoming values. |
| 685 | for (unsigned i = 1, e = PHI->getNumOperands(); i != e; i += 2) { |
| 686 | unsigned IncomingVal = PHI->getOperand(i).getReg(); |
| 687 | MachineInstr *IncomingPHIVal = MRI->getVRegDef(IncomingVal); |
| 688 | if (!IncomingPHIVal->isPHI()) continue; |
| 689 | BB = IncomingPHIVal->getParent(); |
| 690 | BBInfo *Info = (*BBMap)[BB]; |
| 691 | if (!Info || Info->AvailableVal) |
| 692 | continue; |
| 693 | |
| 694 | // Record the PHI and add it to the worklist. |
| 695 | AvailableVals[BB] = IncomingVal; |
| 696 | Info->AvailableVal = IncomingVal; |
| 697 | WorkList.push_back(IncomingPHIVal); |
| 698 | } |
| 699 | } |
Evan Cheng | 651ea53 | 2009-12-02 22:02:52 +0000 | [diff] [blame] | 700 | } |