Typically, the prosecution will go for the "bigger fish" of the two cases and has the discretion not to charge a defendant with a crime if they so choose. It might be that, if a Peeping Tom was to turn in evidence that a neighbor was killing his wife, the prosecutor could strike a deal with the Tom to not charge him with the crime in exchange for his testimony at trial. The rational is that of the two, the murderer is more of a threat to society than the Tom, and if the Peeping Tom must go unpunished. Nothing in the deal needs to immunize the Tom from any future crimes, and police will certainly be knocking on his door if new crimes in the neighborhood come to light. If a witness is granted immunity from prosecution, they cannot invoke their 5th amendment rights on the witness stand, since everything they testify to is never going to be prosecuted.
Another example is that Bob is on trial for the Murder of Alice. The cornor testifies that Alice died no later than 9:30 pm on the night of the 10th. Bob's defense is that he couldn't have done it, because the 10th was on a Friday and Friday's are "Blackjack and Hookers" night, which typically starts at 8 pm and goes well into Saturday morning. Bob not only does this, but produces pictures of him and the girls playing Black Jack with a date stamp of the 10th and all the Hookers in picture testify that Bob was definately there that night and didn't leave for any gap of time long enough to kill Alice. Now, Bob may have some legal problems stemming from the illegal gambling and the solicitation of prostitution, sure... but they are comparitively minor crimes to murdering a human! Bob may even have an explination that will help catch the real killer (such as the coat with his name and blood were left at the scene, which Bob could realize that he cut himself making snacks for the girls in prep for Black Jack and Hookers night, and his brother Charlie offered to clean the coat for him... which tracks with some of the DNA partially matching Bob's DNA... The prosecution could only prove a case against Charlie if Bob testifies and might offer a deal that they will grant him immunity from prosecution for Black Jack and Hookers night if Bob testifies against his brother. Bob can agree and testify, or Bob can refuse and be charged for his crimes (but not the crime he didn't commit). The evidence against Bob entered into the record at his trial for his Friday tradition is now a matter of public record and can be used to link Charlie to Alice without Bob's testimony. The deal is advantageous to Bob because he isn't going to jail on a murder charge and it's advantageous to the prosecution because it means they have to successfully convict Bob before they can even move on Charlie.
In the event that the prosecution discovered the exculpatory evidence through an unconstitutional search and seizure or "Fruit of the Poisonous Tree" (evidence that, while possibley be legally obtained, could only have been done so as a result of evidence obtained by an illegal search) would be barred from introduction by the Prosecution, but not the defendant, since the defendant is not bound by the Constitution (As a general rule, the Bill of Rights are the people's Negative Rights against the Government, which means that the government may not violate those rights unless given permission by the the individual. This comes up in trials from time to time. For example, a defendant may "waive" their right to a trial by jury and instead be granted a bench trial (the Judge alone makes the verdict). One can "waive" their right to Free Speech by not speaking (although U.S. Free Speech Laws are so broad, this is only a technicallity as no speech is a protected form of speech. It's also protected while under oath in that the defendant need not say anything at all and have the presumption of innocence. Or they can waive that by taking the stand and testifying.