CodeoptimizationOptimizationistheprocessoftransformingapieceofcodetomakeitmoreefficient(eitherintermsoftimeorspace)withoutchangingitsoutputorside-effects.Theonlydifferencevisibletothecode’susershouldbethatitrunsfasterand/orconsumeslessmemory.CodeoptimizationOptimizationisthefieldwheremostcompilerresearchisdonetoday.High-qualityoptimizationismoreofanartthanascience.ManyoptimizationproblemsareNP-completeandthusmostoptimizationalgorithmsrelyonheuristicsandapproximations.Itmaybepossibletocomeupwithacasewhereaparticularalgorithmfailstoproducebettercodeorperhapsevenmakesitworse.However,thealgorithmstendtodoratherwelloverall.WhenandinwhatrepresentationtooptimizeThereareavarietyoftacticsforattackingoptimization.Sometechniquesareappliedtotheintermediatecode,Othersareappliedaspartoffinalcodegeneration—choosingwhichinstructionstoemit,howtoallocateregisters…Andstillotheroptimizationsmayoccurafterfinalcodegeneration,attemptingtore-worktheassemblycodeitselfintosomethingmoreefficient.Optimizationcanbeverycomplexandtime-consuming;itofteninvolvesmultiplesub-phases,someofwhichareappliedmorethanonce.Mostcompilersallowoptimizationtobeturnedofftospeedupcompilation(gccevenhasspecificflagstoturnonandoffindividualoptimizations).CodeoptimizationLocaloptimizationsOptimizationsperformedexclusivelywithinabasicblockarecalled“localoptimizations”.CodemotionCodemotion(alsocalledcodehoisting)unifiessequencesofcodecommontooneormorebasicblockstoreducecodesizeandpotentiallyavoidexpensivere-evaluation.Themostcommonformofcodemotionisloop-invariantcodemotionthatmovesstatementsthatevaluatetothesamevalueeveryiterationofthelooptosomewhereoutsidetheloop.GlobaloptimizationsLocaloptimizations•Constantfolding•Constantpropagation•Algebraicsimplificationandreassociation•Operatorstrengthreduction•Copypropagation•Deadcodeelimination•Commonsub-expressioneliminationControl-flowanalysisThecompilerhasdoneanenormousamountofanalysis,butitstilldoesn’treallyknowhowtheprogramdoeswhatitdoes.Incontrol-flowanal...