- BasicBlock *BB = new BasicBlock();
- // Remember where this forward reference came from. FIXME, shouldn't we try
- // to recycle these things??
- CurModule.PlaceHolderInfo.insert(std::make_pair(BB, std::make_pair(ID,
- llvmAsmlineno)));
+ // See if the block has already been defined.
+ if (BB) {
+ // If this is the definition of the block, make sure the existing value was
+ // just a forward reference. If it was a forward reference, there will be
+ // an entry for it in the PlaceHolderInfo map.
+ if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
+ // The existing value was a definition, not a forward reference.
+ ThrowException("Redefinition of label " + ID.getName());
+
+ ID.destroy(); // Free strdup'd memory.
+ return BB;
+ }
+
+ // Otherwise this block has not been seen before.
+ BB = new BasicBlock("", CurFun.CurrentFunction);
+ if (ID.Type == ValID::NameVal) {
+ BB->setName(ID.Name);
+ } else {
+ CurFun.NumberedBlocks[ID.Num] = BB;
+ }
+
+ // If this is not a definition, keep track of it so we can use it as a forward
+ // reference.
+ if (!isDefinition) {
+ // Remember where this forward reference came from.
+ CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
+ } else {
+ // The forward declaration could have been inserted anywhere in the
+ // function: insert it into the correct place now.
+ CurFun.CurrentFunction->getBasicBlockList().remove(BB);
+ CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
+ }