@@ -539,6 +539,19 @@ class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
539
539
Output.push_back (Edge (&Inst, From1, EdgeType::Assign, AttrNone));
540
540
Output.push_back (Edge (&Inst, From2, EdgeType::Assign, AttrNone));
541
541
}
542
+
543
+ void visitConstantExpr (ConstantExpr *CE) {
544
+ switch (CE->getOpcode ()) {
545
+ default :
546
+ llvm_unreachable (" Unknown instruction type encountered!" );
547
+ // Build the switch statement using the Instruction.def file.
548
+ #define HANDLE_INST (NUM, OPCODE, CLASS ) \
549
+ case Instruction::OPCODE: \
550
+ visit##OPCODE (*(CLASS *)CE); \
551
+ break ;
552
+ #include " llvm/IR/Instruction.def"
553
+ }
554
+ }
542
555
};
543
556
544
557
// For a given instruction, we need to know which Value* to get the
@@ -741,6 +754,10 @@ static EdgeType flipWeight(EdgeType);
741
754
static void argsToEdges (CFLAliasAnalysis &, Instruction *,
742
755
SmallVectorImpl<Edge> &);
743
756
757
+ // Gets edges of the given ConstantExpr*, writing them to the SmallVector*.
758
+ static void argsToEdges (CFLAliasAnalysis &, ConstantExpr *,
759
+ SmallVectorImpl<Edge> &);
760
+
744
761
// Gets the "Level" that one should travel in StratifiedSets
745
762
// given an EdgeType.
746
763
static Level directionOfEdgeType (EdgeType);
@@ -807,6 +824,13 @@ static bool hasUsefulEdges(Instruction *Inst) {
807
824
return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator;
808
825
}
809
826
827
+ static bool hasUsefulEdges (ConstantExpr *CE) {
828
+ // ConstantExpr doens't have terminators, invokes, or fences, so only needs
829
+ // to check for compares.
830
+ return CE->getOpcode () != Instruction::ICmp &&
831
+ CE->getOpcode () != Instruction::FCmp;
832
+ }
833
+
810
834
static Optional<StratifiedAttr> valueToAttrIndex (Value *Val) {
811
835
if (isa<GlobalValue>(Val))
812
836
return AttrGlobalIndex;
@@ -846,6 +870,13 @@ static void argsToEdges(CFLAliasAnalysis &Analysis, Instruction *Inst,
846
870
v.visit (Inst);
847
871
}
848
872
873
+ static void argsToEdges (CFLAliasAnalysis &Analysis, ConstantExpr *CE,
874
+ SmallVectorImpl<Edge> &Output) {
875
+ assert (hasUsefulEdges (CE) && " Expected constant expr to have 'useful' edges" );
876
+ GetEdgesVisitor v (Analysis, Output);
877
+ v.visitConstantExpr (CE);
878
+ }
879
+
849
880
static Level directionOfEdgeType (EdgeType Weight) {
850
881
switch (Weight) {
851
882
case EdgeType::Reference:
@@ -865,25 +896,23 @@ static void constexprToEdges(CFLAliasAnalysis &Analysis,
865
896
Worklist.push_back (&CExprToCollapse);
866
897
867
898
SmallVector<Edge, 8 > ConstexprEdges;
899
+ SmallPtrSet<ConstantExpr *, 4 > Visited;
868
900
while (!Worklist.empty ()) {
869
901
auto *CExpr = Worklist.pop_back_val ();
870
- std::unique_ptr<Instruction> Inst (CExpr->getAsInstruction ());
871
902
872
- if (!hasUsefulEdges (Inst. get () ))
903
+ if (!hasUsefulEdges (CExpr ))
873
904
continue ;
874
905
875
906
ConstexprEdges.clear ();
876
- argsToEdges (Analysis, Inst. get () , ConstexprEdges);
907
+ argsToEdges (Analysis, CExpr , ConstexprEdges);
877
908
for (auto &Edge : ConstexprEdges) {
878
- if (Edge.From == Inst.get ())
879
- Edge.From = CExpr;
880
- else if (auto *Nested = dyn_cast<ConstantExpr>(Edge.From ))
881
- Worklist.push_back (Nested);
882
-
883
- if (Edge.To == Inst.get ())
884
- Edge.To = CExpr;
885
- else if (auto *Nested = dyn_cast<ConstantExpr>(Edge.To ))
886
- Worklist.push_back (Nested);
909
+ if (auto *Nested = dyn_cast<ConstantExpr>(Edge.From ))
910
+ if (Visited.insert (Nested).second )
911
+ Worklist.push_back (Nested);
912
+
913
+ if (auto *Nested = dyn_cast<ConstantExpr>(Edge.To ))
914
+ if (Visited.insert (Nested).second )
915
+ Worklist.push_back (Nested);
887
916
}
888
917
889
918
Results.append (ConstexprEdges.begin (), ConstexprEdges.end ());
0 commit comments