You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
9.5 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package pointer
  5. // This file defines a naive Andersen-style solver for the inclusion
  6. // constraint system.
  7. import (
  8. "fmt"
  9. "go/types"
  10. )
  11. type solverState struct {
  12. complex []constraint // complex constraints attached to this node
  13. copyTo nodeset // simple copy constraint edges
  14. pts nodeset // points-to set of this node
  15. prevPTS nodeset // pts(n) in previous iteration (for difference propagation)
  16. }
  17. func (a *analysis) solve() {
  18. start("Solving")
  19. if a.log != nil {
  20. fmt.Fprintf(a.log, "\n\n==== Solving constraints\n\n")
  21. }
  22. // Solver main loop.
  23. var delta nodeset
  24. for {
  25. // Add new constraints to the graph:
  26. // static constraints from SSA on round 1,
  27. // dynamic constraints from reflection thereafter.
  28. a.processNewConstraints()
  29. var x int
  30. if !a.work.TakeMin(&x) {
  31. break // empty
  32. }
  33. id := nodeid(x)
  34. if a.log != nil {
  35. fmt.Fprintf(a.log, "\tnode n%d\n", id)
  36. }
  37. n := a.nodes[id]
  38. // Difference propagation.
  39. delta.Difference(&n.solve.pts.Sparse, &n.solve.prevPTS.Sparse)
  40. if delta.IsEmpty() {
  41. continue
  42. }
  43. if a.log != nil {
  44. fmt.Fprintf(a.log, "\t\tpts(n%d : %s) = %s + %s\n",
  45. id, n.typ, &delta, &n.solve.prevPTS)
  46. }
  47. n.solve.prevPTS.Copy(&n.solve.pts.Sparse)
  48. // Apply all resolution rules attached to n.
  49. a.solveConstraints(n, &delta)
  50. if a.log != nil {
  51. fmt.Fprintf(a.log, "\t\tpts(n%d) = %s\n", id, &n.solve.pts)
  52. }
  53. }
  54. if !a.nodes[0].solve.pts.IsEmpty() {
  55. panic(fmt.Sprintf("pts(0) is nonempty: %s", &a.nodes[0].solve.pts))
  56. }
  57. // Release working state (but keep final PTS).
  58. for _, n := range a.nodes {
  59. n.solve.complex = nil
  60. n.solve.copyTo.Clear()
  61. n.solve.prevPTS.Clear()
  62. }
  63. if a.log != nil {
  64. fmt.Fprintf(a.log, "Solver done\n")
  65. // Dump solution.
  66. for i, n := range a.nodes {
  67. if !n.solve.pts.IsEmpty() {
  68. fmt.Fprintf(a.log, "pts(n%d) = %s : %s\n", i, &n.solve.pts, n.typ)
  69. }
  70. }
  71. }
  72. stop("Solving")
  73. }
  74. // processNewConstraints takes the new constraints from a.constraints
  75. // and adds them to the graph, ensuring
  76. // that new constraints are applied to pre-existing labels and
  77. // that pre-existing constraints are applied to new labels.
  78. //
  79. func (a *analysis) processNewConstraints() {
  80. // Take the slice of new constraints.
  81. // (May grow during call to solveConstraints.)
  82. constraints := a.constraints
  83. a.constraints = nil
  84. // Initialize points-to sets from addr-of (base) constraints.
  85. for _, c := range constraints {
  86. if c, ok := c.(*addrConstraint); ok {
  87. dst := a.nodes[c.dst]
  88. dst.solve.pts.add(c.src)
  89. // Populate the worklist with nodes that point to
  90. // something initially (due to addrConstraints) and
  91. // have other constraints attached.
  92. // (A no-op in round 1.)
  93. if !dst.solve.copyTo.IsEmpty() || len(dst.solve.complex) > 0 {
  94. a.addWork(c.dst)
  95. }
  96. }
  97. }
  98. // Attach simple (copy) and complex constraints to nodes.
  99. var stale nodeset
  100. for _, c := range constraints {
  101. var id nodeid
  102. switch c := c.(type) {
  103. case *addrConstraint:
  104. // base constraints handled in previous loop
  105. continue
  106. case *copyConstraint:
  107. // simple (copy) constraint
  108. id = c.src
  109. a.nodes[id].solve.copyTo.add(c.dst)
  110. default:
  111. // complex constraint
  112. id = c.ptr()
  113. solve := a.nodes[id].solve
  114. solve.complex = append(solve.complex, c)
  115. }
  116. if n := a.nodes[id]; !n.solve.pts.IsEmpty() {
  117. if !n.solve.prevPTS.IsEmpty() {
  118. stale.add(id)
  119. }
  120. a.addWork(id)
  121. }
  122. }
  123. // Apply new constraints to pre-existing PTS labels.
  124. var space [50]int
  125. for _, id := range stale.AppendTo(space[:0]) {
  126. n := a.nodes[nodeid(id)]
  127. a.solveConstraints(n, &n.solve.prevPTS)
  128. }
  129. }
  130. // solveConstraints applies each resolution rule attached to node n to
  131. // the set of labels delta. It may generate new constraints in
  132. // a.constraints.
  133. //
  134. func (a *analysis) solveConstraints(n *node, delta *nodeset) {
  135. if delta.IsEmpty() {
  136. return
  137. }
  138. // Process complex constraints dependent on n.
  139. for _, c := range n.solve.complex {
  140. if a.log != nil {
  141. fmt.Fprintf(a.log, "\t\tconstraint %s\n", c)
  142. }
  143. c.solve(a, delta)
  144. }
  145. // Process copy constraints.
  146. var copySeen nodeset
  147. for _, x := range n.solve.copyTo.AppendTo(a.deltaSpace) {
  148. mid := nodeid(x)
  149. if copySeen.add(mid) {
  150. if a.nodes[mid].solve.pts.addAll(delta) {
  151. a.addWork(mid)
  152. }
  153. }
  154. }
  155. }
  156. // addLabel adds label to the points-to set of ptr and reports whether the set grew.
  157. func (a *analysis) addLabel(ptr, label nodeid) bool {
  158. b := a.nodes[ptr].solve.pts.add(label)
  159. if b && a.log != nil {
  160. fmt.Fprintf(a.log, "\t\tpts(n%d) += n%d\n", ptr, label)
  161. }
  162. return b
  163. }
  164. func (a *analysis) addWork(id nodeid) {
  165. a.work.Insert(int(id))
  166. if a.log != nil {
  167. fmt.Fprintf(a.log, "\t\twork: n%d\n", id)
  168. }
  169. }
  170. // onlineCopy adds a copy edge. It is called online, i.e. during
  171. // solving, so it adds edges and pts members directly rather than by
  172. // instantiating a 'constraint'.
  173. //
  174. // The size of the copy is implicitly 1.
  175. // It returns true if pts(dst) changed.
  176. //
  177. func (a *analysis) onlineCopy(dst, src nodeid) bool {
  178. if dst != src {
  179. if nsrc := a.nodes[src]; nsrc.solve.copyTo.add(dst) {
  180. if a.log != nil {
  181. fmt.Fprintf(a.log, "\t\t\tdynamic copy n%d <- n%d\n", dst, src)
  182. }
  183. // TODO(adonovan): most calls to onlineCopy
  184. // are followed by addWork, possibly batched
  185. // via a 'changed' flag; see if there's a
  186. // noticeable penalty to calling addWork here.
  187. return a.nodes[dst].solve.pts.addAll(&nsrc.solve.pts)
  188. }
  189. }
  190. return false
  191. }
  192. // Returns sizeof.
  193. // Implicitly adds nodes to worklist.
  194. //
  195. // TODO(adonovan): now that we support a.copy() during solving, we
  196. // could eliminate onlineCopyN, but it's much slower. Investigate.
  197. //
  198. func (a *analysis) onlineCopyN(dst, src nodeid, sizeof uint32) uint32 {
  199. for i := uint32(0); i < sizeof; i++ {
  200. if a.onlineCopy(dst, src) {
  201. a.addWork(dst)
  202. }
  203. src++
  204. dst++
  205. }
  206. return sizeof
  207. }
  208. func (c *loadConstraint) solve(a *analysis, delta *nodeset) {
  209. var changed bool
  210. for _, x := range delta.AppendTo(a.deltaSpace) {
  211. k := nodeid(x)
  212. koff := k + nodeid(c.offset)
  213. if a.onlineCopy(c.dst, koff) {
  214. changed = true
  215. }
  216. }
  217. if changed {
  218. a.addWork(c.dst)
  219. }
  220. }
  221. func (c *storeConstraint) solve(a *analysis, delta *nodeset) {
  222. for _, x := range delta.AppendTo(a.deltaSpace) {
  223. k := nodeid(x)
  224. koff := k + nodeid(c.offset)
  225. if a.onlineCopy(koff, c.src) {
  226. a.addWork(koff)
  227. }
  228. }
  229. }
  230. func (c *offsetAddrConstraint) solve(a *analysis, delta *nodeset) {
  231. dst := a.nodes[c.dst]
  232. for _, x := range delta.AppendTo(a.deltaSpace) {
  233. k := nodeid(x)
  234. if dst.solve.pts.add(k + nodeid(c.offset)) {
  235. a.addWork(c.dst)
  236. }
  237. }
  238. }
  239. func (c *typeFilterConstraint) solve(a *analysis, delta *nodeset) {
  240. for _, x := range delta.AppendTo(a.deltaSpace) {
  241. ifaceObj := nodeid(x)
  242. tDyn, _, indirect := a.taggedValue(ifaceObj)
  243. if indirect {
  244. // TODO(adonovan): we'll need to implement this
  245. // when we start creating indirect tagged objects.
  246. panic("indirect tagged object")
  247. }
  248. if types.AssignableTo(tDyn, c.typ) {
  249. if a.addLabel(c.dst, ifaceObj) {
  250. a.addWork(c.dst)
  251. }
  252. }
  253. }
  254. }
  255. func (c *untagConstraint) solve(a *analysis, delta *nodeset) {
  256. predicate := types.AssignableTo
  257. if c.exact {
  258. predicate = types.Identical
  259. }
  260. for _, x := range delta.AppendTo(a.deltaSpace) {
  261. ifaceObj := nodeid(x)
  262. tDyn, v, indirect := a.taggedValue(ifaceObj)
  263. if indirect {
  264. // TODO(adonovan): we'll need to implement this
  265. // when we start creating indirect tagged objects.
  266. panic("indirect tagged object")
  267. }
  268. if predicate(tDyn, c.typ) {
  269. // Copy payload sans tag to dst.
  270. //
  271. // TODO(adonovan): opt: if tDyn is
  272. // nonpointerlike we can skip this entire
  273. // constraint, perhaps. We only care about
  274. // pointers among the fields.
  275. a.onlineCopyN(c.dst, v, a.sizeof(tDyn))
  276. }
  277. }
  278. }
  279. func (c *invokeConstraint) solve(a *analysis, delta *nodeset) {
  280. for _, x := range delta.AppendTo(a.deltaSpace) {
  281. ifaceObj := nodeid(x)
  282. tDyn, v, indirect := a.taggedValue(ifaceObj)
  283. if indirect {
  284. // TODO(adonovan): we may need to implement this if
  285. // we ever apply invokeConstraints to reflect.Value PTSs,
  286. // e.g. for (reflect.Value).Call.
  287. panic("indirect tagged object")
  288. }
  289. // Look up the concrete method.
  290. fn := a.prog.LookupMethod(tDyn, c.method.Pkg(), c.method.Name())
  291. if fn == nil {
  292. panic(fmt.Sprintf("n%d: no ssa.Function for %s", c.iface, c.method))
  293. }
  294. sig := fn.Signature
  295. fnObj := a.globalobj[fn] // dynamic calls use shared contour
  296. if fnObj == 0 {
  297. // a.objectNode(fn) was not called during gen phase.
  298. panic(fmt.Sprintf("a.globalobj[%s]==nil", fn))
  299. }
  300. // Make callsite's fn variable point to identity of
  301. // concrete method. (There's no need to add it to
  302. // worklist since it never has attached constraints.)
  303. a.addLabel(c.params, fnObj)
  304. // Extract value and connect to method's receiver.
  305. // Copy payload to method's receiver param (arg0).
  306. arg0 := a.funcParams(fnObj)
  307. recvSize := a.sizeof(sig.Recv().Type())
  308. a.onlineCopyN(arg0, v, recvSize)
  309. src := c.params + 1 // skip past identity
  310. dst := arg0 + nodeid(recvSize)
  311. // Copy caller's argument block to method formal parameters.
  312. paramsSize := a.sizeof(sig.Params())
  313. a.onlineCopyN(dst, src, paramsSize)
  314. src += nodeid(paramsSize)
  315. dst += nodeid(paramsSize)
  316. // Copy method results to caller's result block.
  317. resultsSize := a.sizeof(sig.Results())
  318. a.onlineCopyN(src, dst, resultsSize)
  319. }
  320. }
  321. func (c *addrConstraint) solve(a *analysis, delta *nodeset) {
  322. panic("addr is not a complex constraint")
  323. }
  324. func (c *copyConstraint) solve(a *analysis, delta *nodeset) {
  325. panic("copy is not a complex constraint")
  326. }