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