llvm.org GIT mirror llvm / 95c3e48
Reinstate r133513 (reverted in r133700) with an additional fix for a -Wshorten-64-to-32 warning in Instructions.h. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@133708 91177308-0d34-0410-b5e6-96231b3b80d8 Jay Foad 9 years ago
19 changed file(s) with 224 addition(s) and 169 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 PHI nodes, terminators,
112 /// BasicBlock can only be used by Users (specifically 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
250254 private:
251255 /// AdjustBlockAddressRefCount - BasicBlock stores the number of BlockAddress
252256 /// 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 * 2) {
1816 ReservedSpace(NumReservedValues) {
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 * 2) {
1824 ReservedSpace(NumReservedValues) {
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
18291834 virtual PHINode *clone_impl() const;
18301835 public:
18311836 /// Constructors - NumReservedValues is a hint for the number of incoming
18441849 /// Provide fast operand accessors
18451850 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
18461851
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
18471878 /// getNumIncomingValues - Return the number of incoming edges
18481879 ///
1849 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1880 unsigned getNumIncomingValues() const { return getNumOperands(); }
18501881
18511882 /// getIncomingValue - Return incoming value number x
18521883 ///
18531884 Value *getIncomingValue(unsigned i) const {
1854 assert(i*2 < getNumOperands() && "Invalid value number!");
1855 return getOperand(i*2);
1885 return getOperand(i);
18561886 }
18571887 void setIncomingValue(unsigned i, Value *V) {
1858 assert(i*2 < getNumOperands() && "Invalid value number!");
1859 setOperand(i*2, V);
1888 setOperand(i, V);
18601889 }
18611890 static unsigned getOperandNumForIncomingValue(unsigned i) {
1862 return i*2;
1891 return i;
18631892 }
18641893 static unsigned getIncomingValueNumForOperand(unsigned i) {
1865 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1866 return i/2;
1894 return i;
18671895 }
18681896
18691897 /// getIncomingBlock - Return incoming basic block number @p i.
18701898 ///
18711899 BasicBlock *getIncomingBlock(unsigned i) const {
1872 return cast(getOperand(i*2+1));
1900 return block_begin()[i];
18731901 }
18741902
18751903 /// getIncomingBlock - Return incoming basic block corresponding
18771905 ///
18781906 BasicBlock *getIncomingBlock(const Use &U) const {
18791907 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1880 return cast((&U + 1)->get());
1908 return getIncomingBlock(unsigned(&U - op_begin()));
18811909 }
18821910
18831911 /// getIncomingBlock - Return incoming basic block corresponding
18881916 return getIncomingBlock(I.getUse());
18891917 }
18901918
1891
18921919 void setIncomingBlock(unsigned i, BasicBlock *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;
1920 block_begin()[i] = BB;
19011921 }
19021922
19031923 /// addIncoming - Add an incoming value to the end of the PHI list
19071927 assert(BB && "PHI node got a null basic block!");
19081928 assert(getType() == V->getType() &&
19091929 "All operands to PHI node must be the same type as the PHI node!");
1910 unsigned OpNo = NumOperands;
1911 if (OpNo+2 > ReservedSpace)
1930 if (NumOperands == ReservedSpace)
19121931 growOperands(); // Get more space!
19131932 // Initialize some new operands.
1914 NumOperands = OpNo+2;
1915 OperandList[OpNo] = V;
1916 OperandList[OpNo+1] = (Value*)BB;
1933 ++NumOperands;
1934 setIncomingValue(NumOperands - 1, V);
1935 setIncomingBlock(NumOperands - 1, BB);
19171936 }
19181937
19191938 /// removeIncomingValue - Remove an incoming value. This is useful if a
19361955 /// block in the value list for this PHI. Returns -1 if no instance.
19371956 ///
19381957 int getBasicBlockIndex(const BasicBlock *BB) const {
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;
1958 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1959 if (block_begin()[i] == BB)
1960 return i;
19421961 return -1;
19431962 }
19441963
19451964 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1946 return getIncomingValue(getBasicBlockIndex(BB));
1965 int Idx = getBasicBlockIndex(BB);
1966 assert(Idx >= 0 && "Invalid basic block argument!");
1967 return getIncomingValue(Idx);
19471968 }
19481969
19491970 /// 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 PHI nodes).
35 // Loop to ignore non terminator uses (for example BlockAddresses).
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
114118 /// zap - This is used to destroy Use operands when the number of operands of
115119 /// a User changes.
116120 static void zap(Use *Start, const Use *Stop, bool del = false);
117121
118122 private:
119123 const Use* getImpliedUser() const;
120 static Use *initTags(Use *Start, Use *Stop);
121124
122125 Value *Val;
123126 Use *Next;
139142 }
140143
141144 friend class Value;
142 friend class User;
143145 };
144146
145147 // simplify_type - Allow clients to treat uses just like values when using
11181118 } else {
11191119 // Otherwise, write out in binary.
11201120 OS << "0b";
1121 for (int j = 7; j >= 0; j--) {
1121 for (unsigned j = 8; j--;) {
11221122 unsigned Bit = (Code[i] >> j) & 1;
11231123
11241124 unsigned FixupBit;
11271127 else
11281128 FixupBit = i * 8 + (7-j);
11291129
1130 if (uint8_t FixupMapEntry = FixupMap[FixupBit]) {
1131 //assert(Bit == 0 && "Encoder wrote into fixed up bit!");
1132 OS << char('A' + FixupMapEntry - 1);
1130 if (uint8_t MapEntry = FixupMap[FixupBit]) {
1131 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
1132 OS << char('A' + MapEntry - 1);
11331133 } else
11341134 OS << Bit;
11351135 }
13551355 for (unsigned i = 0; i < phi->getNumIncomingValues(); ++i) {
13561356 Out << iName << "->addIncoming("
13571357 << opNames[PHINode::getOperandNumForIncomingValue(i)] << ", "
1358 << opNames[PHINode::getOperandNumForIncomingBlock(i)] << ");";
1358 << getOpName(phi->getIncomingBlock(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
10231027 // Move all of the successor contents from Succ to Pred.
10241028 Pred->getInstList().splice(BI, Succ->getInstList(), Succ->begin(),
10251029 Succ->end());
10261030 LPM->deleteSimpleAnalysisValue(BI, L);
10271031 BI->eraseFromParent();
10281032 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
158155 // Make all PHI nodes that referred to BB now refer to Pred as their
159156 // source...
160157 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 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 }
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);
233211 }
234212 }
235213
571571 // removed, so we just need to splice the blocks.
572572 BI->eraseFromParent();
573573
574 // Make all PHI nodes that referred to Dest now refer to I as their source.
575 Dest->replaceAllUsesWith(I);
576
574577 // Move all the instructions in the succ to the pred.
575578 I->getInstList().splice(I->end(), Dest->getInstList());
576579
577 // Make all PHI nodes that referred to Dest now refer to I as their source.
578 Dest->replaceAllUsesWith(I);
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
10991103 // Splice the code from the return block into the block that it will return
11001104 // to, which contains the code that was after the call.
1101 BasicBlock *ReturnBB = Returns[0]->getParent();
11021105 AfterCallBB->getInstList().splice(AfterCallBB->begin(),
11031106 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 CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes
11271128 OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
1128 CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes
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
433429 // Zap anything that took the address of DestBB. Not doing this will give the
434430 // address an invalid value.
435431 if (DestBB->hasAddressTaken()) {
444440 // Anything that branched to PredBB now branches to DestBB.
445441 PredBB->replaceAllUsesWith(DestBB);
446442
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.
662664 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));
666671 Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
667672 }
668673 // 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 }
4957 }
5058
5159 /// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it
7482 // Delete the unconditional branch from the predecessor...
7583 OnlyPred->getInstList().pop_back();
7684
77 // Move all definitions in the successor to the predecessor...
78 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
79
8085 // Make all PHI nodes that referred to BB now refer to Pred as their
8186 // source...
8287 BB->replaceAllUsesWith(OnlyPred);
88
89 // Move all definitions in the successor to the predecessor...
90 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
8391
8492 std::string OldName = BB->getName();
8593
120128
121129 BasicBlock *Header = L->getHeader();
122130 BranchInst *BI = dyn_cast(LatchBlock->getTerminator());
123
131
124132 if (!BI || BI->isUnconditional()) {
125133 // The loop-rotate pass can be helpful to avoid this in many cases.
126134 DEBUG(dbgs() <<
127135 " Can't unroll; loop not terminated by a conditional branch.\n");
128136 return false;
129137 }
130
138
131139 if (Header->hasAddressTaken()) {
132140 // The loop-rotate pass can be helpful to avoid this in many cases.
133141 DEBUG(dbgs() <<
201209 for (BasicBlock::iterator I = Header->begin(); isa(I); ++I) {
202210 PHINode *PN = cast(I);
203211 OrigPHINode.push_back(PN);
204 if (Instruction *I =
212 if (Instruction *I =
205213 dyn_cast(PN->getIncomingValueForBlock(LatchBlock)))
206214 if (L->contains(I))
207215 LastValueMap[I] = I;
214222
215223 for (unsigned It = 1; It != Count; ++It) {
216224 std::vector NewBlocks;
217
225
218226 for (std::vector::iterator BB = LoopBlocks.begin(),
219227 E = LoopBlocks.end(); BB != E; ++BB) {
220228 ValueToValueMapTy VMap;
246254 // the successor of the latch block. The successor of the exit block will
247255 // be updated specially after unrolling all the way.
248256 if (*BB != LatchBlock)
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 }
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();
261 PHINode *phi = dyn_cast(BBI); ++BBI) {
262 Value *Incoming = phi->getIncomingValueForBlock(*BB);
263 phi->addIncoming(Incoming, New);
264 }
259265
260266 // Keep track of new headers and latches as we create them, so that
261267 // we can insert the proper branches later.
275281
276282 NewBlocks.push_back(New);
277283 }
278
284
279285 // Remap all instructions in the most recent iteration
280286 for (unsigned i = 0; i < NewBlocks.size(); ++i)
281287 for (BasicBlock::iterator I = NewBlocks[i]->begin(),
282288 E = NewBlocks[i]->end(); I != E; ++I)
283289 ::RemapInstruction(I, LastValueMap);
284290 }
285
291
286292 // The latch block exits the loop. If there are any PHI nodes in the
287293 // successor blocks, update them to use the appropriate values computed as the
288294 // last iteration of the loop.
289295 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 (SmallPtrSet::iterator SI = Users.begin(), SE = Users.end();
297 for (succ_iterator SI = succ_begin(LatchBlock), SE = succ_end(LatchBlock);
298298 SI != SE; ++SI) {
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];
299 for (BasicBlock::iterator BBI = (*SI)->begin();
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);
306309 }
307 PN->addIncoming(InVal, LastIterationBB);
308310 }
309311 }
310312
351353 // Replace the conditional branch with an unconditional one.
352354 BranchInst::Create(Dest, Term);
353355 Term->eraseFromParent();
354 // Merge adjacent basic blocks, if possible.
355 if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI)) {
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))
356365 std::replace(Latches.begin(), Latches.end(), Dest, Fold);
357 std::replace(Headers.begin(), Headers.end(), Dest, Fold);
358 }
359 }
360 }
361
366 }
367 }
368
362369 // At this point, the code is well formed. We now do a quick sweep over the
363370 // inserted code, doing constant propagation and dead code elimination as we
364371 // go.
1515 #include "llvm/Type.h"
1616 #include "llvm/Constants.h"
1717 #include "llvm/Function.h"
18 #include "llvm/Instructions.h"
1819 #include "llvm/Metadata.h"
1920 #include "llvm/ADT/SmallVector.h"
2021 using namespace llvm;
127128 "Referenced value not in value map!");
128129 }
129130
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
130144 // Remap attached metadata.
131145 SmallVector, 4> MDs;
132146 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 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 }
89 std::copy(PN.op_begin(), PN.op_end(), op_begin());
90 std::copy(PN.block_begin(), PN.block_end(), block_begin());
9491 SubclassOptionalData = PN.SubclassOptionalData;
9592 }
9693
9794 PHINode::~PHINode() {
9895 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);
99108 }
100109
101110 // removeIncomingValue - Remove an incoming value. This is useful if a
102111 // predecessor basic block is deleted.
103112 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
104 unsigned NumOps = getNumOperands();
105 Use *OL = OperandList;
106 assert(Idx*2 < NumOps && "BB not in PHI node!");
107 Value *Removed = OL[Idx*2];
113 Value *Removed = getIncomingValue(Idx);
108114
109115 // Move everything after this operand down.
110116 //
111117 // FIXME: we could just swap with the end of the list, then erase. However,
112 // client might not expect this to happen. The code as it is thrashes the
118 // clients might not expect this to happen. The code as it is thrashes the
113119 // use/def lists, which is kinda lame.
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 }
120 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
121 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
118122
119123 // Nuke the last value.
120 OL[NumOps-2].set(0);
121 OL[NumOps-2+1].set(0);
122 NumOperands = NumOps-2;
124 Op<-1>().set(0);
125 --NumOperands;
123126
124127 // If the PHI node is dead, because it has zero entries, nuke it now.
125 if (NumOps == 2 && DeletePHIIfEmpty) {
128 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
126129 // If anyone is using this PHI, make them use a dummy value instead...
127130 replaceAllUsesWith(UndefValue::get(getType()));
128131 eraseFromParent();
136139 ///
137140 void PHINode::growOperands() {
138141 unsigned e = getNumOperands();
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.
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();
142147
143148 ReservedSpace = NumOps;
144 Use *OldOps = OperandList;
145 Use *NewOps = allocHungoffUses(NumOps);
146 std::copy(OldOps, OldOps + e, NewOps);
147 OperandList = NewOps;
149 OperandList = allocHungoffUses(ReservedSpace);
150
151 std::copy(OldOps, OldOps + e, op_begin());
152 std::copy(OldBlocks, OldBlocks + e, block_begin());
153
148154 Use::zap(OldOps, OldOps + e, true);
149155 }
150156
3939 //===----------------------------------------------------------------------===//
4040
4141 Use *User::allocHungoffUses(unsigned N) const {
42 Use *Begin = static_cast(::operator new(sizeof(Use) * N
43 + sizeof(Use::UserRef)));
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));
4446 Use *End = Begin + N;
4547 (void) new(End) Use::UserRef(const_cast(this), 1);
4648 return Use::initTags(Begin, End);
304304
305305 U.set(New);
306306 }
307
308 if (BasicBlock *BB = dyn_cast(this))
309 BB->replaceSuccessorsPhiUsesWith(cast(New));
307310 }
308311
309312 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);
11441141 }
11451142
11461143 // All other PHI node constraints are checked in the visitBasicBlock method.