Chris Lattner | b63051c | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 1 | //===- PHITransAddr.cpp - PHI Translation for Addresses -------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | b63051c | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the PHITransAddr class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Analysis/PHITransAddr.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/InstructionSimplify.h" |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/ValueTracking.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 16 | #include "llvm/Config/llvm-config.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Instructions.h" |
David Greene | 04e7ae6 | 2009-12-23 21:06:14 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Dan Gohman | 0ab28b6 | 2010-11-18 17:05:57 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | b63051c | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 25 | static bool CanPHITrans(Instruction *Inst) { |
| 26 | if (isa<PHINode>(Inst) || |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 27 | isa<GetElementPtrInst>(Inst)) |
| 28 | return true; |
Dan Gohman | 2e1fc84 | 2010-11-18 17:05:13 +0000 | [diff] [blame] | 29 | |
| 30 | if (isa<CastInst>(Inst) && |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 31 | isSafeToSpeculativelyExecute(Inst)) |
Dan Gohman | 2e1fc84 | 2010-11-18 17:05:13 +0000 | [diff] [blame] | 32 | return true; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 6425a23 | 2009-12-08 06:06:26 +0000 | [diff] [blame] | 34 | if (Inst->getOpcode() == Instruction::Add && |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 35 | isa<ConstantInt>(Inst->getOperand(1))) |
| 36 | return true; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 38 | // cerr << "MEMDEP: Could not PHI translate: " << *Pointer; |
| 39 | // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst)) |
| 40 | // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0); |
| 41 | return false; |
| 42 | } |
| 43 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 44 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 45 | LLVM_DUMP_METHOD void PHITransAddr::dump() const { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 46 | if (!Addr) { |
David Greene | 04e7ae6 | 2009-12-23 21:06:14 +0000 | [diff] [blame] | 47 | dbgs() << "PHITransAddr: null\n"; |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 48 | return; |
| 49 | } |
David Greene | 04e7ae6 | 2009-12-23 21:06:14 +0000 | [diff] [blame] | 50 | dbgs() << "PHITransAddr: " << *Addr << "\n"; |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 51 | for (unsigned i = 0, e = InstInputs.size(); i != e; ++i) |
David Greene | 04e7ae6 | 2009-12-23 21:06:14 +0000 | [diff] [blame] | 52 | dbgs() << " Input #" << i << " is " << *InstInputs[i] << "\n"; |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 53 | } |
Manman Ren | c3366cc | 2012-09-06 19:55:56 +0000 | [diff] [blame] | 54 | #endif |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 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); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 61 | if (!I) return true; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 63 | // If it's an instruction, it is either in Tmp or its operands recursively |
| 64 | // are. |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 65 | SmallVectorImpl<Instruction *>::iterator Entry = find(InstInputs, I); |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 66 | if (Entry != InstInputs.end()) { |
| 67 | InstInputs.erase(Entry); |
| 68 | return true; |
| 69 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 71 | // If it isn't in the InstInputs list it is a subexpr incorporated into the |
| 72 | // address. Sanity check that it is phi translatable. |
| 73 | if (!CanPHITrans(I)) { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 74 | errs() << "Instruction in PHITransAddr is not phi-translatable:\n"; |
David Greene | f790593 | 2009-12-23 23:27:15 +0000 | [diff] [blame] | 75 | errs() << *I << '\n'; |
Dan Gohman | 0ab28b6 | 2010-11-18 17:05:57 +0000 | [diff] [blame] | 76 | llvm_unreachable("Either something is missing from InstInputs or " |
| 77 | "CanPHITrans is wrong."); |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 78 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 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 { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 92 | if (!Addr) return true; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 93 | |
| 94 | SmallVector<Instruction*, 8> Tmp(InstInputs.begin(), InstInputs.end()); |
| 95 | |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 96 | if (!VerifySubExpr(Addr, Tmp)) |
| 97 | return false; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 99 | if (!Tmp.empty()) { |
Dan Gohman | 0ab28b6 | 2010-11-18 17:05:57 +0000 | [diff] [blame] | 100 | errs() << "PHITransAddr contains extra instructions:\n"; |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 101 | for (unsigned i = 0, e = InstInputs.size(); i != e; ++i) |
David Greene | f790593 | 2009-12-23 23:27:15 +0000 | [diff] [blame] | 102 | errs() << " InstInput #" << i << " is " << *InstInputs[i] << "\n"; |
Dan Gohman | 0ab28b6 | 2010-11-18 17:05:57 +0000 | [diff] [blame] | 103 | llvm_unreachable("This is unexpected."); |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 104 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 106 | // a-ok. |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 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); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 118 | return !Inst || CanPHITrans(Inst); |
Chris Lattner | b63051c | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 121 | |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 122 | static void RemoveInstInputs(Value *V, |
Chris Lattner | 5cf4b74 | 2009-12-08 23:42:51 +0000 | [diff] [blame] | 123 | SmallVectorImpl<Instruction*> &InstInputs) { |
Chris Lattner | 11da6b0 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 124 | Instruction *I = dyn_cast<Instruction>(V); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 125 | if (!I) return; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 5cf4b74 | 2009-12-08 23:42:51 +0000 | [diff] [blame] | 127 | // If the instruction is in the InstInputs list, remove it. |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 128 | SmallVectorImpl<Instruction *>::iterator Entry = find(InstInputs, I); |
Chris Lattner | 5cf4b74 | 2009-12-08 23:42:51 +0000 | [diff] [blame] | 129 | if (Entry != InstInputs.end()) { |
| 130 | InstInputs.erase(Entry); |
| 131 | return; |
| 132 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 11da6b0 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 134 | assert(!isa<PHINode>(I) && "Error, removing something that isn't an input"); |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 11da6b0 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 5cf4b74 | 2009-12-08 23:42:51 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 143 | Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB, |
Daniel Dunbar | 693ea89 | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 144 | BasicBlock *PredBB, |
| 145 | const DominatorTree *DT) { |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 146 | // If this is a non-instruction value, it can't require PHI translation. |
| 147 | Instruction *Inst = dyn_cast<Instruction>(V); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 148 | if (!Inst) return V; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 149 | |
Chris Lattner | cfd7637 | 2009-12-09 00:10:55 +0000 | [diff] [blame] | 150 | // Determine whether 'Inst' is an input to our PHI translatable expression. |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 151 | bool isInput = is_contained(InstInputs, Inst); |
Chris Lattner | cfd7637 | 2009-12-09 00:10:55 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 37251f8 | 2009-12-09 00:18:13 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 11da6b0 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 164 | // In either case, the instruction itself isn't an input any longer. |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 165 | InstInputs.erase(find(InstInputs, Inst)); |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 167 | // If this is a PHI, go ahead and translate it. |
| 168 | if (PHINode *PN = dyn_cast<PHINode>(Inst)) |
Chris Lattner | 11da6b0 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 169 | return AddAsInput(PN->getIncomingValueForBlock(PredBB)); |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 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)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 174 | return nullptr; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 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); |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 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. |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 186 | |
Dan Gohman | 2e1fc84 | 2010-11-18 17:05:13 +0000 | [diff] [blame] | 187 | if (CastInst *Cast = dyn_cast<CastInst>(Inst)) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 188 | if (!isSafeToSpeculativelyExecute(Cast)) return nullptr; |
Dan Gohman | 2e1fc84 | 2010-11-18 17:05:13 +0000 | [diff] [blame] | 189 | Value *PHIIn = PHITranslateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 190 | if (!PHIIn) return nullptr; |
Dan Gohman | 2e1fc84 | 2010-11-18 17:05:13 +0000 | [diff] [blame] | 191 | if (PHIIn == Cast->getOperand(0)) |
| 192 | return Cast; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 194 | // Find an available version of this cast. |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 196 | // Constants are trivial to find. |
| 197 | if (Constant *C = dyn_cast<Constant>(PHIIn)) |
Dan Gohman | 2e1fc84 | 2010-11-18 17:05:13 +0000 | [diff] [blame] | 198 | return AddAsInput(ConstantExpr::getCast(Cast->getOpcode(), |
| 199 | C, Cast->getType())); |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 200 | |
Dan Gohman | 2e1fc84 | 2010-11-18 17:05:13 +0000 | [diff] [blame] | 201 | // Otherwise we have to see if a casted version of the incoming pointer |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 202 | // is available. If so, we can use it, otherwise we have to fail. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 203 | for (User *U : PHIIn->users()) { |
| 204 | if (CastInst *CastI = dyn_cast<CastInst>(U)) |
Dan Gohman | 2e1fc84 | 2010-11-18 17:05:13 +0000 | [diff] [blame] | 205 | if (CastI->getOpcode() == Cast->getOpcode() && |
| 206 | CastI->getType() == Cast->getType() && |
| 207 | (!DT || DT->dominates(CastI->getParent(), PredBB))) |
| 208 | return CastI; |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 209 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 210 | return nullptr; |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 211 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 213 | // Handle getelementptr with at least one PHI translatable operand. |
| 214 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { |
| 215 | SmallVector<Value*, 8> GEPOps; |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 216 | bool AnyChanged = false; |
| 217 | for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { |
Daniel Dunbar | 693ea89 | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 218 | Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 219 | if (!GEPOp) return nullptr; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 9e34d15 | 2009-12-07 19:52:57 +0000 | [diff] [blame] | 221 | AnyChanged |= GEPOp != GEP->getOperand(i); |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 222 | GEPOps.push_back(GEPOp); |
| 223 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 225 | if (!AnyChanged) |
| 226 | return GEP; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 228 | // Simplify the GEP to handle 'gep x, 0' -> x etc. |
Manuel Jacob | 20c6d5b | 2016-01-17 22:46:43 +0000 | [diff] [blame] | 229 | if (Value *V = SimplifyGEPInst(GEP->getSourceElementType(), |
Daniel Berlin | e6cb21a | 2017-04-26 20:56:13 +0000 | [diff] [blame] | 230 | GEPOps, {DL, TLI, DT, AC})) { |
Chris Lattner | 11da6b0 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 231 | for (unsigned i = 0, e = GEPOps.size(); i != e; ++i) |
| 232 | RemoveInstInputs(GEPOps[i], InstInputs); |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 11da6b0 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 234 | return AddAsInput(V); |
| 235 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 237 | // Scan to see if we have this GEP available. |
| 238 | Value *APHIOp = GEPOps[0]; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 239 | for (User *U : APHIOp->users()) { |
| 240 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 241 | if (GEPI->getType() == GEP->getType() && |
| 242 | GEPI->getNumOperands() == GEPOps.size() && |
Daniel Dunbar | 693ea89 | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 243 | GEPI->getParent()->getParent() == CurBB->getParent() && |
| 244 | (!DT || DT->dominates(GEPI->getParent(), PredBB))) { |
Benjamin Kramer | c1c3c84 | 2015-06-09 20:41:21 +0000 | [diff] [blame] | 245 | if (std::equal(GEPOps.begin(), GEPOps.end(), GEPI->op_begin())) |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 246 | return GEPI; |
| 247 | } |
| 248 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 249 | return nullptr; |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 250 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 252 | // Handle add with a constant RHS. |
| 253 | if (Inst->getOpcode() == Instruction::Add && |
| 254 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 255 | // PHI translate the LHS. |
| 256 | Constant *RHS = cast<ConstantInt>(Inst->getOperand(1)); |
| 257 | bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap(); |
| 258 | bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap(); |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 259 | |
Daniel Dunbar | 693ea89 | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 260 | Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 261 | if (!LHS) return nullptr; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 263 | // If the PHI translated LHS is an add of a constant, fold the immediates. |
| 264 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS)) |
| 265 | if (BOp->getOpcode() == Instruction::Add) |
| 266 | if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) { |
| 267 | LHS = BOp->getOperand(0); |
| 268 | RHS = ConstantExpr::getAdd(RHS, CI); |
| 269 | isNSW = isNUW = false; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 11da6b0 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 271 | // If the old 'LHS' was an input, add the new 'LHS' as an input. |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 272 | if (is_contained(InstInputs, BOp)) { |
Chris Lattner | 11da6b0 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 273 | RemoveInstInputs(BOp, InstInputs); |
| 274 | AddAsInput(LHS); |
| 275 | } |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 276 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 278 | // See if the add simplifies away. |
Daniel Berlin | e6cb21a | 2017-04-26 20:56:13 +0000 | [diff] [blame] | 279 | if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, {DL, TLI, DT, AC})) { |
Chris Lattner | 11da6b0 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 280 | // If we simplified the operands, the LHS is no longer an input, but Res |
| 281 | // is. |
| 282 | RemoveInstInputs(LHS, InstInputs); |
| 283 | return AddAsInput(Res); |
| 284 | } |
Chris Lattner | 9f9010e | 2009-12-09 17:27:45 +0000 | [diff] [blame] | 285 | |
| 286 | // If we didn't modify the add, just return it. |
| 287 | if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1)) |
| 288 | return Inst; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 290 | // Otherwise, see if we have this add available somewhere. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 291 | for (User *U : LHS->users()) { |
| 292 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) |
Chris Lattner | fa2e536 | 2009-12-09 17:18:49 +0000 | [diff] [blame] | 293 | if (BO->getOpcode() == Instruction::Add && |
| 294 | BO->getOperand(0) == LHS && BO->getOperand(1) == RHS && |
Daniel Dunbar | 693ea89 | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 295 | BO->getParent()->getParent() == CurBB->getParent() && |
| 296 | (!DT || DT->dominates(BO->getParent(), PredBB))) |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 297 | return BO; |
| 298 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 299 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 300 | return nullptr; |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 301 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 77b0d3b | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 303 | // Otherwise, we failed. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 304 | return nullptr; |
Chris Lattner | b63051c | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 307 | |
| 308 | /// PHITranslateValue - PHI translate the current address up the CFG from |
David Majnemer | 7666be7 | 2015-06-01 00:15:08 +0000 | [diff] [blame] | 309 | /// CurBB to Pred, updating our state to reflect any needed changes. If |
| 310 | /// 'MustDominate' is true, the translated value must dominate |
Daniel Dunbar | 693ea89 | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 311 | /// PredBB. This returns true on failure and sets Addr to null. |
| 312 | bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB, |
David Majnemer | 7666be7 | 2015-06-01 00:15:08 +0000 | [diff] [blame] | 313 | const DominatorTree *DT, |
| 314 | bool MustDominate) { |
| 315 | assert(DT || !MustDominate); |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 316 | assert(Verify() && "Invalid PHITransAddr!"); |
David Majnemer | 7666be7 | 2015-06-01 00:15:08 +0000 | [diff] [blame] | 317 | if (DT && DT->isReachableFromEntry(PredBB)) |
| 318 | Addr = |
| 319 | PHITranslateSubExpr(Addr, CurBB, PredBB, MustDominate ? DT : nullptr); |
| 320 | else |
| 321 | Addr = nullptr; |
Chris Lattner | 0aa7568 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 322 | assert(Verify() && "Invalid PHITransAddr!"); |
Daniel Dunbar | 693ea89 | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 323 | |
David Majnemer | 7666be7 | 2015-06-01 00:15:08 +0000 | [diff] [blame] | 324 | if (MustDominate) |
Daniel Dunbar | 693ea89 | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 325 | // Make sure the value is live in the predecessor. |
| 326 | if (Instruction *Inst = dyn_cast_or_null<Instruction>(Addr)) |
| 327 | if (!DT->dominates(Inst->getParent(), PredBB)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 328 | Addr = nullptr; |
Daniel Dunbar | 693ea89 | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 329 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 330 | return Addr == nullptr; |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 333 | /// PHITranslateWithInsertion - PHI translate this value into the specified |
| 334 | /// predecessor block, inserting a computation of the value if it is |
| 335 | /// unavailable. |
| 336 | /// |
| 337 | /// All newly created instructions are added to the NewInsts list. This |
| 338 | /// returns null on failure. |
| 339 | /// |
| 340 | Value *PHITransAddr:: |
| 341 | PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB, |
| 342 | const DominatorTree &DT, |
| 343 | SmallVectorImpl<Instruction*> &NewInsts) { |
| 344 | unsigned NISize = NewInsts.size(); |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 345 | |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 346 | // Attempt to PHI translate with insertion. |
| 347 | Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts); |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 348 | |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 349 | // If successful, return the new value. |
| 350 | if (Addr) return Addr; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 351 | |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 352 | // If not, destroy any intermediate instructions inserted. |
| 353 | while (NewInsts.size() != NISize) |
| 354 | NewInsts.pop_back_val()->eraseFromParent(); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 355 | return nullptr; |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
Chris Lattner | b63051c | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 359 | /// InsertPHITranslatedPointer - Insert a computation of the PHI translated |
| 360 | /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB |
| 361 | /// block. All newly created instructions are added to the NewInsts list. |
| 362 | /// This returns null on failure. |
| 363 | /// |
| 364 | Value *PHITransAddr:: |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 365 | InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB, |
| 366 | BasicBlock *PredBB, const DominatorTree &DT, |
| 367 | SmallVectorImpl<Instruction*> &NewInsts) { |
Chris Lattner | b63051c | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 368 | // See if we have a version of this value already available and dominating |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 369 | // PredBB. If so, there is no need to insert a new instance of it. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 370 | PHITransAddr Tmp(InVal, DL, AC); |
David Majnemer | 7666be7 | 2015-06-01 00:15:08 +0000 | [diff] [blame] | 371 | if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT, /*MustDominate=*/true)) |
Daniel Dunbar | 693ea89 | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 372 | return Tmp.getAddr(); |
Chris Lattner | b63051c | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 373 | |
David Majnemer | 4232fb3 | 2015-08-09 15:43:02 +0000 | [diff] [blame] | 374 | // We don't need to PHI translate values which aren't instructions. |
| 375 | auto *Inst = dyn_cast<Instruction>(InVal); |
| 376 | if (!Inst) |
| 377 | return nullptr; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 378 | |
Dan Gohman | 2e1fc84 | 2010-11-18 17:05:13 +0000 | [diff] [blame] | 379 | // Handle cast of PHI translatable value. |
| 380 | if (CastInst *Cast = dyn_cast<CastInst>(Inst)) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 381 | if (!isSafeToSpeculativelyExecute(Cast)) return nullptr; |
Dan Gohman | 2e1fc84 | 2010-11-18 17:05:13 +0000 | [diff] [blame] | 382 | Value *OpVal = InsertPHITranslatedSubExpr(Cast->getOperand(0), |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 383 | CurBB, PredBB, DT, NewInsts); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 384 | if (!OpVal) return nullptr; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 385 | |
Dan Gohman | 2e1fc84 | 2010-11-18 17:05:13 +0000 | [diff] [blame] | 386 | // Otherwise insert a cast at the end of PredBB. |
Alexey Samsonov | 89645df | 2015-06-10 17:37:38 +0000 | [diff] [blame] | 387 | CastInst *New = CastInst::Create(Cast->getOpcode(), OpVal, InVal->getType(), |
| 388 | InVal->getName() + ".phi.trans.insert", |
Dan Gohman | 2e1fc84 | 2010-11-18 17:05:13 +0000 | [diff] [blame] | 389 | PredBB->getTerminator()); |
Alexey Samsonov | 89645df | 2015-06-10 17:37:38 +0000 | [diff] [blame] | 390 | New->setDebugLoc(Inst->getDebugLoc()); |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 391 | NewInsts.push_back(New); |
| 392 | return New; |
| 393 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 394 | |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 395 | // Handle getelementptr with at least one PHI operand. |
| 396 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { |
| 397 | SmallVector<Value*, 8> GEPOps; |
| 398 | BasicBlock *CurBB = GEP->getParent(); |
| 399 | for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { |
| 400 | Value *OpVal = InsertPHITranslatedSubExpr(GEP->getOperand(i), |
| 401 | CurBB, PredBB, DT, NewInsts); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 402 | if (!OpVal) return nullptr; |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 403 | GEPOps.push_back(OpVal); |
| 404 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 405 | |
David Blaikie | 096b1da | 2015-03-14 19:53:33 +0000 | [diff] [blame] | 406 | GetElementPtrInst *Result = GetElementPtrInst::Create( |
| 407 | GEP->getSourceElementType(), GEPOps[0], makeArrayRef(GEPOps).slice(1), |
| 408 | InVal->getName() + ".phi.trans.insert", PredBB->getTerminator()); |
Alexey Samsonov | 89645df | 2015-06-10 17:37:38 +0000 | [diff] [blame] | 409 | Result->setDebugLoc(Inst->getDebugLoc()); |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 410 | Result->setIsInBounds(GEP->isInBounds()); |
| 411 | NewInsts.push_back(Result); |
| 412 | return Result; |
| 413 | } |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 414 | |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 415 | #if 0 |
| 416 | // FIXME: This code works, but it is unclear that we actually want to insert |
| 417 | // a big chain of computation in order to make a value available in a block. |
| 418 | // This needs to be evaluated carefully to consider its cost trade offs. |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 419 | |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 420 | // Handle add with a constant RHS. |
| 421 | if (Inst->getOpcode() == Instruction::Add && |
| 422 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 423 | // PHI translate the LHS. |
| 424 | Value *OpVal = InsertPHITranslatedSubExpr(Inst->getOperand(0), |
| 425 | CurBB, PredBB, DT, NewInsts); |
| 426 | if (OpVal == 0) return 0; |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 427 | |
Chris Lattner | e60244d | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 428 | BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1), |
| 429 | InVal->getName()+".phi.trans.insert", |
| 430 | PredBB->getTerminator()); |
| 431 | Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap()); |
| 432 | Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap()); |
| 433 | NewInsts.push_back(Res); |
| 434 | return Res; |
| 435 | } |
| 436 | #endif |
Dan Gohman | f1ebfc1 | 2010-11-18 17:06:31 +0000 | [diff] [blame] | 437 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 438 | return nullptr; |
Chris Lattner | b63051c | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 439 | } |