1 //===- PHITransAddr.cpp - PHI Translation for Addresses -------------------===// 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 // This file implements the PHITransAddr class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Analysis/PHITransAddr.h" 14 #include "llvm/Analysis/InstructionSimplify.h" 15 #include "llvm/Analysis/ValueTracking.h" 16 #include "llvm/Config/llvm-config.h" 17 #include "llvm/IR/Constants.h" 18 #include "llvm/IR/Dominators.h" 19 #include "llvm/IR/Instructions.h" 20 #include "llvm/Support/ErrorHandling.h" 21 #include "llvm/Support/raw_ostream.h" 22 using namespace llvm; 23 24 static cl::opt<bool> EnableAddPhiTranslation( 25 "gvn-add-phi-translation", cl::init(false), cl::Hidden, 26 cl::desc("Enable phi-translation of add instructions")); 27 28 static bool canPHITrans(Instruction *Inst) { 29 if (isa<PHINode>(Inst) || isa<GetElementPtrInst>(Inst) || isa<CastInst>(Inst)) 30 return true; 31 32 if (Inst->getOpcode() == Instruction::Add && 33 isa<ConstantInt>(Inst->getOperand(1))) 34 return true; 35 36 return false; 37 } 38 39 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 40 LLVM_DUMP_METHOD void PHITransAddr::dump() const { 41 if (!Addr) { 42 dbgs() << "PHITransAddr: null\n"; 43 return; 44 } 45 dbgs() << "PHITransAddr: " << *Addr << "\n"; 46 for (unsigned i = 0, e = InstInputs.size(); i != e; ++i) 47 dbgs() << " Input #" << i << " is " << *InstInputs[i] << "\n"; 48 } 49 #endif 50 51 static bool verifySubExpr(Value *Expr, 52 SmallVectorImpl<Instruction *> &InstInputs) { 53 // If this is a non-instruction value, there is nothing to do. 54 Instruction *I = dyn_cast<Instruction>(Expr); 55 if (!I) return true; 56 57 // If it's an instruction, it is either in Tmp or its operands recursively 58 // are. 59 SmallVectorImpl<Instruction *>::iterator Entry = find(InstInputs, I); 60 if (Entry != InstInputs.end()) { 61 InstInputs.erase(Entry); 62 return true; 63 } 64 65 // If it isn't in the InstInputs list it is a subexpr incorporated into the 66 // address. Validate that it is phi translatable. 67 if (!canPHITrans(I)) { 68 errs() << "Instruction in PHITransAddr is not phi-translatable:\n"; 69 errs() << *I << '\n'; 70 llvm_unreachable("Either something is missing from InstInputs or " 71 "canPHITrans is wrong."); 72 } 73 74 // Validate the operands of the instruction. 75 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 76 if (!verifySubExpr(I->getOperand(i), InstInputs)) 77 return false; 78 79 return true; 80 } 81 82 /// verify - Check internal consistency of this data structure. If the 83 /// structure is valid, it returns true. If invalid, it prints errors and 84 /// returns false. 85 bool PHITransAddr::verify() const { 86 if (!Addr) return true; 87 88 SmallVector<Instruction*, 8> Tmp(InstInputs.begin(), InstInputs.end()); 89 90 if (!verifySubExpr(Addr, Tmp)) 91 return false; 92 93 if (!Tmp.empty()) { 94 errs() << "PHITransAddr contains extra instructions:\n"; 95 for (unsigned i = 0, e = InstInputs.size(); i != e; ++i) 96 errs() << " InstInput #" << i << " is " << *InstInputs[i] << "\n"; 97 llvm_unreachable("This is unexpected."); 98 } 99 100 // a-ok. 101 return true; 102 } 103 104 /// isPotentiallyPHITranslatable - If this needs PHI translation, return true 105 /// if we have some hope of doing it. This should be used as a filter to 106 /// avoid calling PHITranslateValue in hopeless situations. 107 bool PHITransAddr::isPotentiallyPHITranslatable() const { 108 // If the input value is not an instruction, or if it is not defined in CurBB, 109 // then we don't need to phi translate it. 110 Instruction *Inst = dyn_cast<Instruction>(Addr); 111 return !Inst || canPHITrans(Inst); 112 } 113 114 static void RemoveInstInputs(Value *V, 115 SmallVectorImpl<Instruction*> &InstInputs) { 116 Instruction *I = dyn_cast<Instruction>(V); 117 if (!I) return; 118 119 // If the instruction is in the InstInputs list, remove it. 120 SmallVectorImpl<Instruction *>::iterator Entry = find(InstInputs, I); 121 if (Entry != InstInputs.end()) { 122 InstInputs.erase(Entry); 123 return; 124 } 125 126 assert(!isa<PHINode>(I) && "Error, removing something that isn't an input"); 127 128 // Otherwise, it must have instruction inputs itself. Zap them recursively. 129 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 130 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i))) 131 RemoveInstInputs(Op, InstInputs); 132 } 133 } 134 135 Value *PHITransAddr::translateSubExpr(Value *V, BasicBlock *CurBB, 136 BasicBlock *PredBB, 137 const DominatorTree *DT) { 138 // If this is a non-instruction value, it can't require PHI translation. 139 Instruction *Inst = dyn_cast<Instruction>(V); 140 if (!Inst) return V; 141 142 // Determine whether 'Inst' is an input to our PHI translatable expression. 143 bool isInput = is_contained(InstInputs, Inst); 144 145 // Handle inputs instructions if needed. 146 if (isInput) { 147 if (Inst->getParent() != CurBB) { 148 // If it is an input defined in a different block, then it remains an 149 // input. 150 return Inst; 151 } 152 153 // If 'Inst' is defined in this block and is an input that needs to be phi 154 // translated, we need to incorporate the value into the expression or fail. 155 156 // In either case, the instruction itself isn't an input any longer. 157 InstInputs.erase(find(InstInputs, Inst)); 158 159 // If this is a PHI, go ahead and translate it. 160 if (PHINode *PN = dyn_cast<PHINode>(Inst)) 161 return addAsInput(PN->getIncomingValueForBlock(PredBB)); 162 163 // If this is a non-phi value, and it is analyzable, we can incorporate it 164 // into the expression by making all instruction operands be inputs. 165 if (!canPHITrans(Inst)) 166 return nullptr; 167 168 // All instruction operands are now inputs (and of course, they may also be 169 // defined in this block, so they may need to be phi translated themselves. 170 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) 171 if (Instruction *Op = dyn_cast<Instruction>(Inst->getOperand(i))) 172 InstInputs.push_back(Op); 173 } 174 175 // Ok, it must be an intermediate result (either because it started that way 176 // or because we just incorporated it into the expression). See if its 177 // operands need to be phi translated, and if so, reconstruct it. 178 179 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) { 180 Value *PHIIn = translateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT); 181 if (!PHIIn) return nullptr; 182 if (PHIIn == Cast->getOperand(0)) 183 return Cast; 184 185 // Find an available version of this cast. 186 187 // Constants are trivial to find. 188 if (Constant *C = dyn_cast<Constant>(PHIIn)) 189 return addAsInput( 190 ConstantExpr::getCast(Cast->getOpcode(), C, Cast->getType())); 191 192 // Otherwise we have to see if a casted version of the incoming pointer 193 // is available. If so, we can use it, otherwise we have to fail. 194 for (User *U : PHIIn->users()) { 195 if (CastInst *CastI = dyn_cast<CastInst>(U)) 196 if (CastI->getOpcode() == Cast->getOpcode() && 197 CastI->getType() == Cast->getType() && 198 (!DT || DT->dominates(CastI->getParent(), PredBB))) 199 return CastI; 200 } 201 return nullptr; 202 } 203 204 // Handle getelementptr with at least one PHI translatable operand. 205 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { 206 SmallVector<Value*, 8> GEPOps; 207 bool AnyChanged = false; 208 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { 209 Value *GEPOp = translateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT); 210 if (!GEPOp) return nullptr; 211 212 AnyChanged |= GEPOp != GEP->getOperand(i); 213 GEPOps.push_back(GEPOp); 214 } 215 216 if (!AnyChanged) 217 return GEP; 218 219 // Simplify the GEP to handle 'gep x, 0' -> x etc. 220 if (Value *V = simplifyGEPInst(GEP->getSourceElementType(), GEPOps[0], 221 ArrayRef<Value *>(GEPOps).slice(1), 222 GEP->isInBounds(), {DL, TLI, DT, AC})) { 223 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i) 224 RemoveInstInputs(GEPOps[i], InstInputs); 225 226 return addAsInput(V); 227 } 228 229 // Scan to see if we have this GEP available. 230 Value *APHIOp = GEPOps[0]; 231 for (User *U : APHIOp->users()) { 232 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) 233 if (GEPI->getType() == GEP->getType() && 234 GEPI->getSourceElementType() == GEP->getSourceElementType() && 235 GEPI->getNumOperands() == GEPOps.size() && 236 GEPI->getParent()->getParent() == CurBB->getParent() && 237 (!DT || DT->dominates(GEPI->getParent(), PredBB))) { 238 if (std::equal(GEPOps.begin(), GEPOps.end(), GEPI->op_begin())) 239 return GEPI; 240 } 241 } 242 return nullptr; 243 } 244 245 // Handle add with a constant RHS. 246 if (Inst->getOpcode() == Instruction::Add && 247 isa<ConstantInt>(Inst->getOperand(1))) { 248 // PHI translate the LHS. 249 Constant *RHS = cast<ConstantInt>(Inst->getOperand(1)); 250 bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap(); 251 bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap(); 252 253 Value *LHS = translateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT); 254 if (!LHS) return nullptr; 255 256 // If the PHI translated LHS is an add of a constant, fold the immediates. 257 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS)) 258 if (BOp->getOpcode() == Instruction::Add) 259 if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) { 260 LHS = BOp->getOperand(0); 261 RHS = ConstantExpr::getAdd(RHS, CI); 262 isNSW = isNUW = false; 263 264 // If the old 'LHS' was an input, add the new 'LHS' as an input. 265 if (is_contained(InstInputs, BOp)) { 266 RemoveInstInputs(BOp, InstInputs); 267 addAsInput(LHS); 268 } 269 } 270 271 // See if the add simplifies away. 272 if (Value *Res = simplifyAddInst(LHS, RHS, isNSW, isNUW, {DL, TLI, DT, AC})) { 273 // If we simplified the operands, the LHS is no longer an input, but Res 274 // is. 275 RemoveInstInputs(LHS, InstInputs); 276 return addAsInput(Res); 277 } 278 279 // If we didn't modify the add, just return it. 280 if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1)) 281 return Inst; 282 283 // Otherwise, see if we have this add available somewhere. 284 for (User *U : LHS->users()) { 285 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) 286 if (BO->getOpcode() == Instruction::Add && 287 BO->getOperand(0) == LHS && BO->getOperand(1) == RHS && 288 BO->getParent()->getParent() == CurBB->getParent() && 289 (!DT || DT->dominates(BO->getParent(), PredBB))) 290 return BO; 291 } 292 293 return nullptr; 294 } 295 296 // Otherwise, we failed. 297 return nullptr; 298 } 299 300 /// PHITranslateValue - PHI translate the current address up the CFG from 301 /// CurBB to Pred, updating our state to reflect any needed changes. If 302 /// 'MustDominate' is true, the translated value must dominate PredBB. 303 Value *PHITransAddr::translateValue(BasicBlock *CurBB, BasicBlock *PredBB, 304 const DominatorTree *DT, 305 bool MustDominate) { 306 assert(DT || !MustDominate); 307 assert(verify() && "Invalid PHITransAddr!"); 308 if (DT && DT->isReachableFromEntry(PredBB)) 309 Addr = translateSubExpr(Addr, CurBB, PredBB, DT); 310 else 311 Addr = nullptr; 312 assert(verify() && "Invalid PHITransAddr!"); 313 314 if (MustDominate) 315 // Make sure the value is live in the predecessor. 316 if (Instruction *Inst = dyn_cast_or_null<Instruction>(Addr)) 317 if (!DT->dominates(Inst->getParent(), PredBB)) 318 Addr = nullptr; 319 320 return Addr; 321 } 322 323 /// PHITranslateWithInsertion - PHI translate this value into the specified 324 /// predecessor block, inserting a computation of the value if it is 325 /// unavailable. 326 /// 327 /// All newly created instructions are added to the NewInsts list. This 328 /// returns null on failure. 329 /// 330 Value * 331 PHITransAddr::translateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB, 332 const DominatorTree &DT, 333 SmallVectorImpl<Instruction *> &NewInsts) { 334 unsigned NISize = NewInsts.size(); 335 336 // Attempt to PHI translate with insertion. 337 Addr = insertTranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts); 338 339 // If successful, return the new value. 340 if (Addr) return Addr; 341 342 // If not, destroy any intermediate instructions inserted. 343 while (NewInsts.size() != NISize) 344 NewInsts.pop_back_val()->eraseFromParent(); 345 return nullptr; 346 } 347 348 /// insertTranslatedSubExpr - Insert a computation of the PHI translated 349 /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB 350 /// block. All newly created instructions are added to the NewInsts list. 351 /// This returns null on failure. 352 /// 353 Value *PHITransAddr::insertTranslatedSubExpr( 354 Value *InVal, BasicBlock *CurBB, BasicBlock *PredBB, 355 const DominatorTree &DT, SmallVectorImpl<Instruction *> &NewInsts) { 356 // See if we have a version of this value already available and dominating 357 // PredBB. If so, there is no need to insert a new instance of it. 358 PHITransAddr Tmp(InVal, DL, AC); 359 if (Value *Addr = 360 Tmp.translateValue(CurBB, PredBB, &DT, /*MustDominate=*/true)) 361 return Addr; 362 363 // We don't need to PHI translate values which aren't instructions. 364 auto *Inst = dyn_cast<Instruction>(InVal); 365 if (!Inst) 366 return nullptr; 367 368 // Handle cast of PHI translatable value. 369 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) { 370 Value *OpVal = insertTranslatedSubExpr(Cast->getOperand(0), CurBB, PredBB, 371 DT, NewInsts); 372 if (!OpVal) return nullptr; 373 374 // Otherwise insert a cast at the end of PredBB. 375 CastInst *New = CastInst::Create(Cast->getOpcode(), OpVal, InVal->getType(), 376 InVal->getName() + ".phi.trans.insert", 377 PredBB->getTerminator()); 378 New->setDebugLoc(Inst->getDebugLoc()); 379 NewInsts.push_back(New); 380 return New; 381 } 382 383 // Handle getelementptr with at least one PHI operand. 384 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { 385 SmallVector<Value*, 8> GEPOps; 386 BasicBlock *CurBB = GEP->getParent(); 387 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { 388 Value *OpVal = insertTranslatedSubExpr(GEP->getOperand(i), CurBB, PredBB, 389 DT, NewInsts); 390 if (!OpVal) return nullptr; 391 GEPOps.push_back(OpVal); 392 } 393 394 GetElementPtrInst *Result = GetElementPtrInst::Create( 395 GEP->getSourceElementType(), GEPOps[0], ArrayRef(GEPOps).slice(1), 396 InVal->getName() + ".phi.trans.insert", PredBB->getTerminator()); 397 Result->setDebugLoc(Inst->getDebugLoc()); 398 Result->setIsInBounds(GEP->isInBounds()); 399 NewInsts.push_back(Result); 400 return Result; 401 } 402 403 // Handle add with a constant RHS. 404 if (EnableAddPhiTranslation && Inst->getOpcode() == Instruction::Add && 405 isa<ConstantInt>(Inst->getOperand(1))) { 406 407 // FIXME: This code works, but it is unclear that we actually want to insert 408 // a big chain of computation in order to make a value available in a block. 409 // This needs to be evaluated carefully to consider its cost trade offs. 410 411 // PHI translate the LHS. 412 Value *OpVal = insertTranslatedSubExpr(Inst->getOperand(0), CurBB, PredBB, 413 DT, NewInsts); 414 if (OpVal == nullptr) 415 return nullptr; 416 417 BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1), 418 InVal->getName()+".phi.trans.insert", 419 PredBB->getTerminator()); 420 Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap()); 421 Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap()); 422 NewInsts.push_back(Res); 423 return Res; 424 } 425 426 return nullptr; 427 } 428