tree-ssa-loop-ivopts.c 188 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087
  1. /* Induction variable optimizations.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This pass tries to find the optimal set of induction variables for the loop.
  16. It optimizes just the basic linear induction variables (although adding
  17. support for other types should not be too hard). It includes the
  18. optimizations commonly known as strength reduction, induction variable
  19. coalescing and induction variable elimination. It does it in the
  20. following steps:
  21. 1) The interesting uses of induction variables are found. This includes
  22. -- uses of induction variables in non-linear expressions
  23. -- addresses of arrays
  24. -- comparisons of induction variables
  25. 2) Candidates for the induction variables are found. This includes
  26. -- old induction variables
  27. -- the variables defined by expressions derived from the "interesting
  28. uses" above
  29. 3) The optimal (w.r. to a cost function) set of variables is chosen. The
  30. cost function assigns a cost to sets of induction variables and consists
  31. of three parts:
  32. -- The use costs. Each of the interesting uses chooses the best induction
  33. variable in the set and adds its cost to the sum. The cost reflects
  34. the time spent on modifying the induction variables value to be usable
  35. for the given purpose (adding base and offset for arrays, etc.).
  36. -- The variable costs. Each of the variables has a cost assigned that
  37. reflects the costs associated with incrementing the value of the
  38. variable. The original variables are somewhat preferred.
  39. -- The set cost. Depending on the size of the set, extra cost may be
  40. added to reflect register pressure.
  41. All the costs are defined in a machine-specific way, using the target
  42. hooks and machine descriptions to determine them.
  43. 4) The trees are transformed to use the new variables, the dead code is
  44. removed.
  45. All of this is done loop by loop. Doing it globally is theoretically
  46. possible, it might give a better performance and it might enable us
  47. to decide costs more precisely, but getting all the interactions right
  48. would be complicated. */
  49. #include "config.h"
  50. #include "system.h"
  51. #include "coretypes.h"
  52. #include "tm.h"
  53. #include "hash-set.h"
  54. #include "machmode.h"
  55. #include "vec.h"
  56. #include "double-int.h"
  57. #include "input.h"
  58. #include "alias.h"
  59. #include "symtab.h"
  60. #include "wide-int.h"
  61. #include "inchash.h"
  62. #include "tree.h"
  63. #include "fold-const.h"
  64. #include "stor-layout.h"
  65. #include "tm_p.h"
  66. #include "predict.h"
  67. #include "hard-reg-set.h"
  68. #include "function.h"
  69. #include "dominance.h"
  70. #include "cfg.h"
  71. #include "basic-block.h"
  72. #include "gimple-pretty-print.h"
  73. #include "hash-map.h"
  74. #include "hash-table.h"
  75. #include "tree-ssa-alias.h"
  76. #include "internal-fn.h"
  77. #include "tree-eh.h"
  78. #include "gimple-expr.h"
  79. #include "is-a.h"
  80. #include "gimple.h"
  81. #include "gimplify.h"
  82. #include "gimple-iterator.h"
  83. #include "gimplify-me.h"
  84. #include "gimple-ssa.h"
  85. #include "plugin-api.h"
  86. #include "ipa-ref.h"
  87. #include "cgraph.h"
  88. #include "tree-cfg.h"
  89. #include "tree-phinodes.h"
  90. #include "ssa-iterators.h"
  91. #include "stringpool.h"
  92. #include "tree-ssanames.h"
  93. #include "tree-ssa-loop-ivopts.h"
  94. #include "tree-ssa-loop-manip.h"
  95. #include "tree-ssa-loop-niter.h"
  96. #include "tree-ssa-loop.h"
  97. #include "hashtab.h"
  98. #include "rtl.h"
  99. #include "flags.h"
  100. #include "statistics.h"
  101. #include "real.h"
  102. #include "fixed-value.h"
  103. #include "insn-config.h"
  104. #include "expmed.h"
  105. #include "dojump.h"
  106. #include "explow.h"
  107. #include "calls.h"
  108. #include "emit-rtl.h"
  109. #include "varasm.h"
  110. #include "stmt.h"
  111. #include "expr.h"
  112. #include "tree-dfa.h"
  113. #include "tree-ssa.h"
  114. #include "cfgloop.h"
  115. #include "tree-pass.h"
  116. #include "tree-chrec.h"
  117. #include "tree-scalar-evolution.h"
  118. #include "params.h"
  119. #include "langhooks.h"
  120. #include "tree-affine.h"
  121. #include "target.h"
  122. #include "tree-inline.h"
  123. #include "tree-ssa-propagate.h"
  124. #include "tree-ssa-address.h"
  125. #include "builtins.h"
  126. #include "tree-vectorizer.h"
  127. /* FIXME: Expressions are expanded to RTL in this pass to determine the
  128. cost of different addressing modes. This should be moved to a TBD
  129. interface between the GIMPLE and RTL worlds. */
  130. #include "recog.h"
  131. /* The infinite cost. */
  132. #define INFTY 10000000
  133. #define AVG_LOOP_NITER(LOOP) 5
  134. /* Returns the expected number of loop iterations for LOOP.
  135. The average trip count is computed from profile data if it
  136. exists. */
  137. static inline HOST_WIDE_INT
  138. avg_loop_niter (struct loop *loop)
  139. {
  140. HOST_WIDE_INT niter = estimated_stmt_executions_int (loop);
  141. if (niter == -1)
  142. return AVG_LOOP_NITER (loop);
  143. return niter;
  144. }
  145. /* Representation of the induction variable. */
  146. struct iv
  147. {
  148. tree base; /* Initial value of the iv. */
  149. tree base_object; /* A memory object to that the induction variable points. */
  150. tree step; /* Step of the iv (constant only). */
  151. tree ssa_name; /* The ssa name with the value. */
  152. bool biv_p; /* Is it a biv? */
  153. bool have_use_for; /* Do we already have a use for it? */
  154. unsigned use_id; /* The identifier in the use if it is the case. */
  155. };
  156. /* Per-ssa version information (induction variable descriptions, etc.). */
  157. struct version_info
  158. {
  159. tree name; /* The ssa name. */
  160. struct iv *iv; /* Induction variable description. */
  161. bool has_nonlin_use; /* For a loop-level invariant, whether it is used in
  162. an expression that is not an induction variable. */
  163. bool preserve_biv; /* For the original biv, whether to preserve it. */
  164. unsigned inv_id; /* Id of an invariant. */
  165. };
  166. /* Types of uses. */
  167. enum use_type
  168. {
  169. USE_NONLINEAR_EXPR, /* Use in a nonlinear expression. */
  170. USE_ADDRESS, /* Use in an address. */
  171. USE_COMPARE /* Use is a compare. */
  172. };
  173. /* Cost of a computation. */
  174. typedef struct
  175. {
  176. int cost; /* The runtime cost. */
  177. unsigned complexity; /* The estimate of the complexity of the code for
  178. the computation (in no concrete units --
  179. complexity field should be larger for more
  180. complex expressions and addressing modes). */
  181. } comp_cost;
  182. static const comp_cost no_cost = {0, 0};
  183. static const comp_cost infinite_cost = {INFTY, INFTY};
  184. /* The candidate - cost pair. */
  185. struct cost_pair
  186. {
  187. struct iv_cand *cand; /* The candidate. */
  188. comp_cost cost; /* The cost. */
  189. bitmap depends_on; /* The list of invariants that have to be
  190. preserved. */
  191. tree value; /* For final value elimination, the expression for
  192. the final value of the iv. For iv elimination,
  193. the new bound to compare with. */
  194. enum tree_code comp; /* For iv elimination, the comparison. */
  195. int inv_expr_id; /* Loop invariant expression id. */
  196. };
  197. /* Use. */
  198. struct iv_use
  199. {
  200. unsigned id; /* The id of the use. */
  201. enum use_type type; /* Type of the use. */
  202. struct iv *iv; /* The induction variable it is based on. */
  203. gimple stmt; /* Statement in that it occurs. */
  204. tree *op_p; /* The place where it occurs. */
  205. bitmap related_cands; /* The set of "related" iv candidates, plus the common
  206. important ones. */
  207. unsigned n_map_members; /* Number of candidates in the cost_map list. */
  208. struct cost_pair *cost_map;
  209. /* The costs wrto the iv candidates. */
  210. struct iv_cand *selected;
  211. /* The selected candidate. */
  212. };
  213. /* The position where the iv is computed. */
  214. enum iv_position
  215. {
  216. IP_NORMAL, /* At the end, just before the exit condition. */
  217. IP_END, /* At the end of the latch block. */
  218. IP_BEFORE_USE, /* Immediately before a specific use. */
  219. IP_AFTER_USE, /* Immediately after a specific use. */
  220. IP_ORIGINAL /* The original biv. */
  221. };
  222. /* The induction variable candidate. */
  223. struct iv_cand
  224. {
  225. unsigned id; /* The number of the candidate. */
  226. bool important; /* Whether this is an "important" candidate, i.e. such
  227. that it should be considered by all uses. */
  228. ENUM_BITFIELD(iv_position) pos : 8; /* Where it is computed. */
  229. gimple incremented_at;/* For original biv, the statement where it is
  230. incremented. */
  231. tree var_before; /* The variable used for it before increment. */
  232. tree var_after; /* The variable used for it after increment. */
  233. struct iv *iv; /* The value of the candidate. NULL for
  234. "pseudocandidate" used to indicate the possibility
  235. to replace the final value of an iv by direct
  236. computation of the value. */
  237. unsigned cost; /* Cost of the candidate. */
  238. unsigned cost_step; /* Cost of the candidate's increment operation. */
  239. struct iv_use *ainc_use; /* For IP_{BEFORE,AFTER}_USE candidates, the place
  240. where it is incremented. */
  241. bitmap depends_on; /* The list of invariants that are used in step of the
  242. biv. */
  243. };
  244. /* Loop invariant expression hashtable entry. */
  245. struct iv_inv_expr_ent
  246. {
  247. tree expr;
  248. int id;
  249. hashval_t hash;
  250. };
  251. /* The data used by the induction variable optimizations. */
  252. typedef struct iv_use *iv_use_p;
  253. typedef struct iv_cand *iv_cand_p;
  254. /* Hashtable helpers. */
  255. struct iv_inv_expr_hasher : typed_free_remove <iv_inv_expr_ent>
  256. {
  257. typedef iv_inv_expr_ent value_type;
  258. typedef iv_inv_expr_ent compare_type;
  259. static inline hashval_t hash (const value_type *);
  260. static inline bool equal (const value_type *, const compare_type *);
  261. };
  262. /* Hash function for loop invariant expressions. */
  263. inline hashval_t
  264. iv_inv_expr_hasher::hash (const value_type *expr)
  265. {
  266. return expr->hash;
  267. }
  268. /* Hash table equality function for expressions. */
  269. inline bool
  270. iv_inv_expr_hasher::equal (const value_type *expr1, const compare_type *expr2)
  271. {
  272. return expr1->hash == expr2->hash
  273. && operand_equal_p (expr1->expr, expr2->expr, 0);
  274. }
  275. struct ivopts_data
  276. {
  277. /* The currently optimized loop. */
  278. struct loop *current_loop;
  279. source_location loop_loc;
  280. /* Numbers of iterations for all exits of the current loop. */
  281. hash_map<edge, tree_niter_desc *> *niters;
  282. /* Number of registers used in it. */
  283. unsigned regs_used;
  284. /* The size of version_info array allocated. */
  285. unsigned version_info_size;
  286. /* The array of information for the ssa names. */
  287. struct version_info *version_info;
  288. /* The hashtable of loop invariant expressions created
  289. by ivopt. */
  290. hash_table<iv_inv_expr_hasher> *inv_expr_tab;
  291. /* Loop invariant expression id. */
  292. int inv_expr_id;
  293. /* The bitmap of indices in version_info whose value was changed. */
  294. bitmap relevant;
  295. /* The uses of induction variables. */
  296. vec<iv_use_p> iv_uses;
  297. /* The candidates. */
  298. vec<iv_cand_p> iv_candidates;
  299. /* A bitmap of important candidates. */
  300. bitmap important_candidates;
  301. /* Cache used by tree_to_aff_combination_expand. */
  302. hash_map<tree, name_expansion *> *name_expansion_cache;
  303. /* The maximum invariant id. */
  304. unsigned max_inv_id;
  305. /* Whether to consider just related and important candidates when replacing a
  306. use. */
  307. bool consider_all_candidates;
  308. /* Are we optimizing for speed? */
  309. bool speed;
  310. /* Whether the loop body includes any function calls. */
  311. bool body_includes_call;
  312. /* Whether the loop body can only be exited via single exit. */
  313. bool loop_single_exit_p;
  314. };
  315. /* An assignment of iv candidates to uses. */
  316. struct iv_ca
  317. {
  318. /* The number of uses covered by the assignment. */
  319. unsigned upto;
  320. /* Number of uses that cannot be expressed by the candidates in the set. */
  321. unsigned bad_uses;
  322. /* Candidate assigned to a use, together with the related costs. */
  323. struct cost_pair **cand_for_use;
  324. /* Number of times each candidate is used. */
  325. unsigned *n_cand_uses;
  326. /* The candidates used. */
  327. bitmap cands;
  328. /* The number of candidates in the set. */
  329. unsigned n_cands;
  330. /* Total number of registers needed. */
  331. unsigned n_regs;
  332. /* Total cost of expressing uses. */
  333. comp_cost cand_use_cost;
  334. /* Total cost of candidates. */
  335. unsigned cand_cost;
  336. /* Number of times each invariant is used. */
  337. unsigned *n_invariant_uses;
  338. /* The array holding the number of uses of each loop
  339. invariant expressions created by ivopt. */
  340. unsigned *used_inv_expr;
  341. /* The number of created loop invariants. */
  342. unsigned num_used_inv_expr;
  343. /* Total cost of the assignment. */
  344. comp_cost cost;
  345. };
  346. /* Difference of two iv candidate assignments. */
  347. struct iv_ca_delta
  348. {
  349. /* Changed use. */
  350. struct iv_use *use;
  351. /* An old assignment (for rollback purposes). */
  352. struct cost_pair *old_cp;
  353. /* A new assignment. */
  354. struct cost_pair *new_cp;
  355. /* Next change in the list. */
  356. struct iv_ca_delta *next_change;
  357. };
  358. /* Bound on number of candidates below that all candidates are considered. */
  359. #define CONSIDER_ALL_CANDIDATES_BOUND \
  360. ((unsigned) PARAM_VALUE (PARAM_IV_CONSIDER_ALL_CANDIDATES_BOUND))
  361. /* If there are more iv occurrences, we just give up (it is quite unlikely that
  362. optimizing such a loop would help, and it would take ages). */
  363. #define MAX_CONSIDERED_USES \
  364. ((unsigned) PARAM_VALUE (PARAM_IV_MAX_CONSIDERED_USES))
  365. /* If there are at most this number of ivs in the set, try removing unnecessary
  366. ivs from the set always. */
  367. #define ALWAYS_PRUNE_CAND_SET_BOUND \
  368. ((unsigned) PARAM_VALUE (PARAM_IV_ALWAYS_PRUNE_CAND_SET_BOUND))
  369. /* The list of trees for that the decl_rtl field must be reset is stored
  370. here. */
  371. static vec<tree> decl_rtl_to_reset;
  372. static comp_cost force_expr_to_var_cost (tree, bool);
  373. /* Number of uses recorded in DATA. */
  374. static inline unsigned
  375. n_iv_uses (struct ivopts_data *data)
  376. {
  377. return data->iv_uses.length ();
  378. }
  379. /* Ith use recorded in DATA. */
  380. static inline struct iv_use *
  381. iv_use (struct ivopts_data *data, unsigned i)
  382. {
  383. return data->iv_uses[i];
  384. }
  385. /* Number of candidates recorded in DATA. */
  386. static inline unsigned
  387. n_iv_cands (struct ivopts_data *data)
  388. {
  389. return data->iv_candidates.length ();
  390. }
  391. /* Ith candidate recorded in DATA. */
  392. static inline struct iv_cand *
  393. iv_cand (struct ivopts_data *data, unsigned i)
  394. {
  395. return data->iv_candidates[i];
  396. }
  397. /* The single loop exit if it dominates the latch, NULL otherwise. */
  398. edge
  399. single_dom_exit (struct loop *loop)
  400. {
  401. edge exit = single_exit (loop);
  402. if (!exit)
  403. return NULL;
  404. if (!just_once_each_iteration_p (loop, exit->src))
  405. return NULL;
  406. return exit;
  407. }
  408. /* Dumps information about the induction variable IV to FILE. */
  409. void
  410. dump_iv (FILE *file, struct iv *iv)
  411. {
  412. if (iv->ssa_name)
  413. {
  414. fprintf (file, "ssa name ");
  415. print_generic_expr (file, iv->ssa_name, TDF_SLIM);
  416. fprintf (file, "\n");
  417. }
  418. fprintf (file, " type ");
  419. print_generic_expr (file, TREE_TYPE (iv->base), TDF_SLIM);
  420. fprintf (file, "\n");
  421. if (iv->step)
  422. {
  423. fprintf (file, " base ");
  424. print_generic_expr (file, iv->base, TDF_SLIM);
  425. fprintf (file, "\n");
  426. fprintf (file, " step ");
  427. print_generic_expr (file, iv->step, TDF_SLIM);
  428. fprintf (file, "\n");
  429. }
  430. else
  431. {
  432. fprintf (file, " invariant ");
  433. print_generic_expr (file, iv->base, TDF_SLIM);
  434. fprintf (file, "\n");
  435. }
  436. if (iv->base_object)
  437. {
  438. fprintf (file, " base object ");
  439. print_generic_expr (file, iv->base_object, TDF_SLIM);
  440. fprintf (file, "\n");
  441. }
  442. if (iv->biv_p)
  443. fprintf (file, " is a biv\n");
  444. }
  445. /* Dumps information about the USE to FILE. */
  446. void
  447. dump_use (FILE *file, struct iv_use *use)
  448. {
  449. fprintf (file, "use %d\n", use->id);
  450. switch (use->type)
  451. {
  452. case USE_NONLINEAR_EXPR:
  453. fprintf (file, " generic\n");
  454. break;
  455. case USE_ADDRESS:
  456. fprintf (file, " address\n");
  457. break;
  458. case USE_COMPARE:
  459. fprintf (file, " compare\n");
  460. break;
  461. default:
  462. gcc_unreachable ();
  463. }
  464. fprintf (file, " in statement ");
  465. print_gimple_stmt (file, use->stmt, 0, 0);
  466. fprintf (file, "\n");
  467. fprintf (file, " at position ");
  468. if (use->op_p)
  469. print_generic_expr (file, *use->op_p, TDF_SLIM);
  470. fprintf (file, "\n");
  471. dump_iv (file, use->iv);
  472. if (use->related_cands)
  473. {
  474. fprintf (file, " related candidates ");
  475. dump_bitmap (file, use->related_cands);
  476. }
  477. }
  478. /* Dumps information about the uses to FILE. */
  479. void
  480. dump_uses (FILE *file, struct ivopts_data *data)
  481. {
  482. unsigned i;
  483. struct iv_use *use;
  484. for (i = 0; i < n_iv_uses (data); i++)
  485. {
  486. use = iv_use (data, i);
  487. dump_use (file, use);
  488. fprintf (file, "\n");
  489. }
  490. }
  491. /* Dumps information about induction variable candidate CAND to FILE. */
  492. void
  493. dump_cand (FILE *file, struct iv_cand *cand)
  494. {
  495. struct iv *iv = cand->iv;
  496. fprintf (file, "candidate %d%s\n",
  497. cand->id, cand->important ? " (important)" : "");
  498. if (cand->depends_on)
  499. {
  500. fprintf (file, " depends on ");
  501. dump_bitmap (file, cand->depends_on);
  502. }
  503. if (!iv)
  504. {
  505. fprintf (file, " final value replacement\n");
  506. return;
  507. }
  508. if (cand->var_before)
  509. {
  510. fprintf (file, " var_before ");
  511. print_generic_expr (file, cand->var_before, TDF_SLIM);
  512. fprintf (file, "\n");
  513. }
  514. if (cand->var_after)
  515. {
  516. fprintf (file, " var_after ");
  517. print_generic_expr (file, cand->var_after, TDF_SLIM);
  518. fprintf (file, "\n");
  519. }
  520. switch (cand->pos)
  521. {
  522. case IP_NORMAL:
  523. fprintf (file, " incremented before exit test\n");
  524. break;
  525. case IP_BEFORE_USE:
  526. fprintf (file, " incremented before use %d\n", cand->ainc_use->id);
  527. break;
  528. case IP_AFTER_USE:
  529. fprintf (file, " incremented after use %d\n", cand->ainc_use->id);
  530. break;
  531. case IP_END:
  532. fprintf (file, " incremented at end\n");
  533. break;
  534. case IP_ORIGINAL:
  535. fprintf (file, " original biv\n");
  536. break;
  537. }
  538. dump_iv (file, iv);
  539. }
  540. /* Returns the info for ssa version VER. */
  541. static inline struct version_info *
  542. ver_info (struct ivopts_data *data, unsigned ver)
  543. {
  544. return data->version_info + ver;
  545. }
  546. /* Returns the info for ssa name NAME. */
  547. static inline struct version_info *
  548. name_info (struct ivopts_data *data, tree name)
  549. {
  550. return ver_info (data, SSA_NAME_VERSION (name));
  551. }
  552. /* Returns true if STMT is after the place where the IP_NORMAL ivs will be
  553. emitted in LOOP. */
  554. static bool
  555. stmt_after_ip_normal_pos (struct loop *loop, gimple stmt)
  556. {
  557. basic_block bb = ip_normal_pos (loop), sbb = gimple_bb (stmt);
  558. gcc_assert (bb);
  559. if (sbb == loop->latch)
  560. return true;
  561. if (sbb != bb)
  562. return false;
  563. return stmt == last_stmt (bb);
  564. }
  565. /* Returns true if STMT if after the place where the original induction
  566. variable CAND is incremented. If TRUE_IF_EQUAL is set, we return true
  567. if the positions are identical. */
  568. static bool
  569. stmt_after_inc_pos (struct iv_cand *cand, gimple stmt, bool true_if_equal)
  570. {
  571. basic_block cand_bb = gimple_bb (cand->incremented_at);
  572. basic_block stmt_bb = gimple_bb (stmt);
  573. if (!dominated_by_p (CDI_DOMINATORS, stmt_bb, cand_bb))
  574. return false;
  575. if (stmt_bb != cand_bb)
  576. return true;
  577. if (true_if_equal
  578. && gimple_uid (stmt) == gimple_uid (cand->incremented_at))
  579. return true;
  580. return gimple_uid (stmt) > gimple_uid (cand->incremented_at);
  581. }
  582. /* Returns true if STMT if after the place where the induction variable
  583. CAND is incremented in LOOP. */
  584. static bool
  585. stmt_after_increment (struct loop *loop, struct iv_cand *cand, gimple stmt)
  586. {
  587. switch (cand->pos)
  588. {
  589. case IP_END:
  590. return false;
  591. case IP_NORMAL:
  592. return stmt_after_ip_normal_pos (loop, stmt);
  593. case IP_ORIGINAL:
  594. case IP_AFTER_USE:
  595. return stmt_after_inc_pos (cand, stmt, false);
  596. case IP_BEFORE_USE:
  597. return stmt_after_inc_pos (cand, stmt, true);
  598. default:
  599. gcc_unreachable ();
  600. }
  601. }
  602. /* Returns true if EXP is a ssa name that occurs in an abnormal phi node. */
  603. static bool
  604. abnormal_ssa_name_p (tree exp)
  605. {
  606. if (!exp)
  607. return false;
  608. if (TREE_CODE (exp) != SSA_NAME)
  609. return false;
  610. return SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp) != 0;
  611. }
  612. /* Returns false if BASE or INDEX contains a ssa name that occurs in an
  613. abnormal phi node. Callback for for_each_index. */
  614. static bool
  615. idx_contains_abnormal_ssa_name_p (tree base, tree *index,
  616. void *data ATTRIBUTE_UNUSED)
  617. {
  618. if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
  619. {
  620. if (abnormal_ssa_name_p (TREE_OPERAND (base, 2)))
  621. return false;
  622. if (abnormal_ssa_name_p (TREE_OPERAND (base, 3)))
  623. return false;
  624. }
  625. return !abnormal_ssa_name_p (*index);
  626. }
  627. /* Returns true if EXPR contains a ssa name that occurs in an
  628. abnormal phi node. */
  629. bool
  630. contains_abnormal_ssa_name_p (tree expr)
  631. {
  632. enum tree_code code;
  633. enum tree_code_class codeclass;
  634. if (!expr)
  635. return false;
  636. code = TREE_CODE (expr);
  637. codeclass = TREE_CODE_CLASS (code);
  638. if (code == SSA_NAME)
  639. return SSA_NAME_OCCURS_IN_ABNORMAL_PHI (expr) != 0;
  640. if (code == INTEGER_CST
  641. || is_gimple_min_invariant (expr))
  642. return false;
  643. if (code == ADDR_EXPR)
  644. return !for_each_index (&TREE_OPERAND (expr, 0),
  645. idx_contains_abnormal_ssa_name_p,
  646. NULL);
  647. if (code == COND_EXPR)
  648. return contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 0))
  649. || contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 1))
  650. || contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 2));
  651. switch (codeclass)
  652. {
  653. case tcc_binary:
  654. case tcc_comparison:
  655. if (contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 1)))
  656. return true;
  657. /* Fallthru. */
  658. case tcc_unary:
  659. if (contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 0)))
  660. return true;
  661. break;
  662. default:
  663. gcc_unreachable ();
  664. }
  665. return false;
  666. }
  667. /* Returns the structure describing number of iterations determined from
  668. EXIT of DATA->current_loop, or NULL if something goes wrong. */
  669. static struct tree_niter_desc *
  670. niter_for_exit (struct ivopts_data *data, edge exit)
  671. {
  672. struct tree_niter_desc *desc;
  673. tree_niter_desc **slot;
  674. if (!data->niters)
  675. {
  676. data->niters = new hash_map<edge, tree_niter_desc *>;
  677. slot = NULL;
  678. }
  679. else
  680. slot = data->niters->get (exit);
  681. if (!slot)
  682. {
  683. /* Try to determine number of iterations. We cannot safely work with ssa
  684. names that appear in phi nodes on abnormal edges, so that we do not
  685. create overlapping life ranges for them (PR 27283). */
  686. desc = XNEW (struct tree_niter_desc);
  687. if (!number_of_iterations_exit (data->current_loop,
  688. exit, desc, true)
  689. || contains_abnormal_ssa_name_p (desc->niter))
  690. {
  691. XDELETE (desc);
  692. desc = NULL;
  693. }
  694. data->niters->put (exit, desc);
  695. }
  696. else
  697. desc = *slot;
  698. return desc;
  699. }
  700. /* Returns the structure describing number of iterations determined from
  701. single dominating exit of DATA->current_loop, or NULL if something
  702. goes wrong. */
  703. static struct tree_niter_desc *
  704. niter_for_single_dom_exit (struct ivopts_data *data)
  705. {
  706. edge exit = single_dom_exit (data->current_loop);
  707. if (!exit)
  708. return NULL;
  709. return niter_for_exit (data, exit);
  710. }
  711. /* Initializes data structures used by the iv optimization pass, stored
  712. in DATA. */
  713. static void
  714. tree_ssa_iv_optimize_init (struct ivopts_data *data)
  715. {
  716. data->version_info_size = 2 * num_ssa_names;
  717. data->version_info = XCNEWVEC (struct version_info, data->version_info_size);
  718. data->relevant = BITMAP_ALLOC (NULL);
  719. data->important_candidates = BITMAP_ALLOC (NULL);
  720. data->max_inv_id = 0;
  721. data->niters = NULL;
  722. data->iv_uses.create (20);
  723. data->iv_candidates.create (20);
  724. data->inv_expr_tab = new hash_table<iv_inv_expr_hasher> (10);
  725. data->inv_expr_id = 0;
  726. data->name_expansion_cache = NULL;
  727. decl_rtl_to_reset.create (20);
  728. }
  729. /* Returns a memory object to that EXPR points. In case we are able to
  730. determine that it does not point to any such object, NULL is returned. */
  731. static tree
  732. determine_base_object (tree expr)
  733. {
  734. enum tree_code code = TREE_CODE (expr);
  735. tree base, obj;
  736. /* If this is a pointer casted to any type, we need to determine
  737. the base object for the pointer; so handle conversions before
  738. throwing away non-pointer expressions. */
  739. if (CONVERT_EXPR_P (expr))
  740. return determine_base_object (TREE_OPERAND (expr, 0));
  741. if (!POINTER_TYPE_P (TREE_TYPE (expr)))
  742. return NULL_TREE;
  743. switch (code)
  744. {
  745. case INTEGER_CST:
  746. return NULL_TREE;
  747. case ADDR_EXPR:
  748. obj = TREE_OPERAND (expr, 0);
  749. base = get_base_address (obj);
  750. if (!base)
  751. return expr;
  752. if (TREE_CODE (base) == MEM_REF)
  753. return determine_base_object (TREE_OPERAND (base, 0));
  754. return fold_convert (ptr_type_node,
  755. build_fold_addr_expr (base));
  756. case POINTER_PLUS_EXPR:
  757. return determine_base_object (TREE_OPERAND (expr, 0));
  758. case PLUS_EXPR:
  759. case MINUS_EXPR:
  760. /* Pointer addition is done solely using POINTER_PLUS_EXPR. */
  761. gcc_unreachable ();
  762. default:
  763. return fold_convert (ptr_type_node, expr);
  764. }
  765. }
  766. /* Return true if address expression with non-DECL_P operand appears
  767. in EXPR. */
  768. static bool
  769. contain_complex_addr_expr (tree expr)
  770. {
  771. bool res = false;
  772. STRIP_NOPS (expr);
  773. switch (TREE_CODE (expr))
  774. {
  775. case POINTER_PLUS_EXPR:
  776. case PLUS_EXPR:
  777. case MINUS_EXPR:
  778. res |= contain_complex_addr_expr (TREE_OPERAND (expr, 0));
  779. res |= contain_complex_addr_expr (TREE_OPERAND (expr, 1));
  780. break;
  781. case ADDR_EXPR:
  782. return (!DECL_P (TREE_OPERAND (expr, 0)));
  783. default:
  784. return false;
  785. }
  786. return res;
  787. }
  788. /* Allocates an induction variable with given initial value BASE and step STEP
  789. for loop LOOP. */
  790. static struct iv *
  791. alloc_iv (tree base, tree step)
  792. {
  793. tree expr = base;
  794. struct iv *iv = XCNEW (struct iv);
  795. gcc_assert (step != NULL_TREE);
  796. /* Lower address expression in base except ones with DECL_P as operand.
  797. By doing this:
  798. 1) More accurate cost can be computed for address expressions;
  799. 2) Duplicate candidates won't be created for bases in different
  800. forms, like &a[0] and &a. */
  801. STRIP_NOPS (expr);
  802. if ((TREE_CODE (expr) == ADDR_EXPR && !DECL_P (TREE_OPERAND (expr, 0)))
  803. || contain_complex_addr_expr (expr))
  804. {
  805. aff_tree comb;
  806. tree_to_aff_combination (expr, TREE_TYPE (base), &comb);
  807. base = fold_convert (TREE_TYPE (base), aff_combination_to_tree (&comb));
  808. }
  809. iv->base = base;
  810. iv->base_object = determine_base_object (base);
  811. iv->step = step;
  812. iv->biv_p = false;
  813. iv->have_use_for = false;
  814. iv->use_id = 0;
  815. iv->ssa_name = NULL_TREE;
  816. return iv;
  817. }
  818. /* Sets STEP and BASE for induction variable IV. */
  819. static void
  820. set_iv (struct ivopts_data *data, tree iv, tree base, tree step)
  821. {
  822. struct version_info *info = name_info (data, iv);
  823. gcc_assert (!info->iv);
  824. bitmap_set_bit (data->relevant, SSA_NAME_VERSION (iv));
  825. info->iv = alloc_iv (base, step);
  826. info->iv->ssa_name = iv;
  827. }
  828. /* Finds induction variable declaration for VAR. */
  829. static struct iv *
  830. get_iv (struct ivopts_data *data, tree var)
  831. {
  832. basic_block bb;
  833. tree type = TREE_TYPE (var);
  834. if (!POINTER_TYPE_P (type)
  835. && !INTEGRAL_TYPE_P (type))
  836. return NULL;
  837. if (!name_info (data, var)->iv)
  838. {
  839. bb = gimple_bb (SSA_NAME_DEF_STMT (var));
  840. if (!bb
  841. || !flow_bb_inside_loop_p (data->current_loop, bb))
  842. set_iv (data, var, var, build_int_cst (type, 0));
  843. }
  844. return name_info (data, var)->iv;
  845. }
  846. /* Determines the step of a biv defined in PHI. Returns NULL if PHI does
  847. not define a simple affine biv with nonzero step. */
  848. static tree
  849. determine_biv_step (gphi *phi)
  850. {
  851. struct loop *loop = gimple_bb (phi)->loop_father;
  852. tree name = PHI_RESULT (phi);
  853. affine_iv iv;
  854. if (virtual_operand_p (name))
  855. return NULL_TREE;
  856. if (!simple_iv (loop, loop, name, &iv, true))
  857. return NULL_TREE;
  858. return integer_zerop (iv.step) ? NULL_TREE : iv.step;
  859. }
  860. /* Return the first non-invariant ssa var found in EXPR. */
  861. static tree
  862. extract_single_var_from_expr (tree expr)
  863. {
  864. int i, n;
  865. tree tmp;
  866. enum tree_code code;
  867. if (!expr || is_gimple_min_invariant (expr))
  868. return NULL;
  869. code = TREE_CODE (expr);
  870. if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
  871. {
  872. n = TREE_OPERAND_LENGTH (expr);
  873. for (i = 0; i < n; i++)
  874. {
  875. tmp = extract_single_var_from_expr (TREE_OPERAND (expr, i));
  876. if (tmp)
  877. return tmp;
  878. }
  879. }
  880. return (TREE_CODE (expr) == SSA_NAME) ? expr : NULL;
  881. }
  882. /* Finds basic ivs. */
  883. static bool
  884. find_bivs (struct ivopts_data *data)
  885. {
  886. gphi *phi;
  887. tree step, type, base, stop;
  888. bool found = false;
  889. struct loop *loop = data->current_loop;
  890. gphi_iterator psi;
  891. for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
  892. {
  893. phi = psi.phi ();
  894. if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
  895. continue;
  896. step = determine_biv_step (phi);
  897. if (!step)
  898. continue;
  899. base = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
  900. /* Stop expanding iv base at the first ssa var referred by iv step.
  901. Ideally we should stop at any ssa var, because that's expensive
  902. and unusual to happen, we just do it on the first one.
  903. See PR64705 for the rationale. */
  904. stop = extract_single_var_from_expr (step);
  905. base = expand_simple_operations (base, stop);
  906. if (contains_abnormal_ssa_name_p (base)
  907. || contains_abnormal_ssa_name_p (step))
  908. continue;
  909. type = TREE_TYPE (PHI_RESULT (phi));
  910. base = fold_convert (type, base);
  911. if (step)
  912. {
  913. if (POINTER_TYPE_P (type))
  914. step = convert_to_ptrofftype (step);
  915. else
  916. step = fold_convert (type, step);
  917. }
  918. set_iv (data, PHI_RESULT (phi), base, step);
  919. found = true;
  920. }
  921. return found;
  922. }
  923. /* Marks basic ivs. */
  924. static void
  925. mark_bivs (struct ivopts_data *data)
  926. {
  927. gphi *phi;
  928. gimple def;
  929. tree var;
  930. struct iv *iv, *incr_iv;
  931. struct loop *loop = data->current_loop;
  932. basic_block incr_bb;
  933. gphi_iterator psi;
  934. for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
  935. {
  936. phi = psi.phi ();
  937. iv = get_iv (data, PHI_RESULT (phi));
  938. if (!iv)
  939. continue;
  940. var = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
  941. def = SSA_NAME_DEF_STMT (var);
  942. /* Don't mark iv peeled from other one as biv. */
  943. if (def
  944. && gimple_code (def) == GIMPLE_PHI
  945. && gimple_bb (def) == loop->header)
  946. continue;
  947. incr_iv = get_iv (data, var);
  948. if (!incr_iv)
  949. continue;
  950. /* If the increment is in the subloop, ignore it. */
  951. incr_bb = gimple_bb (SSA_NAME_DEF_STMT (var));
  952. if (incr_bb->loop_father != data->current_loop
  953. || (incr_bb->flags & BB_IRREDUCIBLE_LOOP))
  954. continue;
  955. iv->biv_p = true;
  956. incr_iv->biv_p = true;
  957. }
  958. }
  959. /* Checks whether STMT defines a linear induction variable and stores its
  960. parameters to IV. */
  961. static bool
  962. find_givs_in_stmt_scev (struct ivopts_data *data, gimple stmt, affine_iv *iv)
  963. {
  964. tree lhs, stop;
  965. struct loop *loop = data->current_loop;
  966. iv->base = NULL_TREE;
  967. iv->step = NULL_TREE;
  968. if (gimple_code (stmt) != GIMPLE_ASSIGN)
  969. return false;
  970. lhs = gimple_assign_lhs (stmt);
  971. if (TREE_CODE (lhs) != SSA_NAME)
  972. return false;
  973. if (!simple_iv (loop, loop_containing_stmt (stmt), lhs, iv, true))
  974. return false;
  975. /* Stop expanding iv base at the first ssa var referred by iv step.
  976. Ideally we should stop at any ssa var, because that's expensive
  977. and unusual to happen, we just do it on the first one.
  978. See PR64705 for the rationale. */
  979. stop = extract_single_var_from_expr (iv->step);
  980. iv->base = expand_simple_operations (iv->base, stop);
  981. if (contains_abnormal_ssa_name_p (iv->base)
  982. || contains_abnormal_ssa_name_p (iv->step))
  983. return false;
  984. /* If STMT could throw, then do not consider STMT as defining a GIV.
  985. While this will suppress optimizations, we can not safely delete this
  986. GIV and associated statements, even if it appears it is not used. */
  987. if (stmt_could_throw_p (stmt))
  988. return false;
  989. return true;
  990. }
  991. /* Finds general ivs in statement STMT. */
  992. static void
  993. find_givs_in_stmt (struct ivopts_data *data, gimple stmt)
  994. {
  995. affine_iv iv;
  996. if (!find_givs_in_stmt_scev (data, stmt, &iv))
  997. return;
  998. set_iv (data, gimple_assign_lhs (stmt), iv.base, iv.step);
  999. }
  1000. /* Finds general ivs in basic block BB. */
  1001. static void
  1002. find_givs_in_bb (struct ivopts_data *data, basic_block bb)
  1003. {
  1004. gimple_stmt_iterator bsi;
  1005. for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  1006. find_givs_in_stmt (data, gsi_stmt (bsi));
  1007. }
  1008. /* Finds general ivs. */
  1009. static void
  1010. find_givs (struct ivopts_data *data)
  1011. {
  1012. struct loop *loop = data->current_loop;
  1013. basic_block *body = get_loop_body_in_dom_order (loop);
  1014. unsigned i;
  1015. for (i = 0; i < loop->num_nodes; i++)
  1016. find_givs_in_bb (data, body[i]);
  1017. free (body);
  1018. }
  1019. /* For each ssa name defined in LOOP determines whether it is an induction
  1020. variable and if so, its initial value and step. */
  1021. static bool
  1022. find_induction_variables (struct ivopts_data *data)
  1023. {
  1024. unsigned i;
  1025. bitmap_iterator bi;
  1026. if (!find_bivs (data))
  1027. return false;
  1028. find_givs (data);
  1029. mark_bivs (data);
  1030. if (dump_file && (dump_flags & TDF_DETAILS))
  1031. {
  1032. struct tree_niter_desc *niter = niter_for_single_dom_exit (data);
  1033. if (niter)
  1034. {
  1035. fprintf (dump_file, " number of iterations ");
  1036. print_generic_expr (dump_file, niter->niter, TDF_SLIM);
  1037. if (!integer_zerop (niter->may_be_zero))
  1038. {
  1039. fprintf (dump_file, "; zero if ");
  1040. print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM);
  1041. }
  1042. fprintf (dump_file, "\n\n");
  1043. };
  1044. fprintf (dump_file, "Induction variables:\n\n");
  1045. EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
  1046. {
  1047. if (ver_info (data, i)->iv)
  1048. dump_iv (dump_file, ver_info (data, i)->iv);
  1049. }
  1050. }
  1051. return true;
  1052. }
  1053. /* Records a use of type USE_TYPE at *USE_P in STMT whose value is IV. */
  1054. static struct iv_use *
  1055. record_use (struct ivopts_data *data, tree *use_p, struct iv *iv,
  1056. gimple stmt, enum use_type use_type)
  1057. {
  1058. struct iv_use *use = XCNEW (struct iv_use);
  1059. use->id = n_iv_uses (data);
  1060. use->type = use_type;
  1061. use->iv = iv;
  1062. use->stmt = stmt;
  1063. use->op_p = use_p;
  1064. use->related_cands = BITMAP_ALLOC (NULL);
  1065. /* To avoid showing ssa name in the dumps, if it was not reset by the
  1066. caller. */
  1067. iv->ssa_name = NULL_TREE;
  1068. if (dump_file && (dump_flags & TDF_DETAILS))
  1069. dump_use (dump_file, use);
  1070. data->iv_uses.safe_push (use);
  1071. return use;
  1072. }
  1073. /* Checks whether OP is a loop-level invariant and if so, records it.
  1074. NONLINEAR_USE is true if the invariant is used in a way we do not
  1075. handle specially. */
  1076. static void
  1077. record_invariant (struct ivopts_data *data, tree op, bool nonlinear_use)
  1078. {
  1079. basic_block bb;
  1080. struct version_info *info;
  1081. if (TREE_CODE (op) != SSA_NAME
  1082. || virtual_operand_p (op))
  1083. return;
  1084. bb = gimple_bb (SSA_NAME_DEF_STMT (op));
  1085. if (bb
  1086. && flow_bb_inside_loop_p (data->current_loop, bb))
  1087. return;
  1088. info = name_info (data, op);
  1089. info->name = op;
  1090. info->has_nonlin_use |= nonlinear_use;
  1091. if (!info->inv_id)
  1092. info->inv_id = ++data->max_inv_id;
  1093. bitmap_set_bit (data->relevant, SSA_NAME_VERSION (op));
  1094. }
  1095. /* Checks whether the use OP is interesting and if so, records it. */
  1096. static struct iv_use *
  1097. find_interesting_uses_op (struct ivopts_data *data, tree op)
  1098. {
  1099. struct iv *iv;
  1100. struct iv *civ;
  1101. gimple stmt;
  1102. struct iv_use *use;
  1103. if (TREE_CODE (op) != SSA_NAME)
  1104. return NULL;
  1105. iv = get_iv (data, op);
  1106. if (!iv)
  1107. return NULL;
  1108. if (iv->have_use_for)
  1109. {
  1110. use = iv_use (data, iv->use_id);
  1111. gcc_assert (use->type == USE_NONLINEAR_EXPR);
  1112. return use;
  1113. }
  1114. if (integer_zerop (iv->step))
  1115. {
  1116. record_invariant (data, op, true);
  1117. return NULL;
  1118. }
  1119. iv->have_use_for = true;
  1120. civ = XNEW (struct iv);
  1121. *civ = *iv;
  1122. stmt = SSA_NAME_DEF_STMT (op);
  1123. gcc_assert (gimple_code (stmt) == GIMPLE_PHI
  1124. || is_gimple_assign (stmt));
  1125. use = record_use (data, NULL, civ, stmt, USE_NONLINEAR_EXPR);
  1126. iv->use_id = use->id;
  1127. return use;
  1128. }
  1129. /* Given a condition in statement STMT, checks whether it is a compare
  1130. of an induction variable and an invariant. If this is the case,
  1131. CONTROL_VAR is set to location of the iv, BOUND to the location of
  1132. the invariant, IV_VAR and IV_BOUND are set to the corresponding
  1133. induction variable descriptions, and true is returned. If this is not
  1134. the case, CONTROL_VAR and BOUND are set to the arguments of the
  1135. condition and false is returned. */
  1136. static bool
  1137. extract_cond_operands (struct ivopts_data *data, gimple stmt,
  1138. tree **control_var, tree **bound,
  1139. struct iv **iv_var, struct iv **iv_bound)
  1140. {
  1141. /* The objects returned when COND has constant operands. */
  1142. static struct iv const_iv;
  1143. static tree zero;
  1144. tree *op0 = &zero, *op1 = &zero, *tmp_op;
  1145. struct iv *iv0 = &const_iv, *iv1 = &const_iv, *tmp_iv;
  1146. bool ret = false;
  1147. if (gimple_code (stmt) == GIMPLE_COND)
  1148. {
  1149. gcond *cond_stmt = as_a <gcond *> (stmt);
  1150. op0 = gimple_cond_lhs_ptr (cond_stmt);
  1151. op1 = gimple_cond_rhs_ptr (cond_stmt);
  1152. }
  1153. else
  1154. {
  1155. op0 = gimple_assign_rhs1_ptr (stmt);
  1156. op1 = gimple_assign_rhs2_ptr (stmt);
  1157. }
  1158. zero = integer_zero_node;
  1159. const_iv.step = integer_zero_node;
  1160. if (TREE_CODE (*op0) == SSA_NAME)
  1161. iv0 = get_iv (data, *op0);
  1162. if (TREE_CODE (*op1) == SSA_NAME)
  1163. iv1 = get_iv (data, *op1);
  1164. /* Exactly one of the compared values must be an iv, and the other one must
  1165. be an invariant. */
  1166. if (!iv0 || !iv1)
  1167. goto end;
  1168. if (integer_zerop (iv0->step))
  1169. {
  1170. /* Control variable may be on the other side. */
  1171. tmp_op = op0; op0 = op1; op1 = tmp_op;
  1172. tmp_iv = iv0; iv0 = iv1; iv1 = tmp_iv;
  1173. }
  1174. ret = !integer_zerop (iv0->step) && integer_zerop (iv1->step);
  1175. end:
  1176. if (control_var)
  1177. *control_var = op0;;
  1178. if (iv_var)
  1179. *iv_var = iv0;;
  1180. if (bound)
  1181. *bound = op1;
  1182. if (iv_bound)
  1183. *iv_bound = iv1;
  1184. return ret;
  1185. }
  1186. /* Checks whether the condition in STMT is interesting and if so,
  1187. records it. */
  1188. static void
  1189. find_interesting_uses_cond (struct ivopts_data *data, gimple stmt)
  1190. {
  1191. tree *var_p, *bound_p;
  1192. struct iv *var_iv, *civ;
  1193. if (!extract_cond_operands (data, stmt, &var_p, &bound_p, &var_iv, NULL))
  1194. {
  1195. find_interesting_uses_op (data, *var_p);
  1196. find_interesting_uses_op (data, *bound_p);
  1197. return;
  1198. }
  1199. civ = XNEW (struct iv);
  1200. *civ = *var_iv;
  1201. record_use (data, NULL, civ, stmt, USE_COMPARE);
  1202. }
  1203. /* Returns the outermost loop EXPR is obviously invariant in
  1204. relative to the loop LOOP, i.e. if all its operands are defined
  1205. outside of the returned loop. Returns NULL if EXPR is not
  1206. even obviously invariant in LOOP. */
  1207. struct loop *
  1208. outermost_invariant_loop_for_expr (struct loop *loop, tree expr)
  1209. {
  1210. basic_block def_bb;
  1211. unsigned i, len;
  1212. if (is_gimple_min_invariant (expr))
  1213. return current_loops->tree_root;
  1214. if (TREE_CODE (expr) == SSA_NAME)
  1215. {
  1216. def_bb = gimple_bb (SSA_NAME_DEF_STMT (expr));
  1217. if (def_bb)
  1218. {
  1219. if (flow_bb_inside_loop_p (loop, def_bb))
  1220. return NULL;
  1221. return superloop_at_depth (loop,
  1222. loop_depth (def_bb->loop_father) + 1);
  1223. }
  1224. return current_loops->tree_root;
  1225. }
  1226. if (!EXPR_P (expr))
  1227. return NULL;
  1228. unsigned maxdepth = 0;
  1229. len = TREE_OPERAND_LENGTH (expr);
  1230. for (i = 0; i < len; i++)
  1231. {
  1232. struct loop *ivloop;
  1233. if (!TREE_OPERAND (expr, i))
  1234. continue;
  1235. ivloop = outermost_invariant_loop_for_expr (loop, TREE_OPERAND (expr, i));
  1236. if (!ivloop)
  1237. return NULL;
  1238. maxdepth = MAX (maxdepth, loop_depth (ivloop));
  1239. }
  1240. return superloop_at_depth (loop, maxdepth);
  1241. }
  1242. /* Returns true if expression EXPR is obviously invariant in LOOP,
  1243. i.e. if all its operands are defined outside of the LOOP. LOOP
  1244. should not be the function body. */
  1245. bool
  1246. expr_invariant_in_loop_p (struct loop *loop, tree expr)
  1247. {
  1248. basic_block def_bb;
  1249. unsigned i, len;
  1250. gcc_assert (loop_depth (loop) > 0);
  1251. if (is_gimple_min_invariant (expr))
  1252. return true;
  1253. if (TREE_CODE (expr) == SSA_NAME)
  1254. {
  1255. def_bb = gimple_bb (SSA_NAME_DEF_STMT (expr));
  1256. if (def_bb
  1257. && flow_bb_inside_loop_p (loop, def_bb))
  1258. return false;
  1259. return true;
  1260. }
  1261. if (!EXPR_P (expr))
  1262. return false;
  1263. len = TREE_OPERAND_LENGTH (expr);
  1264. for (i = 0; i < len; i++)
  1265. if (TREE_OPERAND (expr, i)
  1266. && !expr_invariant_in_loop_p (loop, TREE_OPERAND (expr, i)))
  1267. return false;
  1268. return true;
  1269. }
  1270. /* Cumulates the steps of indices into DATA and replaces their values with the
  1271. initial ones. Returns false when the value of the index cannot be determined.
  1272. Callback for for_each_index. */
  1273. struct ifs_ivopts_data
  1274. {
  1275. struct ivopts_data *ivopts_data;
  1276. gimple stmt;
  1277. tree step;
  1278. };
  1279. static bool
  1280. idx_find_step (tree base, tree *idx, void *data)
  1281. {
  1282. struct ifs_ivopts_data *dta = (struct ifs_ivopts_data *) data;
  1283. struct iv *iv;
  1284. tree step, iv_base, iv_step, lbound, off;
  1285. struct loop *loop = dta->ivopts_data->current_loop;
  1286. /* If base is a component ref, require that the offset of the reference
  1287. be invariant. */
  1288. if (TREE_CODE (base) == COMPONENT_REF)
  1289. {
  1290. off = component_ref_field_offset (base);
  1291. return expr_invariant_in_loop_p (loop, off);
  1292. }
  1293. /* If base is array, first check whether we will be able to move the
  1294. reference out of the loop (in order to take its address in strength
  1295. reduction). In order for this to work we need both lower bound
  1296. and step to be loop invariants. */
  1297. if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
  1298. {
  1299. /* Moreover, for a range, the size needs to be invariant as well. */
  1300. if (TREE_CODE (base) == ARRAY_RANGE_REF
  1301. && !expr_invariant_in_loop_p (loop, TYPE_SIZE (TREE_TYPE (base))))
  1302. return false;
  1303. step = array_ref_element_size (base);
  1304. lbound = array_ref_low_bound (base);
  1305. if (!expr_invariant_in_loop_p (loop, step)
  1306. || !expr_invariant_in_loop_p (loop, lbound))
  1307. return false;
  1308. }
  1309. if (TREE_CODE (*idx) != SSA_NAME)
  1310. return true;
  1311. iv = get_iv (dta->ivopts_data, *idx);
  1312. if (!iv)
  1313. return false;
  1314. /* XXX We produce for a base of *D42 with iv->base being &x[0]
  1315. *&x[0], which is not folded and does not trigger the
  1316. ARRAY_REF path below. */
  1317. *idx = iv->base;
  1318. if (integer_zerop (iv->step))
  1319. return true;
  1320. if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
  1321. {
  1322. step = array_ref_element_size (base);
  1323. /* We only handle addresses whose step is an integer constant. */
  1324. if (TREE_CODE (step) != INTEGER_CST)
  1325. return false;
  1326. }
  1327. else
  1328. /* The step for pointer arithmetics already is 1 byte. */
  1329. step = size_one_node;
  1330. iv_base = iv->base;
  1331. iv_step = iv->step;
  1332. if (!convert_affine_scev (dta->ivopts_data->current_loop,
  1333. sizetype, &iv_base, &iv_step, dta->stmt,
  1334. false))
  1335. {
  1336. /* The index might wrap. */
  1337. return false;
  1338. }
  1339. step = fold_build2 (MULT_EXPR, sizetype, step, iv_step);
  1340. dta->step = fold_build2 (PLUS_EXPR, sizetype, dta->step, step);
  1341. return true;
  1342. }
  1343. /* Records use in index IDX. Callback for for_each_index. Ivopts data
  1344. object is passed to it in DATA. */
  1345. static bool
  1346. idx_record_use (tree base, tree *idx,
  1347. void *vdata)
  1348. {
  1349. struct ivopts_data *data = (struct ivopts_data *) vdata;
  1350. find_interesting_uses_op (data, *idx);
  1351. if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
  1352. {
  1353. find_interesting_uses_op (data, array_ref_element_size (base));
  1354. find_interesting_uses_op (data, array_ref_low_bound (base));
  1355. }
  1356. return true;
  1357. }
  1358. /* If we can prove that TOP = cst * BOT for some constant cst,
  1359. store cst to MUL and return true. Otherwise return false.
  1360. The returned value is always sign-extended, regardless of the
  1361. signedness of TOP and BOT. */
  1362. static bool
  1363. constant_multiple_of (tree top, tree bot, widest_int *mul)
  1364. {
  1365. tree mby;
  1366. enum tree_code code;
  1367. unsigned precision = TYPE_PRECISION (TREE_TYPE (top));
  1368. widest_int res, p0, p1;
  1369. STRIP_NOPS (top);
  1370. STRIP_NOPS (bot);
  1371. if (operand_equal_p (top, bot, 0))
  1372. {
  1373. *mul = 1;
  1374. return true;
  1375. }
  1376. code = TREE_CODE (top);
  1377. switch (code)
  1378. {
  1379. case MULT_EXPR:
  1380. mby = TREE_OPERAND (top, 1);
  1381. if (TREE_CODE (mby) != INTEGER_CST)
  1382. return false;
  1383. if (!constant_multiple_of (TREE_OPERAND (top, 0), bot, &res))
  1384. return false;
  1385. *mul = wi::sext (res * wi::to_widest (mby), precision);
  1386. return true;
  1387. case PLUS_EXPR:
  1388. case MINUS_EXPR:
  1389. if (!constant_multiple_of (TREE_OPERAND (top, 0), bot, &p0)
  1390. || !constant_multiple_of (TREE_OPERAND (top, 1), bot, &p1))
  1391. return false;
  1392. if (code == MINUS_EXPR)
  1393. p1 = -p1;
  1394. *mul = wi::sext (p0 + p1, precision);
  1395. return true;
  1396. case INTEGER_CST:
  1397. if (TREE_CODE (bot) != INTEGER_CST)
  1398. return false;
  1399. p0 = widest_int::from (top, SIGNED);
  1400. p1 = widest_int::from (bot, SIGNED);
  1401. if (p1 == 0)
  1402. return false;
  1403. *mul = wi::sext (wi::divmod_trunc (p0, p1, SIGNED, &res), precision);
  1404. return res == 0;
  1405. default:
  1406. return false;
  1407. }
  1408. }
  1409. /* Return true if memory reference REF with step STEP may be unaligned. */
  1410. static bool
  1411. may_be_unaligned_p (tree ref, tree step)
  1412. {
  1413. /* TARGET_MEM_REFs are translated directly to valid MEMs on the target,
  1414. thus they are not misaligned. */
  1415. if (TREE_CODE (ref) == TARGET_MEM_REF)
  1416. return false;
  1417. unsigned int align = TYPE_ALIGN (TREE_TYPE (ref));
  1418. if (GET_MODE_ALIGNMENT (TYPE_MODE (TREE_TYPE (ref))) > align)
  1419. align = GET_MODE_ALIGNMENT (TYPE_MODE (TREE_TYPE (ref)));
  1420. unsigned HOST_WIDE_INT bitpos;
  1421. unsigned int ref_align;
  1422. get_object_alignment_1 (ref, &ref_align, &bitpos);
  1423. if (ref_align < align
  1424. || (bitpos % align) != 0
  1425. || (bitpos % BITS_PER_UNIT) != 0)
  1426. return true;
  1427. unsigned int trailing_zeros = tree_ctz (step);
  1428. if (trailing_zeros < HOST_BITS_PER_INT
  1429. && (1U << trailing_zeros) * BITS_PER_UNIT < align)
  1430. return true;
  1431. return false;
  1432. }
  1433. /* Return true if EXPR may be non-addressable. */
  1434. bool
  1435. may_be_nonaddressable_p (tree expr)
  1436. {
  1437. switch (TREE_CODE (expr))
  1438. {
  1439. case TARGET_MEM_REF:
  1440. /* TARGET_MEM_REFs are translated directly to valid MEMs on the
  1441. target, thus they are always addressable. */
  1442. return false;
  1443. case COMPONENT_REF:
  1444. return DECL_NONADDRESSABLE_P (TREE_OPERAND (expr, 1))
  1445. || may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
  1446. case VIEW_CONVERT_EXPR:
  1447. /* This kind of view-conversions may wrap non-addressable objects
  1448. and make them look addressable. After some processing the
  1449. non-addressability may be uncovered again, causing ADDR_EXPRs
  1450. of inappropriate objects to be built. */
  1451. if (is_gimple_reg (TREE_OPERAND (expr, 0))
  1452. || !is_gimple_addressable (TREE_OPERAND (expr, 0)))
  1453. return true;
  1454. /* ... fall through ... */
  1455. case ARRAY_REF:
  1456. case ARRAY_RANGE_REF:
  1457. return may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
  1458. CASE_CONVERT:
  1459. return true;
  1460. default:
  1461. break;
  1462. }
  1463. return false;
  1464. }
  1465. /* Finds addresses in *OP_P inside STMT. */
  1466. static void
  1467. find_interesting_uses_address (struct ivopts_data *data, gimple stmt, tree *op_p)
  1468. {
  1469. tree base = *op_p, step = size_zero_node;
  1470. struct iv *civ;
  1471. struct ifs_ivopts_data ifs_ivopts_data;
  1472. /* Do not play with volatile memory references. A bit too conservative,
  1473. perhaps, but safe. */
  1474. if (gimple_has_volatile_ops (stmt))
  1475. goto fail;
  1476. /* Ignore bitfields for now. Not really something terribly complicated
  1477. to handle. TODO. */
  1478. if (TREE_CODE (base) == BIT_FIELD_REF)
  1479. goto fail;
  1480. base = unshare_expr (base);
  1481. if (TREE_CODE (base) == TARGET_MEM_REF)
  1482. {
  1483. tree type = build_pointer_type (TREE_TYPE (base));
  1484. tree astep;
  1485. if (TMR_BASE (base)
  1486. && TREE_CODE (TMR_BASE (base)) == SSA_NAME)
  1487. {
  1488. civ = get_iv (data, TMR_BASE (base));
  1489. if (!civ)
  1490. goto fail;
  1491. TMR_BASE (base) = civ->base;
  1492. step = civ->step;
  1493. }
  1494. if (TMR_INDEX2 (base)
  1495. && TREE_CODE (TMR_INDEX2 (base)) == SSA_NAME)
  1496. {
  1497. civ = get_iv (data, TMR_INDEX2 (base));
  1498. if (!civ)
  1499. goto fail;
  1500. TMR_INDEX2 (base) = civ->base;
  1501. step = civ->step;
  1502. }
  1503. if (TMR_INDEX (base)
  1504. && TREE_CODE (TMR_INDEX (base)) == SSA_NAME)
  1505. {
  1506. civ = get_iv (data, TMR_INDEX (base));
  1507. if (!civ)
  1508. goto fail;
  1509. TMR_INDEX (base) = civ->base;
  1510. astep = civ->step;
  1511. if (astep)
  1512. {
  1513. if (TMR_STEP (base))
  1514. astep = fold_build2 (MULT_EXPR, type, TMR_STEP (base), astep);
  1515. step = fold_build2 (PLUS_EXPR, type, step, astep);
  1516. }
  1517. }
  1518. if (integer_zerop (step))
  1519. goto fail;
  1520. base = tree_mem_ref_addr (type, base);
  1521. }
  1522. else
  1523. {
  1524. ifs_ivopts_data.ivopts_data = data;
  1525. ifs_ivopts_data.stmt = stmt;
  1526. ifs_ivopts_data.step = size_zero_node;
  1527. if (!for_each_index (&base, idx_find_step, &ifs_ivopts_data)
  1528. || integer_zerop (ifs_ivopts_data.step))
  1529. goto fail;
  1530. step = ifs_ivopts_data.step;
  1531. /* Check that the base expression is addressable. This needs
  1532. to be done after substituting bases of IVs into it. */
  1533. if (may_be_nonaddressable_p (base))
  1534. goto fail;
  1535. /* Moreover, on strict alignment platforms, check that it is
  1536. sufficiently aligned. */
  1537. if (STRICT_ALIGNMENT && may_be_unaligned_p (base, step))
  1538. goto fail;
  1539. base = build_fold_addr_expr (base);
  1540. /* Substituting bases of IVs into the base expression might
  1541. have caused folding opportunities. */
  1542. if (TREE_CODE (base) == ADDR_EXPR)
  1543. {
  1544. tree *ref = &TREE_OPERAND (base, 0);
  1545. while (handled_component_p (*ref))
  1546. ref = &TREE_OPERAND (*ref, 0);
  1547. if (TREE_CODE (*ref) == MEM_REF)
  1548. {
  1549. tree tem = fold_binary (MEM_REF, TREE_TYPE (*ref),
  1550. TREE_OPERAND (*ref, 0),
  1551. TREE_OPERAND (*ref, 1));
  1552. if (tem)
  1553. *ref = tem;
  1554. }
  1555. }
  1556. }
  1557. civ = alloc_iv (base, step);
  1558. record_use (data, op_p, civ, stmt, USE_ADDRESS);
  1559. return;
  1560. fail:
  1561. for_each_index (op_p, idx_record_use, data);
  1562. }
  1563. /* Finds and records invariants used in STMT. */
  1564. static void
  1565. find_invariants_stmt (struct ivopts_data *data, gimple stmt)
  1566. {
  1567. ssa_op_iter iter;
  1568. use_operand_p use_p;
  1569. tree op;
  1570. FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
  1571. {
  1572. op = USE_FROM_PTR (use_p);
  1573. record_invariant (data, op, false);
  1574. }
  1575. }
  1576. /* Finds interesting uses of induction variables in the statement STMT. */
  1577. static void
  1578. find_interesting_uses_stmt (struct ivopts_data *data, gimple stmt)
  1579. {
  1580. struct iv *iv;
  1581. tree op, *lhs, *rhs;
  1582. ssa_op_iter iter;
  1583. use_operand_p use_p;
  1584. enum tree_code code;
  1585. find_invariants_stmt (data, stmt);
  1586. if (gimple_code (stmt) == GIMPLE_COND)
  1587. {
  1588. find_interesting_uses_cond (data, stmt);
  1589. return;
  1590. }
  1591. if (is_gimple_assign (stmt))
  1592. {
  1593. lhs = gimple_assign_lhs_ptr (stmt);
  1594. rhs = gimple_assign_rhs1_ptr (stmt);
  1595. if (TREE_CODE (*lhs) == SSA_NAME)
  1596. {
  1597. /* If the statement defines an induction variable, the uses are not
  1598. interesting by themselves. */
  1599. iv = get_iv (data, *lhs);
  1600. if (iv && !integer_zerop (iv->step))
  1601. return;
  1602. }
  1603. code = gimple_assign_rhs_code (stmt);
  1604. if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
  1605. && (REFERENCE_CLASS_P (*rhs)
  1606. || is_gimple_val (*rhs)))
  1607. {
  1608. if (REFERENCE_CLASS_P (*rhs))
  1609. find_interesting_uses_address (data, stmt, rhs);
  1610. else
  1611. find_interesting_uses_op (data, *rhs);
  1612. if (REFERENCE_CLASS_P (*lhs))
  1613. find_interesting_uses_address (data, stmt, lhs);
  1614. return;
  1615. }
  1616. else if (TREE_CODE_CLASS (code) == tcc_comparison)
  1617. {
  1618. find_interesting_uses_cond (data, stmt);
  1619. return;
  1620. }
  1621. /* TODO -- we should also handle address uses of type
  1622. memory = call (whatever);
  1623. and
  1624. call (memory). */
  1625. }
  1626. if (gimple_code (stmt) == GIMPLE_PHI
  1627. && gimple_bb (stmt) == data->current_loop->header)
  1628. {
  1629. iv = get_iv (data, PHI_RESULT (stmt));
  1630. if (iv && !integer_zerop (iv->step))
  1631. return;
  1632. }
  1633. FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
  1634. {
  1635. op = USE_FROM_PTR (use_p);
  1636. if (TREE_CODE (op) != SSA_NAME)
  1637. continue;
  1638. iv = get_iv (data, op);
  1639. if (!iv)
  1640. continue;
  1641. find_interesting_uses_op (data, op);
  1642. }
  1643. }
  1644. /* Finds interesting uses of induction variables outside of loops
  1645. on loop exit edge EXIT. */
  1646. static void
  1647. find_interesting_uses_outside (struct ivopts_data *data, edge exit)
  1648. {
  1649. gphi *phi;
  1650. gphi_iterator psi;
  1651. tree def;
  1652. for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
  1653. {
  1654. phi = psi.phi ();
  1655. def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
  1656. if (!virtual_operand_p (def))
  1657. find_interesting_uses_op (data, def);
  1658. }
  1659. }
  1660. /* Finds uses of the induction variables that are interesting. */
  1661. static void
  1662. find_interesting_uses (struct ivopts_data *data)
  1663. {
  1664. basic_block bb;
  1665. gimple_stmt_iterator bsi;
  1666. basic_block *body = get_loop_body (data->current_loop);
  1667. unsigned i;
  1668. struct version_info *info;
  1669. edge e;
  1670. if (dump_file && (dump_flags & TDF_DETAILS))
  1671. fprintf (dump_file, "Uses:\n\n");
  1672. for (i = 0; i < data->current_loop->num_nodes; i++)
  1673. {
  1674. edge_iterator ei;
  1675. bb = body[i];
  1676. FOR_EACH_EDGE (e, ei, bb->succs)
  1677. if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
  1678. && !flow_bb_inside_loop_p (data->current_loop, e->dest))
  1679. find_interesting_uses_outside (data, e);
  1680. for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  1681. find_interesting_uses_stmt (data, gsi_stmt (bsi));
  1682. for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  1683. if (!is_gimple_debug (gsi_stmt (bsi)))
  1684. find_interesting_uses_stmt (data, gsi_stmt (bsi));
  1685. }
  1686. if (dump_file && (dump_flags & TDF_DETAILS))
  1687. {
  1688. bitmap_iterator bi;
  1689. fprintf (dump_file, "\n");
  1690. EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
  1691. {
  1692. info = ver_info (data, i);
  1693. if (info->inv_id)
  1694. {
  1695. fprintf (dump_file, " ");
  1696. print_generic_expr (dump_file, info->name, TDF_SLIM);
  1697. fprintf (dump_file, " is invariant (%d)%s\n",
  1698. info->inv_id, info->has_nonlin_use ? "" : ", eliminable");
  1699. }
  1700. }
  1701. fprintf (dump_file, "\n");
  1702. }
  1703. free (body);
  1704. }
  1705. /* Strips constant offsets from EXPR and stores them to OFFSET. If INSIDE_ADDR
  1706. is true, assume we are inside an address. If TOP_COMPREF is true, assume
  1707. we are at the top-level of the processed address. */
  1708. static tree
  1709. strip_offset_1 (tree expr, bool inside_addr, bool top_compref,
  1710. HOST_WIDE_INT *offset)
  1711. {
  1712. tree op0 = NULL_TREE, op1 = NULL_TREE, tmp, step;
  1713. enum tree_code code;
  1714. tree type, orig_type = TREE_TYPE (expr);
  1715. HOST_WIDE_INT off0, off1, st;
  1716. tree orig_expr = expr;
  1717. STRIP_NOPS (expr);
  1718. type = TREE_TYPE (expr);
  1719. code = TREE_CODE (expr);
  1720. *offset = 0;
  1721. switch (code)
  1722. {
  1723. case INTEGER_CST:
  1724. if (!cst_and_fits_in_hwi (expr)
  1725. || integer_zerop (expr))
  1726. return orig_expr;
  1727. *offset = int_cst_value (expr);
  1728. return build_int_cst (orig_type, 0);
  1729. case POINTER_PLUS_EXPR:
  1730. case PLUS_EXPR:
  1731. case MINUS_EXPR:
  1732. op0 = TREE_OPERAND (expr, 0);
  1733. op1 = TREE_OPERAND (expr, 1);
  1734. op0 = strip_offset_1 (op0, false, false, &off0);
  1735. op1 = strip_offset_1 (op1, false, false, &off1);
  1736. *offset = (code == MINUS_EXPR ? off0 - off1 : off0 + off1);
  1737. if (op0 == TREE_OPERAND (expr, 0)
  1738. && op1 == TREE_OPERAND (expr, 1))
  1739. return orig_expr;
  1740. if (integer_zerop (op1))
  1741. expr = op0;
  1742. else if (integer_zerop (op0))
  1743. {
  1744. if (code == MINUS_EXPR)
  1745. expr = fold_build1 (NEGATE_EXPR, type, op1);
  1746. else
  1747. expr = op1;
  1748. }
  1749. else
  1750. expr = fold_build2 (code, type, op0, op1);
  1751. return fold_convert (orig_type, expr);
  1752. case MULT_EXPR:
  1753. op1 = TREE_OPERAND (expr, 1);
  1754. if (!cst_and_fits_in_hwi (op1))
  1755. return orig_expr;
  1756. op0 = TREE_OPERAND (expr, 0);
  1757. op0 = strip_offset_1 (op0, false, false, &off0);
  1758. if (op0 == TREE_OPERAND (expr, 0))
  1759. return orig_expr;
  1760. *offset = off0 * int_cst_value (op1);
  1761. if (integer_zerop (op0))
  1762. expr = op0;
  1763. else
  1764. expr = fold_build2 (MULT_EXPR, type, op0, op1);
  1765. return fold_convert (orig_type, expr);
  1766. case ARRAY_REF:
  1767. case ARRAY_RANGE_REF:
  1768. if (!inside_addr)
  1769. return orig_expr;
  1770. step = array_ref_element_size (expr);
  1771. if (!cst_and_fits_in_hwi (step))
  1772. break;
  1773. st = int_cst_value (step);
  1774. op1 = TREE_OPERAND (expr, 1);
  1775. op1 = strip_offset_1 (op1, false, false, &off1);
  1776. *offset = off1 * st;
  1777. if (top_compref
  1778. && integer_zerop (op1))
  1779. {
  1780. /* Strip the component reference completely. */
  1781. op0 = TREE_OPERAND (expr, 0);
  1782. op0 = strip_offset_1 (op0, inside_addr, top_compref, &off0);
  1783. *offset += off0;
  1784. return op0;
  1785. }
  1786. break;
  1787. case COMPONENT_REF:
  1788. {
  1789. tree field;
  1790. if (!inside_addr)
  1791. return orig_expr;
  1792. tmp = component_ref_field_offset (expr);
  1793. field = TREE_OPERAND (expr, 1);
  1794. if (top_compref
  1795. && cst_and_fits_in_hwi (tmp)
  1796. && cst_and_fits_in_hwi (DECL_FIELD_BIT_OFFSET (field)))
  1797. {
  1798. HOST_WIDE_INT boffset, abs_off;
  1799. /* Strip the component reference completely. */
  1800. op0 = TREE_OPERAND (expr, 0);
  1801. op0 = strip_offset_1 (op0, inside_addr, top_compref, &off0);
  1802. boffset = int_cst_value (DECL_FIELD_BIT_OFFSET (field));
  1803. abs_off = abs_hwi (boffset) / BITS_PER_UNIT;
  1804. if (boffset < 0)
  1805. abs_off = -abs_off;
  1806. *offset = off0 + int_cst_value (tmp) + abs_off;
  1807. return op0;
  1808. }
  1809. }
  1810. break;
  1811. case ADDR_EXPR:
  1812. op0 = TREE_OPERAND (expr, 0);
  1813. op0 = strip_offset_1 (op0, true, true, &off0);
  1814. *offset += off0;
  1815. if (op0 == TREE_OPERAND (expr, 0))
  1816. return orig_expr;
  1817. expr = build_fold_addr_expr (op0);
  1818. return fold_convert (orig_type, expr);
  1819. case MEM_REF:
  1820. /* ??? Offset operand? */
  1821. inside_addr = false;
  1822. break;
  1823. default:
  1824. return orig_expr;
  1825. }
  1826. /* Default handling of expressions for that we want to recurse into
  1827. the first operand. */
  1828. op0 = TREE_OPERAND (expr, 0);
  1829. op0 = strip_offset_1 (op0, inside_addr, false, &off0);
  1830. *offset += off0;
  1831. if (op0 == TREE_OPERAND (expr, 0)
  1832. && (!op1 || op1 == TREE_OPERAND (expr, 1)))
  1833. return orig_expr;
  1834. expr = copy_node (expr);
  1835. TREE_OPERAND (expr, 0) = op0;
  1836. if (op1)
  1837. TREE_OPERAND (expr, 1) = op1;
  1838. /* Inside address, we might strip the top level component references,
  1839. thus changing type of the expression. Handling of ADDR_EXPR
  1840. will fix that. */
  1841. expr = fold_convert (orig_type, expr);
  1842. return expr;
  1843. }
  1844. /* Strips constant offsets from EXPR and stores them to OFFSET. */
  1845. static tree
  1846. strip_offset (tree expr, unsigned HOST_WIDE_INT *offset)
  1847. {
  1848. HOST_WIDE_INT off;
  1849. tree core = strip_offset_1 (expr, false, false, &off);
  1850. *offset = off;
  1851. return core;
  1852. }
  1853. /* Returns variant of TYPE that can be used as base for different uses.
  1854. We return unsigned type with the same precision, which avoids problems
  1855. with overflows. */
  1856. static tree
  1857. generic_type_for (tree type)
  1858. {
  1859. if (POINTER_TYPE_P (type))
  1860. return unsigned_type_for (type);
  1861. if (TYPE_UNSIGNED (type))
  1862. return type;
  1863. return unsigned_type_for (type);
  1864. }
  1865. /* Records invariants in *EXPR_P. Callback for walk_tree. DATA contains
  1866. the bitmap to that we should store it. */
  1867. static struct ivopts_data *fd_ivopts_data;
  1868. static tree
  1869. find_depends (tree *expr_p, int *ws ATTRIBUTE_UNUSED, void *data)
  1870. {
  1871. bitmap *depends_on = (bitmap *) data;
  1872. struct version_info *info;
  1873. if (TREE_CODE (*expr_p) != SSA_NAME)
  1874. return NULL_TREE;
  1875. info = name_info (fd_ivopts_data, *expr_p);
  1876. if (!info->inv_id || info->has_nonlin_use)
  1877. return NULL_TREE;
  1878. if (!*depends_on)
  1879. *depends_on = BITMAP_ALLOC (NULL);
  1880. bitmap_set_bit (*depends_on, info->inv_id);
  1881. return NULL_TREE;
  1882. }
  1883. /* Adds a candidate BASE + STEP * i. Important field is set to IMPORTANT and
  1884. position to POS. If USE is not NULL, the candidate is set as related to
  1885. it. If both BASE and STEP are NULL, we add a pseudocandidate for the
  1886. replacement of the final value of the iv by a direct computation. */
  1887. static struct iv_cand *
  1888. add_candidate_1 (struct ivopts_data *data,
  1889. tree base, tree step, bool important, enum iv_position pos,
  1890. struct iv_use *use, gimple incremented_at)
  1891. {
  1892. unsigned i;
  1893. struct iv_cand *cand = NULL;
  1894. tree type, orig_type;
  1895. /* For non-original variables, make sure their values are computed in a type
  1896. that does not invoke undefined behavior on overflows (since in general,
  1897. we cannot prove that these induction variables are non-wrapping). */
  1898. if (pos != IP_ORIGINAL)
  1899. {
  1900. orig_type = TREE_TYPE (base);
  1901. type = generic_type_for (orig_type);
  1902. if (type != orig_type)
  1903. {
  1904. base = fold_convert (type, base);
  1905. step = fold_convert (type, step);
  1906. }
  1907. }
  1908. for (i = 0; i < n_iv_cands (data); i++)
  1909. {
  1910. cand = iv_cand (data, i);
  1911. if (cand->pos != pos)
  1912. continue;
  1913. if (cand->incremented_at != incremented_at
  1914. || ((pos == IP_AFTER_USE || pos == IP_BEFORE_USE)
  1915. && cand->ainc_use != use))
  1916. continue;
  1917. if (!cand->iv)
  1918. {
  1919. if (!base && !step)
  1920. break;
  1921. continue;
  1922. }
  1923. if (!base && !step)
  1924. continue;
  1925. if (operand_equal_p (base, cand->iv->base, 0)
  1926. && operand_equal_p (step, cand->iv->step, 0)
  1927. && (TYPE_PRECISION (TREE_TYPE (base))
  1928. == TYPE_PRECISION (TREE_TYPE (cand->iv->base))))
  1929. break;
  1930. }
  1931. if (i == n_iv_cands (data))
  1932. {
  1933. cand = XCNEW (struct iv_cand);
  1934. cand->id = i;
  1935. if (!base && !step)
  1936. cand->iv = NULL;
  1937. else
  1938. cand->iv = alloc_iv (base, step);
  1939. cand->pos = pos;
  1940. if (pos != IP_ORIGINAL && cand->iv)
  1941. {
  1942. cand->var_before = create_tmp_var_raw (TREE_TYPE (base), "ivtmp");
  1943. cand->var_after = cand->var_before;
  1944. }
  1945. cand->important = important;
  1946. cand->incremented_at = incremented_at;
  1947. data->iv_candidates.safe_push (cand);
  1948. if (step
  1949. && TREE_CODE (step) != INTEGER_CST)
  1950. {
  1951. fd_ivopts_data = data;
  1952. walk_tree (&step, find_depends, &cand->depends_on, NULL);
  1953. }
  1954. if (pos == IP_AFTER_USE || pos == IP_BEFORE_USE)
  1955. cand->ainc_use = use;
  1956. else
  1957. cand->ainc_use = NULL;
  1958. if (dump_file && (dump_flags & TDF_DETAILS))
  1959. dump_cand (dump_file, cand);
  1960. }
  1961. if (important && !cand->important)
  1962. {
  1963. cand->important = true;
  1964. if (dump_file && (dump_flags & TDF_DETAILS))
  1965. fprintf (dump_file, "Candidate %d is important\n", cand->id);
  1966. }
  1967. if (use)
  1968. {
  1969. bitmap_set_bit (use->related_cands, i);
  1970. if (dump_file && (dump_flags & TDF_DETAILS))
  1971. fprintf (dump_file, "Candidate %d is related to use %d\n",
  1972. cand->id, use->id);
  1973. }
  1974. return cand;
  1975. }
  1976. /* Returns true if incrementing the induction variable at the end of the LOOP
  1977. is allowed.
  1978. The purpose is to avoid splitting latch edge with a biv increment, thus
  1979. creating a jump, possibly confusing other optimization passes and leaving
  1980. less freedom to scheduler. So we allow IP_END_POS only if IP_NORMAL_POS
  1981. is not available (so we do not have a better alternative), or if the latch
  1982. edge is already nonempty. */
  1983. static bool
  1984. allow_ip_end_pos_p (struct loop *loop)
  1985. {
  1986. if (!ip_normal_pos (loop))
  1987. return true;
  1988. if (!empty_block_p (ip_end_pos (loop)))
  1989. return true;
  1990. return false;
  1991. }
  1992. /* If possible, adds autoincrement candidates BASE + STEP * i based on use USE.
  1993. Important field is set to IMPORTANT. */
  1994. static void
  1995. add_autoinc_candidates (struct ivopts_data *data, tree base, tree step,
  1996. bool important, struct iv_use *use)
  1997. {
  1998. basic_block use_bb = gimple_bb (use->stmt);
  1999. machine_mode mem_mode;
  2000. unsigned HOST_WIDE_INT cstepi;
  2001. /* If we insert the increment in any position other than the standard
  2002. ones, we must ensure that it is incremented once per iteration.
  2003. It must not be in an inner nested loop, or one side of an if
  2004. statement. */
  2005. if (use_bb->loop_father != data->current_loop
  2006. || !dominated_by_p (CDI_DOMINATORS, data->current_loop->latch, use_bb)
  2007. || stmt_could_throw_p (use->stmt)
  2008. || !cst_and_fits_in_hwi (step))
  2009. return;
  2010. cstepi = int_cst_value (step);
  2011. mem_mode = TYPE_MODE (TREE_TYPE (*use->op_p));
  2012. if (((USE_LOAD_PRE_INCREMENT (mem_mode)
  2013. || USE_STORE_PRE_INCREMENT (mem_mode))
  2014. && GET_MODE_SIZE (mem_mode) == cstepi)
  2015. || ((USE_LOAD_PRE_DECREMENT (mem_mode)
  2016. || USE_STORE_PRE_DECREMENT (mem_mode))
  2017. && GET_MODE_SIZE (mem_mode) == -cstepi))
  2018. {
  2019. enum tree_code code = MINUS_EXPR;
  2020. tree new_base;
  2021. tree new_step = step;
  2022. if (POINTER_TYPE_P (TREE_TYPE (base)))
  2023. {
  2024. new_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
  2025. code = POINTER_PLUS_EXPR;
  2026. }
  2027. else
  2028. new_step = fold_convert (TREE_TYPE (base), new_step);
  2029. new_base = fold_build2 (code, TREE_TYPE (base), base, new_step);
  2030. add_candidate_1 (data, new_base, step, important, IP_BEFORE_USE, use,
  2031. use->stmt);
  2032. }
  2033. if (((USE_LOAD_POST_INCREMENT (mem_mode)
  2034. || USE_STORE_POST_INCREMENT (mem_mode))
  2035. && GET_MODE_SIZE (mem_mode) == cstepi)
  2036. || ((USE_LOAD_POST_DECREMENT (mem_mode)
  2037. || USE_STORE_POST_DECREMENT (mem_mode))
  2038. && GET_MODE_SIZE (mem_mode) == -cstepi))
  2039. {
  2040. add_candidate_1 (data, base, step, important, IP_AFTER_USE, use,
  2041. use->stmt);
  2042. }
  2043. }
  2044. /* Adds a candidate BASE + STEP * i. Important field is set to IMPORTANT and
  2045. position to POS. If USE is not NULL, the candidate is set as related to
  2046. it. The candidate computation is scheduled on all available positions. */
  2047. static void
  2048. add_candidate (struct ivopts_data *data,
  2049. tree base, tree step, bool important, struct iv_use *use)
  2050. {
  2051. if (ip_normal_pos (data->current_loop))
  2052. add_candidate_1 (data, base, step, important, IP_NORMAL, use, NULL);
  2053. if (ip_end_pos (data->current_loop)
  2054. && allow_ip_end_pos_p (data->current_loop))
  2055. add_candidate_1 (data, base, step, important, IP_END, use, NULL);
  2056. if (use != NULL && use->type == USE_ADDRESS)
  2057. add_autoinc_candidates (data, base, step, important, use);
  2058. }
  2059. /* Adds standard iv candidates. */
  2060. static void
  2061. add_standard_iv_candidates (struct ivopts_data *data)
  2062. {
  2063. add_candidate (data, integer_zero_node, integer_one_node, true, NULL);
  2064. /* The same for a double-integer type if it is still fast enough. */
  2065. if (TYPE_PRECISION
  2066. (long_integer_type_node) > TYPE_PRECISION (integer_type_node)
  2067. && TYPE_PRECISION (long_integer_type_node) <= BITS_PER_WORD)
  2068. add_candidate (data, build_int_cst (long_integer_type_node, 0),
  2069. build_int_cst (long_integer_type_node, 1), true, NULL);
  2070. /* The same for a double-integer type if it is still fast enough. */
  2071. if (TYPE_PRECISION
  2072. (long_long_integer_type_node) > TYPE_PRECISION (long_integer_type_node)
  2073. && TYPE_PRECISION (long_long_integer_type_node) <= BITS_PER_WORD)
  2074. add_candidate (data, build_int_cst (long_long_integer_type_node, 0),
  2075. build_int_cst (long_long_integer_type_node, 1), true, NULL);
  2076. }
  2077. /* Adds candidates bases on the old induction variable IV. */
  2078. static void
  2079. add_old_iv_candidates (struct ivopts_data *data, struct iv *iv)
  2080. {
  2081. gimple phi;
  2082. tree def;
  2083. struct iv_cand *cand;
  2084. add_candidate (data, iv->base, iv->step, true, NULL);
  2085. /* The same, but with initial value zero. */
  2086. if (POINTER_TYPE_P (TREE_TYPE (iv->base)))
  2087. add_candidate (data, size_int (0), iv->step, true, NULL);
  2088. else
  2089. add_candidate (data, build_int_cst (TREE_TYPE (iv->base), 0),
  2090. iv->step, true, NULL);
  2091. phi = SSA_NAME_DEF_STMT (iv->ssa_name);
  2092. if (gimple_code (phi) == GIMPLE_PHI)
  2093. {
  2094. /* Additionally record the possibility of leaving the original iv
  2095. untouched. */
  2096. def = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (data->current_loop));
  2097. /* Don't add candidate if it's from another PHI node because
  2098. it's an affine iv appearing in the form of PEELED_CHREC. */
  2099. phi = SSA_NAME_DEF_STMT (def);
  2100. if (gimple_code (phi) != GIMPLE_PHI)
  2101. {
  2102. cand = add_candidate_1 (data,
  2103. iv->base, iv->step, true, IP_ORIGINAL, NULL,
  2104. SSA_NAME_DEF_STMT (def));
  2105. cand->var_before = iv->ssa_name;
  2106. cand->var_after = def;
  2107. }
  2108. else
  2109. gcc_assert (gimple_bb (phi) == data->current_loop->header);
  2110. }
  2111. }
  2112. /* Adds candidates based on the old induction variables. */
  2113. static void
  2114. add_old_ivs_candidates (struct ivopts_data *data)
  2115. {
  2116. unsigned i;
  2117. struct iv *iv;
  2118. bitmap_iterator bi;
  2119. EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
  2120. {
  2121. iv = ver_info (data, i)->iv;
  2122. if (iv && iv->biv_p && !integer_zerop (iv->step))
  2123. add_old_iv_candidates (data, iv);
  2124. }
  2125. }
  2126. /* Adds candidates based on the value of the induction variable IV and USE. */
  2127. static void
  2128. add_iv_value_candidates (struct ivopts_data *data,
  2129. struct iv *iv, struct iv_use *use)
  2130. {
  2131. unsigned HOST_WIDE_INT offset;
  2132. tree base;
  2133. tree basetype;
  2134. add_candidate (data, iv->base, iv->step, false, use);
  2135. /* The same, but with initial value zero. Make such variable important,
  2136. since it is generic enough so that possibly many uses may be based
  2137. on it. */
  2138. basetype = TREE_TYPE (iv->base);
  2139. if (POINTER_TYPE_P (basetype))
  2140. basetype = sizetype;
  2141. add_candidate (data, build_int_cst (basetype, 0),
  2142. iv->step, true, use);
  2143. /* Third, try removing the constant offset. Make sure to even
  2144. add a candidate for &a[0] vs. (T *)&a. */
  2145. base = strip_offset (iv->base, &offset);
  2146. if (offset
  2147. || base != iv->base)
  2148. add_candidate (data, base, iv->step, false, use);
  2149. }
  2150. /* Adds candidates based on the uses. */
  2151. static void
  2152. add_derived_ivs_candidates (struct ivopts_data *data)
  2153. {
  2154. unsigned i;
  2155. for (i = 0; i < n_iv_uses (data); i++)
  2156. {
  2157. struct iv_use *use = iv_use (data, i);
  2158. if (!use)
  2159. continue;
  2160. switch (use->type)
  2161. {
  2162. case USE_NONLINEAR_EXPR:
  2163. case USE_COMPARE:
  2164. case USE_ADDRESS:
  2165. /* Just add the ivs based on the value of the iv used here. */
  2166. add_iv_value_candidates (data, use->iv, use);
  2167. break;
  2168. default:
  2169. gcc_unreachable ();
  2170. }
  2171. }
  2172. }
  2173. /* Record important candidates and add them to related_cands bitmaps
  2174. if needed. */
  2175. static void
  2176. record_important_candidates (struct ivopts_data *data)
  2177. {
  2178. unsigned i;
  2179. struct iv_use *use;
  2180. for (i = 0; i < n_iv_cands (data); i++)
  2181. {
  2182. struct iv_cand *cand = iv_cand (data, i);
  2183. if (cand->important)
  2184. bitmap_set_bit (data->important_candidates, i);
  2185. }
  2186. data->consider_all_candidates = (n_iv_cands (data)
  2187. <= CONSIDER_ALL_CANDIDATES_BOUND);
  2188. if (data->consider_all_candidates)
  2189. {
  2190. /* We will not need "related_cands" bitmaps in this case,
  2191. so release them to decrease peak memory consumption. */
  2192. for (i = 0; i < n_iv_uses (data); i++)
  2193. {
  2194. use = iv_use (data, i);
  2195. BITMAP_FREE (use->related_cands);
  2196. }
  2197. }
  2198. else
  2199. {
  2200. /* Add important candidates to the related_cands bitmaps. */
  2201. for (i = 0; i < n_iv_uses (data); i++)
  2202. bitmap_ior_into (iv_use (data, i)->related_cands,
  2203. data->important_candidates);
  2204. }
  2205. }
  2206. /* Allocates the data structure mapping the (use, candidate) pairs to costs.
  2207. If consider_all_candidates is true, we use a two-dimensional array, otherwise
  2208. we allocate a simple list to every use. */
  2209. static void
  2210. alloc_use_cost_map (struct ivopts_data *data)
  2211. {
  2212. unsigned i, size, s;
  2213. for (i = 0; i < n_iv_uses (data); i++)
  2214. {
  2215. struct iv_use *use = iv_use (data, i);
  2216. if (data->consider_all_candidates)
  2217. size = n_iv_cands (data);
  2218. else
  2219. {
  2220. s = bitmap_count_bits (use->related_cands);
  2221. /* Round up to the power of two, so that moduling by it is fast. */
  2222. size = s ? (1 << ceil_log2 (s)) : 1;
  2223. }
  2224. use->n_map_members = size;
  2225. use->cost_map = XCNEWVEC (struct cost_pair, size);
  2226. }
  2227. }
  2228. /* Returns description of computation cost of expression whose runtime
  2229. cost is RUNTIME and complexity corresponds to COMPLEXITY. */
  2230. static comp_cost
  2231. new_cost (unsigned runtime, unsigned complexity)
  2232. {
  2233. comp_cost cost;
  2234. cost.cost = runtime;
  2235. cost.complexity = complexity;
  2236. return cost;
  2237. }
  2238. /* Adds costs COST1 and COST2. */
  2239. static comp_cost
  2240. add_costs (comp_cost cost1, comp_cost cost2)
  2241. {
  2242. cost1.cost += cost2.cost;
  2243. cost1.complexity += cost2.complexity;
  2244. return cost1;
  2245. }
  2246. /* Subtracts costs COST1 and COST2. */
  2247. static comp_cost
  2248. sub_costs (comp_cost cost1, comp_cost cost2)
  2249. {
  2250. cost1.cost -= cost2.cost;
  2251. cost1.complexity -= cost2.complexity;
  2252. return cost1;
  2253. }
  2254. /* Returns a negative number if COST1 < COST2, a positive number if
  2255. COST1 > COST2, and 0 if COST1 = COST2. */
  2256. static int
  2257. compare_costs (comp_cost cost1, comp_cost cost2)
  2258. {
  2259. if (cost1.cost == cost2.cost)
  2260. return cost1.complexity - cost2.complexity;
  2261. return cost1.cost - cost2.cost;
  2262. }
  2263. /* Returns true if COST is infinite. */
  2264. static bool
  2265. infinite_cost_p (comp_cost cost)
  2266. {
  2267. return cost.cost == INFTY;
  2268. }
  2269. /* Sets cost of (USE, CANDIDATE) pair to COST and record that it depends
  2270. on invariants DEPENDS_ON and that the value used in expressing it
  2271. is VALUE, and in case of iv elimination the comparison operator is COMP. */
  2272. static void
  2273. set_use_iv_cost (struct ivopts_data *data,
  2274. struct iv_use *use, struct iv_cand *cand,
  2275. comp_cost cost, bitmap depends_on, tree value,
  2276. enum tree_code comp, int inv_expr_id)
  2277. {
  2278. unsigned i, s;
  2279. if (infinite_cost_p (cost))
  2280. {
  2281. BITMAP_FREE (depends_on);
  2282. return;
  2283. }
  2284. if (data->consider_all_candidates)
  2285. {
  2286. use->cost_map[cand->id].cand = cand;
  2287. use->cost_map[cand->id].cost = cost;
  2288. use->cost_map[cand->id].depends_on = depends_on;
  2289. use->cost_map[cand->id].value = value;
  2290. use->cost_map[cand->id].comp = comp;
  2291. use->cost_map[cand->id].inv_expr_id = inv_expr_id;
  2292. return;
  2293. }
  2294. /* n_map_members is a power of two, so this computes modulo. */
  2295. s = cand->id & (use->n_map_members - 1);
  2296. for (i = s; i < use->n_map_members; i++)
  2297. if (!use->cost_map[i].cand)
  2298. goto found;
  2299. for (i = 0; i < s; i++)
  2300. if (!use->cost_map[i].cand)
  2301. goto found;
  2302. gcc_unreachable ();
  2303. found:
  2304. use->cost_map[i].cand = cand;
  2305. use->cost_map[i].cost = cost;
  2306. use->cost_map[i].depends_on = depends_on;
  2307. use->cost_map[i].value = value;
  2308. use->cost_map[i].comp = comp;
  2309. use->cost_map[i].inv_expr_id = inv_expr_id;
  2310. }
  2311. /* Gets cost of (USE, CANDIDATE) pair. */
  2312. static struct cost_pair *
  2313. get_use_iv_cost (struct ivopts_data *data, struct iv_use *use,
  2314. struct iv_cand *cand)
  2315. {
  2316. unsigned i, s;
  2317. struct cost_pair *ret;
  2318. if (!cand)
  2319. return NULL;
  2320. if (data->consider_all_candidates)
  2321. {
  2322. ret = use->cost_map + cand->id;
  2323. if (!ret->cand)
  2324. return NULL;
  2325. return ret;
  2326. }
  2327. /* n_map_members is a power of two, so this computes modulo. */
  2328. s = cand->id & (use->n_map_members - 1);
  2329. for (i = s; i < use->n_map_members; i++)
  2330. if (use->cost_map[i].cand == cand)
  2331. return use->cost_map + i;
  2332. else if (use->cost_map[i].cand == NULL)
  2333. return NULL;
  2334. for (i = 0; i < s; i++)
  2335. if (use->cost_map[i].cand == cand)
  2336. return use->cost_map + i;
  2337. else if (use->cost_map[i].cand == NULL)
  2338. return NULL;
  2339. return NULL;
  2340. }
  2341. /* Produce DECL_RTL for object obj so it looks like it is stored in memory. */
  2342. static rtx
  2343. produce_memory_decl_rtl (tree obj, int *regno)
  2344. {
  2345. addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (obj));
  2346. machine_mode address_mode = targetm.addr_space.address_mode (as);
  2347. rtx x;
  2348. gcc_assert (obj);
  2349. if (TREE_STATIC (obj) || DECL_EXTERNAL (obj))
  2350. {
  2351. const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (obj));
  2352. x = gen_rtx_SYMBOL_REF (address_mode, name);
  2353. SET_SYMBOL_REF_DECL (x, obj);
  2354. x = gen_rtx_MEM (DECL_MODE (obj), x);
  2355. set_mem_addr_space (x, as);
  2356. targetm.encode_section_info (obj, x, true);
  2357. }
  2358. else
  2359. {
  2360. x = gen_raw_REG (address_mode, (*regno)++);
  2361. x = gen_rtx_MEM (DECL_MODE (obj), x);
  2362. set_mem_addr_space (x, as);
  2363. }
  2364. return x;
  2365. }
  2366. /* Prepares decl_rtl for variables referred in *EXPR_P. Callback for
  2367. walk_tree. DATA contains the actual fake register number. */
  2368. static tree
  2369. prepare_decl_rtl (tree *expr_p, int *ws, void *data)
  2370. {
  2371. tree obj = NULL_TREE;
  2372. rtx x = NULL_RTX;
  2373. int *regno = (int *) data;
  2374. switch (TREE_CODE (*expr_p))
  2375. {
  2376. case ADDR_EXPR:
  2377. for (expr_p = &TREE_OPERAND (*expr_p, 0);
  2378. handled_component_p (*expr_p);
  2379. expr_p = &TREE_OPERAND (*expr_p, 0))
  2380. continue;
  2381. obj = *expr_p;
  2382. if (DECL_P (obj) && HAS_RTL_P (obj) && !DECL_RTL_SET_P (obj))
  2383. x = produce_memory_decl_rtl (obj, regno);
  2384. break;
  2385. case SSA_NAME:
  2386. *ws = 0;
  2387. obj = SSA_NAME_VAR (*expr_p);
  2388. /* Defer handling of anonymous SSA_NAMEs to the expander. */
  2389. if (!obj)
  2390. return NULL_TREE;
  2391. if (!DECL_RTL_SET_P (obj))
  2392. x = gen_raw_REG (DECL_MODE (obj), (*regno)++);
  2393. break;
  2394. case VAR_DECL:
  2395. case PARM_DECL:
  2396. case RESULT_DECL:
  2397. *ws = 0;
  2398. obj = *expr_p;
  2399. if (DECL_RTL_SET_P (obj))
  2400. break;
  2401. if (DECL_MODE (obj) == BLKmode)
  2402. x = produce_memory_decl_rtl (obj, regno);
  2403. else
  2404. x = gen_raw_REG (DECL_MODE (obj), (*regno)++);
  2405. break;
  2406. default:
  2407. break;
  2408. }
  2409. if (x)
  2410. {
  2411. decl_rtl_to_reset.safe_push (obj);
  2412. SET_DECL_RTL (obj, x);
  2413. }
  2414. return NULL_TREE;
  2415. }
  2416. /* Determines cost of the computation of EXPR. */
  2417. static unsigned
  2418. computation_cost (tree expr, bool speed)
  2419. {
  2420. rtx_insn *seq;
  2421. rtx rslt;
  2422. tree type = TREE_TYPE (expr);
  2423. unsigned cost;
  2424. /* Avoid using hard regs in ways which may be unsupported. */
  2425. int regno = LAST_VIRTUAL_REGISTER + 1;
  2426. struct cgraph_node *node = cgraph_node::get (current_function_decl);
  2427. enum node_frequency real_frequency = node->frequency;
  2428. node->frequency = NODE_FREQUENCY_NORMAL;
  2429. crtl->maybe_hot_insn_p = speed;
  2430. walk_tree (&expr, prepare_decl_rtl, &regno, NULL);
  2431. start_sequence ();
  2432. rslt = expand_expr (expr, NULL_RTX, TYPE_MODE (type), EXPAND_NORMAL);
  2433. seq = get_insns ();
  2434. end_sequence ();
  2435. default_rtl_profile ();
  2436. node->frequency = real_frequency;
  2437. cost = seq_cost (seq, speed);
  2438. if (MEM_P (rslt))
  2439. cost += address_cost (XEXP (rslt, 0), TYPE_MODE (type),
  2440. TYPE_ADDR_SPACE (type), speed);
  2441. else if (!REG_P (rslt))
  2442. cost += set_src_cost (rslt, speed);
  2443. return cost;
  2444. }
  2445. /* Returns variable containing the value of candidate CAND at statement AT. */
  2446. static tree
  2447. var_at_stmt (struct loop *loop, struct iv_cand *cand, gimple stmt)
  2448. {
  2449. if (stmt_after_increment (loop, cand, stmt))
  2450. return cand->var_after;
  2451. else
  2452. return cand->var_before;
  2453. }
  2454. /* If A is (TYPE) BA and B is (TYPE) BB, and the types of BA and BB have the
  2455. same precision that is at least as wide as the precision of TYPE, stores
  2456. BA to A and BB to B, and returns the type of BA. Otherwise, returns the
  2457. type of A and B. */
  2458. static tree
  2459. determine_common_wider_type (tree *a, tree *b)
  2460. {
  2461. tree wider_type = NULL;
  2462. tree suba, subb;
  2463. tree atype = TREE_TYPE (*a);
  2464. if (CONVERT_EXPR_P (*a))
  2465. {
  2466. suba = TREE_OPERAND (*a, 0);
  2467. wider_type = TREE_TYPE (suba);
  2468. if (TYPE_PRECISION (wider_type) < TYPE_PRECISION (atype))
  2469. return atype;
  2470. }
  2471. else
  2472. return atype;
  2473. if (CONVERT_EXPR_P (*b))
  2474. {
  2475. subb = TREE_OPERAND (*b, 0);
  2476. if (TYPE_PRECISION (wider_type) != TYPE_PRECISION (TREE_TYPE (subb)))
  2477. return atype;
  2478. }
  2479. else
  2480. return atype;
  2481. *a = suba;
  2482. *b = subb;
  2483. return wider_type;
  2484. }
  2485. /* Determines the expression by that USE is expressed from induction variable
  2486. CAND at statement AT in LOOP. The expression is stored in a decomposed
  2487. form into AFF. Returns false if USE cannot be expressed using CAND. */
  2488. static bool
  2489. get_computation_aff (struct loop *loop,
  2490. struct iv_use *use, struct iv_cand *cand, gimple at,
  2491. struct aff_tree *aff)
  2492. {
  2493. tree ubase = use->iv->base;
  2494. tree ustep = use->iv->step;
  2495. tree cbase = cand->iv->base;
  2496. tree cstep = cand->iv->step, cstep_common;
  2497. tree utype = TREE_TYPE (ubase), ctype = TREE_TYPE (cbase);
  2498. tree common_type, var;
  2499. tree uutype;
  2500. aff_tree cbase_aff, var_aff;
  2501. widest_int rat;
  2502. if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype))
  2503. {
  2504. /* We do not have a precision to express the values of use. */
  2505. return false;
  2506. }
  2507. var = var_at_stmt (loop, cand, at);
  2508. uutype = unsigned_type_for (utype);
  2509. /* If the conversion is not noop, perform it. */
  2510. if (TYPE_PRECISION (utype) < TYPE_PRECISION (ctype))
  2511. {
  2512. cstep = fold_convert (uutype, cstep);
  2513. cbase = fold_convert (uutype, cbase);
  2514. var = fold_convert (uutype, var);
  2515. }
  2516. if (!constant_multiple_of (ustep, cstep, &rat))
  2517. return false;
  2518. /* In case both UBASE and CBASE are shortened to UUTYPE from some common
  2519. type, we achieve better folding by computing their difference in this
  2520. wider type, and cast the result to UUTYPE. We do not need to worry about
  2521. overflows, as all the arithmetics will in the end be performed in UUTYPE
  2522. anyway. */
  2523. common_type = determine_common_wider_type (&ubase, &cbase);
  2524. /* use = ubase - ratio * cbase + ratio * var. */
  2525. tree_to_aff_combination (ubase, common_type, aff);
  2526. tree_to_aff_combination (cbase, common_type, &cbase_aff);
  2527. tree_to_aff_combination (var, uutype, &var_aff);
  2528. /* We need to shift the value if we are after the increment. */
  2529. if (stmt_after_increment (loop, cand, at))
  2530. {
  2531. aff_tree cstep_aff;
  2532. if (common_type != uutype)
  2533. cstep_common = fold_convert (common_type, cstep);
  2534. else
  2535. cstep_common = cstep;
  2536. tree_to_aff_combination (cstep_common, common_type, &cstep_aff);
  2537. aff_combination_add (&cbase_aff, &cstep_aff);
  2538. }
  2539. aff_combination_scale (&cbase_aff, -rat);
  2540. aff_combination_add (aff, &cbase_aff);
  2541. if (common_type != uutype)
  2542. aff_combination_convert (aff, uutype);
  2543. aff_combination_scale (&var_aff, rat);
  2544. aff_combination_add (aff, &var_aff);
  2545. return true;
  2546. }
  2547. /* Return the type of USE. */
  2548. static tree
  2549. get_use_type (struct iv_use *use)
  2550. {
  2551. tree base_type = TREE_TYPE (use->iv->base);
  2552. tree type;
  2553. if (use->type == USE_ADDRESS)
  2554. {
  2555. /* The base_type may be a void pointer. Create a pointer type based on
  2556. the mem_ref instead. */
  2557. type = build_pointer_type (TREE_TYPE (*use->op_p));
  2558. gcc_assert (TYPE_ADDR_SPACE (TREE_TYPE (type))
  2559. == TYPE_ADDR_SPACE (TREE_TYPE (base_type)));
  2560. }
  2561. else
  2562. type = base_type;
  2563. return type;
  2564. }
  2565. /* Determines the expression by that USE is expressed from induction variable
  2566. CAND at statement AT in LOOP. The computation is unshared. */
  2567. static tree
  2568. get_computation_at (struct loop *loop,
  2569. struct iv_use *use, struct iv_cand *cand, gimple at)
  2570. {
  2571. aff_tree aff;
  2572. tree type = get_use_type (use);
  2573. if (!get_computation_aff (loop, use, cand, at, &aff))
  2574. return NULL_TREE;
  2575. unshare_aff_combination (&aff);
  2576. return fold_convert (type, aff_combination_to_tree (&aff));
  2577. }
  2578. /* Determines the expression by that USE is expressed from induction variable
  2579. CAND in LOOP. The computation is unshared. */
  2580. static tree
  2581. get_computation (struct loop *loop, struct iv_use *use, struct iv_cand *cand)
  2582. {
  2583. return get_computation_at (loop, use, cand, use->stmt);
  2584. }
  2585. /* Adjust the cost COST for being in loop setup rather than loop body.
  2586. If we're optimizing for space, the loop setup overhead is constant;
  2587. if we're optimizing for speed, amortize it over the per-iteration cost. */
  2588. static unsigned
  2589. adjust_setup_cost (struct ivopts_data *data, unsigned cost)
  2590. {
  2591. if (cost == INFTY)
  2592. return cost;
  2593. else if (optimize_loop_for_speed_p (data->current_loop))
  2594. return cost / avg_loop_niter (data->current_loop);
  2595. else
  2596. return cost;
  2597. }
  2598. /* Returns true if multiplying by RATIO is allowed in an address. Test the
  2599. validity for a memory reference accessing memory of mode MODE in
  2600. address space AS. */
  2601. bool
  2602. multiplier_allowed_in_address_p (HOST_WIDE_INT ratio, machine_mode mode,
  2603. addr_space_t as)
  2604. {
  2605. #define MAX_RATIO 128
  2606. unsigned int data_index = (int) as * MAX_MACHINE_MODE + (int) mode;
  2607. static vec<sbitmap> valid_mult_list;
  2608. sbitmap valid_mult;
  2609. if (data_index >= valid_mult_list.length ())
  2610. valid_mult_list.safe_grow_cleared (data_index + 1);
  2611. valid_mult = valid_mult_list[data_index];
  2612. if (!valid_mult)
  2613. {
  2614. machine_mode address_mode = targetm.addr_space.address_mode (as);
  2615. rtx reg1 = gen_raw_REG (address_mode, LAST_VIRTUAL_REGISTER + 1);
  2616. rtx reg2 = gen_raw_REG (address_mode, LAST_VIRTUAL_REGISTER + 2);
  2617. rtx addr, scaled;
  2618. HOST_WIDE_INT i;
  2619. valid_mult = sbitmap_alloc (2 * MAX_RATIO + 1);
  2620. bitmap_clear (valid_mult);
  2621. scaled = gen_rtx_fmt_ee (MULT, address_mode, reg1, NULL_RTX);
  2622. addr = gen_rtx_fmt_ee (PLUS, address_mode, scaled, reg2);
  2623. for (i = -MAX_RATIO; i <= MAX_RATIO; i++)
  2624. {
  2625. XEXP (scaled, 1) = gen_int_mode (i, address_mode);
  2626. if (memory_address_addr_space_p (mode, addr, as)
  2627. || memory_address_addr_space_p (mode, scaled, as))
  2628. bitmap_set_bit (valid_mult, i + MAX_RATIO);
  2629. }
  2630. if (dump_file && (dump_flags & TDF_DETAILS))
  2631. {
  2632. fprintf (dump_file, " allowed multipliers:");
  2633. for (i = -MAX_RATIO; i <= MAX_RATIO; i++)
  2634. if (bitmap_bit_p (valid_mult, i + MAX_RATIO))
  2635. fprintf (dump_file, " %d", (int) i);
  2636. fprintf (dump_file, "\n");
  2637. fprintf (dump_file, "\n");
  2638. }
  2639. valid_mult_list[data_index] = valid_mult;
  2640. }
  2641. if (ratio > MAX_RATIO || ratio < -MAX_RATIO)
  2642. return false;
  2643. return bitmap_bit_p (valid_mult, ratio + MAX_RATIO);
  2644. }
  2645. /* Returns cost of address in shape symbol + var + OFFSET + RATIO * index.
  2646. If SYMBOL_PRESENT is false, symbol is omitted. If VAR_PRESENT is false,
  2647. variable is omitted. Compute the cost for a memory reference that accesses
  2648. a memory location of mode MEM_MODE in address space AS.
  2649. MAY_AUTOINC is set to true if the autoincrement (increasing index by
  2650. size of MEM_MODE / RATIO) is available. To make this determination, we
  2651. look at the size of the increment to be made, which is given in CSTEP.
  2652. CSTEP may be zero if the step is unknown.
  2653. STMT_AFTER_INC is true iff the statement we're looking at is after the
  2654. increment of the original biv.
  2655. TODO -- there must be some better way. This all is quite crude. */
  2656. enum ainc_type
  2657. {
  2658. AINC_PRE_INC, /* Pre increment. */
  2659. AINC_PRE_DEC, /* Pre decrement. */
  2660. AINC_POST_INC, /* Post increment. */
  2661. AINC_POST_DEC, /* Post decrement. */
  2662. AINC_NONE /* Also the number of auto increment types. */
  2663. };
  2664. typedef struct address_cost_data_s
  2665. {
  2666. HOST_WIDE_INT min_offset, max_offset;
  2667. unsigned costs[2][2][2][2];
  2668. unsigned ainc_costs[AINC_NONE];
  2669. } *address_cost_data;
  2670. static comp_cost
  2671. get_address_cost (bool symbol_present, bool var_present,
  2672. unsigned HOST_WIDE_INT offset, HOST_WIDE_INT ratio,
  2673. HOST_WIDE_INT cstep, machine_mode mem_mode,
  2674. addr_space_t as, bool speed,
  2675. bool stmt_after_inc, bool *may_autoinc)
  2676. {
  2677. machine_mode address_mode = targetm.addr_space.address_mode (as);
  2678. static vec<address_cost_data> address_cost_data_list;
  2679. unsigned int data_index = (int) as * MAX_MACHINE_MODE + (int) mem_mode;
  2680. address_cost_data data;
  2681. static bool has_preinc[MAX_MACHINE_MODE], has_postinc[MAX_MACHINE_MODE];
  2682. static bool has_predec[MAX_MACHINE_MODE], has_postdec[MAX_MACHINE_MODE];
  2683. unsigned cost, acost, complexity;
  2684. enum ainc_type autoinc_type;
  2685. bool offset_p, ratio_p, autoinc;
  2686. HOST_WIDE_INT s_offset, autoinc_offset, msize;
  2687. unsigned HOST_WIDE_INT mask;
  2688. unsigned bits;
  2689. if (data_index >= address_cost_data_list.length ())
  2690. address_cost_data_list.safe_grow_cleared (data_index + 1);
  2691. data = address_cost_data_list[data_index];
  2692. if (!data)
  2693. {
  2694. HOST_WIDE_INT i;
  2695. HOST_WIDE_INT rat, off = 0;
  2696. int old_cse_not_expected, width;
  2697. unsigned sym_p, var_p, off_p, rat_p, add_c;
  2698. rtx_insn *seq;
  2699. rtx addr, base;
  2700. rtx reg0, reg1;
  2701. data = (address_cost_data) xcalloc (1, sizeof (*data));
  2702. reg1 = gen_raw_REG (address_mode, LAST_VIRTUAL_REGISTER + 1);
  2703. width = GET_MODE_BITSIZE (address_mode) - 1;
  2704. if (width > (HOST_BITS_PER_WIDE_INT - 1))
  2705. width = HOST_BITS_PER_WIDE_INT - 1;
  2706. addr = gen_rtx_fmt_ee (PLUS, address_mode, reg1, NULL_RTX);
  2707. for (i = width; i >= 0; i--)
  2708. {
  2709. off = -((unsigned HOST_WIDE_INT) 1 << i);
  2710. XEXP (addr, 1) = gen_int_mode (off, address_mode);
  2711. if (memory_address_addr_space_p (mem_mode, addr, as))
  2712. break;
  2713. }
  2714. data->min_offset = (i == -1? 0 : off);
  2715. for (i = width; i >= 0; i--)
  2716. {
  2717. off = ((unsigned HOST_WIDE_INT) 1 << i) - 1;
  2718. XEXP (addr, 1) = gen_int_mode (off, address_mode);
  2719. if (memory_address_addr_space_p (mem_mode, addr, as))
  2720. break;
  2721. /* For some strict-alignment targets, the offset must be naturally
  2722. aligned. Try an aligned offset if mem_mode is not QImode. */
  2723. off = mem_mode != QImode
  2724. ? ((unsigned HOST_WIDE_INT) 1 << i)
  2725. - GET_MODE_SIZE (mem_mode)
  2726. : 0;
  2727. if (off > 0)
  2728. {
  2729. XEXP (addr, 1) = gen_int_mode (off, address_mode);
  2730. if (memory_address_addr_space_p (mem_mode, addr, as))
  2731. break;
  2732. }
  2733. }
  2734. if (i == -1)
  2735. off = 0;
  2736. data->max_offset = off;
  2737. if (dump_file && (dump_flags & TDF_DETAILS))
  2738. {
  2739. fprintf (dump_file, "get_address_cost:\n");
  2740. fprintf (dump_file, " min offset %s " HOST_WIDE_INT_PRINT_DEC "\n",
  2741. GET_MODE_NAME (mem_mode),
  2742. data->min_offset);
  2743. fprintf (dump_file, " max offset %s " HOST_WIDE_INT_PRINT_DEC "\n",
  2744. GET_MODE_NAME (mem_mode),
  2745. data->max_offset);
  2746. }
  2747. rat = 1;
  2748. for (i = 2; i <= MAX_RATIO; i++)
  2749. if (multiplier_allowed_in_address_p (i, mem_mode, as))
  2750. {
  2751. rat = i;
  2752. break;
  2753. }
  2754. /* Compute the cost of various addressing modes. */
  2755. acost = 0;
  2756. reg0 = gen_raw_REG (address_mode, LAST_VIRTUAL_REGISTER + 1);
  2757. reg1 = gen_raw_REG (address_mode, LAST_VIRTUAL_REGISTER + 2);
  2758. if (USE_LOAD_PRE_DECREMENT (mem_mode)
  2759. || USE_STORE_PRE_DECREMENT (mem_mode))
  2760. {
  2761. addr = gen_rtx_PRE_DEC (address_mode, reg0);
  2762. has_predec[mem_mode]
  2763. = memory_address_addr_space_p (mem_mode, addr, as);
  2764. if (has_predec[mem_mode])
  2765. data->ainc_costs[AINC_PRE_DEC]
  2766. = address_cost (addr, mem_mode, as, speed);
  2767. }
  2768. if (USE_LOAD_POST_DECREMENT (mem_mode)
  2769. || USE_STORE_POST_DECREMENT (mem_mode))
  2770. {
  2771. addr = gen_rtx_POST_DEC (address_mode, reg0);
  2772. has_postdec[mem_mode]
  2773. = memory_address_addr_space_p (mem_mode, addr, as);
  2774. if (has_postdec[mem_mode])
  2775. data->ainc_costs[AINC_POST_DEC]
  2776. = address_cost (addr, mem_mode, as, speed);
  2777. }
  2778. if (USE_LOAD_PRE_INCREMENT (mem_mode)
  2779. || USE_STORE_PRE_DECREMENT (mem_mode))
  2780. {
  2781. addr = gen_rtx_PRE_INC (address_mode, reg0);
  2782. has_preinc[mem_mode]
  2783. = memory_address_addr_space_p (mem_mode, addr, as);
  2784. if (has_preinc[mem_mode])
  2785. data->ainc_costs[AINC_PRE_INC]
  2786. = address_cost (addr, mem_mode, as, speed);
  2787. }
  2788. if (USE_LOAD_POST_INCREMENT (mem_mode)
  2789. || USE_STORE_POST_INCREMENT (mem_mode))
  2790. {
  2791. addr = gen_rtx_POST_INC (address_mode, reg0);
  2792. has_postinc[mem_mode]
  2793. = memory_address_addr_space_p (mem_mode, addr, as);
  2794. if (has_postinc[mem_mode])
  2795. data->ainc_costs[AINC_POST_INC]
  2796. = address_cost (addr, mem_mode, as, speed);
  2797. }
  2798. for (i = 0; i < 16; i++)
  2799. {
  2800. sym_p = i & 1;
  2801. var_p = (i >> 1) & 1;
  2802. off_p = (i >> 2) & 1;
  2803. rat_p = (i >> 3) & 1;
  2804. addr = reg0;
  2805. if (rat_p)
  2806. addr = gen_rtx_fmt_ee (MULT, address_mode, addr,
  2807. gen_int_mode (rat, address_mode));
  2808. if (var_p)
  2809. addr = gen_rtx_fmt_ee (PLUS, address_mode, addr, reg1);
  2810. if (sym_p)
  2811. {
  2812. base = gen_rtx_SYMBOL_REF (address_mode, ggc_strdup (""));
  2813. /* ??? We can run into trouble with some backends by presenting
  2814. it with symbols which haven't been properly passed through
  2815. targetm.encode_section_info. By setting the local bit, we
  2816. enhance the probability of things working. */
  2817. SYMBOL_REF_FLAGS (base) = SYMBOL_FLAG_LOCAL;
  2818. if (off_p)
  2819. base = gen_rtx_fmt_e (CONST, address_mode,
  2820. gen_rtx_fmt_ee
  2821. (PLUS, address_mode, base,
  2822. gen_int_mode (off, address_mode)));
  2823. }
  2824. else if (off_p)
  2825. base = gen_int_mode (off, address_mode);
  2826. else
  2827. base = NULL_RTX;
  2828. if (base)
  2829. addr = gen_rtx_fmt_ee (PLUS, address_mode, addr, base);
  2830. start_sequence ();
  2831. /* To avoid splitting addressing modes, pretend that no cse will
  2832. follow. */
  2833. old_cse_not_expected = cse_not_expected;
  2834. cse_not_expected = true;
  2835. addr = memory_address_addr_space (mem_mode, addr, as);
  2836. cse_not_expected = old_cse_not_expected;
  2837. seq = get_insns ();
  2838. end_sequence ();
  2839. acost = seq_cost (seq, speed);
  2840. acost += address_cost (addr, mem_mode, as, speed);
  2841. if (!acost)
  2842. acost = 1;
  2843. data->costs[sym_p][var_p][off_p][rat_p] = acost;
  2844. }
  2845. /* On some targets, it is quite expensive to load symbol to a register,
  2846. which makes addresses that contain symbols look much more expensive.
  2847. However, the symbol will have to be loaded in any case before the
  2848. loop (and quite likely we have it in register already), so it does not
  2849. make much sense to penalize them too heavily. So make some final
  2850. tweaks for the SYMBOL_PRESENT modes:
  2851. If VAR_PRESENT is false, and the mode obtained by changing symbol to
  2852. var is cheaper, use this mode with small penalty.
  2853. If VAR_PRESENT is true, try whether the mode with
  2854. SYMBOL_PRESENT = false is cheaper even with cost of addition, and
  2855. if this is the case, use it. */
  2856. add_c = add_cost (speed, address_mode);
  2857. for (i = 0; i < 8; i++)
  2858. {
  2859. var_p = i & 1;
  2860. off_p = (i >> 1) & 1;
  2861. rat_p = (i >> 2) & 1;
  2862. acost = data->costs[0][1][off_p][rat_p] + 1;
  2863. if (var_p)
  2864. acost += add_c;
  2865. if (acost < data->costs[1][var_p][off_p][rat_p])
  2866. data->costs[1][var_p][off_p][rat_p] = acost;
  2867. }
  2868. if (dump_file && (dump_flags & TDF_DETAILS))
  2869. {
  2870. fprintf (dump_file, "Address costs:\n");
  2871. for (i = 0; i < 16; i++)
  2872. {
  2873. sym_p = i & 1;
  2874. var_p = (i >> 1) & 1;
  2875. off_p = (i >> 2) & 1;
  2876. rat_p = (i >> 3) & 1;
  2877. fprintf (dump_file, " ");
  2878. if (sym_p)
  2879. fprintf (dump_file, "sym + ");
  2880. if (var_p)
  2881. fprintf (dump_file, "var + ");
  2882. if (off_p)
  2883. fprintf (dump_file, "cst + ");
  2884. if (rat_p)
  2885. fprintf (dump_file, "rat * ");
  2886. acost = data->costs[sym_p][var_p][off_p][rat_p];
  2887. fprintf (dump_file, "index costs %d\n", acost);
  2888. }
  2889. if (has_predec[mem_mode] || has_postdec[mem_mode]
  2890. || has_preinc[mem_mode] || has_postinc[mem_mode])
  2891. fprintf (dump_file, " May include autoinc/dec\n");
  2892. fprintf (dump_file, "\n");
  2893. }
  2894. address_cost_data_list[data_index] = data;
  2895. }
  2896. bits = GET_MODE_BITSIZE (address_mode);
  2897. mask = ~(~(unsigned HOST_WIDE_INT) 0 << (bits - 1) << 1);
  2898. offset &= mask;
  2899. if ((offset >> (bits - 1) & 1))
  2900. offset |= ~mask;
  2901. s_offset = offset;
  2902. autoinc = false;
  2903. autoinc_type = AINC_NONE;
  2904. msize = GET_MODE_SIZE (mem_mode);
  2905. autoinc_offset = offset;
  2906. if (stmt_after_inc)
  2907. autoinc_offset += ratio * cstep;
  2908. if (symbol_present || var_present || ratio != 1)
  2909. autoinc = false;
  2910. else
  2911. {
  2912. if (has_postinc[mem_mode] && autoinc_offset == 0
  2913. && msize == cstep)
  2914. autoinc_type = AINC_POST_INC;
  2915. else if (has_postdec[mem_mode] && autoinc_offset == 0
  2916. && msize == -cstep)
  2917. autoinc_type = AINC_POST_DEC;
  2918. else if (has_preinc[mem_mode] && autoinc_offset == msize
  2919. && msize == cstep)
  2920. autoinc_type = AINC_PRE_INC;
  2921. else if (has_predec[mem_mode] && autoinc_offset == -msize
  2922. && msize == -cstep)
  2923. autoinc_type = AINC_PRE_DEC;
  2924. if (autoinc_type != AINC_NONE)
  2925. autoinc = true;
  2926. }
  2927. cost = 0;
  2928. offset_p = (s_offset != 0
  2929. && data->min_offset <= s_offset
  2930. && s_offset <= data->max_offset);
  2931. ratio_p = (ratio != 1
  2932. && multiplier_allowed_in_address_p (ratio, mem_mode, as));
  2933. if (ratio != 1 && !ratio_p)
  2934. cost += mult_by_coeff_cost (ratio, address_mode, speed);
  2935. if (s_offset && !offset_p && !symbol_present)
  2936. cost += add_cost (speed, address_mode);
  2937. if (may_autoinc)
  2938. *may_autoinc = autoinc;
  2939. if (autoinc)
  2940. acost = data->ainc_costs[autoinc_type];
  2941. else
  2942. acost = data->costs[symbol_present][var_present][offset_p][ratio_p];
  2943. complexity = (symbol_present != 0) + (var_present != 0) + offset_p + ratio_p;
  2944. return new_cost (cost + acost, complexity);
  2945. }
  2946. /* Calculate the SPEED or size cost of shiftadd EXPR in MODE. MULT is the
  2947. the EXPR operand holding the shift. COST0 and COST1 are the costs for
  2948. calculating the operands of EXPR. Returns true if successful, and returns
  2949. the cost in COST. */
  2950. static bool
  2951. get_shiftadd_cost (tree expr, machine_mode mode, comp_cost cost0,
  2952. comp_cost cost1, tree mult, bool speed, comp_cost *cost)
  2953. {
  2954. comp_cost res;
  2955. tree op1 = TREE_OPERAND (expr, 1);
  2956. tree cst = TREE_OPERAND (mult, 1);
  2957. tree multop = TREE_OPERAND (mult, 0);
  2958. int m = exact_log2 (int_cst_value (cst));
  2959. int maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (mode));
  2960. int as_cost, sa_cost;
  2961. bool mult_in_op1;
  2962. if (!(m >= 0 && m < maxm))
  2963. return false;
  2964. mult_in_op1 = operand_equal_p (op1, mult, 0);
  2965. as_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
  2966. /* If the target has a cheap shift-and-add or shift-and-sub instruction,
  2967. use that in preference to a shift insn followed by an add insn. */
  2968. sa_cost = (TREE_CODE (expr) != MINUS_EXPR
  2969. ? shiftadd_cost (speed, mode, m)
  2970. : (mult_in_op1
  2971. ? shiftsub1_cost (speed, mode, m)
  2972. : shiftsub0_cost (speed, mode, m)));
  2973. res = new_cost (MIN (as_cost, sa_cost), 0);
  2974. res = add_costs (res, mult_in_op1 ? cost0 : cost1);
  2975. STRIP_NOPS (multop);
  2976. if (!is_gimple_val (multop))
  2977. res = add_costs (res, force_expr_to_var_cost (multop, speed));
  2978. *cost = res;
  2979. return true;
  2980. }
  2981. /* Estimates cost of forcing expression EXPR into a variable. */
  2982. static comp_cost
  2983. force_expr_to_var_cost (tree expr, bool speed)
  2984. {
  2985. static bool costs_initialized = false;
  2986. static unsigned integer_cost [2];
  2987. static unsigned symbol_cost [2];
  2988. static unsigned address_cost [2];
  2989. tree op0, op1;
  2990. comp_cost cost0, cost1, cost;
  2991. machine_mode mode;
  2992. if (!costs_initialized)
  2993. {
  2994. tree type = build_pointer_type (integer_type_node);
  2995. tree var, addr;
  2996. rtx x;
  2997. int i;
  2998. var = create_tmp_var_raw (integer_type_node, "test_var");
  2999. TREE_STATIC (var) = 1;
  3000. x = produce_memory_decl_rtl (var, NULL);
  3001. SET_DECL_RTL (var, x);
  3002. addr = build1 (ADDR_EXPR, type, var);
  3003. for (i = 0; i < 2; i++)
  3004. {
  3005. integer_cost[i] = computation_cost (build_int_cst (integer_type_node,
  3006. 2000), i);
  3007. symbol_cost[i] = computation_cost (addr, i) + 1;
  3008. address_cost[i]
  3009. = computation_cost (fold_build_pointer_plus_hwi (addr, 2000), i) + 1;
  3010. if (dump_file && (dump_flags & TDF_DETAILS))
  3011. {
  3012. fprintf (dump_file, "force_expr_to_var_cost %s costs:\n", i ? "speed" : "size");
  3013. fprintf (dump_file, " integer %d\n", (int) integer_cost[i]);
  3014. fprintf (dump_file, " symbol %d\n", (int) symbol_cost[i]);
  3015. fprintf (dump_file, " address %d\n", (int) address_cost[i]);
  3016. fprintf (dump_file, " other %d\n", (int) target_spill_cost[i]);
  3017. fprintf (dump_file, "\n");
  3018. }
  3019. }
  3020. costs_initialized = true;
  3021. }
  3022. STRIP_NOPS (expr);
  3023. if (SSA_VAR_P (expr))
  3024. return no_cost;
  3025. if (is_gimple_min_invariant (expr))
  3026. {
  3027. if (TREE_CODE (expr) == INTEGER_CST)
  3028. return new_cost (integer_cost [speed], 0);
  3029. if (TREE_CODE (expr) == ADDR_EXPR)
  3030. {
  3031. tree obj = TREE_OPERAND (expr, 0);
  3032. if (TREE_CODE (obj) == VAR_DECL
  3033. || TREE_CODE (obj) == PARM_DECL
  3034. || TREE_CODE (obj) == RESULT_DECL)
  3035. return new_cost (symbol_cost [speed], 0);
  3036. }
  3037. return new_cost (address_cost [speed], 0);
  3038. }
  3039. switch (TREE_CODE (expr))
  3040. {
  3041. case POINTER_PLUS_EXPR:
  3042. case PLUS_EXPR:
  3043. case MINUS_EXPR:
  3044. case MULT_EXPR:
  3045. op0 = TREE_OPERAND (expr, 0);
  3046. op1 = TREE_OPERAND (expr, 1);
  3047. STRIP_NOPS (op0);
  3048. STRIP_NOPS (op1);
  3049. break;
  3050. CASE_CONVERT:
  3051. case NEGATE_EXPR:
  3052. op0 = TREE_OPERAND (expr, 0);
  3053. STRIP_NOPS (op0);
  3054. op1 = NULL_TREE;
  3055. break;
  3056. default:
  3057. /* Just an arbitrary value, FIXME. */
  3058. return new_cost (target_spill_cost[speed], 0);
  3059. }
  3060. if (op0 == NULL_TREE
  3061. || TREE_CODE (op0) == SSA_NAME || CONSTANT_CLASS_P (op0))
  3062. cost0 = no_cost;
  3063. else
  3064. cost0 = force_expr_to_var_cost (op0, speed);
  3065. if (op1 == NULL_TREE
  3066. || TREE_CODE (op1) == SSA_NAME || CONSTANT_CLASS_P (op1))
  3067. cost1 = no_cost;
  3068. else
  3069. cost1 = force_expr_to_var_cost (op1, speed);
  3070. mode = TYPE_MODE (TREE_TYPE (expr));
  3071. switch (TREE_CODE (expr))
  3072. {
  3073. case POINTER_PLUS_EXPR:
  3074. case PLUS_EXPR:
  3075. case MINUS_EXPR:
  3076. case NEGATE_EXPR:
  3077. cost = new_cost (add_cost (speed, mode), 0);
  3078. if (TREE_CODE (expr) != NEGATE_EXPR)
  3079. {
  3080. tree mult = NULL_TREE;
  3081. comp_cost sa_cost;
  3082. if (TREE_CODE (op1) == MULT_EXPR)
  3083. mult = op1;
  3084. else if (TREE_CODE (op0) == MULT_EXPR)
  3085. mult = op0;
  3086. if (mult != NULL_TREE
  3087. && cst_and_fits_in_hwi (TREE_OPERAND (mult, 1))
  3088. && get_shiftadd_cost (expr, mode, cost0, cost1, mult,
  3089. speed, &sa_cost))
  3090. return sa_cost;
  3091. }
  3092. break;
  3093. CASE_CONVERT:
  3094. {
  3095. tree inner_mode, outer_mode;
  3096. outer_mode = TREE_TYPE (expr);
  3097. inner_mode = TREE_TYPE (op0);
  3098. cost = new_cost (convert_cost (TYPE_MODE (outer_mode),
  3099. TYPE_MODE (inner_mode), speed), 0);
  3100. }
  3101. break;
  3102. case MULT_EXPR:
  3103. if (cst_and_fits_in_hwi (op0))
  3104. cost = new_cost (mult_by_coeff_cost (int_cst_value (op0),
  3105. mode, speed), 0);
  3106. else if (cst_and_fits_in_hwi (op1))
  3107. cost = new_cost (mult_by_coeff_cost (int_cst_value (op1),
  3108. mode, speed), 0);
  3109. else
  3110. return new_cost (target_spill_cost [speed], 0);
  3111. break;
  3112. default:
  3113. gcc_unreachable ();
  3114. }
  3115. cost = add_costs (cost, cost0);
  3116. cost = add_costs (cost, cost1);
  3117. /* Bound the cost by target_spill_cost. The parts of complicated
  3118. computations often are either loop invariant or at least can
  3119. be shared between several iv uses, so letting this grow without
  3120. limits would not give reasonable results. */
  3121. if (cost.cost > (int) target_spill_cost [speed])
  3122. cost.cost = target_spill_cost [speed];
  3123. return cost;
  3124. }
  3125. /* Estimates cost of forcing EXPR into a variable. DEPENDS_ON is a set of the
  3126. invariants the computation depends on. */
  3127. static comp_cost
  3128. force_var_cost (struct ivopts_data *data,
  3129. tree expr, bitmap *depends_on)
  3130. {
  3131. if (depends_on)
  3132. {
  3133. fd_ivopts_data = data;
  3134. walk_tree (&expr, find_depends, depends_on, NULL);
  3135. }
  3136. return force_expr_to_var_cost (expr, data->speed);
  3137. }
  3138. /* Estimates cost of expressing address ADDR as var + symbol + offset. The
  3139. value of offset is added to OFFSET, SYMBOL_PRESENT and VAR_PRESENT are set
  3140. to false if the corresponding part is missing. DEPENDS_ON is a set of the
  3141. invariants the computation depends on. */
  3142. static comp_cost
  3143. split_address_cost (struct ivopts_data *data,
  3144. tree addr, bool *symbol_present, bool *var_present,
  3145. unsigned HOST_WIDE_INT *offset, bitmap *depends_on)
  3146. {
  3147. tree core;
  3148. HOST_WIDE_INT bitsize;
  3149. HOST_WIDE_INT bitpos;
  3150. tree toffset;
  3151. machine_mode mode;
  3152. int unsignedp, volatilep;
  3153. core = get_inner_reference (addr, &bitsize, &bitpos, &toffset, &mode,
  3154. &unsignedp, &volatilep, false);
  3155. if (toffset != 0
  3156. || bitpos % BITS_PER_UNIT != 0
  3157. || TREE_CODE (core) != VAR_DECL)
  3158. {
  3159. *symbol_present = false;
  3160. *var_present = true;
  3161. fd_ivopts_data = data;
  3162. walk_tree (&addr, find_depends, depends_on, NULL);
  3163. return new_cost (target_spill_cost[data->speed], 0);
  3164. }
  3165. *offset += bitpos / BITS_PER_UNIT;
  3166. if (TREE_STATIC (core)
  3167. || DECL_EXTERNAL (core))
  3168. {
  3169. *symbol_present = true;
  3170. *var_present = false;
  3171. return no_cost;
  3172. }
  3173. *symbol_present = false;
  3174. *var_present = true;
  3175. return no_cost;
  3176. }
  3177. /* Estimates cost of expressing difference of addresses E1 - E2 as
  3178. var + symbol + offset. The value of offset is added to OFFSET,
  3179. SYMBOL_PRESENT and VAR_PRESENT are set to false if the corresponding
  3180. part is missing. DEPENDS_ON is a set of the invariants the computation
  3181. depends on. */
  3182. static comp_cost
  3183. ptr_difference_cost (struct ivopts_data *data,
  3184. tree e1, tree e2, bool *symbol_present, bool *var_present,
  3185. unsigned HOST_WIDE_INT *offset, bitmap *depends_on)
  3186. {
  3187. HOST_WIDE_INT diff = 0;
  3188. aff_tree aff_e1, aff_e2;
  3189. tree type;
  3190. gcc_assert (TREE_CODE (e1) == ADDR_EXPR);
  3191. if (ptr_difference_const (e1, e2, &diff))
  3192. {
  3193. *offset += diff;
  3194. *symbol_present = false;
  3195. *var_present = false;
  3196. return no_cost;
  3197. }
  3198. if (integer_zerop (e2))
  3199. return split_address_cost (data, TREE_OPERAND (e1, 0),
  3200. symbol_present, var_present, offset, depends_on);
  3201. *symbol_present = false;
  3202. *var_present = true;
  3203. type = signed_type_for (TREE_TYPE (e1));
  3204. tree_to_aff_combination (e1, type, &aff_e1);
  3205. tree_to_aff_combination (e2, type, &aff_e2);
  3206. aff_combination_scale (&aff_e2, -1);
  3207. aff_combination_add (&aff_e1, &aff_e2);
  3208. return force_var_cost (data, aff_combination_to_tree (&aff_e1), depends_on);
  3209. }
  3210. /* Estimates cost of expressing difference E1 - E2 as
  3211. var + symbol + offset. The value of offset is added to OFFSET,
  3212. SYMBOL_PRESENT and VAR_PRESENT are set to false if the corresponding
  3213. part is missing. DEPENDS_ON is a set of the invariants the computation
  3214. depends on. */
  3215. static comp_cost
  3216. difference_cost (struct ivopts_data *data,
  3217. tree e1, tree e2, bool *symbol_present, bool *var_present,
  3218. unsigned HOST_WIDE_INT *offset, bitmap *depends_on)
  3219. {
  3220. machine_mode mode = TYPE_MODE (TREE_TYPE (e1));
  3221. unsigned HOST_WIDE_INT off1, off2;
  3222. aff_tree aff_e1, aff_e2;
  3223. tree type;
  3224. e1 = strip_offset (e1, &off1);
  3225. e2 = strip_offset (e2, &off2);
  3226. *offset += off1 - off2;
  3227. STRIP_NOPS (e1);
  3228. STRIP_NOPS (e2);
  3229. if (TREE_CODE (e1) == ADDR_EXPR)
  3230. return ptr_difference_cost (data, e1, e2, symbol_present, var_present,
  3231. offset, depends_on);
  3232. *symbol_present = false;
  3233. if (operand_equal_p (e1, e2, 0))
  3234. {
  3235. *var_present = false;
  3236. return no_cost;
  3237. }
  3238. *var_present = true;
  3239. if (integer_zerop (e2))
  3240. return force_var_cost (data, e1, depends_on);
  3241. if (integer_zerop (e1))
  3242. {
  3243. comp_cost cost = force_var_cost (data, e2, depends_on);
  3244. cost.cost += mult_by_coeff_cost (-1, mode, data->speed);
  3245. return cost;
  3246. }
  3247. type = signed_type_for (TREE_TYPE (e1));
  3248. tree_to_aff_combination (e1, type, &aff_e1);
  3249. tree_to_aff_combination (e2, type, &aff_e2);
  3250. aff_combination_scale (&aff_e2, -1);
  3251. aff_combination_add (&aff_e1, &aff_e2);
  3252. return force_var_cost (data, aff_combination_to_tree (&aff_e1), depends_on);
  3253. }
  3254. /* Returns true if AFF1 and AFF2 are identical. */
  3255. static bool
  3256. compare_aff_trees (aff_tree *aff1, aff_tree *aff2)
  3257. {
  3258. unsigned i;
  3259. if (aff1->n != aff2->n)
  3260. return false;
  3261. for (i = 0; i < aff1->n; i++)
  3262. {
  3263. if (aff1->elts[i].coef != aff2->elts[i].coef)
  3264. return false;
  3265. if (!operand_equal_p (aff1->elts[i].val, aff2->elts[i].val, 0))
  3266. return false;
  3267. }
  3268. return true;
  3269. }
  3270. /* Stores EXPR in DATA->inv_expr_tab, and assigns it an inv_expr_id. */
  3271. static int
  3272. get_expr_id (struct ivopts_data *data, tree expr)
  3273. {
  3274. struct iv_inv_expr_ent ent;
  3275. struct iv_inv_expr_ent **slot;
  3276. ent.expr = expr;
  3277. ent.hash = iterative_hash_expr (expr, 0);
  3278. slot = data->inv_expr_tab->find_slot (&ent, INSERT);
  3279. if (*slot)
  3280. return (*slot)->id;
  3281. *slot = XNEW (struct iv_inv_expr_ent);
  3282. (*slot)->expr = expr;
  3283. (*slot)->hash = ent.hash;
  3284. (*slot)->id = data->inv_expr_id++;
  3285. return (*slot)->id;
  3286. }
  3287. /* Returns the pseudo expr id if expression UBASE - RATIO * CBASE
  3288. requires a new compiler generated temporary. Returns -1 otherwise.
  3289. ADDRESS_P is a flag indicating if the expression is for address
  3290. computation. */
  3291. static int
  3292. get_loop_invariant_expr_id (struct ivopts_data *data, tree ubase,
  3293. tree cbase, HOST_WIDE_INT ratio,
  3294. bool address_p)
  3295. {
  3296. aff_tree ubase_aff, cbase_aff;
  3297. tree expr, ub, cb;
  3298. STRIP_NOPS (ubase);
  3299. STRIP_NOPS (cbase);
  3300. ub = ubase;
  3301. cb = cbase;
  3302. if ((TREE_CODE (ubase) == INTEGER_CST)
  3303. && (TREE_CODE (cbase) == INTEGER_CST))
  3304. return -1;
  3305. /* Strips the constant part. */
  3306. if (TREE_CODE (ubase) == PLUS_EXPR
  3307. || TREE_CODE (ubase) == MINUS_EXPR
  3308. || TREE_CODE (ubase) == POINTER_PLUS_EXPR)
  3309. {
  3310. if (TREE_CODE (TREE_OPERAND (ubase, 1)) == INTEGER_CST)
  3311. ubase = TREE_OPERAND (ubase, 0);
  3312. }
  3313. /* Strips the constant part. */
  3314. if (TREE_CODE (cbase) == PLUS_EXPR
  3315. || TREE_CODE (cbase) == MINUS_EXPR
  3316. || TREE_CODE (cbase) == POINTER_PLUS_EXPR)
  3317. {
  3318. if (TREE_CODE (TREE_OPERAND (cbase, 1)) == INTEGER_CST)
  3319. cbase = TREE_OPERAND (cbase, 0);
  3320. }
  3321. if (address_p)
  3322. {
  3323. if (((TREE_CODE (ubase) == SSA_NAME)
  3324. || (TREE_CODE (ubase) == ADDR_EXPR
  3325. && is_gimple_min_invariant (ubase)))
  3326. && (TREE_CODE (cbase) == INTEGER_CST))
  3327. return -1;
  3328. if (((TREE_CODE (cbase) == SSA_NAME)
  3329. || (TREE_CODE (cbase) == ADDR_EXPR
  3330. && is_gimple_min_invariant (cbase)))
  3331. && (TREE_CODE (ubase) == INTEGER_CST))
  3332. return -1;
  3333. }
  3334. if (ratio == 1)
  3335. {
  3336. if (operand_equal_p (ubase, cbase, 0))
  3337. return -1;
  3338. if (TREE_CODE (ubase) == ADDR_EXPR
  3339. && TREE_CODE (cbase) == ADDR_EXPR)
  3340. {
  3341. tree usym, csym;
  3342. usym = TREE_OPERAND (ubase, 0);
  3343. csym = TREE_OPERAND (cbase, 0);
  3344. if (TREE_CODE (usym) == ARRAY_REF)
  3345. {
  3346. tree ind = TREE_OPERAND (usym, 1);
  3347. if (TREE_CODE (ind) == INTEGER_CST
  3348. && tree_fits_shwi_p (ind)
  3349. && tree_to_shwi (ind) == 0)
  3350. usym = TREE_OPERAND (usym, 0);
  3351. }
  3352. if (TREE_CODE (csym) == ARRAY_REF)
  3353. {
  3354. tree ind = TREE_OPERAND (csym, 1);
  3355. if (TREE_CODE (ind) == INTEGER_CST
  3356. && tree_fits_shwi_p (ind)
  3357. && tree_to_shwi (ind) == 0)
  3358. csym = TREE_OPERAND (csym, 0);
  3359. }
  3360. if (operand_equal_p (usym, csym, 0))
  3361. return -1;
  3362. }
  3363. /* Now do more complex comparison */
  3364. tree_to_aff_combination (ubase, TREE_TYPE (ubase), &ubase_aff);
  3365. tree_to_aff_combination (cbase, TREE_TYPE (cbase), &cbase_aff);
  3366. if (compare_aff_trees (&ubase_aff, &cbase_aff))
  3367. return -1;
  3368. }
  3369. tree_to_aff_combination (ub, TREE_TYPE (ub), &ubase_aff);
  3370. tree_to_aff_combination (cb, TREE_TYPE (cb), &cbase_aff);
  3371. aff_combination_scale (&cbase_aff, -1 * ratio);
  3372. aff_combination_add (&ubase_aff, &cbase_aff);
  3373. expr = aff_combination_to_tree (&ubase_aff);
  3374. return get_expr_id (data, expr);
  3375. }
  3376. /* Determines the cost of the computation by that USE is expressed
  3377. from induction variable CAND. If ADDRESS_P is true, we just need
  3378. to create an address from it, otherwise we want to get it into
  3379. register. A set of invariants we depend on is stored in
  3380. DEPENDS_ON. AT is the statement at that the value is computed.
  3381. If CAN_AUTOINC is nonnull, use it to record whether autoinc
  3382. addressing is likely. */
  3383. static comp_cost
  3384. get_computation_cost_at (struct ivopts_data *data,
  3385. struct iv_use *use, struct iv_cand *cand,
  3386. bool address_p, bitmap *depends_on, gimple at,
  3387. bool *can_autoinc,
  3388. int *inv_expr_id)
  3389. {
  3390. tree ubase = use->iv->base, ustep = use->iv->step;
  3391. tree cbase, cstep;
  3392. tree utype = TREE_TYPE (ubase), ctype;
  3393. unsigned HOST_WIDE_INT cstepi, offset = 0;
  3394. HOST_WIDE_INT ratio, aratio;
  3395. bool var_present, symbol_present, stmt_is_after_inc;
  3396. comp_cost cost;
  3397. widest_int rat;
  3398. bool speed = optimize_bb_for_speed_p (gimple_bb (at));
  3399. machine_mode mem_mode = (address_p
  3400. ? TYPE_MODE (TREE_TYPE (*use->op_p))
  3401. : VOIDmode);
  3402. *depends_on = NULL;
  3403. /* Only consider real candidates. */
  3404. if (!cand->iv)
  3405. return infinite_cost;
  3406. cbase = cand->iv->base;
  3407. cstep = cand->iv->step;
  3408. ctype = TREE_TYPE (cbase);
  3409. if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype))
  3410. {
  3411. /* We do not have a precision to express the values of use. */
  3412. return infinite_cost;
  3413. }
  3414. if (address_p
  3415. || (use->iv->base_object
  3416. && cand->iv->base_object
  3417. && POINTER_TYPE_P (TREE_TYPE (use->iv->base_object))
  3418. && POINTER_TYPE_P (TREE_TYPE (cand->iv->base_object))))
  3419. {
  3420. /* Do not try to express address of an object with computation based
  3421. on address of a different object. This may cause problems in rtl
  3422. level alias analysis (that does not expect this to be happening,
  3423. as this is illegal in C), and would be unlikely to be useful
  3424. anyway. */
  3425. if (use->iv->base_object
  3426. && cand->iv->base_object
  3427. && !operand_equal_p (use->iv->base_object, cand->iv->base_object, 0))
  3428. return infinite_cost;
  3429. }
  3430. if (TYPE_PRECISION (utype) < TYPE_PRECISION (ctype))
  3431. {
  3432. /* TODO -- add direct handling of this case. */
  3433. goto fallback;
  3434. }
  3435. /* CSTEPI is removed from the offset in case statement is after the
  3436. increment. If the step is not constant, we use zero instead.
  3437. This is a bit imprecise (there is the extra addition), but
  3438. redundancy elimination is likely to transform the code so that
  3439. it uses value of the variable before increment anyway,
  3440. so it is not that much unrealistic. */
  3441. if (cst_and_fits_in_hwi (cstep))
  3442. cstepi = int_cst_value (cstep);
  3443. else
  3444. cstepi = 0;
  3445. if (!constant_multiple_of (ustep, cstep, &rat))
  3446. return infinite_cost;
  3447. if (wi::fits_shwi_p (rat))
  3448. ratio = rat.to_shwi ();
  3449. else
  3450. return infinite_cost;
  3451. STRIP_NOPS (cbase);
  3452. ctype = TREE_TYPE (cbase);
  3453. stmt_is_after_inc = stmt_after_increment (data->current_loop, cand, at);
  3454. /* use = ubase + ratio * (var - cbase). If either cbase is a constant
  3455. or ratio == 1, it is better to handle this like
  3456. ubase - ratio * cbase + ratio * var
  3457. (also holds in the case ratio == -1, TODO. */
  3458. if (cst_and_fits_in_hwi (cbase))
  3459. {
  3460. offset = - ratio * (unsigned HOST_WIDE_INT) int_cst_value (cbase);
  3461. cost = difference_cost (data,
  3462. ubase, build_int_cst (utype, 0),
  3463. &symbol_present, &var_present, &offset,
  3464. depends_on);
  3465. cost.cost /= avg_loop_niter (data->current_loop);
  3466. }
  3467. else if (ratio == 1)
  3468. {
  3469. tree real_cbase = cbase;
  3470. /* Check to see if any adjustment is needed. */
  3471. if (cstepi == 0 && stmt_is_after_inc)
  3472. {
  3473. aff_tree real_cbase_aff;
  3474. aff_tree cstep_aff;
  3475. tree_to_aff_combination (cbase, TREE_TYPE (real_cbase),
  3476. &real_cbase_aff);
  3477. tree_to_aff_combination (cstep, TREE_TYPE (cstep), &cstep_aff);
  3478. aff_combination_add (&real_cbase_aff, &cstep_aff);
  3479. real_cbase = aff_combination_to_tree (&real_cbase_aff);
  3480. }
  3481. cost = difference_cost (data,
  3482. ubase, real_cbase,
  3483. &symbol_present, &var_present, &offset,
  3484. depends_on);
  3485. cost.cost /= avg_loop_niter (data->current_loop);
  3486. }
  3487. else if (address_p
  3488. && !POINTER_TYPE_P (ctype)
  3489. && multiplier_allowed_in_address_p
  3490. (ratio, mem_mode,
  3491. TYPE_ADDR_SPACE (TREE_TYPE (utype))))
  3492. {
  3493. cbase
  3494. = fold_build2 (MULT_EXPR, ctype, cbase, build_int_cst (ctype, ratio));
  3495. cost = difference_cost (data,
  3496. ubase, cbase,
  3497. &symbol_present, &var_present, &offset,
  3498. depends_on);
  3499. cost.cost /= avg_loop_niter (data->current_loop);
  3500. }
  3501. else
  3502. {
  3503. cost = force_var_cost (data, cbase, depends_on);
  3504. cost = add_costs (cost,
  3505. difference_cost (data,
  3506. ubase, build_int_cst (utype, 0),
  3507. &symbol_present, &var_present,
  3508. &offset, depends_on));
  3509. cost.cost /= avg_loop_niter (data->current_loop);
  3510. cost.cost += add_cost (data->speed, TYPE_MODE (ctype));
  3511. }
  3512. if (inv_expr_id)
  3513. {
  3514. *inv_expr_id =
  3515. get_loop_invariant_expr_id (data, ubase, cbase, ratio, address_p);
  3516. /* Clear depends on. */
  3517. if (*inv_expr_id != -1 && depends_on && *depends_on)
  3518. bitmap_clear (*depends_on);
  3519. }
  3520. /* If we are after the increment, the value of the candidate is higher by
  3521. one iteration. */
  3522. if (stmt_is_after_inc)
  3523. offset -= ratio * cstepi;
  3524. /* Now the computation is in shape symbol + var1 + const + ratio * var2.
  3525. (symbol/var1/const parts may be omitted). If we are looking for an
  3526. address, find the cost of addressing this. */
  3527. if (address_p)
  3528. return add_costs (cost,
  3529. get_address_cost (symbol_present, var_present,
  3530. offset, ratio, cstepi,
  3531. mem_mode,
  3532. TYPE_ADDR_SPACE (TREE_TYPE (utype)),
  3533. speed, stmt_is_after_inc,
  3534. can_autoinc));
  3535. /* Otherwise estimate the costs for computing the expression. */
  3536. if (!symbol_present && !var_present && !offset)
  3537. {
  3538. if (ratio != 1)
  3539. cost.cost += mult_by_coeff_cost (ratio, TYPE_MODE (ctype), speed);
  3540. return cost;
  3541. }
  3542. /* Symbol + offset should be compile-time computable so consider that they
  3543. are added once to the variable, if present. */
  3544. if (var_present && (symbol_present || offset))
  3545. cost.cost += adjust_setup_cost (data,
  3546. add_cost (speed, TYPE_MODE (ctype)));
  3547. /* Having offset does not affect runtime cost in case it is added to
  3548. symbol, but it increases complexity. */
  3549. if (offset)
  3550. cost.complexity++;
  3551. cost.cost += add_cost (speed, TYPE_MODE (ctype));
  3552. aratio = ratio > 0 ? ratio : -ratio;
  3553. if (aratio != 1)
  3554. cost.cost += mult_by_coeff_cost (aratio, TYPE_MODE (ctype), speed);
  3555. return cost;
  3556. fallback:
  3557. if (can_autoinc)
  3558. *can_autoinc = false;
  3559. {
  3560. /* Just get the expression, expand it and measure the cost. */
  3561. tree comp = get_computation_at (data->current_loop, use, cand, at);
  3562. if (!comp)
  3563. return infinite_cost;
  3564. if (address_p)
  3565. comp = build_simple_mem_ref (comp);
  3566. return new_cost (computation_cost (comp, speed), 0);
  3567. }
  3568. }
  3569. /* Determines the cost of the computation by that USE is expressed
  3570. from induction variable CAND. If ADDRESS_P is true, we just need
  3571. to create an address from it, otherwise we want to get it into
  3572. register. A set of invariants we depend on is stored in
  3573. DEPENDS_ON. If CAN_AUTOINC is nonnull, use it to record whether
  3574. autoinc addressing is likely. */
  3575. static comp_cost
  3576. get_computation_cost (struct ivopts_data *data,
  3577. struct iv_use *use, struct iv_cand *cand,
  3578. bool address_p, bitmap *depends_on,
  3579. bool *can_autoinc, int *inv_expr_id)
  3580. {
  3581. return get_computation_cost_at (data,
  3582. use, cand, address_p, depends_on, use->stmt,
  3583. can_autoinc, inv_expr_id);
  3584. }
  3585. /* Determines cost of basing replacement of USE on CAND in a generic
  3586. expression. */
  3587. static bool
  3588. determine_use_iv_cost_generic (struct ivopts_data *data,
  3589. struct iv_use *use, struct iv_cand *cand)
  3590. {
  3591. bitmap depends_on;
  3592. comp_cost cost;
  3593. int inv_expr_id = -1;
  3594. /* The simple case first -- if we need to express value of the preserved
  3595. original biv, the cost is 0. This also prevents us from counting the
  3596. cost of increment twice -- once at this use and once in the cost of
  3597. the candidate. */
  3598. if (cand->pos == IP_ORIGINAL
  3599. && cand->incremented_at == use->stmt)
  3600. {
  3601. set_use_iv_cost (data, use, cand, no_cost, NULL, NULL_TREE,
  3602. ERROR_MARK, -1);
  3603. return true;
  3604. }
  3605. cost = get_computation_cost (data, use, cand, false, &depends_on,
  3606. NULL, &inv_expr_id);
  3607. set_use_iv_cost (data, use, cand, cost, depends_on, NULL_TREE, ERROR_MARK,
  3608. inv_expr_id);
  3609. return !infinite_cost_p (cost);
  3610. }
  3611. /* Determines cost of basing replacement of USE on CAND in an address. */
  3612. static bool
  3613. determine_use_iv_cost_address (struct ivopts_data *data,
  3614. struct iv_use *use, struct iv_cand *cand)
  3615. {
  3616. bitmap depends_on;
  3617. bool can_autoinc;
  3618. int inv_expr_id = -1;
  3619. comp_cost cost = get_computation_cost (data, use, cand, true, &depends_on,
  3620. &can_autoinc, &inv_expr_id);
  3621. if (cand->ainc_use == use)
  3622. {
  3623. if (can_autoinc)
  3624. cost.cost -= cand->cost_step;
  3625. /* If we generated the candidate solely for exploiting autoincrement
  3626. opportunities, and it turns out it can't be used, set the cost to
  3627. infinity to make sure we ignore it. */
  3628. else if (cand->pos == IP_AFTER_USE || cand->pos == IP_BEFORE_USE)
  3629. cost = infinite_cost;
  3630. }
  3631. set_use_iv_cost (data, use, cand, cost, depends_on, NULL_TREE, ERROR_MARK,
  3632. inv_expr_id);
  3633. return !infinite_cost_p (cost);
  3634. }
  3635. /* Computes value of candidate CAND at position AT in iteration NITER, and
  3636. stores it to VAL. */
  3637. static void
  3638. cand_value_at (struct loop *loop, struct iv_cand *cand, gimple at, tree niter,
  3639. aff_tree *val)
  3640. {
  3641. aff_tree step, delta, nit;
  3642. struct iv *iv = cand->iv;
  3643. tree type = TREE_TYPE (iv->base);
  3644. tree steptype = type;
  3645. if (POINTER_TYPE_P (type))
  3646. steptype = sizetype;
  3647. steptype = unsigned_type_for (type);
  3648. tree_to_aff_combination (iv->step, TREE_TYPE (iv->step), &step);
  3649. aff_combination_convert (&step, steptype);
  3650. tree_to_aff_combination (niter, TREE_TYPE (niter), &nit);
  3651. aff_combination_convert (&nit, steptype);
  3652. aff_combination_mult (&nit, &step, &delta);
  3653. if (stmt_after_increment (loop, cand, at))
  3654. aff_combination_add (&delta, &step);
  3655. tree_to_aff_combination (iv->base, type, val);
  3656. if (!POINTER_TYPE_P (type))
  3657. aff_combination_convert (val, steptype);
  3658. aff_combination_add (val, &delta);
  3659. }
  3660. /* Returns period of induction variable iv. */
  3661. static tree
  3662. iv_period (struct iv *iv)
  3663. {
  3664. tree step = iv->step, period, type;
  3665. tree pow2div;
  3666. gcc_assert (step && TREE_CODE (step) == INTEGER_CST);
  3667. type = unsigned_type_for (TREE_TYPE (step));
  3668. /* Period of the iv is lcm (step, type_range)/step -1,
  3669. i.e., N*type_range/step - 1. Since type range is power
  3670. of two, N == (step >> num_of_ending_zeros_binary (step),
  3671. so the final result is
  3672. (type_range >> num_of_ending_zeros_binary (step)) - 1
  3673. */
  3674. pow2div = num_ending_zeros (step);
  3675. period = build_low_bits_mask (type,
  3676. (TYPE_PRECISION (type)
  3677. - tree_to_uhwi (pow2div)));
  3678. return period;
  3679. }
  3680. /* Returns the comparison operator used when eliminating the iv USE. */
  3681. static enum tree_code
  3682. iv_elimination_compare (struct ivopts_data *data, struct iv_use *use)
  3683. {
  3684. struct loop *loop = data->current_loop;
  3685. basic_block ex_bb;
  3686. edge exit;
  3687. ex_bb = gimple_bb (use->stmt);
  3688. exit = EDGE_SUCC (ex_bb, 0);
  3689. if (flow_bb_inside_loop_p (loop, exit->dest))
  3690. exit = EDGE_SUCC (ex_bb, 1);
  3691. return (exit->flags & EDGE_TRUE_VALUE ? EQ_EXPR : NE_EXPR);
  3692. }
  3693. /* Returns true if we can prove that BASE - OFFSET does not overflow. For now,
  3694. we only detect the situation that BASE = SOMETHING + OFFSET, where the
  3695. calculation is performed in non-wrapping type.
  3696. TODO: More generally, we could test for the situation that
  3697. BASE = SOMETHING + OFFSET' and OFFSET is between OFFSET' and zero.
  3698. This would require knowing the sign of OFFSET. */
  3699. static bool
  3700. difference_cannot_overflow_p (struct ivopts_data *data, tree base, tree offset)
  3701. {
  3702. enum tree_code code;
  3703. tree e1, e2;
  3704. aff_tree aff_e1, aff_e2, aff_offset;
  3705. if (!nowrap_type_p (TREE_TYPE (base)))
  3706. return false;
  3707. base = expand_simple_operations (base);
  3708. if (TREE_CODE (base) == SSA_NAME)
  3709. {
  3710. gimple stmt = SSA_NAME_DEF_STMT (base);
  3711. if (gimple_code (stmt) != GIMPLE_ASSIGN)
  3712. return false;
  3713. code = gimple_assign_rhs_code (stmt);
  3714. if (get_gimple_rhs_class (code) != GIMPLE_BINARY_RHS)
  3715. return false;
  3716. e1 = gimple_assign_rhs1 (stmt);
  3717. e2 = gimple_assign_rhs2 (stmt);
  3718. }
  3719. else
  3720. {
  3721. code = TREE_CODE (base);
  3722. if (get_gimple_rhs_class (code) != GIMPLE_BINARY_RHS)
  3723. return false;
  3724. e1 = TREE_OPERAND (base, 0);
  3725. e2 = TREE_OPERAND (base, 1);
  3726. }
  3727. /* Use affine expansion as deeper inspection to prove the equality. */
  3728. tree_to_aff_combination_expand (e2, TREE_TYPE (e2),
  3729. &aff_e2, &data->name_expansion_cache);
  3730. tree_to_aff_combination_expand (offset, TREE_TYPE (offset),
  3731. &aff_offset, &data->name_expansion_cache);
  3732. aff_combination_scale (&aff_offset, -1);
  3733. switch (code)
  3734. {
  3735. case PLUS_EXPR:
  3736. aff_combination_add (&aff_e2, &aff_offset);
  3737. if (aff_combination_zero_p (&aff_e2))
  3738. return true;
  3739. tree_to_aff_combination_expand (e1, TREE_TYPE (e1),
  3740. &aff_e1, &data->name_expansion_cache);
  3741. aff_combination_add (&aff_e1, &aff_offset);
  3742. return aff_combination_zero_p (&aff_e1);
  3743. case POINTER_PLUS_EXPR:
  3744. aff_combination_add (&aff_e2, &aff_offset);
  3745. return aff_combination_zero_p (&aff_e2);
  3746. default:
  3747. return false;
  3748. }
  3749. }
  3750. /* Tries to replace loop exit by one formulated in terms of a LT_EXPR
  3751. comparison with CAND. NITER describes the number of iterations of
  3752. the loops. If successful, the comparison in COMP_P is altered accordingly.
  3753. We aim to handle the following situation:
  3754. sometype *base, *p;
  3755. int a, b, i;
  3756. i = a;
  3757. p = p_0 = base + a;
  3758. do
  3759. {
  3760. bla (*p);
  3761. p++;
  3762. i++;
  3763. }
  3764. while (i < b);
  3765. Here, the number of iterations of the loop is (a + 1 > b) ? 0 : b - a - 1.
  3766. We aim to optimize this to
  3767. p = p_0 = base + a;
  3768. do
  3769. {
  3770. bla (*p);
  3771. p++;
  3772. }
  3773. while (p < p_0 - a + b);
  3774. This preserves the correctness, since the pointer arithmetics does not
  3775. overflow. More precisely:
  3776. 1) if a + 1 <= b, then p_0 - a + b is the final value of p, hence there is no
  3777. overflow in computing it or the values of p.
  3778. 2) if a + 1 > b, then we need to verify that the expression p_0 - a does not
  3779. overflow. To prove this, we use the fact that p_0 = base + a. */
  3780. static bool
  3781. iv_elimination_compare_lt (struct ivopts_data *data,
  3782. struct iv_cand *cand, enum tree_code *comp_p,
  3783. struct tree_niter_desc *niter)
  3784. {
  3785. tree cand_type, a, b, mbz, nit_type = TREE_TYPE (niter->niter), offset;
  3786. struct aff_tree nit, tmpa, tmpb;
  3787. enum tree_code comp;
  3788. HOST_WIDE_INT step;
  3789. /* We need to know that the candidate induction variable does not overflow.
  3790. While more complex analysis may be used to prove this, for now just
  3791. check that the variable appears in the original program and that it
  3792. is computed in a type that guarantees no overflows. */
  3793. cand_type = TREE_TYPE (cand->iv->base);
  3794. if (cand->pos != IP_ORIGINAL || !nowrap_type_p (cand_type))
  3795. return false;
  3796. /* Make sure that the loop iterates till the loop bound is hit, as otherwise
  3797. the calculation of the BOUND could overflow, making the comparison
  3798. invalid. */
  3799. if (!data->loop_single_exit_p)
  3800. return false;
  3801. /* We need to be able to decide whether candidate is increasing or decreasing
  3802. in order to choose the right comparison operator. */
  3803. if (!cst_and_fits_in_hwi (cand->iv->step))
  3804. return false;
  3805. step = int_cst_value (cand->iv->step);
  3806. /* Check that the number of iterations matches the expected pattern:
  3807. a + 1 > b ? 0 : b - a - 1. */
  3808. mbz = niter->may_be_zero;
  3809. if (TREE_CODE (mbz) == GT_EXPR)
  3810. {
  3811. /* Handle a + 1 > b. */
  3812. tree op0 = TREE_OPERAND (mbz, 0);
  3813. if (TREE_CODE (op0) == PLUS_EXPR && integer_onep (TREE_OPERAND (op0, 1)))
  3814. {
  3815. a = TREE_OPERAND (op0, 0);
  3816. b = TREE_OPERAND (mbz, 1);
  3817. }
  3818. else
  3819. return false;
  3820. }
  3821. else if (TREE_CODE (mbz) == LT_EXPR)
  3822. {
  3823. tree op1 = TREE_OPERAND (mbz, 1);
  3824. /* Handle b < a + 1. */
  3825. if (TREE_CODE (op1) == PLUS_EXPR && integer_onep (TREE_OPERAND (op1, 1)))
  3826. {
  3827. a = TREE_OPERAND (op1, 0);
  3828. b = TREE_OPERAND (mbz, 0);
  3829. }
  3830. else
  3831. return false;
  3832. }
  3833. else
  3834. return false;
  3835. /* Expected number of iterations is B - A - 1. Check that it matches
  3836. the actual number, i.e., that B - A - NITER = 1. */
  3837. tree_to_aff_combination (niter->niter, nit_type, &nit);
  3838. tree_to_aff_combination (fold_convert (nit_type, a), nit_type, &tmpa);
  3839. tree_to_aff_combination (fold_convert (nit_type, b), nit_type, &tmpb);
  3840. aff_combination_scale (&nit, -1);
  3841. aff_combination_scale (&tmpa, -1);
  3842. aff_combination_add (&tmpb, &tmpa);
  3843. aff_combination_add (&tmpb, &nit);
  3844. if (tmpb.n != 0 || tmpb.offset != 1)
  3845. return false;
  3846. /* Finally, check that CAND->IV->BASE - CAND->IV->STEP * A does not
  3847. overflow. */
  3848. offset = fold_build2 (MULT_EXPR, TREE_TYPE (cand->iv->step),
  3849. cand->iv->step,
  3850. fold_convert (TREE_TYPE (cand->iv->step), a));
  3851. if (!difference_cannot_overflow_p (data, cand->iv->base, offset))
  3852. return false;
  3853. /* Determine the new comparison operator. */
  3854. comp = step < 0 ? GT_EXPR : LT_EXPR;
  3855. if (*comp_p == NE_EXPR)
  3856. *comp_p = comp;
  3857. else if (*comp_p == EQ_EXPR)
  3858. *comp_p = invert_tree_comparison (comp, false);
  3859. else
  3860. gcc_unreachable ();
  3861. return true;
  3862. }
  3863. /* Check whether it is possible to express the condition in USE by comparison
  3864. of candidate CAND. If so, store the value compared with to BOUND, and the
  3865. comparison operator to COMP. */
  3866. static bool
  3867. may_eliminate_iv (struct ivopts_data *data,
  3868. struct iv_use *use, struct iv_cand *cand, tree *bound,
  3869. enum tree_code *comp)
  3870. {
  3871. basic_block ex_bb;
  3872. edge exit;
  3873. tree period;
  3874. struct loop *loop = data->current_loop;
  3875. aff_tree bnd;
  3876. struct tree_niter_desc *desc = NULL;
  3877. if (TREE_CODE (cand->iv->step) != INTEGER_CST)
  3878. return false;
  3879. /* For now works only for exits that dominate the loop latch.
  3880. TODO: extend to other conditions inside loop body. */
  3881. ex_bb = gimple_bb (use->stmt);
  3882. if (use->stmt != last_stmt (ex_bb)
  3883. || gimple_code (use->stmt) != GIMPLE_COND
  3884. || !dominated_by_p (CDI_DOMINATORS, loop->latch, ex_bb))
  3885. return false;
  3886. exit = EDGE_SUCC (ex_bb, 0);
  3887. if (flow_bb_inside_loop_p (loop, exit->dest))
  3888. exit = EDGE_SUCC (ex_bb, 1);
  3889. if (flow_bb_inside_loop_p (loop, exit->dest))
  3890. return false;
  3891. desc = niter_for_exit (data, exit);
  3892. if (!desc)
  3893. return false;
  3894. /* Determine whether we can use the variable to test the exit condition.
  3895. This is the case iff the period of the induction variable is greater
  3896. than the number of iterations for which the exit condition is true. */
  3897. period = iv_period (cand->iv);
  3898. /* If the number of iterations is constant, compare against it directly. */
  3899. if (TREE_CODE (desc->niter) == INTEGER_CST)
  3900. {
  3901. /* See cand_value_at. */
  3902. if (stmt_after_increment (loop, cand, use->stmt))
  3903. {
  3904. if (!tree_int_cst_lt (desc->niter, period))
  3905. return false;
  3906. }
  3907. else
  3908. {
  3909. if (tree_int_cst_lt (period, desc->niter))
  3910. return false;
  3911. }
  3912. }
  3913. /* If not, and if this is the only possible exit of the loop, see whether
  3914. we can get a conservative estimate on the number of iterations of the
  3915. entire loop and compare against that instead. */
  3916. else
  3917. {
  3918. widest_int period_value, max_niter;
  3919. max_niter = desc->max;
  3920. if (stmt_after_increment (loop, cand, use->stmt))
  3921. max_niter += 1;
  3922. period_value = wi::to_widest (period);
  3923. if (wi::gtu_p (max_niter, period_value))
  3924. {
  3925. /* See if we can take advantage of inferred loop bound information. */
  3926. if (data->loop_single_exit_p)
  3927. {
  3928. if (!max_loop_iterations (loop, &max_niter))
  3929. return false;
  3930. /* The loop bound is already adjusted by adding 1. */
  3931. if (wi::gtu_p (max_niter, period_value))
  3932. return false;
  3933. }
  3934. else
  3935. return false;
  3936. }
  3937. }
  3938. cand_value_at (loop, cand, use->stmt, desc->niter, &bnd);
  3939. *bound = fold_convert (TREE_TYPE (cand->iv->base),
  3940. aff_combination_to_tree (&bnd));
  3941. *comp = iv_elimination_compare (data, use);
  3942. /* It is unlikely that computing the number of iterations using division
  3943. would be more profitable than keeping the original induction variable. */
  3944. if (expression_expensive_p (*bound))
  3945. return false;
  3946. /* Sometimes, it is possible to handle the situation that the number of
  3947. iterations may be zero unless additional assumtions by using <
  3948. instead of != in the exit condition.
  3949. TODO: we could also calculate the value MAY_BE_ZERO ? 0 : NITER and
  3950. base the exit condition on it. However, that is often too
  3951. expensive. */
  3952. if (!integer_zerop (desc->may_be_zero))
  3953. return iv_elimination_compare_lt (data, cand, comp, desc);
  3954. return true;
  3955. }
  3956. /* Calculates the cost of BOUND, if it is a PARM_DECL. A PARM_DECL must
  3957. be copied, if is is used in the loop body and DATA->body_includes_call. */
  3958. static int
  3959. parm_decl_cost (struct ivopts_data *data, tree bound)
  3960. {
  3961. tree sbound = bound;
  3962. STRIP_NOPS (sbound);
  3963. if (TREE_CODE (sbound) == SSA_NAME
  3964. && SSA_NAME_IS_DEFAULT_DEF (sbound)
  3965. && TREE_CODE (SSA_NAME_VAR (sbound)) == PARM_DECL
  3966. && data->body_includes_call)
  3967. return COSTS_N_INSNS (1);
  3968. return 0;
  3969. }
  3970. /* Determines cost of basing replacement of USE on CAND in a condition. */
  3971. static bool
  3972. determine_use_iv_cost_condition (struct ivopts_data *data,
  3973. struct iv_use *use, struct iv_cand *cand)
  3974. {
  3975. tree bound = NULL_TREE;
  3976. struct iv *cmp_iv;
  3977. bitmap depends_on_elim = NULL, depends_on_express = NULL, depends_on;
  3978. comp_cost elim_cost, express_cost, cost, bound_cost;
  3979. bool ok;
  3980. int elim_inv_expr_id = -1, express_inv_expr_id = -1, inv_expr_id;
  3981. tree *control_var, *bound_cst;
  3982. enum tree_code comp = ERROR_MARK;
  3983. /* Only consider real candidates. */
  3984. if (!cand->iv)
  3985. {
  3986. set_use_iv_cost (data, use, cand, infinite_cost, NULL, NULL_TREE,
  3987. ERROR_MARK, -1);
  3988. return false;
  3989. }
  3990. /* Try iv elimination. */
  3991. if (may_eliminate_iv (data, use, cand, &bound, &comp))
  3992. {
  3993. elim_cost = force_var_cost (data, bound, &depends_on_elim);
  3994. if (elim_cost.cost == 0)
  3995. elim_cost.cost = parm_decl_cost (data, bound);
  3996. else if (TREE_CODE (bound) == INTEGER_CST)
  3997. elim_cost.cost = 0;
  3998. /* If we replace a loop condition 'i < n' with 'p < base + n',
  3999. depends_on_elim will have 'base' and 'n' set, which implies
  4000. that both 'base' and 'n' will be live during the loop. More likely,
  4001. 'base + n' will be loop invariant, resulting in only one live value
  4002. during the loop. So in that case we clear depends_on_elim and set
  4003. elim_inv_expr_id instead. */
  4004. if (depends_on_elim && bitmap_count_bits (depends_on_elim) > 1)
  4005. {
  4006. elim_inv_expr_id = get_expr_id (data, bound);
  4007. bitmap_clear (depends_on_elim);
  4008. }
  4009. /* The bound is a loop invariant, so it will be only computed
  4010. once. */
  4011. elim_cost.cost = adjust_setup_cost (data, elim_cost.cost);
  4012. }
  4013. else
  4014. elim_cost = infinite_cost;
  4015. /* Try expressing the original giv. If it is compared with an invariant,
  4016. note that we cannot get rid of it. */
  4017. ok = extract_cond_operands (data, use->stmt, &control_var, &bound_cst,
  4018. NULL, &cmp_iv);
  4019. gcc_assert (ok);
  4020. /* When the condition is a comparison of the candidate IV against
  4021. zero, prefer this IV.
  4022. TODO: The constant that we're subtracting from the cost should
  4023. be target-dependent. This information should be added to the
  4024. target costs for each backend. */
  4025. if (!infinite_cost_p (elim_cost) /* Do not try to decrease infinite! */
  4026. && integer_zerop (*bound_cst)
  4027. && (operand_equal_p (*control_var, cand->var_after, 0)
  4028. || operand_equal_p (*control_var, cand->var_before, 0)))
  4029. elim_cost.cost -= 1;
  4030. express_cost = get_computation_cost (data, use, cand, false,
  4031. &depends_on_express, NULL,
  4032. &express_inv_expr_id);
  4033. fd_ivopts_data = data;
  4034. walk_tree (&cmp_iv->base, find_depends, &depends_on_express, NULL);
  4035. /* Count the cost of the original bound as well. */
  4036. bound_cost = force_var_cost (data, *bound_cst, NULL);
  4037. if (bound_cost.cost == 0)
  4038. bound_cost.cost = parm_decl_cost (data, *bound_cst);
  4039. else if (TREE_CODE (*bound_cst) == INTEGER_CST)
  4040. bound_cost.cost = 0;
  4041. express_cost.cost += bound_cost.cost;
  4042. /* Choose the better approach, preferring the eliminated IV. */
  4043. if (compare_costs (elim_cost, express_cost) <= 0)
  4044. {
  4045. cost = elim_cost;
  4046. depends_on = depends_on_elim;
  4047. depends_on_elim = NULL;
  4048. inv_expr_id = elim_inv_expr_id;
  4049. }
  4050. else
  4051. {
  4052. cost = express_cost;
  4053. depends_on = depends_on_express;
  4054. depends_on_express = NULL;
  4055. bound = NULL_TREE;
  4056. comp = ERROR_MARK;
  4057. inv_expr_id = express_inv_expr_id;
  4058. }
  4059. set_use_iv_cost (data, use, cand, cost, depends_on, bound, comp, inv_expr_id);
  4060. if (depends_on_elim)
  4061. BITMAP_FREE (depends_on_elim);
  4062. if (depends_on_express)
  4063. BITMAP_FREE (depends_on_express);
  4064. return !infinite_cost_p (cost);
  4065. }
  4066. /* Determines cost of basing replacement of USE on CAND. Returns false
  4067. if USE cannot be based on CAND. */
  4068. static bool
  4069. determine_use_iv_cost (struct ivopts_data *data,
  4070. struct iv_use *use, struct iv_cand *cand)
  4071. {
  4072. switch (use->type)
  4073. {
  4074. case USE_NONLINEAR_EXPR:
  4075. return determine_use_iv_cost_generic (data, use, cand);
  4076. case USE_ADDRESS:
  4077. return determine_use_iv_cost_address (data, use, cand);
  4078. case USE_COMPARE:
  4079. return determine_use_iv_cost_condition (data, use, cand);
  4080. default:
  4081. gcc_unreachable ();
  4082. }
  4083. }
  4084. /* Return true if get_computation_cost indicates that autoincrement is
  4085. a possibility for the pair of USE and CAND, false otherwise. */
  4086. static bool
  4087. autoinc_possible_for_pair (struct ivopts_data *data, struct iv_use *use,
  4088. struct iv_cand *cand)
  4089. {
  4090. bitmap depends_on;
  4091. bool can_autoinc;
  4092. comp_cost cost;
  4093. if (use->type != USE_ADDRESS)
  4094. return false;
  4095. cost = get_computation_cost (data, use, cand, true, &depends_on,
  4096. &can_autoinc, NULL);
  4097. BITMAP_FREE (depends_on);
  4098. return !infinite_cost_p (cost) && can_autoinc;
  4099. }
  4100. /* Examine IP_ORIGINAL candidates to see if they are incremented next to a
  4101. use that allows autoincrement, and set their AINC_USE if possible. */
  4102. static void
  4103. set_autoinc_for_original_candidates (struct ivopts_data *data)
  4104. {
  4105. unsigned i, j;
  4106. for (i = 0; i < n_iv_cands (data); i++)
  4107. {
  4108. struct iv_cand *cand = iv_cand (data, i);
  4109. struct iv_use *closest_before = NULL;
  4110. struct iv_use *closest_after = NULL;
  4111. if (cand->pos != IP_ORIGINAL)
  4112. continue;
  4113. for (j = 0; j < n_iv_uses (data); j++)
  4114. {
  4115. struct iv_use *use = iv_use (data, j);
  4116. unsigned uid = gimple_uid (use->stmt);
  4117. if (gimple_bb (use->stmt) != gimple_bb (cand->incremented_at))
  4118. continue;
  4119. if (uid < gimple_uid (cand->incremented_at)
  4120. && (closest_before == NULL
  4121. || uid > gimple_uid (closest_before->stmt)))
  4122. closest_before = use;
  4123. if (uid > gimple_uid (cand->incremented_at)
  4124. && (closest_after == NULL
  4125. || uid < gimple_uid (closest_after->stmt)))
  4126. closest_after = use;
  4127. }
  4128. if (closest_before != NULL
  4129. && autoinc_possible_for_pair (data, closest_before, cand))
  4130. cand->ainc_use = closest_before;
  4131. else if (closest_after != NULL
  4132. && autoinc_possible_for_pair (data, closest_after, cand))
  4133. cand->ainc_use = closest_after;
  4134. }
  4135. }
  4136. /* Finds the candidates for the induction variables. */
  4137. static void
  4138. find_iv_candidates (struct ivopts_data *data)
  4139. {
  4140. /* Add commonly used ivs. */
  4141. add_standard_iv_candidates (data);
  4142. /* Add old induction variables. */
  4143. add_old_ivs_candidates (data);
  4144. /* Add induction variables derived from uses. */
  4145. add_derived_ivs_candidates (data);
  4146. set_autoinc_for_original_candidates (data);
  4147. /* Record the important candidates. */
  4148. record_important_candidates (data);
  4149. }
  4150. /* Determines costs of basing the use of the iv on an iv candidate. */
  4151. static void
  4152. determine_use_iv_costs (struct ivopts_data *data)
  4153. {
  4154. unsigned i, j;
  4155. struct iv_use *use;
  4156. struct iv_cand *cand;
  4157. bitmap to_clear = BITMAP_ALLOC (NULL);
  4158. alloc_use_cost_map (data);
  4159. for (i = 0; i < n_iv_uses (data); i++)
  4160. {
  4161. use = iv_use (data, i);
  4162. if (data->consider_all_candidates)
  4163. {
  4164. for (j = 0; j < n_iv_cands (data); j++)
  4165. {
  4166. cand = iv_cand (data, j);
  4167. determine_use_iv_cost (data, use, cand);
  4168. }
  4169. }
  4170. else
  4171. {
  4172. bitmap_iterator bi;
  4173. EXECUTE_IF_SET_IN_BITMAP (use->related_cands, 0, j, bi)
  4174. {
  4175. cand = iv_cand (data, j);
  4176. if (!determine_use_iv_cost (data, use, cand))
  4177. bitmap_set_bit (to_clear, j);
  4178. }
  4179. /* Remove the candidates for that the cost is infinite from
  4180. the list of related candidates. */
  4181. bitmap_and_compl_into (use->related_cands, to_clear);
  4182. bitmap_clear (to_clear);
  4183. }
  4184. }
  4185. BITMAP_FREE (to_clear);
  4186. if (dump_file && (dump_flags & TDF_DETAILS))
  4187. {
  4188. fprintf (dump_file, "Use-candidate costs:\n");
  4189. for (i = 0; i < n_iv_uses (data); i++)
  4190. {
  4191. use = iv_use (data, i);
  4192. fprintf (dump_file, "Use %d:\n", i);
  4193. fprintf (dump_file, " cand\tcost\tcompl.\tdepends on\n");
  4194. for (j = 0; j < use->n_map_members; j++)
  4195. {
  4196. if (!use->cost_map[j].cand
  4197. || infinite_cost_p (use->cost_map[j].cost))
  4198. continue;
  4199. fprintf (dump_file, " %d\t%d\t%d\t",
  4200. use->cost_map[j].cand->id,
  4201. use->cost_map[j].cost.cost,
  4202. use->cost_map[j].cost.complexity);
  4203. if (use->cost_map[j].depends_on)
  4204. bitmap_print (dump_file,
  4205. use->cost_map[j].depends_on, "","");
  4206. if (use->cost_map[j].inv_expr_id != -1)
  4207. fprintf (dump_file, " inv_expr:%d", use->cost_map[j].inv_expr_id);
  4208. fprintf (dump_file, "\n");
  4209. }
  4210. fprintf (dump_file, "\n");
  4211. }
  4212. fprintf (dump_file, "\n");
  4213. }
  4214. }
  4215. /* Determines cost of the candidate CAND. */
  4216. static void
  4217. determine_iv_cost (struct ivopts_data *data, struct iv_cand *cand)
  4218. {
  4219. comp_cost cost_base;
  4220. unsigned cost, cost_step;
  4221. tree base;
  4222. if (!cand->iv)
  4223. {
  4224. cand->cost = 0;
  4225. return;
  4226. }
  4227. /* There are two costs associated with the candidate -- its increment
  4228. and its initialization. The second is almost negligible for any loop
  4229. that rolls enough, so we take it just very little into account. */
  4230. base = cand->iv->base;
  4231. cost_base = force_var_cost (data, base, NULL);
  4232. /* It will be exceptional that the iv register happens to be initialized with
  4233. the proper value at no cost. In general, there will at least be a regcopy
  4234. or a const set. */
  4235. if (cost_base.cost == 0)
  4236. cost_base.cost = COSTS_N_INSNS (1);
  4237. cost_step = add_cost (data->speed, TYPE_MODE (TREE_TYPE (base)));
  4238. cost = cost_step + adjust_setup_cost (data, cost_base.cost);
  4239. /* Prefer the original ivs unless we may gain something by replacing it.
  4240. The reason is to make debugging simpler; so this is not relevant for
  4241. artificial ivs created by other optimization passes. */
  4242. if (cand->pos != IP_ORIGINAL
  4243. || !SSA_NAME_VAR (cand->var_before)
  4244. || DECL_ARTIFICIAL (SSA_NAME_VAR (cand->var_before)))
  4245. cost++;
  4246. /* Prefer not to insert statements into latch unless there are some
  4247. already (so that we do not create unnecessary jumps). */
  4248. if (cand->pos == IP_END
  4249. && empty_block_p (ip_end_pos (data->current_loop)))
  4250. cost++;
  4251. cand->cost = cost;
  4252. cand->cost_step = cost_step;
  4253. }
  4254. /* Determines costs of computation of the candidates. */
  4255. static void
  4256. determine_iv_costs (struct ivopts_data *data)
  4257. {
  4258. unsigned i;
  4259. if (dump_file && (dump_flags & TDF_DETAILS))
  4260. {
  4261. fprintf (dump_file, "Candidate costs:\n");
  4262. fprintf (dump_file, " cand\tcost\n");
  4263. }
  4264. for (i = 0; i < n_iv_cands (data); i++)
  4265. {
  4266. struct iv_cand *cand = iv_cand (data, i);
  4267. determine_iv_cost (data, cand);
  4268. if (dump_file && (dump_flags & TDF_DETAILS))
  4269. fprintf (dump_file, " %d\t%d\n", i, cand->cost);
  4270. }
  4271. if (dump_file && (dump_flags & TDF_DETAILS))
  4272. fprintf (dump_file, "\n");
  4273. }
  4274. /* Calculates cost for having SIZE induction variables. */
  4275. static unsigned
  4276. ivopts_global_cost_for_size (struct ivopts_data *data, unsigned size)
  4277. {
  4278. /* We add size to the cost, so that we prefer eliminating ivs
  4279. if possible. */
  4280. return size + estimate_reg_pressure_cost (size, data->regs_used, data->speed,
  4281. data->body_includes_call);
  4282. }
  4283. /* For each size of the induction variable set determine the penalty. */
  4284. static void
  4285. determine_set_costs (struct ivopts_data *data)
  4286. {
  4287. unsigned j, n;
  4288. gphi *phi;
  4289. gphi_iterator psi;
  4290. tree op;
  4291. struct loop *loop = data->current_loop;
  4292. bitmap_iterator bi;
  4293. if (dump_file && (dump_flags & TDF_DETAILS))
  4294. {
  4295. fprintf (dump_file, "Global costs:\n");
  4296. fprintf (dump_file, " target_avail_regs %d\n", target_avail_regs);
  4297. fprintf (dump_file, " target_clobbered_regs %d\n", target_clobbered_regs);
  4298. fprintf (dump_file, " target_reg_cost %d\n", target_reg_cost[data->speed]);
  4299. fprintf (dump_file, " target_spill_cost %d\n", target_spill_cost[data->speed]);
  4300. }
  4301. n = 0;
  4302. for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
  4303. {
  4304. phi = psi.phi ();
  4305. op = PHI_RESULT (phi);
  4306. if (virtual_operand_p (op))
  4307. continue;
  4308. if (get_iv (data, op))
  4309. continue;
  4310. n++;
  4311. }
  4312. EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
  4313. {
  4314. struct version_info *info = ver_info (data, j);
  4315. if (info->inv_id && info->has_nonlin_use)
  4316. n++;
  4317. }
  4318. data->regs_used = n;
  4319. if (dump_file && (dump_flags & TDF_DETAILS))
  4320. fprintf (dump_file, " regs_used %d\n", n);
  4321. if (dump_file && (dump_flags & TDF_DETAILS))
  4322. {
  4323. fprintf (dump_file, " cost for size:\n");
  4324. fprintf (dump_file, " ivs\tcost\n");
  4325. for (j = 0; j <= 2 * target_avail_regs; j++)
  4326. fprintf (dump_file, " %d\t%d\n", j,
  4327. ivopts_global_cost_for_size (data, j));
  4328. fprintf (dump_file, "\n");
  4329. }
  4330. }
  4331. /* Returns true if A is a cheaper cost pair than B. */
  4332. static bool
  4333. cheaper_cost_pair (struct cost_pair *a, struct cost_pair *b)
  4334. {
  4335. int cmp;
  4336. if (!a)
  4337. return false;
  4338. if (!b)
  4339. return true;
  4340. cmp = compare_costs (a->cost, b->cost);
  4341. if (cmp < 0)
  4342. return true;
  4343. if (cmp > 0)
  4344. return false;
  4345. /* In case the costs are the same, prefer the cheaper candidate. */
  4346. if (a->cand->cost < b->cand->cost)
  4347. return true;
  4348. return false;
  4349. }
  4350. /* Returns candidate by that USE is expressed in IVS. */
  4351. static struct cost_pair *
  4352. iv_ca_cand_for_use (struct iv_ca *ivs, struct iv_use *use)
  4353. {
  4354. return ivs->cand_for_use[use->id];
  4355. }
  4356. /* Computes the cost field of IVS structure. */
  4357. static void
  4358. iv_ca_recount_cost (struct ivopts_data *data, struct iv_ca *ivs)
  4359. {
  4360. comp_cost cost = ivs->cand_use_cost;
  4361. cost.cost += ivs->cand_cost;
  4362. cost.cost += ivopts_global_cost_for_size (data,
  4363. ivs->n_regs + ivs->num_used_inv_expr);
  4364. ivs->cost = cost;
  4365. }
  4366. /* Remove invariants in set INVS to set IVS. */
  4367. static void
  4368. iv_ca_set_remove_invariants (struct iv_ca *ivs, bitmap invs)
  4369. {
  4370. bitmap_iterator bi;
  4371. unsigned iid;
  4372. if (!invs)
  4373. return;
  4374. EXECUTE_IF_SET_IN_BITMAP (invs, 0, iid, bi)
  4375. {
  4376. ivs->n_invariant_uses[iid]--;
  4377. if (ivs->n_invariant_uses[iid] == 0)
  4378. ivs->n_regs--;
  4379. }
  4380. }
  4381. /* Set USE not to be expressed by any candidate in IVS. */
  4382. static void
  4383. iv_ca_set_no_cp (struct ivopts_data *data, struct iv_ca *ivs,
  4384. struct iv_use *use)
  4385. {
  4386. unsigned uid = use->id, cid;
  4387. struct cost_pair *cp;
  4388. cp = ivs->cand_for_use[uid];
  4389. if (!cp)
  4390. return;
  4391. cid = cp->cand->id;
  4392. ivs->bad_uses++;
  4393. ivs->cand_for_use[uid] = NULL;
  4394. ivs->n_cand_uses[cid]--;
  4395. if (ivs->n_cand_uses[cid] == 0)
  4396. {
  4397. bitmap_clear_bit (ivs->cands, cid);
  4398. /* Do not count the pseudocandidates. */
  4399. if (cp->cand->iv)
  4400. ivs->n_regs--;
  4401. ivs->n_cands--;
  4402. ivs->cand_cost -= cp->cand->cost;
  4403. iv_ca_set_remove_invariants (ivs, cp->cand->depends_on);
  4404. }
  4405. ivs->cand_use_cost = sub_costs (ivs->cand_use_cost, cp->cost);
  4406. iv_ca_set_remove_invariants (ivs, cp->depends_on);
  4407. if (cp->inv_expr_id != -1)
  4408. {
  4409. ivs->used_inv_expr[cp->inv_expr_id]--;
  4410. if (ivs->used_inv_expr[cp->inv_expr_id] == 0)
  4411. ivs->num_used_inv_expr--;
  4412. }
  4413. iv_ca_recount_cost (data, ivs);
  4414. }
  4415. /* Add invariants in set INVS to set IVS. */
  4416. static void
  4417. iv_ca_set_add_invariants (struct iv_ca *ivs, bitmap invs)
  4418. {
  4419. bitmap_iterator bi;
  4420. unsigned iid;
  4421. if (!invs)
  4422. return;
  4423. EXECUTE_IF_SET_IN_BITMAP (invs, 0, iid, bi)
  4424. {
  4425. ivs->n_invariant_uses[iid]++;
  4426. if (ivs->n_invariant_uses[iid] == 1)
  4427. ivs->n_regs++;
  4428. }
  4429. }
  4430. /* Set cost pair for USE in set IVS to CP. */
  4431. static void
  4432. iv_ca_set_cp (struct ivopts_data *data, struct iv_ca *ivs,
  4433. struct iv_use *use, struct cost_pair *cp)
  4434. {
  4435. unsigned uid = use->id, cid;
  4436. if (ivs->cand_for_use[uid] == cp)
  4437. return;
  4438. if (ivs->cand_for_use[uid])
  4439. iv_ca_set_no_cp (data, ivs, use);
  4440. if (cp)
  4441. {
  4442. cid = cp->cand->id;
  4443. ivs->bad_uses--;
  4444. ivs->cand_for_use[uid] = cp;
  4445. ivs->n_cand_uses[cid]++;
  4446. if (ivs->n_cand_uses[cid] == 1)
  4447. {
  4448. bitmap_set_bit (ivs->cands, cid);
  4449. /* Do not count the pseudocandidates. */
  4450. if (cp->cand->iv)
  4451. ivs->n_regs++;
  4452. ivs->n_cands++;
  4453. ivs->cand_cost += cp->cand->cost;
  4454. iv_ca_set_add_invariants (ivs, cp->cand->depends_on);
  4455. }
  4456. ivs->cand_use_cost = add_costs (ivs->cand_use_cost, cp->cost);
  4457. iv_ca_set_add_invariants (ivs, cp->depends_on);
  4458. if (cp->inv_expr_id != -1)
  4459. {
  4460. ivs->used_inv_expr[cp->inv_expr_id]++;
  4461. if (ivs->used_inv_expr[cp->inv_expr_id] == 1)
  4462. ivs->num_used_inv_expr++;
  4463. }
  4464. iv_ca_recount_cost (data, ivs);
  4465. }
  4466. }
  4467. /* Extend set IVS by expressing USE by some of the candidates in it
  4468. if possible. Consider all important candidates if candidates in
  4469. set IVS don't give any result. */
  4470. static void
  4471. iv_ca_add_use (struct ivopts_data *data, struct iv_ca *ivs,
  4472. struct iv_use *use)
  4473. {
  4474. struct cost_pair *best_cp = NULL, *cp;
  4475. bitmap_iterator bi;
  4476. unsigned i;
  4477. struct iv_cand *cand;
  4478. gcc_assert (ivs->upto >= use->id);
  4479. ivs->upto++;
  4480. ivs->bad_uses++;
  4481. EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
  4482. {
  4483. cand = iv_cand (data, i);
  4484. cp = get_use_iv_cost (data, use, cand);
  4485. if (cheaper_cost_pair (cp, best_cp))
  4486. best_cp = cp;
  4487. }
  4488. if (best_cp == NULL)
  4489. {
  4490. EXECUTE_IF_SET_IN_BITMAP (data->important_candidates, 0, i, bi)
  4491. {
  4492. cand = iv_cand (data, i);
  4493. cp = get_use_iv_cost (data, use, cand);
  4494. if (cheaper_cost_pair (cp, best_cp))
  4495. best_cp = cp;
  4496. }
  4497. }
  4498. iv_ca_set_cp (data, ivs, use, best_cp);
  4499. }
  4500. /* Get cost for assignment IVS. */
  4501. static comp_cost
  4502. iv_ca_cost (struct iv_ca *ivs)
  4503. {
  4504. /* This was a conditional expression but it triggered a bug in
  4505. Sun C 5.5. */
  4506. if (ivs->bad_uses)
  4507. return infinite_cost;
  4508. else
  4509. return ivs->cost;
  4510. }
  4511. /* Returns true if all dependences of CP are among invariants in IVS. */
  4512. static bool
  4513. iv_ca_has_deps (struct iv_ca *ivs, struct cost_pair *cp)
  4514. {
  4515. unsigned i;
  4516. bitmap_iterator bi;
  4517. if (!cp->depends_on)
  4518. return true;
  4519. EXECUTE_IF_SET_IN_BITMAP (cp->depends_on, 0, i, bi)
  4520. {
  4521. if (ivs->n_invariant_uses[i] == 0)
  4522. return false;
  4523. }
  4524. return true;
  4525. }
  4526. /* Creates change of expressing USE by NEW_CP instead of OLD_CP and chains
  4527. it before NEXT_CHANGE. */
  4528. static struct iv_ca_delta *
  4529. iv_ca_delta_add (struct iv_use *use, struct cost_pair *old_cp,
  4530. struct cost_pair *new_cp, struct iv_ca_delta *next_change)
  4531. {
  4532. struct iv_ca_delta *change = XNEW (struct iv_ca_delta);
  4533. change->use = use;
  4534. change->old_cp = old_cp;
  4535. change->new_cp = new_cp;
  4536. change->next_change = next_change;
  4537. return change;
  4538. }
  4539. /* Joins two lists of changes L1 and L2. Destructive -- old lists
  4540. are rewritten. */
  4541. static struct iv_ca_delta *
  4542. iv_ca_delta_join (struct iv_ca_delta *l1, struct iv_ca_delta *l2)
  4543. {
  4544. struct iv_ca_delta *last;
  4545. if (!l2)
  4546. return l1;
  4547. if (!l1)
  4548. return l2;
  4549. for (last = l1; last->next_change; last = last->next_change)
  4550. continue;
  4551. last->next_change = l2;
  4552. return l1;
  4553. }
  4554. /* Reverse the list of changes DELTA, forming the inverse to it. */
  4555. static struct iv_ca_delta *
  4556. iv_ca_delta_reverse (struct iv_ca_delta *delta)
  4557. {
  4558. struct iv_ca_delta *act, *next, *prev = NULL;
  4559. struct cost_pair *tmp;
  4560. for (act = delta; act; act = next)
  4561. {
  4562. next = act->next_change;
  4563. act->next_change = prev;
  4564. prev = act;
  4565. tmp = act->old_cp;
  4566. act->old_cp = act->new_cp;
  4567. act->new_cp = tmp;
  4568. }
  4569. return prev;
  4570. }
  4571. /* Commit changes in DELTA to IVS. If FORWARD is false, the changes are
  4572. reverted instead. */
  4573. static void
  4574. iv_ca_delta_commit (struct ivopts_data *data, struct iv_ca *ivs,
  4575. struct iv_ca_delta *delta, bool forward)
  4576. {
  4577. struct cost_pair *from, *to;
  4578. struct iv_ca_delta *act;
  4579. if (!forward)
  4580. delta = iv_ca_delta_reverse (delta);
  4581. for (act = delta; act; act = act->next_change)
  4582. {
  4583. from = act->old_cp;
  4584. to = act->new_cp;
  4585. gcc_assert (iv_ca_cand_for_use (ivs, act->use) == from);
  4586. iv_ca_set_cp (data, ivs, act->use, to);
  4587. }
  4588. if (!forward)
  4589. iv_ca_delta_reverse (delta);
  4590. }
  4591. /* Returns true if CAND is used in IVS. */
  4592. static bool
  4593. iv_ca_cand_used_p (struct iv_ca *ivs, struct iv_cand *cand)
  4594. {
  4595. return ivs->n_cand_uses[cand->id] > 0;
  4596. }
  4597. /* Returns number of induction variable candidates in the set IVS. */
  4598. static unsigned
  4599. iv_ca_n_cands (struct iv_ca *ivs)
  4600. {
  4601. return ivs->n_cands;
  4602. }
  4603. /* Free the list of changes DELTA. */
  4604. static void
  4605. iv_ca_delta_free (struct iv_ca_delta **delta)
  4606. {
  4607. struct iv_ca_delta *act, *next;
  4608. for (act = *delta; act; act = next)
  4609. {
  4610. next = act->next_change;
  4611. free (act);
  4612. }
  4613. *delta = NULL;
  4614. }
  4615. /* Allocates new iv candidates assignment. */
  4616. static struct iv_ca *
  4617. iv_ca_new (struct ivopts_data *data)
  4618. {
  4619. struct iv_ca *nw = XNEW (struct iv_ca);
  4620. nw->upto = 0;
  4621. nw->bad_uses = 0;
  4622. nw->cand_for_use = XCNEWVEC (struct cost_pair *, n_iv_uses (data));
  4623. nw->n_cand_uses = XCNEWVEC (unsigned, n_iv_cands (data));
  4624. nw->cands = BITMAP_ALLOC (NULL);
  4625. nw->n_cands = 0;
  4626. nw->n_regs = 0;
  4627. nw->cand_use_cost = no_cost;
  4628. nw->cand_cost = 0;
  4629. nw->n_invariant_uses = XCNEWVEC (unsigned, data->max_inv_id + 1);
  4630. nw->cost = no_cost;
  4631. nw->used_inv_expr = XCNEWVEC (unsigned, data->inv_expr_id + 1);
  4632. nw->num_used_inv_expr = 0;
  4633. return nw;
  4634. }
  4635. /* Free memory occupied by the set IVS. */
  4636. static void
  4637. iv_ca_free (struct iv_ca **ivs)
  4638. {
  4639. free ((*ivs)->cand_for_use);
  4640. free ((*ivs)->n_cand_uses);
  4641. BITMAP_FREE ((*ivs)->cands);
  4642. free ((*ivs)->n_invariant_uses);
  4643. free ((*ivs)->used_inv_expr);
  4644. free (*ivs);
  4645. *ivs = NULL;
  4646. }
  4647. /* Dumps IVS to FILE. */
  4648. static void
  4649. iv_ca_dump (struct ivopts_data *data, FILE *file, struct iv_ca *ivs)
  4650. {
  4651. const char *pref = " invariants ";
  4652. unsigned i;
  4653. comp_cost cost = iv_ca_cost (ivs);
  4654. fprintf (file, " cost: %d (complexity %d)\n", cost.cost, cost.complexity);
  4655. fprintf (file, " cand_cost: %d\n cand_use_cost: %d (complexity %d)\n",
  4656. ivs->cand_cost, ivs->cand_use_cost.cost, ivs->cand_use_cost.complexity);
  4657. bitmap_print (file, ivs->cands, " candidates: ","\n");
  4658. for (i = 0; i < ivs->upto; i++)
  4659. {
  4660. struct iv_use *use = iv_use (data, i);
  4661. struct cost_pair *cp = iv_ca_cand_for_use (ivs, use);
  4662. if (cp)
  4663. fprintf (file, " use:%d --> iv_cand:%d, cost=(%d,%d)\n",
  4664. use->id, cp->cand->id, cp->cost.cost, cp->cost.complexity);
  4665. else
  4666. fprintf (file, " use:%d --> ??\n", use->id);
  4667. }
  4668. for (i = 1; i <= data->max_inv_id; i++)
  4669. if (ivs->n_invariant_uses[i])
  4670. {
  4671. fprintf (file, "%s%d", pref, i);
  4672. pref = ", ";
  4673. }
  4674. fprintf (file, "\n\n");
  4675. }
  4676. /* Try changing candidate in IVS to CAND for each use. Return cost of the
  4677. new set, and store differences in DELTA. Number of induction variables
  4678. in the new set is stored to N_IVS. MIN_NCAND is a flag. When it is true
  4679. the function will try to find a solution with mimimal iv candidates. */
  4680. static comp_cost
  4681. iv_ca_extend (struct ivopts_data *data, struct iv_ca *ivs,
  4682. struct iv_cand *cand, struct iv_ca_delta **delta,
  4683. unsigned *n_ivs, bool min_ncand)
  4684. {
  4685. unsigned i;
  4686. comp_cost cost;
  4687. struct iv_use *use;
  4688. struct cost_pair *old_cp, *new_cp;
  4689. *delta = NULL;
  4690. for (i = 0; i < ivs->upto; i++)
  4691. {
  4692. use = iv_use (data, i);
  4693. old_cp = iv_ca_cand_for_use (ivs, use);
  4694. if (old_cp
  4695. && old_cp->cand == cand)
  4696. continue;
  4697. new_cp = get_use_iv_cost (data, use, cand);
  4698. if (!new_cp)
  4699. continue;
  4700. if (!min_ncand && !iv_ca_has_deps (ivs, new_cp))
  4701. continue;
  4702. if (!min_ncand && !cheaper_cost_pair (new_cp, old_cp))
  4703. continue;
  4704. *delta = iv_ca_delta_add (use, old_cp, new_cp, *delta);
  4705. }
  4706. iv_ca_delta_commit (data, ivs, *delta, true);
  4707. cost = iv_ca_cost (ivs);
  4708. if (n_ivs)
  4709. *n_ivs = iv_ca_n_cands (ivs);
  4710. iv_ca_delta_commit (data, ivs, *delta, false);
  4711. return cost;
  4712. }
  4713. /* Try narrowing set IVS by removing CAND. Return the cost of
  4714. the new set and store the differences in DELTA. START is
  4715. the candidate with which we start narrowing. */
  4716. static comp_cost
  4717. iv_ca_narrow (struct ivopts_data *data, struct iv_ca *ivs,
  4718. struct iv_cand *cand, struct iv_cand *start,
  4719. struct iv_ca_delta **delta)
  4720. {
  4721. unsigned i, ci;
  4722. struct iv_use *use;
  4723. struct cost_pair *old_cp, *new_cp, *cp;
  4724. bitmap_iterator bi;
  4725. struct iv_cand *cnd;
  4726. comp_cost cost, best_cost, acost;
  4727. *delta = NULL;
  4728. for (i = 0; i < n_iv_uses (data); i++)
  4729. {
  4730. use = iv_use (data, i);
  4731. old_cp = iv_ca_cand_for_use (ivs, use);
  4732. if (old_cp->cand != cand)
  4733. continue;
  4734. best_cost = iv_ca_cost (ivs);
  4735. /* Start narrowing with START. */
  4736. new_cp = get_use_iv_cost (data, use, start);
  4737. if (data->consider_all_candidates)
  4738. {
  4739. EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, ci, bi)
  4740. {
  4741. if (ci == cand->id || (start && ci == start->id))
  4742. continue;
  4743. cnd = iv_cand (data, ci);
  4744. cp = get_use_iv_cost (data, use, cnd);
  4745. if (!cp)
  4746. continue;
  4747. iv_ca_set_cp (data, ivs, use, cp);
  4748. acost = iv_ca_cost (ivs);
  4749. if (compare_costs (acost, best_cost) < 0)
  4750. {
  4751. best_cost = acost;
  4752. new_cp = cp;
  4753. }
  4754. }
  4755. }
  4756. else
  4757. {
  4758. EXECUTE_IF_AND_IN_BITMAP (use->related_cands, ivs->cands, 0, ci, bi)
  4759. {
  4760. if (ci == cand->id || (start && ci == start->id))
  4761. continue;
  4762. cnd = iv_cand (data, ci);
  4763. cp = get_use_iv_cost (data, use, cnd);
  4764. if (!cp)
  4765. continue;
  4766. iv_ca_set_cp (data, ivs, use, cp);
  4767. acost = iv_ca_cost (ivs);
  4768. if (compare_costs (acost, best_cost) < 0)
  4769. {
  4770. best_cost = acost;
  4771. new_cp = cp;
  4772. }
  4773. }
  4774. }
  4775. /* Restore to old cp for use. */
  4776. iv_ca_set_cp (data, ivs, use, old_cp);
  4777. if (!new_cp)
  4778. {
  4779. iv_ca_delta_free (delta);
  4780. return infinite_cost;
  4781. }
  4782. *delta = iv_ca_delta_add (use, old_cp, new_cp, *delta);
  4783. }
  4784. iv_ca_delta_commit (data, ivs, *delta, true);
  4785. cost = iv_ca_cost (ivs);
  4786. iv_ca_delta_commit (data, ivs, *delta, false);
  4787. return cost;
  4788. }
  4789. /* Try optimizing the set of candidates IVS by removing candidates different
  4790. from to EXCEPT_CAND from it. Return cost of the new set, and store
  4791. differences in DELTA. */
  4792. static comp_cost
  4793. iv_ca_prune (struct ivopts_data *data, struct iv_ca *ivs,
  4794. struct iv_cand *except_cand, struct iv_ca_delta **delta)
  4795. {
  4796. bitmap_iterator bi;
  4797. struct iv_ca_delta *act_delta, *best_delta;
  4798. unsigned i;
  4799. comp_cost best_cost, acost;
  4800. struct iv_cand *cand;
  4801. best_delta = NULL;
  4802. best_cost = iv_ca_cost (ivs);
  4803. EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
  4804. {
  4805. cand = iv_cand (data, i);
  4806. if (cand == except_cand)
  4807. continue;
  4808. acost = iv_ca_narrow (data, ivs, cand, except_cand, &act_delta);
  4809. if (compare_costs (acost, best_cost) < 0)
  4810. {
  4811. best_cost = acost;
  4812. iv_ca_delta_free (&best_delta);
  4813. best_delta = act_delta;
  4814. }
  4815. else
  4816. iv_ca_delta_free (&act_delta);
  4817. }
  4818. if (!best_delta)
  4819. {
  4820. *delta = NULL;
  4821. return best_cost;
  4822. }
  4823. /* Recurse to possibly remove other unnecessary ivs. */
  4824. iv_ca_delta_commit (data, ivs, best_delta, true);
  4825. best_cost = iv_ca_prune (data, ivs, except_cand, delta);
  4826. iv_ca_delta_commit (data, ivs, best_delta, false);
  4827. *delta = iv_ca_delta_join (best_delta, *delta);
  4828. return best_cost;
  4829. }
  4830. /* Check if CAND_IDX is a candidate other than OLD_CAND and has
  4831. cheaper local cost for USE than BEST_CP. Return pointer to
  4832. the corresponding cost_pair, otherwise just return BEST_CP. */
  4833. static struct cost_pair*
  4834. cheaper_cost_with_cand (struct ivopts_data *data, struct iv_use *use,
  4835. unsigned int cand_idx, struct iv_cand *old_cand,
  4836. struct cost_pair *best_cp)
  4837. {
  4838. struct iv_cand *cand;
  4839. struct cost_pair *cp;
  4840. gcc_assert (old_cand != NULL && best_cp != NULL);
  4841. if (cand_idx == old_cand->id)
  4842. return best_cp;
  4843. cand = iv_cand (data, cand_idx);
  4844. cp = get_use_iv_cost (data, use, cand);
  4845. if (cp != NULL && cheaper_cost_pair (cp, best_cp))
  4846. return cp;
  4847. return best_cp;
  4848. }
  4849. /* Try breaking local optimal fixed-point for IVS by replacing candidates
  4850. which are used by more than one iv uses. For each of those candidates,
  4851. this function tries to represent iv uses under that candidate using
  4852. other ones with lower local cost, then tries to prune the new set.
  4853. If the new set has lower cost, It returns the new cost after recording
  4854. candidate replacement in list DELTA. */
  4855. static comp_cost
  4856. iv_ca_replace (struct ivopts_data *data, struct iv_ca *ivs,
  4857. struct iv_ca_delta **delta)
  4858. {
  4859. bitmap_iterator bi, bj;
  4860. unsigned int i, j, k;
  4861. struct iv_use *use;
  4862. struct iv_cand *cand;
  4863. comp_cost orig_cost, acost;
  4864. struct iv_ca_delta *act_delta, *tmp_delta;
  4865. struct cost_pair *old_cp, *best_cp = NULL;
  4866. *delta = NULL;
  4867. orig_cost = iv_ca_cost (ivs);
  4868. EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
  4869. {
  4870. if (ivs->n_cand_uses[i] == 1
  4871. || ivs->n_cand_uses[i] > ALWAYS_PRUNE_CAND_SET_BOUND)
  4872. continue;
  4873. cand = iv_cand (data, i);
  4874. act_delta = NULL;
  4875. /* Represent uses under current candidate using other ones with
  4876. lower local cost. */
  4877. for (j = 0; j < ivs->upto; j++)
  4878. {
  4879. use = iv_use (data, j);
  4880. old_cp = iv_ca_cand_for_use (ivs, use);
  4881. if (old_cp->cand != cand)
  4882. continue;
  4883. best_cp = old_cp;
  4884. if (data->consider_all_candidates)
  4885. for (k = 0; k < n_iv_cands (data); k++)
  4886. best_cp = cheaper_cost_with_cand (data, use, k,
  4887. old_cp->cand, best_cp);
  4888. else
  4889. EXECUTE_IF_SET_IN_BITMAP (use->related_cands, 0, k, bj)
  4890. best_cp = cheaper_cost_with_cand (data, use, k,
  4891. old_cp->cand, best_cp);
  4892. if (best_cp == old_cp)
  4893. continue;
  4894. act_delta = iv_ca_delta_add (use, old_cp, best_cp, act_delta);
  4895. }
  4896. /* No need for further prune. */
  4897. if (!act_delta)
  4898. continue;
  4899. /* Prune the new candidate set. */
  4900. iv_ca_delta_commit (data, ivs, act_delta, true);
  4901. acost = iv_ca_prune (data, ivs, NULL, &tmp_delta);
  4902. iv_ca_delta_commit (data, ivs, act_delta, false);
  4903. act_delta = iv_ca_delta_join (act_delta, tmp_delta);
  4904. if (compare_costs (acost, orig_cost) < 0)
  4905. {
  4906. *delta = act_delta;
  4907. return acost;
  4908. }
  4909. else
  4910. iv_ca_delta_free (&act_delta);
  4911. }
  4912. return orig_cost;
  4913. }
  4914. /* Tries to extend the sets IVS in the best possible way in order
  4915. to express the USE. If ORIGINALP is true, prefer candidates from
  4916. the original set of IVs, otherwise favor important candidates not
  4917. based on any memory object. */
  4918. static bool
  4919. try_add_cand_for (struct ivopts_data *data, struct iv_ca *ivs,
  4920. struct iv_use *use, bool originalp)
  4921. {
  4922. comp_cost best_cost, act_cost;
  4923. unsigned i;
  4924. bitmap_iterator bi;
  4925. struct iv_cand *cand;
  4926. struct iv_ca_delta *best_delta = NULL, *act_delta;
  4927. struct cost_pair *cp;
  4928. iv_ca_add_use (data, ivs, use);
  4929. best_cost = iv_ca_cost (ivs);
  4930. cp = iv_ca_cand_for_use (ivs, use);
  4931. if (cp)
  4932. {
  4933. best_delta = iv_ca_delta_add (use, NULL, cp, NULL);
  4934. iv_ca_set_no_cp (data, ivs, use);
  4935. }
  4936. /* If ORIGINALP is true, try to find the original IV for the use. Otherwise
  4937. first try important candidates not based on any memory object. Only if
  4938. this fails, try the specific ones. Rationale -- in loops with many
  4939. variables the best choice often is to use just one generic biv. If we
  4940. added here many ivs specific to the uses, the optimization algorithm later
  4941. would be likely to get stuck in a local minimum, thus causing us to create
  4942. too many ivs. The approach from few ivs to more seems more likely to be
  4943. successful -- starting from few ivs, replacing an expensive use by a
  4944. specific iv should always be a win. */
  4945. EXECUTE_IF_SET_IN_BITMAP (data->important_candidates, 0, i, bi)
  4946. {
  4947. cand = iv_cand (data, i);
  4948. if (originalp && cand->pos !=IP_ORIGINAL)
  4949. continue;
  4950. if (!originalp && cand->iv->base_object != NULL_TREE)
  4951. continue;
  4952. if (iv_ca_cand_used_p (ivs, cand))
  4953. continue;
  4954. cp = get_use_iv_cost (data, use, cand);
  4955. if (!cp)
  4956. continue;
  4957. iv_ca_set_cp (data, ivs, use, cp);
  4958. act_cost = iv_ca_extend (data, ivs, cand, &act_delta, NULL,
  4959. true);
  4960. iv_ca_set_no_cp (data, ivs, use);
  4961. act_delta = iv_ca_delta_add (use, NULL, cp, act_delta);
  4962. if (compare_costs (act_cost, best_cost) < 0)
  4963. {
  4964. best_cost = act_cost;
  4965. iv_ca_delta_free (&best_delta);
  4966. best_delta = act_delta;
  4967. }
  4968. else
  4969. iv_ca_delta_free (&act_delta);
  4970. }
  4971. if (infinite_cost_p (best_cost))
  4972. {
  4973. for (i = 0; i < use->n_map_members; i++)
  4974. {
  4975. cp = use->cost_map + i;
  4976. cand = cp->cand;
  4977. if (!cand)
  4978. continue;
  4979. /* Already tried this. */
  4980. if (cand->important)
  4981. {
  4982. if (originalp && cand->pos == IP_ORIGINAL)
  4983. continue;
  4984. if (!originalp && cand->iv->base_object == NULL_TREE)
  4985. continue;
  4986. }
  4987. if (iv_ca_cand_used_p (ivs, cand))
  4988. continue;
  4989. act_delta = NULL;
  4990. iv_ca_set_cp (data, ivs, use, cp);
  4991. act_cost = iv_ca_extend (data, ivs, cand, &act_delta, NULL, true);
  4992. iv_ca_set_no_cp (data, ivs, use);
  4993. act_delta = iv_ca_delta_add (use, iv_ca_cand_for_use (ivs, use),
  4994. cp, act_delta);
  4995. if (compare_costs (act_cost, best_cost) < 0)
  4996. {
  4997. best_cost = act_cost;
  4998. if (best_delta)
  4999. iv_ca_delta_free (&best_delta);
  5000. best_delta = act_delta;
  5001. }
  5002. else
  5003. iv_ca_delta_free (&act_delta);
  5004. }
  5005. }
  5006. iv_ca_delta_commit (data, ivs, best_delta, true);
  5007. iv_ca_delta_free (&best_delta);
  5008. return !infinite_cost_p (best_cost);
  5009. }
  5010. /* Finds an initial assignment of candidates to uses. */
  5011. static struct iv_ca *
  5012. get_initial_solution (struct ivopts_data *data, bool originalp)
  5013. {
  5014. struct iv_ca *ivs = iv_ca_new (data);
  5015. unsigned i;
  5016. for (i = 0; i < n_iv_uses (data); i++)
  5017. if (!try_add_cand_for (data, ivs, iv_use (data, i), originalp))
  5018. {
  5019. iv_ca_free (&ivs);
  5020. return NULL;
  5021. }
  5022. return ivs;
  5023. }
  5024. /* Tries to improve set of induction variables IVS. TRY_REPLACE_P
  5025. points to a bool variable, this function tries to break local
  5026. optimal fixed-point by replacing candidates in IVS if it's true. */
  5027. static bool
  5028. try_improve_iv_set (struct ivopts_data *data,
  5029. struct iv_ca *ivs, bool *try_replace_p)
  5030. {
  5031. unsigned i, n_ivs;
  5032. comp_cost acost, best_cost = iv_ca_cost (ivs);
  5033. struct iv_ca_delta *best_delta = NULL, *act_delta, *tmp_delta;
  5034. struct iv_cand *cand;
  5035. /* Try extending the set of induction variables by one. */
  5036. for (i = 0; i < n_iv_cands (data); i++)
  5037. {
  5038. cand = iv_cand (data, i);
  5039. if (iv_ca_cand_used_p (ivs, cand))
  5040. continue;
  5041. acost = iv_ca_extend (data, ivs, cand, &act_delta, &n_ivs, false);
  5042. if (!act_delta)
  5043. continue;
  5044. /* If we successfully added the candidate and the set is small enough,
  5045. try optimizing it by removing other candidates. */
  5046. if (n_ivs <= ALWAYS_PRUNE_CAND_SET_BOUND)
  5047. {
  5048. iv_ca_delta_commit (data, ivs, act_delta, true);
  5049. acost = iv_ca_prune (data, ivs, cand, &tmp_delta);
  5050. iv_ca_delta_commit (data, ivs, act_delta, false);
  5051. act_delta = iv_ca_delta_join (act_delta, tmp_delta);
  5052. }
  5053. if (compare_costs (acost, best_cost) < 0)
  5054. {
  5055. best_cost = acost;
  5056. iv_ca_delta_free (&best_delta);
  5057. best_delta = act_delta;
  5058. }
  5059. else
  5060. iv_ca_delta_free (&act_delta);
  5061. }
  5062. if (!best_delta)
  5063. {
  5064. /* Try removing the candidates from the set instead. */
  5065. best_cost = iv_ca_prune (data, ivs, NULL, &best_delta);
  5066. if (!best_delta && *try_replace_p)
  5067. {
  5068. *try_replace_p = false;
  5069. /* So far candidate selecting algorithm tends to choose fewer IVs
  5070. so that it can handle cases in which loops have many variables
  5071. but the best choice is often to use only one general biv. One
  5072. weakness is it can't handle opposite cases, in which different
  5073. candidates should be chosen with respect to each use. To solve
  5074. the problem, we replace candidates in a manner described by the
  5075. comments of iv_ca_replace, thus give general algorithm a chance
  5076. to break local optimal fixed-point in these cases. */
  5077. best_cost = iv_ca_replace (data, ivs, &best_delta);
  5078. }
  5079. if (!best_delta)
  5080. return false;
  5081. }
  5082. iv_ca_delta_commit (data, ivs, best_delta, true);
  5083. gcc_assert (compare_costs (best_cost, iv_ca_cost (ivs)) == 0);
  5084. iv_ca_delta_free (&best_delta);
  5085. return true;
  5086. }
  5087. /* Attempts to find the optimal set of induction variables. We do simple
  5088. greedy heuristic -- we try to replace at most one candidate in the selected
  5089. solution and remove the unused ivs while this improves the cost. */
  5090. static struct iv_ca *
  5091. find_optimal_iv_set_1 (struct ivopts_data *data, bool originalp)
  5092. {
  5093. struct iv_ca *set;
  5094. bool try_replace_p = true;
  5095. /* Get the initial solution. */
  5096. set = get_initial_solution (data, originalp);
  5097. if (!set)
  5098. {
  5099. if (dump_file && (dump_flags & TDF_DETAILS))
  5100. fprintf (dump_file, "Unable to substitute for ivs, failed.\n");
  5101. return NULL;
  5102. }
  5103. if (dump_file && (dump_flags & TDF_DETAILS))
  5104. {
  5105. fprintf (dump_file, "Initial set of candidates:\n");
  5106. iv_ca_dump (data, dump_file, set);
  5107. }
  5108. while (try_improve_iv_set (data, set, &try_replace_p))
  5109. {
  5110. if (dump_file && (dump_flags & TDF_DETAILS))
  5111. {
  5112. fprintf (dump_file, "Improved to:\n");
  5113. iv_ca_dump (data, dump_file, set);
  5114. }
  5115. }
  5116. return set;
  5117. }
  5118. static struct iv_ca *
  5119. find_optimal_iv_set (struct ivopts_data *data)
  5120. {
  5121. unsigned i;
  5122. struct iv_ca *set, *origset;
  5123. struct iv_use *use;
  5124. comp_cost cost, origcost;
  5125. /* Determine the cost based on a strategy that starts with original IVs,
  5126. and try again using a strategy that prefers candidates not based
  5127. on any IVs. */
  5128. origset = find_optimal_iv_set_1 (data, true);
  5129. set = find_optimal_iv_set_1 (data, false);
  5130. if (!origset && !set)
  5131. return NULL;
  5132. origcost = origset ? iv_ca_cost (origset) : infinite_cost;
  5133. cost = set ? iv_ca_cost (set) : infinite_cost;
  5134. if (dump_file && (dump_flags & TDF_DETAILS))
  5135. {
  5136. fprintf (dump_file, "Original cost %d (complexity %d)\n\n",
  5137. origcost.cost, origcost.complexity);
  5138. fprintf (dump_file, "Final cost %d (complexity %d)\n\n",
  5139. cost.cost, cost.complexity);
  5140. }
  5141. /* Choose the one with the best cost. */
  5142. if (compare_costs (origcost, cost) <= 0)
  5143. {
  5144. if (set)
  5145. iv_ca_free (&set);
  5146. set = origset;
  5147. }
  5148. else if (origset)
  5149. iv_ca_free (&origset);
  5150. for (i = 0; i < n_iv_uses (data); i++)
  5151. {
  5152. use = iv_use (data, i);
  5153. use->selected = iv_ca_cand_for_use (set, use)->cand;
  5154. }
  5155. return set;
  5156. }
  5157. /* Creates a new induction variable corresponding to CAND. */
  5158. static void
  5159. create_new_iv (struct ivopts_data *data, struct iv_cand *cand)
  5160. {
  5161. gimple_stmt_iterator incr_pos;
  5162. tree base;
  5163. bool after = false;
  5164. if (!cand->iv)
  5165. return;
  5166. switch (cand->pos)
  5167. {
  5168. case IP_NORMAL:
  5169. incr_pos = gsi_last_bb (ip_normal_pos (data->current_loop));
  5170. break;
  5171. case IP_END:
  5172. incr_pos = gsi_last_bb (ip_end_pos (data->current_loop));
  5173. after = true;
  5174. break;
  5175. case IP_AFTER_USE:
  5176. after = true;
  5177. /* fall through */
  5178. case IP_BEFORE_USE:
  5179. incr_pos = gsi_for_stmt (cand->incremented_at);
  5180. break;
  5181. case IP_ORIGINAL:
  5182. /* Mark that the iv is preserved. */
  5183. name_info (data, cand->var_before)->preserve_biv = true;
  5184. name_info (data, cand->var_after)->preserve_biv = true;
  5185. /* Rewrite the increment so that it uses var_before directly. */
  5186. find_interesting_uses_op (data, cand->var_after)->selected = cand;
  5187. return;
  5188. }
  5189. gimple_add_tmp_var (cand->var_before);
  5190. base = unshare_expr (cand->iv->base);
  5191. create_iv (base, unshare_expr (cand->iv->step),
  5192. cand->var_before, data->current_loop,
  5193. &incr_pos, after, &cand->var_before, &cand->var_after);
  5194. }
  5195. /* Creates new induction variables described in SET. */
  5196. static void
  5197. create_new_ivs (struct ivopts_data *data, struct iv_ca *set)
  5198. {
  5199. unsigned i;
  5200. struct iv_cand *cand;
  5201. bitmap_iterator bi;
  5202. EXECUTE_IF_SET_IN_BITMAP (set->cands, 0, i, bi)
  5203. {
  5204. cand = iv_cand (data, i);
  5205. create_new_iv (data, cand);
  5206. }
  5207. if (dump_file && (dump_flags & TDF_DETAILS))
  5208. {
  5209. fprintf (dump_file, "Selected IV set for loop %d",
  5210. data->current_loop->num);
  5211. if (data->loop_loc != UNKNOWN_LOCATION)
  5212. fprintf (dump_file, " at %s:%d", LOCATION_FILE (data->loop_loc),
  5213. LOCATION_LINE (data->loop_loc));
  5214. fprintf (dump_file, ", %lu IVs:\n", bitmap_count_bits (set->cands));
  5215. EXECUTE_IF_SET_IN_BITMAP (set->cands, 0, i, bi)
  5216. {
  5217. cand = iv_cand (data, i);
  5218. dump_cand (dump_file, cand);
  5219. }
  5220. fprintf (dump_file, "\n");
  5221. }
  5222. }
  5223. /* Rewrites USE (definition of iv used in a nonlinear expression)
  5224. using candidate CAND. */
  5225. static void
  5226. rewrite_use_nonlinear_expr (struct ivopts_data *data,
  5227. struct iv_use *use, struct iv_cand *cand)
  5228. {
  5229. tree comp;
  5230. tree op, tgt;
  5231. gassign *ass;
  5232. gimple_stmt_iterator bsi;
  5233. /* An important special case -- if we are asked to express value of
  5234. the original iv by itself, just exit; there is no need to
  5235. introduce a new computation (that might also need casting the
  5236. variable to unsigned and back). */
  5237. if (cand->pos == IP_ORIGINAL
  5238. && cand->incremented_at == use->stmt)
  5239. {
  5240. enum tree_code stmt_code;
  5241. gcc_assert (is_gimple_assign (use->stmt));
  5242. gcc_assert (gimple_assign_lhs (use->stmt) == cand->var_after);
  5243. /* Check whether we may leave the computation unchanged.
  5244. This is the case only if it does not rely on other
  5245. computations in the loop -- otherwise, the computation
  5246. we rely upon may be removed in remove_unused_ivs,
  5247. thus leading to ICE. */
  5248. stmt_code = gimple_assign_rhs_code (use->stmt);
  5249. if (stmt_code == PLUS_EXPR
  5250. || stmt_code == MINUS_EXPR
  5251. || stmt_code == POINTER_PLUS_EXPR)
  5252. {
  5253. if (gimple_assign_rhs1 (use->stmt) == cand->var_before)
  5254. op = gimple_assign_rhs2 (use->stmt);
  5255. else if (gimple_assign_rhs2 (use->stmt) == cand->var_before)
  5256. op = gimple_assign_rhs1 (use->stmt);
  5257. else
  5258. op = NULL_TREE;
  5259. }
  5260. else
  5261. op = NULL_TREE;
  5262. if (op && expr_invariant_in_loop_p (data->current_loop, op))
  5263. return;
  5264. }
  5265. comp = get_computation (data->current_loop, use, cand);
  5266. gcc_assert (comp != NULL_TREE);
  5267. switch (gimple_code (use->stmt))
  5268. {
  5269. case GIMPLE_PHI:
  5270. tgt = PHI_RESULT (use->stmt);
  5271. /* If we should keep the biv, do not replace it. */
  5272. if (name_info (data, tgt)->preserve_biv)
  5273. return;
  5274. bsi = gsi_after_labels (gimple_bb (use->stmt));
  5275. break;
  5276. case GIMPLE_ASSIGN:
  5277. tgt = gimple_assign_lhs (use->stmt);
  5278. bsi = gsi_for_stmt (use->stmt);
  5279. break;
  5280. default:
  5281. gcc_unreachable ();
  5282. }
  5283. if (!valid_gimple_rhs_p (comp)
  5284. || (gimple_code (use->stmt) != GIMPLE_PHI
  5285. /* We can't allow re-allocating the stmt as it might be pointed
  5286. to still. */
  5287. && (get_gimple_rhs_num_ops (TREE_CODE (comp))
  5288. >= gimple_num_ops (gsi_stmt (bsi)))))
  5289. {
  5290. comp = force_gimple_operand_gsi (&bsi, comp, true, NULL_TREE,
  5291. true, GSI_SAME_STMT);
  5292. if (POINTER_TYPE_P (TREE_TYPE (tgt)))
  5293. {
  5294. duplicate_ssa_name_ptr_info (comp, SSA_NAME_PTR_INFO (tgt));
  5295. /* As this isn't a plain copy we have to reset alignment
  5296. information. */
  5297. if (SSA_NAME_PTR_INFO (comp))
  5298. mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (comp));
  5299. }
  5300. }
  5301. if (gimple_code (use->stmt) == GIMPLE_PHI)
  5302. {
  5303. ass = gimple_build_assign (tgt, comp);
  5304. gsi_insert_before (&bsi, ass, GSI_SAME_STMT);
  5305. bsi = gsi_for_stmt (use->stmt);
  5306. remove_phi_node (&bsi, false);
  5307. }
  5308. else
  5309. {
  5310. gimple_assign_set_rhs_from_tree (&bsi, comp);
  5311. use->stmt = gsi_stmt (bsi);
  5312. }
  5313. }
  5314. /* Performs a peephole optimization to reorder the iv update statement with
  5315. a mem ref to enable instruction combining in later phases. The mem ref uses
  5316. the iv value before the update, so the reordering transformation requires
  5317. adjustment of the offset. CAND is the selected IV_CAND.
  5318. Example:
  5319. t = MEM_REF (base, iv1, 8, 16); // base, index, stride, offset
  5320. iv2 = iv1 + 1;
  5321. if (t < val) (1)
  5322. goto L;
  5323. goto Head;
  5324. directly propagating t over to (1) will introduce overlapping live range
  5325. thus increase register pressure. This peephole transform it into:
  5326. iv2 = iv1 + 1;
  5327. t = MEM_REF (base, iv2, 8, 8);
  5328. if (t < val)
  5329. goto L;
  5330. goto Head;
  5331. */
  5332. static void
  5333. adjust_iv_update_pos (struct iv_cand *cand, struct iv_use *use)
  5334. {
  5335. tree var_after;
  5336. gimple iv_update, stmt;
  5337. basic_block bb;
  5338. gimple_stmt_iterator gsi, gsi_iv;
  5339. if (cand->pos != IP_NORMAL)
  5340. return;
  5341. var_after = cand->var_after;
  5342. iv_update = SSA_NAME_DEF_STMT (var_after);
  5343. bb = gimple_bb (iv_update);
  5344. gsi = gsi_last_nondebug_bb (bb);
  5345. stmt = gsi_stmt (gsi);
  5346. /* Only handle conditional statement for now. */
  5347. if (gimple_code (stmt) != GIMPLE_COND)
  5348. return;
  5349. gsi_prev_nondebug (&gsi);
  5350. stmt = gsi_stmt (gsi);
  5351. if (stmt != iv_update)
  5352. return;
  5353. gsi_prev_nondebug (&gsi);
  5354. if (gsi_end_p (gsi))
  5355. return;
  5356. stmt = gsi_stmt (gsi);
  5357. if (gimple_code (stmt) != GIMPLE_ASSIGN)
  5358. return;
  5359. if (stmt != use->stmt)
  5360. return;
  5361. if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
  5362. return;
  5363. if (dump_file && (dump_flags & TDF_DETAILS))
  5364. {
  5365. fprintf (dump_file, "Reordering \n");
  5366. print_gimple_stmt (dump_file, iv_update, 0, 0);
  5367. print_gimple_stmt (dump_file, use->stmt, 0, 0);
  5368. fprintf (dump_file, "\n");
  5369. }
  5370. gsi = gsi_for_stmt (use->stmt);
  5371. gsi_iv = gsi_for_stmt (iv_update);
  5372. gsi_move_before (&gsi_iv, &gsi);
  5373. cand->pos = IP_BEFORE_USE;
  5374. cand->incremented_at = use->stmt;
  5375. }
  5376. /* Rewrites USE (address that is an iv) using candidate CAND. */
  5377. static void
  5378. rewrite_use_address (struct ivopts_data *data,
  5379. struct iv_use *use, struct iv_cand *cand)
  5380. {
  5381. aff_tree aff;
  5382. gimple_stmt_iterator bsi = gsi_for_stmt (use->stmt);
  5383. tree base_hint = NULL_TREE;
  5384. tree ref, iv;
  5385. bool ok;
  5386. adjust_iv_update_pos (cand, use);
  5387. ok = get_computation_aff (data->current_loop, use, cand, use->stmt, &aff);
  5388. gcc_assert (ok);
  5389. unshare_aff_combination (&aff);
  5390. /* To avoid undefined overflow problems, all IV candidates use unsigned
  5391. integer types. The drawback is that this makes it impossible for
  5392. create_mem_ref to distinguish an IV that is based on a memory object
  5393. from one that represents simply an offset.
  5394. To work around this problem, we pass a hint to create_mem_ref that
  5395. indicates which variable (if any) in aff is an IV based on a memory
  5396. object. Note that we only consider the candidate. If this is not
  5397. based on an object, the base of the reference is in some subexpression
  5398. of the use -- but these will use pointer types, so they are recognized
  5399. by the create_mem_ref heuristics anyway. */
  5400. if (cand->iv->base_object)
  5401. base_hint = var_at_stmt (data->current_loop, cand, use->stmt);
  5402. iv = var_at_stmt (data->current_loop, cand, use->stmt);
  5403. ref = create_mem_ref (&bsi, TREE_TYPE (*use->op_p), &aff,
  5404. reference_alias_ptr_type (*use->op_p),
  5405. iv, base_hint, data->speed);
  5406. copy_ref_info (ref, *use->op_p);
  5407. *use->op_p = ref;
  5408. }
  5409. /* Rewrites USE (the condition such that one of the arguments is an iv) using
  5410. candidate CAND. */
  5411. static void
  5412. rewrite_use_compare (struct ivopts_data *data,
  5413. struct iv_use *use, struct iv_cand *cand)
  5414. {
  5415. tree comp, *var_p, op, bound;
  5416. gimple_stmt_iterator bsi = gsi_for_stmt (use->stmt);
  5417. enum tree_code compare;
  5418. struct cost_pair *cp = get_use_iv_cost (data, use, cand);
  5419. bool ok;
  5420. bound = cp->value;
  5421. if (bound)
  5422. {
  5423. tree var = var_at_stmt (data->current_loop, cand, use->stmt);
  5424. tree var_type = TREE_TYPE (var);
  5425. gimple_seq stmts;
  5426. if (dump_file && (dump_flags & TDF_DETAILS))
  5427. {
  5428. fprintf (dump_file, "Replacing exit test: ");
  5429. print_gimple_stmt (dump_file, use->stmt, 0, TDF_SLIM);
  5430. }
  5431. compare = cp->comp;
  5432. bound = unshare_expr (fold_convert (var_type, bound));
  5433. op = force_gimple_operand (bound, &stmts, true, NULL_TREE);
  5434. if (stmts)
  5435. gsi_insert_seq_on_edge_immediate (
  5436. loop_preheader_edge (data->current_loop),
  5437. stmts);
  5438. gcond *cond_stmt = as_a <gcond *> (use->stmt);
  5439. gimple_cond_set_lhs (cond_stmt, var);
  5440. gimple_cond_set_code (cond_stmt, compare);
  5441. gimple_cond_set_rhs (cond_stmt, op);
  5442. return;
  5443. }
  5444. /* The induction variable elimination failed; just express the original
  5445. giv. */
  5446. comp = get_computation (data->current_loop, use, cand);
  5447. gcc_assert (comp != NULL_TREE);
  5448. ok = extract_cond_operands (data, use->stmt, &var_p, NULL, NULL, NULL);
  5449. gcc_assert (ok);
  5450. *var_p = force_gimple_operand_gsi (&bsi, comp, true, SSA_NAME_VAR (*var_p),
  5451. true, GSI_SAME_STMT);
  5452. }
  5453. /* Rewrites USE using candidate CAND. */
  5454. static void
  5455. rewrite_use (struct ivopts_data *data, struct iv_use *use, struct iv_cand *cand)
  5456. {
  5457. switch (use->type)
  5458. {
  5459. case USE_NONLINEAR_EXPR:
  5460. rewrite_use_nonlinear_expr (data, use, cand);
  5461. break;
  5462. case USE_ADDRESS:
  5463. rewrite_use_address (data, use, cand);
  5464. break;
  5465. case USE_COMPARE:
  5466. rewrite_use_compare (data, use, cand);
  5467. break;
  5468. default:
  5469. gcc_unreachable ();
  5470. }
  5471. update_stmt (use->stmt);
  5472. }
  5473. /* Rewrite the uses using the selected induction variables. */
  5474. static void
  5475. rewrite_uses (struct ivopts_data *data)
  5476. {
  5477. unsigned i;
  5478. struct iv_cand *cand;
  5479. struct iv_use *use;
  5480. for (i = 0; i < n_iv_uses (data); i++)
  5481. {
  5482. use = iv_use (data, i);
  5483. cand = use->selected;
  5484. gcc_assert (cand);
  5485. rewrite_use (data, use, cand);
  5486. }
  5487. }
  5488. /* Removes the ivs that are not used after rewriting. */
  5489. static void
  5490. remove_unused_ivs (struct ivopts_data *data)
  5491. {
  5492. unsigned j;
  5493. bitmap_iterator bi;
  5494. bitmap toremove = BITMAP_ALLOC (NULL);
  5495. /* Figure out an order in which to release SSA DEFs so that we don't
  5496. release something that we'd have to propagate into a debug stmt
  5497. afterwards. */
  5498. EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
  5499. {
  5500. struct version_info *info;
  5501. info = ver_info (data, j);
  5502. if (info->iv
  5503. && !integer_zerop (info->iv->step)
  5504. && !info->inv_id
  5505. && !info->iv->have_use_for
  5506. && !info->preserve_biv)
  5507. {
  5508. bitmap_set_bit (toremove, SSA_NAME_VERSION (info->iv->ssa_name));
  5509. tree def = info->iv->ssa_name;
  5510. if (MAY_HAVE_DEBUG_STMTS && SSA_NAME_DEF_STMT (def))
  5511. {
  5512. imm_use_iterator imm_iter;
  5513. use_operand_p use_p;
  5514. gimple stmt;
  5515. int count = 0;
  5516. FOR_EACH_IMM_USE_STMT (stmt, imm_iter, def)
  5517. {
  5518. if (!gimple_debug_bind_p (stmt))
  5519. continue;
  5520. /* We just want to determine whether to do nothing
  5521. (count == 0), to substitute the computed
  5522. expression into a single use of the SSA DEF by
  5523. itself (count == 1), or to use a debug temp
  5524. because the SSA DEF is used multiple times or as
  5525. part of a larger expression (count > 1). */
  5526. count++;
  5527. if (gimple_debug_bind_get_value (stmt) != def)
  5528. count++;
  5529. if (count > 1)
  5530. BREAK_FROM_IMM_USE_STMT (imm_iter);
  5531. }
  5532. if (!count)
  5533. continue;
  5534. struct iv_use dummy_use;
  5535. struct iv_cand *best_cand = NULL, *cand;
  5536. unsigned i, best_pref = 0, cand_pref;
  5537. memset (&dummy_use, 0, sizeof (dummy_use));
  5538. dummy_use.iv = info->iv;
  5539. for (i = 0; i < n_iv_uses (data) && i < 64; i++)
  5540. {
  5541. cand = iv_use (data, i)->selected;
  5542. if (cand == best_cand)
  5543. continue;
  5544. cand_pref = operand_equal_p (cand->iv->step,
  5545. info->iv->step, 0)
  5546. ? 4 : 0;
  5547. cand_pref
  5548. += TYPE_MODE (TREE_TYPE (cand->iv->base))
  5549. == TYPE_MODE (TREE_TYPE (info->iv->base))
  5550. ? 2 : 0;
  5551. cand_pref
  5552. += TREE_CODE (cand->iv->base) == INTEGER_CST
  5553. ? 1 : 0;
  5554. if (best_cand == NULL || best_pref < cand_pref)
  5555. {
  5556. best_cand = cand;
  5557. best_pref = cand_pref;
  5558. }
  5559. }
  5560. if (!best_cand)
  5561. continue;
  5562. tree comp = get_computation_at (data->current_loop,
  5563. &dummy_use, best_cand,
  5564. SSA_NAME_DEF_STMT (def));
  5565. if (!comp)
  5566. continue;
  5567. if (count > 1)
  5568. {
  5569. tree vexpr = make_node (DEBUG_EXPR_DECL);
  5570. DECL_ARTIFICIAL (vexpr) = 1;
  5571. TREE_TYPE (vexpr) = TREE_TYPE (comp);
  5572. if (SSA_NAME_VAR (def))
  5573. DECL_MODE (vexpr) = DECL_MODE (SSA_NAME_VAR (def));
  5574. else
  5575. DECL_MODE (vexpr) = TYPE_MODE (TREE_TYPE (vexpr));
  5576. gdebug *def_temp
  5577. = gimple_build_debug_bind (vexpr, comp, NULL);
  5578. gimple_stmt_iterator gsi;
  5579. if (gimple_code (SSA_NAME_DEF_STMT (def)) == GIMPLE_PHI)
  5580. gsi = gsi_after_labels (gimple_bb
  5581. (SSA_NAME_DEF_STMT (def)));
  5582. else
  5583. gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (def));
  5584. gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
  5585. comp = vexpr;
  5586. }
  5587. FOR_EACH_IMM_USE_STMT (stmt, imm_iter, def)
  5588. {
  5589. if (!gimple_debug_bind_p (stmt))
  5590. continue;
  5591. FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
  5592. SET_USE (use_p, comp);
  5593. update_stmt (stmt);
  5594. }
  5595. }
  5596. }
  5597. }
  5598. release_defs_bitset (toremove);
  5599. BITMAP_FREE (toremove);
  5600. }
  5601. /* Frees memory occupied by struct tree_niter_desc in *VALUE. Callback
  5602. for hash_map::traverse. */
  5603. bool
  5604. free_tree_niter_desc (edge const &, tree_niter_desc *const &value, void *)
  5605. {
  5606. free (value);
  5607. return true;
  5608. }
  5609. /* Frees data allocated by the optimization of a single loop. */
  5610. static void
  5611. free_loop_data (struct ivopts_data *data)
  5612. {
  5613. unsigned i, j;
  5614. bitmap_iterator bi;
  5615. tree obj;
  5616. if (data->niters)
  5617. {
  5618. data->niters->traverse<void *, free_tree_niter_desc> (NULL);
  5619. delete data->niters;
  5620. data->niters = NULL;
  5621. }
  5622. EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
  5623. {
  5624. struct version_info *info;
  5625. info = ver_info (data, i);
  5626. free (info->iv);
  5627. info->iv = NULL;
  5628. info->has_nonlin_use = false;
  5629. info->preserve_biv = false;
  5630. info->inv_id = 0;
  5631. }
  5632. bitmap_clear (data->relevant);
  5633. bitmap_clear (data->important_candidates);
  5634. for (i = 0; i < n_iv_uses (data); i++)
  5635. {
  5636. struct iv_use *use = iv_use (data, i);
  5637. free (use->iv);
  5638. BITMAP_FREE (use->related_cands);
  5639. for (j = 0; j < use->n_map_members; j++)
  5640. if (use->cost_map[j].depends_on)
  5641. BITMAP_FREE (use->cost_map[j].depends_on);
  5642. free (use->cost_map);
  5643. free (use);
  5644. }
  5645. data->iv_uses.truncate (0);
  5646. for (i = 0; i < n_iv_cands (data); i++)
  5647. {
  5648. struct iv_cand *cand = iv_cand (data, i);
  5649. free (cand->iv);
  5650. if (cand->depends_on)
  5651. BITMAP_FREE (cand->depends_on);
  5652. free (cand);
  5653. }
  5654. data->iv_candidates.truncate (0);
  5655. if (data->version_info_size < num_ssa_names)
  5656. {
  5657. data->version_info_size = 2 * num_ssa_names;
  5658. free (data->version_info);
  5659. data->version_info = XCNEWVEC (struct version_info, data->version_info_size);
  5660. }
  5661. data->max_inv_id = 0;
  5662. FOR_EACH_VEC_ELT (decl_rtl_to_reset, i, obj)
  5663. SET_DECL_RTL (obj, NULL_RTX);
  5664. decl_rtl_to_reset.truncate (0);
  5665. data->inv_expr_tab->empty ();
  5666. data->inv_expr_id = 0;
  5667. }
  5668. /* Finalizes data structures used by the iv optimization pass. LOOPS is the
  5669. loop tree. */
  5670. static void
  5671. tree_ssa_iv_optimize_finalize (struct ivopts_data *data)
  5672. {
  5673. free_loop_data (data);
  5674. free (data->version_info);
  5675. BITMAP_FREE (data->relevant);
  5676. BITMAP_FREE (data->important_candidates);
  5677. decl_rtl_to_reset.release ();
  5678. data->iv_uses.release ();
  5679. data->iv_candidates.release ();
  5680. delete data->inv_expr_tab;
  5681. data->inv_expr_tab = NULL;
  5682. free_affine_expand_cache (&data->name_expansion_cache);
  5683. }
  5684. /* Returns true if the loop body BODY includes any function calls. */
  5685. static bool
  5686. loop_body_includes_call (basic_block *body, unsigned num_nodes)
  5687. {
  5688. gimple_stmt_iterator gsi;
  5689. unsigned i;
  5690. for (i = 0; i < num_nodes; i++)
  5691. for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
  5692. {
  5693. gimple stmt = gsi_stmt (gsi);
  5694. if (is_gimple_call (stmt)
  5695. && !is_inexpensive_builtin (gimple_call_fndecl (stmt)))
  5696. return true;
  5697. }
  5698. return false;
  5699. }
  5700. /* Optimizes the LOOP. Returns true if anything changed. */
  5701. static bool
  5702. tree_ssa_iv_optimize_loop (struct ivopts_data *data, struct loop *loop)
  5703. {
  5704. bool changed = false;
  5705. struct iv_ca *iv_ca;
  5706. edge exit = single_dom_exit (loop);
  5707. basic_block *body;
  5708. gcc_assert (!data->niters);
  5709. data->current_loop = loop;
  5710. data->loop_loc = find_loop_location (loop);
  5711. data->speed = optimize_loop_for_speed_p (loop);
  5712. if (dump_file && (dump_flags & TDF_DETAILS))
  5713. {
  5714. fprintf (dump_file, "Processing loop %d", loop->num);
  5715. if (data->loop_loc != UNKNOWN_LOCATION)
  5716. fprintf (dump_file, " at %s:%d", LOCATION_FILE (data->loop_loc),
  5717. LOCATION_LINE (data->loop_loc));
  5718. fprintf (dump_file, "\n");
  5719. if (exit)
  5720. {
  5721. fprintf (dump_file, " single exit %d -> %d, exit condition ",
  5722. exit->src->index, exit->dest->index);
  5723. print_gimple_stmt (dump_file, last_stmt (exit->src), 0, TDF_SLIM);
  5724. fprintf (dump_file, "\n");
  5725. }
  5726. fprintf (dump_file, "\n");
  5727. }
  5728. body = get_loop_body (loop);
  5729. data->body_includes_call = loop_body_includes_call (body, loop->num_nodes);
  5730. renumber_gimple_stmt_uids_in_blocks (body, loop->num_nodes);
  5731. free (body);
  5732. data->loop_single_exit_p = exit != NULL && loop_only_exit_p (loop, exit);
  5733. /* For each ssa name determines whether it behaves as an induction variable
  5734. in some loop. */
  5735. if (!find_induction_variables (data))
  5736. goto finish;
  5737. /* Finds interesting uses (item 1). */
  5738. find_interesting_uses (data);
  5739. if (n_iv_uses (data) > MAX_CONSIDERED_USES)
  5740. goto finish;
  5741. /* Finds candidates for the induction variables (item 2). */
  5742. find_iv_candidates (data);
  5743. /* Calculates the costs (item 3, part 1). */
  5744. determine_iv_costs (data);
  5745. determine_use_iv_costs (data);
  5746. determine_set_costs (data);
  5747. /* Find the optimal set of induction variables (item 3, part 2). */
  5748. iv_ca = find_optimal_iv_set (data);
  5749. if (!iv_ca)
  5750. goto finish;
  5751. changed = true;
  5752. /* Create the new induction variables (item 4, part 1). */
  5753. create_new_ivs (data, iv_ca);
  5754. iv_ca_free (&iv_ca);
  5755. /* Rewrite the uses (item 4, part 2). */
  5756. rewrite_uses (data);
  5757. /* Remove the ivs that are unused after rewriting. */
  5758. remove_unused_ivs (data);
  5759. /* We have changed the structure of induction variables; it might happen
  5760. that definitions in the scev database refer to some of them that were
  5761. eliminated. */
  5762. scev_reset ();
  5763. finish:
  5764. free_loop_data (data);
  5765. return changed;
  5766. }
  5767. /* Main entry point. Optimizes induction variables in loops. */
  5768. void
  5769. tree_ssa_iv_optimize (void)
  5770. {
  5771. struct loop *loop;
  5772. struct ivopts_data data;
  5773. tree_ssa_iv_optimize_init (&data);
  5774. /* Optimize the loops starting with the innermost ones. */
  5775. FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
  5776. {
  5777. if (dump_file && (dump_flags & TDF_DETAILS))
  5778. flow_loop_dump (loop, dump_file, NULL, 1);
  5779. tree_ssa_iv_optimize_loop (&data, loop);
  5780. }
  5781. tree_ssa_iv_optimize_finalize (&data);
  5782. }