@@ -651,19 +651,6 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
651
651
return newFunction;
652
652
}
653
653
654
- // / FindPhiPredForUseInBlock - Given a value and a basic block, find a PHI
655
- // / that uses the value within the basic block, and return the predecessor
656
- // / block associated with that use, or return 0 if none is found.
657
- static BasicBlock* FindPhiPredForUseInBlock (Value* Used, BasicBlock* BB) {
658
- for (Use &U : Used->uses ()) {
659
- PHINode *P = dyn_cast<PHINode>(U.getUser ());
660
- if (P && P->getParent () == BB)
661
- return P->getIncomingBlock (U);
662
- }
663
-
664
- return nullptr ;
665
- }
666
-
667
654
// / emitCallAndSwitchStatement - This method sets up the caller side by adding
668
655
// / the call instruction, splitting any PHI nodes in the header block as
669
656
// / necessary.
@@ -736,7 +723,8 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
736
723
if (!AggregateArgs)
737
724
std::advance (OutputArgBegin, inputs.size ());
738
725
739
- // Reload the outputs passed in by reference
726
+ // Reload the outputs passed in by reference.
727
+ Function::arg_iterator OAI = OutputArgBegin;
740
728
for (unsigned i = 0 , e = outputs.size (); i != e; ++i) {
741
729
Value *Output = nullptr ;
742
730
if (AggregateArgs) {
@@ -759,6 +747,34 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
759
747
if (!Blocks.count (inst->getParent ()))
760
748
inst->replaceUsesOfWith (outputs[i], load);
761
749
}
750
+
751
+ // Store to argument right after the definition of output value.
752
+ auto *OutI = dyn_cast<Instruction>(outputs[i]);
753
+ if (!OutI)
754
+ continue ;
755
+ // Find proper insertion point.
756
+ Instruction *InsertPt = OutI->getNextNode ();
757
+ // Let's assume that there is no other guy interleave non-PHI in PHIs.
758
+ if (isa<PHINode>(InsertPt))
759
+ InsertPt = InsertPt->getParent ()->getFirstNonPHI ();
760
+
761
+ assert (OAI != newFunction->arg_end () &&
762
+ " Number of output arguments should match "
763
+ " the amount of defined values" );
764
+ if (AggregateArgs) {
765
+ Value *Idx[2 ];
766
+ Idx[0 ] = Constant::getNullValue (Type::getInt32Ty (Context));
767
+ Idx[1 ] = ConstantInt::get (Type::getInt32Ty (Context), FirstOut + i);
768
+ GetElementPtrInst *GEP = GetElementPtrInst::Create (
769
+ StructArgTy, &*OAI, Idx, " gep_" + outputs[i]->getName (), InsertPt);
770
+ new StoreInst (outputs[i], GEP, InsertPt);
771
+ // Since there should be only one struct argument aggregating
772
+ // all the output values, we shouldn't increment OAI, which always
773
+ // points to the struct argument, in this case.
774
+ } else {
775
+ new StoreInst (outputs[i], &*OAI, InsertPt);
776
+ ++OAI;
777
+ }
762
778
}
763
779
764
780
// Now we can emit a switch statement using the call as a value.
@@ -801,75 +817,13 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
801
817
break ;
802
818
}
803
819
804
- ReturnInst *NTRet = ReturnInst ::Create (Context, brVal, NewTarget);
820
+ ReturnInst::Create (Context, brVal, NewTarget);
805
821
806
822
// Update the switch instruction.
807
823
TheSwitch->addCase (ConstantInt::get (Type::getInt16Ty (Context),
808
824
SuccNum),
809
825
OldTarget);
810
826
811
- // Restore values just before we exit
812
- Function::arg_iterator OAI = OutputArgBegin;
813
- for (unsigned out = 0 , e = outputs.size (); out != e; ++out) {
814
- // For an invoke, the normal destination is the only one that is
815
- // dominated by the result of the invocation
816
- BasicBlock *DefBlock = cast<Instruction>(outputs[out])->getParent ();
817
-
818
- bool DominatesDef = true ;
819
-
820
- BasicBlock *NormalDest = nullptr ;
821
- if (auto *Invoke = dyn_cast<InvokeInst>(outputs[out]))
822
- NormalDest = Invoke->getNormalDest ();
823
-
824
- if (NormalDest) {
825
- DefBlock = NormalDest;
826
-
827
- // Make sure we are looking at the original successor block, not
828
- // at a newly inserted exit block, which won't be in the dominator
829
- // info.
830
- for (const auto &I : ExitBlockMap)
831
- if (DefBlock == I.second ) {
832
- DefBlock = I.first ;
833
- break ;
834
- }
835
-
836
- // In the extract block case, if the block we are extracting ends
837
- // with an invoke instruction, make sure that we don't emit a
838
- // store of the invoke value for the unwind block.
839
- if (!DT && DefBlock != OldTarget)
840
- DominatesDef = false ;
841
- }
842
-
843
- if (DT) {
844
- DominatesDef = DT->dominates (DefBlock, OldTarget);
845
-
846
- // If the output value is used by a phi in the target block,
847
- // then we need to test for dominance of the phi's predecessor
848
- // instead. Unfortunately, this a little complicated since we
849
- // have already rewritten uses of the value to uses of the reload.
850
- BasicBlock* pred = FindPhiPredForUseInBlock (Reloads[out],
851
- OldTarget);
852
- if (pred && DT && DT->dominates (DefBlock, pred))
853
- DominatesDef = true ;
854
- }
855
-
856
- if (DominatesDef) {
857
- if (AggregateArgs) {
858
- Value *Idx[2 ];
859
- Idx[0 ] = Constant::getNullValue (Type::getInt32Ty (Context));
860
- Idx[1 ] = ConstantInt::get (Type::getInt32Ty (Context),
861
- FirstOut+out);
862
- GetElementPtrInst *GEP = GetElementPtrInst::Create (
863
- StructArgTy, &*OAI, Idx, " gep_" + outputs[out]->getName (),
864
- NTRet);
865
- new StoreInst (outputs[out], GEP, NTRet);
866
- } else {
867
- new StoreInst (outputs[out], &*OAI, NTRet);
868
- }
869
- }
870
- // Advance output iterator even if we don't emit a store
871
- if (!AggregateArgs) ++OAI;
872
- }
873
827
}
874
828
875
829
// rewrite the original branch instruction with this new target
0 commit comments