Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 10 | // This file implements all of the non-inline methods for the LLVM instruction |
| 11 | // classes. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Instructions.h" |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 17 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 18 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/DataLayout.h" |
| 21 | #include "llvm/IR/DerivedTypes.h" |
| 22 | #include "llvm/IR/Function.h" |
| 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/IR/Operator.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorHandling.h" |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MathExtras.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // CallSite Class |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 33 | User::op_iterator CallSite::getCallee() const { |
| 34 | Instruction *II(getInstruction()); |
| 35 | return isCall() |
Gabor Greif | 638c823 | 2010-08-05 21:25:49 +0000 | [diff] [blame] | 36 | ? cast<CallInst>(II)->op_end() - 1 // Skip Callee |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 37 | : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | // TerminatorInst Class |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | // Out of line virtual method, so the vtable, etc has a home. |
| 45 | TerminatorInst::~TerminatorInst() { |
| 46 | } |
| 47 | |
Reid Kleckner | 45a13e1 | 2017-05-11 21:26:55 +0000 | [diff] [blame] | 48 | unsigned TerminatorInst::getNumSuccessors() const { |
| 49 | switch (getOpcode()) { |
| 50 | #define HANDLE_TERM_INST(N, OPC, CLASS) \ |
| 51 | case Instruction::OPC: \ |
| 52 | return static_cast<const CLASS *>(this)->getNumSuccessorsV(); |
| 53 | #include "llvm/IR/Instruction.def" |
| 54 | default: |
| 55 | break; |
| 56 | } |
| 57 | llvm_unreachable("not a terminator"); |
| 58 | } |
| 59 | |
| 60 | BasicBlock *TerminatorInst::getSuccessor(unsigned idx) const { |
| 61 | switch (getOpcode()) { |
| 62 | #define HANDLE_TERM_INST(N, OPC, CLASS) \ |
| 63 | case Instruction::OPC: \ |
| 64 | return static_cast<const CLASS *>(this)->getSuccessorV(idx); |
| 65 | #include "llvm/IR/Instruction.def" |
| 66 | default: |
| 67 | break; |
| 68 | } |
| 69 | llvm_unreachable("not a terminator"); |
| 70 | } |
| 71 | |
| 72 | void TerminatorInst::setSuccessor(unsigned idx, BasicBlock *B) { |
| 73 | switch (getOpcode()) { |
| 74 | #define HANDLE_TERM_INST(N, OPC, CLASS) \ |
| 75 | case Instruction::OPC: \ |
| 76 | return static_cast<CLASS *>(this)->setSuccessorV(idx, B); |
| 77 | #include "llvm/IR/Instruction.def" |
| 78 | default: |
| 79 | break; |
| 80 | } |
| 81 | llvm_unreachable("not a terminator"); |
| 82 | } |
| 83 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 84 | //===----------------------------------------------------------------------===// |
| 85 | // UnaryInstruction Class |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 88 | // Out of line virtual method, so the vtable, etc has a home. |
| 89 | UnaryInstruction::~UnaryInstruction() { |
| 90 | } |
| 91 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 92 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 93 | // SelectInst Class |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | |
| 96 | /// areInvalidOperands - Return a string if the specified operands are invalid |
| 97 | /// for a select operation, otherwise return null. |
| 98 | const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { |
| 99 | if (Op1->getType() != Op2->getType()) |
| 100 | return "both values to select must have same type"; |
David Majnemer | b611e3f | 2015-08-14 05:09:07 +0000 | [diff] [blame] | 101 | |
| 102 | if (Op1->getType()->isTokenTy()) |
| 103 | return "select values cannot have token type"; |
| 104 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 105 | if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 106 | // Vector select. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 107 | if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 108 | return "vector select condition element type must be i1"; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 109 | VectorType *ET = dyn_cast<VectorType>(Op1->getType()); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 110 | if (!ET) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 111 | return "selected values for vector select must be vectors"; |
| 112 | if (ET->getNumElements() != VT->getNumElements()) |
| 113 | return "vector select requires selected vectors to have " |
| 114 | "the same vector length as select condition"; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 115 | } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 116 | return "select condition must be i1 or <n x i1>"; |
| 117 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 118 | return nullptr; |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
| 122 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 123 | // PHINode Class |
| 124 | //===----------------------------------------------------------------------===// |
| 125 | |
Pete Cooper | 1d07869 | 2015-12-14 20:29:16 +0000 | [diff] [blame] | 126 | void PHINode::anchor() {} |
| 127 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 128 | PHINode::PHINode(const PHINode &PN) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 129 | : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()), |
| 130 | ReservedSpace(PN.getNumOperands()) { |
| 131 | allocHungoffUses(PN.getNumOperands()); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 132 | std::copy(PN.op_begin(), PN.op_end(), op_begin()); |
| 133 | std::copy(PN.block_begin(), PN.block_end(), block_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 134 | SubclassOptionalData = PN.SubclassOptionalData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 137 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 138 | // predecessor basic block is deleted. |
| 139 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 140 | Value *Removed = getIncomingValue(Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 141 | |
| 142 | // Move everything after this operand down. |
| 143 | // |
| 144 | // FIXME: we could just swap with the end of the list, then erase. However, |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 145 | // clients might not expect this to happen. The code as it is thrashes the |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 146 | // use/def lists, which is kinda lame. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 147 | std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); |
| 148 | std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 149 | |
| 150 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 151 | Op<-1>().set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 152 | setNumHungOffUseOperands(getNumOperands() - 1); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 153 | |
| 154 | // If the PHI node is dead, because it has zero entries, nuke it now. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 155 | if (getNumOperands() == 0 && DeletePHIIfEmpty) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 156 | // If anyone is using this PHI, make them use a dummy value instead... |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 157 | replaceAllUsesWith(UndefValue::get(getType())); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 158 | eraseFromParent(); |
| 159 | } |
| 160 | return Removed; |
| 161 | } |
| 162 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 163 | /// growOperands - grow operands - This grows the operand list in response |
| 164 | /// to a push_back style of operation. This grows the number of ops by 1.5 |
| 165 | /// times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 166 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 167 | void PHINode::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 168 | unsigned e = getNumOperands(); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 169 | unsigned NumOps = e + e / 2; |
| 170 | if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. |
| 171 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 172 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 173 | growHungoffUses(ReservedSpace, /* IsPhi */ true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 176 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 177 | /// value, return the value, otherwise return null. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 178 | Value *PHINode::hasConstantValue() const { |
| 179 | // Exploit the fact that phi nodes always have at least one entry. |
| 180 | Value *ConstantValue = getIncomingValue(0); |
| 181 | for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) |
Nuno Lopes | 90c76df | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 182 | if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) { |
| 183 | if (ConstantValue != this) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 184 | return nullptr; // Incoming values not all the same. |
Nuno Lopes | 90c76df | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 185 | // The case where the first value is this PHI. |
| 186 | ConstantValue = getIncomingValue(i); |
| 187 | } |
Nuno Lopes | 0d44a50 | 2012-07-03 21:15:40 +0000 | [diff] [blame] | 188 | if (ConstantValue == this) |
| 189 | return UndefValue::get(getType()); |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 190 | return ConstantValue; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Nicolai Haehnle | 13d90f3 | 2016-04-14 17:42:47 +0000 | [diff] [blame] | 193 | /// hasConstantOrUndefValue - Whether the specified PHI node always merges |
| 194 | /// together the same value, assuming that undefs result in the same value as |
| 195 | /// non-undefs. |
| 196 | /// Unlike \ref hasConstantValue, this does not return a value because the |
| 197 | /// unique non-undef incoming value need not dominate the PHI node. |
| 198 | bool PHINode::hasConstantOrUndefValue() const { |
| 199 | Value *ConstantValue = nullptr; |
| 200 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) { |
| 201 | Value *Incoming = getIncomingValue(i); |
| 202 | if (Incoming != this && !isa<UndefValue>(Incoming)) { |
| 203 | if (ConstantValue && ConstantValue != Incoming) |
| 204 | return false; |
| 205 | ConstantValue = Incoming; |
| 206 | } |
| 207 | } |
| 208 | return true; |
| 209 | } |
| 210 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 211 | //===----------------------------------------------------------------------===// |
| 212 | // LandingPadInst Implementation |
| 213 | //===----------------------------------------------------------------------===// |
| 214 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 215 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 216 | const Twine &NameStr, Instruction *InsertBefore) |
| 217 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) { |
| 218 | init(NumReservedValues, NameStr); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 219 | } |
| 220 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 221 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 222 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 223 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) { |
| 224 | init(NumReservedValues, NameStr); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | LandingPadInst::LandingPadInst(const LandingPadInst &LP) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 228 | : Instruction(LP.getType(), Instruction::LandingPad, nullptr, |
| 229 | LP.getNumOperands()), |
| 230 | ReservedSpace(LP.getNumOperands()) { |
| 231 | allocHungoffUses(LP.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 232 | Use *OL = getOperandList(); |
| 233 | const Use *InOL = LP.getOperandList(); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 234 | for (unsigned I = 0, E = ReservedSpace; I != E; ++I) |
| 235 | OL[I] = InOL[I]; |
| 236 | |
| 237 | setCleanup(LP.isCleanup()); |
| 238 | } |
| 239 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 240 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 241 | const Twine &NameStr, |
| 242 | Instruction *InsertBefore) { |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 243 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 244 | } |
| 245 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 246 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 247 | const Twine &NameStr, |
| 248 | BasicBlock *InsertAtEnd) { |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 249 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 250 | } |
| 251 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 252 | void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 253 | ReservedSpace = NumReservedValues; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 254 | setNumHungOffUseOperands(0); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 255 | allocHungoffUses(ReservedSpace); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 256 | setName(NameStr); |
| 257 | setCleanup(false); |
| 258 | } |
| 259 | |
| 260 | /// growOperands - grow operands - This grows the operand list in response to a |
| 261 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 262 | void LandingPadInst::growOperands(unsigned Size) { |
| 263 | unsigned e = getNumOperands(); |
| 264 | if (ReservedSpace >= e + Size) return; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 265 | ReservedSpace = (std::max(e, 1U) + Size / 2) * 2; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 266 | growHungoffUses(ReservedSpace); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 269 | void LandingPadInst::addClause(Constant *Val) { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 270 | unsigned OpNo = getNumOperands(); |
| 271 | growOperands(1); |
| 272 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 273 | setNumHungOffUseOperands(getNumOperands() + 1); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 274 | getOperandList()[OpNo] = Val; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 275 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 276 | |
| 277 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 278 | // CallInst Implementation |
| 279 | //===----------------------------------------------------------------------===// |
| 280 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 281 | CallInst::~CallInst() { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 282 | } |
| 283 | |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 284 | void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 285 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) { |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 286 | this->FTy = FTy; |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 287 | assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 && |
| 288 | "NumOperands not set up?"); |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 289 | Op<-1>() = Func; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 290 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 291 | #ifndef NDEBUG |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 292 | assert((Args.size() == FTy->getNumParams() || |
| 293 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 294 | "Calling a function with bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 295 | |
| 296 | for (unsigned i = 0; i != Args.size(); ++i) |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 297 | assert((i >= FTy->getNumParams() || |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 298 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 299 | "Calling a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 300 | #endif |
| 301 | |
| 302 | std::copy(Args.begin(), Args.end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 303 | |
| 304 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 305 | (void)It; |
| 306 | assert(It + 1 == op_end() && "Should add up!"); |
| 307 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 308 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 311 | void CallInst::init(Value *Func, const Twine &NameStr) { |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 312 | FTy = |
| 313 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 314 | assert(getNumOperands() == 1 && "NumOperands not set up?"); |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 315 | Op<-1>() = Func; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 316 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 317 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 318 | |
| 319 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 322 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 323 | Instruction *InsertBefore) |
| 324 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 325 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 326 | Instruction::Call, |
| 327 | OperandTraits<CallInst>::op_end(this) - 1, |
| 328 | 1, InsertBefore) { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 329 | init(Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 332 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 333 | BasicBlock *InsertAtEnd) |
| 334 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 335 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 336 | Instruction::Call, |
| 337 | OperandTraits<CallInst>::op_end(this) - 1, |
| 338 | 1, InsertAtEnd) { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 339 | init(Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 342 | CallInst::CallInst(const CallInst &CI) |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 343 | : Instruction(CI.getType(), Instruction::Call, |
| 344 | OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(), |
| 345 | CI.getNumOperands()), |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 346 | Attrs(CI.Attrs), FTy(CI.FTy) { |
Reid Kleckner | 118e1bf | 2014-05-06 20:08:20 +0000 | [diff] [blame] | 347 | setTailCallKind(CI.getTailCallKind()); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 348 | setCallingConv(CI.getCallingConv()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 349 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 350 | std::copy(CI.op_begin(), CI.op_end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 351 | std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(), |
| 352 | bundle_op_info_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 353 | SubclassOptionalData = CI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 356 | CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB, |
| 357 | Instruction *InsertPt) { |
Sanjoy Das | ccd1456 | 2015-12-10 06:39:02 +0000 | [diff] [blame] | 358 | std::vector<Value *> Args(CI->arg_begin(), CI->arg_end()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 359 | |
| 360 | auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(), |
| 361 | InsertPt); |
| 362 | NewCI->setTailCallKind(CI->getTailCallKind()); |
| 363 | NewCI->setCallingConv(CI->getCallingConv()); |
| 364 | NewCI->SubclassOptionalData = CI->SubclassOptionalData; |
Sanjoy Das | b8dced5 | 2015-12-09 01:01:28 +0000 | [diff] [blame] | 365 | NewCI->setAttributes(CI->getAttributes()); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 366 | NewCI->setDebugLoc(CI->getDebugLoc()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 367 | return NewCI; |
| 368 | } |
| 369 | |
Hal Finkel | e87ad54 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 370 | Value *CallInst::getReturnedArgOperand() const { |
| 371 | unsigned Index; |
| 372 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 373 | if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index) |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 374 | return getArgOperand(Index - AttributeList::FirstArgIndex); |
Hal Finkel | e87ad54 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 375 | if (const Function *F = getCalledFunction()) |
| 376 | if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) && |
| 377 | Index) |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 378 | return getArgOperand(Index - AttributeList::FirstArgIndex); |
| 379 | |
Hal Finkel | e87ad54 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 380 | return nullptr; |
| 381 | } |
| 382 | |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 383 | void CallInst::addAttribute(unsigned i, Attribute::AttrKind Kind) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 384 | AttributeList PAL = getAttributes(); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 385 | PAL = PAL.addAttribute(getContext(), i, Kind); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 386 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Amaury Sechet | a65a237 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 389 | void CallInst::addAttribute(unsigned i, Attribute Attr) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 390 | AttributeList PAL = getAttributes(); |
Amaury Sechet | a65a237 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 391 | PAL = PAL.addAttribute(getContext(), i, Attr); |
| 392 | setAttributes(PAL); |
| 393 | } |
| 394 | |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 395 | void CallInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
| 396 | addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind); |
| 397 | } |
| 398 | |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 399 | void CallInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 400 | AttributeList PAL = getAttributes(); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 401 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
Amaury Sechet | 1a0e097 | 2016-04-21 21:29:10 +0000 | [diff] [blame] | 402 | setAttributes(PAL); |
| 403 | } |
| 404 | |
Amaury Sechet | 6100adf | 2016-06-15 17:50:39 +0000 | [diff] [blame] | 405 | void CallInst::removeAttribute(unsigned i, StringRef Kind) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 406 | AttributeList PAL = getAttributes(); |
Amaury Sechet | 6100adf | 2016-06-15 17:50:39 +0000 | [diff] [blame] | 407 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
| 408 | setAttributes(PAL); |
| 409 | } |
| 410 | |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 411 | void CallInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
| 412 | removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind); |
| 413 | } |
| 414 | |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 415 | void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 416 | AttributeList PAL = getAttributes(); |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 417 | PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); |
| 418 | setAttributes(PAL); |
| 419 | } |
| 420 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 421 | void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 422 | AttributeList PAL = getAttributes(); |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 423 | PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); |
| 424 | setAttributes(PAL); |
| 425 | } |
| 426 | |
Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 427 | bool CallInst::hasRetAttr(Attribute::AttrKind Kind) const { |
| 428 | if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind)) |
| 429 | return true; |
Sanjoy Das | b11b440 | 2015-11-04 20:33:45 +0000 | [diff] [blame] | 430 | |
Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 431 | // Look at the callee, if available. |
| 432 | if (const Function *F = getCalledFunction()) |
| 433 | return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind); |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const { |
| 438 | assert(i < getNumArgOperands() && "Param index out of bounds!"); |
| 439 | |
| 440 | if (Attrs.hasParamAttribute(i, Kind)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 441 | return true; |
| 442 | if (const Function *F = getCalledFunction()) |
Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 443 | return F->getAttributes().hasParamAttribute(i, Kind); |
Bill Wendling | daf8e38 | 2012-10-04 07:18:12 +0000 | [diff] [blame] | 444 | return false; |
| 445 | } |
| 446 | |
Sanjoy Das | a4bae3b | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 447 | bool CallInst::dataOperandHasImpliedAttr(unsigned i, |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 448 | Attribute::AttrKind Kind) const { |
Sanjoy Das | 776e4a7 | 2015-11-05 01:53:26 +0000 | [diff] [blame] | 449 | // There are getNumOperands() - 1 data operands. The last operand is the |
| 450 | // callee. |
| 451 | assert(i < getNumOperands() && "Data operand index out of bounds!"); |
| 452 | |
Sanjoy Das | a4bae3b | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 453 | // The attribute A can either be directly specified, if the operand in |
| 454 | // question is a call argument; or be indirectly implied by the kind of its |
| 455 | // containing operand bundle, if the operand is a bundle operand. |
| 456 | |
Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 457 | // FIXME: Avoid these i - 1 calculations and update the API to use zero-based |
| 458 | // indices. |
Sanjoy Das | a4bae3b | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 459 | if (i < (getNumArgOperands() + 1)) |
Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 460 | return paramHasAttr(i - 1, Kind); |
Sanjoy Das | a4bae3b | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 461 | |
| 462 | assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) && |
| 463 | "Must be either a call argument or an operand bundle!"); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 464 | return bundleOperandHasAttr(i - 1, Kind); |
Sanjoy Das | a4bae3b | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 467 | /// IsConstantOne - Return true only if val is constant int 1 |
| 468 | static bool IsConstantOne(Value *val) { |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 469 | assert(val && "IsConstantOne does not work with nullptr val"); |
Matt Arsenault | 6941785 | 2014-09-15 17:56:51 +0000 | [diff] [blame] | 470 | const ConstantInt *CVal = dyn_cast<ConstantInt>(val); |
| 471 | return CVal && CVal->isOne(); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 474 | static Instruction *createMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 475 | BasicBlock *InsertAtEnd, Type *IntPtrTy, |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 476 | Type *AllocTy, Value *AllocSize, |
| 477 | Value *ArraySize, |
| 478 | ArrayRef<OperandBundleDef> OpB, |
| 479 | Function *MallocF, const Twine &Name) { |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 480 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 481 | "createMalloc needs either InsertBefore or InsertAtEnd"); |
| 482 | |
| 483 | // malloc(type) becomes: |
| 484 | // bitcast (i8* malloc(typeSize)) to type* |
| 485 | // malloc(type, arraySize) becomes: |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 486 | // bitcast (i8* malloc(typeSize*arraySize)) to type* |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 487 | if (!ArraySize) |
| 488 | ArraySize = ConstantInt::get(IntPtrTy, 1); |
| 489 | else if (ArraySize->getType() != IntPtrTy) { |
| 490 | if (InsertBefore) |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 491 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 492 | "", InsertBefore); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 493 | else |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 494 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 495 | "", InsertAtEnd); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 496 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 497 | |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 498 | if (!IsConstantOne(ArraySize)) { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 499 | if (IsConstantOne(AllocSize)) { |
| 500 | AllocSize = ArraySize; // Operand * 1 = Operand |
| 501 | } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { |
| 502 | Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, |
| 503 | false /*ZExt*/); |
| 504 | // Malloc arg is constant product of type size and array size |
| 505 | AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); |
| 506 | } else { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 507 | // Multiply type size by the array size... |
| 508 | if (InsertBefore) |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 509 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 510 | "mallocsize", InsertBefore); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 511 | else |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 512 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 513 | "mallocsize", InsertAtEnd); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 514 | } |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 515 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 516 | |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 517 | assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size"); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 518 | // Create the call to Malloc. |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 519 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 520 | Module *M = BB->getParent()->getParent(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 521 | Type *BPTy = Type::getInt8PtrTy(BB->getContext()); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 522 | Value *MallocFunc = MallocF; |
| 523 | if (!MallocFunc) |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 524 | // prototype malloc as "void *malloc(size_t)" |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 525 | MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 526 | PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 527 | CallInst *MCall = nullptr; |
| 528 | Instruction *Result = nullptr; |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 529 | if (InsertBefore) { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 530 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall", |
| 531 | InsertBefore); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 532 | Result = MCall; |
| 533 | if (Result->getType() != AllocPtrType) |
| 534 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 535 | Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 536 | } else { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 537 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall"); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 538 | Result = MCall; |
| 539 | if (Result->getType() != AllocPtrType) { |
| 540 | InsertAtEnd->getInstList().push_back(MCall); |
| 541 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 542 | Result = new BitCastInst(MCall, AllocPtrType, Name); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 543 | } |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 544 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 545 | MCall->setTailCall(); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 546 | if (Function *F = dyn_cast<Function>(MallocFunc)) { |
| 547 | MCall->setCallingConv(F->getCallingConv()); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 548 | if (!F->returnDoesNotAlias()) |
| 549 | F->setReturnDoesNotAlias(); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 550 | } |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 551 | assert(!MCall->getType()->isVoidTy() && "Malloc has void return type"); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 552 | |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 553 | return Result; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 557 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 558 | /// possibly multiplied by the array size if the array size is not |
| 559 | /// constant 1. |
| 560 | /// 2. Call malloc with that argument. |
| 561 | /// 3. Bitcast the result of the malloc call to the specified type. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 562 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 563 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 564 | Value *AllocSize, Value *ArraySize, |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 565 | Function *MallocF, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 566 | const Twine &Name) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 567 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 568 | ArraySize, None, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 569 | } |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 570 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
| 571 | Type *IntPtrTy, Type *AllocTy, |
| 572 | Value *AllocSize, Value *ArraySize, |
| 573 | ArrayRef<OperandBundleDef> OpB, |
| 574 | Function *MallocF, |
| 575 | const Twine &Name) { |
| 576 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, |
| 577 | ArraySize, OpB, MallocF, Name); |
| 578 | } |
| 579 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 580 | |
| 581 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 582 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 583 | /// possibly multiplied by the array size if the array size is not |
| 584 | /// constant 1. |
| 585 | /// 2. Call malloc with that argument. |
| 586 | /// 3. Bitcast the result of the malloc call to the specified type. |
| 587 | /// Note: This function does not add the bitcast to the basic block, that is the |
| 588 | /// responsibility of the caller. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 589 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 590 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 591 | Value *AllocSize, Value *ArraySize, |
| 592 | Function *MallocF, const Twine &Name) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 593 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 594 | ArraySize, None, MallocF, Name); |
| 595 | } |
| 596 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
| 597 | Type *IntPtrTy, Type *AllocTy, |
| 598 | Value *AllocSize, Value *ArraySize, |
| 599 | ArrayRef<OperandBundleDef> OpB, |
| 600 | Function *MallocF, const Twine &Name) { |
| 601 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
| 602 | ArraySize, OpB, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 603 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 604 | |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 605 | static Instruction *createFree(Value *Source, |
| 606 | ArrayRef<OperandBundleDef> Bundles, |
| 607 | Instruction *InsertBefore, |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 608 | BasicBlock *InsertAtEnd) { |
| 609 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
| 610 | "createFree needs either InsertBefore or InsertAtEnd"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 611 | assert(Source->getType()->isPointerTy() && |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 612 | "Can not free something of nonpointer type!"); |
| 613 | |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 614 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 615 | Module *M = BB->getParent()->getParent(); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 616 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 617 | Type *VoidTy = Type::getVoidTy(M->getContext()); |
| 618 | Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 619 | // prototype free as "void free(void*)" |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 620 | Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy); |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 621 | CallInst *Result = nullptr; |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 622 | Value *PtrCast = Source; |
| 623 | if (InsertBefore) { |
| 624 | if (Source->getType() != IntPtrTy) |
| 625 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 626 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 627 | } else { |
| 628 | if (Source->getType() != IntPtrTy) |
| 629 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 630 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, ""); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 631 | } |
| 632 | Result->setTailCall(); |
Chris Lattner | 2156c22 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 633 | if (Function *F = dyn_cast<Function>(FreeFunc)) |
| 634 | Result->setCallingConv(F->getCallingConv()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 635 | |
| 636 | return Result; |
| 637 | } |
| 638 | |
| 639 | /// CreateFree - Generate the IR for a call to the builtin free function. |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 640 | Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 641 | return createFree(Source, None, InsertBefore, nullptr); |
| 642 | } |
| 643 | Instruction *CallInst::CreateFree(Value *Source, |
| 644 | ArrayRef<OperandBundleDef> Bundles, |
| 645 | Instruction *InsertBefore) { |
| 646 | return createFree(Source, Bundles, InsertBefore, nullptr); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | /// CreateFree - Generate the IR for a call to the builtin free function. |
| 650 | /// Note: This function does not add the call to the basic block, that is the |
| 651 | /// responsibility of the caller. |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 652 | Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 653 | Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd); |
| 654 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 655 | return FreeCall; |
| 656 | } |
| 657 | Instruction *CallInst::CreateFree(Value *Source, |
| 658 | ArrayRef<OperandBundleDef> Bundles, |
| 659 | BasicBlock *InsertAtEnd) { |
| 660 | Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 661 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 662 | return FreeCall; |
| 663 | } |
| 664 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 665 | //===----------------------------------------------------------------------===// |
| 666 | // InvokeInst Implementation |
| 667 | //===----------------------------------------------------------------------===// |
| 668 | |
David Blaikie | 3e80709 | 2015-05-13 18:35:26 +0000 | [diff] [blame] | 669 | void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal, |
| 670 | BasicBlock *IfException, ArrayRef<Value *> Args, |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 671 | ArrayRef<OperandBundleDef> Bundles, |
David Blaikie | 3e80709 | 2015-05-13 18:35:26 +0000 | [diff] [blame] | 672 | const Twine &NameStr) { |
| 673 | this->FTy = FTy; |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 674 | |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 675 | assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) && |
| 676 | "NumOperands not set up?"); |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 677 | Op<-3>() = Fn; |
| 678 | Op<-2>() = IfNormal; |
| 679 | Op<-1>() = IfException; |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 680 | |
| 681 | #ifndef NDEBUG |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 682 | assert(((Args.size() == FTy->getNumParams()) || |
| 683 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Gabor Greif | 668d700 | 2010-03-23 13:45:54 +0000 | [diff] [blame] | 684 | "Invoking a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 685 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 686 | for (unsigned i = 0, e = Args.size(); i != e; i++) |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 687 | assert((i >= FTy->getNumParams() || |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 688 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 689 | "Invoking a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 690 | #endif |
| 691 | |
| 692 | std::copy(Args.begin(), Args.end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 693 | |
| 694 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 695 | (void)It; |
| 696 | assert(It + 3 == op_end() && "Should add up!"); |
| 697 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 698 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 701 | InvokeInst::InvokeInst(const InvokeInst &II) |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 702 | : TerminatorInst(II.getType(), Instruction::Invoke, |
| 703 | OperandTraits<InvokeInst>::op_end(this) - |
| 704 | II.getNumOperands(), |
| 705 | II.getNumOperands()), |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 706 | Attrs(II.Attrs), FTy(II.FTy) { |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 707 | setCallingConv(II.getCallingConv()); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 708 | std::copy(II.op_begin(), II.op_end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 709 | std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(), |
| 710 | bundle_op_info_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 711 | SubclassOptionalData = II.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 714 | InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB, |
| 715 | Instruction *InsertPt) { |
Sanjoy Das | ccd1456 | 2015-12-10 06:39:02 +0000 | [diff] [blame] | 716 | std::vector<Value *> Args(II->arg_begin(), II->arg_end()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 717 | |
| 718 | auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(), |
| 719 | II->getUnwindDest(), Args, OpB, |
| 720 | II->getName(), InsertPt); |
| 721 | NewII->setCallingConv(II->getCallingConv()); |
| 722 | NewII->SubclassOptionalData = II->SubclassOptionalData; |
Sanjoy Das | b8dced5 | 2015-12-09 01:01:28 +0000 | [diff] [blame] | 723 | NewII->setAttributes(II->getAttributes()); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 724 | NewII->setDebugLoc(II->getDebugLoc()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 725 | return NewII; |
| 726 | } |
| 727 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 728 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 729 | return getSuccessor(idx); |
| 730 | } |
| 731 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 732 | return getNumSuccessors(); |
| 733 | } |
| 734 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 735 | return setSuccessor(idx, B); |
| 736 | } |
| 737 | |
Hal Finkel | e87ad54 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 738 | Value *InvokeInst::getReturnedArgOperand() const { |
| 739 | unsigned Index; |
| 740 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 741 | if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index) |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 742 | return getArgOperand(Index - AttributeList::FirstArgIndex); |
Hal Finkel | e87ad54 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 743 | if (const Function *F = getCalledFunction()) |
| 744 | if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) && |
| 745 | Index) |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 746 | return getArgOperand(Index - AttributeList::FirstArgIndex); |
| 747 | |
Hal Finkel | e87ad54 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 748 | return nullptr; |
| 749 | } |
| 750 | |
Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 751 | bool InvokeInst::hasRetAttr(Attribute::AttrKind Kind) const { |
| 752 | if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind)) |
| 753 | return true; |
Sanjoy Das | b11b440 | 2015-11-04 20:33:45 +0000 | [diff] [blame] | 754 | |
Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 755 | // Look at the callee, if available. |
| 756 | if (const Function *F = getCalledFunction()) |
| 757 | return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind); |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const { |
| 762 | assert(i < getNumArgOperands() && "Param index out of bounds!"); |
| 763 | |
| 764 | if (Attrs.hasParamAttribute(i, Kind)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 765 | return true; |
| 766 | if (const Function *F = getCalledFunction()) |
Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 767 | return F->getAttributes().hasParamAttribute(i, Kind); |
Bill Wendling | daf8e38 | 2012-10-04 07:18:12 +0000 | [diff] [blame] | 768 | return false; |
| 769 | } |
| 770 | |
Sanjoy Das | a4bae3b | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 771 | bool InvokeInst::dataOperandHasImpliedAttr(unsigned i, |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 772 | Attribute::AttrKind Kind) const { |
Sanjoy Das | 776e4a7 | 2015-11-05 01:53:26 +0000 | [diff] [blame] | 773 | // There are getNumOperands() - 3 data operands. The last three operands are |
| 774 | // the callee and the two successor basic blocks. |
| 775 | assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!"); |
| 776 | |
Sanjoy Das | a4bae3b | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 777 | // The attribute A can either be directly specified, if the operand in |
| 778 | // question is an invoke argument; or be indirectly implied by the kind of its |
| 779 | // containing operand bundle, if the operand is a bundle operand. |
| 780 | |
Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 781 | // FIXME: Avoid these i - 1 calculations and update the API to use zero-based |
| 782 | // indices. |
Sanjoy Das | a4bae3b | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 783 | if (i < (getNumArgOperands() + 1)) |
Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 784 | return paramHasAttr(i - 1, Kind); |
Sanjoy Das | a4bae3b | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 785 | |
| 786 | assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) && |
| 787 | "Must be either an invoke argument or an operand bundle!"); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 788 | return bundleOperandHasAttr(i - 1, Kind); |
Sanjoy Das | a4bae3b | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 791 | void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind Kind) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 792 | AttributeList PAL = getAttributes(); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 793 | PAL = PAL.addAttribute(getContext(), i, Kind); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 794 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Amaury Sechet | a65a237 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 797 | void InvokeInst::addAttribute(unsigned i, Attribute Attr) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 798 | AttributeList PAL = getAttributes(); |
Amaury Sechet | a65a237 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 799 | PAL = PAL.addAttribute(getContext(), i, Attr); |
| 800 | setAttributes(PAL); |
| 801 | } |
| 802 | |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 803 | void InvokeInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
| 804 | addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind); |
| 805 | } |
| 806 | |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 807 | void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 808 | AttributeList PAL = getAttributes(); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 809 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
Amaury Sechet | 1a0e097 | 2016-04-21 21:29:10 +0000 | [diff] [blame] | 810 | setAttributes(PAL); |
| 811 | } |
| 812 | |
Amaury Sechet | 6100adf | 2016-06-15 17:50:39 +0000 | [diff] [blame] | 813 | void InvokeInst::removeAttribute(unsigned i, StringRef Kind) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 814 | AttributeList PAL = getAttributes(); |
Amaury Sechet | 6100adf | 2016-06-15 17:50:39 +0000 | [diff] [blame] | 815 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
| 816 | setAttributes(PAL); |
| 817 | } |
| 818 | |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 819 | void InvokeInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
| 820 | removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind); |
| 821 | } |
| 822 | |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 823 | void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 824 | AttributeList PAL = getAttributes(); |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 825 | PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); |
| 826 | setAttributes(PAL); |
| 827 | } |
| 828 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 829 | void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 830 | AttributeList PAL = getAttributes(); |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 831 | PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); |
| 832 | setAttributes(PAL); |
| 833 | } |
| 834 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 835 | LandingPadInst *InvokeInst::getLandingPadInst() const { |
| 836 | return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); |
| 837 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 838 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 839 | //===----------------------------------------------------------------------===// |
| 840 | // ReturnInst Implementation |
| 841 | //===----------------------------------------------------------------------===// |
| 842 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 843 | ReturnInst::ReturnInst(const ReturnInst &RI) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 844 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 845 | OperandTraits<ReturnInst>::op_end(this) - |
| 846 | RI.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 847 | RI.getNumOperands()) { |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 848 | if (RI.getNumOperands()) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 849 | Op<0>() = RI.Op<0>(); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 850 | SubclassOptionalData = RI.SubclassOptionalData; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 853 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) |
| 854 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 855 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 856 | InsertBefore) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 857 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 858 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 859 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 860 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) |
| 861 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 862 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 863 | InsertAtEnd) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 864 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 865 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 866 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 867 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 868 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 869 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 872 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 873 | return getNumSuccessors(); |
| 874 | } |
| 875 | |
Devang Patel | ae682fb | 2008-02-26 17:56:20 +0000 | [diff] [blame] | 876 | /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 877 | /// emit the vtable for the class in this translation unit. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 878 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 879 | llvm_unreachable("ReturnInst has no successors!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 882 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 883 | llvm_unreachable("ReturnInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 886 | ReturnInst::~ReturnInst() { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 887 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 888 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 889 | //===----------------------------------------------------------------------===// |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 890 | // ResumeInst Implementation |
| 891 | //===----------------------------------------------------------------------===// |
| 892 | |
| 893 | ResumeInst::ResumeInst(const ResumeInst &RI) |
| 894 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume, |
| 895 | OperandTraits<ResumeInst>::op_begin(this), 1) { |
| 896 | Op<0>() = RI.Op<0>(); |
| 897 | } |
| 898 | |
| 899 | ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) |
| 900 | : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 901 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { |
| 902 | Op<0>() = Exn; |
| 903 | } |
| 904 | |
| 905 | ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) |
| 906 | : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 907 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { |
| 908 | Op<0>() = Exn; |
| 909 | } |
| 910 | |
| 911 | unsigned ResumeInst::getNumSuccessorsV() const { |
| 912 | return getNumSuccessors(); |
| 913 | } |
| 914 | |
| 915 | void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
| 916 | llvm_unreachable("ResumeInst has no successors!"); |
| 917 | } |
| 918 | |
| 919 | BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const { |
| 920 | llvm_unreachable("ResumeInst has no successors!"); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | //===----------------------------------------------------------------------===// |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 924 | // CleanupReturnInst Implementation |
| 925 | //===----------------------------------------------------------------------===// |
| 926 | |
| 927 | CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI) |
| 928 | : TerminatorInst(CRI.getType(), Instruction::CleanupRet, |
| 929 | OperandTraits<CleanupReturnInst>::op_end(this) - |
| 930 | CRI.getNumOperands(), |
| 931 | CRI.getNumOperands()) { |
David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 932 | setInstructionSubclassData(CRI.getSubclassDataFromInstruction()); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 933 | Op<0>() = CRI.Op<0>(); |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 934 | if (CRI.hasUnwindDest()) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 935 | Op<1>() = CRI.Op<1>(); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 936 | } |
| 937 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 938 | void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 939 | if (UnwindBB) |
| 940 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 941 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 942 | Op<0>() = CleanupPad; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 943 | if (UnwindBB) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 944 | Op<1>() = UnwindBB; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 945 | } |
| 946 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 947 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, |
| 948 | unsigned Values, Instruction *InsertBefore) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 949 | : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()), |
| 950 | Instruction::CleanupRet, |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 951 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, |
| 952 | Values, InsertBefore) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 953 | init(CleanupPad, UnwindBB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 954 | } |
| 955 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 956 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, |
| 957 | unsigned Values, BasicBlock *InsertAtEnd) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 958 | : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()), |
| 959 | Instruction::CleanupRet, |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 960 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, |
| 961 | Values, InsertAtEnd) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 962 | init(CleanupPad, UnwindBB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const { |
| 966 | assert(Idx == 0); |
| 967 | return getUnwindDest(); |
| 968 | } |
| 969 | unsigned CleanupReturnInst::getNumSuccessorsV() const { |
| 970 | return getNumSuccessors(); |
| 971 | } |
| 972 | void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) { |
| 973 | assert(Idx == 0); |
| 974 | setUnwindDest(B); |
| 975 | } |
| 976 | |
| 977 | //===----------------------------------------------------------------------===// |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 978 | // CatchReturnInst Implementation |
| 979 | //===----------------------------------------------------------------------===// |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 980 | void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 981 | Op<0>() = CatchPad; |
| 982 | Op<1>() = BB; |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 983 | } |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 984 | |
| 985 | CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI) |
| 986 | : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet, |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 987 | OperandTraits<CatchReturnInst>::op_begin(this), 2) { |
| 988 | Op<0>() = CRI.Op<0>(); |
| 989 | Op<1>() = CRI.Op<1>(); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 990 | } |
| 991 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 992 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 993 | Instruction *InsertBefore) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 994 | : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 995 | OperandTraits<CatchReturnInst>::op_begin(this), 2, |
| 996 | InsertBefore) { |
| 997 | init(CatchPad, BB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 998 | } |
| 999 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1000 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 1001 | BasicBlock *InsertAtEnd) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1002 | : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 1003 | OperandTraits<CatchReturnInst>::op_begin(this), 2, |
| 1004 | InsertAtEnd) { |
| 1005 | init(CatchPad, BB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const { |
David Majnemer | b01aa9f | 2015-08-23 19:22:31 +0000 | [diff] [blame] | 1009 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!"); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1010 | return getSuccessor(); |
| 1011 | } |
| 1012 | unsigned CatchReturnInst::getNumSuccessorsV() const { |
| 1013 | return getNumSuccessors(); |
| 1014 | } |
| 1015 | void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) { |
David Majnemer | b01aa9f | 2015-08-23 19:22:31 +0000 | [diff] [blame] | 1016 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!"); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1017 | setSuccessor(B); |
| 1018 | } |
| 1019 | |
| 1020 | //===----------------------------------------------------------------------===// |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1021 | // CatchSwitchInst Implementation |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1022 | //===----------------------------------------------------------------------===// |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1023 | |
| 1024 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
| 1025 | unsigned NumReservedValues, |
| 1026 | const Twine &NameStr, |
| 1027 | Instruction *InsertBefore) |
| 1028 | : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, |
| 1029 | InsertBefore) { |
| 1030 | if (UnwindDest) |
| 1031 | ++NumReservedValues; |
| 1032 | init(ParentPad, UnwindDest, NumReservedValues + 1); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1033 | setName(NameStr); |
| 1034 | } |
| 1035 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1036 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
| 1037 | unsigned NumReservedValues, |
| 1038 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 1039 | : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, |
David Majnemer | 5c73c94 | 2015-08-13 22:11:40 +0000 | [diff] [blame] | 1040 | InsertAtEnd) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1041 | if (UnwindDest) |
| 1042 | ++NumReservedValues; |
| 1043 | init(ParentPad, UnwindDest, NumReservedValues + 1); |
| 1044 | setName(NameStr); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1047 | CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI) |
| 1048 | : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr, |
| 1049 | CSI.getNumOperands()) { |
| 1050 | init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands()); |
| 1051 | setNumHungOffUseOperands(ReservedSpace); |
| 1052 | Use *OL = getOperandList(); |
| 1053 | const Use *InOL = CSI.getOperandList(); |
| 1054 | for (unsigned I = 1, E = ReservedSpace; I != E; ++I) |
| 1055 | OL[I] = InOL[I]; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1056 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1057 | |
| 1058 | void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest, |
| 1059 | unsigned NumReservedValues) { |
| 1060 | assert(ParentPad && NumReservedValues); |
| 1061 | |
| 1062 | ReservedSpace = NumReservedValues; |
| 1063 | setNumHungOffUseOperands(UnwindDest ? 2 : 1); |
| 1064 | allocHungoffUses(ReservedSpace); |
| 1065 | |
| 1066 | Op<0>() = ParentPad; |
| 1067 | if (UnwindDest) { |
| 1068 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
| 1069 | setUnwindDest(UnwindDest); |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | /// growOperands - grow operands - This grows the operand list in response to a |
| 1074 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 1075 | void CatchSwitchInst::growOperands(unsigned Size) { |
| 1076 | unsigned NumOperands = getNumOperands(); |
| 1077 | assert(NumOperands >= 1); |
| 1078 | if (ReservedSpace >= NumOperands + Size) |
| 1079 | return; |
| 1080 | ReservedSpace = (NumOperands + Size / 2) * 2; |
| 1081 | growHungoffUses(ReservedSpace); |
| 1082 | } |
| 1083 | |
| 1084 | void CatchSwitchInst::addHandler(BasicBlock *Handler) { |
| 1085 | unsigned OpNo = getNumOperands(); |
| 1086 | growOperands(1); |
| 1087 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
| 1088 | setNumHungOffUseOperands(getNumOperands() + 1); |
| 1089 | getOperandList()[OpNo] = Handler; |
| 1090 | } |
| 1091 | |
Joseph Tremoulet | 0d80888 | 2016-01-05 02:37:41 +0000 | [diff] [blame] | 1092 | void CatchSwitchInst::removeHandler(handler_iterator HI) { |
| 1093 | // Move all subsequent handlers up one. |
| 1094 | Use *EndDst = op_end() - 1; |
| 1095 | for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst) |
| 1096 | *CurDst = *(CurDst + 1); |
| 1097 | // Null out the last handler use. |
| 1098 | *EndDst = nullptr; |
| 1099 | |
| 1100 | setNumHungOffUseOperands(getNumOperands() - 1); |
| 1101 | } |
| 1102 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1103 | BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const { |
| 1104 | return getSuccessor(idx); |
| 1105 | } |
| 1106 | unsigned CatchSwitchInst::getNumSuccessorsV() const { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1107 | return getNumSuccessors(); |
| 1108 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 1109 | void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 1110 | setSuccessor(idx, B); |
| 1111 | } |
| 1112 | |
| 1113 | //===----------------------------------------------------------------------===// |
| 1114 | // FuncletPadInst Implementation |
| 1115 | //===----------------------------------------------------------------------===// |
| 1116 | void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args, |
| 1117 | const Twine &NameStr) { |
| 1118 | assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?"); |
| 1119 | std::copy(Args.begin(), Args.end(), op_begin()); |
| 1120 | setParentPad(ParentPad); |
| 1121 | setName(NameStr); |
| 1122 | } |
| 1123 | |
| 1124 | FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI) |
| 1125 | : Instruction(FPI.getType(), FPI.getOpcode(), |
| 1126 | OperandTraits<FuncletPadInst>::op_end(this) - |
| 1127 | FPI.getNumOperands(), |
| 1128 | FPI.getNumOperands()) { |
| 1129 | std::copy(FPI.op_begin(), FPI.op_end(), op_begin()); |
| 1130 | setParentPad(FPI.getParentPad()); |
| 1131 | } |
| 1132 | |
| 1133 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
| 1134 | ArrayRef<Value *> Args, unsigned Values, |
| 1135 | const Twine &NameStr, Instruction *InsertBefore) |
| 1136 | : Instruction(ParentPad->getType(), Op, |
| 1137 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, |
| 1138 | InsertBefore) { |
| 1139 | init(ParentPad, Args, NameStr); |
| 1140 | } |
| 1141 | |
| 1142 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
| 1143 | ArrayRef<Value *> Args, unsigned Values, |
| 1144 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 1145 | : Instruction(ParentPad->getType(), Op, |
| 1146 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, |
| 1147 | InsertAtEnd) { |
| 1148 | init(ParentPad, Args, NameStr); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 1152 | // UnreachableInst Implementation |
| 1153 | //===----------------------------------------------------------------------===// |
| 1154 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1155 | UnreachableInst::UnreachableInst(LLVMContext &Context, |
| 1156 | Instruction *InsertBefore) |
| 1157 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1158 | nullptr, 0, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1159 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1160 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 1161 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1162 | nullptr, 0, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1165 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 1166 | return getNumSuccessors(); |
| 1167 | } |
| 1168 | |
| 1169 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Bill Wendling | 0aef16a | 2012-02-06 21:44:22 +0000 | [diff] [blame] | 1170 | llvm_unreachable("UnreachableInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
Bill Wendling | 0aef16a | 2012-02-06 21:44:22 +0000 | [diff] [blame] | 1174 | llvm_unreachable("UnreachableInst has no successors!"); |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1178 | // BranchInst Implementation |
| 1179 | //===----------------------------------------------------------------------===// |
| 1180 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1181 | void BranchInst::AssertOK() { |
| 1182 | if (isConditional()) |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1183 | assert(getCondition()->getType()->isIntegerTy(1) && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1184 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1187 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1188 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1189 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 1190 | 1, InsertBefore) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1191 | assert(IfTrue && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1192 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1193 | } |
| 1194 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 1195 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1196 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1197 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 1198 | 3, InsertBefore) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1199 | Op<-1>() = IfTrue; |
| 1200 | Op<-2>() = IfFalse; |
| 1201 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1202 | #ifndef NDEBUG |
| 1203 | AssertOK(); |
| 1204 | #endif |
| 1205 | } |
| 1206 | |
| 1207 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1208 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1209 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 1210 | 1, InsertAtEnd) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1211 | assert(IfTrue && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1212 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 1216 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1217 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1218 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 1219 | 3, InsertAtEnd) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1220 | Op<-1>() = IfTrue; |
| 1221 | Op<-2>() = IfFalse; |
| 1222 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1223 | #ifndef NDEBUG |
| 1224 | AssertOK(); |
| 1225 | #endif |
| 1226 | } |
| 1227 | |
| 1228 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1229 | BranchInst::BranchInst(const BranchInst &BI) : |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1230 | TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1231 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), |
| 1232 | BI.getNumOperands()) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1233 | Op<-1>() = BI.Op<-1>(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1234 | if (BI.getNumOperands() != 1) { |
| 1235 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1236 | Op<-3>() = BI.Op<-3>(); |
| 1237 | Op<-2>() = BI.Op<-2>(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1238 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1239 | SubclassOptionalData = BI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Chandler Carruth | 3e8aa65 | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 1242 | void BranchInst::swapSuccessors() { |
| 1243 | assert(isConditional() && |
| 1244 | "Cannot swap successors of an unconditional branch"); |
| 1245 | Op<-1>().swap(Op<-2>()); |
| 1246 | |
| 1247 | // Update profile metadata if present and it matches our structural |
| 1248 | // expectations. |
Xinliang David Li | dc49140 | 2016-08-23 15:39:03 +0000 | [diff] [blame] | 1249 | swapProfMetadata(); |
Chandler Carruth | 3e8aa65 | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1252 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 1253 | return getSuccessor(idx); |
| 1254 | } |
| 1255 | unsigned BranchInst::getNumSuccessorsV() const { |
| 1256 | return getNumSuccessors(); |
| 1257 | } |
| 1258 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 1259 | setSuccessor(idx, B); |
| 1260 | } |
| 1261 | |
| 1262 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1263 | //===----------------------------------------------------------------------===// |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1264 | // AllocaInst Implementation |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1265 | //===----------------------------------------------------------------------===// |
| 1266 | |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 1267 | static Value *getAISize(LLVMContext &Context, Value *Amt) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1268 | if (!Amt) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1269 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 1270 | else { |
| 1271 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 9b6ec77 | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 1272 | "Passed basic block into allocation size parameter! Use other ctor"); |
Dan Gohman | 2140a74 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 1273 | assert(Amt->getType()->isIntegerTy() && |
| 1274 | "Allocation array size is not an integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 1275 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1276 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1279 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1280 | Instruction *InsertBefore) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1281 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {} |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1282 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1283 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1284 | BasicBlock *InsertAtEnd) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1285 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {} |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1286 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1287 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1288 | const Twine &Name, Instruction *InsertBefore) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1289 | : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {} |
| 1290 | |
| 1291 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1292 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 1293 | : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {} |
| 1294 | |
| 1295 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1296 | unsigned Align, const Twine &Name, |
| 1297 | Instruction *InsertBefore) |
| 1298 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, |
| 1299 | getAISize(Ty->getContext(), ArraySize), InsertBefore), |
| 1300 | AllocatedType(Ty) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1301 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1302 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1303 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1306 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1307 | unsigned Align, const Twine &Name, |
| 1308 | BasicBlock *InsertAtEnd) |
| 1309 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, |
| 1310 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd), |
David Blaikie | bf0a42a | 2015-04-29 23:00:35 +0000 | [diff] [blame] | 1311 | AllocatedType(Ty) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1312 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1313 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1314 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 1317 | // Out of line virtual method, so the vtable, etc has a home. |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1318 | AllocaInst::~AllocaInst() { |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1321 | void AllocaInst::setAlignment(unsigned Align) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1322 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1323 | assert(Align <= MaximumAlignment && |
| 1324 | "Alignment is greater than MaximumAlignment!"); |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 1325 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) | |
| 1326 | (Log2_32(Align) + 1)); |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1327 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 1328 | } |
| 1329 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1330 | bool AllocaInst::isArrayAllocation() const { |
Reid Spencer | a9e6e31 | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 1331 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
Dan Gohman | 9a1b859 | 2010-09-27 15:15:44 +0000 | [diff] [blame] | 1332 | return !CI->isOne(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1333 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1336 | /// isStaticAlloca - Return true if this alloca is in the entry block of the |
| 1337 | /// function and is a constant size. If so, the code generator will fold it |
| 1338 | /// into the prolog/epilog code, so it is basically free. |
| 1339 | bool AllocaInst::isStaticAlloca() const { |
| 1340 | // Must be constant size. |
| 1341 | if (!isa<ConstantInt>(getArraySize())) return false; |
| 1342 | |
| 1343 | // Must be in the entry block. |
| 1344 | const BasicBlock *Parent = getParent(); |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 1345 | return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca(); |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1348 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1349 | // LoadInst Implementation |
| 1350 | //===----------------------------------------------------------------------===// |
| 1351 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1352 | void LoadInst::AssertOK() { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1353 | assert(getOperand(0)->getType()->isPointerTy() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1354 | "Ptr must have pointer type."); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1355 | assert(!(isAtomic() && getAlignment() == 0) && |
| 1356 | "Alignment required for atomic load"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1359 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1360 | : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1361 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1362 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1363 | : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1364 | |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1365 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1366 | Instruction *InsertBef) |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1367 | : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {} |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1368 | |
| 1369 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 1370 | BasicBlock *InsertAE) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1371 | : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {} |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1372 | |
David Blaikie | b7a02987 | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 1373 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1374 | unsigned Align, Instruction *InsertBef) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1375 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, |
| 1376 | CrossThread, InsertBef) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1377 | |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1378 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1379 | unsigned Align, BasicBlock *InsertAE) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1380 | : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, |
| 1381 | CrossThread, InsertAE) {} |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1382 | |
David Blaikie | 15d9a4c | 2015-04-06 20:59:48 +0000 | [diff] [blame] | 1383 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1384 | unsigned Align, AtomicOrdering Order, |
David Blaikie | 15d9a4c | 2015-04-06 20:59:48 +0000 | [diff] [blame] | 1385 | SynchronizationScope SynchScope, Instruction *InsertBef) |
| 1386 | : UnaryInstruction(Ty, Load, Ptr, InsertBef) { |
David Blaikie | 79009f8 | 2015-05-20 20:22:31 +0000 | [diff] [blame] | 1387 | assert(Ty == cast<PointerType>(Ptr->getType())->getElementType()); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1388 | setVolatile(isVolatile); |
| 1389 | setAlignment(Align); |
| 1390 | setAtomic(Order, SynchScope); |
| 1391 | AssertOK(); |
| 1392 | setName(Name); |
| 1393 | } |
| 1394 | |
| 1395 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 1396 | unsigned Align, AtomicOrdering Order, |
| 1397 | SynchronizationScope SynchScope, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1398 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1399 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1400 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1401 | setVolatile(isVolatile); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1402 | setAlignment(Align); |
| 1403 | setAtomic(Order, SynchScope); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1404 | AssertOK(); |
| 1405 | setName(Name); |
| 1406 | } |
| 1407 | |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1408 | LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) |
| 1409 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1410 | Load, Ptr, InsertBef) { |
| 1411 | setVolatile(false); |
| 1412 | setAlignment(0); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1413 | setAtomic(AtomicOrdering::NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1414 | AssertOK(); |
| 1415 | if (Name && Name[0]) setName(Name); |
| 1416 | } |
| 1417 | |
| 1418 | LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) |
| 1419 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1420 | Load, Ptr, InsertAE) { |
| 1421 | setVolatile(false); |
| 1422 | setAlignment(0); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1423 | setAtomic(AtomicOrdering::NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1424 | AssertOK(); |
| 1425 | if (Name && Name[0]) setName(Name); |
| 1426 | } |
| 1427 | |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1428 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile, |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1429 | Instruction *InsertBef) |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1430 | : UnaryInstruction(Ty, Load, Ptr, InsertBef) { |
| 1431 | assert(Ty == cast<PointerType>(Ptr->getType())->getElementType()); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1432 | setVolatile(isVolatile); |
| 1433 | setAlignment(0); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1434 | setAtomic(AtomicOrdering::NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1435 | AssertOK(); |
| 1436 | if (Name && Name[0]) setName(Name); |
| 1437 | } |
| 1438 | |
| 1439 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 1440 | BasicBlock *InsertAE) |
| 1441 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1442 | Load, Ptr, InsertAE) { |
| 1443 | setVolatile(isVolatile); |
| 1444 | setAlignment(0); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1445 | setAtomic(AtomicOrdering::NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1446 | AssertOK(); |
| 1447 | if (Name && Name[0]) setName(Name); |
| 1448 | } |
| 1449 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1450 | void LoadInst::setAlignment(unsigned Align) { |
| 1451 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1452 | assert(Align <= MaximumAlignment && |
| 1453 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1454 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1455 | ((Log2_32(Align)+1)<<1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1456 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1457 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1458 | |
| 1459 | //===----------------------------------------------------------------------===// |
| 1460 | // StoreInst Implementation |
| 1461 | //===----------------------------------------------------------------------===// |
| 1462 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1463 | void StoreInst::AssertOK() { |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1464 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1465 | assert(getOperand(1)->getType()->isPointerTy() && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1466 | "Ptr must have pointer type!"); |
| 1467 | assert(getOperand(0)->getType() == |
| 1468 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 1469 | && "Ptr must be a pointer to Val type!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1470 | assert(!(isAtomic() && getAlignment() == 0) && |
Mark Lacey | 1d7c97e | 2013-12-21 00:00:49 +0000 | [diff] [blame] | 1471 | "Alignment required for atomic store"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1472 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1473 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1474 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1475 | : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1476 | |
| 1477 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1478 | : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1479 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1480 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1481 | Instruction *InsertBefore) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1482 | : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {} |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1483 | |
| 1484 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1485 | BasicBlock *InsertAtEnd) |
| 1486 | : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {} |
| 1487 | |
| 1488 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align, |
| 1489 | Instruction *InsertBefore) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1490 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, |
| 1491 | CrossThread, InsertBefore) {} |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1492 | |
| 1493 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align, |
| 1494 | BasicBlock *InsertAtEnd) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1495 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, |
| 1496 | CrossThread, InsertAtEnd) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1497 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1498 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1499 | unsigned Align, AtomicOrdering Order, |
| 1500 | SynchronizationScope SynchScope, |
| 1501 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1502 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1503 | OperandTraits<StoreInst>::op_begin(this), |
| 1504 | OperandTraits<StoreInst>::operands(this), |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1505 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1506 | Op<0>() = val; |
| 1507 | Op<1>() = addr; |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1508 | setVolatile(isVolatile); |
| 1509 | setAlignment(Align); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1510 | setAtomic(Order, SynchScope); |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1511 | AssertOK(); |
| 1512 | } |
| 1513 | |
| 1514 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1515 | unsigned Align, AtomicOrdering Order, |
| 1516 | SynchronizationScope SynchScope, |
| 1517 | BasicBlock *InsertAtEnd) |
| 1518 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
| 1519 | OperandTraits<StoreInst>::op_begin(this), |
| 1520 | OperandTraits<StoreInst>::operands(this), |
| 1521 | InsertAtEnd) { |
| 1522 | Op<0>() = val; |
| 1523 | Op<1>() = addr; |
| 1524 | setVolatile(isVolatile); |
| 1525 | setAlignment(Align); |
| 1526 | setAtomic(Order, SynchScope); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1527 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1530 | void StoreInst::setAlignment(unsigned Align) { |
| 1531 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1532 | assert(Align <= MaximumAlignment && |
| 1533 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1534 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1535 | ((Log2_32(Align)+1) << 1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1536 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1539 | //===----------------------------------------------------------------------===// |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1540 | // AtomicCmpXchgInst Implementation |
| 1541 | //===----------------------------------------------------------------------===// |
| 1542 | |
| 1543 | void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1544 | AtomicOrdering SuccessOrdering, |
| 1545 | AtomicOrdering FailureOrdering, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1546 | SynchronizationScope SynchScope) { |
| 1547 | Op<0>() = Ptr; |
| 1548 | Op<1>() = Cmp; |
| 1549 | Op<2>() = NewVal; |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1550 | setSuccessOrdering(SuccessOrdering); |
| 1551 | setFailureOrdering(FailureOrdering); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1552 | setSynchScope(SynchScope); |
| 1553 | |
| 1554 | assert(getOperand(0) && getOperand(1) && getOperand(2) && |
| 1555 | "All operands must be non-null!"); |
| 1556 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1557 | "Ptr must have pointer type!"); |
| 1558 | assert(getOperand(1)->getType() == |
| 1559 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1560 | && "Ptr must be a pointer to Cmp type!"); |
| 1561 | assert(getOperand(2)->getType() == |
| 1562 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1563 | && "Ptr must be a pointer to NewVal type!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1564 | assert(SuccessOrdering != AtomicOrdering::NotAtomic && |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1565 | "AtomicCmpXchg instructions must be atomic!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1566 | assert(FailureOrdering != AtomicOrdering::NotAtomic && |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1567 | "AtomicCmpXchg instructions must be atomic!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1568 | assert(!isStrongerThan(FailureOrdering, SuccessOrdering) && |
| 1569 | "AtomicCmpXchg failure argument shall be no stronger than the success " |
| 1570 | "argument"); |
| 1571 | assert(FailureOrdering != AtomicOrdering::Release && |
| 1572 | FailureOrdering != AtomicOrdering::AcquireRelease && |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1573 | "AtomicCmpXchg failure ordering cannot include release semantics"); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1577 | AtomicOrdering SuccessOrdering, |
| 1578 | AtomicOrdering FailureOrdering, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1579 | SynchronizationScope SynchScope, |
| 1580 | Instruction *InsertBefore) |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1581 | : Instruction( |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1582 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1583 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1584 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) { |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1585 | Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1589 | AtomicOrdering SuccessOrdering, |
| 1590 | AtomicOrdering FailureOrdering, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1591 | SynchronizationScope SynchScope, |
| 1592 | BasicBlock *InsertAtEnd) |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1593 | : Instruction( |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1594 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1595 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1596 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) { |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1597 | Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1598 | } |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1599 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1600 | //===----------------------------------------------------------------------===// |
| 1601 | // AtomicRMWInst Implementation |
| 1602 | //===----------------------------------------------------------------------===// |
| 1603 | |
| 1604 | void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, |
| 1605 | AtomicOrdering Ordering, |
| 1606 | SynchronizationScope SynchScope) { |
| 1607 | Op<0>() = Ptr; |
| 1608 | Op<1>() = Val; |
| 1609 | setOperation(Operation); |
| 1610 | setOrdering(Ordering); |
| 1611 | setSynchScope(SynchScope); |
| 1612 | |
| 1613 | assert(getOperand(0) && getOperand(1) && |
| 1614 | "All operands must be non-null!"); |
| 1615 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1616 | "Ptr must have pointer type!"); |
| 1617 | assert(getOperand(1)->getType() == |
| 1618 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1619 | && "Ptr must be a pointer to Val type!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1620 | assert(Ordering != AtomicOrdering::NotAtomic && |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1621 | "AtomicRMW instructions must be atomic!"); |
| 1622 | } |
| 1623 | |
| 1624 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1625 | AtomicOrdering Ordering, |
| 1626 | SynchronizationScope SynchScope, |
| 1627 | Instruction *InsertBefore) |
| 1628 | : Instruction(Val->getType(), AtomicRMW, |
| 1629 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1630 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1631 | InsertBefore) { |
| 1632 | Init(Operation, Ptr, Val, Ordering, SynchScope); |
| 1633 | } |
| 1634 | |
| 1635 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1636 | AtomicOrdering Ordering, |
| 1637 | SynchronizationScope SynchScope, |
| 1638 | BasicBlock *InsertAtEnd) |
| 1639 | : Instruction(Val->getType(), AtomicRMW, |
| 1640 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1641 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1642 | InsertAtEnd) { |
| 1643 | Init(Operation, Ptr, Val, Ordering, SynchScope); |
| 1644 | } |
| 1645 | |
| 1646 | //===----------------------------------------------------------------------===// |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1647 | // FenceInst Implementation |
| 1648 | //===----------------------------------------------------------------------===// |
| 1649 | |
| 1650 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
| 1651 | SynchronizationScope SynchScope, |
| 1652 | Instruction *InsertBefore) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1653 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1654 | setOrdering(Ordering); |
| 1655 | setSynchScope(SynchScope); |
| 1656 | } |
| 1657 | |
| 1658 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
| 1659 | SynchronizationScope SynchScope, |
| 1660 | BasicBlock *InsertAtEnd) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1661 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1662 | setOrdering(Ordering); |
| 1663 | setSynchScope(SynchScope); |
| 1664 | } |
| 1665 | |
| 1666 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1667 | // GetElementPtrInst Implementation |
| 1668 | //===----------------------------------------------------------------------===// |
| 1669 | |
Pete Cooper | 1d07869 | 2015-12-14 20:29:16 +0000 | [diff] [blame] | 1670 | void GetElementPtrInst::anchor() {} |
| 1671 | |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1672 | void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1673 | const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1674 | assert(getNumOperands() == 1 + IdxList.size() && |
| 1675 | "NumOperands not initialized?"); |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 1676 | Op<0>() = Ptr; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1677 | std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1); |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1678 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1681 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) |
David Blaikie | 73cf872 | 2015-05-05 18:03:48 +0000 | [diff] [blame] | 1682 | : Instruction(GEPI.getType(), GetElementPtr, |
| 1683 | OperandTraits<GetElementPtrInst>::op_end(this) - |
| 1684 | GEPI.getNumOperands(), |
| 1685 | GEPI.getNumOperands()), |
David Blaikie | f5147ef | 2015-06-01 03:09:34 +0000 | [diff] [blame] | 1686 | SourceElementType(GEPI.SourceElementType), |
| 1687 | ResultElementType(GEPI.ResultElementType) { |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1688 | std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1689 | SubclassOptionalData = GEPI.SubclassOptionalData; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1692 | /// getIndexedType - Returns the type of the element that would be accessed with |
| 1693 | /// a gep instruction with the specified parameters. |
| 1694 | /// |
| 1695 | /// The Idxs pointer should point to a continuous piece of memory containing the |
| 1696 | /// indices, either as Value* or uint64_t. |
| 1697 | /// |
| 1698 | /// A null type is returned if the indices are invalid for the specified |
| 1699 | /// pointer type. |
| 1700 | /// |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1701 | template <typename IndexTy> |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1702 | static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) { |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1703 | // Handle the special case of the empty set index set, which is always valid. |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1704 | if (IdxList.empty()) |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1705 | return Agg; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1706 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1707 | // If there is at least one index, the top level type must be sized, otherwise |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1708 | // it cannot be 'stepped over'. |
| 1709 | if (!Agg->isSized()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1710 | return nullptr; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1711 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1712 | unsigned CurIdx = 1; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1713 | for (; CurIdx != IdxList.size(); ++CurIdx) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1714 | CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1715 | if (!CT || CT->isPointerTy()) return nullptr; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1716 | IndexTy Index = IdxList[CurIdx]; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1717 | if (!CT->indexValid(Index)) return nullptr; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1718 | Agg = CT->getTypeAtIndex(Index); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1719 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1720 | return CurIdx == IdxList.size() ? Agg : nullptr; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1723 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) { |
| 1724 | return getIndexedTypeInternal(Ty, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1727 | Type *GetElementPtrInst::getIndexedType(Type *Ty, |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1728 | ArrayRef<Constant *> IdxList) { |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1729 | return getIndexedTypeInternal(Ty, IdxList); |
Jay Foad | 1d4a8fe | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1732 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) { |
| 1733 | return getIndexedTypeInternal(Ty, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1736 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1737 | /// zeros. If so, the result pointer and the first operand have the same |
| 1738 | /// value, just potentially different types. |
| 1739 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1740 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1741 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1742 | if (!CI->isZero()) return false; |
| 1743 | } else { |
| 1744 | return false; |
| 1745 | } |
| 1746 | } |
| 1747 | return true; |
| 1748 | } |
| 1749 | |
Chris Lattner | 2705829 | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1750 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1751 | /// constant integers. If so, the result pointer and the first operand have |
| 1752 | /// a constant offset between them. |
| 1753 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1754 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1755 | if (!isa<ConstantInt>(getOperand(i))) |
| 1756 | return false; |
| 1757 | } |
| 1758 | return true; |
| 1759 | } |
| 1760 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1761 | void GetElementPtrInst::setIsInBounds(bool B) { |
| 1762 | cast<GEPOperator>(this)->setIsInBounds(B); |
| 1763 | } |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1764 | |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1765 | bool GetElementPtrInst::isInBounds() const { |
| 1766 | return cast<GEPOperator>(this)->isInBounds(); |
| 1767 | } |
| 1768 | |
Chandler Carruth | 1e14053 | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1769 | bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, |
| 1770 | APInt &Offset) const { |
Chandler Carruth | 7ec41c7 | 2012-12-11 11:05:15 +0000 | [diff] [blame] | 1771 | // Delegate to the generic GEPOperator implementation. |
| 1772 | return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); |
Chandler Carruth | 1e14053 | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1775 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1776 | // ExtractElementInst Implementation |
| 1777 | //===----------------------------------------------------------------------===// |
| 1778 | |
| 1779 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1780 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1781 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1782 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1783 | ExtractElement, |
| 1784 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1785 | 2, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1786 | assert(isValidOperands(Val, Index) && |
| 1787 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1788 | Op<0>() = Val; |
| 1789 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1790 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1794 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1795 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1796 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1797 | ExtractElement, |
| 1798 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1799 | 2, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1800 | assert(isValidOperands(Val, Index) && |
| 1801 | "Invalid extractelement instruction operands!"); |
| 1802 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1803 | Op<0>() = Val; |
| 1804 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1805 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1808 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1809 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1810 | if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy()) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1811 | return false; |
| 1812 | return true; |
| 1813 | } |
| 1814 | |
| 1815 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1816 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1817 | // InsertElementInst Implementation |
| 1818 | //===----------------------------------------------------------------------===// |
| 1819 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1820 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1821 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1822 | Instruction *InsertBef) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1823 | : Instruction(Vec->getType(), InsertElement, |
| 1824 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1825 | 3, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1826 | assert(isValidOperands(Vec, Elt, Index) && |
| 1827 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1828 | Op<0>() = Vec; |
| 1829 | Op<1>() = Elt; |
| 1830 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1831 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1834 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1835 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1836 | BasicBlock *InsertAE) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1837 | : Instruction(Vec->getType(), InsertElement, |
| 1838 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1839 | 3, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1840 | assert(isValidOperands(Vec, Elt, Index) && |
| 1841 | "Invalid insertelement instruction operands!"); |
| 1842 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1843 | Op<0>() = Vec; |
| 1844 | Op<1>() = Elt; |
| 1845 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1846 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1849 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 1850 | const Value *Index) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1851 | if (!Vec->getType()->isVectorTy()) |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1852 | return false; // First operand of insertelement must be vector type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1853 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1854 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1855 | return false;// Second operand of insertelement must be vector element type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1856 | |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1857 | if (!Index->getType()->isIntegerTy()) |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1858 | return false; // Third operand of insertelement must be i32. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1859 | return true; |
| 1860 | } |
| 1861 | |
| 1862 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1863 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1864 | // ShuffleVectorInst Implementation |
| 1865 | //===----------------------------------------------------------------------===// |
| 1866 | |
| 1867 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1868 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1869 | Instruction *InsertBefore) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1870 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1871 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1872 | ShuffleVector, |
| 1873 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1874 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1875 | InsertBefore) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1876 | assert(isValidOperands(V1, V2, Mask) && |
| 1877 | "Invalid shuffle vector instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1878 | Op<0>() = V1; |
| 1879 | Op<1>() = V2; |
| 1880 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1881 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1885 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1886 | BasicBlock *InsertAtEnd) |
Dan Gohman | e5af8cd | 2009-08-25 23:27:45 +0000 | [diff] [blame] | 1887 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
| 1888 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1889 | ShuffleVector, |
| 1890 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1891 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1892 | InsertAtEnd) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1893 | assert(isValidOperands(V1, V2, Mask) && |
| 1894 | "Invalid shuffle vector instruction operands!"); |
| 1895 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1896 | Op<0>() = V1; |
| 1897 | Op<1>() = V2; |
| 1898 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1899 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1902 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1903 | const Value *Mask) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1904 | // V1 and V2 must be vectors of the same type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1905 | if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1906 | return false; |
| 1907 | |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1908 | // Mask must be vector of i32. |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1909 | auto *MaskTy = dyn_cast<VectorType>(Mask->getType()); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1910 | if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32)) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1911 | return false; |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1912 | |
| 1913 | // Check to see if Mask is valid. |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1914 | if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) |
| 1915 | return true; |
| 1916 | |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1917 | if (const auto *MV = dyn_cast<ConstantVector>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1918 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1919 | for (Value *Op : MV->operands()) { |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1920 | if (auto *CI = dyn_cast<ConstantInt>(Op)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1921 | if (CI->uge(V1Size*2)) |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1922 | return false; |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1923 | } else if (!isa<UndefValue>(Op)) { |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1924 | return false; |
| 1925 | } |
| 1926 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1927 | return true; |
Mon P Wang | 6ebf401 | 2011-10-26 00:34:48 +0000 | [diff] [blame] | 1928 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1929 | |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1930 | if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1931 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
| 1932 | for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i) |
| 1933 | if (CDS->getElementAsInteger(i) >= V1Size*2) |
| 1934 | return false; |
| 1935 | return true; |
| 1936 | } |
| 1937 | |
| 1938 | // The bitcode reader can create a place holder for a forward reference |
| 1939 | // used as the shuffle mask. When this occurs, the shuffle mask will |
| 1940 | // fall into this case and fail. To avoid this error, do this bit of |
| 1941 | // ugliness to allow such a mask pass. |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1942 | if (const auto *CE = dyn_cast<ConstantExpr>(Mask)) |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1943 | if (CE->getOpcode() == Instruction::UserOp1) |
| 1944 | return true; |
| 1945 | |
| 1946 | return false; |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1949 | int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) { |
| 1950 | assert(i < Mask->getType()->getVectorNumElements() && "Index out of range"); |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1951 | if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1952 | return CDS->getElementAsInteger(i); |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1953 | Constant *C = Mask->getAggregateElement(i); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1954 | if (isa<UndefValue>(C)) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1955 | return -1; |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1956 | return cast<ConstantInt>(C)->getZExtValue(); |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1959 | void ShuffleVectorInst::getShuffleMask(Constant *Mask, |
| 1960 | SmallVectorImpl<int> &Result) { |
| 1961 | unsigned NumElts = Mask->getType()->getVectorNumElements(); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1962 | |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1963 | if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1964 | for (unsigned i = 0; i != NumElts; ++i) |
| 1965 | Result.push_back(CDS->getElementAsInteger(i)); |
| 1966 | return; |
| 1967 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1968 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1969 | Constant *C = Mask->getAggregateElement(i); |
| 1970 | Result.push_back(isa<UndefValue>(C) ? -1 : |
Chris Lattner | 3dbad403 | 2012-01-26 00:41:50 +0000 | [diff] [blame] | 1971 | cast<ConstantInt>(C)->getZExtValue()); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1976 | //===----------------------------------------------------------------------===// |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1977 | // InsertValueInst Class |
| 1978 | //===----------------------------------------------------------------------===// |
| 1979 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1980 | void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, |
| 1981 | const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1982 | assert(getNumOperands() == 2 && "NumOperands not initialized?"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1983 | |
| 1984 | // There's no fundamental reason why we require at least one index |
| 1985 | // (other than weirdness with &*IdxBegin being invalid; see |
| 1986 | // getelementptr's init routine for example). But there's no |
| 1987 | // present need to support it. |
| 1988 | assert(Idxs.size() > 0 && "InsertValueInst must have at least one index"); |
| 1989 | |
| 1990 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1991 | Val->getType() && "Inserted value must match indexed type!"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1992 | Op<0>() = Agg; |
| 1993 | Op<1>() = Val; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1994 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1995 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1996 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 2000 | : Instruction(IVI.getType(), InsertValue, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2001 | OperandTraits<InsertValueInst>::op_begin(this), 2), |
| 2002 | Indices(IVI.Indices) { |
Dan Gohman | d8ca05f | 2008-06-17 23:25:49 +0000 | [diff] [blame] | 2003 | Op<0>() = IVI.getOperand(0); |
| 2004 | Op<1>() = IVI.getOperand(1); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2005 | SubclassOptionalData = IVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | //===----------------------------------------------------------------------===// |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2009 | // ExtractValueInst Class |
| 2010 | //===----------------------------------------------------------------------===// |
| 2011 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2012 | void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 2013 | assert(getNumOperands() == 1 && "NumOperands not initialized?"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2014 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2015 | // There's no fundamental reason why we require at least one index. |
| 2016 | // But there's no present need to support it. |
| 2017 | assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2018 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2019 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 2020 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 2024 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2025 | Indices(EVI.Indices) { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2026 | SubclassOptionalData = EVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2029 | // getIndexedType - Returns the type of the element that would be extracted |
| 2030 | // with an extractvalue instruction with the specified parameters. |
| 2031 | // |
| 2032 | // A null type is returned if the indices are invalid for the specified |
| 2033 | // pointer type. |
| 2034 | // |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2035 | Type *ExtractValueInst::getIndexedType(Type *Agg, |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2036 | ArrayRef<unsigned> Idxs) { |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 2037 | for (unsigned Index : Idxs) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2038 | // We can't use CompositeType::indexValid(Index) here. |
| 2039 | // indexValid() always returns true for arrays because getelementptr allows |
| 2040 | // out-of-bounds indices. Since we don't allow those for extractvalue and |
| 2041 | // insertvalue we need to check array indexing manually. |
| 2042 | // Since the only other types we can index into are struct types it's just |
| 2043 | // as easy to check those manually as well. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2044 | if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2045 | if (Index >= AT->getNumElements()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2046 | return nullptr; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2047 | } else if (StructType *ST = dyn_cast<StructType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2048 | if (Index >= ST->getNumElements()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2049 | return nullptr; |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2050 | } else { |
| 2051 | // Not a valid type to index into. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2052 | return nullptr; |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2056 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2057 | return const_cast<Type*>(Agg); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2058 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2059 | |
| 2060 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2061 | // BinaryOperator Class |
| 2062 | //===----------------------------------------------------------------------===// |
| 2063 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2064 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2065 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2066 | Instruction *InsertBefore) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2067 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2068 | OperandTraits<BinaryOperator>::op_begin(this), |
| 2069 | OperandTraits<BinaryOperator>::operands(this), |
| 2070 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2071 | Op<0>() = S1; |
| 2072 | Op<1>() = S2; |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2073 | init(iType); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2074 | setName(Name); |
| 2075 | } |
| 2076 | |
| 2077 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2078 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2079 | BasicBlock *InsertAtEnd) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2080 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2081 | OperandTraits<BinaryOperator>::op_begin(this), |
| 2082 | OperandTraits<BinaryOperator>::operands(this), |
| 2083 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2084 | Op<0>() = S1; |
| 2085 | Op<1>() = S2; |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2086 | init(iType); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2087 | setName(Name); |
| 2088 | } |
| 2089 | |
| 2090 | |
| 2091 | void BinaryOperator::init(BinaryOps iType) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2092 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Jeffrey Yasskin | 9b43f33 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 2093 | (void)LHS; (void)RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2094 | assert(LHS->getType() == RHS->getType() && |
| 2095 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2096 | #ifndef NDEBUG |
| 2097 | switch (iType) { |
| 2098 | case Add: case Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2099 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2100 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2101 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2102 | assert(getType()->isIntOrIntVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2103 | "Tried to create an integer operation on a non-integer type!"); |
| 2104 | break; |
| 2105 | case FAdd: case FSub: |
| 2106 | case FMul: |
| 2107 | assert(getType() == LHS->getType() && |
| 2108 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2109 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2110 | "Tried to create a floating-point operation on a " |
| 2111 | "non-floating-point type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2112 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2113 | case UDiv: |
| 2114 | case SDiv: |
| 2115 | assert(getType() == LHS->getType() && |
| 2116 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2117 | assert((getType()->isIntegerTy() || (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2118 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2119 | "Incorrect operand type (not integer) for S/UDIV"); |
| 2120 | break; |
| 2121 | case FDiv: |
| 2122 | assert(getType() == LHS->getType() && |
| 2123 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2124 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2125 | "Incorrect operand type (not floating point) for FDIV"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2126 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2127 | case URem: |
| 2128 | case SRem: |
| 2129 | assert(getType() == LHS->getType() && |
| 2130 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2131 | assert((getType()->isIntegerTy() || (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2132 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2133 | "Incorrect operand type (not integer) for S/UREM"); |
| 2134 | break; |
| 2135 | case FRem: |
| 2136 | assert(getType() == LHS->getType() && |
| 2137 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2138 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2139 | "Incorrect operand type (not floating point) for FREM"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2140 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2141 | case Shl: |
| 2142 | case LShr: |
| 2143 | case AShr: |
| 2144 | assert(getType() == LHS->getType() && |
| 2145 | "Shift operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2146 | assert((getType()->isIntegerTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2147 | (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2148 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 2149 | "Tried to create a shift operation on a non-integral type!"); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2150 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2151 | case And: case Or: |
| 2152 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2153 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2154 | "Logical operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2155 | assert((getType()->isIntegerTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2156 | (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2157 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 2158 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2159 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2160 | default: |
| 2161 | break; |
| 2162 | } |
| 2163 | #endif |
| 2164 | } |
| 2165 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2166 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2167 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2168 | Instruction *InsertBefore) { |
| 2169 | assert(S1->getType() == S2->getType() && |
| 2170 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2171 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2174 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2175 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2176 | BasicBlock *InsertAtEnd) { |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2177 | BinaryOperator *Res = Create(Op, S1, S2, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2178 | InsertAtEnd->getInstList().push_back(Res); |
| 2179 | return Res; |
| 2180 | } |
| 2181 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2182 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2183 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2184 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2185 | return new BinaryOperator(Instruction::Sub, |
| 2186 | zero, Op, |
| 2187 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2190 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2191 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2192 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2193 | return new BinaryOperator(Instruction::Sub, |
| 2194 | zero, Op, |
| 2195 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
Dan Gohman | 4ab4420 | 2009-12-18 02:58:50 +0000 | [diff] [blame] | 2198 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 2199 | Instruction *InsertBefore) { |
| 2200 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2201 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); |
| 2202 | } |
| 2203 | |
| 2204 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 2205 | BasicBlock *InsertAtEnd) { |
| 2206 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2207 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); |
| 2208 | } |
| 2209 | |
Duncan Sands | fa5f596 | 2010-02-02 12:53:04 +0000 | [diff] [blame] | 2210 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 2211 | Instruction *InsertBefore) { |
| 2212 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2213 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); |
| 2214 | } |
| 2215 | |
| 2216 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 2217 | BasicBlock *InsertAtEnd) { |
| 2218 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2219 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); |
| 2220 | } |
| 2221 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2222 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2223 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2224 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2225 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2226 | Op->getType(), Name, InsertBefore); |
| 2227 | } |
| 2228 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2229 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2230 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2231 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2232 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2233 | Op->getType(), Name, InsertAtEnd); |
| 2234 | } |
| 2235 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2236 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2237 | Instruction *InsertBefore) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2238 | Constant *C = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 2239 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2240 | Op->getType(), Name, InsertBefore); |
| 2241 | } |
| 2242 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2243 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2244 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2245 | Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 2246 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2247 | Op->getType(), Name, InsertAtEnd); |
| 2248 | } |
| 2249 | |
| 2250 | |
| 2251 | // isConstantAllOnes - Helper function for several functions below |
| 2252 | static inline bool isConstantAllOnes(const Value *V) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 2253 | if (const Constant *C = dyn_cast<Constant>(V)) |
| 2254 | return C->isAllOnesValue(); |
Chris Lattner | 1edec38 | 2007-06-15 06:04:24 +0000 | [diff] [blame] | 2255 | return false; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 2258 | bool BinaryOperator::isNeg(const Value *V) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2259 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 2260 | if (Bop->getOpcode() == Instruction::Sub) |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 2261 | if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 2262 | return C->isNegativeZeroValue(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2263 | return false; |
| 2264 | } |
| 2265 | |
Shuxin Yang | f0537ab | 2013-01-09 00:13:41 +0000 | [diff] [blame] | 2266 | bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2267 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 2268 | if (Bop->getOpcode() == Instruction::FSub) |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 2269 | if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) { |
Shuxin Yang | f0537ab | 2013-01-09 00:13:41 +0000 | [diff] [blame] | 2270 | if (!IgnoreZeroSign) |
| 2271 | IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros(); |
| 2272 | return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue(); |
| 2273 | } |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2274 | return false; |
| 2275 | } |
| 2276 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2277 | bool BinaryOperator::isNot(const Value *V) { |
| 2278 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 2279 | return (Bop->getOpcode() == Instruction::Xor && |
| 2280 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 2281 | isConstantAllOnes(Bop->getOperand(0)))); |
| 2282 | return false; |
| 2283 | } |
| 2284 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2285 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2286 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2289 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 2290 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2293 | Value *BinaryOperator::getFNegArgument(Value *BinOp) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2294 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
| 2295 | } |
| 2296 | |
| 2297 | const Value *BinaryOperator::getFNegArgument(const Value *BinOp) { |
| 2298 | return getFNegArgument(const_cast<Value*>(BinOp)); |
| 2299 | } |
| 2300 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2301 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 2302 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 2303 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 2304 | Value *Op0 = BO->getOperand(0); |
| 2305 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2306 | if (isConstantAllOnes(Op0)) return Op1; |
| 2307 | |
| 2308 | assert(isConstantAllOnes(Op1)); |
| 2309 | return Op0; |
| 2310 | } |
| 2311 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2312 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 2313 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | |
Sanjay Patel | 4ce99d4 | 2016-11-16 18:09:44 +0000 | [diff] [blame] | 2317 | // Exchange the two operands to this instruction. This instruction is safe to |
| 2318 | // use on any binary instruction and does not modify the semantics of the |
| 2319 | // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode |
| 2320 | // is changed. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2321 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2322 | if (!isCommutative()) |
| 2323 | return true; // Can't commute operands |
Gabor Greif | 5ef7404 | 2008-05-13 22:51:52 +0000 | [diff] [blame] | 2324 | Op<0>().swap(Op<1>()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2325 | return false; |
| 2326 | } |
| 2327 | |
Sanjay Patel | a982d99 | 2014-09-03 01:06:50 +0000 | [diff] [blame] | 2328 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2329 | //===----------------------------------------------------------------------===// |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2330 | // FPMathOperator Class |
| 2331 | //===----------------------------------------------------------------------===// |
| 2332 | |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2333 | float FPMathOperator::getFPAccuracy() const { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 2334 | const MDNode *MD = |
| 2335 | cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2336 | if (!MD) |
| 2337 | return 0.0; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2338 | ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0)); |
Duncan Sands | 9af6298 | 2012-04-16 19:39:33 +0000 | [diff] [blame] | 2339 | return Accuracy->getValueAPF().convertToFloat(); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | |
| 2343 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2344 | // CastInst Class |
| 2345 | //===----------------------------------------------------------------------===// |
| 2346 | |
David Blaikie | 3a15e14 | 2011-12-01 08:00:17 +0000 | [diff] [blame] | 2347 | void CastInst::anchor() {} |
| 2348 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2349 | // Just determine if this cast only deals with integral->integral conversion. |
| 2350 | bool CastInst::isIntegerCast() const { |
| 2351 | switch (getOpcode()) { |
| 2352 | default: return false; |
| 2353 | case Instruction::ZExt: |
| 2354 | case Instruction::SExt: |
| 2355 | case Instruction::Trunc: |
| 2356 | return true; |
| 2357 | case Instruction::BitCast: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2358 | return getOperand(0)->getType()->isIntegerTy() && |
| 2359 | getType()->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2360 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2363 | bool CastInst::isLosslessCast() const { |
| 2364 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 2365 | if (getOpcode() != Instruction::BitCast) |
| 2366 | return false; |
| 2367 | |
| 2368 | // Identity cast is always lossless |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 2369 | Type *SrcTy = getOperand(0)->getType(); |
| 2370 | Type *DstTy = getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2371 | if (SrcTy == DstTy) |
| 2372 | return true; |
| 2373 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 2374 | // Pointer to pointer is always lossless. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2375 | if (SrcTy->isPointerTy()) |
| 2376 | return DstTy->isPointerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2377 | return false; // Other types have no identity values |
| 2378 | } |
| 2379 | |
| 2380 | /// This function determines if the CastInst does not require any bits to be |
| 2381 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 2382 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 2383 | /// example, the following are all no-op casts: |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 2384 | /// # bitcast i32* %x to i8* |
| 2385 | /// # bitcast <2 x i32> %x to <4 x i16> |
| 2386 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2387 | /// @brief Determine if the described cast is a no-op. |
| 2388 | bool CastInst::isNoopCast(Instruction::CastOps Opcode, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2389 | Type *SrcTy, |
| 2390 | Type *DestTy, |
| 2391 | Type *IntPtrTy) { |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2392 | switch (Opcode) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2393 | default: llvm_unreachable("Invalid CastOp"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2394 | case Instruction::Trunc: |
| 2395 | case Instruction::ZExt: |
| 2396 | case Instruction::SExt: |
| 2397 | case Instruction::FPTrunc: |
| 2398 | case Instruction::FPExt: |
| 2399 | case Instruction::UIToFP: |
| 2400 | case Instruction::SIToFP: |
| 2401 | case Instruction::FPToUI: |
| 2402 | case Instruction::FPToSI: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2403 | case Instruction::AddrSpaceCast: |
| 2404 | // TODO: Target informations may give a more accurate answer here. |
| 2405 | return false; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2406 | case Instruction::BitCast: |
| 2407 | return true; // BitCast never modifies bits. |
| 2408 | case Instruction::PtrToInt: |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2409 | return IntPtrTy->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2410 | DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2411 | case Instruction::IntToPtr: |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2412 | return IntPtrTy->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2413 | SrcTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2414 | } |
| 2415 | } |
| 2416 | |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2417 | /// @brief Determine if a cast is a no-op. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2418 | bool CastInst::isNoopCast(Type *IntPtrTy) const { |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2419 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy); |
| 2420 | } |
| 2421 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2422 | bool CastInst::isNoopCast(const DataLayout &DL) const { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2423 | Type *PtrOpTy = nullptr; |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 2424 | if (getOpcode() == Instruction::PtrToInt) |
| 2425 | PtrOpTy = getOperand(0)->getType(); |
| 2426 | else if (getOpcode() == Instruction::IntToPtr) |
| 2427 | PtrOpTy = getType(); |
| 2428 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2429 | Type *IntPtrTy = |
| 2430 | PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0); |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 2431 | |
| 2432 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy); |
| 2433 | } |
| 2434 | |
| 2435 | /// This function determines if a pair of casts can be eliminated and what |
| 2436 | /// opcode should be used in the elimination. This assumes that there are two |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2437 | /// instructions like this: |
| 2438 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 2439 | /// * %S = secondOpcode MidTy %F to DstTy |
| 2440 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 2441 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
Sanjay Patel | 4dad27e | 2015-12-11 18:12:01 +0000 | [diff] [blame] | 2442 | /// If no such cast is permitted, the function returns 0. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2443 | unsigned CastInst::isEliminableCastPair( |
| 2444 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2445 | Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, |
| 2446 | Type *DstIntPtrTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2447 | // Define the 144 possibilities for these two cast instructions. The values |
| 2448 | // in this matrix determine what to do in a given situation and select the |
| 2449 | // case in the switch below. The rows correspond to firstOp, the columns |
Sanjay Patel | 4dad27e | 2015-12-11 18:12:01 +0000 | [diff] [blame] | 2450 | // correspond to secondOp. In looking at the table below, keep in mind |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2451 | // the following cast properties: |
| 2452 | // |
| 2453 | // Size Compare Source Destination |
| 2454 | // Operator Src ? Size Type Sign Type Sign |
| 2455 | // -------- ------------ ------------------- --------------------- |
| 2456 | // TRUNC > Integer Any Integral Any |
| 2457 | // ZEXT < Integral Unsigned Integer Any |
| 2458 | // SEXT < Integral Signed Integer Any |
| 2459 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2460 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 2461 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 2462 | // SITOFP n/a Integral Signed FloatPt n/a |
| 2463 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 2464 | // FPEXT < FloatPt n/a FloatPt n/a |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2465 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 2466 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2467 | // BITCAST = FirstClass n/a FirstClass n/a |
| 2468 | // ADDRSPCST n/a Pointer n/a Pointer n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2469 | // |
| 2470 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 2471 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", |
| 2472 | // into "fptoui double to i64", but this loses information about the range |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2473 | // of the produced value (we no longer know the top-part is all zeros). |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2474 | // Further this conversion is often much more expensive for typical hardware, |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2475 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2476 | // same reason. |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2477 | const unsigned numCastOps = |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2478 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 2479 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2480 | // T F F U S F F P I B A -+ |
| 2481 | // R Z S P P I I T P 2 N T S | |
| 2482 | // U E E 2 2 2 2 R E I T C C +- secondOp |
| 2483 | // N X X U S F F N X N 2 V V | |
| 2484 | // C T T I I P P C T T P T T -+ |
| 2485 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+ |
Fiona Glaser | 0d41db1 | 2015-04-21 00:05:41 +0000 | [diff] [blame] | 2486 | { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2487 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt | |
| 2488 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI | |
| 2489 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI | |
| 2490 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp |
| 2491 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP | |
Ahmed Bougacha | 0ea9d1e | 2015-05-29 00:04:30 +0000 | [diff] [blame] | 2492 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2493 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt | |
| 2494 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt | |
| 2495 | { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr | |
| 2496 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast | |
| 2497 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+ |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2498 | }; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2499 | |
Sanjay Patel | 93f55dd | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2500 | // TODO: This logic could be encoded into the table above and handled in the |
| 2501 | // switch below. |
Chris Lattner | 25eea4d | 2010-07-12 01:19:22 +0000 | [diff] [blame] | 2502 | // If either of the casts are a bitcast from scalar to vector, disallow the |
Sanjay Patel | 93f55dd | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2503 | // merging. However, any pair of bitcasts are allowed. |
| 2504 | bool IsFirstBitcast = (firstOp == Instruction::BitCast); |
| 2505 | bool IsSecondBitcast = (secondOp == Instruction::BitCast); |
| 2506 | bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast; |
Nadav Rotem | 5fc81ff | 2011-08-29 19:58:36 +0000 | [diff] [blame] | 2507 | |
Sanjay Patel | 93f55dd | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2508 | // Check if any of the casts convert scalars <-> vectors. |
| 2509 | if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || |
| 2510 | (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) |
| 2511 | if (!AreBothBitcasts) |
| 2512 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2513 | |
| 2514 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 2515 | [secondOp-Instruction::CastOpsBegin]; |
| 2516 | switch (ElimCase) { |
| 2517 | case 0: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2518 | // Categorically disallowed. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2519 | return 0; |
| 2520 | case 1: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2521 | // Allowed, use first cast's opcode. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2522 | return firstOp; |
| 2523 | case 2: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2524 | // Allowed, use second cast's opcode. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2525 | return secondOp; |
| 2526 | case 3: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2527 | // No-op cast in second op implies firstOp as long as the DestTy |
Mon P Wang | e04b456 | 2010-01-23 04:35:57 +0000 | [diff] [blame] | 2528 | // is integer and we are not converting between a vector and a |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 2529 | // non-vector type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2530 | if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2531 | return firstOp; |
| 2532 | return 0; |
| 2533 | case 4: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2534 | // No-op cast in second op implies firstOp as long as the DestTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2535 | // is floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2536 | if (DstTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2537 | return firstOp; |
| 2538 | return 0; |
| 2539 | case 5: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2540 | // No-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2541 | // is an integer. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2542 | if (SrcTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2543 | return secondOp; |
| 2544 | return 0; |
| 2545 | case 6: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2546 | // No-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2547 | // is a floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2548 | if (SrcTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2549 | return secondOp; |
| 2550 | return 0; |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2551 | case 7: { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2552 | // Cannot simplify if address spaces are different! |
| 2553 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) |
| 2554 | return 0; |
| 2555 | |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2556 | unsigned MidSize = MidTy->getScalarSizeInBits(); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2557 | // We can still fold this without knowing the actual sizes as long we |
| 2558 | // know that the intermediate pointer is the largest possible |
| 2559 | // pointer size. |
| 2560 | // FIXME: Is this always true? |
| 2561 | if (MidSize == 64) |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2562 | return Instruction::BitCast; |
| 2563 | |
| 2564 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size. |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2565 | if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2566 | return 0; |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2567 | unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2568 | if (MidSize >= PtrSize) |
| 2569 | return Instruction::BitCast; |
| 2570 | return 0; |
| 2571 | } |
| 2572 | case 8: { |
| 2573 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 2574 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 2575 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2576 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2577 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2578 | if (SrcSize == DstSize) |
| 2579 | return Instruction::BitCast; |
| 2580 | else if (SrcSize < DstSize) |
| 2581 | return firstOp; |
| 2582 | return secondOp; |
| 2583 | } |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2584 | case 9: |
| 2585 | // zext, sext -> zext, because sext can't sign extend after zext |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2586 | return Instruction::ZExt; |
| 2587 | case 10: |
| 2588 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 2589 | // the same as the original, in which case its just a bitcast |
| 2590 | if (SrcTy == DstTy) |
| 2591 | return Instruction::BitCast; |
| 2592 | return 0; // If the types are not the same we can't eliminate it. |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2593 | case 11: { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2594 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2595 | if (!MidIntPtrTy) |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2596 | return 0; |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2597 | unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2598 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2599 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2600 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 2601 | return Instruction::BitCast; |
| 2602 | return 0; |
| 2603 | } |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2604 | case 12: { |
| 2605 | // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS |
| 2606 | // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS |
| 2607 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) |
| 2608 | return Instruction::AddrSpaceCast; |
| 2609 | return Instruction::BitCast; |
| 2610 | } |
| 2611 | case 13: |
| 2612 | // FIXME: this state can be merged with (1), but the following assert |
| 2613 | // is useful to check the correcteness of the sequence due to semantic |
| 2614 | // change of bitcast. |
| 2615 | assert( |
| 2616 | SrcTy->isPtrOrPtrVectorTy() && |
| 2617 | MidTy->isPtrOrPtrVectorTy() && |
| 2618 | DstTy->isPtrOrPtrVectorTy() && |
| 2619 | SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() && |
| 2620 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && |
| 2621 | "Illegal addrspacecast, bitcast sequence!"); |
| 2622 | // Allowed, use first cast's opcode |
| 2623 | return firstOp; |
| 2624 | case 14: |
Jingyue Wu | 77145d9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 2625 | // bitcast, addrspacecast -> addrspacecast if the element type of |
| 2626 | // bitcast's source is the same as that of addrspacecast's destination. |
Peter Collingbourne | 9d5fd4d | 2016-11-13 06:58:45 +0000 | [diff] [blame] | 2627 | if (SrcTy->getScalarType()->getPointerElementType() == |
| 2628 | DstTy->getScalarType()->getPointerElementType()) |
Jingyue Wu | 77145d9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 2629 | return Instruction::AddrSpaceCast; |
| 2630 | return 0; |
| 2631 | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2632 | case 15: |
| 2633 | // FIXME: this state can be merged with (1), but the following assert |
| 2634 | // is useful to check the correcteness of the sequence due to semantic |
| 2635 | // change of bitcast. |
| 2636 | assert( |
| 2637 | SrcTy->isIntOrIntVectorTy() && |
| 2638 | MidTy->isPtrOrPtrVectorTy() && |
| 2639 | DstTy->isPtrOrPtrVectorTy() && |
| 2640 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && |
| 2641 | "Illegal inttoptr, bitcast sequence!"); |
| 2642 | // Allowed, use first cast's opcode |
| 2643 | return firstOp; |
| 2644 | case 16: |
| 2645 | // FIXME: this state can be merged with (2), but the following assert |
| 2646 | // is useful to check the correcteness of the sequence due to semantic |
| 2647 | // change of bitcast. |
| 2648 | assert( |
| 2649 | SrcTy->isPtrOrPtrVectorTy() && |
| 2650 | MidTy->isPtrOrPtrVectorTy() && |
| 2651 | DstTy->isIntOrIntVectorTy() && |
| 2652 | SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() && |
| 2653 | "Illegal bitcast, ptrtoint sequence!"); |
| 2654 | // Allowed, use second cast's opcode |
| 2655 | return secondOp; |
Fiona Glaser | 0d41db1 | 2015-04-21 00:05:41 +0000 | [diff] [blame] | 2656 | case 17: |
| 2657 | // (sitofp (zext x)) -> (uitofp x) |
| 2658 | return Instruction::UIToFP; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2659 | case 99: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2660 | // Cast combination can't happen (error in input). This is for all cases |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2661 | // where the MidTy is not the same for the two cast instructions. |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2662 | llvm_unreachable("Invalid Cast Combination"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2663 | default: |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2664 | llvm_unreachable("Error in CastResults table!!!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2665 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2668 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2669 | const Twine &Name, Instruction *InsertBefore) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2670 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2671 | // Construct and return the appropriate CastInst subclass |
| 2672 | switch (op) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2673 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 2674 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 2675 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 2676 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 2677 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 2678 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 2679 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 2680 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 2681 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 2682 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 2683 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 2684 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 2685 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore); |
| 2686 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2687 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2690 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2691 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2692 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2693 | // Construct and return the appropriate CastInst subclass |
| 2694 | switch (op) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2695 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 2696 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 2697 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 2698 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 2699 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 2700 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2701 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2702 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 2703 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 2704 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 2705 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 2706 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 2707 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd); |
| 2708 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2709 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2712 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2713 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2714 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2715 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2716 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2717 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2720 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2721 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2722 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2723 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2724 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2725 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2728 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2729 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2730 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2731 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2732 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2733 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2736 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2737 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2738 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2739 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2740 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2741 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2744 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2745 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2746 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2747 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2748 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2749 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2752 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2753 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2754 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2755 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2756 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2757 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2760 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2761 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2762 | BasicBlock *InsertAtEnd) { |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2763 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2764 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
| 2765 | "Invalid cast"); |
| 2766 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); |
Richard Trieu | 8dc4323 | 2013-07-31 04:07:28 +0000 | [diff] [blame] | 2767 | assert((!Ty->isVectorTy() || |
| 2768 | Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2769 | "Invalid cast"); |
| 2770 | |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2771 | if (Ty->isIntOrIntVectorTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2772 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2773 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2774 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
| 2777 | /// @brief Create a BitCast or a PtrToInt cast instruction |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2778 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
| 2779 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2780 | Instruction *InsertBefore) { |
Evgeniy Stepanov | c9bd35b | 2013-01-15 16:43:00 +0000 | [diff] [blame] | 2781 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2782 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2783 | "Invalid cast"); |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2784 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); |
Richard Trieu | 8dc4323 | 2013-07-31 04:07:28 +0000 | [diff] [blame] | 2785 | assert((!Ty->isVectorTy() || |
| 2786 | Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2787 | "Invalid cast"); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2788 | |
Evgeniy Stepanov | c9bd35b | 2013-01-15 16:43:00 +0000 | [diff] [blame] | 2789 | if (Ty->isIntOrIntVectorTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2790 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2791 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2792 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore); |
| 2793 | } |
| 2794 | |
| 2795 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( |
| 2796 | Value *S, Type *Ty, |
| 2797 | const Twine &Name, |
| 2798 | BasicBlock *InsertAtEnd) { |
| 2799 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2800 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2801 | |
| 2802 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
| 2803 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd); |
| 2804 | |
| 2805 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2806 | } |
| 2807 | |
| 2808 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( |
| 2809 | Value *S, Type *Ty, |
| 2810 | const Twine &Name, |
| 2811 | Instruction *InsertBefore) { |
| 2812 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2813 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2814 | |
| 2815 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2816 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore); |
| 2817 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2818 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2821 | CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, |
| 2822 | const Twine &Name, |
| 2823 | Instruction *InsertBefore) { |
| 2824 | if (S->getType()->isPointerTy() && Ty->isIntegerTy()) |
| 2825 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 2826 | if (S->getType()->isIntegerTy() && Ty->isPointerTy()) |
| 2827 | return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); |
| 2828 | |
| 2829 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2830 | } |
| 2831 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2832 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2833 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2834 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2835 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Chris Lattner | 5370ae7 | 2010-01-10 20:21:42 +0000 | [diff] [blame] | 2836 | "Invalid integer cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2837 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2838 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2839 | Instruction::CastOps opcode = |
| 2840 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2841 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2842 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2843 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2846 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2847 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2848 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2849 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2850 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2851 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2852 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2853 | Instruction::CastOps opcode = |
| 2854 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2855 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2856 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2857 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2860 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2861 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2862 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2863 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2864 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2865 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2866 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2867 | Instruction::CastOps opcode = |
| 2868 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2869 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2870 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2873 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2874 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2875 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2876 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2877 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2878 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2879 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2880 | Instruction::CastOps opcode = |
| 2881 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2882 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2883 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2886 | // Check whether it is valid to call getCastOpcode for these types. |
| 2887 | // This routine must be kept in sync with getCastOpcode. |
| 2888 | bool CastInst::isCastable(Type *SrcTy, Type *DestTy) { |
| 2889 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2890 | return false; |
| 2891 | |
| 2892 | if (SrcTy == DestTy) |
| 2893 | return true; |
| 2894 | |
| 2895 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2896 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
| 2897 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2898 | // An element by element cast. Valid if casting the elements is valid. |
| 2899 | SrcTy = SrcVecTy->getElementType(); |
| 2900 | DestTy = DestVecTy->getElementType(); |
| 2901 | } |
| 2902 | |
| 2903 | // Get the bit sizes, we'll need these |
| 2904 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2905 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2906 | |
| 2907 | // Run through the possibilities ... |
| 2908 | if (DestTy->isIntegerTy()) { // Casting to integral |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2909 | if (SrcTy->isIntegerTy()) // Casting from integral |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2910 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2911 | if (SrcTy->isFloatingPointTy()) // Casting from floating pt |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2912 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2913 | if (SrcTy->isVectorTy()) // Casting from vector |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2914 | return DestBits == SrcBits; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2915 | // Casting from something else |
| 2916 | return SrcTy->isPointerTy(); |
| 2917 | } |
| 2918 | if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2919 | if (SrcTy->isIntegerTy()) // Casting from integral |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2920 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2921 | if (SrcTy->isFloatingPointTy()) // Casting from floating pt |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2922 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2923 | if (SrcTy->isVectorTy()) // Casting from vector |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2924 | return DestBits == SrcBits; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2925 | // Casting from something else |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2926 | return false; |
| 2927 | } |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2928 | if (DestTy->isVectorTy()) // Casting to vector |
| 2929 | return DestBits == SrcBits; |
| 2930 | if (DestTy->isPointerTy()) { // Casting to pointer |
| 2931 | if (SrcTy->isPointerTy()) // Casting from pointer |
| 2932 | return true; |
| 2933 | return SrcTy->isIntegerTy(); // Casting from integral |
| 2934 | } |
| 2935 | if (DestTy->isX86_MMXTy()) { |
| 2936 | if (SrcTy->isVectorTy()) |
| 2937 | return DestBits == SrcBits; // 64-bit vector to MMX |
| 2938 | return false; |
| 2939 | } // Casting to something else |
| 2940 | return false; |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2943 | bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { |
| 2944 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2945 | return false; |
| 2946 | |
| 2947 | if (SrcTy == DestTy) |
| 2948 | return true; |
| 2949 | |
| 2950 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 2951 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) { |
| 2952 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2953 | // An element by element cast. Valid if casting the elements is valid. |
| 2954 | SrcTy = SrcVecTy->getElementType(); |
| 2955 | DestTy = DestVecTy->getElementType(); |
| 2956 | } |
| 2957 | } |
| 2958 | } |
| 2959 | |
| 2960 | if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) { |
| 2961 | if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) { |
| 2962 | return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); |
| 2963 | } |
| 2964 | } |
| 2965 | |
| 2966 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2967 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2968 | |
| 2969 | // Could still have vectors of pointers if the number of elements doesn't |
| 2970 | // match |
| 2971 | if (SrcBits == 0 || DestBits == 0) |
| 2972 | return false; |
| 2973 | |
| 2974 | if (SrcBits != DestBits) |
| 2975 | return false; |
| 2976 | |
| 2977 | if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) |
| 2978 | return false; |
| 2979 | |
| 2980 | return true; |
| 2981 | } |
| 2982 | |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2983 | bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2984 | const DataLayout &DL) { |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2985 | if (auto *PtrTy = dyn_cast<PointerType>(SrcTy)) |
| 2986 | if (auto *IntTy = dyn_cast<IntegerType>(DestTy)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2987 | return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy); |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2988 | if (auto *PtrTy = dyn_cast<PointerType>(DestTy)) |
| 2989 | if (auto *IntTy = dyn_cast<IntegerType>(SrcTy)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2990 | return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy); |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2991 | |
| 2992 | return isBitCastable(SrcTy, DestTy); |
| 2993 | } |
| 2994 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2995 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 2996 | // types and size of the operand. This, basically, is a parallel of the |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2997 | // logic in the castIsValid function below. This axiom should hold: |
| 2998 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 2999 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3000 | // casting opcode for the arguments passed to it. |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 3001 | // This routine must be kept in sync with isCastable. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3002 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 3003 | CastInst::getCastOpcode( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3004 | const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { |
| 3005 | Type *SrcTy = Src->getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3006 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 3007 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && |
| 3008 | "Only first class types are castable!"); |
| 3009 | |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 3010 | if (SrcTy == DestTy) |
| 3011 | return BitCast; |
| 3012 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 3013 | // FIXME: Check address space sizes here |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3014 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 3015 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 3016 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 3017 | // An element by element cast. Find the appropriate opcode based on the |
| 3018 | // element types. |
| 3019 | SrcTy = SrcVecTy->getElementType(); |
| 3020 | DestTy = DestVecTy->getElementType(); |
| 3021 | } |
| 3022 | |
| 3023 | // Get the bit sizes, we'll need these |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3024 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 3025 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 3026 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3027 | // Run through the possibilities ... |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3028 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 3029 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3030 | if (DestBits < SrcBits) |
| 3031 | return Trunc; // int -> smaller int |
| 3032 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 3033 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3034 | return SExt; // signed -> SEXT |
| 3035 | else |
| 3036 | return ZExt; // unsigned -> ZEXT |
| 3037 | } else { |
| 3038 | return BitCast; // Same size, No-op cast |
| 3039 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3040 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 3041 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3042 | return FPToSI; // FP -> sint |
| 3043 | else |
| 3044 | return FPToUI; // FP -> uint |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 3045 | } else if (SrcTy->isVectorTy()) { |
| 3046 | assert(DestBits == SrcBits && |
| 3047 | "Casting vector to integer of different width"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3048 | return BitCast; // Same size, no-op cast |
| 3049 | } else { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3050 | assert(SrcTy->isPointerTy() && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3051 | "Casting from a value that is not first-class type"); |
| 3052 | return PtrToInt; // ptr -> int |
| 3053 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3054 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 3055 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 3056 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3057 | return SIToFP; // sint -> FP |
| 3058 | else |
| 3059 | return UIToFP; // uint -> FP |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3060 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3061 | if (DestBits < SrcBits) { |
| 3062 | return FPTrunc; // FP -> smaller FP |
| 3063 | } else if (DestBits > SrcBits) { |
| 3064 | return FPExt; // FP -> larger FP |
| 3065 | } else { |
| 3066 | return BitCast; // same size, no-op cast |
| 3067 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 3068 | } else if (SrcTy->isVectorTy()) { |
| 3069 | assert(DestBits == SrcBits && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 3070 | "Casting vector to floating point of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 3071 | return BitCast; // same size, no-op cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3072 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3073 | llvm_unreachable("Casting pointer or non-first class to float"); |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 3074 | } else if (DestTy->isVectorTy()) { |
| 3075 | assert(DestBits == SrcBits && |
| 3076 | "Illegal cast to vector (wrong type or size)"); |
| 3077 | return BitCast; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3078 | } else if (DestTy->isPointerTy()) { |
| 3079 | if (SrcTy->isPointerTy()) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3080 | if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace()) |
| 3081 | return AddrSpaceCast; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3082 | return BitCast; // ptr -> ptr |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3083 | } else if (SrcTy->isIntegerTy()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3084 | return IntToPtr; // int -> ptr |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3085 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3086 | llvm_unreachable("Casting pointer to other than pointer or int"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 3087 | } else if (DestTy->isX86_MMXTy()) { |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 3088 | if (SrcTy->isVectorTy()) { |
| 3089 | assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 3090 | return BitCast; // 64-bit vector to MMX |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 3091 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3092 | llvm_unreachable("Illegal cast to X86_MMX"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3093 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3094 | llvm_unreachable("Casting to type that is not first-class"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | //===----------------------------------------------------------------------===// |
| 3098 | // CastInst SubClass Constructors |
| 3099 | //===----------------------------------------------------------------------===// |
| 3100 | |
| 3101 | /// Check that the construction parameters for a CastInst are correct. This |
| 3102 | /// could be broken out into the separate constructors but it is useful to have |
| 3103 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 3104 | /// of the types involved. |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3105 | bool |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3106 | CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3107 | |
| 3108 | // Check for type sanity on the arguments |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3109 | Type *SrcTy = S->getType(); |
Evan Cheng | 098d7b7 | 2013-01-10 23:22:53 +0000 | [diff] [blame] | 3110 | |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 3111 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || |
| 3112 | SrcTy->isAggregateType() || DstTy->isAggregateType()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3113 | return false; |
| 3114 | |
| 3115 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3116 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 3117 | unsigned DstBitSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3118 | |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3119 | // If these are vector types, get the lengths of the vectors (using zero for |
| 3120 | // scalar types means that checking that vector lengths match also checks that |
| 3121 | // scalars are not being converted to vectors or vectors to scalars). |
| 3122 | unsigned SrcLength = SrcTy->isVectorTy() ? |
| 3123 | cast<VectorType>(SrcTy)->getNumElements() : 0; |
| 3124 | unsigned DstLength = DstTy->isVectorTy() ? |
| 3125 | cast<VectorType>(DstTy)->getNumElements() : 0; |
| 3126 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3127 | // Switch on the opcode provided |
| 3128 | switch (op) { |
| 3129 | default: return false; // This is an input error |
| 3130 | case Instruction::Trunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3131 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3132 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3133 | case Instruction::ZExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3134 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3135 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3136 | case Instruction::SExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3137 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3138 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3139 | case Instruction::FPTrunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3140 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3141 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3142 | case Instruction::FPExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3143 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3144 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3145 | case Instruction::UIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3146 | case Instruction::SIToFP: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3147 | return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3148 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3149 | case Instruction::FPToUI: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3150 | case Instruction::FPToSI: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3151 | return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3152 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3153 | case Instruction::PtrToInt: |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3154 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3155 | return false; |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3156 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 3157 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 3158 | return false; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3159 | return SrcTy->getScalarType()->isPointerTy() && |
| 3160 | DstTy->getScalarType()->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3161 | case Instruction::IntToPtr: |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3162 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3163 | return false; |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3164 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 3165 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 3166 | return false; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3167 | return SrcTy->getScalarType()->isIntegerTy() && |
| 3168 | DstTy->getScalarType()->isPointerTy(); |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3169 | case Instruction::BitCast: { |
| 3170 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); |
| 3171 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); |
| 3172 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3173 | // BitCast implies a no-op cast of type only. No bits change. |
| 3174 | // However, you can't cast pointers to anything but pointers. |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3175 | if (!SrcPtrTy != !DstPtrTy) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3176 | return false; |
| 3177 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 3178 | // For non-pointer cases, the cast is okay if the source and destination bit |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3179 | // widths are identical. |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3180 | if (!SrcPtrTy) |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3181 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); |
| 3182 | |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3183 | // If both are pointers then the address spaces must match. |
| 3184 | if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) |
| 3185 | return false; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3186 | |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3187 | // A vector of pointers must have the same number of elements. |
| 3188 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 3189 | if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy)) |
| 3190 | return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); |
| 3191 | |
| 3192 | return false; |
| 3193 | } |
| 3194 | |
| 3195 | return true; |
| 3196 | } |
| 3197 | case Instruction::AddrSpaceCast: { |
| 3198 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); |
| 3199 | if (!SrcPtrTy) |
| 3200 | return false; |
| 3201 | |
| 3202 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); |
| 3203 | if (!DstPtrTy) |
| 3204 | return false; |
| 3205 | |
| 3206 | if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) |
| 3207 | return false; |
| 3208 | |
| 3209 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 3210 | if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy)) |
| 3211 | return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); |
| 3212 | |
| 3213 | return false; |
| 3214 | } |
| 3215 | |
| 3216 | return true; |
| 3217 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3218 | } |
| 3219 | } |
| 3220 | |
| 3221 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3222 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3223 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3224 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3228 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3229 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3230 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3234 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3235 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3236 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3240 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3241 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3242 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3243 | } |
| 3244 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3245 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3246 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3247 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 3250 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3251 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3252 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3253 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3257 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3258 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3259 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3263 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3264 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3265 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3269 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3270 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3271 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3275 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3276 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3277 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3281 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3282 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3283 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3287 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3288 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3289 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3293 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3294 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3295 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3299 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3300 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3301 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3305 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3306 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3307 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3311 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3312 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3313 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3317 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3318 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3319 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3323 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3324 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3325 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3329 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3330 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3331 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3335 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3336 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3337 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3341 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3342 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3343 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3347 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3348 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3349 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3353 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3354 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3355 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3359 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3360 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3361 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3362 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3363 | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3364 | AddrSpaceCastInst::AddrSpaceCastInst( |
| 3365 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
| 3366 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) { |
| 3367 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); |
| 3368 | } |
| 3369 | |
| 3370 | AddrSpaceCastInst::AddrSpaceCastInst( |
| 3371 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
| 3372 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) { |
| 3373 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); |
| 3374 | } |
| 3375 | |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3376 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3377 | // CmpInst Classes |
| 3378 | //===----------------------------------------------------------------------===// |
| 3379 | |
Craig Topper | 3186c01 | 2012-09-23 02:12:10 +0000 | [diff] [blame] | 3380 | void CmpInst::anchor() {} |
Chris Lattner | aec33da | 2010-01-22 06:25:37 +0000 | [diff] [blame] | 3381 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3382 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, |
| 3383 | Value *RHS, const Twine &Name, Instruction *InsertBefore) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3384 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3385 | OperandTraits<CmpInst>::op_begin(this), |
| 3386 | OperandTraits<CmpInst>::operands(this), |
| 3387 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3388 | Op<0>() = LHS; |
| 3389 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3390 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3391 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3392 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3393 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3394 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, |
| 3395 | Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3396 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3397 | OperandTraits<CmpInst>::op_begin(this), |
| 3398 | OperandTraits<CmpInst>::operands(this), |
| 3399 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3400 | Op<0>() = LHS; |
| 3401 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3402 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3403 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | CmpInst * |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3407 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3408 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3409 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3410 | if (InsertBefore) |
| 3411 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3412 | S1, S2, Name); |
| 3413 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3414 | return new ICmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3415 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3416 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3417 | |
| 3418 | if (InsertBefore) |
| 3419 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3420 | S1, S2, Name); |
| 3421 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3422 | return new FCmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3423 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | CmpInst * |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3427 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3428 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3429 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3430 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3431 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3432 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3433 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3434 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | void CmpInst::swapOperands() { |
| 3438 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 3439 | IC->swapOperands(); |
| 3440 | else |
| 3441 | cast<FCmpInst>(this)->swapOperands(); |
| 3442 | } |
| 3443 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3444 | bool CmpInst::isCommutative() const { |
| 3445 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3446 | return IC->isCommutative(); |
| 3447 | return cast<FCmpInst>(this)->isCommutative(); |
| 3448 | } |
| 3449 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3450 | bool CmpInst::isEquality() const { |
| 3451 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3452 | return IC->isEquality(); |
| 3453 | return cast<FCmpInst>(this)->isEquality(); |
| 3454 | } |
| 3455 | |
| 3456 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3457 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3458 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3459 | default: llvm_unreachable("Unknown cmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3460 | case ICMP_EQ: return ICMP_NE; |
| 3461 | case ICMP_NE: return ICMP_EQ; |
| 3462 | case ICMP_UGT: return ICMP_ULE; |
| 3463 | case ICMP_ULT: return ICMP_UGE; |
| 3464 | case ICMP_UGE: return ICMP_ULT; |
| 3465 | case ICMP_ULE: return ICMP_UGT; |
| 3466 | case ICMP_SGT: return ICMP_SLE; |
| 3467 | case ICMP_SLT: return ICMP_SGE; |
| 3468 | case ICMP_SGE: return ICMP_SLT; |
| 3469 | case ICMP_SLE: return ICMP_SGT; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3470 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3471 | case FCMP_OEQ: return FCMP_UNE; |
| 3472 | case FCMP_ONE: return FCMP_UEQ; |
| 3473 | case FCMP_OGT: return FCMP_ULE; |
| 3474 | case FCMP_OLT: return FCMP_UGE; |
| 3475 | case FCMP_OGE: return FCMP_ULT; |
| 3476 | case FCMP_OLE: return FCMP_UGT; |
| 3477 | case FCMP_UEQ: return FCMP_ONE; |
| 3478 | case FCMP_UNE: return FCMP_OEQ; |
| 3479 | case FCMP_UGT: return FCMP_OLE; |
| 3480 | case FCMP_ULT: return FCMP_OGE; |
| 3481 | case FCMP_UGE: return FCMP_OLT; |
| 3482 | case FCMP_ULE: return FCMP_OGT; |
| 3483 | case FCMP_ORD: return FCMP_UNO; |
| 3484 | case FCMP_UNO: return FCMP_ORD; |
| 3485 | case FCMP_TRUE: return FCMP_FALSE; |
| 3486 | case FCMP_FALSE: return FCMP_TRUE; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3487 | } |
| 3488 | } |
| 3489 | |
Tim Northover | de3aea041 | 2016-08-17 20:25:25 +0000 | [diff] [blame] | 3490 | StringRef CmpInst::getPredicateName(Predicate Pred) { |
| 3491 | switch (Pred) { |
| 3492 | default: return "unknown"; |
| 3493 | case FCmpInst::FCMP_FALSE: return "false"; |
| 3494 | case FCmpInst::FCMP_OEQ: return "oeq"; |
| 3495 | case FCmpInst::FCMP_OGT: return "ogt"; |
| 3496 | case FCmpInst::FCMP_OGE: return "oge"; |
| 3497 | case FCmpInst::FCMP_OLT: return "olt"; |
| 3498 | case FCmpInst::FCMP_OLE: return "ole"; |
| 3499 | case FCmpInst::FCMP_ONE: return "one"; |
| 3500 | case FCmpInst::FCMP_ORD: return "ord"; |
| 3501 | case FCmpInst::FCMP_UNO: return "uno"; |
| 3502 | case FCmpInst::FCMP_UEQ: return "ueq"; |
| 3503 | case FCmpInst::FCMP_UGT: return "ugt"; |
| 3504 | case FCmpInst::FCMP_UGE: return "uge"; |
| 3505 | case FCmpInst::FCMP_ULT: return "ult"; |
| 3506 | case FCmpInst::FCMP_ULE: return "ule"; |
| 3507 | case FCmpInst::FCMP_UNE: return "une"; |
| 3508 | case FCmpInst::FCMP_TRUE: return "true"; |
| 3509 | case ICmpInst::ICMP_EQ: return "eq"; |
| 3510 | case ICmpInst::ICMP_NE: return "ne"; |
| 3511 | case ICmpInst::ICMP_SGT: return "sgt"; |
| 3512 | case ICmpInst::ICMP_SGE: return "sge"; |
| 3513 | case ICmpInst::ICMP_SLT: return "slt"; |
| 3514 | case ICmpInst::ICMP_SLE: return "sle"; |
| 3515 | case ICmpInst::ICMP_UGT: return "ugt"; |
| 3516 | case ICmpInst::ICMP_UGE: return "uge"; |
| 3517 | case ICmpInst::ICMP_ULT: return "ult"; |
| 3518 | case ICmpInst::ICMP_ULE: return "ule"; |
| 3519 | } |
| 3520 | } |
| 3521 | |
Pete Cooper | 1d07869 | 2015-12-14 20:29:16 +0000 | [diff] [blame] | 3522 | void ICmpInst::anchor() {} |
| 3523 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3524 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 3525 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3526 | default: llvm_unreachable("Unknown icmp predicate!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3527 | case ICMP_EQ: case ICMP_NE: |
| 3528 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 3529 | return pred; |
| 3530 | case ICMP_UGT: return ICMP_SGT; |
| 3531 | case ICMP_ULT: return ICMP_SLT; |
| 3532 | case ICMP_UGE: return ICMP_SGE; |
| 3533 | case ICMP_ULE: return ICMP_SLE; |
| 3534 | } |
| 3535 | } |
| 3536 | |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3537 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { |
| 3538 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3539 | default: llvm_unreachable("Unknown icmp predicate!"); |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3540 | case ICMP_EQ: case ICMP_NE: |
| 3541 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: |
| 3542 | return pred; |
| 3543 | case ICMP_SGT: return ICMP_UGT; |
| 3544 | case ICMP_SLT: return ICMP_ULT; |
| 3545 | case ICMP_SGE: return ICMP_UGE; |
| 3546 | case ICMP_SLE: return ICMP_ULE; |
| 3547 | } |
| 3548 | } |
| 3549 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3550 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3551 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3552 | default: llvm_unreachable("Unknown cmp predicate!"); |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3553 | case ICMP_EQ: case ICMP_NE: |
| 3554 | return pred; |
| 3555 | case ICMP_SGT: return ICMP_SLT; |
| 3556 | case ICMP_SLT: return ICMP_SGT; |
| 3557 | case ICMP_SGE: return ICMP_SLE; |
| 3558 | case ICMP_SLE: return ICMP_SGE; |
| 3559 | case ICMP_UGT: return ICMP_ULT; |
| 3560 | case ICMP_ULT: return ICMP_UGT; |
| 3561 | case ICMP_UGE: return ICMP_ULE; |
| 3562 | case ICMP_ULE: return ICMP_UGE; |
| 3563 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3564 | case FCMP_FALSE: case FCMP_TRUE: |
| 3565 | case FCMP_OEQ: case FCMP_ONE: |
| 3566 | case FCMP_UEQ: case FCMP_UNE: |
| 3567 | case FCMP_ORD: case FCMP_UNO: |
| 3568 | return pred; |
| 3569 | case FCMP_OGT: return FCMP_OLT; |
| 3570 | case FCMP_OLT: return FCMP_OGT; |
| 3571 | case FCMP_OGE: return FCMP_OLE; |
| 3572 | case FCMP_OLE: return FCMP_OGE; |
| 3573 | case FCMP_UGT: return FCMP_ULT; |
| 3574 | case FCMP_ULT: return FCMP_UGT; |
| 3575 | case FCMP_UGE: return FCMP_ULE; |
| 3576 | case FCMP_ULE: return FCMP_UGE; |
| 3577 | } |
| 3578 | } |
| 3579 | |
Sanjoy Das | 6e78b17 | 2015-10-22 19:57:34 +0000 | [diff] [blame] | 3580 | CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) { |
| 3581 | assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!"); |
| 3582 | |
| 3583 | switch (pred) { |
| 3584 | default: |
| 3585 | llvm_unreachable("Unknown predicate!"); |
| 3586 | case CmpInst::ICMP_ULT: |
| 3587 | return CmpInst::ICMP_SLT; |
| 3588 | case CmpInst::ICMP_ULE: |
| 3589 | return CmpInst::ICMP_SLE; |
| 3590 | case CmpInst::ICMP_UGT: |
| 3591 | return CmpInst::ICMP_SGT; |
| 3592 | case CmpInst::ICMP_UGE: |
| 3593 | return CmpInst::ICMP_SGE; |
| 3594 | } |
| 3595 | } |
| 3596 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3597 | bool CmpInst::isUnsigned(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3598 | switch (predicate) { |
| 3599 | default: return false; |
| 3600 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 3601 | case ICmpInst::ICMP_UGE: return true; |
| 3602 | } |
| 3603 | } |
| 3604 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3605 | bool CmpInst::isSigned(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3606 | switch (predicate) { |
| 3607 | default: return false; |
| 3608 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 3609 | case ICmpInst::ICMP_SGE: return true; |
| 3610 | } |
| 3611 | } |
| 3612 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3613 | bool CmpInst::isOrdered(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3614 | switch (predicate) { |
| 3615 | default: return false; |
| 3616 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 3617 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 3618 | case FCmpInst::FCMP_ORD: return true; |
| 3619 | } |
| 3620 | } |
| 3621 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3622 | bool CmpInst::isUnordered(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3623 | switch (predicate) { |
| 3624 | default: return false; |
| 3625 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 3626 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 3627 | case FCmpInst::FCMP_UNO: return true; |
| 3628 | } |
| 3629 | } |
| 3630 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3631 | bool CmpInst::isTrueWhenEqual(Predicate predicate) { |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3632 | switch(predicate) { |
| 3633 | default: return false; |
| 3634 | case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: |
| 3635 | case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; |
| 3636 | } |
| 3637 | } |
| 3638 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3639 | bool CmpInst::isFalseWhenEqual(Predicate predicate) { |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3640 | switch(predicate) { |
| 3641 | case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: |
| 3642 | case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; |
| 3643 | default: return false; |
| 3644 | } |
| 3645 | } |
| 3646 | |
Chad Rosier | 99bc480 | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 3647 | bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) { |
Chad Rosier | af83e40 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3648 | // If the predicates match, then we know the first condition implies the |
| 3649 | // second is true. |
| 3650 | if (Pred1 == Pred2) |
| 3651 | return true; |
| 3652 | |
| 3653 | switch (Pred1) { |
| 3654 | default: |
| 3655 | break; |
Chad Rosier | 1a60159 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3656 | case ICMP_EQ: |
Chad Rosier | 3d75f8c | 2016-04-25 13:25:14 +0000 | [diff] [blame] | 3657 | // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true. |
Chad Rosier | 1a60159 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3658 | return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE || |
| 3659 | Pred2 == ICMP_SLE; |
Chad Rosier | 3456cb5 | 2016-04-22 17:14:12 +0000 | [diff] [blame] | 3660 | case ICMP_UGT: // A >u B implies A != B and A >=u B are true. |
| 3661 | return Pred2 == ICMP_NE || Pred2 == ICMP_UGE; |
| 3662 | case ICMP_ULT: // A <u B implies A != B and A <=u B are true. |
| 3663 | return Pred2 == ICMP_NE || Pred2 == ICMP_ULE; |
| 3664 | case ICMP_SGT: // A >s B implies A != B and A >=s B are true. |
| 3665 | return Pred2 == ICMP_NE || Pred2 == ICMP_SGE; |
| 3666 | case ICMP_SLT: // A <s B implies A != B and A <=s B are true. |
| 3667 | return Pred2 == ICMP_NE || Pred2 == ICMP_SLE; |
Chad Rosier | af83e40 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3668 | } |
| 3669 | return false; |
| 3670 | } |
| 3671 | |
Chad Rosier | 99bc480 | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 3672 | bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) { |
Chad Rosier | 1a60159 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3673 | return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2)); |
Chad Rosier | af83e40 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3674 | } |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3675 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3676 | //===----------------------------------------------------------------------===// |
| 3677 | // SwitchInst Implementation |
| 3678 | //===----------------------------------------------------------------------===// |
| 3679 | |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3680 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { |
| 3681 | assert(Value && Default && NumReserved); |
| 3682 | ReservedSpace = NumReserved; |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3683 | setNumHungOffUseOperands(2); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3684 | allocHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3685 | |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 3686 | Op<0>() = Value; |
| 3687 | Op<1>() = Default; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3690 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3691 | /// switch on and a default destination. The number of additional cases can |
| 3692 | /// be specified here to make memory allocation more efficient. This |
| 3693 | /// constructor can also autoinsert before another instruction. |
| 3694 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3695 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3696 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3697 | nullptr, 0, InsertBefore) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3698 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3702 | /// switch on and a default destination. The number of additional cases can |
| 3703 | /// be specified here to make memory allocation more efficient. This |
| 3704 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 3705 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3706 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3707 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3708 | nullptr, 0, InsertAtEnd) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3709 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3712 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3713 | : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3714 | init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3715 | setNumHungOffUseOperands(SI.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3716 | Use *OL = getOperandList(); |
| 3717 | const Use *InOL = SI.getOperandList(); |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3718 | for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3719 | OL[i] = InOL[i]; |
| 3720 | OL[i+1] = InOL[i+1]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3721 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3722 | SubclassOptionalData = SI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3725 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3726 | /// addCase - Add an entry to the switch instruction... |
| 3727 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 3728 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3729 | unsigned NewCaseIdx = getNumCases(); |
| 3730 | unsigned OpNo = getNumOperands(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3731 | if (OpNo+2 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3732 | growOperands(); // Get more space! |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3733 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 3734 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3735 | setNumHungOffUseOperands(OpNo+2); |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 3736 | CaseHandle Case(this, NewCaseIdx); |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3737 | Case.setValue(OnVal); |
Stepan Dyatkovskiy | 5b648af | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 3738 | Case.setSuccessor(Dest); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3741 | /// removeCase - This method removes the specified case and its successor |
| 3742 | /// from the switch instruction. |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 3743 | SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) { |
| 3744 | unsigned idx = I->getCaseIndex(); |
| 3745 | |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3746 | assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3747 | |
| 3748 | unsigned NumOps = getNumOperands(); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3749 | Use *OL = getOperandList(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3750 | |
Jay Foad | 1427772 | 2011-02-01 09:22:34 +0000 | [diff] [blame] | 3751 | // Overwrite this case with the end of the list. |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3752 | if (2 + (idx + 1) * 2 != NumOps) { |
| 3753 | OL[2 + idx * 2] = OL[NumOps - 2]; |
| 3754 | OL[2 + idx * 2 + 1] = OL[NumOps - 1]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3758 | OL[NumOps-2].set(nullptr); |
| 3759 | OL[NumOps-2+1].set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3760 | setNumHungOffUseOperands(NumOps-2); |
Chandler Carruth | 0d256c0 | 2017-03-26 02:49:23 +0000 | [diff] [blame] | 3761 | |
| 3762 | return CaseIt(this, idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3765 | /// growOperands - grow operands - This grows the operand list in response |
| 3766 | /// to a push_back style of operation. This grows the number of ops by 3 times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3767 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3768 | void SwitchInst::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3769 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3770 | unsigned NumOps = e*3; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3771 | |
| 3772 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 3773 | growHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3774 | } |
| 3775 | |
| 3776 | |
| 3777 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 3778 | return getSuccessor(idx); |
| 3779 | } |
| 3780 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 3781 | return getNumSuccessors(); |
| 3782 | } |
| 3783 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 3784 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3785 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3786 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3787 | //===----------------------------------------------------------------------===// |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3788 | // IndirectBrInst Implementation |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3789 | //===----------------------------------------------------------------------===// |
| 3790 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3791 | void IndirectBrInst::init(Value *Address, unsigned NumDests) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3792 | assert(Address && Address->getType()->isPointerTy() && |
Chris Lattner | 6747b4c | 2009-10-29 05:53:32 +0000 | [diff] [blame] | 3793 | "Address of indirectbr must be a pointer"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3794 | ReservedSpace = 1+NumDests; |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3795 | setNumHungOffUseOperands(1); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3796 | allocHungoffUses(ReservedSpace); |
| 3797 | |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 3798 | Op<0>() = Address; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3802 | /// growOperands - grow operands - This grows the operand list in response |
| 3803 | /// to a push_back style of operation. This grows the number of ops by 2 times. |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3804 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3805 | void IndirectBrInst::growOperands() { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3806 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3807 | unsigned NumOps = e*2; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3808 | |
| 3809 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 3810 | growHungoffUses(ReservedSpace); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3813 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3814 | Instruction *InsertBefore) |
| 3815 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3816 | nullptr, 0, InsertBefore) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3817 | init(Address, NumCases); |
| 3818 | } |
| 3819 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3820 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3821 | BasicBlock *InsertAtEnd) |
| 3822 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3823 | nullptr, 0, InsertAtEnd) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3824 | init(Address, NumCases); |
| 3825 | } |
| 3826 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3827 | IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3828 | : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, |
| 3829 | nullptr, IBI.getNumOperands()) { |
| 3830 | allocHungoffUses(IBI.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3831 | Use *OL = getOperandList(); |
| 3832 | const Use *InOL = IBI.getOperandList(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3833 | for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) |
| 3834 | OL[i] = InOL[i]; |
| 3835 | SubclassOptionalData = IBI.SubclassOptionalData; |
| 3836 | } |
| 3837 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3838 | /// addDestination - Add a destination. |
| 3839 | /// |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3840 | void IndirectBrInst::addDestination(BasicBlock *DestBB) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3841 | unsigned OpNo = getNumOperands(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3842 | if (OpNo+1 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3843 | growOperands(); // Get more space! |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3844 | // Initialize some new operands. |
| 3845 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3846 | setNumHungOffUseOperands(OpNo+1); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3847 | getOperandList()[OpNo] = DestBB; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
| 3850 | /// removeDestination - This method removes the specified successor from the |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3851 | /// indirectbr instruction. |
| 3852 | void IndirectBrInst::removeDestination(unsigned idx) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3853 | assert(idx < getNumOperands()-1 && "Successor index out of range!"); |
| 3854 | |
| 3855 | unsigned NumOps = getNumOperands(); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3856 | Use *OL = getOperandList(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3857 | |
| 3858 | // Replace this value with the last one. |
| 3859 | OL[idx+1] = OL[NumOps-1]; |
| 3860 | |
| 3861 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3862 | OL[NumOps-1].set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3863 | setNumHungOffUseOperands(NumOps-1); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3864 | } |
| 3865 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3866 | BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3867 | return getSuccessor(idx); |
| 3868 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3869 | unsigned IndirectBrInst::getNumSuccessorsV() const { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3870 | return getNumSuccessors(); |
| 3871 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3872 | void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3873 | setSuccessor(idx, B); |
| 3874 | } |
| 3875 | |
| 3876 | //===----------------------------------------------------------------------===// |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3877 | // cloneImpl() implementations |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3878 | //===----------------------------------------------------------------------===// |
| 3879 | |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3880 | // Define these methods here so vtables don't get emitted into every translation |
| 3881 | // unit that uses these classes. |
| 3882 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3883 | GetElementPtrInst *GetElementPtrInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3884 | return new (getNumOperands()) GetElementPtrInst(*this); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3887 | BinaryOperator *BinaryOperator::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3888 | return Create(getOpcode(), Op<0>(), Op<1>()); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3891 | FCmpInst *FCmpInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3892 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3895 | ICmpInst *ICmpInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3896 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 3897 | } |
| 3898 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3899 | ExtractValueInst *ExtractValueInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3900 | return new ExtractValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3903 | InsertValueInst *InsertValueInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3904 | return new InsertValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3907 | AllocaInst *AllocaInst::cloneImpl() const { |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3908 | AllocaInst *Result = new AllocaInst(getAllocatedType(), |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 3909 | getType()->getAddressSpace(), |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3910 | (Value *)getOperand(0), getAlignment()); |
| 3911 | Result->setUsedWithInAlloca(isUsedWithInAlloca()); |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 3912 | Result->setSwiftError(isSwiftError()); |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3913 | return Result; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3916 | LoadInst *LoadInst::cloneImpl() const { |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3917 | return new LoadInst(getOperand(0), Twine(), isVolatile(), |
| 3918 | getAlignment(), getOrdering(), getSynchScope()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3921 | StoreInst *StoreInst::cloneImpl() const { |
Eli Friedman | cad9f2a | 2011-08-10 17:39:11 +0000 | [diff] [blame] | 3922 | return new StoreInst(getOperand(0), getOperand(1), isVolatile(), |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3923 | getAlignment(), getOrdering(), getSynchScope()); |
| 3924 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3927 | AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3928 | AtomicCmpXchgInst *Result = |
| 3929 | new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2), |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 3930 | getSuccessOrdering(), getFailureOrdering(), |
| 3931 | getSynchScope()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3932 | Result->setVolatile(isVolatile()); |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 3933 | Result->setWeak(isWeak()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3934 | return Result; |
| 3935 | } |
| 3936 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3937 | AtomicRMWInst *AtomicRMWInst::cloneImpl() const { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3938 | AtomicRMWInst *Result = |
| 3939 | new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1), |
| 3940 | getOrdering(), getSynchScope()); |
| 3941 | Result->setVolatile(isVolatile()); |
| 3942 | return Result; |
| 3943 | } |
| 3944 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3945 | FenceInst *FenceInst::cloneImpl() const { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3946 | return new FenceInst(getContext(), getOrdering(), getSynchScope()); |
| 3947 | } |
| 3948 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3949 | TruncInst *TruncInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3950 | return new TruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3953 | ZExtInst *ZExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3954 | return new ZExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3957 | SExtInst *SExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3958 | return new SExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3961 | FPTruncInst *FPTruncInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3962 | return new FPTruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3965 | FPExtInst *FPExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3966 | return new FPExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3969 | UIToFPInst *UIToFPInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3970 | return new UIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3973 | SIToFPInst *SIToFPInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3974 | return new SIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3977 | FPToUIInst *FPToUIInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3978 | return new FPToUIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3981 | FPToSIInst *FPToSIInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3982 | return new FPToSIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3985 | PtrToIntInst *PtrToIntInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3986 | return new PtrToIntInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3989 | IntToPtrInst *IntToPtrInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3990 | return new IntToPtrInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3991 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3992 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3993 | BitCastInst *BitCastInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3994 | return new BitCastInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3995 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3996 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3997 | AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3998 | return new AddrSpaceCastInst(getOperand(0), getType()); |
| 3999 | } |
| 4000 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4001 | CallInst *CallInst::cloneImpl() const { |
Sanjoy Das | bd1c1bf | 2015-11-10 20:13:21 +0000 | [diff] [blame] | 4002 | if (hasOperandBundles()) { |
| 4003 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); |
| 4004 | return new(getNumOperands(), DescriptorBytes) CallInst(*this); |
| 4005 | } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4006 | return new(getNumOperands()) CallInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4009 | SelectInst *SelectInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4010 | return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 4011 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4012 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4013 | VAArgInst *VAArgInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4014 | return new VAArgInst(getOperand(0), getType()); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 4015 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4016 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4017 | ExtractElementInst *ExtractElementInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4018 | return ExtractElementInst::Create(getOperand(0), getOperand(1)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 4019 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4020 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4021 | InsertElementInst *InsertElementInst::cloneImpl() const { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 4022 | return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4025 | ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 4026 | return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2)); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 4027 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4028 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4029 | PHINode *PHINode::cloneImpl() const { return new PHINode(*this); } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4030 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4031 | LandingPadInst *LandingPadInst::cloneImpl() const { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4032 | return new LandingPadInst(*this); |
| 4033 | } |
| 4034 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4035 | ReturnInst *ReturnInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4036 | return new(getNumOperands()) ReturnInst(*this); |
| 4037 | } |
| 4038 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4039 | BranchInst *BranchInst::cloneImpl() const { |
Jay Foad | d81f3c9 | 2011-01-07 20:29:02 +0000 | [diff] [blame] | 4040 | return new(getNumOperands()) BranchInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 4041 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4042 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4043 | SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4044 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4045 | IndirectBrInst *IndirectBrInst::cloneImpl() const { |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 4046 | return new IndirectBrInst(*this); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4049 | InvokeInst *InvokeInst::cloneImpl() const { |
Sanjoy Das | bd1c1bf | 2015-11-10 20:13:21 +0000 | [diff] [blame] | 4050 | if (hasOperandBundles()) { |
| 4051 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); |
| 4052 | return new(getNumOperands(), DescriptorBytes) InvokeInst(*this); |
| 4053 | } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4054 | return new(getNumOperands()) InvokeInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 4055 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4056 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4057 | ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); } |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4058 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4059 | CleanupReturnInst *CleanupReturnInst::cloneImpl() const { |
| 4060 | return new (getNumOperands()) CleanupReturnInst(*this); |
| 4061 | } |
| 4062 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4063 | CatchReturnInst *CatchReturnInst::cloneImpl() const { |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 4064 | return new (getNumOperands()) CatchReturnInst(*this); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4065 | } |
| 4066 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4067 | CatchSwitchInst *CatchSwitchInst::cloneImpl() const { |
| 4068 | return new CatchSwitchInst(*this); |
| 4069 | } |
| 4070 | |
| 4071 | FuncletPadInst *FuncletPadInst::cloneImpl() const { |
| 4072 | return new (getNumOperands()) FuncletPadInst(*this); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4075 | UnreachableInst *UnreachableInst::cloneImpl() const { |
Nick Lewycky | 42fb745 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 4076 | LLVMContext &Context = getContext(); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4077 | return new UnreachableInst(Context); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4078 | } |