@@ -258,6 +258,14 @@ void PromoteBools::cleanUp(Module& module)
258
258
{
259
259
renameAndClean (alloca, it.second );
260
260
}
261
+ else if (auto bitcast = dyn_cast<BitCastInst>(it.first ))
262
+ {
263
+ renameAndClean (bitcast, it.second );
264
+ }
265
+ else if (auto addrSpaceCast = dyn_cast<AddrSpaceCastInst>(it.first ))
266
+ {
267
+ renameAndClean (addrSpaceCast, it.second );
268
+ }
261
269
}
262
270
263
271
std::vector<Instruction*> deadTruncs;
@@ -376,6 +384,14 @@ Value* PromoteBools::getOrCreatePromotedValue(Value* value)
376
384
{
377
385
newValue = promoteAlloca (alloca);
378
386
}
387
+ else if (auto bitcast = dyn_cast<BitCastInst>(value))
388
+ {
389
+ newValue = promoteBitCast (bitcast);
390
+ }
391
+ else if (auto addrSpaceCast = dyn_cast<AddrSpaceCastInst>(value))
392
+ {
393
+ newValue = promoteAddrSpaceCast (addrSpaceCast);
394
+ }
379
395
380
396
if (newValue != value)
381
397
{
@@ -481,22 +497,74 @@ GlobalVariable* PromoteBools::promoteGlobalVariable(GlobalVariable* globalVariab
481
497
globalVariable->getType ()->getPointerAddressSpace ());
482
498
}
483
499
484
- AllocaInst * PromoteBools::promoteAlloca (AllocaInst* alloca)
500
+ Value * PromoteBools::promoteAlloca (AllocaInst* alloca)
485
501
{
486
502
if (!alloca || !typeNeedsPromotion (alloca->getAllocatedType ()))
487
503
{
488
504
return alloca;
489
505
}
490
506
507
+ auto oldAllocaName = alloca->getName ().str ();
508
+ alloca->setName (oldAllocaName + " _bitcast" );
509
+
491
510
auto newAlloca = new AllocaInst (
492
511
getOrCreatePromotedType (alloca->getAllocatedType ()),
493
512
alloca->getType ()->getAddressSpace (),
494
513
alloca->isArrayAllocation () ? alloca->getArraySize () : nullptr ,
495
- alloca-> getName () ,
514
+ oldAllocaName ,
496
515
alloca);
497
516
newAlloca->setAlignment (IGCLLVM::getAlign (*alloca));
498
517
499
- return newAlloca;
518
+ IRBuilder<> builder (alloca);
519
+ auto bitcast = builder.CreateBitCast (newAlloca, alloca->getType ());
520
+ alloca->replaceAllUsesWith (bitcast);
521
+
522
+ return bitcast;
523
+ }
524
+
525
+ Value* PromoteBools::promoteBitCast (BitCastInst* bitcast)
526
+ {
527
+ if (!bitcast || !typeNeedsPromotion (bitcast->getDestTy ()))
528
+ {
529
+ return bitcast;
530
+ }
531
+
532
+ auto newType = getOrCreatePromotedType (bitcast->getDestTy ());
533
+ if (bitcast->getSrcTy () == newType)
534
+ {
535
+ auto result = bitcast->getOperand (0 );
536
+
537
+ // swap names
538
+ auto name = result->getName ().str ();
539
+ result->setName (name + " _tmp" );
540
+ bitcast->setName (name);
541
+
542
+ return result;
543
+ }
544
+
545
+ auto newBitCast = new BitCastInst (
546
+ bitcast->getOperand (0 ),
547
+ getOrCreatePromotedType (bitcast->getDestTy ()),
548
+ bitcast->getName (),
549
+ bitcast
550
+ );
551
+ return newBitCast;
552
+ }
553
+
554
+ Value* PromoteBools::promoteAddrSpaceCast (AddrSpaceCastInst* addrSpaceCast)
555
+ {
556
+ if (!addrSpaceCast || !typeNeedsPromotion (addrSpaceCast->getDestTy ()))
557
+ {
558
+ return addrSpaceCast;
559
+ }
560
+
561
+ auto newAddrSpaceCast = new AddrSpaceCastInst (
562
+ addrSpaceCast->getOperand (0 ),
563
+ getOrCreatePromotedType (addrSpaceCast->getDestTy ()),
564
+ addrSpaceCast->getName (),
565
+ addrSpaceCast
566
+ );
567
+ return newAddrSpaceCast;
500
568
}
501
569
502
570
Constant* PromoteBools::createPromotedConstant (Constant* constant)
0 commit comments