Skip to content

Instantly share code, notes, and snippets.

@mewmew
Last active December 24, 2019 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mewmew/05d625cc101d81e4f7b79469f41105fe to your computer and use it in GitHub Desktop.
Save mewmew/05d625cc101d81e4f7b79469f41105fe to your computer and use it in GitHub Desktop.
Example of use-tracking API with modification of value.
// nopTrunc propagates From values of truncation instructions that have no
// effect on type (where From and To type are the same).
//
// Example:
//
// before:
// %10 = xor i32 %src2, %src1
// %11 = trunc i32 %10 to i32
// %12 = xor i32 %sum, %11
//
// after:
// %10 = xor i32 %src2, %src1
// %11 = trunc i32 %10 to i32
// ; note use of %10 instead of %11, as trunc was applied from i32 to i32
// %12 = xor i32 %sum, %10
func nopTrunc(f *ir.Func) {
uses := irutil.FuncUses(nil, f)
for _, use := range uses {
truncInst, ok := (*use.Val).(*ir.InstTrunc)
if !ok {
continue
}
// Check if types of From and To are equal.
if !types.Equal(truncInst.From.Type(), truncInst.To) {
continue
}
// Types of From and To are equal, propagate From and skip trunc
// instruction.
*use.Val = truncInst.From
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment