The only evidence that it has no advantgage for the poppy is that you don't know of one, right?
Im not an expert (despite the name ;) but I would imagine that opiates are alkaloids which act as a natural insecticide of sorts, much like nicotine does for tobacco.
Someone correct me if I am wrong.
According to this article, it's basically an autoimmune defense enhancement mechanism:
We identified a novel metabolic system of morphine in the opium poppy (Papaver somniferum L.). In response to stress, morphine is quickly metabolized to bismorphine consisting of two morphine units, followed by accumulation in the cell wall. This bismorphine binds predominantly to pectins, which possess high galacturonic acid residue contents, through ionical bonds. Our newly developed method using artificial polysaccharides demonstrated that bismorphine bridges are formed between the two amino groups of bismorphine and the carboxyl groups of galacturonic acid residues, resulting in cross-linking of galacturonic acid-containing polysaccharides to each other. The ability of bismorphine to cross-link pectins is much higher than that of Ca2+, which also acts as a cross-linker of these polysaccharides. Furthermore, we confirmed that cross-linking of pectins through bismorphine bridges leads to resistance against hydrolysis by pectinases. These results indicated that production of bismorphine is a defense response of the opium poppy. Bismorphine formation is catalyzed by anionic peroxidase that pre-exists in the capsules and leaves of opium poppies. The constitutive presence of morphine, together with bismorphine-forming peroxidase, enables the opium poppy to rapidly induce the defense system.
"and how does this fit in with the whole evolution selects life best suited for survival-why waste valuable energy producing opium when it has no benefit for the plant??
"
That one's easy. Opium is a poisonous alkaloid. Many plants produce poisonous alkaloids. They have evolved to protect the plant from being eaten by insects. Even the common milkweed produces poisonous alkaloids. The interesting thing is that a species of caterpillar has evolved that is not harmed by those alkaloids, so it eats milkweed exclusively.
Isn't nature amazing?