@@ -54,55 +54,6 @@ static void logFailure(llvm::ScopedPrinter &os, StringRef fmt, Args &&...args) {
5454 });
5555}
5656
57- // / Given two insertion points in the same block, choose the later one.
58- static OpBuilder::InsertPoint
59- chooseLaterInsertPointInBlock (OpBuilder::InsertPoint a,
60- OpBuilder::InsertPoint b) {
61- assert (a.getBlock () == b.getBlock () && " expected same block" );
62- Block *block = a.getBlock ();
63- if (a.getPoint () == block->begin ())
64- return b;
65- if (b.getPoint () == block->begin ())
66- return a;
67- if (a.getPoint ()->isBeforeInBlock (&*b.getPoint ()))
68- return b;
69- return a;
70- }
71-
72- // / Helper function that chooses the insertion point among the two given ones
73- // / that is later.
74- // TODO: Extend DominanceInfo API to work with block iterators.
75- static OpBuilder::InsertPoint chooseLaterInsertPoint (OpBuilder::InsertPoint a,
76- OpBuilder::InsertPoint b) {
77- // Case 1: Same block.
78- if (a.getBlock () == b.getBlock ())
79- return chooseLaterInsertPointInBlock (a, b);
80-
81- // Case 2: Different block, but same region.
82- if (a.getBlock ()->getParent () == b.getBlock ()->getParent ()) {
83- DominanceInfo domInfo;
84- if (domInfo.properlyDominates (a.getBlock (), b.getBlock ()))
85- return b;
86- if (domInfo.properlyDominates (b.getBlock (), a.getBlock ()))
87- return a;
88- // Neither of the two blocks dominante each other.
89- llvm_unreachable (" unable to find valid insertion point" );
90- }
91-
92- // Case 3: b's region contains a: choose a.
93- if (Operation *aParent = b.getBlock ()->getParent ()->findAncestorOpInRegion (
94- *a.getPoint ()->getParentOp ()))
95- return a;
96-
97- // Case 4: a's region contains b: choose b.
98- if (Operation *bParent = a.getBlock ()->getParent ()->findAncestorOpInRegion (
99- *b.getPoint ()->getParentOp ()))
100- return b;
101-
102- // Neither of the two operations contain each other.
103- llvm_unreachable (" unable to find valid insertion point" );
104- }
105-
10657// / Helper function that computes an insertion point where the given value is
10758// / defined and can be used without a dominance violation.
10859static OpBuilder::InsertPoint computeInsertPoint (Value value) {
@@ -117,9 +68,26 @@ static OpBuilder::InsertPoint computeInsertPoint(Value value) {
11768// / defined and can be used without a dominance violation.
11869static OpBuilder::InsertPoint computeInsertPoint (ArrayRef<Value> vals) {
11970 assert (!vals.empty () && " expected at least one value" );
71+ DominanceInfo domInfo;
12072 OpBuilder::InsertPoint pt = computeInsertPoint (vals.front ());
121- for (Value v : vals.drop_front ())
122- pt = chooseLaterInsertPoint (pt, computeInsertPoint (v));
73+ for (Value v : vals.drop_front ()) {
74+ // Choose the "later" insertion point.
75+ OpBuilder::InsertPoint nextPt = computeInsertPoint (v);
76+ if (domInfo.dominates (pt.getBlock (), pt.getPoint (), nextPt.getBlock (),
77+ nextPt.getPoint ())) {
78+ // pt is before nextPt => choose nextPt.
79+ pt = nextPt;
80+ } else {
81+ #ifndef NDEBUG
82+ // nextPt should be before pt => choose pt.
83+ // If pt, nextPt are no dominance relationship, then there is no valid
84+ // insertion point at which all given values are defined.
85+ bool dom = domInfo.dominates (nextPt.getBlock (), nextPt.getPoint (),
86+ pt.getBlock (), pt.getPoint ());
87+ assert (dom && " unable to find valid insertion point" );
88+ #endif // NDEBUG
89+ }
90+ }
12391 return pt;
12492}
12593
0 commit comments