llvm.org GIT mirror llvm / 17d7215
[MMI] Use TinyPtrVector instead of PointerUnion with vector. Also simplify duplicated code a bit. No functionality change intended. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240990 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 5 years ago
3 changed file(s) with 29 addition(s) and 105 deletion(s). Raw diff Collapse all Expand all
283283 /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
284284 /// block when its address is taken. This cannot be its normal LBB label
285285 /// because the block may be accessed outside its containing function.
286 MCSymbol *getAddrLabelSymbol(const BasicBlock *BB);
286 MCSymbol *getAddrLabelSymbol(const BasicBlock *BB) {
287 return getAddrLabelSymbolToEmit(BB).front();
288 }
287289
288290 /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
289291 /// basic block when its address is taken. If other blocks were RAUW'd to
290292 /// this one, we may have to emit them as well, return the whole set.
291 std::vector*> getAddrLabelSymbolToEmit(const BasicBlock *BB);
293 ArrayRef*> getAddrLabelSymbolToEmit(const BasicBlock *BB);
292294
293295 /// takeDeletedSymbolsForFunction - If the specified function has had any
294296 /// references to address-taken blocks generated, but the block got deleted,
23892389 if (isVerbose())
23902390 OutStreamer->AddComment("Block address taken");
23912391
2392 std::vector Symbols = MMI->getAddrLabelSymbolToEmit(BB);
2393 for (auto *Sym : Symbols)
2392 for (MCSymbol *Sym : MMI->getAddrLabelSymbolToEmit(BB))
23942393 OutStreamer->EmitLabel(Sym);
23952394 }
23962395
5353 class MMIAddrLabelMap {
5454 MCContext &Context;
5555 struct AddrLabelSymEntry {
56 /// Symbols - The symbols for the label. This is a pointer union that is
57 /// either one symbol (the common case) or a list of symbols.
58 PointerUnion*> Symbols;
56 /// Symbols - The symbols for the label.
57 TinyPtrVector Symbols;
5958
6059 Function *Fn; // The containing function of the BasicBlock.
6160 unsigned Index; // The index in BBCallbacks for the BasicBlock.
7978 ~MMIAddrLabelMap() {
8079 assert(DeletedAddrLabelsNeedingEmission.empty() &&
8180 "Some labels for deleted blocks never got emitted");
82
83 // Deallocate any of the 'list of symbols' case.
84 for (DenseMap, AddrLabelSymEntry>::iterator
85 I = AddrLabelSymbols.begin(), E = AddrLabelSymbols.end(); I != E; ++I)
86 if (I->second.Symbols.is*>())
87 delete I->second.Symbols.get*>();
88 }
89
90 MCSymbol *getAddrLabelSymbol(BasicBlock *BB);
91 std::vector getAddrLabelSymbolToEmit(BasicBlock *BB);
81 }
82
83 ArrayRef getAddrLabelSymbolToEmit(BasicBlock *BB);
9284
9385 void takeDeletedSymbolsForFunction(Function *F,
9486 std::vector &Result);
9890 };
9991 }
10092
101 MCSymbol *MMIAddrLabelMap::getAddrLabelSymbol(BasicBlock *BB) {
93 ArrayRef MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
10294 assert(BB->hasAddressTaken() &&
10395 "Shouldn't get label for block without address taken");
10496 AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
10597
10698 // If we already had an entry for this block, just return it.
107 if (!Entry.Symbols.isNull()) {
99 if (!Entry.Symbols.empty()) {
108100 assert(BB->getParent() == Entry.Fn && "Parent changed");
109 if (Entry.Symbols.is())
110 return Entry.Symbols.get();
111 return (*Entry.Symbols.get*>())[0];
101 return Entry.Symbols;
112102 }
113103
114104 // Otherwise, this is a new entry, create a new symbol for it and add an
115105 // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
116106 BBCallbacks.emplace_back(BB);
117107 BBCallbacks.back().setMap(this);
118 Entry.Index = BBCallbacks.size()-1;
108 Entry.Index = BBCallbacks.size() - 1;
119109 Entry.Fn = BB->getParent();
120 MCSymbol *Result = Context.createTempSymbol();
121 Entry.Symbols = Result;
122 return Result;
123 }
124
125 std::vector
126 MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
127 assert(BB->hasAddressTaken() &&
128 "Shouldn't get label for block without address taken");
129 AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
130
131 std::vector Result;
132
133 // If we already had an entry for this block, just return it.
134 if (Entry.Symbols.isNull())
135 Result.push_back(getAddrLabelSymbol(BB));
136 else if (MCSymbol *Sym = Entry.Symbols.dyn_cast())
137 Result.push_back(Sym);
138 else
139 Result = *Entry.Symbols.get*>();
140 return Result;
141 }
142
110 Entry.Symbols.push_back(Context.createTempSymbol());
111 return Entry.Symbols;
112 }
143113
144114 /// takeDeletedSymbolsForFunction - If we have any deleted symbols for F, return
145115 /// them.
161131 // If the block got deleted, there is no need for the symbol. If the symbol
162132 // was already emitted, we can just forget about it, otherwise we need to
163133 // queue it up for later emission when the function is output.
164 AddrLabelSymEntry Entry = AddrLabelSymbols[BB];
134 AddrLabelSymEntry Entry = std::move(AddrLabelSymbols[BB]);
165135 AddrLabelSymbols.erase(BB);
166 assert(!Entry.Symbols.isNull() && "Didn't have a symbol, why a callback?");
136 assert(!Entry.Symbols.empty() && "Didn't have a symbol, why a callback?");
167137 BBCallbacks[Entry.Index] = nullptr; // Clear the callback.
168138
169139 assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) &&
170140 "Block/parent mismatch");
171141
172 // Handle both the single and the multiple symbols cases.
173 if (MCSymbol *Sym = Entry.Symbols.dyn_cast()) {
142 for (MCSymbol *Sym : Entry.Symbols) {
174143 if (Sym->isDefined())
175144 return;
176145
179148 // for the containing Function. Since the block is being deleted, its
180149 // parent may already be removed, we have to get the function from 'Entry'.
181150 DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
182 } else {
183 std::vector *Syms = Entry.Symbols.get*>();
184
185 for (unsigned i = 0, e = Syms->size(); i != e; ++i) {
186 MCSymbol *Sym = (*Syms)[i];
187 if (Sym->isDefined()) continue; // Ignore already emitted labels.
188
189 // If the block is not yet defined, we need to emit it at the end of the
190 // function. Add the symbol to the DeletedAddrLabelsNeedingEmission list
191 // for the containing Function. Since the block is being deleted, its
192 // parent may already be removed, we have to get the function from
193 // 'Entry'.
194 DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
195 }
196
197 // The entry is deleted, free the memory associated with the symbol list.
198 delete Syms;
199151 }
200152 }
201153
202154 void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
203155 // Get the entry for the RAUW'd block and remove it from our map.
204 AddrLabelSymEntry OldEntry = AddrLabelSymbols[Old];
156 AddrLabelSymEntry OldEntry = std::move(AddrLabelSymbols[Old]);
205157 AddrLabelSymbols.erase(Old);
206 assert(!OldEntry.Symbols.isNull() && "Didn't have a symbol, why a callback?");
158 assert(!OldEntry.Symbols.empty() && "Didn't have a symbol, why a callback?");
207159
208160 AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New];
209161
210162 // If New is not address taken, just move our symbol over to it.
211 if (NewEntry.Symbols.isNull()) {
163 if (NewEntry.Symbols.empty()) {
212164 BBCallbacks[OldEntry.Index].setPtr(New); // Update the callback.
213 NewEntry = OldEntry; // Set New's entry.
165 NewEntry = std::move(OldEntry); // Set New's entry.
214166 return;
215167 }
216168
217169 BBCallbacks[OldEntry.Index] = nullptr; // Update the callback.
218170
219 // Otherwise, we need to add the old symbol to the new block's set. If it is
220 // just a single entry, upgrade it to a symbol list.
221 if (MCSymbol *PrevSym = NewEntry.Symbols.dyn_cast()) {
222 std::vector *SymList = new std::vector();
223 SymList->push_back(PrevSym);
224 NewEntry.Symbols = SymList;
225 }
226
227 std::vector *SymList =
228 NewEntry.Symbols.get*>();
229
230 // If the old entry was a single symbol, add it.
231 if (MCSymbol *Sym = OldEntry.Symbols.dyn_cast()) {
232 SymList->push_back(Sym);
233 return;
234 }
235
236 // Otherwise, concatenate the list.
237 std::vector *Syms =OldEntry.Symbols.get*>();
238 SymList->insert(SymList->end(), Syms->begin(), Syms->end());
239 delete Syms;
171 // Otherwise, we need to add the old symbols to the new block's set.
172 NewEntry.Symbols.insert(NewEntry.Symbols.end(), OldEntry.Symbols.begin(),
173 OldEntry.Symbols.end());
240174 }
241175
242176
319253
320254 //===- Address of Block Management ----------------------------------------===//
321255
322
323 /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
324 /// block when its address is taken. This cannot be its normal LBB label
325 /// because the block may be accessed outside its containing function.
326 MCSymbol *MachineModuleInfo::getAddrLabelSymbol(const BasicBlock *BB) {
327 // Lazily create AddrLabelSymbols.
328 if (!AddrLabelSymbols)
329 AddrLabelSymbols = new MMIAddrLabelMap(Context);
330 return AddrLabelSymbols->getAddrLabelSymbol(const_cast(BB));
331 }
332
333256 /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
334257 /// basic block when its address is taken. If other blocks were RAUW'd to
335258 /// this one, we may have to emit them as well, return the whole set.
336 std::vector MachineModuleInfo::
337 getAddrLabelSymbolToEmit(const BasicBlock *BB) {
259 ArrayRef
260 MachineModuleInfo::getAddrLabelSymbolToEmit(const BasicBlock *BB) {
338261 // Lazily create AddrLabelSymbols.
339262 if (!AddrLabelSymbols)
340263 AddrLabelSymbols = new MMIAddrLabelMap(Context);