@@ -244,11 +244,10 @@ class PointerReplacer {
244
244
void replacePointer (Value *V);
245
245
246
246
private:
247
- bool collectUsersRecursive (Instruction &I);
248
247
void replace (Instruction *I);
249
- Value *getReplacement (Value *I);
248
+ Value *getReplacement (Value *V) const { return WorkMap. lookup (V); }
250
249
bool isAvailable (Instruction *I) const {
251
- return I == &Root || Worklist .contains (I);
250
+ return I == &Root || UsersToReplace .contains (I);
252
251
}
253
252
254
253
bool isEqualOrValidAddrSpaceCast (const Instruction *I,
@@ -260,8 +259,7 @@ class PointerReplacer {
260
259
return (FromAS == ToAS) || IC.isValidAddrSpaceCast (FromAS, ToAS);
261
260
}
262
261
263
- SmallPtrSet<Instruction *, 32 > ValuesToRevisit;
264
- SmallSetVector<Instruction *, 4 > Worklist;
262
+ SmallSetVector<Instruction *, 4 > UsersToReplace;
265
263
MapVector<Value *, Value *> WorkMap;
266
264
InstCombinerImpl &IC;
267
265
Instruction &Root;
@@ -270,80 +268,131 @@ class PointerReplacer {
270
268
} // end anonymous namespace
271
269
272
270
bool PointerReplacer::collectUsers () {
273
- if (!collectUsersRecursive (Root))
274
- return false ;
275
-
276
- // Ensure that all outstanding (indirect) users of I
277
- // are inserted into the Worklist. Return false
278
- // otherwise.
279
- return llvm::set_is_subset (ValuesToRevisit, Worklist);
280
- }
271
+ SmallVector<Instruction *> Worklist;
272
+ SmallSetVector<Instruction *, 4 > ValuesToRevisit;
273
+
274
+ auto PushUsersToWorklist = [&](Instruction *Inst) {
275
+ for (auto *U : Inst->users ()) {
276
+ if (auto *I = dyn_cast<Instruction>(U)) {
277
+ if (!isAvailable (I) && !ValuesToRevisit.contains (I))
278
+ Worklist.emplace_back (I);
279
+ }
280
+ }
281
+ };
281
282
282
- bool PointerReplacer::collectUsersRecursive (Instruction &I) {
283
- for (auto *U : I.users ()) {
284
- auto *Inst = cast<Instruction>(&*U);
283
+ PushUsersToWorklist (&Root);
284
+ while (!Worklist.empty ()) {
285
+ auto *Inst = Worklist.pop_back_val ();
286
+ if (!Inst)
287
+ return false ;
288
+ if (isAvailable (Inst))
289
+ continue ;
285
290
if (auto *Load = dyn_cast<LoadInst>(Inst)) {
286
291
if (Load->isVolatile ())
287
292
return false ;
288
- Worklist .insert (Load);
293
+ UsersToReplace .insert (Load);
289
294
} else if (auto *PHI = dyn_cast<PHINode>(Inst)) {
290
295
// All incoming values must be instructions for replacability
291
296
if (any_of (PHI->incoming_values (),
292
297
[](Value *V) { return !isa<Instruction>(V); }))
293
298
return false ;
294
299
295
- // If at least one incoming value of the PHI is not in Worklist,
296
- // store the PHI for revisiting and skip this iteration of the
297
- // loop.
298
- if (any_of (PHI->incoming_values (), [this ](Value *V) {
299
- return !isAvailable (cast<Instruction>(V));
300
- })) {
301
- ValuesToRevisit.insert (Inst);
300
+ // If all incoming values are available, mark this PHI as
301
+ // replacable and push it's users into the worklist.
302
+ if (all_of (PHI->incoming_values (),
303
+ [&](Value *V) { return isAvailable (cast<Instruction>(V)); })) {
304
+ UsersToReplace.insert (PHI);
305
+ PushUsersToWorklist (PHI);
302
306
continue ;
303
307
}
304
308
305
- Worklist.insert (PHI);
306
- if (!collectUsersRecursive (*PHI))
307
- return false ;
308
- } else if (auto *SI = dyn_cast<SelectInst>(Inst)) {
309
- if (!isa<Instruction>(SI->getTrueValue ()) ||
310
- !isa<Instruction>(SI->getFalseValue ()))
309
+ // Not all incoming values are available. If this PHI was already
310
+ // visited prior to this iteration, return false.
311
+ if (!ValuesToRevisit.insert (PHI))
311
312
return false ;
312
313
313
- if (!isAvailable (cast<Instruction>(SI->getTrueValue ())) ||
314
- !isAvailable (cast<Instruction>(SI->getFalseValue ()))) {
315
- ValuesToRevisit.insert (Inst);
316
- continue ;
314
+ // Push PHI back into the stack, followed by unavailable
315
+ // incoming values.
316
+ Worklist.emplace_back (PHI);
317
+ for (unsigned Idx = 0 ; Idx < PHI->getNumIncomingValues (); ++Idx) {
318
+ auto *IncomingValue = cast<Instruction>(PHI->getIncomingValue (Idx));
319
+ if (UsersToReplace.contains (IncomingValue))
320
+ continue ;
321
+ if (!ValuesToRevisit.insert (IncomingValue))
322
+ return false ;
323
+ Worklist.emplace_back (IncomingValue);
317
324
}
318
- Worklist.insert (SI);
319
- if (!collectUsersRecursive (*SI))
320
- return false ;
321
- } else if (isa<GetElementPtrInst>(Inst)) {
322
- Worklist.insert (Inst);
323
- if (!collectUsersRecursive (*Inst))
325
+ } else if (auto *SI = dyn_cast<SelectInst>(Inst)) {
326
+ auto *TrueInst = dyn_cast<Instruction>(SI->getTrueValue ());
327
+ auto *FalseInst = dyn_cast<Instruction>(SI->getFalseValue ());
328
+ if (!TrueInst || !FalseInst)
324
329
return false ;
330
+
331
+ UsersToReplace.insert (SI);
332
+ PushUsersToWorklist (SI);
333
+ } else if (auto *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
334
+ UsersToReplace.insert (GEP);
335
+ PushUsersToWorklist (GEP);
325
336
} else if (auto *MI = dyn_cast<MemTransferInst>(Inst)) {
326
337
if (MI->isVolatile ())
327
338
return false ;
328
- Worklist .insert (Inst);
339
+ UsersToReplace .insert (Inst);
329
340
} else if (isEqualOrValidAddrSpaceCast (Inst, FromAS)) {
330
- Worklist.insert (Inst);
331
- if (!collectUsersRecursive (*Inst))
332
- return false ;
341
+ UsersToReplace.insert (Inst);
342
+ PushUsersToWorklist (Inst);
333
343
} else if (Inst->isLifetimeStartOrEnd ()) {
334
344
continue ;
335
345
} else {
336
346
// TODO: For arbitrary uses with address space mismatches, should we check
337
347
// if we can introduce a valid addrspacecast?
338
- LLVM_DEBUG (dbgs () << " Cannot handle pointer user: " << *U << ' \n ' );
348
+ LLVM_DEBUG (dbgs () << " Cannot handle pointer user: " << *Inst << ' \n ' );
339
349
return false ;
340
350
}
341
351
}
342
352
343
- return true ;
353
+ return llvm::set_is_subset (ValuesToRevisit, UsersToReplace) ;
344
354
}
345
355
346
- Value *PointerReplacer::getReplacement (Value *V) { return WorkMap.lookup (V); }
356
+ void PointerReplacer::replacePointer (Value *V) {
357
+ #ifndef NDEBUG
358
+ auto *PT = cast<PointerType>(Root.getType ());
359
+ auto *NT = cast<PointerType>(V->getType ());
360
+ assert (PT != NT && " Invalid usage" );
361
+ #endif
362
+
363
+ WorkMap[&Root] = V;
364
+ SmallVector<Instruction *> Worklist;
365
+ SetVector<Instruction *> PostOrderWorklist;
366
+ SmallPtrSet<Instruction *, 4 > Visited;
367
+
368
+ // Perform a postorder traversal of the users of Root.
369
+ Worklist.push_back (&Root);
370
+ while (!Worklist.empty ()) {
371
+ Instruction *I = Worklist.back ();
372
+
373
+ // If I has not been processed before, push each of its
374
+ // replacable users into the worklist.
375
+ if (Visited.insert (I).second ) {
376
+ for (auto *U : I->users ()) {
377
+ assert (isa<Instruction>(U) &&
378
+ " User should not have been"
379
+ " collected as it is not an instruction." );
380
+ auto *UserInst = cast<Instruction>(U);
381
+ if (UsersToReplace.contains (UserInst))
382
+ Worklist.push_back (UserInst);
383
+ }
384
+ // Otherwise, users of I have already been pushed into
385
+ // the PostOrderWorklist. Push I as well.
386
+ } else {
387
+ PostOrderWorklist.insert (I);
388
+ Worklist.pop_back ();
389
+ }
390
+ }
391
+
392
+ // Replace pointers in reverse-postorder.
393
+ for (Instruction *I : llvm::reverse (PostOrderWorklist))
394
+ replace (I);
395
+ }
347
396
348
397
void PointerReplacer::replace (Instruction *I) {
349
398
if (getReplacement (I))
@@ -365,12 +414,20 @@ void PointerReplacer::replace(Instruction *I) {
365
414
// replacement (new value).
366
415
WorkMap[NewI] = NewI;
367
416
} else if (auto *PHI = dyn_cast<PHINode>(I)) {
368
- Type *NewTy = getReplacement (PHI->getIncomingValue (0 ))->getType ();
369
- auto *NewPHI = PHINode::Create (NewTy, PHI->getNumIncomingValues (),
370
- PHI->getName (), PHI->getIterator ());
417
+ // Create a new PHI by replacing any incoming value that is a user of the
418
+ // root pointer and has a replacement.
419
+ SmallVector<Value *, 4 > IncomingValues;
420
+ for (unsigned int I = 0 ; I < PHI->getNumIncomingValues (); ++I) {
421
+ Value *V = getReplacement (PHI->getIncomingValue (I));
422
+ if (!V)
423
+ V = PHI->getIncomingValue (I);
424
+ IncomingValues.push_back (V);
425
+ }
426
+ auto *NewPHI = PHINode::Create (IncomingValues[0 ]->getType (),
427
+ PHI->getNumIncomingValues (),
428
+ PHI->getName () + " .r" , PHI->getIterator ());
371
429
for (unsigned int I = 0 ; I < PHI->getNumIncomingValues (); ++I)
372
- NewPHI->addIncoming (getReplacement (PHI->getIncomingValue (I)),
373
- PHI->getIncomingBlock (I));
430
+ NewPHI->addIncoming (IncomingValues[I], PHI->getIncomingBlock (I));
374
431
WorkMap[PHI] = NewPHI;
375
432
} else if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
376
433
auto *V = getReplacement (GEP->getPointerOperand ());
@@ -431,22 +488,12 @@ void PointerReplacer::replace(Instruction *I) {
431
488
}
432
489
433
490
} else {
491
+ dbgs () << " Instruction " << *I
492
+ << " is not supported in PointerReplacer::replace\n " ;
434
493
llvm_unreachable (" should never reach here" );
435
494
}
436
495
}
437
496
438
- void PointerReplacer::replacePointer (Value *V) {
439
- #ifndef NDEBUG
440
- auto *PT = cast<PointerType>(Root.getType ());
441
- auto *NT = cast<PointerType>(V->getType ());
442
- assert (PT != NT && " Invalid usage" );
443
- #endif
444
- WorkMap[&Root] = V;
445
-
446
- for (Instruction *Workitem : Worklist)
447
- replace (Workitem);
448
- }
449
-
450
497
Instruction *InstCombinerImpl::visitAllocaInst (AllocaInst &AI) {
451
498
if (auto *I = simplifyAllocaArraySize (*this , AI, DT))
452
499
return I;
0 commit comments