/// <summary> /// Simplifies needlessly complicated conditional branch structures. /// </summary> public ILRef SimplifyConditionalBranch(ILRef ifBranch) { if (ifBranch == null) { throw new ArgumentNullException("ifBranch"); } if (!ifBranch.Valid) { throw new InvalidOperationException("Cannot use an invalid ILRef!"); } CodeInstruction conditionalBranch = GetInsn(ifBranch); CodeInstruction nonconditionalBranch = GetInsn(ifBranch.GetRefByOffset(1)); if (conditionalBranch.opcode.FlowControl == FlowControl.Cond_Branch && nonconditionalBranch.opcode.FlowControl == FlowControl.Branch && GetInsn(ifBranch.GetRefByOffset(2)).labels.Contains((Label)conditionalBranch.operand) && conditionalBranch.opcode != GetReverseConditionalCode(conditionalBranch.opcode)) { conditionalBranch.opcode = GetReverseConditionalCode(conditionalBranch.opcode); conditionalBranch.operand = nonconditionalBranch.operand; RemoveInsn(ifBranch.GetRefByOffset(1)); } UpdateMetadata(); return(ifBranch); }
/// <summary> /// Removes the set of instructions in the block started by the given <see cref="ILRef"/>. /// Accepts values of <paramref name="count"/> higher than the number of instructions. /// </summary> public void RemoveInsns(ILRef target, int count) { ShiftInsns(target.GetRefByOffset(count), -count); }