1 //=== ScopDetectionDiagnostic.cpp - Error diagnostics --------- -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Small set of diagnostic helper classes to encapsulate any errors occurred 11 // during the detection of Scops. 12 // 13 // The ScopDetection defines a set of error classes (via Statistic variables) 14 // that groups a number of individual errors into a group, e.g. non-affinity 15 // related errors. 16 // On error we generate an object that carries enough additional information 17 // to diagnose the error and generate a helpful error message. 18 // 19 //===----------------------------------------------------------------------===// 20 #include "polly/ScopDetectionDiagnostic.h" 21 22 #include "llvm/Analysis/LoopInfo.h" 23 #include "llvm/Analysis/AliasSetTracker.h" 24 #include "llvm/IR/BasicBlock.h" 25 #include "llvm/IR/DebugInfo.h" 26 #include "llvm/IR/DebugLoc.h" 27 #include "llvm/IR/DiagnosticInfo.h" 28 #include "llvm/IR/LLVMContext.h" 29 #include "llvm/IR/Value.h" 30 #include "llvm/ADT/SmallVector.h" 31 #include "llvm/ADT/Statistic.h" 32 33 #include "llvm/Analysis/RegionInfo.h" 34 35 #define DEBUG_TYPE "polly-detect" 36 #include "llvm/Support/Debug.h" 37 38 #include <string> 39 40 #define BADSCOP_STAT(NAME, DESC) \ 41 STATISTIC(Bad##NAME##ForScop, "Number of bad regions for Scop: " DESC) 42 43 BADSCOP_STAT(CFG, "CFG too complex"); 44 BADSCOP_STAT(IndVar, "Non canonical induction variable in loop"); 45 BADSCOP_STAT(IndEdge, "Found invalid region entering edges"); 46 BADSCOP_STAT(LoopBound, "Loop bounds can not be computed"); 47 BADSCOP_STAT(FuncCall, "Function call with side effects appeared"); 48 BADSCOP_STAT(AffFunc, "Expression not affine"); 49 BADSCOP_STAT(Alias, "Found base address alias"); 50 BADSCOP_STAT(SimpleLoop, "Loop not in -loop-simplify form"); 51 BADSCOP_STAT(Other, "Others"); 52 53 namespace polly { 54 /// @brief Small string conversion via raw_string_stream. 55 template <typename T> std::string operator+(Twine LHS, const T &RHS) { 56 std::string Buf; 57 raw_string_ostream fmt(Buf); 58 fmt << RHS; 59 fmt.flush(); 60 61 return LHS.concat(Buf).str(); 62 } 63 64 void getDebugLocation(const Region *R, unsigned &LineBegin, unsigned &LineEnd, 65 std::string &FileName) { 66 LineBegin = -1; 67 LineEnd = 0; 68 69 for (const BasicBlock *BB : R->blocks()) 70 for (const Instruction &Inst : *BB) { 71 DebugLoc DL = Inst.getDebugLoc(); 72 if (DL.isUnknown()) 73 continue; 74 75 DIScope Scope(DL.getScope(Inst.getContext())); 76 77 if (FileName.empty()) 78 FileName = Scope.getFilename(); 79 80 unsigned NewLine = DL.getLine(); 81 82 LineBegin = std::min(LineBegin, NewLine); 83 LineEnd = std::max(LineEnd, NewLine); 84 } 85 } 86 87 void emitRejectionRemarks(const llvm::Function &F, const RejectLog &Log) { 88 LLVMContext &Ctx = F.getContext(); 89 90 const Region *R = Log.region(); 91 const BasicBlock *Entry = R->getEntry(); 92 DebugLoc DL = Entry->getTerminator()->getDebugLoc(); 93 94 emitOptimizationRemarkMissed( 95 Ctx, DEBUG_TYPE, F, DL, 96 "The following errors keep this region from being a Scop."); 97 for (RejectReasonPtr RR : Log) { 98 const DebugLoc &Loc = RR->getDebugLoc(); 99 if (!Loc.isUnknown()) 100 emitOptimizationRemarkMissed(Ctx, DEBUG_TYPE, F, Loc, 101 RR->getEndUserMessage()); 102 } 103 } 104 105 void emitValidRemarks(const llvm::Function &F, const Region *R) { 106 LLVMContext &Ctx = F.getContext(); 107 108 const BasicBlock *Entry = R->getEntry(); 109 const BasicBlock *Exit = R->getExit(); 110 111 const DebugLoc &Begin = Entry->getFirstNonPHIOrDbg()->getDebugLoc(); 112 const DebugLoc &End = Exit->getFirstNonPHIOrDbg()->getDebugLoc(); 113 114 emitOptimizationRemark(Ctx, DEBUG_TYPE, F, Begin, 115 "A valid Scop begins here."); 116 emitOptimizationRemark(Ctx, DEBUG_TYPE, F, End, "A valid Scop ends here."); 117 } 118 119 //===----------------------------------------------------------------------===// 120 // RejectReason. 121 const DebugLoc RejectReason::Unknown = DebugLoc(); 122 123 const llvm::DebugLoc &RejectReason::getDebugLoc() const { 124 // Allocate an empty DebugLoc and return it a reference to it. 125 return Unknown; 126 } 127 128 //===----------------------------------------------------------------------===// 129 // ReportCFG. 130 131 ReportCFG::ReportCFG(const RejectReasonKind K) : RejectReason(K) { 132 ++BadCFGForScop; 133 } 134 135 bool ReportCFG::classof(const RejectReason *RR) { 136 return RR->getKind() >= rrkCFG && RR->getKind() <= rrkLastCFG; 137 } 138 139 //===----------------------------------------------------------------------===// 140 // ReportNonBranchTerminator. 141 142 std::string ReportNonBranchTerminator::getMessage() const { 143 return ("Non branch instruction terminates BB: " + BB->getName()).str(); 144 } 145 146 const DebugLoc &ReportNonBranchTerminator::getDebugLoc() const { 147 return BB->getTerminator()->getDebugLoc(); 148 } 149 150 bool ReportNonBranchTerminator::classof(const RejectReason *RR) { 151 return RR->getKind() == rrkNonBranchTerminator; 152 } 153 154 //===----------------------------------------------------------------------===// 155 // ReportCondition. 156 157 std::string ReportCondition::getMessage() const { 158 return ("Not well structured condition at BB: " + BB->getName()).str(); 159 } 160 161 const DebugLoc &ReportCondition::getDebugLoc() const { 162 return BB->getTerminator()->getDebugLoc(); 163 } 164 165 bool ReportCondition::classof(const RejectReason *RR) { 166 return RR->getKind() == rrkCondition; 167 } 168 169 //===----------------------------------------------------------------------===// 170 // ReportAffFunc. 171 172 ReportAffFunc::ReportAffFunc(const RejectReasonKind K, const Instruction *Inst) 173 : RejectReason(K), Inst(Inst) { 174 ++BadAffFuncForScop; 175 } 176 177 bool ReportAffFunc::classof(const RejectReason *RR) { 178 return RR->getKind() >= rrkAffFunc && RR->getKind() <= rrkLastAffFunc; 179 } 180 181 //===----------------------------------------------------------------------===// 182 // ReportUndefCond. 183 184 std::string ReportUndefCond::getMessage() const { 185 return ("Condition based on 'undef' value in BB: " + BB->getName()).str(); 186 } 187 188 bool ReportUndefCond::classof(const RejectReason *RR) { 189 return RR->getKind() == rrkUndefCond; 190 } 191 192 //===----------------------------------------------------------------------===// 193 // ReportInvalidCond. 194 195 std::string ReportInvalidCond::getMessage() const { 196 return ("Condition in BB '" + BB->getName()).str() + 197 "' neither constant nor an icmp instruction"; 198 } 199 200 bool ReportInvalidCond::classof(const RejectReason *RR) { 201 return RR->getKind() == rrkInvalidCond; 202 } 203 204 //===----------------------------------------------------------------------===// 205 // ReportUndefOperand. 206 207 std::string ReportUndefOperand::getMessage() const { 208 return ("undef operand in branch at BB: " + BB->getName()).str(); 209 } 210 211 bool ReportUndefOperand::classof(const RejectReason *RR) { 212 return RR->getKind() == rrkUndefOperand; 213 } 214 215 //===----------------------------------------------------------------------===// 216 // ReportNonAffBranch. 217 218 std::string ReportNonAffBranch::getMessage() const { 219 return ("Non affine branch in BB '" + BB->getName()).str() + "' with LHS: " + 220 *LHS + " and RHS: " + *RHS; 221 } 222 223 bool ReportNonAffBranch::classof(const RejectReason *RR) { 224 return RR->getKind() == rrkNonAffBranch; 225 } 226 227 //===----------------------------------------------------------------------===// 228 // ReportNoBasePtr. 229 230 std::string ReportNoBasePtr::getMessage() const { return "No base pointer"; } 231 232 bool ReportNoBasePtr::classof(const RejectReason *RR) { 233 return RR->getKind() == rrkNoBasePtr; 234 } 235 236 //===----------------------------------------------------------------------===// 237 // ReportUndefBasePtr. 238 239 std::string ReportUndefBasePtr::getMessage() const { 240 return "Undefined base pointer"; 241 } 242 243 bool ReportUndefBasePtr::classof(const RejectReason *RR) { 244 return RR->getKind() == rrkUndefBasePtr; 245 } 246 247 //===----------------------------------------------------------------------===// 248 // ReportVariantBasePtr. 249 250 std::string ReportVariantBasePtr::getMessage() const { 251 return "Base address not invariant in current region:" + *BaseValue; 252 } 253 254 std::string ReportVariantBasePtr::getEndUserMessage() const { 255 return "The base address of this array is not invariant inside the loop"; 256 } 257 258 bool ReportVariantBasePtr::classof(const RejectReason *RR) { 259 return RR->getKind() == rrkVariantBasePtr; 260 } 261 262 //===----------------------------------------------------------------------===// 263 // ReportNonAffineAccess. 264 265 std::string ReportNonAffineAccess::getMessage() const { 266 return "Non affine access function: " + *AccessFunction; 267 } 268 269 bool ReportNonAffineAccess::classof(const RejectReason *RR) { 270 return RR->getKind() == rrkNonAffineAccess; 271 } 272 273 //===----------------------------------------------------------------------===// 274 // ReportIndVar. 275 276 ReportIndVar::ReportIndVar(const RejectReasonKind K) : RejectReason(K) { 277 ++BadIndVarForScop; 278 } 279 280 //===----------------------------------------------------------------------===// 281 // ReportPhiNodeRefInRegion. 282 283 ReportPhiNodeRefInRegion::ReportPhiNodeRefInRegion(Instruction *Inst) 284 : ReportIndVar(rrkPhiNodeRefInRegion), Inst(Inst) {} 285 286 std::string ReportPhiNodeRefInRegion::getMessage() const { 287 return "SCEV of PHI node refers to SSA names in region: " + *Inst; 288 } 289 290 const DebugLoc &ReportPhiNodeRefInRegion::getDebugLoc() const { 291 return Inst->getDebugLoc(); 292 } 293 294 bool ReportPhiNodeRefInRegion::classof(const RejectReason *RR) { 295 return RR->getKind() == rrkPhiNodeRefInRegion; 296 } 297 298 //===----------------------------------------------------------------------===// 299 // ReportNonCanonicalPhiNode. 300 301 ReportNonCanonicalPhiNode::ReportNonCanonicalPhiNode(Instruction *Inst) 302 : ReportIndVar(rrkNonCanonicalPhiNode), Inst(Inst) {} 303 304 std::string ReportNonCanonicalPhiNode::getMessage() const { 305 return "Non canonical PHI node: " + *Inst; 306 } 307 308 const DebugLoc &ReportNonCanonicalPhiNode::getDebugLoc() const { 309 return Inst->getDebugLoc(); 310 } 311 312 bool ReportNonCanonicalPhiNode::classof(const RejectReason *RR) { 313 return RR->getKind() == rrkNonCanonicalPhiNode; 314 } 315 316 //===----------------------------------------------------------------------===// 317 // ReportLoopHeader. 318 319 ReportLoopHeader::ReportLoopHeader(Loop *L) 320 : ReportIndVar(rrkLoopHeader), L(L) {} 321 322 std::string ReportLoopHeader::getMessage() const { 323 return ("No canonical IV at loop header: " + L->getHeader()->getName()).str(); 324 } 325 326 const DebugLoc &ReportLoopHeader::getDebugLoc() const { 327 BasicBlock *BB = L->getHeader(); 328 return BB->getTerminator()->getDebugLoc(); 329 } 330 331 bool ReportLoopHeader::classof(const RejectReason *RR) { 332 return RR->getKind() == rrkLoopHeader; 333 } 334 335 //===----------------------------------------------------------------------===// 336 // ReportIndEdge. 337 338 ReportIndEdge::ReportIndEdge(BasicBlock *BB) 339 : RejectReason(rrkIndEdge), BB(BB) { 340 ++BadIndEdgeForScop; 341 } 342 343 std::string ReportIndEdge::getMessage() const { 344 return "Region has invalid entering edges!"; 345 } 346 347 const DebugLoc &ReportIndEdge::getDebugLoc() const { 348 return BB->getTerminator()->getDebugLoc(); 349 } 350 351 bool ReportIndEdge::classof(const RejectReason *RR) { 352 return RR->getKind() == rrkIndEdge; 353 } 354 355 //===----------------------------------------------------------------------===// 356 // ReportLoopBound. 357 358 ReportLoopBound::ReportLoopBound(Loop *L, const SCEV *LoopCount) 359 : RejectReason(rrkLoopBound), L(L), LoopCount(LoopCount) { 360 ++BadLoopBoundForScop; 361 } 362 363 std::string ReportLoopBound::getMessage() const { 364 return "Non affine loop bound '" + *LoopCount + "' in loop: " + 365 L->getHeader()->getName(); 366 } 367 368 const DebugLoc &ReportLoopBound::getDebugLoc() const { 369 const BasicBlock *BB = L->getHeader(); 370 return BB->getTerminator()->getDebugLoc(); 371 } 372 373 bool ReportLoopBound::classof(const RejectReason *RR) { 374 return RR->getKind() == rrkLoopBound; 375 } 376 377 //===----------------------------------------------------------------------===// 378 // ReportFuncCall. 379 380 ReportFuncCall::ReportFuncCall(Instruction *Inst) 381 : RejectReason(rrkFuncCall), Inst(Inst) { 382 ++BadFuncCallForScop; 383 } 384 385 std::string ReportFuncCall::getMessage() const { 386 return "Call instruction: " + *Inst; 387 } 388 389 const DebugLoc &ReportFuncCall::getDebugLoc() const { 390 return Inst->getDebugLoc(); 391 } 392 393 std::string ReportFuncCall::getEndUserMessage() const { 394 return "This function call cannot be handeled. " 395 "Try to inline it."; 396 } 397 398 bool ReportFuncCall::classof(const RejectReason *RR) { 399 return RR->getKind() == rrkFuncCall; 400 } 401 402 //===----------------------------------------------------------------------===// 403 // ReportAlias. 404 405 ReportAlias::ReportAlias(Instruction *Inst, AliasSet &AS) 406 : RejectReason(rrkAlias), Inst(Inst), AS(AS) { 407 ++BadAliasForScop; 408 } 409 410 std::string ReportAlias::formatInvalidAlias(AliasSet &AS) const { 411 std::string Message; 412 raw_string_ostream OS(Message); 413 414 OS << "Possible aliasing: "; 415 416 std::vector<Value *> Pointers; 417 418 for (const auto &I : AS) 419 Pointers.push_back(I.getValue()); 420 421 std::sort(Pointers.begin(), Pointers.end()); 422 423 for (std::vector<Value *>::iterator PI = Pointers.begin(), 424 PE = Pointers.end(); 425 ;) { 426 Value *V = *PI; 427 428 if (V->getName().size() == 0) 429 OS << "\"" << *V << "\""; 430 else 431 OS << "\"" << V->getName() << "\""; 432 433 ++PI; 434 435 if (PI != PE) 436 OS << ", "; 437 else 438 break; 439 } 440 441 return OS.str(); 442 } 443 444 std::string ReportAlias::getMessage() const { return formatInvalidAlias(AS); } 445 446 const DebugLoc &ReportAlias::getDebugLoc() const { return Inst->getDebugLoc(); } 447 448 bool ReportAlias::classof(const RejectReason *RR) { 449 return RR->getKind() == rrkAlias; 450 } 451 452 //===----------------------------------------------------------------------===// 453 // ReportSimpleLoop. 454 455 ReportSimpleLoop::ReportSimpleLoop() : RejectReason(rrkSimpleLoop) { 456 ++BadSimpleLoopForScop; 457 } 458 459 std::string ReportSimpleLoop::getMessage() const { 460 return "Loop not in simplify form is invalid!"; 461 } 462 463 bool ReportSimpleLoop::classof(const RejectReason *RR) { 464 return RR->getKind() == rrkSimpleLoop; 465 } 466 467 //===----------------------------------------------------------------------===// 468 // ReportOther. 469 470 std::string ReportOther::getMessage() const { return "Unknown reject reason"; } 471 472 ReportOther::ReportOther(const RejectReasonKind K) : RejectReason(K) { 473 ++BadOtherForScop; 474 } 475 476 bool ReportOther::classof(const RejectReason *RR) { 477 return RR->getKind() >= rrkOther && RR->getKind() <= rrkLastOther; 478 } 479 480 //===----------------------------------------------------------------------===// 481 // ReportIntToPtr. 482 ReportIntToPtr::ReportIntToPtr(Instruction *BaseValue) 483 : ReportOther(rrkIntToPtr), BaseValue(BaseValue) {} 484 485 std::string ReportIntToPtr::getMessage() const { 486 return "Find bad intToptr prt: " + *BaseValue; 487 } 488 489 const DebugLoc &ReportIntToPtr::getDebugLoc() const { 490 return BaseValue->getDebugLoc(); 491 } 492 493 bool ReportIntToPtr::classof(const RejectReason *RR) { 494 return RR->getKind() == rrkIntToPtr; 495 } 496 497 //===----------------------------------------------------------------------===// 498 // ReportAlloca. 499 500 ReportAlloca::ReportAlloca(Instruction *Inst) 501 : ReportOther(rrkAlloca), Inst(Inst) {} 502 503 std::string ReportAlloca::getMessage() const { 504 return "Alloca instruction: " + *Inst; 505 } 506 507 const DebugLoc &ReportAlloca::getDebugLoc() const { 508 return Inst->getDebugLoc(); 509 } 510 511 bool ReportAlloca::classof(const RejectReason *RR) { 512 return RR->getKind() == rrkAlloca; 513 } 514 515 //===----------------------------------------------------------------------===// 516 // ReportUnknownInst. 517 518 ReportUnknownInst::ReportUnknownInst(Instruction *Inst) 519 : ReportOther(rrkUnknownInst), Inst(Inst) {} 520 521 std::string ReportUnknownInst::getMessage() const { 522 return "Unknown instruction: " + *Inst; 523 } 524 525 const DebugLoc &ReportUnknownInst::getDebugLoc() const { 526 return Inst->getDebugLoc(); 527 } 528 529 bool ReportUnknownInst::classof(const RejectReason *RR) { 530 return RR->getKind() == rrkUnknownInst; 531 } 532 533 //===----------------------------------------------------------------------===// 534 // ReportPHIinExit. 535 536 ReportPHIinExit::ReportPHIinExit(Instruction *Inst) 537 : ReportOther(rrkPHIinExit), Inst(Inst) {} 538 539 std::string ReportPHIinExit::getMessage() const { 540 return "PHI node in exit BB"; 541 } 542 543 const DebugLoc &ReportPHIinExit::getDebugLoc() const { 544 return Inst->getDebugLoc(); 545 } 546 547 bool ReportPHIinExit::classof(const RejectReason *RR) { 548 return RR->getKind() == rrkPHIinExit; 549 } 550 551 //===----------------------------------------------------------------------===// 552 // ReportEntry. 553 ReportEntry::ReportEntry(BasicBlock *BB) : ReportOther(rrkEntry), BB(BB) {} 554 555 std::string ReportEntry::getMessage() const { 556 return "Region containing entry block of function is invalid!"; 557 } 558 559 const DebugLoc &ReportEntry::getDebugLoc() const { 560 return BB->getTerminator()->getDebugLoc(); 561 } 562 563 bool ReportEntry::classof(const RejectReason *RR) { 564 return RR->getKind() == rrkEntry; 565 } 566 } // namespace polly 567