Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 0433a64

Browse files
committed
Don't create instructions from ConstantExpr's in CFLAliasAnalysis.
The CFLAA code currently calls ConstantExpr::getAsInstruction which creates an instruction from a constant expr. We then pass that instruction to the InstVisitor to analyze it. Its not necessary to create these instructions as we can just cast from Constant to Operator in the visitor. This is how other InstVisitor’s such as SelectionDAGBuilder handle ConstantExpr. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239616 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent cb2dfa6 commit 0433a64

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

lib/Analysis/CFLAliasAnalysis.cpp

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,19 @@ class GetEdgesVisitor : public InstVisitor<GetEdgesVisitor, void> {
539539
Output.push_back(Edge(&Inst, From1, EdgeType::Assign, AttrNone));
540540
Output.push_back(Edge(&Inst, From2, EdgeType::Assign, AttrNone));
541541
}
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+
}
542555
};
543556

544557
// For a given instruction, we need to know which Value* to get the
@@ -741,6 +754,10 @@ static EdgeType flipWeight(EdgeType);
741754
static void argsToEdges(CFLAliasAnalysis &, Instruction *,
742755
SmallVectorImpl<Edge> &);
743756

757+
// Gets edges of the given ConstantExpr*, writing them to the SmallVector*.
758+
static void argsToEdges(CFLAliasAnalysis &, ConstantExpr *,
759+
SmallVectorImpl<Edge> &);
760+
744761
// Gets the "Level" that one should travel in StratifiedSets
745762
// given an EdgeType.
746763
static Level directionOfEdgeType(EdgeType);
@@ -807,6 +824,13 @@ static bool hasUsefulEdges(Instruction *Inst) {
807824
return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) && !IsNonInvokeTerminator;
808825
}
809826

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+
810834
static Optional<StratifiedAttr> valueToAttrIndex(Value *Val) {
811835
if (isa<GlobalValue>(Val))
812836
return AttrGlobalIndex;
@@ -846,6 +870,13 @@ static void argsToEdges(CFLAliasAnalysis &Analysis, Instruction *Inst,
846870
v.visit(Inst);
847871
}
848872

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+
849880
static Level directionOfEdgeType(EdgeType Weight) {
850881
switch (Weight) {
851882
case EdgeType::Reference:
@@ -865,25 +896,23 @@ static void constexprToEdges(CFLAliasAnalysis &Analysis,
865896
Worklist.push_back(&CExprToCollapse);
866897

867898
SmallVector<Edge, 8> ConstexprEdges;
899+
SmallPtrSet<ConstantExpr *, 4> Visited;
868900
while (!Worklist.empty()) {
869901
auto *CExpr = Worklist.pop_back_val();
870-
std::unique_ptr<Instruction> Inst(CExpr->getAsInstruction());
871902

872-
if (!hasUsefulEdges(Inst.get()))
903+
if (!hasUsefulEdges(CExpr))
873904
continue;
874905

875906
ConstexprEdges.clear();
876-
argsToEdges(Analysis, Inst.get(), ConstexprEdges);
907+
argsToEdges(Analysis, CExpr, ConstexprEdges);
877908
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);
887916
}
888917

889918
Results.append(ConstexprEdges.begin(), ConstexprEdges.end());

0 commit comments

Comments
 (0)