1 // Copyright 2015 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.
8 "cmd/compile/internal/base"
9 "cmd/compile/internal/logopt"
10 "cmd/compile/internal/types"
12 "cmd/internal/obj/s390x"
24 type deadValueChoice bool
27 leaveDeadValues deadValueChoice = false
28 removeDeadValues = true
31 // deadcode indicates whether rewrite should try to remove any values that become dead.
32 func applyRewrite(f *Func, rb blockRewriter, rv valueRewriter, deadcode deadValueChoice) {
33 // repeat rewrites until we find no more rewrites
34 pendingLines := f.cachedLineStarts // Holds statement boundaries that need to be moved to a new value/block
38 fmt.Printf("%s: rewriting for %s\n", f.pass.name, f.Name)
41 var states map[string]bool
45 for _, b := range f.Blocks {
50 b0.Succs = append([]Edge{}, b.Succs...) // make a new copy, not aliasing
52 for i, c := range b.ControlValues() {
55 b.ReplaceControl(i, c)
61 fmt.Printf("rewriting %s -> %s\n", b0.LongString(), b.LongString())
64 for j, v := range b.Values {
69 v0.Args = append([]*Value{}, v.Args...) // make a new copy, not aliasing
71 if v.Uses == 0 && v.removeable() {
72 if v.Op != OpInvalid && deadcode == removeDeadValues {
73 // Reset any values that are now unused, so that we decrement
74 // the use count of all of its arguments.
75 // Not quite a deadcode pass, because it does not handle cycles.
76 // But it should help Uses==1 rules to fire.
80 // No point rewriting values which aren't used.
84 vchange := phielimValue(v)
85 if vchange && debug > 1 {
86 fmt.Printf("rewriting %s -> %s\n", v0.LongString(), v.LongString())
89 // Eliminate copy inputs.
90 // If any copy input becomes unused, mark it
91 // as invalid and discard its argument. Repeat
92 // recursively on the discarded argument.
93 // This phase helps remove phantom "dead copy" uses
94 // of a value so that a x.Uses==1 rule condition
96 for i, a := range v.Args {
102 // If a, a copy, has a line boundary indicator, attempt to find a new value
103 // to hold it. The first candidate is the value that will replace a (aa),
104 // if it shares the same block and line and is eligible.
105 // The second option is v, which has a as an input. Because aa is earlier in
106 // the data flow, it is the better choice.
107 if a.Pos.IsStmt() == src.PosIsStmt {
108 if aa.Block == a.Block && aa.Pos.Line() == a.Pos.Line() && aa.Pos.IsStmt() != src.PosNotStmt {
109 aa.Pos = aa.Pos.WithIsStmt()
110 } else if v.Block == a.Block && v.Pos.Line() == a.Pos.Line() && v.Pos.IsStmt() != src.PosNotStmt {
111 v.Pos = v.Pos.WithIsStmt()
113 // Record the lost line and look for a new home after all rewrites are complete.
114 // TODO: it's possible (in FOR loops, in particular) for statement boundaries for the same
115 // line to appear in more than one block, but only one block is stored, so if both end
116 // up here, then one will be lost.
117 pendingLines.set(a.Pos, int32(a.Block.ID))
119 a.Pos = a.Pos.WithNotStmt()
128 if vchange && debug > 1 {
129 fmt.Printf("rewriting %s -> %s\n", v0.LongString(), v.LongString())
132 // apply rewrite function
135 // If value changed to a poor choice for a statement boundary, move the boundary
136 if v.Pos.IsStmt() == src.PosIsStmt {
137 if k := nextGoodStatementIndex(v, j, b); k != j {
138 v.Pos = v.Pos.WithNotStmt()
139 b.Values[k].Pos = b.Values[k].Pos.WithIsStmt()
144 change = change || vchange
145 if vchange && debug > 1 {
146 fmt.Printf("rewriting %s -> %s\n", v0.LongString(), v.LongString())
150 if !change && !deadChange {
154 if (iters > 1000 || debug >= 2) && change {
155 // We've done a suspiciously large number of rewrites (or we're in debug mode).
156 // As of Sep 2021, 90% of rewrites complete in 4 iterations or fewer
157 // and the maximum value encountered during make.bash is 12.
158 // Start checking for cycles. (This is too expensive to do routinely.)
159 // Note: we avoid this path for deadChange-only iterations, to fix #51639.
161 states = make(map[string]bool)
164 if _, ok := states[h]; ok {
165 // We've found a cycle.
166 // To diagnose it, set debug to 2 and start again,
167 // so that we'll print all rules applied until we complete another cycle.
168 // If debug is already >= 2, we've already done that, so it's time to crash.
171 states = make(map[string]bool)
173 f.Fatalf("rewrite cycle detected")
179 // remove clobbered values
180 for _, b := range f.Blocks {
182 for i, v := range b.Values {
184 if v.Op == OpInvalid {
185 if v.Pos.IsStmt() == src.PosIsStmt {
186 pendingLines.set(vl, int32(b.ID))
191 if v.Pos.IsStmt() != src.PosNotStmt && !notStmtBoundary(v.Op) && pendingLines.get(vl) == int32(b.ID) {
192 pendingLines.remove(vl)
193 v.Pos = v.Pos.WithIsStmt()
200 if pendingLines.get(b.Pos) == int32(b.ID) {
201 b.Pos = b.Pos.WithIsStmt()
202 pendingLines.remove(b.Pos)
208 // Common functions called from rewriting rules
210 func is64BitFloat(t *types.Type) bool {
211 return t.Size() == 8 && t.IsFloat()
214 func is32BitFloat(t *types.Type) bool {
215 return t.Size() == 4 && t.IsFloat()
218 func is64BitInt(t *types.Type) bool {
219 return t.Size() == 8 && t.IsInteger()
222 func is32BitInt(t *types.Type) bool {
223 return t.Size() == 4 && t.IsInteger()
226 func is16BitInt(t *types.Type) bool {
227 return t.Size() == 2 && t.IsInteger()
230 func is8BitInt(t *types.Type) bool {
231 return t.Size() == 1 && t.IsInteger()
234 func isPtr(t *types.Type) bool {
235 return t.IsPtrShaped()
238 func isSigned(t *types.Type) bool {
242 // mergeSym merges two symbolic offsets. There is no real merging of
243 // offsets, we just pick the non-nil one.
244 func mergeSym(x, y Sym) Sym {
251 panic(fmt.Sprintf("mergeSym with two non-nil syms %v %v", x, y))
254 func canMergeSym(x, y Sym) bool {
255 return x == nil || y == nil
258 // canMergeLoadClobber reports whether the load can be merged into target without
259 // invalidating the schedule.
260 // It also checks that the other non-load argument x is something we
261 // are ok with clobbering.
262 func canMergeLoadClobber(target, load, x *Value) bool {
263 // The register containing x is going to get clobbered.
264 // Don't merge if we still need the value of x.
265 // We don't have liveness information here, but we can
266 // approximate x dying with:
267 // 1) target is x's only use.
268 // 2) target is not in a deeper loop than x.
272 loopnest := x.Block.Func.loopnest()
273 loopnest.calculateDepths()
274 if loopnest.depth(target.Block.ID) > loopnest.depth(x.Block.ID) {
277 return canMergeLoad(target, load)
280 // canMergeLoad reports whether the load can be merged into target without
281 // invalidating the schedule.
282 func canMergeLoad(target, load *Value) bool {
283 if target.Block.ID != load.Block.ID {
284 // If the load is in a different block do not merge it.
288 // We can't merge the load into the target if the load
289 // has more than one use.
294 mem := load.MemoryArg()
296 // We need the load's memory arg to still be alive at target. That
297 // can't be the case if one of target's args depends on a memory
298 // state that is a successor of load's memory arg.
300 // For example, it would be invalid to merge load into target in
301 // the following situation because newmem has killed oldmem
302 // before target is reached:
303 // load = read ... oldmem
304 // newmem = write ... oldmem
305 // arg0 = read ... newmem
306 // target = add arg0 load
308 // If the argument comes from a different block then we can exclude
309 // it immediately because it must dominate load (which is in the
310 // same block as target).
312 for _, a := range target.Args {
313 if a != load && a.Block.ID == target.Block.ID {
314 args = append(args, a)
318 // memPreds contains memory states known to be predecessors of load's
319 // memory state. It is lazily initialized.
320 var memPreds map[*Value]bool
321 for i := 0; len(args) > 0; i++ {
324 // Give up if we have done a lot of iterations.
327 v := args[len(args)-1]
328 args = args[:len(args)-1]
329 if target.Block.ID != v.Block.ID {
330 // Since target and load are in the same block
331 // we can stop searching when we leave the block.
335 // A Phi implies we have reached the top of the block.
336 // The memory phi, if it exists, is always
337 // the first logical store in the block.
340 if v.Type.IsTuple() && v.Type.FieldType(1).IsMemory() {
341 // We could handle this situation however it is likely
345 if v.Op.SymEffect()&SymAddr != 0 {
346 // This case prevents an operation that calculates the
347 // address of a local variable from being forced to schedule
348 // before its corresponding VarDef.
354 // We don't want to combine the CMPQ with the load, because
355 // that would force the CMPQ to schedule before the VARDEF, which
356 // in turn requires the LEAQ to schedule before the VARDEF.
359 if v.Type.IsMemory() {
361 // Initialise a map containing memory states
362 // known to be predecessors of load's memory
364 memPreds = make(map[*Value]bool)
367 for i := 0; i < limit; i++ {
369 // The memory phi, if it exists, is always
370 // the first logical store in the block.
373 if m.Block.ID != target.Block.ID {
376 if !m.Type.IsMemory() {
380 if len(m.Args) == 0 {
387 // We can merge if v is a predecessor of mem.
389 // For example, we can merge load into target in the
390 // following scenario:
393 // load = read ... mem
394 // target = add x load
400 if len(v.Args) > 0 && v.Args[len(v.Args)-1] == mem {
401 // If v takes mem as an input then we know mem
402 // is valid at this point.
405 for _, a := range v.Args {
406 if target.Block.ID == a.Block.ID {
407 args = append(args, a)
415 // isSameCall reports whether sym is the same as the given named symbol
416 func isSameCall(sym interface{}, name string) bool {
417 fn := sym.(*AuxCall).Fn
418 return fn != nil && fn.String() == name
421 // canLoadUnaligned reports if the achitecture supports unaligned load operations
422 func canLoadUnaligned(c *Config) bool {
423 return c.ctxt.Arch.Alignment == 1
426 // nlz returns the number of leading zeros.
427 func nlz64(x int64) int { return bits.LeadingZeros64(uint64(x)) }
428 func nlz32(x int32) int { return bits.LeadingZeros32(uint32(x)) }
429 func nlz16(x int16) int { return bits.LeadingZeros16(uint16(x)) }
430 func nlz8(x int8) int { return bits.LeadingZeros8(uint8(x)) }
432 // ntzX returns the number of trailing zeros.
433 func ntz64(x int64) int { return bits.TrailingZeros64(uint64(x)) }
434 func ntz32(x int32) int { return bits.TrailingZeros32(uint32(x)) }
435 func ntz16(x int16) int { return bits.TrailingZeros16(uint16(x)) }
436 func ntz8(x int8) int { return bits.TrailingZeros8(uint8(x)) }
438 func oneBit(x int64) bool { return x&(x-1) == 0 && x != 0 }
439 func oneBit8(x int8) bool { return x&(x-1) == 0 && x != 0 }
440 func oneBit16(x int16) bool { return x&(x-1) == 0 && x != 0 }
441 func oneBit32(x int32) bool { return x&(x-1) == 0 && x != 0 }
442 func oneBit64(x int64) bool { return x&(x-1) == 0 && x != 0 }
444 // nto returns the number of trailing ones.
445 func nto(x int64) int64 {
446 return int64(ntz64(^x))
449 // logX returns logarithm of n base 2.
450 // n must be a positive power of 2 (isPowerOfTwoX returns true).
451 func log8(n int8) int64 {
452 return int64(bits.Len8(uint8(n))) - 1
454 func log16(n int16) int64 {
455 return int64(bits.Len16(uint16(n))) - 1
457 func log32(n int32) int64 {
458 return int64(bits.Len32(uint32(n))) - 1
460 func log64(n int64) int64 {
461 return int64(bits.Len64(uint64(n))) - 1
464 // log2uint32 returns logarithm in base 2 of uint32(n), with log2(0) = -1.
466 func log2uint32(n int64) int64 {
467 return int64(bits.Len32(uint32(n))) - 1
470 // isPowerOfTwo functions report whether n is a power of 2.
471 func isPowerOfTwo8(n int8) bool {
472 return n > 0 && n&(n-1) == 0
474 func isPowerOfTwo16(n int16) bool {
475 return n > 0 && n&(n-1) == 0
477 func isPowerOfTwo32(n int32) bool {
478 return n > 0 && n&(n-1) == 0
480 func isPowerOfTwo64(n int64) bool {
481 return n > 0 && n&(n-1) == 0
484 // isUint64PowerOfTwo reports whether uint64(n) is a power of 2.
485 func isUint64PowerOfTwo(in int64) bool {
487 return n > 0 && n&(n-1) == 0
490 // isUint32PowerOfTwo reports whether uint32(n) is a power of 2.
491 func isUint32PowerOfTwo(in int64) bool {
492 n := uint64(uint32(in))
493 return n > 0 && n&(n-1) == 0
496 // is32Bit reports whether n can be represented as a signed 32 bit integer.
497 func is32Bit(n int64) bool {
498 return n == int64(int32(n))
501 // is16Bit reports whether n can be represented as a signed 16 bit integer.
502 func is16Bit(n int64) bool {
503 return n == int64(int16(n))
506 // is8Bit reports whether n can be represented as a signed 8 bit integer.
507 func is8Bit(n int64) bool {
508 return n == int64(int8(n))
511 // isU8Bit reports whether n can be represented as an unsigned 8 bit integer.
512 func isU8Bit(n int64) bool {
513 return n == int64(uint8(n))
516 // isU12Bit reports whether n can be represented as an unsigned 12 bit integer.
517 func isU12Bit(n int64) bool {
518 return 0 <= n && n < (1<<12)
521 // isU16Bit reports whether n can be represented as an unsigned 16 bit integer.
522 func isU16Bit(n int64) bool {
523 return n == int64(uint16(n))
526 // isU32Bit reports whether n can be represented as an unsigned 32 bit integer.
527 func isU32Bit(n int64) bool {
528 return n == int64(uint32(n))
531 // is20Bit reports whether n can be represented as a signed 20 bit integer.
532 func is20Bit(n int64) bool {
533 return -(1<<19) <= n && n < (1<<19)
536 // b2i translates a boolean value to 0 or 1 for assigning to auxInt.
537 func b2i(b bool) int64 {
544 // b2i32 translates a boolean value to 0 or 1.
545 func b2i32(b bool) int32 {
552 // shiftIsBounded reports whether (left/right) shift Value v is known to be bounded.
553 // A shift is bounded if it is shifting by less than the width of the shifted value.
554 func shiftIsBounded(v *Value) bool {
558 // canonLessThan returns whether x is "ordered" less than y, for purposes of normalizing
559 // generated code as much as possible.
560 func canonLessThan(x, y *Value) bool {
564 if !x.Pos.SameFileAndLine(y.Pos) {
565 return x.Pos.Before(y.Pos)
570 // truncate64Fto32F converts a float64 value to a float32 preserving the bit pattern
571 // of the mantissa. It will panic if the truncation results in lost information.
572 func truncate64Fto32F(f float64) float32 {
573 if !isExactFloat32(f) {
574 panic("truncate64Fto32F: truncation is not exact")
579 // NaN bit patterns aren't necessarily preserved across conversion
580 // instructions so we need to do the conversion manually.
581 b := math.Float64bits(f)
582 m := b & ((1 << 52) - 1) // mantissa (a.k.a. significand)
583 // | sign | exponent | mantissa |
584 r := uint32(((b >> 32) & (1 << 31)) | 0x7f800000 | (m >> (52 - 23)))
585 return math.Float32frombits(r)
588 // extend32Fto64F converts a float32 value to a float64 value preserving the bit
589 // pattern of the mantissa.
590 func extend32Fto64F(f float32) float64 {
591 if !math.IsNaN(float64(f)) {
594 // NaN bit patterns aren't necessarily preserved across conversion
595 // instructions so we need to do the conversion manually.
596 b := uint64(math.Float32bits(f))
597 // | sign | exponent | mantissa |
598 r := ((b << 32) & (1 << 63)) | (0x7ff << 52) | ((b & 0x7fffff) << (52 - 23))
599 return math.Float64frombits(r)
602 // DivisionNeedsFixUp reports whether the division needs fix-up code.
603 func DivisionNeedsFixUp(v *Value) bool {
607 // auxFrom64F encodes a float64 value so it can be stored in an AuxInt.
608 func auxFrom64F(f float64) int64 {
610 panic("can't encode a NaN in AuxInt field")
612 return int64(math.Float64bits(f))
615 // auxFrom32F encodes a float32 value so it can be stored in an AuxInt.
616 func auxFrom32F(f float32) int64 {
618 panic("can't encode a NaN in AuxInt field")
620 return int64(math.Float64bits(extend32Fto64F(f)))
623 // auxTo32F decodes a float32 from the AuxInt value provided.
624 func auxTo32F(i int64) float32 {
625 return truncate64Fto32F(math.Float64frombits(uint64(i)))
628 // auxTo64F decodes a float64 from the AuxInt value provided.
629 func auxTo64F(i int64) float64 {
630 return math.Float64frombits(uint64(i))
633 func auxIntToBool(i int64) bool {
639 func auxIntToInt8(i int64) int8 {
642 func auxIntToInt16(i int64) int16 {
645 func auxIntToInt32(i int64) int32 {
648 func auxIntToInt64(i int64) int64 {
651 func auxIntToUint8(i int64) uint8 {
654 func auxIntToFloat32(i int64) float32 {
655 return float32(math.Float64frombits(uint64(i)))
657 func auxIntToFloat64(i int64) float64 {
658 return math.Float64frombits(uint64(i))
660 func auxIntToValAndOff(i int64) ValAndOff {
663 func auxIntToArm64BitField(i int64) arm64BitField {
664 return arm64BitField(i)
666 func auxIntToInt128(x int64) int128 {
668 panic("nonzero int128 not allowed")
672 func auxIntToFlagConstant(x int64) flagConstant {
673 return flagConstant(x)
676 func auxIntToOp(cc int64) Op {
680 func boolToAuxInt(b bool) int64 {
686 func int8ToAuxInt(i int8) int64 {
689 func int16ToAuxInt(i int16) int64 {
692 func int32ToAuxInt(i int32) int64 {
695 func int64ToAuxInt(i int64) int64 {
698 func uint8ToAuxInt(i uint8) int64 {
699 return int64(int8(i))
701 func float32ToAuxInt(f float32) int64 {
702 return int64(math.Float64bits(float64(f)))
704 func float64ToAuxInt(f float64) int64 {
705 return int64(math.Float64bits(f))
707 func valAndOffToAuxInt(v ValAndOff) int64 {
710 func arm64BitFieldToAuxInt(v arm64BitField) int64 {
713 func int128ToAuxInt(x int128) int64 {
715 panic("nonzero int128 not allowed")
719 func flagConstantToAuxInt(x flagConstant) int64 {
723 func opToAuxInt(o Op) int64 {
727 // Aux is an interface to hold miscellaneous data in Blocks and Values.
732 // stringAux wraps string values for use in Aux.
733 type stringAux string
735 func (stringAux) CanBeAnSSAAux() {}
737 func auxToString(i Aux) string {
738 return string(i.(stringAux))
740 func auxToSym(i Aux) Sym {
741 // TODO: kind of a hack - allows nil interface through
745 func auxToType(i Aux) *types.Type {
746 return i.(*types.Type)
748 func auxToCall(i Aux) *AuxCall {
751 func auxToS390xCCMask(i Aux) s390x.CCMask {
752 return i.(s390x.CCMask)
754 func auxToS390xRotateParams(i Aux) s390x.RotateParams {
755 return i.(s390x.RotateParams)
758 func StringToAux(s string) Aux {
761 func symToAux(s Sym) Aux {
764 func callToAux(s *AuxCall) Aux {
767 func typeToAux(t *types.Type) Aux {
770 func s390xCCMaskToAux(c s390x.CCMask) Aux {
773 func s390xRotateParamsToAux(r s390x.RotateParams) Aux {
777 // uaddOvf reports whether unsigned a+b would overflow.
778 func uaddOvf(a, b int64) bool {
779 return uint64(a)+uint64(b) < uint64(a)
782 // loadLSymOffset simulates reading a word at an offset into a
783 // read-only symbol's runtime memory. If it would read a pointer to
784 // another symbol, that symbol is returned. Otherwise, it returns nil.
785 func loadLSymOffset(lsym *obj.LSym, offset int64) *obj.LSym {
786 if lsym.Type != objabi.SRODATA {
790 for _, r := range lsym.R {
791 if int64(r.Off) == offset && r.Type&^objabi.R_WEAK == objabi.R_ADDR && r.Add == 0 {
799 // de-virtualize an InterLECall
800 // 'sym' is the symbol for the itab
801 func devirtLESym(v *Value, aux Aux, sym Sym, offset int64) *obj.LSym {
802 n, ok := sym.(*obj.LSym)
807 lsym := loadLSymOffset(n, offset)
808 if f := v.Block.Func; f.pass.debug > 0 {
810 f.Warnl(v.Pos, "de-virtualizing call")
812 f.Warnl(v.Pos, "couldn't de-virtualize call")
818 func devirtLECall(v *Value, sym *obj.LSym) *Value {
819 v.Op = OpStaticLECall
820 auxcall := v.Aux.(*AuxCall)
824 copy(v.Args[0:], v.Args[1:])
825 v.Args[len(v.Args)-1] = nil // aid GC
826 v.Args = v.Args[:len(v.Args)-1]
830 // isSamePtr reports whether p1 and p2 point to the same address.
831 func isSamePtr(p1, p2 *Value) bool {
840 return p1.AuxInt == p2.AuxInt && isSamePtr(p1.Args[0], p2.Args[0])
841 case OpAddr, OpLocalAddr:
842 // OpAddr's 0th arg is either OpSP or OpSB, which means that it is uniquely identified by its Op.
843 // Checking for value equality only works after [z]cse has run.
844 return p1.Aux == p2.Aux && p1.Args[0].Op == p2.Args[0].Op
846 return p1.Args[1] == p2.Args[1] && isSamePtr(p1.Args[0], p2.Args[0])
851 func isStackPtr(v *Value) bool {
852 for v.Op == OpOffPtr || v.Op == OpAddPtr {
855 return v.Op == OpSP || v.Op == OpLocalAddr
858 // disjoint reports whether the memory region specified by [p1:p1+n1)
859 // does not overlap with [p2:p2+n2).
860 // A return value of false does not imply the regions overlap.
861 func disjoint(p1 *Value, n1 int64, p2 *Value, n2 int64) bool {
862 if n1 == 0 || n2 == 0 {
868 baseAndOffset := func(ptr *Value) (base *Value, offset int64) {
869 base, offset = ptr, 0
870 for base.Op == OpOffPtr {
871 offset += base.AuxInt
876 p1, off1 := baseAndOffset(p1)
877 p2, off2 := baseAndOffset(p2)
878 if isSamePtr(p1, p2) {
879 return !overlap(off1, n1, off2, n2)
881 // p1 and p2 are not the same, so if they are both OpAddrs then
882 // they point to different variables.
883 // If one pointer is on the stack and the other is an argument
884 // then they can't overlap.
886 case OpAddr, OpLocalAddr:
887 if p2.Op == OpAddr || p2.Op == OpLocalAddr || p2.Op == OpSP {
890 return (p2.Op == OpArg || p2.Op == OpArgIntReg) && p1.Args[0].Op == OpSP
891 case OpArg, OpArgIntReg:
892 if p2.Op == OpSP || p2.Op == OpLocalAddr {
896 return p2.Op == OpAddr || p2.Op == OpLocalAddr || p2.Op == OpArg || p2.Op == OpArgIntReg || p2.Op == OpSP
901 // moveSize returns the number of bytes an aligned MOV instruction moves
902 func moveSize(align int64, c *Config) int64 {
904 case align%8 == 0 && c.PtrSize == 8:
914 // mergePoint finds a block among a's blocks which dominates b and is itself
915 // dominated by all of a's blocks. Returns nil if it can't find one.
916 // Might return nil even if one does exist.
917 func mergePoint(b *Block, a ...*Value) *Block {
918 // Walk backward from b looking for one of the a's blocks.
924 for _, x := range a {
929 if len(b.Preds) > 1 {
930 // Don't know which way to go back. Abort.
936 return nil // too far away
938 // At this point, r is the first value in a that we find by walking backwards.
939 // if we return anything, r will be it.
942 // Keep going, counting the other a's that we find. They must all dominate r.
945 for _, x := range a {
951 // Found all of a in a backwards walk. We can return r.
954 if len(b.Preds) > 1 {
961 return nil // too far away
964 // clobber invalidates values. Returns true.
965 // clobber is used by rewrite rules to:
967 // A) make sure the values are really dead and never used again.
968 // B) decrement use counts of the values' args.
969 func clobber(vv ...*Value) bool {
970 for _, v := range vv {
972 // Note: leave v.Block intact. The Block field is used after clobber.
977 // clobberIfDead resets v when use count is 1. Returns true.
978 // clobberIfDead is used by rewrite rules to decrement
979 // use counts of v's args when v is dead and never used.
980 func clobberIfDead(v *Value) bool {
984 // Note: leave v.Block intact. The Block field is used after clobberIfDead.
988 // noteRule is an easy way to track if a rule is matched when writing
989 // new ones. Make the rule of interest also conditional on
991 // noteRule("note to self: rule of interest matched")
993 // and that message will print when the rule matches.
994 func noteRule(s string) bool {
999 // countRule increments Func.ruleMatches[key].
1000 // If Func.ruleMatches is non-nil at the end
1001 // of compilation, it will be printed to stdout.
1002 // This is intended to make it easier to find which functions
1003 // which contain lots of rules matches when developing new rules.
1004 func countRule(v *Value, key string) bool {
1006 if f.ruleMatches == nil {
1007 f.ruleMatches = make(map[string]int)
1009 f.ruleMatches[key]++
1013 // warnRule generates compiler debug output with string s when
1014 // v is not in autogenerated code, cond is true and the rule has fired.
1015 func warnRule(cond bool, v *Value, s string) bool {
1016 if pos := v.Pos; pos.Line() > 1 && cond {
1017 v.Block.Func.Warnl(pos, s)
1022 // for a pseudo-op like (LessThan x), extract x
1023 func flagArg(v *Value) *Value {
1024 if len(v.Args) != 1 || !v.Args[0].Type.IsFlags() {
1030 // arm64Negate finds the complement to an ARM64 condition code,
1031 // for example !Equal -> NotEqual or !LessThan -> GreaterEqual
1033 // For floating point, it's more subtle because NaN is unordered. We do
1034 // !LessThanF -> NotLessThanF, the latter takes care of NaNs.
1035 func arm64Negate(op Op) Op {
1037 case OpARM64LessThan:
1038 return OpARM64GreaterEqual
1039 case OpARM64LessThanU:
1040 return OpARM64GreaterEqualU
1041 case OpARM64GreaterThan:
1042 return OpARM64LessEqual
1043 case OpARM64GreaterThanU:
1044 return OpARM64LessEqualU
1045 case OpARM64LessEqual:
1046 return OpARM64GreaterThan
1047 case OpARM64LessEqualU:
1048 return OpARM64GreaterThanU
1049 case OpARM64GreaterEqual:
1050 return OpARM64LessThan
1051 case OpARM64GreaterEqualU:
1052 return OpARM64LessThanU
1054 return OpARM64NotEqual
1055 case OpARM64NotEqual:
1057 case OpARM64LessThanF:
1058 return OpARM64NotLessThanF
1059 case OpARM64NotLessThanF:
1060 return OpARM64LessThanF
1061 case OpARM64LessEqualF:
1062 return OpARM64NotLessEqualF
1063 case OpARM64NotLessEqualF:
1064 return OpARM64LessEqualF
1065 case OpARM64GreaterThanF:
1066 return OpARM64NotGreaterThanF
1067 case OpARM64NotGreaterThanF:
1068 return OpARM64GreaterThanF
1069 case OpARM64GreaterEqualF:
1070 return OpARM64NotGreaterEqualF
1071 case OpARM64NotGreaterEqualF:
1072 return OpARM64GreaterEqualF
1074 panic("unreachable")
1078 // arm64Invert evaluates (InvertFlags op), which
1079 // is the same as altering the condition codes such
1080 // that the same result would be produced if the arguments
1081 // to the flag-generating instruction were reversed, e.g.
1082 // (InvertFlags (CMP x y)) -> (CMP y x)
1083 func arm64Invert(op Op) Op {
1085 case OpARM64LessThan:
1086 return OpARM64GreaterThan
1087 case OpARM64LessThanU:
1088 return OpARM64GreaterThanU
1089 case OpARM64GreaterThan:
1090 return OpARM64LessThan
1091 case OpARM64GreaterThanU:
1092 return OpARM64LessThanU
1093 case OpARM64LessEqual:
1094 return OpARM64GreaterEqual
1095 case OpARM64LessEqualU:
1096 return OpARM64GreaterEqualU
1097 case OpARM64GreaterEqual:
1098 return OpARM64LessEqual
1099 case OpARM64GreaterEqualU:
1100 return OpARM64LessEqualU
1101 case OpARM64Equal, OpARM64NotEqual:
1103 case OpARM64LessThanF:
1104 return OpARM64GreaterThanF
1105 case OpARM64GreaterThanF:
1106 return OpARM64LessThanF
1107 case OpARM64LessEqualF:
1108 return OpARM64GreaterEqualF
1109 case OpARM64GreaterEqualF:
1110 return OpARM64LessEqualF
1111 case OpARM64NotLessThanF:
1112 return OpARM64NotGreaterThanF
1113 case OpARM64NotGreaterThanF:
1114 return OpARM64NotLessThanF
1115 case OpARM64NotLessEqualF:
1116 return OpARM64NotGreaterEqualF
1117 case OpARM64NotGreaterEqualF:
1118 return OpARM64NotLessEqualF
1120 panic("unreachable")
1124 // evaluate an ARM64 op against a flags value
1125 // that is potentially constant; return 1 for true,
1126 // -1 for false, and 0 for not constant.
1127 func ccARM64Eval(op Op, flags *Value) int {
1129 if fop == OpARM64InvertFlags {
1130 return -ccARM64Eval(op, flags.Args[0])
1132 if fop != OpARM64FlagConstant {
1135 fc := flagConstant(flags.AuxInt)
1136 b2i := func(b bool) int {
1145 case OpARM64NotEqual:
1147 case OpARM64LessThan:
1149 case OpARM64LessThanU:
1150 return b2i(fc.ult())
1151 case OpARM64GreaterThan:
1153 case OpARM64GreaterThanU:
1154 return b2i(fc.ugt())
1155 case OpARM64LessEqual:
1157 case OpARM64LessEqualU:
1158 return b2i(fc.ule())
1159 case OpARM64GreaterEqual:
1161 case OpARM64GreaterEqualU:
1162 return b2i(fc.uge())
1167 // logRule logs the use of the rule s. This will only be enabled if
1168 // rewrite rules were generated with the -log option, see gen/rulegen.go.
1169 func logRule(s string) {
1170 if ruleFile == nil {
1171 // Open a log file to write log to. We open in append
1172 // mode because all.bash runs the compiler lots of times,
1173 // and we want the concatenation of all of those logs.
1174 // This means, of course, that users need to rm the old log
1175 // to get fresh data.
1176 // TODO: all.bash runs compilers in parallel. Need to synchronize logging somehow?
1177 w, err := os.OpenFile(filepath.Join(os.Getenv("GOROOT"), "src", "rulelog"),
1178 os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
1184 _, err := fmt.Fprintln(ruleFile, s)
1190 var ruleFile io.Writer
1192 func min(x, y int64) int64 {
1199 func isConstZero(v *Value) bool {
1203 case OpConst64, OpConst32, OpConst16, OpConst8, OpConstBool, OpConst32F, OpConst64F:
1204 return v.AuxInt == 0
1209 // reciprocalExact64 reports whether 1/c is exactly representable.
1210 func reciprocalExact64(c float64) bool {
1211 b := math.Float64bits(c)
1212 man := b & (1<<52 - 1)
1214 return false // not a power of 2, denormal, or NaN
1216 exp := b >> 52 & (1<<11 - 1)
1217 // exponent bias is 0x3ff. So taking the reciprocal of a number
1218 // changes the exponent to 0x7fe-exp.
1223 return false // ±inf
1225 return false // exponent is not representable
1231 // reciprocalExact32 reports whether 1/c is exactly representable.
1232 func reciprocalExact32(c float32) bool {
1233 b := math.Float32bits(c)
1234 man := b & (1<<23 - 1)
1236 return false // not a power of 2, denormal, or NaN
1238 exp := b >> 23 & (1<<8 - 1)
1239 // exponent bias is 0x7f. So taking the reciprocal of a number
1240 // changes the exponent to 0xfe-exp.
1245 return false // ±inf
1247 return false // exponent is not representable
1253 // check if an immediate can be directly encoded into an ARM's instruction
1254 func isARMImmRot(v uint32) bool {
1255 for i := 0; i < 16; i++ {
1265 // overlap reports whether the ranges given by the given offset and
1266 // size pairs overlap.
1267 func overlap(offset1, size1, offset2, size2 int64) bool {
1268 if offset1 >= offset2 && offset2+size2 > offset1 {
1271 if offset2 >= offset1 && offset1+size1 > offset2 {
1277 func areAdjacentOffsets(off1, off2, size int64) bool {
1278 return off1+size == off2 || off1 == off2+size
1281 // check if value zeroes out upper 32-bit of 64-bit register.
1282 // depth limits recursion depth. In AMD64.rules 3 is used as limit,
1283 // because it catches same amount of cases as 4.
1284 func zeroUpper32Bits(x *Value, depth int) bool {
1286 case OpAMD64MOVLconst, OpAMD64MOVLload, OpAMD64MOVLQZX, OpAMD64MOVLloadidx1,
1287 OpAMD64MOVWload, OpAMD64MOVWloadidx1, OpAMD64MOVBload, OpAMD64MOVBloadidx1,
1288 OpAMD64MOVLloadidx4, OpAMD64ADDLload, OpAMD64SUBLload, OpAMD64ANDLload,
1289 OpAMD64ORLload, OpAMD64XORLload, OpAMD64CVTTSD2SL,
1290 OpAMD64ADDL, OpAMD64ADDLconst, OpAMD64SUBL, OpAMD64SUBLconst,
1291 OpAMD64ANDL, OpAMD64ANDLconst, OpAMD64ORL, OpAMD64ORLconst,
1292 OpAMD64XORL, OpAMD64XORLconst, OpAMD64NEGL, OpAMD64NOTL,
1293 OpAMD64SHRL, OpAMD64SHRLconst, OpAMD64SARL, OpAMD64SARLconst,
1294 OpAMD64SHLL, OpAMD64SHLLconst:
1297 return x.Type.Size() == 4
1298 case OpPhi, OpSelect0, OpSelect1:
1299 // Phis can use each-other as an arguments, instead of tracking visited values,
1300 // just limit recursion depth.
1304 for i := range x.Args {
1305 if !zeroUpper32Bits(x.Args[i], depth-1) {
1315 // zeroUpper48Bits is similar to zeroUpper32Bits, but for upper 48 bits
1316 func zeroUpper48Bits(x *Value, depth int) bool {
1318 case OpAMD64MOVWQZX, OpAMD64MOVWload, OpAMD64MOVWloadidx1, OpAMD64MOVWloadidx2:
1321 return x.Type.Size() == 2
1322 case OpPhi, OpSelect0, OpSelect1:
1323 // Phis can use each-other as an arguments, instead of tracking visited values,
1324 // just limit recursion depth.
1328 for i := range x.Args {
1329 if !zeroUpper48Bits(x.Args[i], depth-1) {
1339 // zeroUpper56Bits is similar to zeroUpper32Bits, but for upper 56 bits
1340 func zeroUpper56Bits(x *Value, depth int) bool {
1342 case OpAMD64MOVBQZX, OpAMD64MOVBload, OpAMD64MOVBloadidx1:
1345 return x.Type.Size() == 1
1346 case OpPhi, OpSelect0, OpSelect1:
1347 // Phis can use each-other as an arguments, instead of tracking visited values,
1348 // just limit recursion depth.
1352 for i := range x.Args {
1353 if !zeroUpper56Bits(x.Args[i], depth-1) {
1363 // isInlinableMemmove reports whether the given arch performs a Move of the given size
1364 // faster than memmove. It will only return true if replacing the memmove with a Move is
1365 // safe, either because Move is small or because the arguments are disjoint.
1366 // This is used as a check for replacing memmove with Move ops.
1367 func isInlinableMemmove(dst, src *Value, sz int64, c *Config) bool {
1368 // It is always safe to convert memmove into Move when its arguments are disjoint.
1369 // Move ops may or may not be faster for large sizes depending on how the platform
1370 // lowers them, so we only perform this optimization on platforms that we know to
1371 // have fast Move ops.
1374 return sz <= 16 || (sz < 1024 && disjoint(dst, sz, src, sz))
1375 case "386", "arm64":
1377 case "s390x", "ppc64", "ppc64le":
1378 return sz <= 8 || disjoint(dst, sz, src, sz)
1379 case "arm", "mips", "mips64", "mipsle", "mips64le":
1385 // logLargeCopy logs the occurrence of a large copy.
1386 // The best place to do this is in the rewrite rules where the size of the move is easy to find.
1387 // "Large" is arbitrarily chosen to be 128 bytes; this may change.
1388 func logLargeCopy(v *Value, s int64) bool {
1392 if logopt.Enabled() {
1393 logopt.LogOpt(v.Pos, "copy", "lower", v.Block.Func.Name, fmt.Sprintf("%d bytes", s))
1398 // hasSmallRotate reports whether the architecture has rotate instructions
1399 // for sizes < 32-bit. This is used to decide whether to promote some rotations.
1400 func hasSmallRotate(c *Config) bool {
1402 case "amd64", "386":
1409 func newPPC64ShiftAuxInt(sh, mb, me, sz int64) int32 {
1410 if sh < 0 || sh >= sz {
1411 panic("PPC64 shift arg sh out of range")
1413 if mb < 0 || mb >= sz {
1414 panic("PPC64 shift arg mb out of range")
1416 if me < 0 || me >= sz {
1417 panic("PPC64 shift arg me out of range")
1419 return int32(sh<<16 | mb<<8 | me)
1422 func GetPPC64Shiftsh(auxint int64) int64 {
1423 return int64(int8(auxint >> 16))
1426 func GetPPC64Shiftmb(auxint int64) int64 {
1427 return int64(int8(auxint >> 8))
1430 func GetPPC64Shiftme(auxint int64) int64 {
1431 return int64(int8(auxint))
1434 // Test if this value can encoded as a mask for a rlwinm like
1435 // operation. Masks can also extend from the msb and wrap to
1436 // the lsb too. That is, the valid masks are 32 bit strings
1437 // of the form: 0..01..10..0 or 1..10..01..1 or 1...1
1438 func isPPC64WordRotateMask(v64 int64) bool {
1439 // Isolate rightmost 1 (if none 0) and add.
1442 // Likewise, for the wrapping case.
1444 vpn := (vn & -vn) + vn
1445 return (v&vp == 0 || vn&vpn == 0) && v != 0
1448 // Compress mask and shift into single value of the form
1449 // me | mb<<8 | rotate<<16 | nbits<<24 where me and mb can
1450 // be used to regenerate the input mask.
1451 func encodePPC64RotateMask(rotate, mask, nbits int64) int64 {
1452 var mb, me, mbn, men int
1454 // Determine boundaries and then decode them
1455 if mask == 0 || ^mask == 0 || rotate >= nbits {
1456 panic("Invalid PPC64 rotate mask")
1457 } else if nbits == 32 {
1458 mb = bits.LeadingZeros32(uint32(mask))
1459 me = 32 - bits.TrailingZeros32(uint32(mask))
1460 mbn = bits.LeadingZeros32(^uint32(mask))
1461 men = 32 - bits.TrailingZeros32(^uint32(mask))
1463 mb = bits.LeadingZeros64(uint64(mask))
1464 me = 64 - bits.TrailingZeros64(uint64(mask))
1465 mbn = bits.LeadingZeros64(^uint64(mask))
1466 men = 64 - bits.TrailingZeros64(^uint64(mask))
1468 // Check for a wrapping mask (e.g bits at 0 and 63)
1469 if mb == 0 && me == int(nbits) {
1470 // swap the inverted values
1474 return int64(me) | int64(mb<<8) | int64(rotate<<16) | int64(nbits<<24)
1477 // The inverse operation of encodePPC64RotateMask. The values returned as
1478 // mb and me satisfy the POWER ISA definition of MASK(x,y) where MASK(mb,me) = mask.
1479 func DecodePPC64RotateMask(sauxint int64) (rotate, mb, me int64, mask uint64) {
1480 auxint := uint64(sauxint)
1481 rotate = int64((auxint >> 16) & 0xFF)
1482 mb = int64((auxint >> 8) & 0xFF)
1483 me = int64((auxint >> 0) & 0xFF)
1484 nbits := int64((auxint >> 24) & 0xFF)
1485 mask = ((1 << uint(nbits-mb)) - 1) ^ ((1 << uint(nbits-me)) - 1)
1490 mask = uint64(uint32(mask))
1493 // Fixup ME to match ISA definition. The second argument to MASK(..,me)
1495 me = (me - 1) & (nbits - 1)
1499 // This verifies that the mask is a set of
1500 // consecutive bits including the least
1502 func isPPC64ValidShiftMask(v int64) bool {
1503 if (v != 0) && ((v+1)&v) == 0 {
1509 func getPPC64ShiftMaskLength(v int64) int64 {
1510 return int64(bits.Len64(uint64(v)))
1513 // Decompose a shift right into an equivalent rotate/mask,
1514 // and return mask & m.
1515 func mergePPC64RShiftMask(m, s, nbits int64) int64 {
1516 smask := uint64((1<<uint(nbits))-1) >> uint(s)
1517 return m & int64(smask)
1520 // Combine (ANDconst [m] (SRWconst [s])) into (RLWINM [y]) or return 0
1521 func mergePPC64AndSrwi(m, s int64) int64 {
1522 mask := mergePPC64RShiftMask(m, s, 32)
1523 if !isPPC64WordRotateMask(mask) {
1526 return encodePPC64RotateMask((32-s)&31, mask, 32)
1529 // Test if a shift right feeding into a CLRLSLDI can be merged into RLWINM.
1530 // Return the encoded RLWINM constant, or 0 if they cannot be merged.
1531 func mergePPC64ClrlsldiSrw(sld, srw int64) int64 {
1532 mask_1 := uint64(0xFFFFFFFF >> uint(srw))
1533 // for CLRLSLDI, it's more convient to think of it as a mask left bits then rotate left.
1534 mask_2 := uint64(0xFFFFFFFFFFFFFFFF) >> uint(GetPPC64Shiftmb(int64(sld)))
1536 // Rewrite mask to apply after the final left shift.
1537 mask_3 := (mask_1 & mask_2) << uint(GetPPC64Shiftsh(sld))
1540 r_2 := GetPPC64Shiftsh(sld)
1541 r_3 := (r_1 + r_2) & 31 // This can wrap.
1543 if uint64(uint32(mask_3)) != mask_3 || mask_3 == 0 {
1546 return encodePPC64RotateMask(int64(r_3), int64(mask_3), 32)
1549 // Test if a RLWINM feeding into a CLRLSLDI can be merged into RLWINM. Return
1550 // the encoded RLWINM constant, or 0 if they cannot be merged.
1551 func mergePPC64ClrlsldiRlwinm(sld int32, rlw int64) int64 {
1552 r_1, _, _, mask_1 := DecodePPC64RotateMask(rlw)
1553 // for CLRLSLDI, it's more convient to think of it as a mask left bits then rotate left.
1554 mask_2 := uint64(0xFFFFFFFFFFFFFFFF) >> uint(GetPPC64Shiftmb(int64(sld)))
1556 // combine the masks, and adjust for the final left shift.
1557 mask_3 := (mask_1 & mask_2) << uint(GetPPC64Shiftsh(int64(sld)))
1558 r_2 := GetPPC64Shiftsh(int64(sld))
1559 r_3 := (r_1 + r_2) & 31 // This can wrap.
1561 // Verify the result is still a valid bitmask of <= 32 bits.
1562 if !isPPC64WordRotateMask(int64(mask_3)) || uint64(uint32(mask_3)) != mask_3 {
1565 return encodePPC64RotateMask(r_3, int64(mask_3), 32)
1568 // Compute the encoded RLWINM constant from combining (SLDconst [sld] (SRWconst [srw] x)),
1569 // or return 0 if they cannot be combined.
1570 func mergePPC64SldiSrw(sld, srw int64) int64 {
1571 if sld > srw || srw >= 32 {
1574 mask_r := uint32(0xFFFFFFFF) >> uint(srw)
1575 mask_l := uint32(0xFFFFFFFF) >> uint(sld)
1576 mask := (mask_r & mask_l) << uint(sld)
1577 return encodePPC64RotateMask((32-srw+sld)&31, int64(mask), 32)
1580 // Convenience function to rotate a 32 bit constant value by another constant.
1581 func rotateLeft32(v, rotate int64) int64 {
1582 return int64(bits.RotateLeft32(uint32(v), int(rotate)))
1585 func rotateRight64(v, rotate int64) int64 {
1586 return int64(bits.RotateLeft64(uint64(v), int(-rotate)))
1589 // encodes the lsb and width for arm(64) bitfield ops into the expected auxInt format.
1590 func armBFAuxInt(lsb, width int64) arm64BitField {
1591 if lsb < 0 || lsb > 63 {
1592 panic("ARM(64) bit field lsb constant out of range")
1594 if width < 1 || lsb+width > 64 {
1595 panic("ARM(64) bit field width constant out of range")
1597 return arm64BitField(width | lsb<<8)
1600 // returns the lsb part of the auxInt field of arm64 bitfield ops.
1601 func (bfc arm64BitField) getARM64BFlsb() int64 {
1602 return int64(uint64(bfc) >> 8)
1605 // returns the width part of the auxInt field of arm64 bitfield ops.
1606 func (bfc arm64BitField) getARM64BFwidth() int64 {
1607 return int64(bfc) & 0xff
1610 // checks if mask >> rshift applied at lsb is a valid arm64 bitfield op mask.
1611 func isARM64BFMask(lsb, mask, rshift int64) bool {
1612 shiftedMask := int64(uint64(mask) >> uint64(rshift))
1613 return shiftedMask != 0 && isPowerOfTwo64(shiftedMask+1) && nto(shiftedMask)+lsb < 64
1616 // returns the bitfield width of mask >> rshift for arm64 bitfield ops
1617 func arm64BFWidth(mask, rshift int64) int64 {
1618 shiftedMask := int64(uint64(mask) >> uint64(rshift))
1619 if shiftedMask == 0 {
1620 panic("ARM64 BF mask is zero")
1622 return nto(shiftedMask)
1625 // sizeof returns the size of t in bytes.
1626 // It will panic if t is not a *types.Type.
1627 func sizeof(t interface{}) int64 {
1628 return t.(*types.Type).Size()
1631 // registerizable reports whether t is a primitive type that fits in
1632 // a register. It assumes float64 values will always fit into registers
1633 // even if that isn't strictly true.
1634 func registerizable(b *Block, typ *types.Type) bool {
1635 if typ.IsPtrShaped() || typ.IsFloat() {
1638 if typ.IsInteger() {
1639 return typ.Size() <= b.Func.Config.RegSize
1644 // needRaceCleanup reports whether this call to racefuncenter/exit isn't needed.
1645 func needRaceCleanup(sym *AuxCall, v *Value) bool {
1650 if !isSameCall(sym, "runtime.racefuncenter") && !isSameCall(sym, "runtime.racefuncexit") {
1653 for _, b := range f.Blocks {
1654 for _, v := range b.Values {
1656 case OpStaticCall, OpStaticLECall:
1657 // Check for racefuncenter will encounter racefuncexit and vice versa.
1658 // Allow calls to panic*
1659 s := v.Aux.(*AuxCall).Fn.String()
1661 case "runtime.racefuncenter", "runtime.racefuncexit",
1662 "runtime.panicdivide", "runtime.panicwrap",
1663 "runtime.panicshift":
1666 // If we encountered any call, we need to keep racefunc*,
1667 // for accurate stacktraces.
1669 case OpPanicBounds, OpPanicExtend:
1670 // Note: these are panic generators that are ok (like the static calls above).
1671 case OpClosureCall, OpInterCall, OpClosureLECall, OpInterLECall:
1672 // We must keep the race functions if there are any other call types.
1677 if isSameCall(sym, "runtime.racefuncenter") {
1678 // TODO REGISTER ABI this needs to be cleaned up.
1679 // If we're removing racefuncenter, remove its argument as well.
1680 if v.Args[0].Op != OpStore {
1681 if v.Op == OpStaticLECall {
1682 // there is no store, yet.
1687 mem := v.Args[0].Args[2]
1688 v.Args[0].reset(OpCopy)
1689 v.Args[0].AddArg(mem)
1694 // symIsRO reports whether sym is a read-only global.
1695 func symIsRO(sym interface{}) bool {
1696 lsym := sym.(*obj.LSym)
1697 return lsym.Type == objabi.SRODATA && len(lsym.R) == 0
1700 // symIsROZero reports whether sym is a read-only global whose data contains all zeros.
1701 func symIsROZero(sym Sym) bool {
1702 lsym := sym.(*obj.LSym)
1703 if lsym.Type != objabi.SRODATA || len(lsym.R) != 0 {
1706 for _, b := range lsym.P {
1714 // read8 reads one byte from the read-only global sym at offset off.
1715 func read8(sym interface{}, off int64) uint8 {
1716 lsym := sym.(*obj.LSym)
1717 if off >= int64(len(lsym.P)) || off < 0 {
1718 // Invalid index into the global sym.
1719 // This can happen in dead code, so we don't want to panic.
1720 // Just return any value, it will eventually get ignored.
1727 // read16 reads two bytes from the read-only global sym at offset off.
1728 func read16(sym interface{}, off int64, byteorder binary.ByteOrder) uint16 {
1729 lsym := sym.(*obj.LSym)
1730 // lsym.P is written lazily.
1731 // Bytes requested after the end of lsym.P are 0.
1733 if 0 <= off && off < int64(len(lsym.P)) {
1736 buf := make([]byte, 2)
1738 return byteorder.Uint16(buf)
1741 // read32 reads four bytes from the read-only global sym at offset off.
1742 func read32(sym interface{}, off int64, byteorder binary.ByteOrder) uint32 {
1743 lsym := sym.(*obj.LSym)
1745 if 0 <= off && off < int64(len(lsym.P)) {
1748 buf := make([]byte, 4)
1750 return byteorder.Uint32(buf)
1753 // read64 reads eight bytes from the read-only global sym at offset off.
1754 func read64(sym interface{}, off int64, byteorder binary.ByteOrder) uint64 {
1755 lsym := sym.(*obj.LSym)
1757 if 0 <= off && off < int64(len(lsym.P)) {
1760 buf := make([]byte, 8)
1762 return byteorder.Uint64(buf)
1765 // sequentialAddresses reports true if it can prove that x + n == y
1766 func sequentialAddresses(x, y *Value, n int64) bool {
1767 if x.Op == Op386ADDL && y.Op == Op386LEAL1 && y.AuxInt == n && y.Aux == nil &&
1768 (x.Args[0] == y.Args[0] && x.Args[1] == y.Args[1] ||
1769 x.Args[0] == y.Args[1] && x.Args[1] == y.Args[0]) {
1772 if x.Op == Op386LEAL1 && y.Op == Op386LEAL1 && y.AuxInt == x.AuxInt+n && x.Aux == y.Aux &&
1773 (x.Args[0] == y.Args[0] && x.Args[1] == y.Args[1] ||
1774 x.Args[0] == y.Args[1] && x.Args[1] == y.Args[0]) {
1777 if x.Op == OpAMD64ADDQ && y.Op == OpAMD64LEAQ1 && y.AuxInt == n && y.Aux == nil &&
1778 (x.Args[0] == y.Args[0] && x.Args[1] == y.Args[1] ||
1779 x.Args[0] == y.Args[1] && x.Args[1] == y.Args[0]) {
1782 if x.Op == OpAMD64LEAQ1 && y.Op == OpAMD64LEAQ1 && y.AuxInt == x.AuxInt+n && x.Aux == y.Aux &&
1783 (x.Args[0] == y.Args[0] && x.Args[1] == y.Args[1] ||
1784 x.Args[0] == y.Args[1] && x.Args[1] == y.Args[0]) {
1790 // flagConstant represents the result of a compile-time comparison.
1791 // The sense of these flags does not necessarily represent the hardware's notion
1792 // of a flags register - these are just a compile-time construct.
1793 // We happen to match the semantics to those of arm/arm64.
1794 // Note that these semantics differ from x86: the carry flag has the opposite
1795 // sense on a subtraction!
1797 // On amd64, C=1 represents a borrow, e.g. SBB on amd64 does x - y - C.
1798 // On arm64, C=0 represents a borrow, e.g. SBC on arm64 does x - y - ^C.
1799 // (because it does x + ^y + C).
1801 // See https://en.wikipedia.org/wiki/Carry_flag#Vs._borrow_flag
1802 type flagConstant uint8
1804 // N reports whether the result of an operation is negative (high bit set).
1805 func (fc flagConstant) N() bool {
1809 // Z reports whether the result of an operation is 0.
1810 func (fc flagConstant) Z() bool {
1814 // C reports whether an unsigned add overflowed (carry), or an
1815 // unsigned subtract did not underflow (borrow).
1816 func (fc flagConstant) C() bool {
1820 // V reports whether a signed operation overflowed or underflowed.
1821 func (fc flagConstant) V() bool {
1825 func (fc flagConstant) eq() bool {
1828 func (fc flagConstant) ne() bool {
1831 func (fc flagConstant) lt() bool {
1832 return fc.N() != fc.V()
1834 func (fc flagConstant) le() bool {
1835 return fc.Z() || fc.lt()
1837 func (fc flagConstant) gt() bool {
1838 return !fc.Z() && fc.ge()
1840 func (fc flagConstant) ge() bool {
1841 return fc.N() == fc.V()
1843 func (fc flagConstant) ult() bool {
1846 func (fc flagConstant) ule() bool {
1847 return fc.Z() || fc.ult()
1849 func (fc flagConstant) ugt() bool {
1850 return !fc.Z() && fc.uge()
1852 func (fc flagConstant) uge() bool {
1856 func (fc flagConstant) ltNoov() bool {
1857 return fc.lt() && !fc.V()
1859 func (fc flagConstant) leNoov() bool {
1860 return fc.le() && !fc.V()
1862 func (fc flagConstant) gtNoov() bool {
1863 return fc.gt() && !fc.V()
1865 func (fc flagConstant) geNoov() bool {
1866 return fc.ge() && !fc.V()
1869 func (fc flagConstant) String() string {
1870 return fmt.Sprintf("N=%v,Z=%v,C=%v,V=%v", fc.N(), fc.Z(), fc.C(), fc.V())
1873 type flagConstantBuilder struct {
1880 func (fcs flagConstantBuilder) encode() flagConstant {
1897 // Note: addFlags(x,y) != subFlags(x,-y) in some situations:
1898 // - the results of the C flag are different
1899 // - the results of the V flag when y==minint are different
1901 // addFlags64 returns the flags that would be set from computing x+y.
1902 func addFlags64(x, y int64) flagConstant {
1903 var fcb flagConstantBuilder
1906 fcb.C = uint64(x+y) < uint64(x)
1907 fcb.V = x >= 0 && y >= 0 && x+y < 0 || x < 0 && y < 0 && x+y >= 0
1911 // subFlags64 returns the flags that would be set from computing x-y.
1912 func subFlags64(x, y int64) flagConstant {
1913 var fcb flagConstantBuilder
1916 fcb.C = uint64(y) <= uint64(x) // This code follows the arm carry flag model.
1917 fcb.V = x >= 0 && y < 0 && x-y < 0 || x < 0 && y >= 0 && x-y >= 0
1921 // addFlags32 returns the flags that would be set from computing x+y.
1922 func addFlags32(x, y int32) flagConstant {
1923 var fcb flagConstantBuilder
1926 fcb.C = uint32(x+y) < uint32(x)
1927 fcb.V = x >= 0 && y >= 0 && x+y < 0 || x < 0 && y < 0 && x+y >= 0
1931 // subFlags32 returns the flags that would be set from computing x-y.
1932 func subFlags32(x, y int32) flagConstant {
1933 var fcb flagConstantBuilder
1936 fcb.C = uint32(y) <= uint32(x) // This code follows the arm carry flag model.
1937 fcb.V = x >= 0 && y < 0 && x-y < 0 || x < 0 && y >= 0 && x-y >= 0
1941 // logicFlags64 returns flags set to the sign/zeroness of x.
1942 // C and V are set to false.
1943 func logicFlags64(x int64) flagConstant {
1944 var fcb flagConstantBuilder
1950 // logicFlags32 returns flags set to the sign/zeroness of x.
1951 // C and V are set to false.
1952 func logicFlags32(x int32) flagConstant {
1953 var fcb flagConstantBuilder
1959 func makeJumpTableSym(b *Block) *obj.LSym {
1960 s := base.Ctxt.Lookup(fmt.Sprintf("%s.jump%d", b.Func.fe.LSym(), b.ID))
1961 s.Set(obj.AttrDuplicateOK, true)