llvm.org GIT mirror llvm / a88a0ca
Revert r133435 and r133449 to appease buildbots. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@133499 91177308-0d34-0410-b5e6-96231b3b80d8 Chad Rosier 9 years ago
18 changed file(s) with 156 addition(s) and 211 deletion(s). Raw diff Collapse all Expand all
109109 Function *getParent() { return Parent; }
110110
111111 /// use_back - Specialize the methods defined in Value, as we know that an
112 /// BasicBlock can only be used by Users (specifically terminators
112 /// BasicBlock can only be used by Users (specifically PHI nodes, terminators,
113113 /// and BlockAddress's).
114114 User *use_back() { return cast(*use_begin());}
115115 const User *use_back() const { return cast(*use_begin());}
247247 /// other than direct branches, switches, etc. to it.
248248 bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; }
249249
250 /// replaceSuccessorsPhiUsesWith - Update all phi nodes in all our successors
251 /// to refer to basic block New instead of to us.
252 void replaceSuccessorsPhiUsesWith(BasicBlock *New);
253
254250 private:
255251 /// AdjustBlockAddressRefCount - BasicBlock stores the number of BlockAddress
256252 /// objects using it. This is almost always 0, sometimes one, possibly but
18131813 explicit PHINode(const Type *Ty, unsigned NumReservedValues,
18141814 const Twine &NameStr = "", Instruction *InsertBefore = 0)
18151815 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1816 ReservedSpace(NumReservedValues) {
1816 ReservedSpace(NumReservedValues * 2) {
18171817 setName(NameStr);
18181818 OperandList = allocHungoffUses(ReservedSpace);
18191819 }
18211821 PHINode(const Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
18221822 BasicBlock *InsertAtEnd)
18231823 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1824 ReservedSpace(NumReservedValues) {
1824 ReservedSpace(NumReservedValues * 2) {
18251825 setName(NameStr);
18261826 OperandList = allocHungoffUses(ReservedSpace);
18271827 }
18281828 protected:
1829 // allocHungoffUses - this is more complicated than the generic
1830 // User::allocHungoffUses, because we have to allocate Uses for the incoming
1831 // values and pointers to the incoming blocks, all in one allocation.
1832 Use *allocHungoffUses(unsigned) const;
1833
18341829 virtual PHINode *clone_impl() const;
18351830 public:
18361831 /// Constructors - NumReservedValues is a hint for the number of incoming
18491844 /// Provide fast operand accessors
18501845 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
18511846
1852 // Block iterator interface. This provides access to the list of incoming
1853 // basic blocks, which parallels the list of incoming values.
1854
1855 typedef BasicBlock **block_iterator;
1856 typedef BasicBlock * const *const_block_iterator;
1857
1858 block_iterator block_begin() {
1859 Use::UserRef *ref =
1860 reinterpret_cast(op_begin() + ReservedSpace);
1861 return reinterpret_cast(ref + 1);
1862 }
1863
1864 const_block_iterator block_begin() const {
1865 const Use::UserRef *ref =
1866 reinterpret_cast(op_begin() + ReservedSpace);
1867 return reinterpret_cast(ref + 1);
1868 }
1869
1870 block_iterator block_end() {
1871 return block_begin() + getNumOperands();
1872 }
1873
1874 const_block_iterator block_end() const {
1875 return block_begin() + getNumOperands();
1876 }
1877
18781847 /// getNumIncomingValues - Return the number of incoming edges
18791848 ///
1880 unsigned getNumIncomingValues() const { return getNumOperands(); }
1849 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
18811850
18821851 /// getIncomingValue - Return incoming value number x
18831852 ///
18841853 Value *getIncomingValue(unsigned i) const {
1885 return getOperand(i);
1854 assert(i*2 < getNumOperands() && "Invalid value number!");
1855 return getOperand(i*2);
18861856 }
18871857 void setIncomingValue(unsigned i, Value *V) {
1888 setOperand(i, V);
1858 assert(i*2 < getNumOperands() && "Invalid value number!");
1859 setOperand(i*2, V);
18891860 }
18901861 static unsigned getOperandNumForIncomingValue(unsigned i) {
1891 return i;
1862 return i*2;
18921863 }
18931864 static unsigned getIncomingValueNumForOperand(unsigned i) {
1894 return i;
1865 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1866 return i/2;
18951867 }
18961868
18971869 /// getIncomingBlock - Return incoming basic block number @p i.
18981870 ///
18991871 BasicBlock *getIncomingBlock(unsigned i) const {
1900 return block_begin()[i];
1872 return cast(getOperand(i*2+1));
19011873 }
19021874
19031875 /// getIncomingBlock - Return incoming basic block corresponding
19051877 ///
19061878 BasicBlock *getIncomingBlock(const Use &U) const {
19071879 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1908 return getIncomingBlock(&U - op_begin());
1880 return cast((&U + 1)->get());
19091881 }
19101882
19111883 /// getIncomingBlock - Return incoming basic block corresponding
19161888 return getIncomingBlock(I.getUse());
19171889 }
19181890
1891
19191892 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1920 block_begin()[i] = BB;
1893 setOperand(i*2+1, (Value*)BB);
1894 }
1895 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1896 return i*2+1;
1897 }
1898 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1899 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1900 return i/2;
19211901 }
19221902
19231903 /// addIncoming - Add an incoming value to the end of the PHI list
19271907 assert(BB && "PHI node got a null basic block!");
19281908 assert(getType() == V->getType() &&
19291909 "All operands to PHI node must be the same type as the PHI node!");
1930 if (NumOperands == ReservedSpace)
1910 unsigned OpNo = NumOperands;
1911 if (OpNo+2 > ReservedSpace)
19311912 growOperands(); // Get more space!
19321913 // Initialize some new operands.
1933 ++NumOperands;
1934 setIncomingValue(NumOperands - 1, V);
1935 setIncomingBlock(NumOperands - 1, BB);
1914 NumOperands = OpNo+2;
1915 OperandList[OpNo] = V;
1916 OperandList[OpNo+1] = (Value*)BB;
19361917 }
19371918
19381919 /// removeIncomingValue - Remove an incoming value. This is useful if a
19551936 /// block in the value list for this PHI. Returns -1 if no instance.
19561937 ///
19571938 int getBasicBlockIndex(const BasicBlock *BB) const {
1958 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1959 if (block_begin()[i] == BB)
1960 return i;
1939 Use *OL = OperandList;
1940 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1941 if (OL[i+1].get() == (const Value*)BB) return i/2;
19611942 return -1;
19621943 }
19631944
19641945 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1965 int Idx = getBasicBlockIndex(BB);
1966 assert(Idx >= 0 && "Invalid basic block argument!");
1967 return getIncomingValue(Idx);
1946 return getIncomingValue(getBasicBlockIndex(BB));
19681947 }
19691948
19701949 /// hasConstantValue - If the specified PHI node always merges together the
3232 USE_iterator It;
3333
3434 inline void advancePastNonTerminators() {
35 // Loop to ignore non terminator uses (for example BlockAddresses).
35 // Loop to ignore non terminator uses (for example PHI nodes).
3636 while (!It.atEnd() && !isa(*It))
3737 ++It;
3838 }
111111 Use *getNext() const { return Next; }
112112
113113
114 /// initTags - initialize the waymarking tags on an array of Uses, so that
115 /// getUser() can find the User from any of those Uses.
116 static Use *initTags(Use *Start, Use *Stop);
117
118114 /// zap - This is used to destroy Use operands when the number of operands of
119115 /// a User changes.
120116 static void zap(Use *Start, const Use *Stop, bool del = false);
121117
122118 private:
123119 const Use* getImpliedUser() const;
120 static Use *initTags(Use *Start, Use *Stop);
124121
125122 Value *Val;
126123 Use *Next;
142139 }
143140
144141 friend class Value;
142 friend class User;
145143 };
146144
147145 // simplify_type - Allow clients to treat uses just like values when using
13551355 for (unsigned i = 0; i < phi->getNumIncomingValues(); ++i) {
13561356 Out << iName << "->addIncoming("
13571357 << opNames[PHINode::getOperandNumForIncomingValue(i)] << ", "
1358 << getOpName(phi->getIncomingBlock(i)) << ");";
1358 << opNames[PHINode::getOperandNumForIncomingBlock(i)] << ");";
13591359 nl(Out);
13601360 }
13611361 break;
10201020 while (PHINode *PN = dyn_cast(Succ->begin()))
10211021 ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM);
10221022
1023 // If Succ has any successors with PHI nodes, update them to have
1024 // entries coming from Pred instead of Succ.
1025 Succ->replaceAllUsesWith(Pred);
1026
10271023 // Move all of the successor contents from Succ to Pred.
10281024 Pred->getInstList().splice(BI, Succ->getInstList(), Succ->begin(),
10291025 Succ->end());
10301026 LPM->deleteSimpleAnalysisValue(BI, L);
10311027 BI->eraseFromParent();
10321028 RemoveFromWorklist(BI, Worklist);
1029
1030 // If Succ has any successors with PHI nodes, update them to have
1031 // entries coming from Pred instead of Succ.
1032 Succ->replaceAllUsesWith(Pred);
10331033
10341034 // Remove Succ from the loop tree.
10351035 LI->removeBlock(Succ);
152152 // Delete the unconditional branch from the predecessor...
153153 PredBB->getInstList().pop_back();
154154
155 // Move all definitions in the successor to the predecessor...
156 PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
157
155158 // Make all PHI nodes that referred to BB now refer to Pred as their
156159 // source...
157160 BB->replaceAllUsesWith(PredBB);
158
159 // Move all definitions in the successor to the predecessor...
160 PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
161161
162162 // Inherit predecessors name if it exists.
163163 if (!PredBB->hasName())
192192
193193 // If there are any PHI nodes in DestBB, we need to update them so that they
194194 // merge incoming values from NewBB instead of from TIBB.
195 {
196 unsigned BBIdx = 0;
197 for (BasicBlock::iterator I = DestBB->begin(); isa(I); ++I) {
198 // We no longer enter through TIBB, now we come in through NewBB.
199 // Revector exactly one entry in the PHI node that used to come from
200 // TIBB to come from NewBB.
201 PHINode *PN = cast(I);
202
203 // Reuse the previous value of BBIdx if it lines up. In cases where we
204 // have multiple phi nodes with *lots* of predecessors, this is a speed
205 // win because we don't have to scan the PHI looking for TIBB. This
206 // happens because the BB list of PHI nodes are usually in the same
207 // order.
208 if (PN->getIncomingBlock(BBIdx) != TIBB)
209 BBIdx = PN->getBasicBlockIndex(TIBB);
210 PN->setIncomingBlock(BBIdx, NewBB);
195 if (PHINode *APHI = dyn_cast(DestBB->begin())) {
196 // This conceptually does:
197 // foreach (PHINode *PN in DestBB)
198 // PN->setIncomingBlock(PN->getIncomingBlock(TIBB), NewBB);
199 // but is optimized for two cases.
200
201 if (APHI->getNumIncomingValues() <= 8) { // Small # preds case.
202 unsigned BBIdx = 0;
203 for (BasicBlock::iterator I = DestBB->begin(); isa(I); ++I) {
204 // We no longer enter through TIBB, now we come in through NewBB.
205 // Revector exactly one entry in the PHI node that used to come from
206 // TIBB to come from NewBB.
207 PHINode *PN = cast(I);
208
209 // Reuse the previous value of BBIdx if it lines up. In cases where we
210 // have multiple phi nodes with *lots* of predecessors, this is a speed
211 // win because we don't have to scan the PHI looking for TIBB. This
212 // happens because the BB list of PHI nodes are usually in the same
213 // order.
214 if (PN->getIncomingBlock(BBIdx) != TIBB)
215 BBIdx = PN->getBasicBlockIndex(TIBB);
216 PN->setIncomingBlock(BBIdx, NewBB);
217 }
218 } else {
219 // However, the foreach loop is slow for blocks with lots of predecessors
220 // because PHINode::getIncomingBlock is O(n) in # preds. Instead, walk
221 // the user list of TIBB to find the PHI nodes.
222 SmallPtrSet UpdatedPHIs;
223
224 for (Value::use_iterator UI = TIBB->use_begin(), E = TIBB->use_end();
225 UI != E; ) {
226 Value::use_iterator Use = UI++;
227 if (PHINode *PN = dyn_cast(*Use)) {
228 // Remove one entry from each PHI.
229 if (PN->getParent() == DestBB && UpdatedPHIs.insert(PN))
230 PN->setOperand(Use.getOperandNo(), NewBB);
231 }
232 }
211233 }
212234 }
213235
571571 // removed, so we just need to splice the blocks.
572572 BI->eraseFromParent();
573573
574 // Move all the instructions in the succ to the pred.
575 I->getInstList().splice(I->end(), Dest->getInstList());
576
574577 // Make all PHI nodes that referred to Dest now refer to I as their source.
575578 Dest->replaceAllUsesWith(I);
576579
577 // Move all the instructions in the succ to the pred.
578 I->getInstList().splice(I->end(), Dest->getInstList());
579
580580 // Remove the dest block.
581581 Dest->eraseFromParent();
582582
10961096 TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
10971097 }
10981098
1099 // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
1100 BasicBlock *ReturnBB = Returns[0]->getParent();
1101 ReturnBB->replaceAllUsesWith(AfterCallBB);
1102
11031099 // Splice the code from the return block into the block that it will return
11041100 // to, which contains the code that was after the call.
1101 BasicBlock *ReturnBB = Returns[0]->getParent();
11051102 AfterCallBB->getInstList().splice(AfterCallBB->begin(),
11061103 ReturnBB->getInstList());
1104
1105 // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
1106 ReturnBB->replaceAllUsesWith(AfterCallBB);
11071107
11081108 // Delete the return instruction now and empty ReturnBB now.
11091109 Returns[0]->eraseFromParent();
11241124
11251125 // Splice the code entry block into calling block, right before the
11261126 // unconditional branch.
1127 OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
11271128 CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes
1128 OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
11291129
11301130 // Remove the unconditional branch.
11311131 OrigBB->getInstList().erase(Br);
426426 BasicBlock *PredBB = DestBB->getSinglePredecessor();
427427 assert(PredBB && "Block doesn't have a single predecessor!");
428428
429 // Splice all the instructions from PredBB to DestBB.
430 PredBB->getTerminator()->eraseFromParent();
431 DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
432
429433 // Zap anything that took the address of DestBB. Not doing this will give the
430434 // address an invalid value.
431435 if (DestBB->hasAddressTaken()) {
440444 // Anything that branched to PredBB now branches to DestBB.
441445 PredBB->replaceAllUsesWith(DestBB);
442446
443 // Splice all the instructions from PredBB to DestBB.
444 PredBB->getTerminator()->eraseFromParent();
445 DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
446
447447 if (P) {
448448 DominatorTree *DT = P->getAnalysisIfAvailable();
449449 if (DT) {
659659 // them, which helps expose duplicates, but we have to check all the
660660 // operands to be safe in case instcombine hasn't run.
661661 uintptr_t Hash = 0;
662 // This hash algorithm is quite weak as hash functions go, but it seems
663 // to do a good enough job for this particular purpose, and is very quick.
664662 for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) {
663 // This hash algorithm is quite weak as hash functions go, but it seems
664 // to do a good enough job for this particular purpose, and is very quick.
665665 Hash ^= reinterpret_cast(static_cast(*I));
666 Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
667 }
668 for (PHINode::block_iterator I = PN->block_begin(), E = PN->block_end();
669 I != E; ++I) {
670 Hash ^= reinterpret_cast(static_cast(*I));
671666 Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
672667 }
673668 // Avoid colliding with the DenseMap sentinels ~0 and ~0-1.
4646 if (It != VMap.end())
4747 I->setOperand(op, It->second);
4848 }
49
50 if (PHINode *PN = dyn_cast(I)) {
51 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
52 ValueToValueMapTy::iterator It = VMap.find(PN->getIncomingBlock(i));
53 if (It != VMap.end())
54 PN->setIncomingBlock(i, cast(It->second));
55 }
56 }
5749 }
5850
5951 /// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it
8274 // Delete the unconditional branch from the predecessor...
8375 OnlyPred->getInstList().pop_back();
8476
77 // Move all definitions in the successor to the predecessor...
78 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
79
8580 // Make all PHI nodes that referred to BB now refer to Pred as their
8681 // source...
8782 BB->replaceAllUsesWith(OnlyPred);
88
89 // Move all definitions in the successor to the predecessor...
90 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
9183
9284 std::string OldName = BB->getName();
9385
254246 // the successor of the latch block. The successor of the exit block will
255247 // be updated specially after unrolling all the way.
256248 if (*BB != LatchBlock)
257 for (succ_iterator SI = succ_begin(*BB), SE = succ_end(*BB); SI != SE;
258 ++SI)
259 if (!L->contains(*SI))
260 for (BasicBlock::iterator BBI = (*SI)->begin(), BBE = (*SI)->end();
261 PHINode *phi = dyn_cast(BBI); ++BBI) {
262 Value *Incoming = phi->getIncomingValueForBlock(*BB);
263 phi->addIncoming(Incoming, New);
264 }
249 for (Value::use_iterator UI = (*BB)->use_begin(), UE = (*BB)->use_end();
250 UI != UE;) {
251 Instruction *UseInst = cast(*UI);
252 ++UI;
253 if (isa(UseInst) && !L->contains(UseInst)) {
254 PHINode *phi = cast(UseInst);
255 Value *Incoming = phi->getIncomingValueForBlock(*BB);
256 phi->addIncoming(Incoming, New);
257 }
258 }
265259
266260 // Keep track of new headers and latches as we create them, so that
267261 // we can insert the proper branches later.
293287 // successor blocks, update them to use the appropriate values computed as the
294288 // last iteration of the loop.
295289 if (Count != 1) {
290 SmallPtrSet Users;
291 for (Value::use_iterator UI = LatchBlock->use_begin(),
292 UE = LatchBlock->use_end(); UI != UE; ++UI)
293 if (PHINode *phi = dyn_cast(*UI))
294 Users.insert(phi);
295
296296 BasicBlock *LastIterationBB = cast(LastValueMap[LatchBlock]);
297 for (succ_iterator SI = succ_begin(LatchBlock), SE = succ_end(LatchBlock);
297 for (SmallPtrSet::iterator SI = Users.begin(), SE = Users.end();
298298 SI != SE; ++SI) {
299 for (BasicBlock::iterator BBI = (*SI)->begin(), BBE = (*SI)->end();
300 PHINode *PN = dyn_cast(BBI); ++BBI) {
301 Value *InVal = PN->removeIncomingValue(LatchBlock, false);
302 // If this value was defined in the loop, take the value defined by the
303 // last iteration of the loop.
304 if (Instruction *InValI = dyn_cast(InVal)) {
305 if (L->contains(InValI))
306 InVal = LastValueMap[InVal];
307 }
308 PN->addIncoming(InVal, LastIterationBB);
299 PHINode *PN = *SI;
300 Value *InVal = PN->removeIncomingValue(LatchBlock, false);
301 // If this value was defined in the loop, take the value defined by the
302 // last iteration of the loop.
303 if (Instruction *InValI = dyn_cast(InVal)) {
304 if (L->contains(InValI))
305 InVal = LastValueMap[InVal];
309306 }
307 PN->addIncoming(InVal, LastIterationBB);
310308 }
311309 }
312310
353351 // Replace the conditional branch with an unconditional one.
354352 BranchInst::Create(Dest, Term);
355353 Term->eraseFromParent();
356 }
357 }
358
359 // Merge adjacent basic blocks, if possible.
360 for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
361 BranchInst *Term = cast(Latches[i]->getTerminator());
362 if (Term->isUnconditional()) {
363 BasicBlock *Dest = Term->getSuccessor(0);
364 if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI))
354 // Merge adjacent basic blocks, if possible.
355 if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI)) {
365356 std::replace(Latches.begin(), Latches.end(), Dest, Fold);
357 std::replace(Headers.begin(), Headers.end(), Dest, Fold);
358 }
366359 }
367360 }
368361
1515 #include "llvm/Type.h"
1616 #include "llvm/Constants.h"
1717 #include "llvm/Function.h"
18 #include "llvm/Instructions.h"
1918 #include "llvm/Metadata.h"
2019 #include "llvm/ADT/SmallVector.h"
2120 using namespace llvm;
128127 "Referenced value not in value map!");
129128 }
130129
131 // Remap phi nodes' incoming blocks.
132 if (PHINode *PN = dyn_cast(I)) {
133 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
134 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
135 // If we aren't ignoring missing entries, assert that something happened.
136 if (V != 0)
137 PN->setIncomingBlock(i, cast(V));
138 else
139 assert((Flags & RF_IgnoreMissingEntries) &&
140 "Referenced block not in value map!");
141 }
142 }
143
144130 // Remap attached metadata.
145131 SmallVector, 4> MDs;
146132 I->getAllMetadata(MDs);
307307 return New;
308308 }
309309
310 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) {
311 TerminatorInst *TI = getTerminator();
312 if (!TI)
313 // Cope with being called on a BasicBlock that doesn't have a terminator
314 // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this.
315 return;
316 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
317 BasicBlock *Succ = TI->getSuccessor(i);
318 for (iterator II = Succ->begin(); PHINode *PN = dyn_cast(II);
319 ++II) {
320 int i;
321 while ((i = PN->getBasicBlockIndex(this)) >= 0)
322 PN->setIncomingBlock(i, New);
323 }
324 }
325 }
8686 : Instruction(PN.getType(), Instruction::PHI,
8787 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
8888 ReservedSpace(PN.getNumOperands()) {
89 std::copy(PN.op_begin(), PN.op_end(), op_begin());
90 std::copy(PN.block_begin(), PN.block_end(), block_begin());
89 Use *OL = OperandList;
90 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
91 OL[i] = PN.getOperand(i);
92 OL[i+1] = PN.getOperand(i+1);
93 }
9194 SubclassOptionalData = PN.SubclassOptionalData;
9295 }
9396
9497 PHINode::~PHINode() {
9598 dropHungoffUses();
96 }
97
98 Use *PHINode::allocHungoffUses(unsigned N) const {
99 // Allocate the array of Uses of the incoming values, followed by a pointer
100 // (with bottom bit set) to the User, followed by the array of pointers to
101 // the incoming basic blocks.
102 size_t size = N * sizeof(Use) + sizeof(Use::UserRef)
103 + N * sizeof(BasicBlock*);
104 Use *Begin = static_cast(::operator new(size));
105 Use *End = Begin + N;
106 (void) new(End) Use::UserRef(const_cast(this), 1);
107 return Use::initTags(Begin, End);
10899 }
109100
110101 // removeIncomingValue - Remove an incoming value. This is useful if a
111102 // predecessor basic block is deleted.
112103 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
113 Value *Removed = getIncomingValue(Idx);
104 unsigned NumOps = getNumOperands();
105 Use *OL = OperandList;
106 assert(Idx*2 < NumOps && "BB not in PHI node!");
107 Value *Removed = OL[Idx*2];
114108
115109 // Move everything after this operand down.
116110 //
117111 // FIXME: we could just swap with the end of the list, then erase. However,
118 // clients might not expect this to happen. The code as it is thrashes the
112 // client might not expect this to happen. The code as it is thrashes the
119113 // use/def lists, which is kinda lame.
120 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
121 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
114 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
115 OL[i-2] = OL[i];
116 OL[i-2+1] = OL[i+1];
117 }
122118
123119 // Nuke the last value.
124 Op<-1>().set(0);
125 --NumOperands;
120 OL[NumOps-2].set(0);
121 OL[NumOps-2+1].set(0);
122 NumOperands = NumOps-2;
126123
127124 // If the PHI node is dead, because it has zero entries, nuke it now.
128 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
125 if (NumOps == 2 && DeletePHIIfEmpty) {
129126 // If anyone is using this PHI, make them use a dummy value instead...
130127 replaceAllUsesWith(UndefValue::get(getType()));
131128 eraseFromParent();
139136 ///
140137 void PHINode::growOperands() {
141138 unsigned e = getNumOperands();
142 unsigned NumOps = e + e / 2;
143 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
144
145 Use *OldOps = op_begin();
146 BasicBlock **OldBlocks = block_begin();
139 // Multiply by 1.5 and round down so the result is still even.
140 unsigned NumOps = e + e / 4 * 2;
141 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
147142
148143 ReservedSpace = NumOps;
149 OperandList = allocHungoffUses(ReservedSpace);
150
151 std::copy(OldOps, OldOps + e, op_begin());
152 std::copy(OldBlocks, OldBlocks + e, block_begin());
153
144 Use *OldOps = OperandList;
145 Use *NewOps = allocHungoffUses(NumOps);
146 std::copy(OldOps, OldOps + e, NewOps);
147 OperandList = NewOps;
154148 Use::zap(OldOps, OldOps + e, true);
155149 }
156150
3939 //===----------------------------------------------------------------------===//
4040
4141 Use *User::allocHungoffUses(unsigned N) const {
42 // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
43 // the User.
44 size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
45 Use *Begin = static_cast(::operator new(size));
42 Use *Begin = static_cast(::operator new(sizeof(Use) * N
43 + sizeof(Use::UserRef)));
4644 Use *End = Begin + N;
4745 (void) new(End) Use::UserRef(const_cast(this), 1);
4846 return Use::initTags(Begin, End);
304304
305305 U.set(New);
306306 }
307
308 if (BasicBlock *BB = dyn_cast(this))
309 BB->replaceSuccessorsPhiUsesWith(cast(New));
310307 }
311308
312309 void Value::replaceAllUsesWith(Value *New) {
11381138 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
11391139 Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
11401140 "PHI node operands are not the same type as the result!", &PN);
1141 Assert1(isa(PN.getOperand(
1142 PHINode::getOperandNumForIncomingBlock(i))),
1143 "PHI node incoming block is not a BasicBlock!", &PN);
11411144 }
11421145
11431146 // All other PHI node constraints are checked in the visitBasicBlock method.