@@ -55,10 +55,6 @@ FpExceptionMode parse_exceptions(llvm::Instruction &i) {
5555 }
5656}
5757
58- bool has_constant_expr (llvm::Value *val) {
59- return isa<llvm::ConstantExpr, llvm::ConstantAggregate>(val);
60- }
61-
6258unsigned constexpr_idx;
6359unsigned copy_idx;
6460unsigned alignopbundle_idx;
@@ -106,6 +102,7 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
106102 vector<llvm::Instruction*> i_constexprs;
107103 const vector<string_view> &gvnamesInSrc;
108104 vector<pair<Phi*, llvm::PHINode*>> todo_phis;
105+ const Instr *insert_constexpr_before = nullptr ;
109106 ostream *out;
110107 // (LLVM alloca, (Alive2 alloc, has lifetime.start?))
111108 map<const llvm::AllocaInst *, std::pair<Alloc *, bool >> allocs;
@@ -137,7 +134,10 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
137134 return nullptr ;
138135
139136 auto i = ptr.get ();
140- BB->addInstr (std::move (ptr));
137+ if (insert_constexpr_before)
138+ BB->addInstrAt (std::move (ptr), insert_constexpr_before, true );
139+ else
140+ BB->addInstr (std::move (ptr));
141141 return i;
142142 }
143143
@@ -1540,49 +1540,12 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
15401540 }
15411541 }
15421542
1543- // BB -> BB edge
1544- set<pair<llvm::BasicBlock*, llvm::BasicBlock*>> split_edges;
1545- for (auto &[phi, i] : todo_phis) {
1546- for (unsigned idx = 0 , e = i->getNumIncomingValues (); idx != e; ++idx) {
1547- if (has_constant_expr (i->getIncomingValue (idx))) {
1548- split_edges.emplace (i->getParent (), i->getIncomingBlock (idx));
1549- break ;
1550- }
1551- }
1552- }
1553-
1554- auto predecessor = [&](llvm::PHINode *phi, unsigned idx) {
1555- auto pred = phi->getIncomingBlock (idx);
1556- auto name = value_name (*pred);
1557- if (!split_edges.count ({phi->getParent (), pred}))
1558- return name;
1559- return std::move (name) + " _" + getBB (phi->getParent ()).getName ();
1560- };
1561-
15621543 // patch phi nodes for recursive defs
15631544 for (auto &[phi, i] : todo_phis) {
15641545 for (unsigned idx = 0 , e = i->getNumIncomingValues (); idx != e; ++idx) {
1565- // evaluation of constexprs in phi nodes is done "in the edge", thus we
1566- // introduce a new BB even if not always needed.
1567- auto val = i->getIncomingValue (idx);
1568- if (has_constant_expr (val)) {
1569- auto bridge = predecessor (i, idx);
1570- auto &pred = getBB (i->getIncomingBlock (idx));
1571- BB = &Fn.insertBBAfter (bridge, pred);
1572-
1573- auto &phi_bb = getBB (i->getParent ());
1574- pred.replaceTargetWith (&phi_bb, BB);
1575- if (auto op = get_operand (val)) {
1576- phi->addValue (*op, std::move (bridge));
1577- BB->addInstr (make_unique<Branch>(phi_bb));
1578- continue ;
1579- }
1580- error (*i);
1581- return {};
1582- }
1583-
1584- if (auto op = get_operand (val)) {
1585- phi->addValue (*op, predecessor (i, idx));
1546+ insert_constexpr_before = phi;
1547+ if (auto op = get_operand (i->getIncomingValue (idx))) {
1548+ phi->addValue (*op, value_name (*i->getIncomingBlock (idx)));
15861549 } else {
15871550 error (*i);
15881551 return {};
0 commit comments