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