Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 1 | //===- Instructions.cpp - Implement the LLVM instructions -----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 9 | // This file implements all of the non-inline methods for the LLVM instruction |
| 10 | // classes. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Instructions.h" |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/None.h" |
| 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/ADT/Twine.h" |
| 19 | #include "llvm/IR/Attributes.h" |
| 20 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constants.h" |
| 24 | #include "llvm/IR/DataLayout.h" |
| 25 | #include "llvm/IR/DerivedTypes.h" |
| 26 | #include "llvm/IR/Function.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 27 | #include "llvm/IR/InstrTypes.h" |
| 28 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 05b5bd8 | 2018-12-27 23:40:17 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Intrinsics.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 30 | #include "llvm/IR/LLVMContext.h" |
| 31 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Module.h" |
| 33 | #include "llvm/IR/Operator.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Type.h" |
| 35 | #include "llvm/IR/Value.h" |
| 36 | #include "llvm/Support/AtomicOrdering.h" |
| 37 | #include "llvm/Support/Casting.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ErrorHandling.h" |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MathExtras.h" |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 40 | #include <algorithm> |
| 41 | #include <cassert> |
| 42 | #include <cstdint> |
| 43 | #include <vector> |
| 44 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
Bjorn Pettersson | 550517b | 2018-06-26 06:17:00 +0000 | [diff] [blame] | 48 | // AllocaInst Class |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
| 51 | Optional<uint64_t> |
| 52 | AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const { |
| 53 | uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType()); |
| 54 | if (isArrayAllocation()) { |
| 55 | auto C = dyn_cast<ConstantInt>(getArraySize()); |
| 56 | if (!C) |
| 57 | return None; |
| 58 | Size *= C->getZExtValue(); |
| 59 | } |
| 60 | return Size; |
| 61 | } |
| 62 | |
| 63 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 64 | // CallSite Class |
| 65 | //===----------------------------------------------------------------------===// |
| 66 | |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 67 | User::op_iterator CallSite::getCallee() const { |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 68 | return cast<CallBase>(getInstruction())->op_end() - 1; |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 71 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 72 | // SelectInst Class |
| 73 | //===----------------------------------------------------------------------===// |
| 74 | |
| 75 | /// areInvalidOperands - Return a string if the specified operands are invalid |
| 76 | /// for a select operation, otherwise return null. |
| 77 | const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { |
| 78 | if (Op1->getType() != Op2->getType()) |
| 79 | return "both values to select must have same type"; |
David Majnemer | b611e3f | 2015-08-14 05:09:07 +0000 | [diff] [blame] | 80 | |
| 81 | if (Op1->getType()->isTokenTy()) |
| 82 | return "select values cannot have token type"; |
| 83 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 84 | if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 85 | // Vector select. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 86 | if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 87 | return "vector select condition element type must be i1"; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 88 | VectorType *ET = dyn_cast<VectorType>(Op1->getType()); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 89 | if (!ET) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 90 | return "selected values for vector select must be vectors"; |
| 91 | if (ET->getNumElements() != VT->getNumElements()) |
| 92 | return "vector select requires selected vectors to have " |
| 93 | "the same vector length as select condition"; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 94 | } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 95 | return "select condition must be i1 or <n x i1>"; |
| 96 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 97 | return nullptr; |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 100 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 101 | // PHINode Class |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | |
| 104 | PHINode::PHINode(const PHINode &PN) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 105 | : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()), |
| 106 | ReservedSpace(PN.getNumOperands()) { |
| 107 | allocHungoffUses(PN.getNumOperands()); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 108 | std::copy(PN.op_begin(), PN.op_end(), op_begin()); |
| 109 | std::copy(PN.block_begin(), PN.block_end(), block_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 110 | SubclassOptionalData = PN.SubclassOptionalData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 113 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 114 | // predecessor basic block is deleted. |
| 115 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 116 | Value *Removed = getIncomingValue(Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 117 | |
| 118 | // Move everything after this operand down. |
| 119 | // |
| 120 | // 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] | 121 | // 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] | 122 | // use/def lists, which is kinda lame. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 123 | std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); |
| 124 | std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 125 | |
| 126 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 127 | Op<-1>().set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 128 | setNumHungOffUseOperands(getNumOperands() - 1); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 129 | |
| 130 | // 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] | 131 | if (getNumOperands() == 0 && DeletePHIIfEmpty) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 132 | // 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] | 133 | replaceAllUsesWith(UndefValue::get(getType())); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 134 | eraseFromParent(); |
| 135 | } |
| 136 | return Removed; |
| 137 | } |
| 138 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 139 | /// growOperands - grow operands - This grows the operand list in response |
| 140 | /// to a push_back style of operation. This grows the number of ops by 1.5 |
| 141 | /// times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 142 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 143 | void PHINode::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 144 | unsigned e = getNumOperands(); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 145 | unsigned NumOps = e + e / 2; |
| 146 | if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. |
| 147 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 148 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 149 | growHungoffUses(ReservedSpace, /* IsPhi */ true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 152 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 153 | /// value, return the value, otherwise return null. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 154 | Value *PHINode::hasConstantValue() const { |
| 155 | // Exploit the fact that phi nodes always have at least one entry. |
| 156 | Value *ConstantValue = getIncomingValue(0); |
| 157 | for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) |
Nuno Lopes | 90c76df | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 158 | if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) { |
| 159 | if (ConstantValue != this) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 160 | return nullptr; // Incoming values not all the same. |
Nuno Lopes | 90c76df | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 161 | // The case where the first value is this PHI. |
| 162 | ConstantValue = getIncomingValue(i); |
| 163 | } |
Nuno Lopes | 0d44a50 | 2012-07-03 21:15:40 +0000 | [diff] [blame] | 164 | if (ConstantValue == this) |
| 165 | return UndefValue::get(getType()); |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 166 | return ConstantValue; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Nicolai Haehnle | 13d90f3 | 2016-04-14 17:42:47 +0000 | [diff] [blame] | 169 | /// hasConstantOrUndefValue - Whether the specified PHI node always merges |
| 170 | /// together the same value, assuming that undefs result in the same value as |
| 171 | /// non-undefs. |
| 172 | /// Unlike \ref hasConstantValue, this does not return a value because the |
| 173 | /// unique non-undef incoming value need not dominate the PHI node. |
| 174 | bool PHINode::hasConstantOrUndefValue() const { |
| 175 | Value *ConstantValue = nullptr; |
| 176 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) { |
| 177 | Value *Incoming = getIncomingValue(i); |
| 178 | if (Incoming != this && !isa<UndefValue>(Incoming)) { |
| 179 | if (ConstantValue && ConstantValue != Incoming) |
| 180 | return false; |
| 181 | ConstantValue = Incoming; |
| 182 | } |
| 183 | } |
| 184 | return true; |
| 185 | } |
| 186 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 187 | //===----------------------------------------------------------------------===// |
| 188 | // LandingPadInst Implementation |
| 189 | //===----------------------------------------------------------------------===// |
| 190 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 191 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 192 | const Twine &NameStr, Instruction *InsertBefore) |
| 193 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) { |
| 194 | init(NumReservedValues, NameStr); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 195 | } |
| 196 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 197 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 198 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 199 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) { |
| 200 | init(NumReservedValues, NameStr); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | LandingPadInst::LandingPadInst(const LandingPadInst &LP) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 204 | : Instruction(LP.getType(), Instruction::LandingPad, nullptr, |
| 205 | LP.getNumOperands()), |
| 206 | ReservedSpace(LP.getNumOperands()) { |
| 207 | allocHungoffUses(LP.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 208 | Use *OL = getOperandList(); |
| 209 | const Use *InOL = LP.getOperandList(); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 210 | for (unsigned I = 0, E = ReservedSpace; I != E; ++I) |
| 211 | OL[I] = InOL[I]; |
| 212 | |
| 213 | setCleanup(LP.isCleanup()); |
| 214 | } |
| 215 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 216 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 217 | const Twine &NameStr, |
| 218 | Instruction *InsertBefore) { |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 219 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 220 | } |
| 221 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 222 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 223 | const Twine &NameStr, |
| 224 | BasicBlock *InsertAtEnd) { |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 225 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 226 | } |
| 227 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 228 | void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 229 | ReservedSpace = NumReservedValues; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 230 | setNumHungOffUseOperands(0); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 231 | allocHungoffUses(ReservedSpace); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 232 | setName(NameStr); |
| 233 | setCleanup(false); |
| 234 | } |
| 235 | |
| 236 | /// growOperands - grow operands - This grows the operand list in response to a |
| 237 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 238 | void LandingPadInst::growOperands(unsigned Size) { |
| 239 | unsigned e = getNumOperands(); |
| 240 | if (ReservedSpace >= e + Size) return; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 241 | ReservedSpace = (std::max(e, 1U) + Size / 2) * 2; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 242 | growHungoffUses(ReservedSpace); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 245 | void LandingPadInst::addClause(Constant *Val) { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 246 | unsigned OpNo = getNumOperands(); |
| 247 | growOperands(1); |
| 248 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 249 | setNumHungOffUseOperands(getNumOperands() + 1); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 250 | getOperandList()[OpNo] = Val; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 251 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 252 | |
| 253 | //===----------------------------------------------------------------------===// |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 254 | // CallBase Implementation |
| 255 | //===----------------------------------------------------------------------===// |
| 256 | |
Chandler Carruth | 05b5bd8 | 2018-12-27 23:40:17 +0000 | [diff] [blame] | 257 | Function *CallBase::getCaller() { return getParent()->getParent(); } |
| 258 | |
Chandler Carruth | 57578aa | 2019-01-07 07:15:51 +0000 | [diff] [blame] | 259 | bool CallBase::isIndirectCall() const { |
| 260 | const Value *V = getCalledValue(); |
| 261 | if (isa<Function>(V) || isa<Constant>(V)) |
| 262 | return false; |
| 263 | if (const CallInst *CI = dyn_cast<CallInst>(this)) |
| 264 | if (CI->isInlineAsm()) |
| 265 | return false; |
| 266 | return true; |
| 267 | } |
| 268 | |
Chandler Carruth | 05b5bd8 | 2018-12-27 23:40:17 +0000 | [diff] [blame] | 269 | Intrinsic::ID CallBase::getIntrinsicID() const { |
| 270 | if (auto *F = getCalledFunction()) |
| 271 | return F->getIntrinsicID(); |
| 272 | return Intrinsic::not_intrinsic; |
| 273 | } |
| 274 | |
| 275 | bool CallBase::isReturnNonNull() const { |
| 276 | if (hasRetAttr(Attribute::NonNull)) |
| 277 | return true; |
| 278 | |
| 279 | if (getDereferenceableBytes(AttributeList::ReturnIndex) > 0 && |
| 280 | !NullPointerIsDefined(getCaller(), |
| 281 | getType()->getPointerAddressSpace())) |
| 282 | return true; |
| 283 | |
| 284 | return false; |
| 285 | } |
| 286 | |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 287 | Value *CallBase::getReturnedArgOperand() const { |
| 288 | unsigned Index; |
| 289 | |
| 290 | if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index) |
| 291 | return getArgOperand(Index - AttributeList::FirstArgIndex); |
| 292 | if (const Function *F = getCalledFunction()) |
| 293 | if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) && |
| 294 | Index) |
| 295 | return getArgOperand(Index - AttributeList::FirstArgIndex); |
| 296 | |
| 297 | return nullptr; |
| 298 | } |
| 299 | |
| 300 | bool CallBase::hasRetAttr(Attribute::AttrKind Kind) const { |
| 301 | if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind)) |
| 302 | return true; |
| 303 | |
| 304 | // Look at the callee, if available. |
| 305 | if (const Function *F = getCalledFunction()) |
| 306 | return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind); |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | /// Determine whether the argument or parameter has the given attribute. |
| 311 | bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { |
| 312 | assert(ArgNo < getNumArgOperands() && "Param index out of bounds!"); |
| 313 | |
| 314 | if (Attrs.hasParamAttribute(ArgNo, Kind)) |
| 315 | return true; |
| 316 | if (const Function *F = getCalledFunction()) |
| 317 | return F->getAttributes().hasParamAttribute(ArgNo, Kind); |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const { |
| 322 | if (const Function *F = getCalledFunction()) |
| 323 | return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind); |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const { |
| 328 | if (const Function *F = getCalledFunction()) |
| 329 | return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind); |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | CallBase::op_iterator |
| 334 | CallBase::populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, |
| 335 | const unsigned BeginIndex) { |
| 336 | auto It = op_begin() + BeginIndex; |
| 337 | for (auto &B : Bundles) |
| 338 | It = std::copy(B.input_begin(), B.input_end(), It); |
| 339 | |
| 340 | auto *ContextImpl = getContext().pImpl; |
| 341 | auto BI = Bundles.begin(); |
| 342 | unsigned CurrentIndex = BeginIndex; |
| 343 | |
| 344 | for (auto &BOI : bundle_op_infos()) { |
| 345 | assert(BI != Bundles.end() && "Incorrect allocation?"); |
| 346 | |
| 347 | BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag()); |
| 348 | BOI.Begin = CurrentIndex; |
| 349 | BOI.End = CurrentIndex + BI->input_size(); |
| 350 | CurrentIndex = BOI.End; |
| 351 | BI++; |
| 352 | } |
| 353 | |
| 354 | assert(BI == Bundles.end() && "Incorrect allocation?"); |
| 355 | |
| 356 | return It; |
| 357 | } |
| 358 | |
| 359 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 360 | // CallInst Implementation |
| 361 | //===----------------------------------------------------------------------===// |
| 362 | |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 363 | void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 364 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) { |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 365 | this->FTy = FTy; |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 366 | assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 && |
| 367 | "NumOperands not set up?"); |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 368 | setCalledOperand(Func); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 369 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 370 | #ifndef NDEBUG |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 371 | assert((Args.size() == FTy->getNumParams() || |
| 372 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 373 | "Calling a function with bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 374 | |
| 375 | for (unsigned i = 0; i != Args.size(); ++i) |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 376 | assert((i >= FTy->getNumParams() || |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 377 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 378 | "Calling a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 379 | #endif |
| 380 | |
Fangrui Song | 7570932 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 381 | llvm::copy(Args, op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 382 | |
| 383 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 384 | (void)It; |
| 385 | assert(It + 1 == op_end() && "Should add up!"); |
| 386 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 387 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 388 | } |
| 389 | |
James Y Knight | f956390 | 2019-01-14 21:37:42 +0000 | [diff] [blame] | 390 | void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) { |
| 391 | this->FTy = FTy; |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 392 | assert(getNumOperands() == 1 && "NumOperands not set up?"); |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 393 | setCalledOperand(Func); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 394 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 395 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 396 | |
| 397 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 398 | } |
| 399 | |
James Y Knight | f956390 | 2019-01-14 21:37:42 +0000 | [diff] [blame] | 400 | CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, |
| 401 | Instruction *InsertBefore) |
| 402 | : CallBase(Ty->getReturnType(), Instruction::Call, |
| 403 | OperandTraits<CallBase>::op_end(this) - 1, 1, InsertBefore) { |
| 404 | init(Ty, Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 405 | } |
| 406 | |
James Y Knight | f956390 | 2019-01-14 21:37:42 +0000 | [diff] [blame] | 407 | CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, |
| 408 | BasicBlock *InsertAtEnd) |
| 409 | : CallBase(Ty->getReturnType(), Instruction::Call, |
| 410 | OperandTraits<CallBase>::op_end(this) - 1, 1, InsertAtEnd) { |
| 411 | init(Ty, Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 414 | CallInst::CallInst(const CallInst &CI) |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 415 | : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call, |
| 416 | OperandTraits<CallBase>::op_end(this) - CI.getNumOperands(), |
| 417 | CI.getNumOperands()) { |
Reid Kleckner | 118e1bf | 2014-05-06 20:08:20 +0000 | [diff] [blame] | 418 | setTailCallKind(CI.getTailCallKind()); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 419 | setCallingConv(CI.getCallingConv()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 420 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 421 | std::copy(CI.op_begin(), CI.op_end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 422 | std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(), |
| 423 | bundle_op_info_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 424 | SubclassOptionalData = CI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 427 | CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB, |
| 428 | Instruction *InsertPt) { |
Sanjoy Das | ccd1456 | 2015-12-10 06:39:02 +0000 | [diff] [blame] | 429 | std::vector<Value *> Args(CI->arg_begin(), CI->arg_end()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 430 | |
| 431 | auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(), |
| 432 | InsertPt); |
| 433 | NewCI->setTailCallKind(CI->getTailCallKind()); |
| 434 | NewCI->setCallingConv(CI->getCallingConv()); |
| 435 | NewCI->SubclassOptionalData = CI->SubclassOptionalData; |
Sanjoy Das | b8dced5 | 2015-12-09 01:01:28 +0000 | [diff] [blame] | 436 | NewCI->setAttributes(CI->getAttributes()); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 437 | NewCI->setDebugLoc(CI->getDebugLoc()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 438 | return NewCI; |
| 439 | } |
| 440 | |
Hal Finkel | e87ad54 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 441 | |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 442 | |
Hal Finkel | e87ad54 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 443 | |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 444 | |
Amaury Sechet | a65a237 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 445 | |
Reid Kleckner | 5fbdd17 | 2017-05-31 19:23:09 +0000 | [diff] [blame] | 446 | |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 447 | |
Amaury Sechet | 1a0e097 | 2016-04-21 21:29:10 +0000 | [diff] [blame] | 448 | |
Sanjoy Das | a4bae3b | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 449 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 450 | /// IsConstantOne - Return true only if val is constant int 1 |
| 451 | static bool IsConstantOne(Value *val) { |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 452 | assert(val && "IsConstantOne does not work with nullptr val"); |
Matt Arsenault | 6941785 | 2014-09-15 17:56:51 +0000 | [diff] [blame] | 453 | const ConstantInt *CVal = dyn_cast<ConstantInt>(val); |
| 454 | return CVal && CVal->isOne(); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 457 | static Instruction *createMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 458 | BasicBlock *InsertAtEnd, Type *IntPtrTy, |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 459 | Type *AllocTy, Value *AllocSize, |
| 460 | Value *ArraySize, |
| 461 | ArrayRef<OperandBundleDef> OpB, |
| 462 | Function *MallocF, const Twine &Name) { |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 463 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 464 | "createMalloc needs either InsertBefore or InsertAtEnd"); |
| 465 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 466 | // malloc(type) becomes: |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 467 | // bitcast (i8* malloc(typeSize)) to type* |
| 468 | // malloc(type, arraySize) becomes: |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 469 | // bitcast (i8* malloc(typeSize*arraySize)) to type* |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 470 | if (!ArraySize) |
| 471 | ArraySize = ConstantInt::get(IntPtrTy, 1); |
| 472 | else if (ArraySize->getType() != IntPtrTy) { |
| 473 | if (InsertBefore) |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 474 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 475 | "", InsertBefore); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 476 | else |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 477 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 478 | "", InsertAtEnd); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 479 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 480 | |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 481 | if (!IsConstantOne(ArraySize)) { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 482 | if (IsConstantOne(AllocSize)) { |
| 483 | AllocSize = ArraySize; // Operand * 1 = Operand |
| 484 | } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { |
| 485 | Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, |
| 486 | false /*ZExt*/); |
| 487 | // Malloc arg is constant product of type size and array size |
| 488 | AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); |
| 489 | } else { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 490 | // Multiply type size by the array size... |
| 491 | if (InsertBefore) |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 492 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 493 | "mallocsize", InsertBefore); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 494 | else |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 495 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 496 | "mallocsize", InsertAtEnd); |
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 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 499 | |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 500 | assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size"); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 501 | // Create the call to Malloc. |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 502 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 503 | Module *M = BB->getParent()->getParent(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 504 | Type *BPTy = Type::getInt8PtrTy(BB->getContext()); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 505 | Value *MallocFunc = MallocF; |
| 506 | if (!MallocFunc) |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 507 | // prototype malloc as "void *malloc(size_t)" |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 508 | MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 509 | PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 510 | CallInst *MCall = nullptr; |
| 511 | Instruction *Result = nullptr; |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 512 | if (InsertBefore) { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 513 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall", |
| 514 | InsertBefore); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 515 | Result = MCall; |
| 516 | if (Result->getType() != AllocPtrType) |
| 517 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 518 | Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 519 | } else { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 520 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall"); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 521 | Result = MCall; |
| 522 | if (Result->getType() != AllocPtrType) { |
| 523 | InsertAtEnd->getInstList().push_back(MCall); |
| 524 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 525 | Result = new BitCastInst(MCall, AllocPtrType, Name); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 526 | } |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 527 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 528 | MCall->setTailCall(); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 529 | if (Function *F = dyn_cast<Function>(MallocFunc)) { |
| 530 | MCall->setCallingConv(F->getCallingConv()); |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 531 | if (!F->returnDoesNotAlias()) |
| 532 | F->setReturnDoesNotAlias(); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 533 | } |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 534 | assert(!MCall->getType()->isVoidTy() && "Malloc has void return type"); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 535 | |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 536 | return Result; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 540 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 541 | /// possibly multiplied by the array size if the array size is not |
| 542 | /// constant 1. |
| 543 | /// 2. Call malloc with that argument. |
| 544 | /// 3. Bitcast the result of the malloc call to the specified type. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 545 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 546 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 547 | Value *AllocSize, Value *ArraySize, |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 548 | Function *MallocF, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 549 | const Twine &Name) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 550 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 551 | ArraySize, None, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 552 | } |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 553 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
| 554 | Type *IntPtrTy, Type *AllocTy, |
| 555 | Value *AllocSize, Value *ArraySize, |
| 556 | ArrayRef<OperandBundleDef> OpB, |
| 557 | Function *MallocF, |
| 558 | const Twine &Name) { |
| 559 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, |
| 560 | ArraySize, OpB, MallocF, Name); |
| 561 | } |
| 562 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 563 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 564 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 565 | /// possibly multiplied by the array size if the array size is not |
| 566 | /// constant 1. |
| 567 | /// 2. Call malloc with that argument. |
| 568 | /// 3. Bitcast the result of the malloc call to the specified type. |
| 569 | /// Note: This function does not add the bitcast to the basic block, that is the |
| 570 | /// responsibility of the caller. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 571 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 572 | Type *IntPtrTy, Type *AllocTy, |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 573 | Value *AllocSize, Value *ArraySize, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 574 | Function *MallocF, const Twine &Name) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 575 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 576 | ArraySize, None, MallocF, Name); |
| 577 | } |
| 578 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
| 579 | Type *IntPtrTy, Type *AllocTy, |
| 580 | Value *AllocSize, Value *ArraySize, |
| 581 | ArrayRef<OperandBundleDef> OpB, |
| 582 | Function *MallocF, const Twine &Name) { |
| 583 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
| 584 | ArraySize, OpB, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 585 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 586 | |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 587 | static Instruction *createFree(Value *Source, |
| 588 | ArrayRef<OperandBundleDef> Bundles, |
| 589 | Instruction *InsertBefore, |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 590 | BasicBlock *InsertAtEnd) { |
| 591 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
| 592 | "createFree needs either InsertBefore or InsertAtEnd"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 593 | assert(Source->getType()->isPointerTy() && |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 594 | "Can not free something of nonpointer type!"); |
| 595 | |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 596 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 597 | Module *M = BB->getParent()->getParent(); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 598 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 599 | Type *VoidTy = Type::getVoidTy(M->getContext()); |
| 600 | Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 601 | // prototype free as "void free(void*)" |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 602 | Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy); |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 603 | CallInst *Result = nullptr; |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 604 | Value *PtrCast = Source; |
| 605 | if (InsertBefore) { |
| 606 | if (Source->getType() != IntPtrTy) |
| 607 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 608 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 609 | } else { |
| 610 | if (Source->getType() != IntPtrTy) |
| 611 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 612 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, ""); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 613 | } |
| 614 | Result->setTailCall(); |
Chris Lattner | 2156c22 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 615 | if (Function *F = dyn_cast<Function>(FreeFunc)) |
| 616 | Result->setCallingConv(F->getCallingConv()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 617 | |
| 618 | return Result; |
| 619 | } |
| 620 | |
| 621 | /// CreateFree - Generate the IR for a call to the builtin free function. |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 622 | Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 623 | return createFree(Source, None, InsertBefore, nullptr); |
| 624 | } |
| 625 | Instruction *CallInst::CreateFree(Value *Source, |
| 626 | ArrayRef<OperandBundleDef> Bundles, |
| 627 | Instruction *InsertBefore) { |
| 628 | return createFree(Source, Bundles, InsertBefore, nullptr); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /// CreateFree - Generate the IR for a call to the builtin free function. |
| 632 | /// Note: This function does not add the call to the basic block, that is the |
| 633 | /// responsibility of the caller. |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 634 | Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) { |
David Majnemer | fadc6db | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 635 | Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd); |
| 636 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 637 | return FreeCall; |
| 638 | } |
| 639 | Instruction *CallInst::CreateFree(Value *Source, |
| 640 | ArrayRef<OperandBundleDef> Bundles, |
| 641 | BasicBlock *InsertAtEnd) { |
| 642 | Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 643 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 644 | return FreeCall; |
| 645 | } |
| 646 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 647 | //===----------------------------------------------------------------------===// |
| 648 | // InvokeInst Implementation |
| 649 | //===----------------------------------------------------------------------===// |
| 650 | |
David Blaikie | 3e80709 | 2015-05-13 18:35:26 +0000 | [diff] [blame] | 651 | void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal, |
| 652 | BasicBlock *IfException, ArrayRef<Value *> Args, |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 653 | ArrayRef<OperandBundleDef> Bundles, |
David Blaikie | 3e80709 | 2015-05-13 18:35:26 +0000 | [diff] [blame] | 654 | const Twine &NameStr) { |
| 655 | this->FTy = FTy; |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 656 | |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 657 | assert((int)getNumOperands() == |
| 658 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) && |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 659 | "NumOperands not set up?"); |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 660 | setNormalDest(IfNormal); |
| 661 | setUnwindDest(IfException); |
| 662 | setCalledOperand(Fn); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 663 | |
| 664 | #ifndef NDEBUG |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 665 | assert(((Args.size() == FTy->getNumParams()) || |
| 666 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Gabor Greif | 668d700 | 2010-03-23 13:45:54 +0000 | [diff] [blame] | 667 | "Invoking a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 668 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 669 | for (unsigned i = 0, e = Args.size(); i != e; i++) |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 670 | assert((i >= FTy->getNumParams() || |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 671 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 672 | "Invoking a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 673 | #endif |
| 674 | |
Fangrui Song | 7570932 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 675 | llvm::copy(Args, op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 676 | |
| 677 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 678 | (void)It; |
| 679 | assert(It + 3 == op_end() && "Should add up!"); |
| 680 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 681 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 684 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chandler Carruth | e429c79 | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 685 | : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke, |
| 686 | OperandTraits<CallBase>::op_end(this) - II.getNumOperands(), |
| 687 | II.getNumOperands()) { |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 688 | setCallingConv(II.getCallingConv()); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 689 | std::copy(II.op_begin(), II.op_end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 690 | std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(), |
| 691 | bundle_op_info_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 692 | SubclassOptionalData = II.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 695 | InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB, |
| 696 | Instruction *InsertPt) { |
Sanjoy Das | ccd1456 | 2015-12-10 06:39:02 +0000 | [diff] [blame] | 697 | std::vector<Value *> Args(II->arg_begin(), II->arg_end()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 698 | |
| 699 | auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(), |
| 700 | II->getUnwindDest(), Args, OpB, |
| 701 | II->getName(), InsertPt); |
| 702 | NewII->setCallingConv(II->getCallingConv()); |
| 703 | NewII->SubclassOptionalData = II->SubclassOptionalData; |
Sanjoy Das | b8dced5 | 2015-12-09 01:01:28 +0000 | [diff] [blame] | 704 | NewII->setAttributes(II->getAttributes()); |
Joseph Tremoulet | bba70e4 | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 705 | NewII->setDebugLoc(II->getDebugLoc()); |
Sanjoy Das | 2d16145 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 706 | return NewII; |
| 707 | } |
| 708 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 709 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 710 | LandingPadInst *InvokeInst::getLandingPadInst() const { |
| 711 | return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); |
| 712 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 713 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 714 | //===----------------------------------------------------------------------===// |
| 715 | // ReturnInst Implementation |
| 716 | //===----------------------------------------------------------------------===// |
| 717 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 718 | ReturnInst::ReturnInst(const ReturnInst &RI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 719 | : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret, |
| 720 | OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(), |
| 721 | RI.getNumOperands()) { |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 722 | if (RI.getNumOperands()) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 723 | Op<0>() = RI.Op<0>(); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 724 | SubclassOptionalData = RI.SubclassOptionalData; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 727 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 728 | : Instruction(Type::getVoidTy(C), Instruction::Ret, |
| 729 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 730 | InsertBefore) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 731 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 732 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 733 | } |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 734 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 735 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 736 | : Instruction(Type::getVoidTy(C), Instruction::Ret, |
| 737 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 738 | InsertAtEnd) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 739 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 740 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 741 | } |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 742 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 743 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 744 | : Instruction(Type::getVoidTy(Context), Instruction::Ret, |
| 745 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {} |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 746 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 747 | //===----------------------------------------------------------------------===// |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 748 | // ResumeInst Implementation |
| 749 | //===----------------------------------------------------------------------===// |
| 750 | |
| 751 | ResumeInst::ResumeInst(const ResumeInst &RI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 752 | : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume, |
| 753 | OperandTraits<ResumeInst>::op_begin(this), 1) { |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 754 | Op<0>() = RI.Op<0>(); |
| 755 | } |
| 756 | |
| 757 | ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 758 | : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 759 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 760 | Op<0>() = Exn; |
| 761 | } |
| 762 | |
| 763 | ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 764 | : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 765 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 766 | Op<0>() = Exn; |
| 767 | } |
| 768 | |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 769 | //===----------------------------------------------------------------------===// |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 770 | // CleanupReturnInst Implementation |
| 771 | //===----------------------------------------------------------------------===// |
| 772 | |
| 773 | CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 774 | : Instruction(CRI.getType(), Instruction::CleanupRet, |
| 775 | OperandTraits<CleanupReturnInst>::op_end(this) - |
| 776 | CRI.getNumOperands(), |
| 777 | CRI.getNumOperands()) { |
David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 778 | setInstructionSubclassData(CRI.getSubclassDataFromInstruction()); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 779 | Op<0>() = CRI.Op<0>(); |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 780 | if (CRI.hasUnwindDest()) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 781 | Op<1>() = CRI.Op<1>(); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 782 | } |
| 783 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 784 | void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 785 | if (UnwindBB) |
| 786 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 787 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 788 | Op<0>() = CleanupPad; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 789 | if (UnwindBB) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 790 | Op<1>() = UnwindBB; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 791 | } |
| 792 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 793 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, |
| 794 | unsigned Values, Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 795 | : Instruction(Type::getVoidTy(CleanupPad->getContext()), |
| 796 | Instruction::CleanupRet, |
| 797 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, |
| 798 | Values, InsertBefore) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 799 | init(CleanupPad, UnwindBB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 800 | } |
| 801 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 802 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, |
| 803 | unsigned Values, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 804 | : Instruction(Type::getVoidTy(CleanupPad->getContext()), |
| 805 | Instruction::CleanupRet, |
| 806 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, |
| 807 | Values, InsertAtEnd) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 808 | init(CleanupPad, UnwindBB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 809 | } |
| 810 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 811 | //===----------------------------------------------------------------------===// |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 812 | // CatchReturnInst Implementation |
| 813 | //===----------------------------------------------------------------------===// |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 814 | void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 815 | Op<0>() = CatchPad; |
| 816 | Op<1>() = BB; |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 817 | } |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 818 | |
| 819 | CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 820 | : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet, |
| 821 | OperandTraits<CatchReturnInst>::op_begin(this), 2) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 822 | Op<0>() = CRI.Op<0>(); |
| 823 | Op<1>() = CRI.Op<1>(); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 824 | } |
| 825 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 826 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 827 | Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 828 | : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, |
| 829 | OperandTraits<CatchReturnInst>::op_begin(this), 2, |
| 830 | InsertBefore) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 831 | init(CatchPad, BB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 832 | } |
| 833 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 834 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 835 | BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 836 | : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, |
| 837 | OperandTraits<CatchReturnInst>::op_begin(this), 2, |
| 838 | InsertAtEnd) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 839 | init(CatchPad, BB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 840 | } |
| 841 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 842 | //===----------------------------------------------------------------------===// |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 843 | // CatchSwitchInst Implementation |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 844 | //===----------------------------------------------------------------------===// |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 845 | |
| 846 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
| 847 | unsigned NumReservedValues, |
| 848 | const Twine &NameStr, |
| 849 | Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 850 | : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, |
| 851 | InsertBefore) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 852 | if (UnwindDest) |
| 853 | ++NumReservedValues; |
| 854 | init(ParentPad, UnwindDest, NumReservedValues + 1); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 855 | setName(NameStr); |
| 856 | } |
| 857 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 858 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
| 859 | unsigned NumReservedValues, |
| 860 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 861 | : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, |
| 862 | InsertAtEnd) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 863 | if (UnwindDest) |
| 864 | ++NumReservedValues; |
| 865 | init(ParentPad, UnwindDest, NumReservedValues + 1); |
| 866 | setName(NameStr); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 867 | } |
| 868 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 869 | CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 870 | : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr, |
| 871 | CSI.getNumOperands()) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 872 | init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands()); |
| 873 | setNumHungOffUseOperands(ReservedSpace); |
| 874 | Use *OL = getOperandList(); |
| 875 | const Use *InOL = CSI.getOperandList(); |
| 876 | for (unsigned I = 1, E = ReservedSpace; I != E; ++I) |
| 877 | OL[I] = InOL[I]; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 878 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 879 | |
| 880 | void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest, |
| 881 | unsigned NumReservedValues) { |
| 882 | assert(ParentPad && NumReservedValues); |
| 883 | |
| 884 | ReservedSpace = NumReservedValues; |
| 885 | setNumHungOffUseOperands(UnwindDest ? 2 : 1); |
| 886 | allocHungoffUses(ReservedSpace); |
| 887 | |
| 888 | Op<0>() = ParentPad; |
| 889 | if (UnwindDest) { |
| 890 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
| 891 | setUnwindDest(UnwindDest); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | /// growOperands - grow operands - This grows the operand list in response to a |
| 896 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 897 | void CatchSwitchInst::growOperands(unsigned Size) { |
| 898 | unsigned NumOperands = getNumOperands(); |
| 899 | assert(NumOperands >= 1); |
| 900 | if (ReservedSpace >= NumOperands + Size) |
| 901 | return; |
| 902 | ReservedSpace = (NumOperands + Size / 2) * 2; |
| 903 | growHungoffUses(ReservedSpace); |
| 904 | } |
| 905 | |
| 906 | void CatchSwitchInst::addHandler(BasicBlock *Handler) { |
| 907 | unsigned OpNo = getNumOperands(); |
| 908 | growOperands(1); |
| 909 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
| 910 | setNumHungOffUseOperands(getNumOperands() + 1); |
| 911 | getOperandList()[OpNo] = Handler; |
| 912 | } |
| 913 | |
Joseph Tremoulet | 0d80888 | 2016-01-05 02:37:41 +0000 | [diff] [blame] | 914 | void CatchSwitchInst::removeHandler(handler_iterator HI) { |
| 915 | // Move all subsequent handlers up one. |
| 916 | Use *EndDst = op_end() - 1; |
| 917 | for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst) |
| 918 | *CurDst = *(CurDst + 1); |
| 919 | // Null out the last handler use. |
| 920 | *EndDst = nullptr; |
| 921 | |
| 922 | setNumHungOffUseOperands(getNumOperands() - 1); |
| 923 | } |
| 924 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 925 | //===----------------------------------------------------------------------===// |
| 926 | // FuncletPadInst Implementation |
| 927 | //===----------------------------------------------------------------------===// |
| 928 | void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args, |
| 929 | const Twine &NameStr) { |
| 930 | assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?"); |
Fangrui Song | 7570932 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 931 | llvm::copy(Args, op_begin()); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 932 | setParentPad(ParentPad); |
| 933 | setName(NameStr); |
| 934 | } |
| 935 | |
| 936 | FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI) |
| 937 | : Instruction(FPI.getType(), FPI.getOpcode(), |
| 938 | OperandTraits<FuncletPadInst>::op_end(this) - |
| 939 | FPI.getNumOperands(), |
| 940 | FPI.getNumOperands()) { |
| 941 | std::copy(FPI.op_begin(), FPI.op_end(), op_begin()); |
| 942 | setParentPad(FPI.getParentPad()); |
| 943 | } |
| 944 | |
| 945 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
| 946 | ArrayRef<Value *> Args, unsigned Values, |
| 947 | const Twine &NameStr, Instruction *InsertBefore) |
| 948 | : Instruction(ParentPad->getType(), Op, |
| 949 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, |
| 950 | InsertBefore) { |
| 951 | init(ParentPad, Args, NameStr); |
| 952 | } |
| 953 | |
| 954 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
| 955 | ArrayRef<Value *> Args, unsigned Values, |
| 956 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 957 | : Instruction(ParentPad->getType(), Op, |
| 958 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, |
| 959 | InsertAtEnd) { |
| 960 | init(ParentPad, Args, NameStr); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 964 | // UnreachableInst Implementation |
| 965 | //===----------------------------------------------------------------------===// |
| 966 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 967 | UnreachableInst::UnreachableInst(LLVMContext &Context, |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 968 | Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 969 | : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, |
| 970 | 0, InsertBefore) {} |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 971 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 972 | : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, |
| 973 | 0, InsertAtEnd) {} |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 974 | |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 975 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 976 | // BranchInst Implementation |
| 977 | //===----------------------------------------------------------------------===// |
| 978 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 979 | void BranchInst::AssertOK() { |
| 980 | if (isConditional()) |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 981 | assert(getCondition()->getType()->isIntegerTy(1) && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 982 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 985 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 986 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
| 987 | OperandTraits<BranchInst>::op_end(this) - 1, 1, |
| 988 | InsertBefore) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 989 | assert(IfTrue && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 990 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 991 | } |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 992 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 993 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 994 | Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 995 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
| 996 | OperandTraits<BranchInst>::op_end(this) - 3, 3, |
| 997 | InsertBefore) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 998 | Op<-1>() = IfTrue; |
| 999 | Op<-2>() = IfFalse; |
| 1000 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1001 | #ifndef NDEBUG |
| 1002 | AssertOK(); |
| 1003 | #endif |
| 1004 | } |
| 1005 | |
| 1006 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1007 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
| 1008 | OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1009 | assert(IfTrue && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1010 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1014 | BasicBlock *InsertAtEnd) |
| 1015 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
| 1016 | OperandTraits<BranchInst>::op_end(this) - 3, 3, InsertAtEnd) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1017 | Op<-1>() = IfTrue; |
| 1018 | Op<-2>() = IfFalse; |
| 1019 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1020 | #ifndef NDEBUG |
| 1021 | AssertOK(); |
| 1022 | #endif |
| 1023 | } |
| 1024 | |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1025 | BranchInst::BranchInst(const BranchInst &BI) |
| 1026 | : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br, |
| 1027 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), |
| 1028 | BI.getNumOperands()) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1029 | Op<-1>() = BI.Op<-1>(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1030 | if (BI.getNumOperands() != 1) { |
| 1031 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1032 | Op<-3>() = BI.Op<-3>(); |
| 1033 | Op<-2>() = BI.Op<-2>(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1034 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1035 | SubclassOptionalData = BI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Chandler Carruth | 3e8aa65 | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 1038 | void BranchInst::swapSuccessors() { |
| 1039 | assert(isConditional() && |
| 1040 | "Cannot swap successors of an unconditional branch"); |
| 1041 | Op<-1>().swap(Op<-2>()); |
| 1042 | |
| 1043 | // Update profile metadata if present and it matches our structural |
| 1044 | // expectations. |
Xinliang David Li | dc49140 | 2016-08-23 15:39:03 +0000 | [diff] [blame] | 1045 | swapProfMetadata(); |
Chandler Carruth | 3e8aa65 | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1048 | //===----------------------------------------------------------------------===// |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1049 | // AllocaInst Implementation |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1050 | //===----------------------------------------------------------------------===// |
| 1051 | |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 1052 | static Value *getAISize(LLVMContext &Context, Value *Amt) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1053 | if (!Amt) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1054 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 1055 | else { |
| 1056 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 9b6ec77 | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 1057 | "Passed basic block into allocation size parameter! Use other ctor"); |
Dan Gohman | 2140a74 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 1058 | assert(Amt->getType()->isIntegerTy() && |
| 1059 | "Allocation array size is not an integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 1060 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1061 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1064 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1065 | Instruction *InsertBefore) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1066 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {} |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1067 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1068 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1069 | BasicBlock *InsertAtEnd) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1070 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {} |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1071 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1072 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1073 | const Twine &Name, Instruction *InsertBefore) |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1074 | : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {} |
| 1075 | |
| 1076 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1077 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 1078 | : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {} |
| 1079 | |
| 1080 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1081 | unsigned Align, const Twine &Name, |
| 1082 | Instruction *InsertBefore) |
| 1083 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, |
| 1084 | getAISize(Ty->getContext(), ArraySize), InsertBefore), |
| 1085 | AllocatedType(Ty) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1086 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1087 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1088 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1091 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1092 | unsigned Align, const Twine &Name, |
| 1093 | BasicBlock *InsertAtEnd) |
| 1094 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, |
| 1095 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd), |
David Blaikie | bf0a42a | 2015-04-29 23:00:35 +0000 | [diff] [blame] | 1096 | AllocatedType(Ty) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1097 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1098 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1099 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1102 | void AllocaInst::setAlignment(unsigned Align) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1103 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1104 | assert(Align <= MaximumAlignment && |
| 1105 | "Alignment is greater than MaximumAlignment!"); |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 1106 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) | |
| 1107 | (Log2_32(Align) + 1)); |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1108 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 1109 | } |
| 1110 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1111 | bool AllocaInst::isArrayAllocation() const { |
Reid Spencer | a9e6e31 | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 1112 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
Dan Gohman | 9a1b859 | 2010-09-27 15:15:44 +0000 | [diff] [blame] | 1113 | return !CI->isOne(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1114 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1117 | /// isStaticAlloca - Return true if this alloca is in the entry block of the |
| 1118 | /// function and is a constant size. If so, the code generator will fold it |
| 1119 | /// into the prolog/epilog code, so it is basically free. |
| 1120 | bool AllocaInst::isStaticAlloca() const { |
| 1121 | // Must be constant size. |
| 1122 | if (!isa<ConstantInt>(getArraySize())) return false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1123 | |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1124 | // Must be in the entry block. |
| 1125 | const BasicBlock *Parent = getParent(); |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 1126 | return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca(); |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1129 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1130 | // LoadInst Implementation |
| 1131 | //===----------------------------------------------------------------------===// |
| 1132 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1133 | void LoadInst::AssertOK() { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1134 | assert(getOperand(0)->getType()->isPointerTy() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1135 | "Ptr must have pointer type."); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1136 | assert(!(isAtomic() && getAlignment() == 0) && |
| 1137 | "Alignment required for atomic load"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1140 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, |
| 1141 | Instruction *InsertBef) |
| 1142 | : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1143 | |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1144 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, |
| 1145 | BasicBlock *InsertAE) |
| 1146 | : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertAE) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1147 | |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1148 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1149 | Instruction *InsertBef) |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1150 | : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {} |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1151 | |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1152 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1153 | BasicBlock *InsertAE) |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1154 | : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {} |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1155 | |
David Blaikie | b7a02987 | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 1156 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1157 | unsigned Align, Instruction *InsertBef) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1158 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1159 | SyncScope::System, InsertBef) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1160 | |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1161 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1162 | unsigned Align, BasicBlock *InsertAE) |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1163 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1164 | SyncScope::System, InsertAE) {} |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1165 | |
David Blaikie | 15d9a4c | 2015-04-06 20:59:48 +0000 | [diff] [blame] | 1166 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1167 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1168 | SyncScope::ID SSID, Instruction *InsertBef) |
David Blaikie | 15d9a4c | 2015-04-06 20:59:48 +0000 | [diff] [blame] | 1169 | : UnaryInstruction(Ty, Load, Ptr, InsertBef) { |
David Blaikie | 79009f8 | 2015-05-20 20:22:31 +0000 | [diff] [blame] | 1170 | assert(Ty == cast<PointerType>(Ptr->getType())->getElementType()); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1171 | setVolatile(isVolatile); |
| 1172 | setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1173 | setAtomic(Order, SSID); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1174 | AssertOK(); |
| 1175 | setName(Name); |
| 1176 | } |
| 1177 | |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1178 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
| 1179 | unsigned Align, AtomicOrdering Order, SyncScope::ID SSID, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1180 | BasicBlock *InsertAE) |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1181 | : UnaryInstruction(Ty, Load, Ptr, InsertAE) { |
| 1182 | assert(Ty == cast<PointerType>(Ptr->getType())->getElementType()); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1183 | setVolatile(isVolatile); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1184 | setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1185 | setAtomic(Order, SSID); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1186 | AssertOK(); |
| 1187 | setName(Name); |
| 1188 | } |
| 1189 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1190 | void LoadInst::setAlignment(unsigned Align) { |
| 1191 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1192 | assert(Align <= MaximumAlignment && |
| 1193 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1194 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1195 | ((Log2_32(Align)+1)<<1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1196 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1197 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1198 | |
| 1199 | //===----------------------------------------------------------------------===// |
| 1200 | // StoreInst Implementation |
| 1201 | //===----------------------------------------------------------------------===// |
| 1202 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1203 | void StoreInst::AssertOK() { |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1204 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1205 | assert(getOperand(1)->getType()->isPointerTy() && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1206 | "Ptr must have pointer type!"); |
| 1207 | assert(getOperand(0)->getType() == |
| 1208 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 1209 | && "Ptr must be a pointer to Val type!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1210 | assert(!(isAtomic() && getAlignment() == 0) && |
Mark Lacey | 1d7c97e | 2013-12-21 00:00:49 +0000 | [diff] [blame] | 1211 | "Alignment required for atomic store"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1212 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1214 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1215 | : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1216 | |
| 1217 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1218 | : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1219 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1220 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1221 | Instruction *InsertBefore) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1222 | : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {} |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1223 | |
| 1224 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1225 | BasicBlock *InsertAtEnd) |
| 1226 | : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {} |
| 1227 | |
| 1228 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align, |
| 1229 | Instruction *InsertBefore) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1230 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1231 | SyncScope::System, InsertBefore) {} |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1232 | |
| 1233 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align, |
| 1234 | BasicBlock *InsertAtEnd) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1235 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1236 | SyncScope::System, InsertAtEnd) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1237 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1238 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1239 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1240 | SyncScope::ID SSID, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1241 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1242 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1243 | OperandTraits<StoreInst>::op_begin(this), |
| 1244 | OperandTraits<StoreInst>::operands(this), |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1245 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1246 | Op<0>() = val; |
| 1247 | Op<1>() = addr; |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1248 | setVolatile(isVolatile); |
| 1249 | setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1250 | setAtomic(Order, SSID); |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1251 | AssertOK(); |
| 1252 | } |
| 1253 | |
| 1254 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1255 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1256 | SyncScope::ID SSID, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1257 | BasicBlock *InsertAtEnd) |
| 1258 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
| 1259 | OperandTraits<StoreInst>::op_begin(this), |
| 1260 | OperandTraits<StoreInst>::operands(this), |
| 1261 | InsertAtEnd) { |
| 1262 | Op<0>() = val; |
| 1263 | Op<1>() = addr; |
| 1264 | setVolatile(isVolatile); |
| 1265 | setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1266 | setAtomic(Order, SSID); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1267 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1270 | void StoreInst::setAlignment(unsigned Align) { |
| 1271 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1272 | assert(Align <= MaximumAlignment && |
| 1273 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1274 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1275 | ((Log2_32(Align)+1) << 1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1276 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1279 | //===----------------------------------------------------------------------===// |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1280 | // AtomicCmpXchgInst Implementation |
| 1281 | //===----------------------------------------------------------------------===// |
| 1282 | |
| 1283 | void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1284 | AtomicOrdering SuccessOrdering, |
| 1285 | AtomicOrdering FailureOrdering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1286 | SyncScope::ID SSID) { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1287 | Op<0>() = Ptr; |
| 1288 | Op<1>() = Cmp; |
| 1289 | Op<2>() = NewVal; |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1290 | setSuccessOrdering(SuccessOrdering); |
| 1291 | setFailureOrdering(FailureOrdering); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1292 | setSyncScopeID(SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1293 | |
| 1294 | assert(getOperand(0) && getOperand(1) && getOperand(2) && |
| 1295 | "All operands must be non-null!"); |
| 1296 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1297 | "Ptr must have pointer type!"); |
| 1298 | assert(getOperand(1)->getType() == |
| 1299 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1300 | && "Ptr must be a pointer to Cmp type!"); |
| 1301 | assert(getOperand(2)->getType() == |
| 1302 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1303 | && "Ptr must be a pointer to NewVal type!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1304 | assert(SuccessOrdering != AtomicOrdering::NotAtomic && |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1305 | "AtomicCmpXchg instructions must be atomic!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1306 | assert(FailureOrdering != AtomicOrdering::NotAtomic && |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1307 | "AtomicCmpXchg instructions must be atomic!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1308 | assert(!isStrongerThan(FailureOrdering, SuccessOrdering) && |
| 1309 | "AtomicCmpXchg failure argument shall be no stronger than the success " |
| 1310 | "argument"); |
| 1311 | assert(FailureOrdering != AtomicOrdering::Release && |
| 1312 | FailureOrdering != AtomicOrdering::AcquireRelease && |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1313 | "AtomicCmpXchg failure ordering cannot include release semantics"); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1317 | AtomicOrdering SuccessOrdering, |
| 1318 | AtomicOrdering FailureOrdering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1319 | SyncScope::ID SSID, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1320 | Instruction *InsertBefore) |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1321 | : Instruction( |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1322 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1323 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1324 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1325 | Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1329 | AtomicOrdering SuccessOrdering, |
| 1330 | AtomicOrdering FailureOrdering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1331 | SyncScope::ID SSID, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1332 | BasicBlock *InsertAtEnd) |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1333 | : Instruction( |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1334 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1335 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1336 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1337 | Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1338 | } |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1339 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1340 | //===----------------------------------------------------------------------===// |
| 1341 | // AtomicRMWInst Implementation |
| 1342 | //===----------------------------------------------------------------------===// |
| 1343 | |
| 1344 | void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, |
| 1345 | AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1346 | SyncScope::ID SSID) { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1347 | Op<0>() = Ptr; |
| 1348 | Op<1>() = Val; |
| 1349 | setOperation(Operation); |
| 1350 | setOrdering(Ordering); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1351 | setSyncScopeID(SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1352 | |
| 1353 | assert(getOperand(0) && getOperand(1) && |
| 1354 | "All operands must be non-null!"); |
| 1355 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1356 | "Ptr must have pointer type!"); |
| 1357 | assert(getOperand(1)->getType() == |
| 1358 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1359 | && "Ptr must be a pointer to Val type!"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1360 | assert(Ordering != AtomicOrdering::NotAtomic && |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1361 | "AtomicRMW instructions must be atomic!"); |
| 1362 | } |
| 1363 | |
| 1364 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1365 | AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1366 | SyncScope::ID SSID, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1367 | Instruction *InsertBefore) |
| 1368 | : Instruction(Val->getType(), AtomicRMW, |
| 1369 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1370 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1371 | InsertBefore) { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1372 | Init(Operation, Ptr, Val, Ordering, SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1376 | AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1377 | SyncScope::ID SSID, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1378 | BasicBlock *InsertAtEnd) |
| 1379 | : Instruction(Val->getType(), AtomicRMW, |
| 1380 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1381 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1382 | InsertAtEnd) { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1383 | Init(Operation, Ptr, Val, Ordering, SSID); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Matt Arsenault | b02ba99 | 2018-10-02 23:44:11 +0000 | [diff] [blame] | 1386 | StringRef AtomicRMWInst::getOperationName(BinOp Op) { |
| 1387 | switch (Op) { |
| 1388 | case AtomicRMWInst::Xchg: |
| 1389 | return "xchg"; |
| 1390 | case AtomicRMWInst::Add: |
| 1391 | return "add"; |
| 1392 | case AtomicRMWInst::Sub: |
| 1393 | return "sub"; |
| 1394 | case AtomicRMWInst::And: |
| 1395 | return "and"; |
| 1396 | case AtomicRMWInst::Nand: |
| 1397 | return "nand"; |
| 1398 | case AtomicRMWInst::Or: |
| 1399 | return "or"; |
| 1400 | case AtomicRMWInst::Xor: |
| 1401 | return "xor"; |
| 1402 | case AtomicRMWInst::Max: |
| 1403 | return "max"; |
| 1404 | case AtomicRMWInst::Min: |
| 1405 | return "min"; |
| 1406 | case AtomicRMWInst::UMax: |
| 1407 | return "umax"; |
| 1408 | case AtomicRMWInst::UMin: |
| 1409 | return "umin"; |
Matt Arsenault | 3950833 | 2019-01-22 18:18:02 +0000 | [diff] [blame^] | 1410 | case AtomicRMWInst::FAdd: |
| 1411 | return "fadd"; |
| 1412 | case AtomicRMWInst::FSub: |
| 1413 | return "fsub"; |
Matt Arsenault | b02ba99 | 2018-10-02 23:44:11 +0000 | [diff] [blame] | 1414 | case AtomicRMWInst::BAD_BINOP: |
| 1415 | return "<invalid operation>"; |
| 1416 | } |
| 1417 | |
| 1418 | llvm_unreachable("invalid atomicrmw operation"); |
| 1419 | } |
| 1420 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1421 | //===----------------------------------------------------------------------===// |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1422 | // FenceInst Implementation |
| 1423 | //===----------------------------------------------------------------------===// |
| 1424 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1425 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1426 | SyncScope::ID SSID, |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1427 | Instruction *InsertBefore) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1428 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1429 | setOrdering(Ordering); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1430 | setSyncScopeID(SSID); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1433 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1434 | SyncScope::ID SSID, |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1435 | BasicBlock *InsertAtEnd) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1436 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1437 | setOrdering(Ordering); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1438 | setSyncScopeID(SSID); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1442 | // GetElementPtrInst Implementation |
| 1443 | //===----------------------------------------------------------------------===// |
| 1444 | |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1445 | void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1446 | const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1447 | assert(getNumOperands() == 1 + IdxList.size() && |
| 1448 | "NumOperands not initialized?"); |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 1449 | Op<0>() = Ptr; |
Fangrui Song | 7570932 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 1450 | llvm::copy(IdxList, op_begin() + 1); |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1451 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1454 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) |
David Blaikie | 73cf872 | 2015-05-05 18:03:48 +0000 | [diff] [blame] | 1455 | : Instruction(GEPI.getType(), GetElementPtr, |
| 1456 | OperandTraits<GetElementPtrInst>::op_end(this) - |
| 1457 | GEPI.getNumOperands(), |
| 1458 | GEPI.getNumOperands()), |
David Blaikie | f5147ef | 2015-06-01 03:09:34 +0000 | [diff] [blame] | 1459 | SourceElementType(GEPI.SourceElementType), |
| 1460 | ResultElementType(GEPI.ResultElementType) { |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1461 | std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1462 | SubclassOptionalData = GEPI.SubclassOptionalData; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1465 | /// getIndexedType - Returns the type of the element that would be accessed with |
| 1466 | /// a gep instruction with the specified parameters. |
| 1467 | /// |
| 1468 | /// The Idxs pointer should point to a continuous piece of memory containing the |
| 1469 | /// indices, either as Value* or uint64_t. |
| 1470 | /// |
| 1471 | /// A null type is returned if the indices are invalid for the specified |
| 1472 | /// pointer type. |
| 1473 | /// |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1474 | template <typename IndexTy> |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1475 | static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) { |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1476 | // 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] | 1477 | if (IdxList.empty()) |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1478 | return Agg; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1479 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1480 | // 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] | 1481 | // it cannot be 'stepped over'. |
| 1482 | if (!Agg->isSized()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1483 | return nullptr; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1484 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1485 | unsigned CurIdx = 1; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1486 | for (; CurIdx != IdxList.size(); ++CurIdx) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1487 | CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1488 | if (!CT || CT->isPointerTy()) return nullptr; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1489 | IndexTy Index = IdxList[CurIdx]; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1490 | if (!CT->indexValid(Index)) return nullptr; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1491 | Agg = CT->getTypeAtIndex(Index); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1492 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1493 | return CurIdx == IdxList.size() ? Agg : nullptr; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1496 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) { |
| 1497 | return getIndexedTypeInternal(Ty, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1500 | Type *GetElementPtrInst::getIndexedType(Type *Ty, |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1501 | ArrayRef<Constant *> IdxList) { |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1502 | return getIndexedTypeInternal(Ty, IdxList); |
Jay Foad | 1d4a8fe | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1505 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) { |
| 1506 | return getIndexedTypeInternal(Ty, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1509 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1510 | /// zeros. If so, the result pointer and the first operand have the same |
| 1511 | /// value, just potentially different types. |
| 1512 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1513 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1514 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1515 | if (!CI->isZero()) return false; |
| 1516 | } else { |
| 1517 | return false; |
| 1518 | } |
| 1519 | } |
| 1520 | return true; |
| 1521 | } |
| 1522 | |
Chris Lattner | 2705829 | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1523 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1524 | /// constant integers. If so, the result pointer and the first operand have |
| 1525 | /// a constant offset between them. |
| 1526 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1527 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1528 | if (!isa<ConstantInt>(getOperand(i))) |
| 1529 | return false; |
| 1530 | } |
| 1531 | return true; |
| 1532 | } |
| 1533 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1534 | void GetElementPtrInst::setIsInBounds(bool B) { |
| 1535 | cast<GEPOperator>(this)->setIsInBounds(B); |
| 1536 | } |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1537 | |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1538 | bool GetElementPtrInst::isInBounds() const { |
| 1539 | return cast<GEPOperator>(this)->isInBounds(); |
| 1540 | } |
| 1541 | |
Chandler Carruth | 1e14053 | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1542 | bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, |
| 1543 | APInt &Offset) const { |
Chandler Carruth | 7ec41c7 | 2012-12-11 11:05:15 +0000 | [diff] [blame] | 1544 | // Delegate to the generic GEPOperator implementation. |
| 1545 | return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); |
Chandler Carruth | 1e14053 | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1548 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1549 | // ExtractElementInst Implementation |
| 1550 | //===----------------------------------------------------------------------===// |
| 1551 | |
| 1552 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1553 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1554 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1555 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1556 | ExtractElement, |
| 1557 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1558 | 2, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1559 | assert(isValidOperands(Val, Index) && |
| 1560 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1561 | Op<0>() = Val; |
| 1562 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1563 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1567 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1568 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1569 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1570 | ExtractElement, |
| 1571 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1572 | 2, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1573 | assert(isValidOperands(Val, Index) && |
| 1574 | "Invalid extractelement instruction operands!"); |
| 1575 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1576 | Op<0>() = Val; |
| 1577 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1578 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1581 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1582 | if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy()) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1583 | return false; |
| 1584 | return true; |
| 1585 | } |
| 1586 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1587 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1588 | // InsertElementInst Implementation |
| 1589 | //===----------------------------------------------------------------------===// |
| 1590 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1591 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1592 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1593 | Instruction *InsertBef) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1594 | : Instruction(Vec->getType(), InsertElement, |
| 1595 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1596 | 3, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1597 | assert(isValidOperands(Vec, Elt, Index) && |
| 1598 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1599 | Op<0>() = Vec; |
| 1600 | Op<1>() = Elt; |
| 1601 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1602 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1605 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1606 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1607 | BasicBlock *InsertAE) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1608 | : Instruction(Vec->getType(), InsertElement, |
| 1609 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1610 | 3, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1611 | assert(isValidOperands(Vec, Elt, Index) && |
| 1612 | "Invalid insertelement instruction operands!"); |
| 1613 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1614 | Op<0>() = Vec; |
| 1615 | Op<1>() = Elt; |
| 1616 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1617 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1620 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1621 | const Value *Index) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1622 | if (!Vec->getType()->isVectorTy()) |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1623 | return false; // First operand of insertelement must be vector type. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1624 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1625 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1626 | return false;// Second operand of insertelement must be vector element type. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1627 | |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1628 | if (!Index->getType()->isIntegerTy()) |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1629 | return false; // Third operand of insertelement must be i32. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1630 | return true; |
| 1631 | } |
| 1632 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1633 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1634 | // ShuffleVectorInst Implementation |
| 1635 | //===----------------------------------------------------------------------===// |
| 1636 | |
| 1637 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1638 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1639 | Instruction *InsertBefore) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1640 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1641 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1642 | ShuffleVector, |
| 1643 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1644 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1645 | InsertBefore) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1646 | assert(isValidOperands(V1, V2, Mask) && |
| 1647 | "Invalid shuffle vector instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1648 | Op<0>() = V1; |
| 1649 | Op<1>() = V2; |
| 1650 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1651 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1655 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1656 | BasicBlock *InsertAtEnd) |
Dan Gohman | e5af8cd | 2009-08-25 23:27:45 +0000 | [diff] [blame] | 1657 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
| 1658 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1659 | ShuffleVector, |
| 1660 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1661 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1662 | InsertAtEnd) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1663 | assert(isValidOperands(V1, V2, Mask) && |
| 1664 | "Invalid shuffle vector instruction operands!"); |
| 1665 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1666 | Op<0>() = V1; |
| 1667 | Op<1>() = V2; |
| 1668 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1669 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1672 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1673 | const Value *Mask) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1674 | // V1 and V2 must be vectors of the same type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1675 | if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1676 | return false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1677 | |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1678 | // Mask must be vector of i32. |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1679 | auto *MaskTy = dyn_cast<VectorType>(Mask->getType()); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1680 | if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32)) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1681 | return false; |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1682 | |
| 1683 | // Check to see if Mask is valid. |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1684 | if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) |
| 1685 | return true; |
| 1686 | |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1687 | if (const auto *MV = dyn_cast<ConstantVector>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1688 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1689 | for (Value *Op : MV->operands()) { |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1690 | if (auto *CI = dyn_cast<ConstantInt>(Op)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1691 | if (CI->uge(V1Size*2)) |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1692 | return false; |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1693 | } else if (!isa<UndefValue>(Op)) { |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1694 | return false; |
| 1695 | } |
| 1696 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1697 | return true; |
Mon P Wang | 6ebf401 | 2011-10-26 00:34:48 +0000 | [diff] [blame] | 1698 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1699 | |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1700 | if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1701 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
| 1702 | for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i) |
| 1703 | if (CDS->getElementAsInteger(i) >= V1Size*2) |
| 1704 | return false; |
| 1705 | return true; |
| 1706 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1707 | |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1708 | // The bitcode reader can create a place holder for a forward reference |
| 1709 | // used as the shuffle mask. When this occurs, the shuffle mask will |
| 1710 | // fall into this case and fail. To avoid this error, do this bit of |
| 1711 | // ugliness to allow such a mask pass. |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1712 | if (const auto *CE = dyn_cast<ConstantExpr>(Mask)) |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1713 | if (CE->getOpcode() == Instruction::UserOp1) |
| 1714 | return true; |
| 1715 | |
| 1716 | return false; |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1719 | int ShuffleVectorInst::getMaskValue(const Constant *Mask, unsigned i) { |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1720 | assert(i < Mask->getType()->getVectorNumElements() && "Index out of range"); |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1721 | if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1722 | return CDS->getElementAsInteger(i); |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1723 | Constant *C = Mask->getAggregateElement(i); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1724 | if (isa<UndefValue>(C)) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1725 | return -1; |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1726 | return cast<ConstantInt>(C)->getZExtValue(); |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1729 | void ShuffleVectorInst::getShuffleMask(const Constant *Mask, |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1730 | SmallVectorImpl<int> &Result) { |
| 1731 | unsigned NumElts = Mask->getType()->getVectorNumElements(); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1732 | |
Sanjay Patel | 8bd5228 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1733 | if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1734 | for (unsigned i = 0; i != NumElts; ++i) |
| 1735 | Result.push_back(CDS->getElementAsInteger(i)); |
| 1736 | return; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1737 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1738 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1739 | Constant *C = Mask->getAggregateElement(i); |
| 1740 | Result.push_back(isa<UndefValue>(C) ? -1 : |
Chris Lattner | 3dbad403 | 2012-01-26 00:41:50 +0000 | [diff] [blame] | 1741 | cast<ConstantInt>(C)->getZExtValue()); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1745 | static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) { |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1746 | assert(!Mask.empty() && "Shuffle mask must contain elements"); |
| 1747 | bool UsesLHS = false; |
| 1748 | bool UsesRHS = false; |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1749 | for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) { |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1750 | if (Mask[i] == -1) |
| 1751 | continue; |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1752 | assert(Mask[i] >= 0 && Mask[i] < (NumOpElts * 2) && |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1753 | "Out-of-bounds shuffle mask element"); |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1754 | UsesLHS |= (Mask[i] < NumOpElts); |
| 1755 | UsesRHS |= (Mask[i] >= NumOpElts); |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1756 | if (UsesLHS && UsesRHS) |
| 1757 | return false; |
| 1758 | } |
| 1759 | assert((UsesLHS ^ UsesRHS) && "Should have selected from exactly 1 source"); |
| 1760 | return true; |
| 1761 | } |
| 1762 | |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1763 | bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) { |
| 1764 | // We don't have vector operand size information, so assume operands are the |
| 1765 | // same size as the mask. |
| 1766 | return isSingleSourceMaskImpl(Mask, Mask.size()); |
| 1767 | } |
| 1768 | |
| 1769 | static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) { |
| 1770 | if (!isSingleSourceMaskImpl(Mask, NumOpElts)) |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1771 | return false; |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1772 | for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) { |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1773 | if (Mask[i] == -1) |
| 1774 | continue; |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1775 | if (Mask[i] != i && Mask[i] != (NumOpElts + i)) |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1776 | return false; |
| 1777 | } |
| 1778 | return true; |
| 1779 | } |
| 1780 | |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1781 | bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) { |
| 1782 | // We don't have vector operand size information, so assume operands are the |
| 1783 | // same size as the mask. |
| 1784 | return isIdentityMaskImpl(Mask, Mask.size()); |
| 1785 | } |
| 1786 | |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1787 | bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) { |
| 1788 | if (!isSingleSourceMask(Mask)) |
| 1789 | return false; |
| 1790 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { |
| 1791 | if (Mask[i] == -1) |
| 1792 | continue; |
| 1793 | if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i)) |
| 1794 | return false; |
| 1795 | } |
| 1796 | return true; |
| 1797 | } |
| 1798 | |
| 1799 | bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) { |
| 1800 | if (!isSingleSourceMask(Mask)) |
| 1801 | return false; |
| 1802 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { |
| 1803 | if (Mask[i] == -1) |
| 1804 | continue; |
| 1805 | if (Mask[i] != 0 && Mask[i] != NumElts) |
| 1806 | return false; |
| 1807 | } |
| 1808 | return true; |
| 1809 | } |
| 1810 | |
| 1811 | bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) { |
| 1812 | // Select is differentiated from identity. It requires using both sources. |
| 1813 | if (isSingleSourceMask(Mask)) |
| 1814 | return false; |
| 1815 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { |
| 1816 | if (Mask[i] == -1) |
| 1817 | continue; |
| 1818 | if (Mask[i] != i && Mask[i] != (NumElts + i)) |
| 1819 | return false; |
| 1820 | } |
| 1821 | return true; |
| 1822 | } |
| 1823 | |
| 1824 | bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) { |
| 1825 | // Example masks that will return true: |
| 1826 | // v1 = <a, b, c, d> |
| 1827 | // v2 = <e, f, g, h> |
| 1828 | // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g> |
| 1829 | // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h> |
| 1830 | |
| 1831 | // 1. The number of elements in the mask must be a power-of-2 and at least 2. |
| 1832 | int NumElts = Mask.size(); |
| 1833 | if (NumElts < 2 || !isPowerOf2_32(NumElts)) |
| 1834 | return false; |
| 1835 | |
| 1836 | // 2. The first element of the mask must be either a 0 or a 1. |
| 1837 | if (Mask[0] != 0 && Mask[0] != 1) |
| 1838 | return false; |
| 1839 | |
| 1840 | // 3. The difference between the first 2 elements must be equal to the |
| 1841 | // number of elements in the mask. |
| 1842 | if ((Mask[1] - Mask[0]) != NumElts) |
| 1843 | return false; |
| 1844 | |
| 1845 | // 4. The difference between consecutive even-numbered and odd-numbered |
| 1846 | // elements must be equal to 2. |
| 1847 | for (int i = 2; i < NumElts; ++i) { |
| 1848 | int MaskEltVal = Mask[i]; |
| 1849 | if (MaskEltVal == -1) |
| 1850 | return false; |
| 1851 | int MaskEltPrevVal = Mask[i - 2]; |
| 1852 | if (MaskEltVal - MaskEltPrevVal != 2) |
| 1853 | return false; |
| 1854 | } |
| 1855 | return true; |
| 1856 | } |
| 1857 | |
Simon Pilgrim | d0c7160 | 2018-11-09 16:28:19 +0000 | [diff] [blame] | 1858 | bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask, |
| 1859 | int NumSrcElts, int &Index) { |
| 1860 | // Must extract from a single source. |
| 1861 | if (!isSingleSourceMaskImpl(Mask, NumSrcElts)) |
| 1862 | return false; |
| 1863 | |
| 1864 | // Must be smaller (else this is an Identity shuffle). |
Fangrui Song | 4f2e66c | 2018-11-09 16:45:37 +0000 | [diff] [blame] | 1865 | if (NumSrcElts <= (int)Mask.size()) |
Simon Pilgrim | d0c7160 | 2018-11-09 16:28:19 +0000 | [diff] [blame] | 1866 | return false; |
| 1867 | |
| 1868 | // Find start of extraction, accounting that we may start with an UNDEF. |
| 1869 | int SubIndex = -1; |
| 1870 | for (int i = 0, e = Mask.size(); i != e; ++i) { |
| 1871 | int M = Mask[i]; |
| 1872 | if (M < 0) |
| 1873 | continue; |
| 1874 | int Offset = (M % NumSrcElts) - i; |
| 1875 | if (0 <= SubIndex && SubIndex != Offset) |
| 1876 | return false; |
| 1877 | SubIndex = Offset; |
| 1878 | } |
| 1879 | |
| 1880 | if (0 <= SubIndex) { |
| 1881 | Index = SubIndex; |
| 1882 | return true; |
| 1883 | } |
| 1884 | return false; |
| 1885 | } |
| 1886 | |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1887 | bool ShuffleVectorInst::isIdentityWithPadding() const { |
| 1888 | int NumOpElts = Op<0>()->getType()->getVectorNumElements(); |
| 1889 | int NumMaskElts = getType()->getVectorNumElements(); |
| 1890 | if (NumMaskElts <= NumOpElts) |
| 1891 | return false; |
| 1892 | |
| 1893 | // The first part of the mask must choose elements from exactly 1 source op. |
Sanjay Patel | 8d39ed8 | 2018-08-30 16:44:07 +0000 | [diff] [blame] | 1894 | SmallVector<int, 16> Mask = getShuffleMask(); |
Sanjay Patel | ac619a0 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1895 | if (!isIdentityMaskImpl(Mask, NumOpElts)) |
| 1896 | return false; |
| 1897 | |
| 1898 | // All extending must be with undef elements. |
| 1899 | for (int i = NumOpElts; i < NumMaskElts; ++i) |
| 1900 | if (Mask[i] != -1) |
| 1901 | return false; |
| 1902 | |
| 1903 | return true; |
| 1904 | } |
| 1905 | |
| 1906 | bool ShuffleVectorInst::isIdentityWithExtract() const { |
| 1907 | int NumOpElts = Op<0>()->getType()->getVectorNumElements(); |
| 1908 | int NumMaskElts = getType()->getVectorNumElements(); |
| 1909 | if (NumMaskElts >= NumOpElts) |
| 1910 | return false; |
| 1911 | |
| 1912 | return isIdentityMaskImpl(getShuffleMask(), NumOpElts); |
| 1913 | } |
Sanjay Patel | 2ca3360 | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1914 | |
Sanjay Patel | fd4976b | 2018-09-20 15:21:52 +0000 | [diff] [blame] | 1915 | bool ShuffleVectorInst::isConcat() const { |
| 1916 | // Vector concatenation is differentiated from identity with padding. |
| 1917 | if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>())) |
| 1918 | return false; |
| 1919 | |
| 1920 | int NumOpElts = Op<0>()->getType()->getVectorNumElements(); |
| 1921 | int NumMaskElts = getType()->getVectorNumElements(); |
| 1922 | if (NumMaskElts != NumOpElts * 2) |
| 1923 | return false; |
| 1924 | |
| 1925 | // Use the mask length rather than the operands' vector lengths here. We |
| 1926 | // already know that the shuffle returns a vector twice as long as the inputs, |
| 1927 | // and neither of the inputs are undef vectors. If the mask picks consecutive |
| 1928 | // elements from both inputs, then this is a concatenation of the inputs. |
| 1929 | return isIdentityMaskImpl(getShuffleMask(), NumMaskElts); |
| 1930 | } |
| 1931 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1932 | //===----------------------------------------------------------------------===// |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1933 | // InsertValueInst Class |
| 1934 | //===----------------------------------------------------------------------===// |
| 1935 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1936 | void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1937 | const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1938 | assert(getNumOperands() == 2 && "NumOperands not initialized?"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1939 | |
| 1940 | // There's no fundamental reason why we require at least one index |
| 1941 | // (other than weirdness with &*IdxBegin being invalid; see |
| 1942 | // getelementptr's init routine for example). But there's no |
| 1943 | // present need to support it. |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 1944 | assert(!Idxs.empty() && "InsertValueInst must have at least one index"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1945 | |
| 1946 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1947 | Val->getType() && "Inserted value must match indexed type!"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1948 | Op<0>() = Agg; |
| 1949 | Op<1>() = Val; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1950 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1951 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1952 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1956 | : Instruction(IVI.getType(), InsertValue, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1957 | OperandTraits<InsertValueInst>::op_begin(this), 2), |
| 1958 | Indices(IVI.Indices) { |
Dan Gohman | d8ca05f | 2008-06-17 23:25:49 +0000 | [diff] [blame] | 1959 | Op<0>() = IVI.getOperand(0); |
| 1960 | Op<1>() = IVI.getOperand(1); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1961 | SubclassOptionalData = IVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | //===----------------------------------------------------------------------===// |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1965 | // ExtractValueInst Class |
| 1966 | //===----------------------------------------------------------------------===// |
| 1967 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1968 | void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1969 | assert(getNumOperands() == 1 && "NumOperands not initialized?"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1970 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1971 | // There's no fundamental reason why we require at least one index. |
| 1972 | // But there's no present need to support it. |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 1973 | assert(!Idxs.empty() && "ExtractValueInst must have at least one index"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1974 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1975 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1976 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1980 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1981 | Indices(EVI.Indices) { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1982 | SubclassOptionalData = EVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1985 | // getIndexedType - Returns the type of the element that would be extracted |
| 1986 | // with an extractvalue instruction with the specified parameters. |
| 1987 | // |
| 1988 | // A null type is returned if the indices are invalid for the specified |
| 1989 | // pointer type. |
| 1990 | // |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1991 | Type *ExtractValueInst::getIndexedType(Type *Agg, |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1992 | ArrayRef<unsigned> Idxs) { |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1993 | for (unsigned Index : Idxs) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1994 | // We can't use CompositeType::indexValid(Index) here. |
| 1995 | // indexValid() always returns true for arrays because getelementptr allows |
| 1996 | // out-of-bounds indices. Since we don't allow those for extractvalue and |
| 1997 | // insertvalue we need to check array indexing manually. |
| 1998 | // Since the only other types we can index into are struct types it's just |
| 1999 | // as easy to check those manually as well. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2000 | if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2001 | if (Index >= AT->getNumElements()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2002 | return nullptr; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2003 | } else if (StructType *ST = dyn_cast<StructType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2004 | if (Index >= ST->getNumElements()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2005 | return nullptr; |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2006 | } else { |
| 2007 | // Not a valid type to index into. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2008 | return nullptr; |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2012 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2013 | return const_cast<Type*>(Agg); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2014 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2015 | |
| 2016 | //===----------------------------------------------------------------------===// |
Cameron McInally | cbde0d9 | 2018-11-13 18:15:47 +0000 | [diff] [blame] | 2017 | // UnaryOperator Class |
| 2018 | //===----------------------------------------------------------------------===// |
| 2019 | |
| 2020 | UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, |
| 2021 | Type *Ty, const Twine &Name, |
| 2022 | Instruction *InsertBefore) |
| 2023 | : UnaryInstruction(Ty, iType, S, InsertBefore) { |
| 2024 | Op<0>() = S; |
| 2025 | setName(Name); |
| 2026 | AssertOK(); |
| 2027 | } |
| 2028 | |
| 2029 | UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, |
| 2030 | Type *Ty, const Twine &Name, |
| 2031 | BasicBlock *InsertAtEnd) |
| 2032 | : UnaryInstruction(Ty, iType, S, InsertAtEnd) { |
| 2033 | Op<0>() = S; |
| 2034 | setName(Name); |
| 2035 | AssertOK(); |
| 2036 | } |
| 2037 | |
| 2038 | UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, |
| 2039 | const Twine &Name, |
| 2040 | Instruction *InsertBefore) { |
| 2041 | return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore); |
| 2042 | } |
| 2043 | |
| 2044 | UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, |
| 2045 | const Twine &Name, |
| 2046 | BasicBlock *InsertAtEnd) { |
| 2047 | UnaryOperator *Res = Create(Op, S, Name); |
| 2048 | InsertAtEnd->getInstList().push_back(Res); |
| 2049 | return Res; |
| 2050 | } |
| 2051 | |
| 2052 | void UnaryOperator::AssertOK() { |
| 2053 | Value *LHS = getOperand(0); |
| 2054 | (void)LHS; // Silence warnings. |
| 2055 | #ifndef NDEBUG |
| 2056 | switch (getOpcode()) { |
| 2057 | case FNeg: |
| 2058 | assert(getType() == LHS->getType() && |
| 2059 | "Unary operation should return same type as operand!"); |
| 2060 | assert(getType()->isFPOrFPVectorTy() && |
| 2061 | "Tried to create a floating-point operation on a " |
| 2062 | "non-floating-point type!"); |
| 2063 | break; |
| 2064 | default: llvm_unreachable("Invalid opcode provided"); |
| 2065 | } |
| 2066 | #endif |
| 2067 | } |
| 2068 | |
| 2069 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2070 | // BinaryOperator Class |
| 2071 | //===----------------------------------------------------------------------===// |
| 2072 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2073 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2074 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2075 | Instruction *InsertBefore) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2076 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2077 | OperandTraits<BinaryOperator>::op_begin(this), |
| 2078 | OperandTraits<BinaryOperator>::operands(this), |
| 2079 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2080 | Op<0>() = S1; |
| 2081 | Op<1>() = S2; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2082 | setName(Name); |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2083 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2086 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2087 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2088 | BasicBlock *InsertAtEnd) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2089 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2090 | OperandTraits<BinaryOperator>::op_begin(this), |
| 2091 | OperandTraits<BinaryOperator>::operands(this), |
| 2092 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2093 | Op<0>() = S1; |
| 2094 | Op<1>() = S2; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2095 | setName(Name); |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2096 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2099 | void BinaryOperator::AssertOK() { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2100 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Jeffrey Yasskin | 9b43f33 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 2101 | (void)LHS; (void)RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2102 | assert(LHS->getType() == RHS->getType() && |
| 2103 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2104 | #ifndef NDEBUG |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2105 | switch (getOpcode()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2106 | case Add: case Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2107 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2108 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2109 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2110 | assert(getType()->isIntOrIntVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2111 | "Tried to create an integer operation on a non-integer type!"); |
| 2112 | break; |
| 2113 | case FAdd: case FSub: |
| 2114 | case FMul: |
| 2115 | assert(getType() == LHS->getType() && |
| 2116 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2117 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2118 | "Tried to create a floating-point operation on a " |
| 2119 | "non-floating-point type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2120 | break; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2121 | case UDiv: |
| 2122 | case SDiv: |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2123 | assert(getType() == LHS->getType() && |
| 2124 | "Arithmetic operation should return same type as operands!"); |
Craig Topper | d1fbb38 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 2125 | assert(getType()->isIntOrIntVectorTy() && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2126 | "Incorrect operand type (not integer) for S/UDIV"); |
| 2127 | break; |
| 2128 | case FDiv: |
| 2129 | assert(getType() == LHS->getType() && |
| 2130 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2131 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2132 | "Incorrect operand type (not floating point) for FDIV"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2133 | break; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2134 | case URem: |
| 2135 | case SRem: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2136 | assert(getType() == LHS->getType() && |
| 2137 | "Arithmetic operation should return same type as operands!"); |
Craig Topper | d1fbb38 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 2138 | assert(getType()->isIntOrIntVectorTy() && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2139 | "Incorrect operand type (not integer) for S/UREM"); |
| 2140 | break; |
| 2141 | case FRem: |
| 2142 | assert(getType() == LHS->getType() && |
| 2143 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2144 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2145 | "Incorrect operand type (not floating point) for FREM"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2146 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2147 | case Shl: |
| 2148 | case LShr: |
| 2149 | case AShr: |
| 2150 | assert(getType() == LHS->getType() && |
| 2151 | "Shift operation should return same type as operands!"); |
Craig Topper | d1fbb38 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 2152 | assert(getType()->isIntOrIntVectorTy() && |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 2153 | "Tried to create a shift operation on a non-integral type!"); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2154 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2155 | case And: case Or: |
| 2156 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2157 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2158 | "Logical operation should return same type as operands!"); |
Craig Topper | d1fbb38 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 2159 | assert(getType()->isIntOrIntVectorTy() && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 2160 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2161 | break; |
Craig Topper | 700892f | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2162 | default: llvm_unreachable("Invalid opcode provided"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2163 | } |
| 2164 | #endif |
| 2165 | } |
| 2166 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2167 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2168 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2169 | Instruction *InsertBefore) { |
| 2170 | assert(S1->getType() == S2->getType() && |
| 2171 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2172 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2175 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2176 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2177 | BasicBlock *InsertAtEnd) { |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2178 | BinaryOperator *Res = Create(Op, S1, S2, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2179 | InsertAtEnd->getInstList().push_back(Res); |
| 2180 | return Res; |
| 2181 | } |
| 2182 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2183 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2184 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2185 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2186 | return new BinaryOperator(Instruction::Sub, |
| 2187 | zero, Op, |
| 2188 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2191 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2192 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2193 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2194 | return new BinaryOperator(Instruction::Sub, |
| 2195 | zero, Op, |
| 2196 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Dan Gohman | 4ab4420 | 2009-12-18 02:58:50 +0000 | [diff] [blame] | 2199 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 2200 | Instruction *InsertBefore) { |
| 2201 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2202 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); |
| 2203 | } |
| 2204 | |
| 2205 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 2206 | BasicBlock *InsertAtEnd) { |
| 2207 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2208 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); |
| 2209 | } |
| 2210 | |
Duncan Sands | fa5f596 | 2010-02-02 12:53:04 +0000 | [diff] [blame] | 2211 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 2212 | Instruction *InsertBefore) { |
| 2213 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2214 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); |
| 2215 | } |
| 2216 | |
| 2217 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 2218 | BasicBlock *InsertAtEnd) { |
| 2219 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2220 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); |
| 2221 | } |
| 2222 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2223 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2224 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2225 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2226 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2227 | Op->getType(), Name, InsertBefore); |
| 2228 | } |
| 2229 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2230 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2231 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2232 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2233 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2234 | Op->getType(), Name, InsertAtEnd); |
| 2235 | } |
| 2236 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2237 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2238 | Instruction *InsertBefore) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2239 | Constant *C = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 2240 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2241 | Op->getType(), Name, InsertBefore); |
| 2242 | } |
| 2243 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2244 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2245 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2246 | Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 2247 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2248 | Op->getType(), Name, InsertAtEnd); |
| 2249 | } |
| 2250 | |
Sanjay Patel | 4ce99d4 | 2016-11-16 18:09:44 +0000 | [diff] [blame] | 2251 | // Exchange the two operands to this instruction. This instruction is safe to |
| 2252 | // use on any binary instruction and does not modify the semantics of the |
| 2253 | // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode |
| 2254 | // is changed. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2255 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2256 | if (!isCommutative()) |
| 2257 | return true; // Can't commute operands |
Gabor Greif | 5ef7404 | 2008-05-13 22:51:52 +0000 | [diff] [blame] | 2258 | Op<0>().swap(Op<1>()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2259 | return false; |
| 2260 | } |
| 2261 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2262 | //===----------------------------------------------------------------------===// |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2263 | // FPMathOperator Class |
| 2264 | //===----------------------------------------------------------------------===// |
| 2265 | |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2266 | float FPMathOperator::getFPAccuracy() const { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 2267 | const MDNode *MD = |
| 2268 | cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2269 | if (!MD) |
| 2270 | return 0.0; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2271 | ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0)); |
Duncan Sands | 9af6298 | 2012-04-16 19:39:33 +0000 | [diff] [blame] | 2272 | return Accuracy->getValueAPF().convertToFloat(); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2275 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2276 | // CastInst Class |
| 2277 | //===----------------------------------------------------------------------===// |
| 2278 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2279 | // Just determine if this cast only deals with integral->integral conversion. |
| 2280 | bool CastInst::isIntegerCast() const { |
| 2281 | switch (getOpcode()) { |
| 2282 | default: return false; |
| 2283 | case Instruction::ZExt: |
| 2284 | case Instruction::SExt: |
| 2285 | case Instruction::Trunc: |
| 2286 | return true; |
| 2287 | case Instruction::BitCast: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2288 | return getOperand(0)->getType()->isIntegerTy() && |
| 2289 | getType()->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2290 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2293 | bool CastInst::isLosslessCast() const { |
| 2294 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 2295 | if (getOpcode() != Instruction::BitCast) |
| 2296 | return false; |
| 2297 | |
| 2298 | // Identity cast is always lossless |
Ana Pazos | b359602 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 2299 | Type *SrcTy = getOperand(0)->getType(); |
| 2300 | Type *DstTy = getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2301 | if (SrcTy == DstTy) |
| 2302 | return true; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2303 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 2304 | // Pointer to pointer is always lossless. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2305 | if (SrcTy->isPointerTy()) |
| 2306 | return DstTy->isPointerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2307 | return false; // Other types have no identity values |
| 2308 | } |
| 2309 | |
| 2310 | /// This function determines if the CastInst does not require any bits to be |
| 2311 | /// changed in order to effect the cast. Essentially, it identifies cases where |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2312 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2313 | /// example, the following are all no-op casts: |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 2314 | /// # bitcast i32* %x to i8* |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2315 | /// # bitcast <2 x i32> %x to <4 x i16> |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 2316 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only |
Adrian Prantl | 4dfcc4a | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 2317 | /// Determine if the described cast is a no-op. |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2318 | bool CastInst::isNoopCast(Instruction::CastOps Opcode, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2319 | Type *SrcTy, |
| 2320 | Type *DestTy, |
Mikael Holmen | 0ec1d25 | 2017-10-05 07:07:09 +0000 | [diff] [blame] | 2321 | const DataLayout &DL) { |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2322 | switch (Opcode) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2323 | default: llvm_unreachable("Invalid CastOp"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2324 | case Instruction::Trunc: |
| 2325 | case Instruction::ZExt: |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2326 | case Instruction::SExt: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2327 | case Instruction::FPTrunc: |
| 2328 | case Instruction::FPExt: |
| 2329 | case Instruction::UIToFP: |
| 2330 | case Instruction::SIToFP: |
| 2331 | case Instruction::FPToUI: |
| 2332 | case Instruction::FPToSI: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2333 | case Instruction::AddrSpaceCast: |
| 2334 | // TODO: Target informations may give a more accurate answer here. |
| 2335 | return false; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2336 | case Instruction::BitCast: |
| 2337 | return true; // BitCast never modifies bits. |
| 2338 | case Instruction::PtrToInt: |
Mikael Holmen | 0ec1d25 | 2017-10-05 07:07:09 +0000 | [diff] [blame] | 2339 | return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2340 | DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2341 | case Instruction::IntToPtr: |
Mikael Holmen | 0ec1d25 | 2017-10-05 07:07:09 +0000 | [diff] [blame] | 2342 | return DL.getIntPtrType(DestTy)->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2343 | SrcTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2344 | } |
| 2345 | } |
| 2346 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2347 | bool CastInst::isNoopCast(const DataLayout &DL) const { |
Mikael Holmen | 6efe507 | 2017-10-03 06:03:49 +0000 | [diff] [blame] | 2348 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL); |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | /// This function determines if a pair of casts can be eliminated and what |
| 2352 | /// 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] | 2353 | /// instructions like this: |
| 2354 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 2355 | /// * %S = secondOpcode MidTy %F to DstTy |
| 2356 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 2357 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
Sanjay Patel | 4dad27e | 2015-12-11 18:12:01 +0000 | [diff] [blame] | 2358 | /// If no such cast is permitted, the function returns 0. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2359 | unsigned CastInst::isEliminableCastPair( |
| 2360 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2361 | Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, |
| 2362 | Type *DstIntPtrTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2363 | // Define the 144 possibilities for these two cast instructions. The values |
| 2364 | // in this matrix determine what to do in a given situation and select the |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2365 | // case in the switch below. The rows correspond to firstOp, the columns |
Sanjay Patel | 4dad27e | 2015-12-11 18:12:01 +0000 | [diff] [blame] | 2366 | // correspond to secondOp. In looking at the table below, keep in mind |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2367 | // the following cast properties: |
| 2368 | // |
| 2369 | // Size Compare Source Destination |
| 2370 | // Operator Src ? Size Type Sign Type Sign |
| 2371 | // -------- ------------ ------------------- --------------------- |
| 2372 | // TRUNC > Integer Any Integral Any |
| 2373 | // ZEXT < Integral Unsigned Integer Any |
| 2374 | // SEXT < Integral Signed Integer Any |
| 2375 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2376 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 2377 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 2378 | // SITOFP n/a Integral Signed FloatPt n/a |
| 2379 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 2380 | // FPEXT < FloatPt n/a FloatPt n/a |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2381 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 2382 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2383 | // BITCAST = FirstClass n/a FirstClass n/a |
| 2384 | // ADDRSPCST n/a Pointer n/a Pointer n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2385 | // |
| 2386 | // 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] | 2387 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", |
| 2388 | // into "fptoui double to i64", but this loses information about the range |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2389 | // 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] | 2390 | // Further this conversion is often much more expensive for typical hardware, |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2391 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2392 | // same reason. |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2393 | const unsigned numCastOps = |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2394 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 2395 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2396 | // T F F U S F F P I B A -+ |
| 2397 | // R Z S P P I I T P 2 N T S | |
| 2398 | // U E E 2 2 2 2 R E I T C C +- secondOp |
| 2399 | // N X X U S F F N X N 2 V V | |
| 2400 | // C T T I I P P C T T P T T -+ |
| 2401 | { 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] | 2402 | { 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] | 2403 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt | |
| 2404 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI | |
| 2405 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI | |
| 2406 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp |
| 2407 | { 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] | 2408 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc | |
Craig Topper | 18799f4 | 2018-03-02 18:16:51 +0000 | [diff] [blame] | 2409 | { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2410 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt | |
| 2411 | { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr | |
| 2412 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast | |
| 2413 | { 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] | 2414 | }; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2415 | |
Sanjay Patel | 93f55dd | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2416 | // TODO: This logic could be encoded into the table above and handled in the |
| 2417 | // switch below. |
Chris Lattner | 25eea4d | 2010-07-12 01:19:22 +0000 | [diff] [blame] | 2418 | // 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] | 2419 | // merging. However, any pair of bitcasts are allowed. |
| 2420 | bool IsFirstBitcast = (firstOp == Instruction::BitCast); |
| 2421 | bool IsSecondBitcast = (secondOp == Instruction::BitCast); |
| 2422 | bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast; |
Nadav Rotem | 5fc81ff | 2011-08-29 19:58:36 +0000 | [diff] [blame] | 2423 | |
Sanjay Patel | 93f55dd | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2424 | // Check if any of the casts convert scalars <-> vectors. |
| 2425 | if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || |
| 2426 | (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) |
| 2427 | if (!AreBothBitcasts) |
| 2428 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2429 | |
| 2430 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 2431 | [secondOp-Instruction::CastOpsBegin]; |
| 2432 | switch (ElimCase) { |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2433 | case 0: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2434 | // Categorically disallowed. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2435 | return 0; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2436 | case 1: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2437 | // Allowed, use first cast's opcode. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2438 | return firstOp; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2439 | case 2: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2440 | // Allowed, use second cast's opcode. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2441 | return secondOp; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2442 | case 3: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2443 | // 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] | 2444 | // is integer and we are not converting between a vector and a |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 2445 | // non-vector type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2446 | if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2447 | return firstOp; |
| 2448 | return 0; |
| 2449 | case 4: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2450 | // 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] | 2451 | // is floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2452 | if (DstTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2453 | return firstOp; |
| 2454 | return 0; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2455 | case 5: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2456 | // 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] | 2457 | // is an integer. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2458 | if (SrcTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2459 | return secondOp; |
| 2460 | return 0; |
| 2461 | case 6: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2462 | // 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] | 2463 | // is a floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2464 | if (SrcTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2465 | return secondOp; |
| 2466 | return 0; |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2467 | case 7: { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2468 | // Cannot simplify if address spaces are different! |
| 2469 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) |
| 2470 | return 0; |
| 2471 | |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2472 | unsigned MidSize = MidTy->getScalarSizeInBits(); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2473 | // We can still fold this without knowing the actual sizes as long we |
| 2474 | // know that the intermediate pointer is the largest possible |
| 2475 | // pointer size. |
| 2476 | // FIXME: Is this always true? |
| 2477 | if (MidSize == 64) |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2478 | return Instruction::BitCast; |
| 2479 | |
| 2480 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size. |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2481 | if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2482 | return 0; |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2483 | unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2484 | if (MidSize >= PtrSize) |
| 2485 | return Instruction::BitCast; |
| 2486 | return 0; |
| 2487 | } |
| 2488 | case 8: { |
| 2489 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 2490 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 2491 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2492 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2493 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2494 | if (SrcSize == DstSize) |
| 2495 | return Instruction::BitCast; |
| 2496 | else if (SrcSize < DstSize) |
| 2497 | return firstOp; |
| 2498 | return secondOp; |
| 2499 | } |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2500 | case 9: |
| 2501 | // zext, sext -> zext, because sext can't sign extend after zext |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2502 | return Instruction::ZExt; |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2503 | case 11: { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2504 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2505 | if (!MidIntPtrTy) |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2506 | return 0; |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2507 | unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2508 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2509 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2510 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 2511 | return Instruction::BitCast; |
| 2512 | return 0; |
| 2513 | } |
Eugene Zelenko | d761e2c | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 2514 | case 12: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2515 | // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS |
| 2516 | // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS |
| 2517 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) |
| 2518 | return Instruction::AddrSpaceCast; |
| 2519 | return Instruction::BitCast; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2520 | case 13: |
| 2521 | // FIXME: this state can be merged with (1), but the following assert |
| 2522 | // is useful to check the correcteness of the sequence due to semantic |
| 2523 | // change of bitcast. |
| 2524 | assert( |
| 2525 | SrcTy->isPtrOrPtrVectorTy() && |
| 2526 | MidTy->isPtrOrPtrVectorTy() && |
| 2527 | DstTy->isPtrOrPtrVectorTy() && |
| 2528 | SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() && |
| 2529 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && |
| 2530 | "Illegal addrspacecast, bitcast sequence!"); |
| 2531 | // Allowed, use first cast's opcode |
| 2532 | return firstOp; |
| 2533 | case 14: |
Jingyue Wu | 77145d9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 2534 | // bitcast, addrspacecast -> addrspacecast if the element type of |
| 2535 | // bitcast's source is the same as that of addrspacecast's destination. |
Peter Collingbourne | 9d5fd4d | 2016-11-13 06:58:45 +0000 | [diff] [blame] | 2536 | if (SrcTy->getScalarType()->getPointerElementType() == |
| 2537 | DstTy->getScalarType()->getPointerElementType()) |
Jingyue Wu | 77145d9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 2538 | return Instruction::AddrSpaceCast; |
| 2539 | return 0; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2540 | case 15: |
| 2541 | // FIXME: this state can be merged with (1), but the following assert |
| 2542 | // is useful to check the correcteness of the sequence due to semantic |
| 2543 | // change of bitcast. |
| 2544 | assert( |
| 2545 | SrcTy->isIntOrIntVectorTy() && |
| 2546 | MidTy->isPtrOrPtrVectorTy() && |
| 2547 | DstTy->isPtrOrPtrVectorTy() && |
| 2548 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && |
| 2549 | "Illegal inttoptr, bitcast sequence!"); |
| 2550 | // Allowed, use first cast's opcode |
| 2551 | return firstOp; |
| 2552 | case 16: |
| 2553 | // FIXME: this state can be merged with (2), but the following assert |
| 2554 | // is useful to check the correcteness of the sequence due to semantic |
| 2555 | // change of bitcast. |
| 2556 | assert( |
| 2557 | SrcTy->isPtrOrPtrVectorTy() && |
| 2558 | MidTy->isPtrOrPtrVectorTy() && |
| 2559 | DstTy->isIntOrIntVectorTy() && |
| 2560 | SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() && |
| 2561 | "Illegal bitcast, ptrtoint sequence!"); |
| 2562 | // Allowed, use second cast's opcode |
| 2563 | return secondOp; |
Fiona Glaser | 0d41db1 | 2015-04-21 00:05:41 +0000 | [diff] [blame] | 2564 | case 17: |
| 2565 | // (sitofp (zext x)) -> (uitofp x) |
| 2566 | return Instruction::UIToFP; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2567 | case 99: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2568 | // 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] | 2569 | // where the MidTy is not the same for the two cast instructions. |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2570 | llvm_unreachable("Invalid Cast Combination"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2571 | default: |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2572 | llvm_unreachable("Error in CastResults table!!!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2573 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2576 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2577 | const Twine &Name, Instruction *InsertBefore) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2578 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2579 | // Construct and return the appropriate CastInst subclass |
| 2580 | switch (op) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2581 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 2582 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 2583 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 2584 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 2585 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 2586 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 2587 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 2588 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 2589 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 2590 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 2591 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 2592 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 2593 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore); |
| 2594 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2595 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2598 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2599 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2600 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2601 | // Construct and return the appropriate CastInst subclass |
| 2602 | switch (op) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2603 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 2604 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 2605 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 2606 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 2607 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 2608 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2609 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2610 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 2611 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 2612 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 2613 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 2614 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 2615 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd); |
| 2616 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2617 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2620 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2621 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2622 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2623 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2624 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2625 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2628 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2629 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2630 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2631 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2632 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2633 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2636 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2637 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2638 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2639 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2640 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2641 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2644 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2645 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2646 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2647 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2648 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2649 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2652 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2653 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2654 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2655 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2656 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2657 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2660 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2661 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2662 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2663 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2664 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2665 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2668 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2669 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2670 | BasicBlock *InsertAtEnd) { |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2671 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2672 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
| 2673 | "Invalid cast"); |
| 2674 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); |
Richard Trieu | 8dc4323 | 2013-07-31 04:07:28 +0000 | [diff] [blame] | 2675 | assert((!Ty->isVectorTy() || |
| 2676 | Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2677 | "Invalid cast"); |
| 2678 | |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2679 | if (Ty->isIntOrIntVectorTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2680 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2681 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2682 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Adrian Prantl | 4dfcc4a | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 2685 | /// Create a BitCast or a PtrToInt cast instruction |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2686 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
| 2687 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2688 | Instruction *InsertBefore) { |
Evgeniy Stepanov | c9bd35b | 2013-01-15 16:43:00 +0000 | [diff] [blame] | 2689 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2690 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2691 | "Invalid cast"); |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2692 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); |
Richard Trieu | 8dc4323 | 2013-07-31 04:07:28 +0000 | [diff] [blame] | 2693 | assert((!Ty->isVectorTy() || |
| 2694 | Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2695 | "Invalid cast"); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2696 | |
Evgeniy Stepanov | c9bd35b | 2013-01-15 16:43:00 +0000 | [diff] [blame] | 2697 | if (Ty->isIntOrIntVectorTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2698 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2699 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2700 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore); |
| 2701 | } |
| 2702 | |
| 2703 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( |
| 2704 | Value *S, Type *Ty, |
| 2705 | const Twine &Name, |
| 2706 | BasicBlock *InsertAtEnd) { |
| 2707 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2708 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2709 | |
| 2710 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
| 2711 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd); |
| 2712 | |
| 2713 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2714 | } |
| 2715 | |
| 2716 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( |
| 2717 | Value *S, Type *Ty, |
| 2718 | const Twine &Name, |
| 2719 | Instruction *InsertBefore) { |
| 2720 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2721 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2722 | |
| 2723 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2724 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore); |
| 2725 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2726 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2729 | CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, |
| 2730 | const Twine &Name, |
| 2731 | Instruction *InsertBefore) { |
| 2732 | if (S->getType()->isPointerTy() && Ty->isIntegerTy()) |
| 2733 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 2734 | if (S->getType()->isIntegerTy() && Ty->isPointerTy()) |
| 2735 | return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); |
| 2736 | |
| 2737 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2738 | } |
| 2739 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2740 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2741 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2742 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2743 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Chris Lattner | 5370ae7 | 2010-01-10 20:21:42 +0000 | [diff] [blame] | 2744 | "Invalid integer cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2745 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2746 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2747 | Instruction::CastOps opcode = |
| 2748 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2749 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2750 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2751 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2754 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2755 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2756 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2757 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2758 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2759 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2760 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2761 | Instruction::CastOps opcode = |
| 2762 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2763 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2764 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2765 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2768 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
| 2769 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2770 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2771 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2772 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2773 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2774 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2775 | Instruction::CastOps opcode = |
| 2776 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2777 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2778 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2781 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
| 2782 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2783 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2784 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2785 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2786 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2787 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2788 | Instruction::CastOps opcode = |
| 2789 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2790 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2791 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2794 | // Check whether it is valid to call getCastOpcode for these types. |
| 2795 | // This routine must be kept in sync with getCastOpcode. |
| 2796 | bool CastInst::isCastable(Type *SrcTy, Type *DestTy) { |
| 2797 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2798 | return false; |
| 2799 | |
| 2800 | if (SrcTy == DestTy) |
| 2801 | return true; |
| 2802 | |
| 2803 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2804 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
| 2805 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2806 | // An element by element cast. Valid if casting the elements is valid. |
| 2807 | SrcTy = SrcVecTy->getElementType(); |
| 2808 | DestTy = DestVecTy->getElementType(); |
| 2809 | } |
| 2810 | |
| 2811 | // Get the bit sizes, we'll need these |
| 2812 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2813 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2814 | |
| 2815 | // Run through the possibilities ... |
| 2816 | if (DestTy->isIntegerTy()) { // Casting to integral |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2817 | if (SrcTy->isIntegerTy()) // Casting from integral |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2818 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2819 | if (SrcTy->isFloatingPointTy()) // Casting from floating pt |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2820 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2821 | if (SrcTy->isVectorTy()) // Casting from vector |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2822 | return DestBits == SrcBits; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2823 | // Casting from something else |
| 2824 | return SrcTy->isPointerTy(); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2825 | } |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2826 | if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2827 | if (SrcTy->isIntegerTy()) // Casting from integral |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2828 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2829 | if (SrcTy->isFloatingPointTy()) // Casting from floating pt |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2830 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2831 | if (SrcTy->isVectorTy()) // Casting from vector |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2832 | return DestBits == SrcBits; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2833 | // Casting from something else |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2834 | return false; |
| 2835 | } |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2836 | if (DestTy->isVectorTy()) // Casting to vector |
| 2837 | return DestBits == SrcBits; |
| 2838 | if (DestTy->isPointerTy()) { // Casting to pointer |
| 2839 | if (SrcTy->isPointerTy()) // Casting from pointer |
| 2840 | return true; |
| 2841 | return SrcTy->isIntegerTy(); // Casting from integral |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2842 | } |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2843 | if (DestTy->isX86_MMXTy()) { |
| 2844 | if (SrcTy->isVectorTy()) |
| 2845 | return DestBits == SrcBits; // 64-bit vector to MMX |
| 2846 | return false; |
| 2847 | } // Casting to something else |
| 2848 | return false; |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2851 | bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { |
| 2852 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2853 | return false; |
| 2854 | |
| 2855 | if (SrcTy == DestTy) |
| 2856 | return true; |
| 2857 | |
| 2858 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 2859 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) { |
| 2860 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2861 | // An element by element cast. Valid if casting the elements is valid. |
| 2862 | SrcTy = SrcVecTy->getElementType(); |
| 2863 | DestTy = DestVecTy->getElementType(); |
| 2864 | } |
| 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) { |
| 2869 | if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) { |
| 2870 | return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); |
| 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2875 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2876 | |
| 2877 | // Could still have vectors of pointers if the number of elements doesn't |
| 2878 | // match |
| 2879 | if (SrcBits == 0 || DestBits == 0) |
| 2880 | return false; |
| 2881 | |
| 2882 | if (SrcBits != DestBits) |
| 2883 | return false; |
| 2884 | |
| 2885 | if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) |
| 2886 | return false; |
| 2887 | |
| 2888 | return true; |
| 2889 | } |
| 2890 | |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2891 | bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2892 | const DataLayout &DL) { |
Yichao Yu | 92c11ee | 2017-10-22 20:28:17 +0000 | [diff] [blame] | 2893 | // ptrtoint and inttoptr are not allowed on non-integral pointers |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2894 | if (auto *PtrTy = dyn_cast<PointerType>(SrcTy)) |
| 2895 | if (auto *IntTy = dyn_cast<IntegerType>(DestTy)) |
Yichao Yu | 92c11ee | 2017-10-22 20:28:17 +0000 | [diff] [blame] | 2896 | return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && |
| 2897 | !DL.isNonIntegralPointerType(PtrTy)); |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2898 | if (auto *PtrTy = dyn_cast<PointerType>(DestTy)) |
| 2899 | if (auto *IntTy = dyn_cast<IntegerType>(SrcTy)) |
Yichao Yu | 92c11ee | 2017-10-22 20:28:17 +0000 | [diff] [blame] | 2900 | return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && |
| 2901 | !DL.isNonIntegralPointerType(PtrTy)); |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2902 | |
| 2903 | return isBitCastable(SrcTy, DestTy); |
| 2904 | } |
| 2905 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2906 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 2907 | // 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] | 2908 | // logic in the castIsValid function below. This axiom should hold: |
| 2909 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 2910 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2911 | // casting opcode for the arguments passed to it. |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2912 | // This routine must be kept in sync with isCastable. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2913 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2914 | CastInst::getCastOpcode( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2915 | const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { |
| 2916 | Type *SrcTy = Src->getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2917 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2918 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && |
| 2919 | "Only first class types are castable!"); |
| 2920 | |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2921 | if (SrcTy == DestTy) |
| 2922 | return BitCast; |
| 2923 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2924 | // FIXME: Check address space sizes here |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2925 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2926 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2927 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2928 | // An element by element cast. Find the appropriate opcode based on the |
| 2929 | // element types. |
| 2930 | SrcTy = SrcVecTy->getElementType(); |
| 2931 | DestTy = DestVecTy->getElementType(); |
| 2932 | } |
| 2933 | |
| 2934 | // Get the bit sizes, we'll need these |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2935 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2936 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2937 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2938 | // Run through the possibilities ... |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2939 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 2940 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2941 | if (DestBits < SrcBits) |
| 2942 | return Trunc; // int -> smaller int |
| 2943 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2944 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2945 | return SExt; // signed -> SEXT |
| 2946 | else |
| 2947 | return ZExt; // unsigned -> ZEXT |
| 2948 | } else { |
| 2949 | return BitCast; // Same size, No-op cast |
| 2950 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2951 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2952 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2953 | return FPToSI; // FP -> sint |
| 2954 | else |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2955 | return FPToUI; // FP -> uint |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2956 | } else if (SrcTy->isVectorTy()) { |
| 2957 | assert(DestBits == SrcBits && |
| 2958 | "Casting vector to integer of different width"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2959 | return BitCast; // Same size, no-op cast |
| 2960 | } else { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2961 | assert(SrcTy->isPointerTy() && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2962 | "Casting from a value that is not first-class type"); |
| 2963 | return PtrToInt; // ptr -> int |
| 2964 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2965 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2966 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2967 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2968 | return SIToFP; // sint -> FP |
| 2969 | else |
| 2970 | return UIToFP; // uint -> FP |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2971 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2972 | if (DestBits < SrcBits) { |
| 2973 | return FPTrunc; // FP -> smaller FP |
| 2974 | } else if (DestBits > SrcBits) { |
| 2975 | return FPExt; // FP -> larger FP |
| 2976 | } else { |
| 2977 | return BitCast; // same size, no-op cast |
| 2978 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2979 | } else if (SrcTy->isVectorTy()) { |
| 2980 | assert(DestBits == SrcBits && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2981 | "Casting vector to floating point of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 2982 | return BitCast; // same size, no-op cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2983 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2984 | llvm_unreachable("Casting pointer or non-first class to float"); |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2985 | } else if (DestTy->isVectorTy()) { |
| 2986 | assert(DestBits == SrcBits && |
| 2987 | "Illegal cast to vector (wrong type or size)"); |
| 2988 | return BitCast; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2989 | } else if (DestTy->isPointerTy()) { |
| 2990 | if (SrcTy->isPointerTy()) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2991 | if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace()) |
| 2992 | return AddrSpaceCast; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2993 | return BitCast; // ptr -> ptr |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2994 | } else if (SrcTy->isIntegerTy()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2995 | return IntToPtr; // int -> ptr |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2996 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2997 | llvm_unreachable("Casting pointer to other than pointer or int"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2998 | } else if (DestTy->isX86_MMXTy()) { |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2999 | if (SrcTy->isVectorTy()) { |
| 3000 | assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 3001 | return BitCast; // 64-bit vector to MMX |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 3002 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3003 | llvm_unreachable("Illegal cast to X86_MMX"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3004 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3005 | llvm_unreachable("Casting to type that is not first-class"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | //===----------------------------------------------------------------------===// |
| 3009 | // CastInst SubClass Constructors |
| 3010 | //===----------------------------------------------------------------------===// |
| 3011 | |
| 3012 | /// Check that the construction parameters for a CastInst are correct. This |
| 3013 | /// could be broken out into the separate constructors but it is useful to have |
| 3014 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 3015 | /// of the types involved. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3016 | bool |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3017 | CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3018 | // Check for type sanity on the arguments |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3019 | Type *SrcTy = S->getType(); |
Evan Cheng | 098d7b7 | 2013-01-10 23:22:53 +0000 | [diff] [blame] | 3020 | |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 3021 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || |
| 3022 | SrcTy->isAggregateType() || DstTy->isAggregateType()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3023 | return false; |
| 3024 | |
| 3025 | // 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] | 3026 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 3027 | unsigned DstBitSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3028 | |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3029 | // If these are vector types, get the lengths of the vectors (using zero for |
| 3030 | // scalar types means that checking that vector lengths match also checks that |
| 3031 | // scalars are not being converted to vectors or vectors to scalars). |
| 3032 | unsigned SrcLength = SrcTy->isVectorTy() ? |
| 3033 | cast<VectorType>(SrcTy)->getNumElements() : 0; |
| 3034 | unsigned DstLength = DstTy->isVectorTy() ? |
| 3035 | cast<VectorType>(DstTy)->getNumElements() : 0; |
| 3036 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3037 | // Switch on the opcode provided |
| 3038 | switch (op) { |
| 3039 | default: return false; // This is an input error |
| 3040 | case Instruction::Trunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3041 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3042 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3043 | case Instruction::ZExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3044 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3045 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3046 | case Instruction::SExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3047 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3048 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3049 | case Instruction::FPTrunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3050 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3051 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3052 | case Instruction::FPExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3053 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3054 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3055 | case Instruction::UIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3056 | case Instruction::SIToFP: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3057 | return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3058 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3059 | case Instruction::FPToUI: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3060 | case Instruction::FPToSI: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3061 | return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3062 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3063 | case Instruction::PtrToInt: |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3064 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3065 | return false; |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3066 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 3067 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 3068 | return false; |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 3069 | return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3070 | case Instruction::IntToPtr: |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3071 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3072 | return false; |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3073 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 3074 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 3075 | return false; |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 3076 | return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy(); |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3077 | case Instruction::BitCast: { |
| 3078 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); |
| 3079 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); |
| 3080 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3081 | // BitCast implies a no-op cast of type only. No bits change. |
| 3082 | // However, you can't cast pointers to anything but pointers. |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3083 | if (!SrcPtrTy != !DstPtrTy) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3084 | return false; |
| 3085 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 3086 | // 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] | 3087 | // widths are identical. |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3088 | if (!SrcPtrTy) |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3089 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); |
| 3090 | |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3091 | // If both are pointers then the address spaces must match. |
| 3092 | if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) |
| 3093 | return false; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3094 | |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3095 | // A vector of pointers must have the same number of elements. |
Serguei Katkov | 09ab506 | 2018-08-21 04:27:07 +0000 | [diff] [blame] | 3096 | VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy); |
| 3097 | VectorType *DstVecTy = dyn_cast<VectorType>(DstTy); |
| 3098 | if (SrcVecTy && DstVecTy) |
| 3099 | return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); |
| 3100 | if (SrcVecTy) |
| 3101 | return SrcVecTy->getNumElements() == 1; |
| 3102 | if (DstVecTy) |
| 3103 | return DstVecTy->getNumElements() == 1; |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3104 | |
| 3105 | return true; |
| 3106 | } |
| 3107 | case Instruction::AddrSpaceCast: { |
| 3108 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); |
| 3109 | if (!SrcPtrTy) |
| 3110 | return false; |
| 3111 | |
| 3112 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); |
| 3113 | if (!DstPtrTy) |
| 3114 | return false; |
| 3115 | |
| 3116 | if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) |
| 3117 | return false; |
| 3118 | |
| 3119 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 3120 | if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy)) |
| 3121 | return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); |
| 3122 | |
| 3123 | return false; |
| 3124 | } |
| 3125 | |
| 3126 | return true; |
| 3127 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3132 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3133 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3134 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3138 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3139 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3140 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3144 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3145 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3146 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3150 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3151 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3152 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3153 | } |
| 3154 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3155 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3156 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3157 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 3160 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3161 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3162 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3163 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3167 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3168 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3169 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3173 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3174 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3175 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3179 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3180 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3181 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3185 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3186 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3187 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3191 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3192 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3193 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3197 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3198 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3199 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3203 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3204 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3205 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3209 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3210 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3211 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
| 3214 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3215 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3216 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3217 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3221 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3222 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3223 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3227 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3228 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3229 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3233 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3234 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3235 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3239 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3240 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3241 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3245 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3246 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3247 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
| 3250 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3251 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3252 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3253 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3257 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3258 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3259 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3263 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3264 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3265 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3269 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3270 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3271 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3272 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3273 | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3274 | AddrSpaceCastInst::AddrSpaceCastInst( |
| 3275 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
| 3276 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) { |
| 3277 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); |
| 3278 | } |
| 3279 | |
| 3280 | AddrSpaceCastInst::AddrSpaceCastInst( |
| 3281 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
| 3282 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) { |
| 3283 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); |
| 3284 | } |
| 3285 | |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3286 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3287 | // CmpInst Classes |
| 3288 | //===----------------------------------------------------------------------===// |
| 3289 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3290 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, |
Sanjay Patel | d1172a0 | 2018-11-07 00:00:42 +0000 | [diff] [blame] | 3291 | Value *RHS, const Twine &Name, Instruction *InsertBefore, |
| 3292 | Instruction *FlagsSource) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3293 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3294 | OperandTraits<CmpInst>::op_begin(this), |
| 3295 | OperandTraits<CmpInst>::operands(this), |
| 3296 | InsertBefore) { |
Sanjay Patel | d1172a0 | 2018-11-07 00:00:42 +0000 | [diff] [blame] | 3297 | Op<0>() = LHS; |
| 3298 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3299 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3300 | setName(Name); |
Sanjay Patel | d1172a0 | 2018-11-07 00:00:42 +0000 | [diff] [blame] | 3301 | if (FlagsSource) |
| 3302 | copyIRFlags(FlagsSource); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3303 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3304 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3305 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, |
| 3306 | Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3307 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3308 | OperandTraits<CmpInst>::op_begin(this), |
| 3309 | OperandTraits<CmpInst>::operands(this), |
| 3310 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3311 | Op<0>() = LHS; |
| 3312 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3313 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3314 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
| 3317 | CmpInst * |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3318 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3319 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3320 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3321 | if (InsertBefore) |
| 3322 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3323 | S1, S2, Name); |
| 3324 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3325 | return new ICmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3326 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3327 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3328 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3329 | if (InsertBefore) |
| 3330 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3331 | S1, S2, Name); |
| 3332 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3333 | return new FCmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3334 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | CmpInst * |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3338 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3339 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3340 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3341 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3342 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3343 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3344 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3345 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3346 | } |
| 3347 | |
| 3348 | void CmpInst::swapOperands() { |
| 3349 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 3350 | IC->swapOperands(); |
| 3351 | else |
| 3352 | cast<FCmpInst>(this)->swapOperands(); |
| 3353 | } |
| 3354 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3355 | bool CmpInst::isCommutative() const { |
| 3356 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3357 | return IC->isCommutative(); |
| 3358 | return cast<FCmpInst>(this)->isCommutative(); |
| 3359 | } |
| 3360 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3361 | bool CmpInst::isEquality() const { |
| 3362 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3363 | return IC->isEquality(); |
| 3364 | return cast<FCmpInst>(this)->isEquality(); |
| 3365 | } |
| 3366 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3367 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3368 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3369 | default: llvm_unreachable("Unknown cmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3370 | case ICMP_EQ: return ICMP_NE; |
| 3371 | case ICMP_NE: return ICMP_EQ; |
| 3372 | case ICMP_UGT: return ICMP_ULE; |
| 3373 | case ICMP_ULT: return ICMP_UGE; |
| 3374 | case ICMP_UGE: return ICMP_ULT; |
| 3375 | case ICMP_ULE: return ICMP_UGT; |
| 3376 | case ICMP_SGT: return ICMP_SLE; |
| 3377 | case ICMP_SLT: return ICMP_SGE; |
| 3378 | case ICMP_SGE: return ICMP_SLT; |
| 3379 | case ICMP_SLE: return ICMP_SGT; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3380 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3381 | case FCMP_OEQ: return FCMP_UNE; |
| 3382 | case FCMP_ONE: return FCMP_UEQ; |
| 3383 | case FCMP_OGT: return FCMP_ULE; |
| 3384 | case FCMP_OLT: return FCMP_UGE; |
| 3385 | case FCMP_OGE: return FCMP_ULT; |
| 3386 | case FCMP_OLE: return FCMP_UGT; |
| 3387 | case FCMP_UEQ: return FCMP_ONE; |
| 3388 | case FCMP_UNE: return FCMP_OEQ; |
| 3389 | case FCMP_UGT: return FCMP_OLE; |
| 3390 | case FCMP_ULT: return FCMP_OGE; |
| 3391 | case FCMP_UGE: return FCMP_OLT; |
| 3392 | case FCMP_ULE: return FCMP_OGT; |
| 3393 | case FCMP_ORD: return FCMP_UNO; |
| 3394 | case FCMP_UNO: return FCMP_ORD; |
| 3395 | case FCMP_TRUE: return FCMP_FALSE; |
| 3396 | case FCMP_FALSE: return FCMP_TRUE; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3397 | } |
| 3398 | } |
| 3399 | |
Tim Northover | de3aea041 | 2016-08-17 20:25:25 +0000 | [diff] [blame] | 3400 | StringRef CmpInst::getPredicateName(Predicate Pred) { |
| 3401 | switch (Pred) { |
| 3402 | default: return "unknown"; |
| 3403 | case FCmpInst::FCMP_FALSE: return "false"; |
| 3404 | case FCmpInst::FCMP_OEQ: return "oeq"; |
| 3405 | case FCmpInst::FCMP_OGT: return "ogt"; |
| 3406 | case FCmpInst::FCMP_OGE: return "oge"; |
| 3407 | case FCmpInst::FCMP_OLT: return "olt"; |
| 3408 | case FCmpInst::FCMP_OLE: return "ole"; |
| 3409 | case FCmpInst::FCMP_ONE: return "one"; |
| 3410 | case FCmpInst::FCMP_ORD: return "ord"; |
| 3411 | case FCmpInst::FCMP_UNO: return "uno"; |
| 3412 | case FCmpInst::FCMP_UEQ: return "ueq"; |
| 3413 | case FCmpInst::FCMP_UGT: return "ugt"; |
| 3414 | case FCmpInst::FCMP_UGE: return "uge"; |
| 3415 | case FCmpInst::FCMP_ULT: return "ult"; |
| 3416 | case FCmpInst::FCMP_ULE: return "ule"; |
| 3417 | case FCmpInst::FCMP_UNE: return "une"; |
| 3418 | case FCmpInst::FCMP_TRUE: return "true"; |
| 3419 | case ICmpInst::ICMP_EQ: return "eq"; |
| 3420 | case ICmpInst::ICMP_NE: return "ne"; |
| 3421 | case ICmpInst::ICMP_SGT: return "sgt"; |
| 3422 | case ICmpInst::ICMP_SGE: return "sge"; |
| 3423 | case ICmpInst::ICMP_SLT: return "slt"; |
| 3424 | case ICmpInst::ICMP_SLE: return "sle"; |
| 3425 | case ICmpInst::ICMP_UGT: return "ugt"; |
| 3426 | case ICmpInst::ICMP_UGE: return "uge"; |
| 3427 | case ICmpInst::ICMP_ULT: return "ult"; |
| 3428 | case ICmpInst::ICMP_ULE: return "ule"; |
| 3429 | } |
| 3430 | } |
| 3431 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3432 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 3433 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3434 | default: llvm_unreachable("Unknown icmp predicate!"); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3435 | case ICMP_EQ: case ICMP_NE: |
| 3436 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3437 | return pred; |
| 3438 | case ICMP_UGT: return ICMP_SGT; |
| 3439 | case ICMP_ULT: return ICMP_SLT; |
| 3440 | case ICMP_UGE: return ICMP_SGE; |
| 3441 | case ICMP_ULE: return ICMP_SLE; |
| 3442 | } |
| 3443 | } |
| 3444 | |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3445 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { |
| 3446 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3447 | default: llvm_unreachable("Unknown icmp predicate!"); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3448 | case ICMP_EQ: case ICMP_NE: |
| 3449 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3450 | return pred; |
| 3451 | case ICMP_SGT: return ICMP_UGT; |
| 3452 | case ICMP_SLT: return ICMP_ULT; |
| 3453 | case ICMP_SGE: return ICMP_UGE; |
| 3454 | case ICMP_SLE: return ICMP_ULE; |
| 3455 | } |
| 3456 | } |
| 3457 | |
Serguei Katkov | 3cb4c34 | 2018-02-09 07:59:07 +0000 | [diff] [blame] | 3458 | CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) { |
| 3459 | switch (pred) { |
| 3460 | default: llvm_unreachable("Unknown or unsupported cmp predicate!"); |
| 3461 | case ICMP_SGT: return ICMP_SGE; |
| 3462 | case ICMP_SLT: return ICMP_SLE; |
| 3463 | case ICMP_SGE: return ICMP_SGT; |
| 3464 | case ICMP_SLE: return ICMP_SLT; |
| 3465 | case ICMP_UGT: return ICMP_UGE; |
| 3466 | case ICMP_ULT: return ICMP_ULE; |
| 3467 | case ICMP_UGE: return ICMP_UGT; |
| 3468 | case ICMP_ULE: return ICMP_ULT; |
| 3469 | |
| 3470 | case FCMP_OGT: return FCMP_OGE; |
| 3471 | case FCMP_OLT: return FCMP_OLE; |
| 3472 | case FCMP_OGE: return FCMP_OGT; |
| 3473 | case FCMP_OLE: return FCMP_OLT; |
| 3474 | case FCMP_UGT: return FCMP_UGE; |
| 3475 | case FCMP_ULT: return FCMP_ULE; |
| 3476 | case FCMP_UGE: return FCMP_UGT; |
| 3477 | case FCMP_ULE: return FCMP_ULT; |
| 3478 | } |
| 3479 | } |
| 3480 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3481 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3482 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3483 | default: llvm_unreachable("Unknown cmp predicate!"); |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3484 | case ICMP_EQ: case ICMP_NE: |
| 3485 | return pred; |
| 3486 | case ICMP_SGT: return ICMP_SLT; |
| 3487 | case ICMP_SLT: return ICMP_SGT; |
| 3488 | case ICMP_SGE: return ICMP_SLE; |
| 3489 | case ICMP_SLE: return ICMP_SGE; |
| 3490 | case ICMP_UGT: return ICMP_ULT; |
| 3491 | case ICMP_ULT: return ICMP_UGT; |
| 3492 | case ICMP_UGE: return ICMP_ULE; |
| 3493 | case ICMP_ULE: return ICMP_UGE; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3494 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3495 | case FCMP_FALSE: case FCMP_TRUE: |
| 3496 | case FCMP_OEQ: case FCMP_ONE: |
| 3497 | case FCMP_UEQ: case FCMP_UNE: |
| 3498 | case FCMP_ORD: case FCMP_UNO: |
| 3499 | return pred; |
| 3500 | case FCMP_OGT: return FCMP_OLT; |
| 3501 | case FCMP_OLT: return FCMP_OGT; |
| 3502 | case FCMP_OGE: return FCMP_OLE; |
| 3503 | case FCMP_OLE: return FCMP_OGE; |
| 3504 | case FCMP_UGT: return FCMP_ULT; |
| 3505 | case FCMP_ULT: return FCMP_UGT; |
| 3506 | case FCMP_UGE: return FCMP_ULE; |
| 3507 | case FCMP_ULE: return FCMP_UGE; |
| 3508 | } |
| 3509 | } |
| 3510 | |
Max Kazantsev | b299ade | 2018-02-07 11:16:29 +0000 | [diff] [blame] | 3511 | CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) { |
| 3512 | switch (pred) { |
| 3513 | case ICMP_SGT: return ICMP_SGE; |
| 3514 | case ICMP_SLT: return ICMP_SLE; |
| 3515 | case ICMP_UGT: return ICMP_UGE; |
| 3516 | case ICMP_ULT: return ICMP_ULE; |
| 3517 | case FCMP_OGT: return FCMP_OGE; |
| 3518 | case FCMP_OLT: return FCMP_OLE; |
| 3519 | case FCMP_UGT: return FCMP_UGE; |
| 3520 | case FCMP_ULT: return FCMP_ULE; |
| 3521 | default: return pred; |
| 3522 | } |
| 3523 | } |
| 3524 | |
Sanjoy Das | 6e78b17 | 2015-10-22 19:57:34 +0000 | [diff] [blame] | 3525 | CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) { |
| 3526 | assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!"); |
| 3527 | |
| 3528 | switch (pred) { |
| 3529 | default: |
| 3530 | llvm_unreachable("Unknown predicate!"); |
| 3531 | case CmpInst::ICMP_ULT: |
| 3532 | return CmpInst::ICMP_SLT; |
| 3533 | case CmpInst::ICMP_ULE: |
| 3534 | return CmpInst::ICMP_SLE; |
| 3535 | case CmpInst::ICMP_UGT: |
| 3536 | return CmpInst::ICMP_SGT; |
| 3537 | case CmpInst::ICMP_UGE: |
| 3538 | return CmpInst::ICMP_SGE; |
| 3539 | } |
| 3540 | } |
| 3541 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3542 | bool CmpInst::isUnsigned(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3543 | switch (predicate) { |
| 3544 | default: return false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3545 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3546 | case ICmpInst::ICMP_UGE: return true; |
| 3547 | } |
| 3548 | } |
| 3549 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3550 | bool CmpInst::isSigned(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3551 | switch (predicate) { |
| 3552 | default: return false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3553 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3554 | case ICmpInst::ICMP_SGE: return true; |
| 3555 | } |
| 3556 | } |
| 3557 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3558 | bool CmpInst::isOrdered(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3559 | switch (predicate) { |
| 3560 | default: return false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3561 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 3562 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3563 | case FCmpInst::FCMP_ORD: return true; |
| 3564 | } |
| 3565 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3566 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3567 | bool CmpInst::isUnordered(Predicate predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3568 | switch (predicate) { |
| 3569 | default: return false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3570 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 3571 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3572 | case FCmpInst::FCMP_UNO: return true; |
| 3573 | } |
| 3574 | } |
| 3575 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3576 | bool CmpInst::isTrueWhenEqual(Predicate predicate) { |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3577 | switch(predicate) { |
| 3578 | default: return false; |
| 3579 | case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: |
| 3580 | case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; |
| 3581 | } |
| 3582 | } |
| 3583 | |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3584 | bool CmpInst::isFalseWhenEqual(Predicate predicate) { |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3585 | switch(predicate) { |
| 3586 | case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: |
| 3587 | case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; |
| 3588 | default: return false; |
| 3589 | } |
| 3590 | } |
| 3591 | |
Chad Rosier | 99bc480 | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 3592 | bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) { |
Chad Rosier | af83e40 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3593 | // If the predicates match, then we know the first condition implies the |
| 3594 | // second is true. |
| 3595 | if (Pred1 == Pred2) |
| 3596 | return true; |
| 3597 | |
| 3598 | switch (Pred1) { |
| 3599 | default: |
| 3600 | break; |
Chad Rosier | 1a60159 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3601 | case ICMP_EQ: |
Chad Rosier | 3d75f8c | 2016-04-25 13:25:14 +0000 | [diff] [blame] | 3602 | // 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] | 3603 | return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE || |
| 3604 | Pred2 == ICMP_SLE; |
Chad Rosier | 3456cb5 | 2016-04-22 17:14:12 +0000 | [diff] [blame] | 3605 | case ICMP_UGT: // A >u B implies A != B and A >=u B are true. |
| 3606 | return Pred2 == ICMP_NE || Pred2 == ICMP_UGE; |
| 3607 | case ICMP_ULT: // A <u B implies A != B and A <=u B are true. |
| 3608 | return Pred2 == ICMP_NE || Pred2 == ICMP_ULE; |
| 3609 | case ICMP_SGT: // A >s B implies A != B and A >=s B are true. |
| 3610 | return Pred2 == ICMP_NE || Pred2 == ICMP_SGE; |
| 3611 | case ICMP_SLT: // A <s B implies A != B and A <=s B are true. |
| 3612 | return Pred2 == ICMP_NE || Pred2 == ICMP_SLE; |
Chad Rosier | af83e40 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3613 | } |
| 3614 | return false; |
| 3615 | } |
| 3616 | |
Chad Rosier | 99bc480 | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 3617 | bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) { |
Chad Rosier | 1a60159 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3618 | return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2)); |
Chad Rosier | af83e40 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3619 | } |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3620 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3621 | //===----------------------------------------------------------------------===// |
| 3622 | // SwitchInst Implementation |
| 3623 | //===----------------------------------------------------------------------===// |
| 3624 | |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3625 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { |
| 3626 | assert(Value && Default && NumReserved); |
| 3627 | ReservedSpace = NumReserved; |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3628 | setNumHungOffUseOperands(2); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3629 | allocHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3630 | |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 3631 | Op<0>() = Value; |
| 3632 | Op<1>() = Default; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3635 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3636 | /// switch on and a default destination. The number of additional cases can |
| 3637 | /// be specified here to make memory allocation more efficient. This |
| 3638 | /// constructor can also autoinsert before another instruction. |
| 3639 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3640 | Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3641 | : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 3642 | nullptr, 0, InsertBefore) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3643 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
| 3646 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3647 | /// switch on and a default destination. The number of additional cases can |
| 3648 | /// be specified here to make memory allocation more efficient. This |
| 3649 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 3650 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3651 | BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3652 | : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 3653 | nullptr, 0, InsertAtEnd) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3654 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3657 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3658 | : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3659 | init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3660 | setNumHungOffUseOperands(SI.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3661 | Use *OL = getOperandList(); |
| 3662 | const Use *InOL = SI.getOperandList(); |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3663 | for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3664 | OL[i] = InOL[i]; |
| 3665 | OL[i+1] = InOL[i+1]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3666 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3667 | SubclassOptionalData = SI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | /// addCase - Add an entry to the switch instruction... |
| 3671 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 3672 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3673 | unsigned NewCaseIdx = getNumCases(); |
| 3674 | unsigned OpNo = getNumOperands(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3675 | if (OpNo+2 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3676 | growOperands(); // Get more space! |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3677 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 3678 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3679 | setNumHungOffUseOperands(OpNo+2); |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 3680 | CaseHandle Case(this, NewCaseIdx); |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3681 | Case.setValue(OnVal); |
Stepan Dyatkovskiy | 5b648af | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 3682 | Case.setSuccessor(Dest); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3685 | /// removeCase - This method removes the specified case and its successor |
| 3686 | /// from the switch instruction. |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 3687 | SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) { |
| 3688 | unsigned idx = I->getCaseIndex(); |
| 3689 | |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3690 | assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3691 | |
| 3692 | unsigned NumOps = getNumOperands(); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3693 | Use *OL = getOperandList(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3694 | |
Jay Foad | 1427772 | 2011-02-01 09:22:34 +0000 | [diff] [blame] | 3695 | // Overwrite this case with the end of the list. |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3696 | if (2 + (idx + 1) * 2 != NumOps) { |
| 3697 | OL[2 + idx * 2] = OL[NumOps - 2]; |
| 3698 | OL[2 + idx * 2 + 1] = OL[NumOps - 1]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3702 | OL[NumOps-2].set(nullptr); |
| 3703 | OL[NumOps-2+1].set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3704 | setNumHungOffUseOperands(NumOps-2); |
Chandler Carruth | 0d256c0 | 2017-03-26 02:49:23 +0000 | [diff] [blame] | 3705 | |
| 3706 | return CaseIt(this, idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3709 | /// growOperands - grow operands - This grows the operand list in response |
| 3710 | /// 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] | 3711 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3712 | void SwitchInst::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3713 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3714 | unsigned NumOps = e*3; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3715 | |
| 3716 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 3717 | growHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3720 | //===----------------------------------------------------------------------===// |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3721 | // IndirectBrInst Implementation |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3722 | //===----------------------------------------------------------------------===// |
| 3723 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3724 | void IndirectBrInst::init(Value *Address, unsigned NumDests) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3725 | assert(Address && Address->getType()->isPointerTy() && |
Chris Lattner | 6747b4c | 2009-10-29 05:53:32 +0000 | [diff] [blame] | 3726 | "Address of indirectbr must be a pointer"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3727 | ReservedSpace = 1+NumDests; |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3728 | setNumHungOffUseOperands(1); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3729 | allocHungoffUses(ReservedSpace); |
| 3730 | |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 3731 | Op<0>() = Address; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3735 | /// growOperands - grow operands - This grows the operand list in response |
| 3736 | /// 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] | 3737 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3738 | void IndirectBrInst::growOperands() { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3739 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3740 | unsigned NumOps = e*2; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3741 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3742 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 3743 | growHungoffUses(ReservedSpace); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3744 | } |
| 3745 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3746 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3747 | Instruction *InsertBefore) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3748 | : Instruction(Type::getVoidTy(Address->getContext()), |
| 3749 | Instruction::IndirectBr, nullptr, 0, InsertBefore) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3750 | init(Address, NumCases); |
| 3751 | } |
| 3752 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3753 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3754 | BasicBlock *InsertAtEnd) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3755 | : Instruction(Type::getVoidTy(Address->getContext()), |
| 3756 | Instruction::IndirectBr, nullptr, 0, InsertAtEnd) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3757 | init(Address, NumCases); |
| 3758 | } |
| 3759 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3760 | IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) |
Chandler Carruth | 509e20e | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3761 | : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, |
| 3762 | nullptr, IBI.getNumOperands()) { |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3763 | allocHungoffUses(IBI.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3764 | Use *OL = getOperandList(); |
| 3765 | const Use *InOL = IBI.getOperandList(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3766 | for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) |
| 3767 | OL[i] = InOL[i]; |
| 3768 | SubclassOptionalData = IBI.SubclassOptionalData; |
| 3769 | } |
| 3770 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3771 | /// addDestination - Add a destination. |
| 3772 | /// |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3773 | void IndirectBrInst::addDestination(BasicBlock *DestBB) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3774 | unsigned OpNo = getNumOperands(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3775 | if (OpNo+1 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3776 | growOperands(); // Get more space! |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3777 | // Initialize some new operands. |
| 3778 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3779 | setNumHungOffUseOperands(OpNo+1); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3780 | getOperandList()[OpNo] = DestBB; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
| 3783 | /// removeDestination - This method removes the specified successor from the |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3784 | /// indirectbr instruction. |
| 3785 | void IndirectBrInst::removeDestination(unsigned idx) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3786 | assert(idx < getNumOperands()-1 && "Successor index out of range!"); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3787 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3788 | unsigned NumOps = getNumOperands(); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3789 | Use *OL = getOperandList(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3790 | |
| 3791 | // Replace this value with the last one. |
| 3792 | OL[idx+1] = OL[NumOps-1]; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3793 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3794 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3795 | OL[NumOps-1].set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3796 | setNumHungOffUseOperands(NumOps-1); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3799 | //===----------------------------------------------------------------------===// |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3800 | // cloneImpl() implementations |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3801 | //===----------------------------------------------------------------------===// |
| 3802 | |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3803 | // Define these methods here so vtables don't get emitted into every translation |
| 3804 | // unit that uses these classes. |
| 3805 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3806 | GetElementPtrInst *GetElementPtrInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3807 | return new (getNumOperands()) GetElementPtrInst(*this); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
Cameron McInally | cbde0d9 | 2018-11-13 18:15:47 +0000 | [diff] [blame] | 3810 | UnaryOperator *UnaryOperator::cloneImpl() const { |
| 3811 | return Create(getOpcode(), Op<0>()); |
| 3812 | } |
| 3813 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3814 | BinaryOperator *BinaryOperator::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3815 | return Create(getOpcode(), Op<0>(), Op<1>()); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3818 | FCmpInst *FCmpInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3819 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3820 | } |
| 3821 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3822 | ICmpInst *ICmpInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3823 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3826 | ExtractValueInst *ExtractValueInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3827 | return new ExtractValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3828 | } |
| 3829 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3830 | InsertValueInst *InsertValueInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3831 | return new InsertValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3834 | AllocaInst *AllocaInst::cloneImpl() const { |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3835 | AllocaInst *Result = new AllocaInst(getAllocatedType(), |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 3836 | getType()->getAddressSpace(), |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3837 | (Value *)getOperand(0), getAlignment()); |
| 3838 | Result->setUsedWithInAlloca(isUsedWithInAlloca()); |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 3839 | Result->setSwiftError(isSwiftError()); |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3840 | return Result; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3843 | LoadInst *LoadInst::cloneImpl() const { |
James Y Knight | 84c1dbd | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 3844 | return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(), |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3845 | getAlignment(), getOrdering(), getSyncScopeID()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3848 | StoreInst *StoreInst::cloneImpl() const { |
Eli Friedman | cad9f2a | 2011-08-10 17:39:11 +0000 | [diff] [blame] | 3849 | return new StoreInst(getOperand(0), getOperand(1), isVolatile(), |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3850 | getAlignment(), getOrdering(), getSyncScopeID()); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3851 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3854 | AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3855 | AtomicCmpXchgInst *Result = |
| 3856 | new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2), |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 3857 | getSuccessOrdering(), getFailureOrdering(), |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3858 | getSyncScopeID()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3859 | Result->setVolatile(isVolatile()); |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 3860 | Result->setWeak(isWeak()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3861 | return Result; |
| 3862 | } |
| 3863 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3864 | AtomicRMWInst *AtomicRMWInst::cloneImpl() const { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3865 | AtomicRMWInst *Result = |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3866 | new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1), |
| 3867 | getOrdering(), getSyncScopeID()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3868 | Result->setVolatile(isVolatile()); |
| 3869 | return Result; |
| 3870 | } |
| 3871 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3872 | FenceInst *FenceInst::cloneImpl() const { |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3873 | return new FenceInst(getContext(), getOrdering(), getSyncScopeID()); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3876 | TruncInst *TruncInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3877 | return new TruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3880 | ZExtInst *ZExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3881 | return new ZExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3884 | SExtInst *SExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3885 | return new SExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3886 | } |
| 3887 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3888 | FPTruncInst *FPTruncInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3889 | return new FPTruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3892 | FPExtInst *FPExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3893 | return new FPExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3896 | UIToFPInst *UIToFPInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3897 | return new UIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3900 | SIToFPInst *SIToFPInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3901 | return new SIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3904 | FPToUIInst *FPToUIInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3905 | return new FPToUIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3908 | FPToSIInst *FPToSIInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3909 | return new FPToSIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3910 | } |
| 3911 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3912 | PtrToIntInst *PtrToIntInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3913 | return new PtrToIntInst(getOperand(0), getType()); |
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 | IntToPtrInst *IntToPtrInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3917 | return new IntToPtrInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3918 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3919 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3920 | BitCastInst *BitCastInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3921 | return new BitCastInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3922 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3923 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3924 | AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3925 | return new AddrSpaceCastInst(getOperand(0), getType()); |
| 3926 | } |
| 3927 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3928 | CallInst *CallInst::cloneImpl() const { |
Sanjoy Das | bd1c1bf | 2015-11-10 20:13:21 +0000 | [diff] [blame] | 3929 | if (hasOperandBundles()) { |
| 3930 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); |
| 3931 | return new(getNumOperands(), DescriptorBytes) CallInst(*this); |
| 3932 | } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3933 | return new(getNumOperands()) CallInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3936 | SelectInst *SelectInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3937 | return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3938 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3939 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3940 | VAArgInst *VAArgInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3941 | return new VAArgInst(getOperand(0), getType()); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3942 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3943 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3944 | ExtractElementInst *ExtractElementInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3945 | return ExtractElementInst::Create(getOperand(0), getOperand(1)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3946 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3947 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3948 | InsertElementInst *InsertElementInst::cloneImpl() const { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 3949 | return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3952 | ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 3953 | return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2)); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3954 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3955 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3956 | PHINode *PHINode::cloneImpl() const { return new PHINode(*this); } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3957 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3958 | LandingPadInst *LandingPadInst::cloneImpl() const { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3959 | return new LandingPadInst(*this); |
| 3960 | } |
| 3961 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3962 | ReturnInst *ReturnInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3963 | return new(getNumOperands()) ReturnInst(*this); |
| 3964 | } |
| 3965 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3966 | BranchInst *BranchInst::cloneImpl() const { |
Jay Foad | d81f3c9 | 2011-01-07 20:29:02 +0000 | [diff] [blame] | 3967 | return new(getNumOperands()) BranchInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3968 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3969 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3970 | SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3971 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3972 | IndirectBrInst *IndirectBrInst::cloneImpl() const { |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3973 | return new IndirectBrInst(*this); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3976 | InvokeInst *InvokeInst::cloneImpl() const { |
Sanjoy Das | bd1c1bf | 2015-11-10 20:13:21 +0000 | [diff] [blame] | 3977 | if (hasOperandBundles()) { |
| 3978 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); |
| 3979 | return new(getNumOperands(), DescriptorBytes) InvokeInst(*this); |
| 3980 | } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3981 | return new(getNumOperands()) InvokeInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3982 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3983 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3984 | ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); } |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3985 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3986 | CleanupReturnInst *CleanupReturnInst::cloneImpl() const { |
| 3987 | return new (getNumOperands()) CleanupReturnInst(*this); |
| 3988 | } |
| 3989 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3990 | CatchReturnInst *CatchReturnInst::cloneImpl() const { |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 3991 | return new (getNumOperands()) CatchReturnInst(*this); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3994 | CatchSwitchInst *CatchSwitchInst::cloneImpl() const { |
| 3995 | return new CatchSwitchInst(*this); |
| 3996 | } |
| 3997 | |
| 3998 | FuncletPadInst *FuncletPadInst::cloneImpl() const { |
| 3999 | return new (getNumOperands()) FuncletPadInst(*this); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4002 | UnreachableInst *UnreachableInst::cloneImpl() const { |
Nick Lewycky | 42fb745 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 4003 | LLVMContext &Context = getContext(); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4004 | return new UnreachableInst(Context); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4005 | } |