1 //===-- llvm/CodeGen/GlobalISel/Legalizer.cpp -----------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 /// \file This file implements the LegalizerHelper class to legalize individual 10 /// instructions and the LegalizePass wrapper pass for the primary 11 /// legalization. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/CodeGen/GlobalISel/Legalizer.h" 16 #include "llvm/ADT/PostOrderIterator.h" 17 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 18 #include "llvm/CodeGen/GlobalISel/CSEInfo.h" 19 #include "llvm/CodeGen/GlobalISel/CSEMIRBuilder.h" 20 #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h" 21 #include "llvm/CodeGen/GlobalISel/GISelWorkList.h" 22 #include "llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h" 23 #include "llvm/CodeGen/GlobalISel/LegalizerHelper.h" 24 #include "llvm/CodeGen/GlobalISel/LostDebugLocObserver.h" 25 #include "llvm/CodeGen/GlobalISel/Utils.h" 26 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" 27 #include "llvm/CodeGen/TargetPassConfig.h" 28 #include "llvm/CodeGen/TargetSubtargetInfo.h" 29 #include "llvm/InitializePasses.h" 30 #include "llvm/Support/Debug.h" 31 #include "llvm/Support/Error.h" 32 33 #define DEBUG_TYPE "legalizer" 34 35 using namespace llvm; 36 37 static cl::opt<bool> 38 EnableCSEInLegalizer("enable-cse-in-legalizer", 39 cl::desc("Should enable CSE in Legalizer"), 40 cl::Optional, cl::init(false)); 41 42 // This is a temporary hack, should be removed soon. 43 static cl::opt<bool> AllowGInsertAsArtifact( 44 "allow-ginsert-as-artifact", 45 cl::desc("Allow G_INSERT to be considered an artifact. Hack around AMDGPU " 46 "test infinite loops."), 47 cl::Optional, cl::init(true)); 48 49 enum class DebugLocVerifyLevel { 50 None, 51 Legalizations, 52 LegalizationsAndArtifactCombiners, 53 }; 54 #ifndef NDEBUG 55 static cl::opt<DebugLocVerifyLevel> VerifyDebugLocs( 56 "verify-legalizer-debug-locs", 57 cl::desc("Verify that debug locations are handled"), 58 cl::values( 59 clEnumValN(DebugLocVerifyLevel::None, "none", "No verification"), 60 clEnumValN(DebugLocVerifyLevel::Legalizations, "legalizations", 61 "Verify legalizations"), 62 clEnumValN(DebugLocVerifyLevel::LegalizationsAndArtifactCombiners, 63 "legalizations+artifactcombiners", 64 "Verify legalizations and artifact combines")), 65 cl::init(DebugLocVerifyLevel::Legalizations)); 66 #else 67 // Always disable it for release builds by preventing the observer from being 68 // installed. 69 static const DebugLocVerifyLevel VerifyDebugLocs = DebugLocVerifyLevel::None; 70 #endif 71 72 char Legalizer::ID = 0; 73 INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE, 74 "Legalize the Machine IR a function's Machine IR", false, 75 false) 76 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) 77 INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass) 78 INITIALIZE_PASS_END(Legalizer, DEBUG_TYPE, 79 "Legalize the Machine IR a function's Machine IR", false, 80 false) 81 82 Legalizer::Legalizer() : MachineFunctionPass(ID) { } 83 84 void Legalizer::getAnalysisUsage(AnalysisUsage &AU) const { 85 AU.addRequired<TargetPassConfig>(); 86 AU.addRequired<GISelCSEAnalysisWrapperPass>(); 87 AU.addPreserved<GISelCSEAnalysisWrapperPass>(); 88 getSelectionDAGFallbackAnalysisUsage(AU); 89 MachineFunctionPass::getAnalysisUsage(AU); 90 } 91 92 void Legalizer::init(MachineFunction &MF) { 93 } 94 95 static bool isArtifact(const MachineInstr &MI) { 96 switch (MI.getOpcode()) { 97 default: 98 return false; 99 case TargetOpcode::G_TRUNC: 100 case TargetOpcode::G_ZEXT: 101 case TargetOpcode::G_ANYEXT: 102 case TargetOpcode::G_SEXT: 103 case TargetOpcode::G_MERGE_VALUES: 104 case TargetOpcode::G_UNMERGE_VALUES: 105 case TargetOpcode::G_CONCAT_VECTORS: 106 case TargetOpcode::G_BUILD_VECTOR: 107 case TargetOpcode::G_EXTRACT: 108 return true; 109 case TargetOpcode::G_INSERT: 110 return AllowGInsertAsArtifact; 111 } 112 } 113 using InstListTy = GISelWorkList<256>; 114 using ArtifactListTy = GISelWorkList<128>; 115 116 namespace { 117 class LegalizerWorkListManager : public GISelChangeObserver { 118 InstListTy &InstList; 119 ArtifactListTy &ArtifactList; 120 #ifndef NDEBUG 121 SmallVector<MachineInstr *, 4> NewMIs; 122 #endif 123 124 public: 125 LegalizerWorkListManager(InstListTy &Insts, ArtifactListTy &Arts) 126 : InstList(Insts), ArtifactList(Arts) {} 127 128 void createdOrChangedInstr(MachineInstr &MI) { 129 // Only legalize pre-isel generic instructions. 130 // Legalization process could generate Target specific pseudo 131 // instructions with generic types. Don't record them 132 if (isPreISelGenericOpcode(MI.getOpcode())) { 133 if (isArtifact(MI)) 134 ArtifactList.insert(&MI); 135 else 136 InstList.insert(&MI); 137 } 138 } 139 140 void createdInstr(MachineInstr &MI) override { 141 LLVM_DEBUG(NewMIs.push_back(&MI)); 142 createdOrChangedInstr(MI); 143 } 144 145 void printNewInstrs() { 146 LLVM_DEBUG({ 147 for (const auto *MI : NewMIs) 148 dbgs() << ".. .. New MI: " << *MI; 149 NewMIs.clear(); 150 }); 151 } 152 153 void erasingInstr(MachineInstr &MI) override { 154 LLVM_DEBUG(dbgs() << ".. .. Erasing: " << MI); 155 InstList.remove(&MI); 156 ArtifactList.remove(&MI); 157 } 158 159 void changingInstr(MachineInstr &MI) override { 160 LLVM_DEBUG(dbgs() << ".. .. Changing MI: " << MI); 161 } 162 163 void changedInstr(MachineInstr &MI) override { 164 // When insts change, we want to revisit them to legalize them again. 165 // We'll consider them the same as created. 166 LLVM_DEBUG(dbgs() << ".. .. Changed MI: " << MI); 167 createdOrChangedInstr(MI); 168 } 169 }; 170 } // namespace 171 172 Legalizer::MFResult 173 Legalizer::legalizeMachineFunction(MachineFunction &MF, const LegalizerInfo &LI, 174 ArrayRef<GISelChangeObserver *> AuxObservers, 175 LostDebugLocObserver &LocObserver, 176 MachineIRBuilder &MIRBuilder) { 177 MIRBuilder.setMF(MF); 178 MachineRegisterInfo &MRI = MF.getRegInfo(); 179 180 // Populate worklists. 181 InstListTy InstList; 182 ArtifactListTy ArtifactList; 183 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF); 184 // Perform legalization bottom up so we can DCE as we legalize. 185 // Traverse BB in RPOT and within each basic block, add insts top down, 186 // so when we pop_back_val in the legalization process, we traverse bottom-up. 187 for (auto *MBB : RPOT) { 188 if (MBB->empty()) 189 continue; 190 for (MachineInstr &MI : *MBB) { 191 // Only legalize pre-isel generic instructions: others don't have types 192 // and are assumed to be legal. 193 if (!isPreISelGenericOpcode(MI.getOpcode())) 194 continue; 195 if (isArtifact(MI)) 196 ArtifactList.deferred_insert(&MI); 197 else 198 InstList.deferred_insert(&MI); 199 } 200 } 201 ArtifactList.finalize(); 202 InstList.finalize(); 203 204 // This observer keeps the worklists updated. 205 LegalizerWorkListManager WorkListObserver(InstList, ArtifactList); 206 // We want both WorkListObserver as well as all the auxiliary observers (e.g. 207 // CSEInfo) to observe all changes. Use the wrapper observer. 208 GISelObserverWrapper WrapperObserver(&WorkListObserver); 209 for (GISelChangeObserver *Observer : AuxObservers) 210 WrapperObserver.addObserver(Observer); 211 212 // Now install the observer as the delegate to MF. 213 // This will keep all the observers notified about new insertions/deletions. 214 RAIIMFObsDelInstaller Installer(MF, WrapperObserver); 215 LegalizerHelper Helper(MF, LI, WrapperObserver, MIRBuilder); 216 LegalizationArtifactCombiner ArtCombiner(MIRBuilder, MRI, LI); 217 bool Changed = false; 218 SmallVector<MachineInstr *, 128> RetryList; 219 do { 220 LLVM_DEBUG(dbgs() << "=== New Iteration ===\n"); 221 assert(RetryList.empty() && "Expected no instructions in RetryList"); 222 unsigned NumArtifacts = ArtifactList.size(); 223 while (!InstList.empty()) { 224 MachineInstr &MI = *InstList.pop_back_val(); 225 assert(isPreISelGenericOpcode(MI.getOpcode()) && 226 "Expecting generic opcode"); 227 if (isTriviallyDead(MI, MRI)) { 228 eraseInstr(MI, MRI, &LocObserver); 229 continue; 230 } 231 232 // Do the legalization for this instruction. 233 auto Res = Helper.legalizeInstrStep(MI, LocObserver); 234 // Error out if we couldn't legalize this instruction. We may want to 235 // fall back to DAG ISel instead in the future. 236 if (Res == LegalizerHelper::UnableToLegalize) { 237 // Move illegal artifacts to RetryList instead of aborting because 238 // legalizing InstList may generate artifacts that allow 239 // ArtifactCombiner to combine away them. 240 if (isArtifact(MI)) { 241 LLVM_DEBUG(dbgs() << ".. Not legalized, moving to artifacts retry\n"); 242 assert(NumArtifacts == 0 && 243 "Artifacts are only expected in instruction list starting the " 244 "second iteration, but each iteration starting second must " 245 "start with an empty artifacts list"); 246 (void)NumArtifacts; 247 RetryList.push_back(&MI); 248 continue; 249 } 250 Helper.MIRBuilder.stopObservingChanges(); 251 return {Changed, &MI}; 252 } 253 WorkListObserver.printNewInstrs(); 254 LocObserver.checkpoint(); 255 Changed |= Res == LegalizerHelper::Legalized; 256 } 257 // Try to combine the instructions in RetryList again if there 258 // are new artifacts. If not, stop legalizing. 259 if (!RetryList.empty()) { 260 if (!ArtifactList.empty()) { 261 while (!RetryList.empty()) 262 ArtifactList.insert(RetryList.pop_back_val()); 263 } else { 264 LLVM_DEBUG(dbgs() << "No new artifacts created, not retrying!\n"); 265 Helper.MIRBuilder.stopObservingChanges(); 266 return {Changed, RetryList.front()}; 267 } 268 } 269 LocObserver.checkpoint(); 270 while (!ArtifactList.empty()) { 271 MachineInstr &MI = *ArtifactList.pop_back_val(); 272 assert(isPreISelGenericOpcode(MI.getOpcode()) && 273 "Expecting generic opcode"); 274 if (isTriviallyDead(MI, MRI)) { 275 eraseInstr(MI, MRI, &LocObserver); 276 continue; 277 } 278 SmallVector<MachineInstr *, 4> DeadInstructions; 279 LLVM_DEBUG(dbgs() << "Trying to combine: " << MI); 280 if (ArtCombiner.tryCombineInstruction(MI, DeadInstructions, 281 WrapperObserver)) { 282 WorkListObserver.printNewInstrs(); 283 eraseInstrs(DeadInstructions, MRI, &LocObserver); 284 LocObserver.checkpoint( 285 VerifyDebugLocs == 286 DebugLocVerifyLevel::LegalizationsAndArtifactCombiners); 287 Changed = true; 288 continue; 289 } 290 // If this was not an artifact (that could be combined away), this might 291 // need special handling. Add it to InstList, so when it's processed 292 // there, it has to be legal or specially handled. 293 else { 294 LLVM_DEBUG(dbgs() << ".. Not combined, moving to instructions list\n"); 295 InstList.insert(&MI); 296 } 297 } 298 } while (!InstList.empty()); 299 300 return {Changed, /*FailedOn*/ nullptr}; 301 } 302 303 bool Legalizer::runOnMachineFunction(MachineFunction &MF) { 304 // If the ISel pipeline failed, do not bother running that pass. 305 if (MF.getProperties().hasProperty( 306 MachineFunctionProperties::Property::FailedISel)) 307 return false; 308 LLVM_DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n'); 309 init(MF); 310 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>(); 311 GISelCSEAnalysisWrapper &Wrapper = 312 getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper(); 313 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr); 314 315 const size_t NumBlocks = MF.size(); 316 317 std::unique_ptr<MachineIRBuilder> MIRBuilder; 318 GISelCSEInfo *CSEInfo = nullptr; 319 bool EnableCSE = EnableCSEInLegalizer.getNumOccurrences() 320 ? EnableCSEInLegalizer 321 : TPC.isGISelCSEEnabled(); 322 if (EnableCSE) { 323 MIRBuilder = std::make_unique<CSEMIRBuilder>(); 324 CSEInfo = &Wrapper.get(TPC.getCSEConfig()); 325 MIRBuilder->setCSEInfo(CSEInfo); 326 } else 327 MIRBuilder = std::make_unique<MachineIRBuilder>(); 328 329 SmallVector<GISelChangeObserver *, 1> AuxObservers; 330 if (EnableCSE && CSEInfo) { 331 // We want CSEInfo in addition to WorkListObserver to observe all changes. 332 AuxObservers.push_back(CSEInfo); 333 } 334 assert(!CSEInfo || !errorToBool(CSEInfo->verify())); 335 LostDebugLocObserver LocObserver(DEBUG_TYPE); 336 if (VerifyDebugLocs > DebugLocVerifyLevel::None) 337 AuxObservers.push_back(&LocObserver); 338 339 const LegalizerInfo &LI = *MF.getSubtarget().getLegalizerInfo(); 340 MFResult Result = 341 legalizeMachineFunction(MF, LI, AuxObservers, LocObserver, *MIRBuilder); 342 343 if (Result.FailedOn) { 344 reportGISelFailure(MF, TPC, MORE, "gisel-legalize", 345 "unable to legalize instruction", *Result.FailedOn); 346 return false; 347 } 348 // For now don't support if new blocks are inserted - we would need to fix the 349 // outer loop for that. 350 if (MF.size() != NumBlocks) { 351 MachineOptimizationRemarkMissed R("gisel-legalize", "GISelFailure", 352 MF.getFunction().getSubprogram(), 353 /*MBB=*/nullptr); 354 R << "inserting blocks is not supported yet"; 355 reportGISelFailure(MF, TPC, MORE, R); 356 return false; 357 } 358 359 if (LocObserver.getNumLostDebugLocs()) { 360 MachineOptimizationRemarkMissed R("gisel-legalize", "LostDebugLoc", 361 MF.getFunction().getSubprogram(), 362 /*MBB=*/&*MF.begin()); 363 R << "lost " 364 << ore::NV("NumLostDebugLocs", LocObserver.getNumLostDebugLocs()) 365 << " debug locations during pass"; 366 reportGISelWarning(MF, TPC, MORE, R); 367 // Example remark: 368 // --- !Missed 369 // Pass: gisel-legalize 370 // Name: GISelFailure 371 // DebugLoc: { File: '.../legalize-urem.mir', Line: 1, Column: 0 } 372 // Function: test_urem_s32 373 // Args: 374 // - String: 'lost ' 375 // - NumLostDebugLocs: '1' 376 // - String: ' debug locations during pass' 377 // ... 378 } 379 380 // If for some reason CSE was not enabled, make sure that we invalidate the 381 // CSEInfo object (as we currently declare that the analysis is preserved). 382 // The next time get on the wrapper is called, it will force it to recompute 383 // the analysis. 384 if (!EnableCSE) 385 Wrapper.setComputed(false); 386 return Result.Changed; 387 } 388