Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 1 | //===- Instructions.cpp - Implement the LLVM instructions -----------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 10 | // This file implements all of the non-inline methods for the LLVM instruction |
| 11 | // classes. |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Instructions.h" |
Devang Patel | 6c6c016 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/None.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/Twine.h" |
| 20 | #include "llvm/IR/Attributes.h" |
| 21 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 4bbfbdf | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/DataLayout.h" |
| 26 | #include "llvm/IR/DerivedTypes.h" |
| 27 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 28 | #include "llvm/IR/InstrTypes.h" |
| 29 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 6bef5bc | 2018-12-27 23:40:17 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Intrinsics.h" |
Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 31 | #include "llvm/IR/LLVMContext.h" |
| 32 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Module.h" |
| 34 | #include "llvm/IR/Operator.h" |
Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Type.h" |
| 36 | #include "llvm/IR/Value.h" |
| 37 | #include "llvm/Support/AtomicOrdering.h" |
| 38 | #include "llvm/Support/Casting.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ErrorHandling.h" |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MathExtras.h" |
Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 41 | #include <algorithm> |
| 42 | #include <cassert> |
| 43 | #include <cstdint> |
| 44 | #include <vector> |
| 45 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 46 | using namespace llvm; |
| 47 | |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
Bjorn Pettersson | 2079dc3 | 2018-06-26 06:17:00 +0000 | [diff] [blame] | 49 | // AllocaInst Class |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
| 52 | Optional<uint64_t> |
| 53 | AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const { |
| 54 | uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType()); |
| 55 | if (isArrayAllocation()) { |
| 56 | auto C = dyn_cast<ConstantInt>(getArraySize()); |
| 57 | if (!C) |
| 58 | return None; |
| 59 | Size *= C->getZExtValue(); |
| 60 | } |
| 61 | return Size; |
| 62 | } |
| 63 | |
| 64 | //===----------------------------------------------------------------------===// |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 65 | // CallSite Class |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 68 | User::op_iterator CallSite::getCallee() const { |
Chandler Carruth | 9fa222d | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 69 | return cast<CallBase>(getInstruction())->op_end() - 1; |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Gordon Henriksen | afba8fe66 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 72 | //===----------------------------------------------------------------------===// |
Chris Lattner | b76ec32 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 73 | // SelectInst Class |
| 74 | //===----------------------------------------------------------------------===// |
| 75 | |
| 76 | /// areInvalidOperands - Return a string if the specified operands are invalid |
| 77 | /// for a select operation, otherwise return null. |
| 78 | const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { |
| 79 | if (Op1->getType() != Op2->getType()) |
| 80 | return "both values to select must have same type"; |
David Majnemer | 2dacece | 2015-08-14 05:09:07 +0000 | [diff] [blame] | 81 | |
| 82 | if (Op1->getType()->isTokenTy()) |
| 83 | return "select values cannot have token type"; |
| 84 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 85 | if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { |
Chris Lattner | b76ec32 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 86 | // Vector select. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 87 | if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) |
Chris Lattner | b76ec32 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 88 | return "vector select condition element type must be i1"; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 89 | VectorType *ET = dyn_cast<VectorType>(Op1->getType()); |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 90 | if (!ET) |
Chris Lattner | b76ec32 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 91 | return "selected values for vector select must be vectors"; |
| 92 | if (ET->getNumElements() != VT->getNumElements()) |
| 93 | return "vector select requires selected vectors to have " |
| 94 | "the same vector length as select condition"; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 95 | } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { |
Chris Lattner | b76ec32 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 96 | return "select condition must be i1 or <n x i1>"; |
| 97 | } |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 98 | return nullptr; |
Chris Lattner | b76ec32 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Chris Lattner | b76ec32 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 101 | //===----------------------------------------------------------------------===// |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 102 | // PHINode Class |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | |
| 105 | PHINode::PHINode(const PHINode &PN) |
Pete Cooper | ea42367 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 106 | : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()), |
| 107 | ReservedSpace(PN.getNumOperands()) { |
| 108 | allocHungoffUses(PN.getNumOperands()); |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 109 | std::copy(PN.op_begin(), PN.op_end(), op_begin()); |
| 110 | std::copy(PN.block_begin(), PN.block_end(), block_begin()); |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 111 | SubclassOptionalData = PN.SubclassOptionalData; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 114 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 115 | // predecessor basic block is deleted. |
| 116 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 117 | Value *Removed = getIncomingValue(Idx); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 118 | |
| 119 | // Move everything after this operand down. |
| 120 | // |
| 121 | // FIXME: we could just swap with the end of the list, then erase. However, |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 122 | // clients might not expect this to happen. The code as it is thrashes the |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 123 | // use/def lists, which is kinda lame. |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 124 | std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); |
| 125 | std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 126 | |
| 127 | // Nuke the last value. |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 128 | Op<-1>().set(nullptr); |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 129 | setNumHungOffUseOperands(getNumOperands() - 1); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 130 | |
| 131 | // If the PHI node is dead, because it has zero entries, nuke it now. |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 132 | if (getNumOperands() == 0 && DeletePHIIfEmpty) { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 133 | // If anyone is using this PHI, make them use a dummy value instead... |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 134 | replaceAllUsesWith(UndefValue::get(getType())); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 135 | eraseFromParent(); |
| 136 | } |
| 137 | return Removed; |
| 138 | } |
| 139 | |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 140 | /// growOperands - grow operands - This grows the operand list in response |
| 141 | /// to a push_back style of operation. This grows the number of ops by 1.5 |
| 142 | /// times. |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 143 | /// |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 144 | void PHINode::growOperands() { |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 145 | unsigned e = getNumOperands(); |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 146 | unsigned NumOps = e + e / 2; |
| 147 | if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. |
| 148 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 149 | ReservedSpace = NumOps; |
Pete Cooper | 33102d2 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 150 | growHungoffUses(ReservedSpace, /* IsPhi */ true); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 153 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 154 | /// value, return the value, otherwise return null. |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 155 | Value *PHINode::hasConstantValue() const { |
| 156 | // Exploit the fact that phi nodes always have at least one entry. |
| 157 | Value *ConstantValue = getIncomingValue(0); |
| 158 | for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) |
Nuno Lopes | 44d5c06 | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 159 | if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) { |
| 160 | if (ConstantValue != this) |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 161 | return nullptr; // Incoming values not all the same. |
Nuno Lopes | 44d5c06 | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 162 | // The case where the first value is this PHI. |
| 163 | ConstantValue = getIncomingValue(i); |
| 164 | } |
Nuno Lopes | 0fd518b | 2012-07-03 21:15:40 +0000 | [diff] [blame] | 165 | if (ConstantValue == this) |
| 166 | return UndefValue::get(getType()); |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 167 | return ConstantValue; |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Nicolai Haehnle | a16ecd43 | 2016-04-14 17:42:47 +0000 | [diff] [blame] | 170 | /// hasConstantOrUndefValue - Whether the specified PHI node always merges |
| 171 | /// together the same value, assuming that undefs result in the same value as |
| 172 | /// non-undefs. |
| 173 | /// Unlike \ref hasConstantValue, this does not return a value because the |
| 174 | /// unique non-undef incoming value need not dominate the PHI node. |
| 175 | bool PHINode::hasConstantOrUndefValue() const { |
| 176 | Value *ConstantValue = nullptr; |
| 177 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) { |
| 178 | Value *Incoming = getIncomingValue(i); |
| 179 | if (Incoming != this && !isa<UndefValue>(Incoming)) { |
| 180 | if (ConstantValue && ConstantValue != Incoming) |
| 181 | return false; |
| 182 | ConstantValue = Incoming; |
| 183 | } |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 188 | //===----------------------------------------------------------------------===// |
| 189 | // LandingPadInst Implementation |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | |
David Majnemer | cc714e2 | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 192 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 193 | const Twine &NameStr, Instruction *InsertBefore) |
| 194 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) { |
| 195 | init(NumReservedValues, NameStr); |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 196 | } |
| 197 | |
David Majnemer | cc714e2 | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 198 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 199 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 200 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) { |
| 201 | init(NumReservedValues, NameStr); |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | LandingPadInst::LandingPadInst(const LandingPadInst &LP) |
Pete Cooper | ea42367 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 205 | : Instruction(LP.getType(), Instruction::LandingPad, nullptr, |
| 206 | LP.getNumOperands()), |
| 207 | ReservedSpace(LP.getNumOperands()) { |
| 208 | allocHungoffUses(LP.getNumOperands()); |
Pete Cooper | cff40fc | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 209 | Use *OL = getOperandList(); |
| 210 | const Use *InOL = LP.getOperandList(); |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 211 | for (unsigned I = 0, E = ReservedSpace; I != E; ++I) |
| 212 | OL[I] = InOL[I]; |
| 213 | |
| 214 | setCleanup(LP.isCleanup()); |
| 215 | } |
| 216 | |
David Majnemer | cc714e2 | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 217 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 218 | const Twine &NameStr, |
| 219 | Instruction *InsertBefore) { |
David Majnemer | cc714e2 | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 220 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore); |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 221 | } |
| 222 | |
David Majnemer | cc714e2 | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 223 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 224 | const Twine &NameStr, |
| 225 | BasicBlock *InsertAtEnd) { |
David Majnemer | cc714e2 | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 226 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd); |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 227 | } |
| 228 | |
David Majnemer | cc714e2 | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 229 | void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) { |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 230 | ReservedSpace = NumReservedValues; |
David Majnemer | cc714e2 | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 231 | setNumHungOffUseOperands(0); |
Pete Cooper | ea42367 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 232 | allocHungoffUses(ReservedSpace); |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 233 | setName(NameStr); |
| 234 | setCleanup(false); |
| 235 | } |
| 236 | |
| 237 | /// growOperands - grow operands - This grows the operand list in response to a |
| 238 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 239 | void LandingPadInst::growOperands(unsigned Size) { |
| 240 | unsigned e = getNumOperands(); |
| 241 | if (ReservedSpace >= e + Size) return; |
David Majnemer | cc714e2 | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 242 | ReservedSpace = (std::max(e, 1U) + Size / 2) * 2; |
Pete Cooper | 33102d2 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 243 | growHungoffUses(ReservedSpace); |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Rafael Espindola | dcac152 | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 246 | void LandingPadInst::addClause(Constant *Val) { |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 247 | unsigned OpNo = getNumOperands(); |
| 248 | growOperands(1); |
| 249 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 250 | setNumHungOffUseOperands(getNumOperands() + 1); |
Pete Cooper | cff40fc | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 251 | getOperandList()[OpNo] = Val; |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 252 | } |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 253 | |
| 254 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 9fa222d | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 255 | // CallBase Implementation |
| 256 | //===----------------------------------------------------------------------===// |
| 257 | |
Chandler Carruth | 6bef5bc | 2018-12-27 23:40:17 +0000 | [diff] [blame] | 258 | Function *CallBase::getCaller() { return getParent()->getParent(); } |
| 259 | |
Chandler Carruth | 9d8b7a0 | 2019-01-07 07:15:51 +0000 | [diff] [blame] | 260 | bool CallBase::isIndirectCall() const { |
| 261 | const Value *V = getCalledValue(); |
| 262 | if (isa<Function>(V) || isa<Constant>(V)) |
| 263 | return false; |
| 264 | if (const CallInst *CI = dyn_cast<CallInst>(this)) |
| 265 | if (CI->isInlineAsm()) |
| 266 | return false; |
| 267 | return true; |
| 268 | } |
| 269 | |
Chandler Carruth | 6bef5bc | 2018-12-27 23:40:17 +0000 | [diff] [blame] | 270 | Intrinsic::ID CallBase::getIntrinsicID() const { |
| 271 | if (auto *F = getCalledFunction()) |
| 272 | return F->getIntrinsicID(); |
| 273 | return Intrinsic::not_intrinsic; |
| 274 | } |
| 275 | |
| 276 | bool CallBase::isReturnNonNull() const { |
| 277 | if (hasRetAttr(Attribute::NonNull)) |
| 278 | return true; |
| 279 | |
| 280 | if (getDereferenceableBytes(AttributeList::ReturnIndex) > 0 && |
| 281 | !NullPointerIsDefined(getCaller(), |
| 282 | getType()->getPointerAddressSpace())) |
| 283 | return true; |
| 284 | |
| 285 | return false; |
| 286 | } |
| 287 | |
Chandler Carruth | 9fa222d | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 288 | Value *CallBase::getReturnedArgOperand() const { |
| 289 | unsigned Index; |
| 290 | |
| 291 | if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index) |
| 292 | return getArgOperand(Index - AttributeList::FirstArgIndex); |
| 293 | if (const Function *F = getCalledFunction()) |
| 294 | if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) && |
| 295 | Index) |
| 296 | return getArgOperand(Index - AttributeList::FirstArgIndex); |
| 297 | |
| 298 | return nullptr; |
| 299 | } |
| 300 | |
| 301 | bool CallBase::hasRetAttr(Attribute::AttrKind Kind) const { |
| 302 | if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind)) |
| 303 | return true; |
| 304 | |
| 305 | // Look at the callee, if available. |
| 306 | if (const Function *F = getCalledFunction()) |
| 307 | return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind); |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | /// Determine whether the argument or parameter has the given attribute. |
| 312 | bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { |
| 313 | assert(ArgNo < getNumArgOperands() && "Param index out of bounds!"); |
| 314 | |
| 315 | if (Attrs.hasParamAttribute(ArgNo, Kind)) |
| 316 | return true; |
| 317 | if (const Function *F = getCalledFunction()) |
| 318 | return F->getAttributes().hasParamAttribute(ArgNo, Kind); |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const { |
| 323 | if (const Function *F = getCalledFunction()) |
| 324 | return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind); |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const { |
| 329 | if (const Function *F = getCalledFunction()) |
| 330 | return F->getAttributes().hasAttribute(AttributeList::FunctionIndex, Kind); |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | CallBase::op_iterator |
| 335 | CallBase::populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, |
| 336 | const unsigned BeginIndex) { |
| 337 | auto It = op_begin() + BeginIndex; |
| 338 | for (auto &B : Bundles) |
| 339 | It = std::copy(B.input_begin(), B.input_end(), It); |
| 340 | |
| 341 | auto *ContextImpl = getContext().pImpl; |
| 342 | auto BI = Bundles.begin(); |
| 343 | unsigned CurrentIndex = BeginIndex; |
| 344 | |
| 345 | for (auto &BOI : bundle_op_infos()) { |
| 346 | assert(BI != Bundles.end() && "Incorrect allocation?"); |
| 347 | |
| 348 | BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag()); |
| 349 | BOI.Begin = CurrentIndex; |
| 350 | BOI.End = CurrentIndex + BI->input_size(); |
| 351 | CurrentIndex = BOI.End; |
| 352 | BI++; |
| 353 | } |
| 354 | |
| 355 | assert(BI == Bundles.end() && "Incorrect allocation?"); |
| 356 | |
| 357 | return It; |
| 358 | } |
| 359 | |
| 360 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 361 | // CallInst Implementation |
| 362 | //===----------------------------------------------------------------------===// |
| 363 | |
David Blaikie | 93a23a3 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 364 | void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, |
Sanjoy Das | 5b674c0 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 365 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) { |
David Blaikie | 93a23a3 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 366 | this->FTy = FTy; |
Sanjoy Das | 5b674c0 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 367 | assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 && |
| 368 | "NumOperands not set up?"); |
Chandler Carruth | 9fa222d | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 369 | setCalledOperand(Func); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 370 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 371 | #ifndef NDEBUG |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 372 | assert((Args.size() == FTy->getNumParams() || |
| 373 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 374 | "Calling a function with bad signature!"); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 375 | |
| 376 | for (unsigned i = 0; i != Args.size(); ++i) |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 377 | assert((i >= FTy->getNumParams() || |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 378 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 379 | "Calling a function with a bad signature!"); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 380 | #endif |
| 381 | |
Fangrui Song | 53a6224 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 382 | llvm::copy(Args, op_begin()); |
Sanjoy Das | 5b674c0 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 383 | |
| 384 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 385 | (void)It; |
| 386 | assert(It + 1 == op_end() && "Should add up!"); |
| 387 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 388 | setName(NameStr); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 389 | } |
| 390 | |
James Y Knight | f806f42 | 2019-01-14 21:37:42 +0000 | [diff] [blame] | 391 | void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) { |
| 392 | this->FTy = FTy; |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 393 | assert(getNumOperands() == 1 && "NumOperands not set up?"); |
Chandler Carruth | 9fa222d | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 394 | setCalledOperand(Func); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 2e21fce | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 396 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 397 | |
| 398 | setName(NameStr); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 399 | } |
| 400 | |
James Y Knight | f806f42 | 2019-01-14 21:37:42 +0000 | [diff] [blame] | 401 | CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, |
| 402 | Instruction *InsertBefore) |
| 403 | : CallBase(Ty->getReturnType(), Instruction::Call, |
| 404 | OperandTraits<CallBase>::op_end(this) - 1, 1, InsertBefore) { |
| 405 | init(Ty, Func, Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 406 | } |
| 407 | |
James Y Knight | f806f42 | 2019-01-14 21:37:42 +0000 | [diff] [blame] | 408 | CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, |
| 409 | BasicBlock *InsertAtEnd) |
| 410 | : CallBase(Ty->getReturnType(), Instruction::Call, |
| 411 | OperandTraits<CallBase>::op_end(this) - 1, 1, InsertAtEnd) { |
| 412 | init(Ty, Func, Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 415 | CallInst::CallInst(const CallInst &CI) |
Chandler Carruth | 9fa222d | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 416 | : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call, |
| 417 | OperandTraits<CallBase>::op_end(this) - CI.getNumOperands(), |
| 418 | CI.getNumOperands()) { |
Reid Kleckner | 3949749 | 2014-05-06 20:08:20 +0000 | [diff] [blame] | 419 | setTailCallKind(CI.getTailCallKind()); |
Chris Lattner | cafe9bb | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 420 | setCallingConv(CI.getCallingConv()); |
Sanjoy Das | 5b674c0 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 421 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 422 | std::copy(CI.op_begin(), CI.op_end(), op_begin()); |
Sanjoy Das | 5b674c0 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 423 | std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(), |
| 424 | bundle_op_info_begin()); |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 425 | SubclassOptionalData = CI.SubclassOptionalData; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Sanjoy Das | aca8112 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 428 | CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB, |
| 429 | Instruction *InsertPt) { |
Sanjoy Das | fd947f3 | 2015-12-10 06:39:02 +0000 | [diff] [blame] | 430 | std::vector<Value *> Args(CI->arg_begin(), CI->arg_end()); |
Sanjoy Das | aca8112 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 431 | |
| 432 | auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(), |
| 433 | InsertPt); |
| 434 | NewCI->setTailCallKind(CI->getTailCallKind()); |
| 435 | NewCI->setCallingConv(CI->getCallingConv()); |
| 436 | NewCI->SubclassOptionalData = CI->SubclassOptionalData; |
Sanjoy Das | f001e6b | 2015-12-09 01:01:28 +0000 | [diff] [blame] | 437 | NewCI->setAttributes(CI->getAttributes()); |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 438 | NewCI->setDebugLoc(CI->getDebugLoc()); |
Sanjoy Das | aca8112 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 439 | return NewCI; |
| 440 | } |
| 441 | |
Hal Finkel | 9a51ef1 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 442 | |
Reid Kleckner | a82b376 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 443 | |
Hal Finkel | 9a51ef1 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 444 | |
Eric Christopher | 0bf7b41 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 445 | |
Amaury Sechet | 4e14c83 | 2016-06-15 05:14:29 +0000 | [diff] [blame] | 446 | |
Reid Kleckner | 1e9afac | 2017-05-31 19:23:09 +0000 | [diff] [blame] | 447 | |
Reid Kleckner | a82b376 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 448 | |
Amaury Sechet | b505275 | 2016-04-21 21:29:10 +0000 | [diff] [blame] | 449 | |
Sanjoy Das | a6abdeb | 2015-11-04 21:05:24 +0000 | [diff] [blame] | 450 | |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 451 | /// IsConstantOne - Return true only if val is constant int 1 |
| 452 | static bool IsConstantOne(Value *val) { |
Reid Kleckner | e094cca | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 453 | assert(val && "IsConstantOne does not work with nullptr val"); |
Matt Arsenault | 7447426 | 2014-09-15 17:56:51 +0000 | [diff] [blame] | 454 | const ConstantInt *CVal = dyn_cast<ConstantInt>(val); |
| 455 | return CVal && CVal->isOne(); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Nick Lewycky | 3fc35c5 | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 458 | static Instruction *createMalloc(Instruction *InsertBefore, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 459 | BasicBlock *InsertAtEnd, Type *IntPtrTy, |
David Majnemer | 3047183 | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 460 | Type *AllocTy, Value *AllocSize, |
| 461 | Value *ArraySize, |
| 462 | ArrayRef<OperandBundleDef> OpB, |
| 463 | Function *MallocF, const Twine &Name) { |
Benjamin Kramer | b84c5ae | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 464 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 465 | "createMalloc needs either InsertBefore or InsertAtEnd"); |
| 466 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 467 | // malloc(type) becomes: |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 468 | // bitcast (i8* malloc(typeSize)) to type* |
| 469 | // malloc(type, arraySize) becomes: |
Ana Pazos | e6463c9 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 470 | // bitcast (i8* malloc(typeSize*arraySize)) to type* |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 471 | if (!ArraySize) |
| 472 | ArraySize = ConstantInt::get(IntPtrTy, 1); |
| 473 | else if (ArraySize->getType() != IntPtrTy) { |
| 474 | if (InsertBefore) |
Victor Hernandez | 4d81d97 | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 475 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 476 | "", InsertBefore); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 477 | else |
Victor Hernandez | 4d81d97 | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 478 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 479 | "", InsertAtEnd); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 480 | } |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 481 | |
Benjamin Kramer | b84c5ae | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 482 | if (!IsConstantOne(ArraySize)) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 483 | if (IsConstantOne(AllocSize)) { |
| 484 | AllocSize = ArraySize; // Operand * 1 = Operand |
| 485 | } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { |
| 486 | Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, |
| 487 | false /*ZExt*/); |
| 488 | // Malloc arg is constant product of type size and array size |
| 489 | AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); |
| 490 | } else { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 491 | // Multiply type size by the array size... |
| 492 | if (InsertBefore) |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 493 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 494 | "mallocsize", InsertBefore); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 495 | else |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 496 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 497 | "mallocsize", InsertAtEnd); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 498 | } |
Benjamin Kramer | b84c5ae | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 499 | } |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 500 | |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 501 | assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size"); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 502 | // Create the call to Malloc. |
Ana Pazos | e6463c9 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 503 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 504 | Module *M = BB->getParent()->getParent(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 505 | Type *BPTy = Type::getInt8PtrTy(BB->getContext()); |
Victor Hernandez | f06dca2 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 506 | Value *MallocFunc = MallocF; |
| 507 | if (!MallocFunc) |
Victor Hernandez | 13ad5aa | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 508 | // prototype malloc as "void *malloc(size_t)" |
Serge Guelton | 9d54400 | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 509 | MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 510 | PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 511 | CallInst *MCall = nullptr; |
| 512 | Instruction *Result = nullptr; |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 513 | if (InsertBefore) { |
David Majnemer | 3047183 | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 514 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall", |
| 515 | InsertBefore); |
Victor Hernandez | 13ad5aa | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 516 | Result = MCall; |
| 517 | if (Result->getType() != AllocPtrType) |
| 518 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 519 | Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 520 | } else { |
David Majnemer | 3047183 | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 521 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall"); |
Victor Hernandez | 13ad5aa | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 522 | Result = MCall; |
| 523 | if (Result->getType() != AllocPtrType) { |
| 524 | InsertAtEnd->getInstList().push_back(MCall); |
| 525 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 526 | Result = new BitCastInst(MCall, AllocPtrType, Name); |
Victor Hernandez | 13ad5aa | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 527 | } |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 528 | } |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 529 | MCall->setTailCall(); |
Victor Hernandez | f06dca2 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 530 | if (Function *F = dyn_cast<Function>(MallocFunc)) { |
| 531 | MCall->setCallingConv(F->getCallingConv()); |
Reid Kleckner | a82b376 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 532 | if (!F->returnDoesNotAlias()) |
| 533 | F->setReturnDoesNotAlias(); |
Victor Hernandez | f06dca2 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 534 | } |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 535 | assert(!MCall->getType()->isVoidTy() && "Malloc has void return type"); |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 536 | |
Victor Hernandez | 13ad5aa | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 537 | return Result; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 541 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 542 | /// possibly multiplied by the array size if the array size is not |
| 543 | /// constant 1. |
| 544 | /// 2. Call malloc with that argument. |
| 545 | /// 3. Bitcast the result of the malloc call to the specified type. |
Nick Lewycky | 3fc35c5 | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 546 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 547 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 548 | Value *AllocSize, Value *ArraySize, |
Ana Pazos | e6463c9 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 549 | Function *MallocF, |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 550 | const Twine &Name) { |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 551 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, |
David Majnemer | 3047183 | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 552 | ArraySize, None, MallocF, Name); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 553 | } |
David Majnemer | 3047183 | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 554 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
| 555 | Type *IntPtrTy, Type *AllocTy, |
| 556 | Value *AllocSize, Value *ArraySize, |
| 557 | ArrayRef<OperandBundleDef> OpB, |
| 558 | Function *MallocF, |
| 559 | const Twine &Name) { |
| 560 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, |
| 561 | ArraySize, OpB, MallocF, Name); |
| 562 | } |
| 563 | |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 564 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 565 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 566 | /// possibly multiplied by the array size if the array size is not |
| 567 | /// constant 1. |
| 568 | /// 2. Call malloc with that argument. |
| 569 | /// 3. Bitcast the result of the malloc call to the specified type. |
| 570 | /// Note: This function does not add the bitcast to the basic block, that is the |
| 571 | /// responsibility of the caller. |
Nick Lewycky | 3fc35c5 | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 572 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 573 | Type *IntPtrTy, Type *AllocTy, |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 574 | Value *AllocSize, Value *ArraySize, |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 575 | Function *MallocF, const Twine &Name) { |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 576 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
David Majnemer | 3047183 | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 577 | ArraySize, None, MallocF, Name); |
| 578 | } |
| 579 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
| 580 | Type *IntPtrTy, Type *AllocTy, |
| 581 | Value *AllocSize, Value *ArraySize, |
| 582 | ArrayRef<OperandBundleDef> OpB, |
| 583 | Function *MallocF, const Twine &Name) { |
| 584 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
| 585 | ArraySize, OpB, MallocF, Name); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 586 | } |
Duncan Sands | afa3b6d | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 587 | |
David Majnemer | 3047183 | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 588 | static Instruction *createFree(Value *Source, |
| 589 | ArrayRef<OperandBundleDef> Bundles, |
| 590 | Instruction *InsertBefore, |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 591 | BasicBlock *InsertAtEnd) { |
| 592 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
| 593 | "createFree needs either InsertBefore or InsertAtEnd"); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 594 | assert(Source->getType()->isPointerTy() && |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 595 | "Can not free something of nonpointer type!"); |
| 596 | |
Ana Pazos | e6463c9 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 597 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 598 | Module *M = BB->getParent()->getParent(); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 599 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 600 | Type *VoidTy = Type::getVoidTy(M->getContext()); |
| 601 | Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 602 | // prototype free as "void free(void*)" |
Serge Guelton | 9d54400 | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 603 | Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy); |
Ana Pazos | e6463c9 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 604 | CallInst *Result = nullptr; |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 605 | Value *PtrCast = Source; |
| 606 | if (InsertBefore) { |
| 607 | if (Source->getType() != IntPtrTy) |
| 608 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); |
David Majnemer | 3047183 | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 609 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 610 | } else { |
| 611 | if (Source->getType() != IntPtrTy) |
| 612 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); |
David Majnemer | 3047183 | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 613 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, ""); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 614 | } |
| 615 | Result->setTailCall(); |
Chris Lattner | 47fc9d3 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 616 | if (Function *F = dyn_cast<Function>(FreeFunc)) |
| 617 | Result->setCallingConv(F->getCallingConv()); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 618 | |
| 619 | return Result; |
| 620 | } |
| 621 | |
| 622 | /// CreateFree - Generate the IR for a call to the builtin free function. |
Ana Pazos | e6463c9 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 623 | Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) { |
David Majnemer | 3047183 | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 624 | return createFree(Source, None, InsertBefore, nullptr); |
| 625 | } |
| 626 | Instruction *CallInst::CreateFree(Value *Source, |
| 627 | ArrayRef<OperandBundleDef> Bundles, |
| 628 | Instruction *InsertBefore) { |
| 629 | return createFree(Source, Bundles, InsertBefore, nullptr); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /// CreateFree - Generate the IR for a call to the builtin free function. |
| 633 | /// Note: This function does not add the call to the basic block, that is the |
| 634 | /// responsibility of the caller. |
Ana Pazos | e6463c9 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 635 | Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) { |
David Majnemer | 3047183 | 2016-04-29 08:07:22 +0000 | [diff] [blame] | 636 | Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd); |
| 637 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 638 | return FreeCall; |
| 639 | } |
| 640 | Instruction *CallInst::CreateFree(Value *Source, |
| 641 | ArrayRef<OperandBundleDef> Bundles, |
| 642 | BasicBlock *InsertAtEnd) { |
| 643 | Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 644 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 645 | return FreeCall; |
| 646 | } |
| 647 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 648 | //===----------------------------------------------------------------------===// |
| 649 | // InvokeInst Implementation |
| 650 | //===----------------------------------------------------------------------===// |
| 651 | |
David Blaikie | 115494b | 2015-05-13 18:35:26 +0000 | [diff] [blame] | 652 | void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal, |
| 653 | BasicBlock *IfException, ArrayRef<Value *> Args, |
Sanjoy Das | 5b674c0 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 654 | ArrayRef<OperandBundleDef> Bundles, |
David Blaikie | 115494b | 2015-05-13 18:35:26 +0000 | [diff] [blame] | 655 | const Twine &NameStr) { |
| 656 | this->FTy = FTy; |
David Blaikie | 93a23a3 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 657 | |
Chandler Carruth | 9fa222d | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 658 | assert((int)getNumOperands() == |
| 659 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) && |
Sanjoy Das | 5b674c0 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 660 | "NumOperands not set up?"); |
Chandler Carruth | 9fa222d | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 661 | setNormalDest(IfNormal); |
| 662 | setUnwindDest(IfException); |
| 663 | setCalledOperand(Fn); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 664 | |
| 665 | #ifndef NDEBUG |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 666 | assert(((Args.size() == FTy->getNumParams()) || |
| 667 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Gabor Greif | 050560e | 2010-03-23 13:45:54 +0000 | [diff] [blame] | 668 | "Invoking a function with bad signature"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 669 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 670 | for (unsigned i = 0, e = Args.size(); i != e; i++) |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 671 | assert((i >= FTy->getNumParams() || |
Chris Lattner | d2dd150 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 672 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 673 | "Invoking a function with a bad signature!"); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 674 | #endif |
| 675 | |
Fangrui Song | 53a6224 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 676 | llvm::copy(Args, op_begin()); |
Sanjoy Das | 5b674c0 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 677 | |
| 678 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 679 | (void)It; |
| 680 | assert(It + 3 == op_end() && "Should add up!"); |
| 681 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 682 | setName(NameStr); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 685 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chandler Carruth | 9fa222d | 2018-11-22 10:31:35 +0000 | [diff] [blame] | 686 | : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke, |
| 687 | OperandTraits<CallBase>::op_end(this) - II.getNumOperands(), |
| 688 | II.getNumOperands()) { |
Chris Lattner | cafe9bb | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 689 | setCallingConv(II.getCallingConv()); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 690 | std::copy(II.op_begin(), II.op_end(), op_begin()); |
Sanjoy Das | 5b674c0 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 691 | std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(), |
| 692 | bundle_op_info_begin()); |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 693 | SubclassOptionalData = II.SubclassOptionalData; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Sanjoy Das | aca8112 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 696 | InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB, |
| 697 | Instruction *InsertPt) { |
Sanjoy Das | fd947f3 | 2015-12-10 06:39:02 +0000 | [diff] [blame] | 698 | std::vector<Value *> Args(II->arg_begin(), II->arg_end()); |
Sanjoy Das | aca8112 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 699 | |
| 700 | auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(), |
| 701 | II->getUnwindDest(), Args, OpB, |
| 702 | II->getName(), InsertPt); |
| 703 | NewII->setCallingConv(II->getCallingConv()); |
| 704 | NewII->SubclassOptionalData = II->SubclassOptionalData; |
Sanjoy Das | f001e6b | 2015-12-09 01:01:28 +0000 | [diff] [blame] | 705 | NewII->setAttributes(II->getAttributes()); |
Joseph Tremoulet | 0d05e6c | 2016-01-14 06:21:42 +0000 | [diff] [blame] | 706 | NewII->setDebugLoc(II->getDebugLoc()); |
Sanjoy Das | aca8112 | 2015-11-18 06:23:38 +0000 | [diff] [blame] | 707 | return NewII; |
| 708 | } |
| 709 | |
Sanjoy Das | 5ff5907 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 710 | |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 711 | LandingPadInst *InvokeInst::getLandingPadInst() const { |
| 712 | return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); |
| 713 | } |
Duncan Sands | afa3b6d | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 714 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 715 | //===----------------------------------------------------------------------===// |
| 716 | // ReturnInst Implementation |
| 717 | //===----------------------------------------------------------------------===// |
| 718 | |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 719 | ReturnInst::ReturnInst(const ReturnInst &RI) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 720 | : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret, |
| 721 | OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(), |
| 722 | RI.getNumOperands()) { |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 723 | if (RI.getNumOperands()) |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 724 | Op<0>() = RI.Op<0>(); |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 725 | SubclassOptionalData = RI.SubclassOptionalData; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 728 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 729 | : Instruction(Type::getVoidTy(C), Instruction::Ret, |
| 730 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 731 | InsertBefore) { |
Devang Patel | 814ebd7 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 732 | if (retVal) |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 733 | Op<0>() = retVal; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 734 | } |
Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 735 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 736 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 737 | : Instruction(Type::getVoidTy(C), Instruction::Ret, |
| 738 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 739 | InsertAtEnd) { |
Devang Patel | 814ebd7 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 740 | if (retVal) |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 741 | Op<0>() = retVal; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 742 | } |
Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 743 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 744 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 745 | : Instruction(Type::getVoidTy(Context), Instruction::Ret, |
| 746 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {} |
Devang Patel | 57ef4f4 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 747 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 748 | //===----------------------------------------------------------------------===// |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 749 | // ResumeInst Implementation |
| 750 | //===----------------------------------------------------------------------===// |
| 751 | |
| 752 | ResumeInst::ResumeInst(const ResumeInst &RI) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 753 | : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume, |
| 754 | OperandTraits<ResumeInst>::op_begin(this), 1) { |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 755 | Op<0>() = RI.Op<0>(); |
| 756 | } |
| 757 | |
| 758 | ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 759 | : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 760 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 761 | Op<0>() = Exn; |
| 762 | } |
| 763 | |
| 764 | ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 765 | : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 766 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 767 | Op<0>() = Exn; |
| 768 | } |
| 769 | |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 770 | //===----------------------------------------------------------------------===// |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 771 | // CleanupReturnInst Implementation |
| 772 | //===----------------------------------------------------------------------===// |
| 773 | |
| 774 | CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 775 | : Instruction(CRI.getType(), Instruction::CleanupRet, |
| 776 | OperandTraits<CleanupReturnInst>::op_end(this) - |
| 777 | CRI.getNumOperands(), |
| 778 | CRI.getNumOperands()) { |
David Majnemer | b892465 | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 779 | setInstructionSubclassData(CRI.getSubclassDataFromInstruction()); |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 780 | Op<0>() = CRI.Op<0>(); |
Joseph Tremoulet | d4a765f | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 781 | if (CRI.hasUnwindDest()) |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 782 | Op<1>() = CRI.Op<1>(); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 783 | } |
| 784 | |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 785 | void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) { |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 786 | if (UnwindBB) |
| 787 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 788 | |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 789 | Op<0>() = CleanupPad; |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 790 | if (UnwindBB) |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 791 | Op<1>() = UnwindBB; |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 792 | } |
| 793 | |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 794 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, |
| 795 | unsigned Values, Instruction *InsertBefore) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 796 | : Instruction(Type::getVoidTy(CleanupPad->getContext()), |
| 797 | Instruction::CleanupRet, |
| 798 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, |
| 799 | Values, InsertBefore) { |
Joseph Tremoulet | d4a765f | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 800 | init(CleanupPad, UnwindBB); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 801 | } |
| 802 | |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 803 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, |
| 804 | unsigned Values, BasicBlock *InsertAtEnd) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 805 | : Instruction(Type::getVoidTy(CleanupPad->getContext()), |
| 806 | Instruction::CleanupRet, |
| 807 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, |
| 808 | Values, InsertAtEnd) { |
Joseph Tremoulet | d4a765f | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 809 | init(CleanupPad, UnwindBB); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 810 | } |
| 811 | |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 812 | //===----------------------------------------------------------------------===// |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 813 | // CatchReturnInst Implementation |
| 814 | //===----------------------------------------------------------------------===// |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 815 | void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) { |
Joseph Tremoulet | d4a765f | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 816 | Op<0>() = CatchPad; |
| 817 | Op<1>() = BB; |
David Majnemer | de17e77 | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 818 | } |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 819 | |
| 820 | CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 821 | : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet, |
| 822 | OperandTraits<CatchReturnInst>::op_begin(this), 2) { |
Joseph Tremoulet | d4a765f | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 823 | Op<0>() = CRI.Op<0>(); |
| 824 | Op<1>() = CRI.Op<1>(); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 825 | } |
| 826 | |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 827 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, |
David Majnemer | de17e77 | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 828 | Instruction *InsertBefore) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 829 | : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, |
| 830 | OperandTraits<CatchReturnInst>::op_begin(this), 2, |
| 831 | InsertBefore) { |
Joseph Tremoulet | d4a765f | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 832 | init(CatchPad, BB); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 833 | } |
| 834 | |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 835 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, |
David Majnemer | de17e77 | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 836 | BasicBlock *InsertAtEnd) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 837 | : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, |
| 838 | OperandTraits<CatchReturnInst>::op_begin(this), 2, |
| 839 | InsertAtEnd) { |
Joseph Tremoulet | d4a765f | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 840 | init(CatchPad, BB); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 841 | } |
| 842 | |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 843 | //===----------------------------------------------------------------------===// |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 844 | // CatchSwitchInst Implementation |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 845 | //===----------------------------------------------------------------------===// |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 846 | |
| 847 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
| 848 | unsigned NumReservedValues, |
| 849 | const Twine &NameStr, |
| 850 | Instruction *InsertBefore) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 851 | : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, |
| 852 | InsertBefore) { |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 853 | if (UnwindDest) |
| 854 | ++NumReservedValues; |
| 855 | init(ParentPad, UnwindDest, NumReservedValues + 1); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 856 | setName(NameStr); |
| 857 | } |
| 858 | |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 859 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
| 860 | unsigned NumReservedValues, |
| 861 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 862 | : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, |
| 863 | InsertAtEnd) { |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 864 | if (UnwindDest) |
| 865 | ++NumReservedValues; |
| 866 | init(ParentPad, UnwindDest, NumReservedValues + 1); |
| 867 | setName(NameStr); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 868 | } |
| 869 | |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 870 | CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 871 | : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr, |
| 872 | CSI.getNumOperands()) { |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 873 | init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands()); |
| 874 | setNumHungOffUseOperands(ReservedSpace); |
| 875 | Use *OL = getOperandList(); |
| 876 | const Use *InOL = CSI.getOperandList(); |
| 877 | for (unsigned I = 1, E = ReservedSpace; I != E; ++I) |
| 878 | OL[I] = InOL[I]; |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 879 | } |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 880 | |
| 881 | void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest, |
| 882 | unsigned NumReservedValues) { |
| 883 | assert(ParentPad && NumReservedValues); |
| 884 | |
| 885 | ReservedSpace = NumReservedValues; |
| 886 | setNumHungOffUseOperands(UnwindDest ? 2 : 1); |
| 887 | allocHungoffUses(ReservedSpace); |
| 888 | |
| 889 | Op<0>() = ParentPad; |
| 890 | if (UnwindDest) { |
| 891 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
| 892 | setUnwindDest(UnwindDest); |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | /// growOperands - grow operands - This grows the operand list in response to a |
| 897 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 898 | void CatchSwitchInst::growOperands(unsigned Size) { |
| 899 | unsigned NumOperands = getNumOperands(); |
| 900 | assert(NumOperands >= 1); |
| 901 | if (ReservedSpace >= NumOperands + Size) |
| 902 | return; |
| 903 | ReservedSpace = (NumOperands + Size / 2) * 2; |
| 904 | growHungoffUses(ReservedSpace); |
| 905 | } |
| 906 | |
| 907 | void CatchSwitchInst::addHandler(BasicBlock *Handler) { |
| 908 | unsigned OpNo = getNumOperands(); |
| 909 | growOperands(1); |
| 910 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
| 911 | setNumHungOffUseOperands(getNumOperands() + 1); |
| 912 | getOperandList()[OpNo] = Handler; |
| 913 | } |
| 914 | |
Joseph Tremoulet | d28c1bc | 2016-01-05 02:37:41 +0000 | [diff] [blame] | 915 | void CatchSwitchInst::removeHandler(handler_iterator HI) { |
| 916 | // Move all subsequent handlers up one. |
| 917 | Use *EndDst = op_end() - 1; |
| 918 | for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst) |
| 919 | *CurDst = *(CurDst + 1); |
| 920 | // Null out the last handler use. |
| 921 | *EndDst = nullptr; |
| 922 | |
| 923 | setNumHungOffUseOperands(getNumOperands() - 1); |
| 924 | } |
| 925 | |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 926 | //===----------------------------------------------------------------------===// |
| 927 | // FuncletPadInst Implementation |
| 928 | //===----------------------------------------------------------------------===// |
| 929 | void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args, |
| 930 | const Twine &NameStr) { |
| 931 | assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?"); |
Fangrui Song | 53a6224 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 932 | llvm::copy(Args, op_begin()); |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 933 | setParentPad(ParentPad); |
| 934 | setName(NameStr); |
| 935 | } |
| 936 | |
| 937 | FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI) |
| 938 | : Instruction(FPI.getType(), FPI.getOpcode(), |
| 939 | OperandTraits<FuncletPadInst>::op_end(this) - |
| 940 | FPI.getNumOperands(), |
| 941 | FPI.getNumOperands()) { |
| 942 | std::copy(FPI.op_begin(), FPI.op_end(), op_begin()); |
| 943 | setParentPad(FPI.getParentPad()); |
| 944 | } |
| 945 | |
| 946 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
| 947 | ArrayRef<Value *> Args, unsigned Values, |
| 948 | const Twine &NameStr, Instruction *InsertBefore) |
| 949 | : Instruction(ParentPad->getType(), Op, |
| 950 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, |
| 951 | InsertBefore) { |
| 952 | init(ParentPad, Args, NameStr); |
| 953 | } |
| 954 | |
| 955 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
| 956 | ArrayRef<Value *> Args, unsigned Values, |
| 957 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 958 | : Instruction(ParentPad->getType(), Op, |
| 959 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, |
| 960 | InsertAtEnd) { |
| 961 | init(ParentPad, Args, NameStr); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | //===----------------------------------------------------------------------===// |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 965 | // UnreachableInst Implementation |
| 966 | //===----------------------------------------------------------------------===// |
| 967 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 968 | UnreachableInst::UnreachableInst(LLVMContext &Context, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 969 | Instruction *InsertBefore) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 970 | : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, |
| 971 | 0, InsertBefore) {} |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 972 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 973 | : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, |
| 974 | 0, InsertAtEnd) {} |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 975 | |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 976 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 977 | // BranchInst Implementation |
| 978 | //===----------------------------------------------------------------------===// |
| 979 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 980 | void BranchInst::AssertOK() { |
| 981 | if (isConditional()) |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 982 | assert(getCondition()->getType()->isIntegerTy(1) && |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 983 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 986 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 987 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
| 988 | OperandTraits<BranchInst>::op_end(this) - 1, 1, |
| 989 | InsertBefore) { |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 990 | assert(IfTrue && "Branch destination may not be null!"); |
Gabor Greif | ae5a20a | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 991 | Op<-1>() = IfTrue; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 992 | } |
Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 993 | |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 994 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 995 | Instruction *InsertBefore) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 996 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
| 997 | OperandTraits<BranchInst>::op_end(this) - 3, 3, |
| 998 | InsertBefore) { |
Gabor Greif | ae5a20a | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 999 | Op<-1>() = IfTrue; |
| 1000 | Op<-2>() = IfFalse; |
| 1001 | Op<-3>() = Cond; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1002 | #ifndef NDEBUG |
| 1003 | AssertOK(); |
| 1004 | #endif |
| 1005 | } |
| 1006 | |
| 1007 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1008 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
| 1009 | OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) { |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1010 | assert(IfTrue && "Branch destination may not be null!"); |
Gabor Greif | ae5a20a | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1011 | Op<-1>() = IfTrue; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1015 | BasicBlock *InsertAtEnd) |
| 1016 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
| 1017 | OperandTraits<BranchInst>::op_end(this) - 3, 3, InsertAtEnd) { |
Gabor Greif | ae5a20a | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1018 | Op<-1>() = IfTrue; |
| 1019 | Op<-2>() = IfFalse; |
| 1020 | Op<-3>() = Cond; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1021 | #ifndef NDEBUG |
| 1022 | AssertOK(); |
| 1023 | #endif |
| 1024 | } |
| 1025 | |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 1026 | BranchInst::BranchInst(const BranchInst &BI) |
| 1027 | : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br, |
| 1028 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), |
| 1029 | BI.getNumOperands()) { |
Gabor Greif | ae5a20a | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1030 | Op<-1>() = BI.Op<-1>(); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1031 | if (BI.getNumOperands() != 1) { |
| 1032 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
Gabor Greif | ae5a20a | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1033 | Op<-3>() = BI.Op<-3>(); |
| 1034 | Op<-2>() = BI.Op<-2>(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1035 | } |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1036 | SubclassOptionalData = BI.SubclassOptionalData; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Chandler Carruth | 602650c | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 1039 | void BranchInst::swapSuccessors() { |
| 1040 | assert(isConditional() && |
| 1041 | "Cannot swap successors of an unconditional branch"); |
| 1042 | Op<-1>().swap(Op<-2>()); |
| 1043 | |
| 1044 | // Update profile metadata if present and it matches our structural |
| 1045 | // expectations. |
Xinliang David Li | ee836b9 | 2016-08-23 15:39:03 +0000 | [diff] [blame] | 1046 | swapProfMetadata(); |
Chandler Carruth | 602650c | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1049 | //===----------------------------------------------------------------------===// |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1050 | // AllocaInst Implementation |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1051 | //===----------------------------------------------------------------------===// |
| 1052 | |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 1053 | static Value *getAISize(LLVMContext &Context, Value *Amt) { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1054 | if (!Amt) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1055 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); |
Chris Lattner | e46749c | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 1056 | else { |
| 1057 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 53336cb | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 1058 | "Passed basic block into allocation size parameter! Use other ctor"); |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 1059 | assert(Amt->getType()->isIntegerTy() && |
| 1060 | "Allocation array size is not an integer!"); |
Chris Lattner | e46749c | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 1061 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1062 | return Amt; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Matt Arsenault | e0b3c33 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1065 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1066 | Instruction *InsertBefore) |
Matt Arsenault | e0b3c33 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1067 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {} |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1068 | |
Matt Arsenault | e0b3c33 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1069 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1070 | BasicBlock *InsertAtEnd) |
Matt Arsenault | e0b3c33 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1071 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {} |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1072 | |
Matt Arsenault | e0b3c33 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1073 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1074 | const Twine &Name, Instruction *InsertBefore) |
Matt Arsenault | e0b3c33 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1075 | : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {} |
| 1076 | |
| 1077 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1078 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 1079 | : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {} |
| 1080 | |
| 1081 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1082 | unsigned Align, const Twine &Name, |
| 1083 | Instruction *InsertBefore) |
| 1084 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, |
| 1085 | getAISize(Ty->getContext(), ArraySize), InsertBefore), |
| 1086 | AllocatedType(Ty) { |
Dan Gohman | 5283707 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1087 | setAlignment(Align); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1088 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1089 | setName(Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Matt Arsenault | e0b3c33 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 1092 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 1093 | unsigned Align, const Twine &Name, |
| 1094 | BasicBlock *InsertAtEnd) |
| 1095 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, |
| 1096 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd), |
David Blaikie | 3453e8b | 2015-04-29 23:00:35 +0000 | [diff] [blame] | 1097 | AllocatedType(Ty) { |
Dan Gohman | 5283707 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1098 | setAlignment(Align); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1099 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1100 | setName(Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1103 | void AllocaInst::setAlignment(unsigned Align) { |
Dan Gohman | 5283707 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1104 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1105 | assert(Align <= MaximumAlignment && |
| 1106 | "Alignment is greater than MaximumAlignment!"); |
Reid Kleckner | 3cbfa16 | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 1107 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) | |
| 1108 | (Log2_32(Align) + 1)); |
Dan Gohman | 5283707 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1109 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 1110 | } |
| 1111 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1112 | bool AllocaInst::isArrayAllocation() const { |
Reid Spencer | bb9723b | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 1113 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
Dan Gohman | e0214d5 | 2010-09-27 15:15:44 +0000 | [diff] [blame] | 1114 | return !CI->isOne(); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1115 | return true; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Chris Lattner | c5dd22a | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1118 | /// isStaticAlloca - Return true if this alloca is in the entry block of the |
| 1119 | /// function and is a constant size. If so, the code generator will fold it |
| 1120 | /// into the prolog/epilog code, so it is basically free. |
| 1121 | bool AllocaInst::isStaticAlloca() const { |
| 1122 | // Must be constant size. |
| 1123 | if (!isa<ConstantInt>(getArraySize())) return false; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1124 | |
Chris Lattner | c5dd22a | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1125 | // Must be in the entry block. |
| 1126 | const BasicBlock *Parent = getParent(); |
Reid Kleckner | 3cbfa16 | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 1127 | return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca(); |
Chris Lattner | c5dd22a | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1130 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1131 | // LoadInst Implementation |
| 1132 | //===----------------------------------------------------------------------===// |
| 1133 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1134 | void LoadInst::AssertOK() { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1135 | assert(getOperand(0)->getType()->isPointerTy() && |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1136 | "Ptr must have pointer type."); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1137 | assert(!(isAtomic() && getAlignment() == 0) && |
| 1138 | "Alignment required for atomic load"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
James Y Knight | 4ed9d9e | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1141 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, |
| 1142 | Instruction *InsertBef) |
| 1143 | : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {} |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1144 | |
James Y Knight | 4ed9d9e | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1145 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, |
| 1146 | BasicBlock *InsertAE) |
| 1147 | : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertAE) {} |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1148 | |
David Blaikie | 14a714f | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1149 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1150 | Instruction *InsertBef) |
David Blaikie | 14a714f | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1151 | : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {} |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1152 | |
James Y Knight | 4ed9d9e | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1153 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1154 | BasicBlock *InsertAE) |
James Y Knight | 4ed9d9e | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1155 | : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {} |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1156 | |
David Blaikie | 0f0d21e | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 1157 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1158 | unsigned Align, Instruction *InsertBef) |
JF Bastien | b36d1a8 | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1159 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1160 | SyncScope::System, InsertBef) {} |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1161 | |
James Y Knight | 4ed9d9e | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1162 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Dan Gohman | 6ab2d18 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1163 | unsigned Align, BasicBlock *InsertAE) |
James Y Knight | 4ed9d9e | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1164 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1165 | SyncScope::System, InsertAE) {} |
Dan Gohman | 6ab2d18 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1166 | |
David Blaikie | af10235 | 2015-04-06 20:59:48 +0000 | [diff] [blame] | 1167 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1168 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1169 | SyncScope::ID SSID, Instruction *InsertBef) |
David Blaikie | af10235 | 2015-04-06 20:59:48 +0000 | [diff] [blame] | 1170 | : UnaryInstruction(Ty, Load, Ptr, InsertBef) { |
David Blaikie | e86ba4b | 2015-05-20 20:22:31 +0000 | [diff] [blame] | 1171 | assert(Ty == cast<PointerType>(Ptr->getType())->getElementType()); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1172 | setVolatile(isVolatile); |
| 1173 | setAlignment(Align); |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1174 | setAtomic(Order, SSID); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1175 | AssertOK(); |
| 1176 | setName(Name); |
| 1177 | } |
| 1178 | |
James Y Knight | 4ed9d9e | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1179 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
| 1180 | unsigned Align, AtomicOrdering Order, SyncScope::ID SSID, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1181 | BasicBlock *InsertAE) |
James Y Knight | 4ed9d9e | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 1182 | : UnaryInstruction(Ty, Load, Ptr, InsertAE) { |
| 1183 | assert(Ty == cast<PointerType>(Ptr->getType())->getElementType()); |
Chris Lattner | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1184 | setVolatile(isVolatile); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1185 | setAlignment(Align); |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1186 | setAtomic(Order, SSID); |
Chris Lattner | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1187 | AssertOK(); |
| 1188 | setName(Name); |
| 1189 | } |
| 1190 | |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1191 | void LoadInst::setAlignment(unsigned Align) { |
| 1192 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1193 | assert(Align <= MaximumAlignment && |
| 1194 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1195 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | b2406d9 | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1196 | ((Log2_32(Align)+1)<<1)); |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1197 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1198 | } |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1199 | |
| 1200 | //===----------------------------------------------------------------------===// |
| 1201 | // StoreInst Implementation |
| 1202 | //===----------------------------------------------------------------------===// |
| 1203 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1204 | void StoreInst::AssertOK() { |
Nate Begeman | 5bc1ea0 | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1205 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1206 | assert(getOperand(1)->getType()->isPointerTy() && |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1207 | "Ptr must have pointer type!"); |
| 1208 | assert(getOperand(0)->getType() == |
| 1209 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 8fabb62 | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 1210 | && "Ptr must be a pointer to Val type!"); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1211 | assert(!(isAtomic() && getAlignment() == 0) && |
Mark Lacey | efe6aa1 | 2013-12-21 00:00:49 +0000 | [diff] [blame] | 1212 | "Alignment required for atomic store"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1213 | } |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1214 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1215 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Benjamin Kramer | fdef53e | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1216 | : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {} |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1217 | |
| 1218 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Benjamin Kramer | fdef53e | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1219 | : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {} |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1220 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1221 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1222 | Instruction *InsertBefore) |
Benjamin Kramer | fdef53e | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1223 | : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {} |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1224 | |
| 1225 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Benjamin Kramer | fdef53e | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1226 | BasicBlock *InsertAtEnd) |
| 1227 | : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {} |
| 1228 | |
| 1229 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align, |
| 1230 | Instruction *InsertBefore) |
JF Bastien | b36d1a8 | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1231 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1232 | SyncScope::System, InsertBefore) {} |
Benjamin Kramer | fdef53e | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1233 | |
| 1234 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align, |
| 1235 | BasicBlock *InsertAtEnd) |
JF Bastien | b36d1a8 | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1236 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1237 | SyncScope::System, InsertAtEnd) {} |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1238 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1239 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1240 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1241 | SyncScope::ID SSID, |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1242 | Instruction *InsertBefore) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1243 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1244 | OperandTraits<StoreInst>::op_begin(this), |
| 1245 | OperandTraits<StoreInst>::operands(this), |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1246 | InsertBefore) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1247 | Op<0>() = val; |
| 1248 | Op<1>() = addr; |
Dan Gohman | 6ab2d18 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1249 | setVolatile(isVolatile); |
| 1250 | setAlignment(Align); |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1251 | setAtomic(Order, SSID); |
Dan Gohman | 6ab2d18 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1252 | AssertOK(); |
| 1253 | } |
| 1254 | |
| 1255 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1256 | unsigned Align, AtomicOrdering Order, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1257 | SyncScope::ID SSID, |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1258 | BasicBlock *InsertAtEnd) |
| 1259 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
| 1260 | OperandTraits<StoreInst>::op_begin(this), |
| 1261 | OperandTraits<StoreInst>::operands(this), |
| 1262 | InsertAtEnd) { |
| 1263 | Op<0>() = val; |
| 1264 | Op<1>() = addr; |
| 1265 | setVolatile(isVolatile); |
| 1266 | setAlignment(Align); |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1267 | setAtomic(Order, SSID); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1268 | AssertOK(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1271 | void StoreInst::setAlignment(unsigned Align) { |
| 1272 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1273 | assert(Align <= MaximumAlignment && |
| 1274 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1275 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | b2406d9 | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1276 | ((Log2_32(Align)+1) << 1)); |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1277 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1280 | //===----------------------------------------------------------------------===// |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1281 | // AtomicCmpXchgInst Implementation |
| 1282 | //===----------------------------------------------------------------------===// |
| 1283 | |
| 1284 | void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | ca396e3 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1285 | AtomicOrdering SuccessOrdering, |
| 1286 | AtomicOrdering FailureOrdering, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1287 | SyncScope::ID SSID) { |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1288 | Op<0>() = Ptr; |
| 1289 | Op<1>() = Cmp; |
| 1290 | Op<2>() = NewVal; |
Tim Northover | ca396e3 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1291 | setSuccessOrdering(SuccessOrdering); |
| 1292 | setFailureOrdering(FailureOrdering); |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1293 | setSyncScopeID(SSID); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1294 | |
| 1295 | assert(getOperand(0) && getOperand(1) && getOperand(2) && |
| 1296 | "All operands must be non-null!"); |
| 1297 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1298 | "Ptr must have pointer type!"); |
| 1299 | assert(getOperand(1)->getType() == |
| 1300 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1301 | && "Ptr must be a pointer to Cmp type!"); |
| 1302 | assert(getOperand(2)->getType() == |
| 1303 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1304 | && "Ptr must be a pointer to NewVal type!"); |
JF Bastien | b36d1a8 | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1305 | assert(SuccessOrdering != AtomicOrdering::NotAtomic && |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1306 | "AtomicCmpXchg instructions must be atomic!"); |
JF Bastien | b36d1a8 | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1307 | assert(FailureOrdering != AtomicOrdering::NotAtomic && |
Tim Northover | ca396e3 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1308 | "AtomicCmpXchg instructions must be atomic!"); |
JF Bastien | b36d1a8 | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1309 | assert(!isStrongerThan(FailureOrdering, SuccessOrdering) && |
| 1310 | "AtomicCmpXchg failure argument shall be no stronger than the success " |
| 1311 | "argument"); |
| 1312 | assert(FailureOrdering != AtomicOrdering::Release && |
| 1313 | FailureOrdering != AtomicOrdering::AcquireRelease && |
Tim Northover | ca396e3 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1314 | "AtomicCmpXchg failure ordering cannot include release semantics"); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | ca396e3 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1318 | AtomicOrdering SuccessOrdering, |
| 1319 | AtomicOrdering FailureOrdering, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1320 | SyncScope::ID SSID, |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1321 | Instruction *InsertBefore) |
Tim Northover | 8f2a85e | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1322 | : Instruction( |
Serge Guelton | d35f86e | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1323 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), |
Tim Northover | 8f2a85e | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1324 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1325 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) { |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1326 | Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | ca396e3 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1330 | AtomicOrdering SuccessOrdering, |
| 1331 | AtomicOrdering FailureOrdering, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1332 | SyncScope::ID SSID, |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1333 | BasicBlock *InsertAtEnd) |
Tim Northover | 8f2a85e | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1334 | : Instruction( |
Serge Guelton | d35f86e | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1335 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), |
Tim Northover | 8f2a85e | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1336 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1337 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) { |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1338 | Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1339 | } |
Tim Northover | 8f2a85e | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1340 | |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1341 | //===----------------------------------------------------------------------===// |
| 1342 | // AtomicRMWInst Implementation |
| 1343 | //===----------------------------------------------------------------------===// |
| 1344 | |
| 1345 | void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, |
| 1346 | AtomicOrdering Ordering, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1347 | SyncScope::ID SSID) { |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1348 | Op<0>() = Ptr; |
| 1349 | Op<1>() = Val; |
| 1350 | setOperation(Operation); |
| 1351 | setOrdering(Ordering); |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1352 | setSyncScopeID(SSID); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1353 | |
| 1354 | assert(getOperand(0) && getOperand(1) && |
| 1355 | "All operands must be non-null!"); |
| 1356 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1357 | "Ptr must have pointer type!"); |
| 1358 | assert(getOperand(1)->getType() == |
| 1359 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1360 | && "Ptr must be a pointer to Val type!"); |
JF Bastien | b36d1a8 | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1361 | assert(Ordering != AtomicOrdering::NotAtomic && |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1362 | "AtomicRMW instructions must be atomic!"); |
| 1363 | } |
| 1364 | |
| 1365 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1366 | AtomicOrdering Ordering, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1367 | SyncScope::ID SSID, |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1368 | Instruction *InsertBefore) |
| 1369 | : Instruction(Val->getType(), AtomicRMW, |
| 1370 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1371 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1372 | InsertBefore) { |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1373 | Init(Operation, Ptr, Val, Ordering, SSID); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1377 | AtomicOrdering Ordering, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1378 | SyncScope::ID SSID, |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1379 | BasicBlock *InsertAtEnd) |
| 1380 | : Instruction(Val->getType(), AtomicRMW, |
| 1381 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1382 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1383 | InsertAtEnd) { |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1384 | Init(Operation, Ptr, Val, Ordering, SSID); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Matt Arsenault | 2c2c51b | 2018-10-02 23:44:11 +0000 | [diff] [blame] | 1387 | StringRef AtomicRMWInst::getOperationName(BinOp Op) { |
| 1388 | switch (Op) { |
| 1389 | case AtomicRMWInst::Xchg: |
| 1390 | return "xchg"; |
| 1391 | case AtomicRMWInst::Add: |
| 1392 | return "add"; |
| 1393 | case AtomicRMWInst::Sub: |
| 1394 | return "sub"; |
| 1395 | case AtomicRMWInst::And: |
| 1396 | return "and"; |
| 1397 | case AtomicRMWInst::Nand: |
| 1398 | return "nand"; |
| 1399 | case AtomicRMWInst::Or: |
| 1400 | return "or"; |
| 1401 | case AtomicRMWInst::Xor: |
| 1402 | return "xor"; |
| 1403 | case AtomicRMWInst::Max: |
| 1404 | return "max"; |
| 1405 | case AtomicRMWInst::Min: |
| 1406 | return "min"; |
| 1407 | case AtomicRMWInst::UMax: |
| 1408 | return "umax"; |
| 1409 | case AtomicRMWInst::UMin: |
| 1410 | return "umin"; |
| 1411 | case AtomicRMWInst::BAD_BINOP: |
| 1412 | return "<invalid operation>"; |
| 1413 | } |
| 1414 | |
| 1415 | llvm_unreachable("invalid atomicrmw operation"); |
| 1416 | } |
| 1417 | |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1418 | //===----------------------------------------------------------------------===// |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1419 | // FenceInst Implementation |
| 1420 | //===----------------------------------------------------------------------===// |
| 1421 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1422 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1423 | SyncScope::ID SSID, |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1424 | Instruction *InsertBefore) |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1425 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) { |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1426 | setOrdering(Ordering); |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1427 | setSyncScopeID(SSID); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1430 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1431 | SyncScope::ID SSID, |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1432 | BasicBlock *InsertAtEnd) |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1433 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) { |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1434 | setOrdering(Ordering); |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1435 | setSyncScopeID(SSID); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1439 | // GetElementPtrInst Implementation |
| 1440 | //===----------------------------------------------------------------------===// |
| 1441 | |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1442 | void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1443 | const Twine &Name) { |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1444 | assert(getNumOperands() == 1 + IdxList.size() && |
| 1445 | "NumOperands not initialized?"); |
Pete Cooper | 1052042 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 1446 | Op<0>() = Ptr; |
Fangrui Song | 53a6224 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 1447 | llvm::copy(IdxList, op_begin() + 1); |
Matthijs Kooijman | 338169d | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1448 | setName(Name); |
Chris Lattner | 38bacf2 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1451 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) |
David Blaikie | a67d5ab | 2015-05-05 18:03:48 +0000 | [diff] [blame] | 1452 | : Instruction(GEPI.getType(), GetElementPtr, |
| 1453 | OperandTraits<GetElementPtrInst>::op_end(this) - |
| 1454 | GEPI.getNumOperands(), |
| 1455 | GEPI.getNumOperands()), |
David Blaikie | d84c8ef | 2015-06-01 03:09:34 +0000 | [diff] [blame] | 1456 | SourceElementType(GEPI.SourceElementType), |
| 1457 | ResultElementType(GEPI.ResultElementType) { |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1458 | std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1459 | SubclassOptionalData = GEPI.SubclassOptionalData; |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Chris Lattner | c66996a | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1462 | /// getIndexedType - Returns the type of the element that would be accessed with |
| 1463 | /// a gep instruction with the specified parameters. |
| 1464 | /// |
| 1465 | /// The Idxs pointer should point to a continuous piece of memory containing the |
| 1466 | /// indices, either as Value* or uint64_t. |
| 1467 | /// |
| 1468 | /// A null type is returned if the indices are invalid for the specified |
| 1469 | /// pointer type. |
| 1470 | /// |
Matthijs Kooijman | e2afded | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1471 | template <typename IndexTy> |
David Blaikie | 25e3d2d | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1472 | static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) { |
Chris Lattner | c66996a | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1473 | // Handle the special case of the empty set index set, which is always valid. |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1474 | if (IdxList.empty()) |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1475 | return Agg; |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1476 | |
Chris Lattner | c66996a | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1477 | // If there is at least one index, the top level type must be sized, otherwise |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1478 | // it cannot be 'stepped over'. |
| 1479 | if (!Agg->isSized()) |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1480 | return nullptr; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1481 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1482 | unsigned CurIdx = 1; |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1483 | for (; CurIdx != IdxList.size(); ++CurIdx) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1484 | CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1485 | if (!CT || CT->isPointerTy()) return nullptr; |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1486 | IndexTy Index = IdxList[CurIdx]; |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1487 | if (!CT->indexValid(Index)) return nullptr; |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1488 | Agg = CT->getTypeAtIndex(Index); |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1489 | } |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1490 | return CurIdx == IdxList.size() ? Agg : nullptr; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
David Blaikie | 25e3d2d | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1493 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) { |
| 1494 | return getIndexedTypeInternal(Ty, IdxList); |
Matthijs Kooijman | e2afded | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
David Blaikie | 25e3d2d | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1497 | Type *GetElementPtrInst::getIndexedType(Type *Ty, |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1498 | ArrayRef<Constant *> IdxList) { |
David Blaikie | 25e3d2d | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1499 | return getIndexedTypeInternal(Ty, IdxList); |
Jay Foad | 25052d8 | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
David Blaikie | 25e3d2d | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1502 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) { |
| 1503 | return getIndexedTypeInternal(Ty, IdxList); |
Matthijs Kooijman | e2afded | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Chris Lattner | 6f771d4 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1506 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1507 | /// zeros. If so, the result pointer and the first operand have the same |
| 1508 | /// value, just potentially different types. |
| 1509 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1510 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1511 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1512 | if (!CI->isZero()) return false; |
| 1513 | } else { |
| 1514 | return false; |
| 1515 | } |
| 1516 | } |
| 1517 | return true; |
| 1518 | } |
| 1519 | |
Chris Lattner | 6b0974c | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1520 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1521 | /// constant integers. If so, the result pointer and the first operand have |
| 1522 | /// a constant offset between them. |
| 1523 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1524 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1525 | if (!isa<ConstantInt>(getOperand(i))) |
| 1526 | return false; |
| 1527 | } |
| 1528 | return true; |
| 1529 | } |
| 1530 | |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1531 | void GetElementPtrInst::setIsInBounds(bool B) { |
| 1532 | cast<GEPOperator>(this)->setIsInBounds(B); |
| 1533 | } |
Chris Lattner | 6f771d4 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1534 | |
Nick Lewycky | ae05e7d | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1535 | bool GetElementPtrInst::isInBounds() const { |
| 1536 | return cast<GEPOperator>(this)->isInBounds(); |
| 1537 | } |
| 1538 | |
Chandler Carruth | 4ced4ee | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1539 | bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, |
| 1540 | APInt &Offset) const { |
Chandler Carruth | 7550f96 | 2012-12-11 11:05:15 +0000 | [diff] [blame] | 1541 | // Delegate to the generic GEPOperator implementation. |
| 1542 | return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); |
Chandler Carruth | 4ced4ee | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1545 | //===----------------------------------------------------------------------===// |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1546 | // ExtractElementInst Implementation |
| 1547 | //===----------------------------------------------------------------------===// |
| 1548 | |
| 1549 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1550 | const Twine &Name, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1551 | Instruction *InsertBef) |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1552 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1553 | ExtractElement, |
| 1554 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1555 | 2, InsertBef) { |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1556 | assert(isValidOperands(Val, Index) && |
| 1557 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1558 | Op<0>() = Val; |
| 1559 | Op<1>() = Index; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1560 | setName(Name); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1564 | const Twine &Name, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1565 | BasicBlock *InsertAE) |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1566 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1567 | ExtractElement, |
| 1568 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1569 | 2, InsertAE) { |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1570 | assert(isValidOperands(Val, Index) && |
| 1571 | "Invalid extractelement instruction operands!"); |
| 1572 | |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1573 | Op<0>() = Val; |
| 1574 | Op<1>() = Index; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1575 | setName(Name); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1578 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Michael J. Spencer | d4b4f2d | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1579 | if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy()) |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1580 | return false; |
| 1581 | return true; |
| 1582 | } |
| 1583 | |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1584 | //===----------------------------------------------------------------------===// |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1585 | // InsertElementInst Implementation |
| 1586 | //===----------------------------------------------------------------------===// |
| 1587 | |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1588 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1589 | const Twine &Name, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1590 | Instruction *InsertBef) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1591 | : Instruction(Vec->getType(), InsertElement, |
| 1592 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1593 | 3, InsertBef) { |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1594 | assert(isValidOperands(Vec, Elt, Index) && |
| 1595 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1596 | Op<0>() = Vec; |
| 1597 | Op<1>() = Elt; |
| 1598 | Op<2>() = Index; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1599 | setName(Name); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1602 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1603 | const Twine &Name, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1604 | BasicBlock *InsertAE) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1605 | : Instruction(Vec->getType(), InsertElement, |
| 1606 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1607 | 3, InsertAE) { |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1608 | assert(isValidOperands(Vec, Elt, Index) && |
| 1609 | "Invalid insertelement instruction operands!"); |
| 1610 | |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1611 | Op<0>() = Vec; |
| 1612 | Op<1>() = Elt; |
| 1613 | Op<2>() = Index; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1614 | setName(Name); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1617 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1618 | const Value *Index) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1619 | if (!Vec->getType()->isVectorTy()) |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1620 | return false; // First operand of insertelement must be vector type. |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1621 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1622 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | 86296cc | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1623 | return false;// Second operand of insertelement must be vector element type. |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1624 | |
Michael J. Spencer | d4b4f2d | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1625 | if (!Index->getType()->isIntegerTy()) |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1626 | return false; // Third operand of insertelement must be i32. |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1627 | return true; |
| 1628 | } |
| 1629 | |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1630 | //===----------------------------------------------------------------------===// |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1631 | // ShuffleVectorInst Implementation |
| 1632 | //===----------------------------------------------------------------------===// |
| 1633 | |
| 1634 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1635 | const Twine &Name, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1636 | Instruction *InsertBefore) |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1637 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1638 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1639 | ShuffleVector, |
| 1640 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1641 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1642 | InsertBefore) { |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1643 | assert(isValidOperands(V1, V2, Mask) && |
| 1644 | "Invalid shuffle vector instruction operands!"); |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1645 | Op<0>() = V1; |
| 1646 | Op<1>() = V2; |
| 1647 | Op<2>() = Mask; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1648 | setName(Name); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1652 | const Twine &Name, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1653 | BasicBlock *InsertAtEnd) |
Dan Gohman | 638d853 | 2009-08-25 23:27:45 +0000 | [diff] [blame] | 1654 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
| 1655 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1656 | ShuffleVector, |
| 1657 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1658 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1659 | InsertAtEnd) { |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1660 | assert(isValidOperands(V1, V2, Mask) && |
| 1661 | "Invalid shuffle vector instruction operands!"); |
| 1662 | |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1663 | Op<0>() = V1; |
| 1664 | Op<1>() = V2; |
| 1665 | Op<2>() = Mask; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1666 | setName(Name); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1669 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1670 | const Value *Mask) { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1671 | // V1 and V2 must be vectors of the same type. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1672 | if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) |
Chris Lattner | 8728f19 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1673 | return false; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1674 | |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1675 | // Mask must be vector of i32. |
Sanjay Patel | af30bb9 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1676 | auto *MaskTy = dyn_cast<VectorType>(Mask->getType()); |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1677 | if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32)) |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1678 | return false; |
Nate Begeman | a966af2 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1679 | |
| 1680 | // Check to see if Mask is valid. |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1681 | if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) |
| 1682 | return true; |
| 1683 | |
Sanjay Patel | af30bb9 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1684 | if (const auto *MV = dyn_cast<ConstantVector>(Mask)) { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1685 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
Benjamin Kramer | 6e9eeab | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1686 | for (Value *Op : MV->operands()) { |
Sanjay Patel | af30bb9 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1687 | if (auto *CI = dyn_cast<ConstantInt>(Op)) { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1688 | if (CI->uge(V1Size*2)) |
Nate Begeman | a966af2 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1689 | return false; |
Benjamin Kramer | 6e9eeab | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1690 | } else if (!isa<UndefValue>(Op)) { |
Nate Begeman | a966af2 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1691 | return false; |
| 1692 | } |
| 1693 | } |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1694 | return true; |
Mon P Wang | cf62b37 | 2011-10-26 00:34:48 +0000 | [diff] [blame] | 1695 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1696 | |
Sanjay Patel | af30bb9 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1697 | if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1698 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
| 1699 | for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i) |
| 1700 | if (CDS->getElementAsInteger(i) >= V1Size*2) |
| 1701 | return false; |
| 1702 | return true; |
| 1703 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1704 | |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1705 | // The bitcode reader can create a place holder for a forward reference |
| 1706 | // used as the shuffle mask. When this occurs, the shuffle mask will |
| 1707 | // fall into this case and fail. To avoid this error, do this bit of |
| 1708 | // ugliness to allow such a mask pass. |
Sanjay Patel | af30bb9 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1709 | if (const auto *CE = dyn_cast<ConstantExpr>(Mask)) |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1710 | if (CE->getOpcode() == Instruction::UserOp1) |
| 1711 | return true; |
| 1712 | |
| 1713 | return false; |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1716 | int ShuffleVectorInst::getMaskValue(const Constant *Mask, unsigned i) { |
Chris Lattner | 56243b8 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1717 | assert(i < Mask->getType()->getVectorNumElements() && "Index out of range"); |
Sanjay Patel | af30bb9 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1718 | if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1719 | return CDS->getElementAsInteger(i); |
Chris Lattner | 56243b8 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1720 | Constant *C = Mask->getAggregateElement(i); |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1721 | if (isa<UndefValue>(C)) |
Chris Lattner | 8728f19 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1722 | return -1; |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1723 | return cast<ConstantInt>(C)->getZExtValue(); |
Chris Lattner | 8728f19 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1726 | void ShuffleVectorInst::getShuffleMask(const Constant *Mask, |
Chris Lattner | 56243b8 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1727 | SmallVectorImpl<int> &Result) { |
| 1728 | unsigned NumElts = Mask->getType()->getVectorNumElements(); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1729 | |
Sanjay Patel | af30bb9 | 2017-04-19 16:22:19 +0000 | [diff] [blame] | 1730 | if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1731 | for (unsigned i = 0; i != NumElts; ++i) |
| 1732 | Result.push_back(CDS->getElementAsInteger(i)); |
| 1733 | return; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1734 | } |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1735 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1736 | Constant *C = Mask->getAggregateElement(i); |
| 1737 | Result.push_back(isa<UndefValue>(C) ? -1 : |
Chris Lattner | 220dfa7 | 2012-01-26 00:41:50 +0000 | [diff] [blame] | 1738 | cast<ConstantInt>(C)->getZExtValue()); |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1739 | } |
| 1740 | } |
| 1741 | |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1742 | static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) { |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1743 | assert(!Mask.empty() && "Shuffle mask must contain elements"); |
| 1744 | bool UsesLHS = false; |
| 1745 | bool UsesRHS = false; |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1746 | for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) { |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1747 | if (Mask[i] == -1) |
| 1748 | continue; |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1749 | assert(Mask[i] >= 0 && Mask[i] < (NumOpElts * 2) && |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1750 | "Out-of-bounds shuffle mask element"); |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1751 | UsesLHS |= (Mask[i] < NumOpElts); |
| 1752 | UsesRHS |= (Mask[i] >= NumOpElts); |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1753 | if (UsesLHS && UsesRHS) |
| 1754 | return false; |
| 1755 | } |
| 1756 | assert((UsesLHS ^ UsesRHS) && "Should have selected from exactly 1 source"); |
| 1757 | return true; |
| 1758 | } |
| 1759 | |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1760 | bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) { |
| 1761 | // We don't have vector operand size information, so assume operands are the |
| 1762 | // same size as the mask. |
| 1763 | return isSingleSourceMaskImpl(Mask, Mask.size()); |
| 1764 | } |
| 1765 | |
| 1766 | static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) { |
| 1767 | if (!isSingleSourceMaskImpl(Mask, NumOpElts)) |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1768 | return false; |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1769 | for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) { |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1770 | if (Mask[i] == -1) |
| 1771 | continue; |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1772 | if (Mask[i] != i && Mask[i] != (NumOpElts + i)) |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1773 | return false; |
| 1774 | } |
| 1775 | return true; |
| 1776 | } |
| 1777 | |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1778 | bool ShuffleVectorInst::isIdentityMask(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 isIdentityMaskImpl(Mask, Mask.size()); |
| 1782 | } |
| 1783 | |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1784 | bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) { |
| 1785 | if (!isSingleSourceMask(Mask)) |
| 1786 | return false; |
| 1787 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { |
| 1788 | if (Mask[i] == -1) |
| 1789 | continue; |
| 1790 | if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i)) |
| 1791 | return false; |
| 1792 | } |
| 1793 | return true; |
| 1794 | } |
| 1795 | |
| 1796 | bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) { |
| 1797 | if (!isSingleSourceMask(Mask)) |
| 1798 | return false; |
| 1799 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { |
| 1800 | if (Mask[i] == -1) |
| 1801 | continue; |
| 1802 | if (Mask[i] != 0 && Mask[i] != NumElts) |
| 1803 | return false; |
| 1804 | } |
| 1805 | return true; |
| 1806 | } |
| 1807 | |
| 1808 | bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) { |
| 1809 | // Select is differentiated from identity. It requires using both sources. |
| 1810 | if (isSingleSourceMask(Mask)) |
| 1811 | return false; |
| 1812 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { |
| 1813 | if (Mask[i] == -1) |
| 1814 | continue; |
| 1815 | if (Mask[i] != i && Mask[i] != (NumElts + i)) |
| 1816 | return false; |
| 1817 | } |
| 1818 | return true; |
| 1819 | } |
| 1820 | |
| 1821 | bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) { |
| 1822 | // Example masks that will return true: |
| 1823 | // v1 = <a, b, c, d> |
| 1824 | // v2 = <e, f, g, h> |
| 1825 | // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g> |
| 1826 | // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h> |
| 1827 | |
| 1828 | // 1. The number of elements in the mask must be a power-of-2 and at least 2. |
| 1829 | int NumElts = Mask.size(); |
| 1830 | if (NumElts < 2 || !isPowerOf2_32(NumElts)) |
| 1831 | return false; |
| 1832 | |
| 1833 | // 2. The first element of the mask must be either a 0 or a 1. |
| 1834 | if (Mask[0] != 0 && Mask[0] != 1) |
| 1835 | return false; |
| 1836 | |
| 1837 | // 3. The difference between the first 2 elements must be equal to the |
| 1838 | // number of elements in the mask. |
| 1839 | if ((Mask[1] - Mask[0]) != NumElts) |
| 1840 | return false; |
| 1841 | |
| 1842 | // 4. The difference between consecutive even-numbered and odd-numbered |
| 1843 | // elements must be equal to 2. |
| 1844 | for (int i = 2; i < NumElts; ++i) { |
| 1845 | int MaskEltVal = Mask[i]; |
| 1846 | if (MaskEltVal == -1) |
| 1847 | return false; |
| 1848 | int MaskEltPrevVal = Mask[i - 2]; |
| 1849 | if (MaskEltVal - MaskEltPrevVal != 2) |
| 1850 | return false; |
| 1851 | } |
| 1852 | return true; |
| 1853 | } |
| 1854 | |
Simon Pilgrim | 3ccdf22 | 2018-11-09 16:28:19 +0000 | [diff] [blame] | 1855 | bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask, |
| 1856 | int NumSrcElts, int &Index) { |
| 1857 | // Must extract from a single source. |
| 1858 | if (!isSingleSourceMaskImpl(Mask, NumSrcElts)) |
| 1859 | return false; |
| 1860 | |
| 1861 | // Must be smaller (else this is an Identity shuffle). |
Fangrui Song | b9e1133 | 2018-11-09 16:45:37 +0000 | [diff] [blame] | 1862 | if (NumSrcElts <= (int)Mask.size()) |
Simon Pilgrim | 3ccdf22 | 2018-11-09 16:28:19 +0000 | [diff] [blame] | 1863 | return false; |
| 1864 | |
| 1865 | // Find start of extraction, accounting that we may start with an UNDEF. |
| 1866 | int SubIndex = -1; |
| 1867 | for (int i = 0, e = Mask.size(); i != e; ++i) { |
| 1868 | int M = Mask[i]; |
| 1869 | if (M < 0) |
| 1870 | continue; |
| 1871 | int Offset = (M % NumSrcElts) - i; |
| 1872 | if (0 <= SubIndex && SubIndex != Offset) |
| 1873 | return false; |
| 1874 | SubIndex = Offset; |
| 1875 | } |
| 1876 | |
| 1877 | if (0 <= SubIndex) { |
| 1878 | Index = SubIndex; |
| 1879 | return true; |
| 1880 | } |
| 1881 | return false; |
| 1882 | } |
| 1883 | |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1884 | bool ShuffleVectorInst::isIdentityWithPadding() const { |
| 1885 | int NumOpElts = Op<0>()->getType()->getVectorNumElements(); |
| 1886 | int NumMaskElts = getType()->getVectorNumElements(); |
| 1887 | if (NumMaskElts <= NumOpElts) |
| 1888 | return false; |
| 1889 | |
| 1890 | // The first part of the mask must choose elements from exactly 1 source op. |
Sanjay Patel | e2e1cab | 2018-08-30 16:44:07 +0000 | [diff] [blame] | 1891 | SmallVector<int, 16> Mask = getShuffleMask(); |
Sanjay Patel | 14ea008 | 2018-08-30 15:05:38 +0000 | [diff] [blame] | 1892 | if (!isIdentityMaskImpl(Mask, NumOpElts)) |
| 1893 | return false; |
| 1894 | |
| 1895 | // All extending must be with undef elements. |
| 1896 | for (int i = NumOpElts; i < NumMaskElts; ++i) |
| 1897 | if (Mask[i] != -1) |
| 1898 | return false; |
| 1899 | |
| 1900 | return true; |
| 1901 | } |
| 1902 | |
| 1903 | bool ShuffleVectorInst::isIdentityWithExtract() const { |
| 1904 | int NumOpElts = Op<0>()->getType()->getVectorNumElements(); |
| 1905 | int NumMaskElts = getType()->getVectorNumElements(); |
| 1906 | if (NumMaskElts >= NumOpElts) |
| 1907 | return false; |
| 1908 | |
| 1909 | return isIdentityMaskImpl(getShuffleMask(), NumOpElts); |
| 1910 | } |
Sanjay Patel | 42f462a | 2018-06-19 18:44:00 +0000 | [diff] [blame] | 1911 | |
Sanjay Patel | 1c86ce8 | 2018-09-20 15:21:52 +0000 | [diff] [blame] | 1912 | bool ShuffleVectorInst::isConcat() const { |
| 1913 | // Vector concatenation is differentiated from identity with padding. |
| 1914 | if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>())) |
| 1915 | return false; |
| 1916 | |
| 1917 | int NumOpElts = Op<0>()->getType()->getVectorNumElements(); |
| 1918 | int NumMaskElts = getType()->getVectorNumElements(); |
| 1919 | if (NumMaskElts != NumOpElts * 2) |
| 1920 | return false; |
| 1921 | |
| 1922 | // Use the mask length rather than the operands' vector lengths here. We |
| 1923 | // already know that the shuffle returns a vector twice as long as the inputs, |
| 1924 | // and neither of the inputs are undef vectors. If the mask picks consecutive |
| 1925 | // elements from both inputs, then this is a concatenation of the inputs. |
| 1926 | return isIdentityMaskImpl(getShuffleMask(), NumMaskElts); |
| 1927 | } |
| 1928 | |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1929 | //===----------------------------------------------------------------------===// |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1930 | // InsertValueInst Class |
| 1931 | //===----------------------------------------------------------------------===// |
| 1932 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1933 | void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1934 | const Twine &Name) { |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1935 | assert(getNumOperands() == 2 && "NumOperands not initialized?"); |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1936 | |
| 1937 | // There's no fundamental reason why we require at least one index |
| 1938 | // (other than weirdness with &*IdxBegin being invalid; see |
| 1939 | // getelementptr's init routine for example). But there's no |
| 1940 | // present need to support it. |
Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 1941 | assert(!Idxs.empty() && "InsertValueInst must have at least one index"); |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1942 | |
| 1943 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == |
Frits van Bommel | a4805cf | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1944 | Val->getType() && "Inserted value must match indexed type!"); |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1945 | Op<0>() = Agg; |
| 1946 | Op<1>() = Val; |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1947 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1948 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | 444099f6 | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1949 | setName(Name); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) |
Gabor Greif | b9d9eba | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1953 | : Instruction(IVI.getType(), InsertValue, |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1954 | OperandTraits<InsertValueInst>::op_begin(this), 2), |
| 1955 | Indices(IVI.Indices) { |
Dan Gohman | 80b9626 | 2008-06-17 23:25:49 +0000 | [diff] [blame] | 1956 | Op<0>() = IVI.getOperand(0); |
| 1957 | Op<1>() = IVI.getOperand(1); |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1958 | SubclassOptionalData = IVI.SubclassOptionalData; |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | //===----------------------------------------------------------------------===// |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1962 | // ExtractValueInst Class |
| 1963 | //===----------------------------------------------------------------------===// |
| 1964 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1965 | void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1966 | assert(getNumOperands() == 1 && "NumOperands not initialized?"); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1967 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1968 | // There's no fundamental reason why we require at least one index. |
| 1969 | // But there's no present need to support it. |
Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 1970 | assert(!Idxs.empty() && "ExtractValueInst must have at least one index"); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1971 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1972 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | 444099f6 | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1973 | setName(Name); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) |
Gabor Greif | d4f268b | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1977 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1978 | Indices(EVI.Indices) { |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1979 | SubclassOptionalData = EVI.SubclassOptionalData; |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1982 | // getIndexedType - Returns the type of the element that would be extracted |
| 1983 | // with an extractvalue instruction with the specified parameters. |
| 1984 | // |
| 1985 | // A null type is returned if the indices are invalid for the specified |
| 1986 | // pointer type. |
| 1987 | // |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1988 | Type *ExtractValueInst::getIndexedType(Type *Agg, |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1989 | ArrayRef<unsigned> Idxs) { |
Benjamin Kramer | 6e9eeab | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1990 | for (unsigned Index : Idxs) { |
Frits van Bommel | a4805cf | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1991 | // We can't use CompositeType::indexValid(Index) here. |
| 1992 | // indexValid() always returns true for arrays because getelementptr allows |
| 1993 | // out-of-bounds indices. Since we don't allow those for extractvalue and |
| 1994 | // insertvalue we need to check array indexing manually. |
| 1995 | // Since the only other types we can index into are struct types it's just |
| 1996 | // as easy to check those manually as well. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1997 | if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { |
Frits van Bommel | a4805cf | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1998 | if (Index >= AT->getNumElements()) |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1999 | return nullptr; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2000 | } else if (StructType *ST = dyn_cast<StructType>(Agg)) { |
Frits van Bommel | a4805cf | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2001 | if (Index >= ST->getNumElements()) |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2002 | return nullptr; |
Frits van Bommel | a4805cf | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2003 | } else { |
| 2004 | // Not a valid type to index into. |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2005 | return nullptr; |
Frits van Bommel | a4805cf | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2009 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2010 | return const_cast<Type*>(Agg); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2011 | } |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2012 | |
| 2013 | //===----------------------------------------------------------------------===// |
Cameron McInally | ca8cb68 | 2018-11-13 18:15:47 +0000 | [diff] [blame] | 2014 | // UnaryOperator Class |
| 2015 | //===----------------------------------------------------------------------===// |
| 2016 | |
| 2017 | UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, |
| 2018 | Type *Ty, const Twine &Name, |
| 2019 | Instruction *InsertBefore) |
| 2020 | : UnaryInstruction(Ty, iType, S, InsertBefore) { |
| 2021 | Op<0>() = S; |
| 2022 | setName(Name); |
| 2023 | AssertOK(); |
| 2024 | } |
| 2025 | |
| 2026 | UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, |
| 2027 | Type *Ty, const Twine &Name, |
| 2028 | BasicBlock *InsertAtEnd) |
| 2029 | : UnaryInstruction(Ty, iType, S, InsertAtEnd) { |
| 2030 | Op<0>() = S; |
| 2031 | setName(Name); |
| 2032 | AssertOK(); |
| 2033 | } |
| 2034 | |
| 2035 | UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, |
| 2036 | const Twine &Name, |
| 2037 | Instruction *InsertBefore) { |
| 2038 | return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore); |
| 2039 | } |
| 2040 | |
| 2041 | UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, |
| 2042 | const Twine &Name, |
| 2043 | BasicBlock *InsertAtEnd) { |
| 2044 | UnaryOperator *Res = Create(Op, S, Name); |
| 2045 | InsertAtEnd->getInstList().push_back(Res); |
| 2046 | return Res; |
| 2047 | } |
| 2048 | |
| 2049 | void UnaryOperator::AssertOK() { |
| 2050 | Value *LHS = getOperand(0); |
| 2051 | (void)LHS; // Silence warnings. |
| 2052 | #ifndef NDEBUG |
| 2053 | switch (getOpcode()) { |
| 2054 | case FNeg: |
| 2055 | assert(getType() == LHS->getType() && |
| 2056 | "Unary operation should return same type as operand!"); |
| 2057 | assert(getType()->isFPOrFPVectorTy() && |
| 2058 | "Tried to create a floating-point operation on a " |
| 2059 | "non-floating-point type!"); |
| 2060 | break; |
| 2061 | default: llvm_unreachable("Invalid opcode provided"); |
| 2062 | } |
| 2063 | #endif |
| 2064 | } |
| 2065 | |
| 2066 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2067 | // BinaryOperator Class |
| 2068 | //===----------------------------------------------------------------------===// |
| 2069 | |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2070 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2071 | Type *Ty, const Twine &Name, |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2072 | Instruction *InsertBefore) |
Dan Gohman | 1eaac53 | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2073 | : Instruction(Ty, iType, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2074 | OperandTraits<BinaryOperator>::op_begin(this), |
| 2075 | OperandTraits<BinaryOperator>::operands(this), |
| 2076 | InsertBefore) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2077 | Op<0>() = S1; |
| 2078 | Op<1>() = S2; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2079 | setName(Name); |
Craig Topper | 8edc5b1 | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2080 | AssertOK(); |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2083 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2084 | Type *Ty, const Twine &Name, |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2085 | BasicBlock *InsertAtEnd) |
Dan Gohman | 1eaac53 | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2086 | : Instruction(Ty, iType, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2087 | OperandTraits<BinaryOperator>::op_begin(this), |
| 2088 | OperandTraits<BinaryOperator>::operands(this), |
| 2089 | InsertAtEnd) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2090 | Op<0>() = S1; |
| 2091 | Op<1>() = S2; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2092 | setName(Name); |
Craig Topper | 8edc5b1 | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2093 | AssertOK(); |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Craig Topper | 8edc5b1 | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2096 | void BinaryOperator::AssertOK() { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2097 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Jeffrey Yasskin | 8e68c38 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 2098 | (void)LHS; (void)RHS; // Silence warnings. |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2099 | assert(LHS->getType() == RHS->getType() && |
| 2100 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2101 | #ifndef NDEBUG |
Craig Topper | 8edc5b1 | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2102 | switch (getOpcode()) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2103 | case Add: case Sub: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2104 | case Mul: |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2105 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2106 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2107 | assert(getType()->isIntOrIntVectorTy() && |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2108 | "Tried to create an integer operation on a non-integer type!"); |
| 2109 | break; |
| 2110 | case FAdd: case FSub: |
| 2111 | case FMul: |
| 2112 | assert(getType() == LHS->getType() && |
| 2113 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2114 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2115 | "Tried to create a floating-point operation on a " |
| 2116 | "non-floating-point type!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2117 | break; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2118 | case UDiv: |
| 2119 | case SDiv: |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2120 | assert(getType() == LHS->getType() && |
| 2121 | "Arithmetic operation should return same type as operands!"); |
Craig Topper | 0b2cfb7 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 2122 | assert(getType()->isIntOrIntVectorTy() && |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2123 | "Incorrect operand type (not integer) for S/UDIV"); |
| 2124 | break; |
| 2125 | case FDiv: |
| 2126 | assert(getType() == LHS->getType() && |
| 2127 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2128 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2129 | "Incorrect operand type (not floating point) for FDIV"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2130 | break; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2131 | case URem: |
| 2132 | case SRem: |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2133 | assert(getType() == LHS->getType() && |
| 2134 | "Arithmetic operation should return same type as operands!"); |
Craig Topper | 0b2cfb7 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 2135 | assert(getType()->isIntOrIntVectorTy() && |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2136 | "Incorrect operand type (not integer) for S/UREM"); |
| 2137 | break; |
| 2138 | case FRem: |
| 2139 | assert(getType() == LHS->getType() && |
| 2140 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2141 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2142 | "Incorrect operand type (not floating point) for FREM"); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2143 | break; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2144 | case Shl: |
| 2145 | case LShr: |
| 2146 | case AShr: |
| 2147 | assert(getType() == LHS->getType() && |
| 2148 | "Shift operation should return same type as operands!"); |
Craig Topper | 0b2cfb7 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 2149 | assert(getType()->isIntOrIntVectorTy() && |
Nate Begeman | 5bc1ea0 | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 2150 | "Tried to create a shift operation on a non-integral type!"); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2151 | break; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2152 | case And: case Or: |
| 2153 | case Xor: |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2154 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2155 | "Logical operation should return same type as operands!"); |
Craig Topper | 0b2cfb7 | 2017-06-25 17:33:48 +0000 | [diff] [blame] | 2156 | assert(getType()->isIntOrIntVectorTy() && |
Misha Brukman | 1bae291 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 2157 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2158 | break; |
Craig Topper | 8edc5b1 | 2017-06-26 07:15:59 +0000 | [diff] [blame] | 2159 | default: llvm_unreachable("Invalid opcode provided"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2160 | } |
| 2161 | #endif |
| 2162 | } |
| 2163 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2164 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2165 | const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2166 | Instruction *InsertBefore) { |
| 2167 | assert(S1->getType() == S2->getType() && |
| 2168 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2169 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2172 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2173 | const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2174 | BasicBlock *InsertAtEnd) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2175 | BinaryOperator *Res = Create(Op, S1, S2, Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2176 | InsertAtEnd->getInstList().push_back(Res); |
| 2177 | return Res; |
| 2178 | } |
| 2179 | |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2180 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2181 | Instruction *InsertBefore) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2182 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2183 | return new BinaryOperator(Instruction::Sub, |
| 2184 | zero, Op, |
| 2185 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2188 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2189 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2190 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2191 | return new BinaryOperator(Instruction::Sub, |
| 2192 | zero, Op, |
| 2193 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Dan Gohman | bdc46c6 | 2009-12-18 02:58:50 +0000 | [diff] [blame] | 2196 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 2197 | Instruction *InsertBefore) { |
| 2198 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2199 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); |
| 2200 | } |
| 2201 | |
| 2202 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 2203 | BasicBlock *InsertAtEnd) { |
| 2204 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2205 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); |
| 2206 | } |
| 2207 | |
Duncan Sands | 8991d51 | 2010-02-02 12:53:04 +0000 | [diff] [blame] | 2208 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 2209 | Instruction *InsertBefore) { |
| 2210 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2211 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); |
| 2212 | } |
| 2213 | |
| 2214 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 2215 | BasicBlock *InsertAtEnd) { |
| 2216 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2217 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); |
| 2218 | } |
| 2219 | |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2220 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2221 | Instruction *InsertBefore) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2222 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2223 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2224 | Op->getType(), Name, InsertBefore); |
| 2225 | } |
| 2226 | |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2227 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2228 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2229 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2230 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2231 | Op->getType(), Name, InsertAtEnd); |
| 2232 | } |
| 2233 | |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2234 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2235 | Instruction *InsertBefore) { |
Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2236 | Constant *C = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | fdbc82a | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 2237 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2238 | Op->getType(), Name, InsertBefore); |
| 2239 | } |
| 2240 | |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2241 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2242 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2243 | Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | b9d4100 | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 2244 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2245 | Op->getType(), Name, InsertAtEnd); |
| 2246 | } |
| 2247 | |
Sanjay Patel | b152eae | 2016-11-16 18:09:44 +0000 | [diff] [blame] | 2248 | // Exchange the two operands to this instruction. This instruction is safe to |
| 2249 | // use on any binary instruction and does not modify the semantics of the |
| 2250 | // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode |
| 2251 | // is changed. |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2252 | bool BinaryOperator::swapOperands() { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2253 | if (!isCommutative()) |
| 2254 | return true; // Can't commute operands |
Gabor Greif | 94fb68b | 2008-05-13 22:51:52 +0000 | [diff] [blame] | 2255 | Op<0>().swap(Op<1>()); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2256 | return false; |
| 2257 | } |
| 2258 | |
Chris Lattner | 79bc332 | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2259 | //===----------------------------------------------------------------------===// |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2260 | // FPMathOperator Class |
| 2261 | //===----------------------------------------------------------------------===// |
| 2262 | |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2263 | float FPMathOperator::getFPAccuracy() const { |
Duncan P. N. Exon Smith | 5bf8ade | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 2264 | const MDNode *MD = |
| 2265 | cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2266 | if (!MD) |
| 2267 | return 0.0; |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2268 | ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0)); |
Duncan Sands | 2867c85 | 2012-04-16 19:39:33 +0000 | [diff] [blame] | 2269 | return Accuracy->getValueAPF().convertToFloat(); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2272 | //===----------------------------------------------------------------------===// |
Chris Lattner | 79bc332 | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2273 | // CastInst Class |
| 2274 | //===----------------------------------------------------------------------===// |
| 2275 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2276 | // Just determine if this cast only deals with integral->integral conversion. |
| 2277 | bool CastInst::isIntegerCast() const { |
| 2278 | switch (getOpcode()) { |
| 2279 | default: return false; |
| 2280 | case Instruction::ZExt: |
| 2281 | case Instruction::SExt: |
| 2282 | case Instruction::Trunc: |
| 2283 | return true; |
| 2284 | case Instruction::BitCast: |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2285 | return getOperand(0)->getType()->isIntegerTy() && |
| 2286 | getType()->isIntegerTy(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2287 | } |
Chris Lattner | 79bc332 | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2290 | bool CastInst::isLosslessCast() const { |
| 2291 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 2292 | if (getOpcode() != Instruction::BitCast) |
| 2293 | return false; |
| 2294 | |
| 2295 | // Identity cast is always lossless |
Ana Pazos | e6463c9 | 2016-02-03 21:34:39 +0000 | [diff] [blame] | 2296 | Type *SrcTy = getOperand(0)->getType(); |
| 2297 | Type *DstTy = getType(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2298 | if (SrcTy == DstTy) |
| 2299 | return true; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2300 | |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 2301 | // Pointer to pointer is always lossless. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2302 | if (SrcTy->isPointerTy()) |
| 2303 | return DstTy->isPointerTy(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2304 | return false; // Other types have no identity values |
| 2305 | } |
| 2306 | |
| 2307 | /// This function determines if the CastInst does not require any bits to be |
| 2308 | /// changed in order to effect the cast. Essentially, it identifies cases where |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2309 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2310 | /// example, the following are all no-op casts: |
Dan Gohman | 9b38d7d | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 2311 | /// # bitcast i32* %x to i8* |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2312 | /// # bitcast <2 x i32> %x to <4 x i16> |
Dan Gohman | 9b38d7d | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 2313 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only |
Adrian Prantl | 0b24b74 | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 2314 | /// Determine if the described cast is a no-op. |
Dan Gohman | 2c048ea | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2315 | bool CastInst::isNoopCast(Instruction::CastOps Opcode, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2316 | Type *SrcTy, |
| 2317 | Type *DestTy, |
Mikael Holmen | e2da263 | 2017-10-05 07:07:09 +0000 | [diff] [blame] | 2318 | const DataLayout &DL) { |
Dan Gohman | 2c048ea | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2319 | switch (Opcode) { |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2320 | default: llvm_unreachable("Invalid CastOp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2321 | case Instruction::Trunc: |
| 2322 | case Instruction::ZExt: |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2323 | case Instruction::SExt: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2324 | case Instruction::FPTrunc: |
| 2325 | case Instruction::FPExt: |
| 2326 | case Instruction::UIToFP: |
| 2327 | case Instruction::SIToFP: |
| 2328 | case Instruction::FPToUI: |
| 2329 | case Instruction::FPToSI: |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2330 | case Instruction::AddrSpaceCast: |
| 2331 | // TODO: Target informations may give a more accurate answer here. |
| 2332 | return false; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2333 | case Instruction::BitCast: |
| 2334 | return true; // BitCast never modifies bits. |
| 2335 | case Instruction::PtrToInt: |
Mikael Holmen | e2da263 | 2017-10-05 07:07:09 +0000 | [diff] [blame] | 2336 | return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() == |
Dan Gohman | 2c048ea | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2337 | DestTy->getScalarSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2338 | case Instruction::IntToPtr: |
Mikael Holmen | e2da263 | 2017-10-05 07:07:09 +0000 | [diff] [blame] | 2339 | return DL.getIntPtrType(DestTy)->getScalarSizeInBits() == |
Dan Gohman | 2c048ea | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2340 | SrcTy->getScalarSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2341 | } |
| 2342 | } |
| 2343 | |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2344 | bool CastInst::isNoopCast(const DataLayout &DL) const { |
Mikael Holmen | 42c9fd0 | 2017-10-03 06:03:49 +0000 | [diff] [blame] | 2345 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL); |
Matt Arsenault | 38c18ef | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | /// This function determines if a pair of casts can be eliminated and what |
| 2349 | /// opcode should be used in the elimination. This assumes that there are two |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2350 | /// instructions like this: |
| 2351 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 2352 | /// * %S = secondOpcode MidTy %F to DstTy |
| 2353 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 2354 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
Sanjay Patel | 46e81ea | 2015-12-11 18:12:01 +0000 | [diff] [blame] | 2355 | /// If no such cast is permitted, the function returns 0. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2356 | unsigned CastInst::isEliminableCastPair( |
| 2357 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2358 | Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, |
| 2359 | Type *DstIntPtrTy) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2360 | // Define the 144 possibilities for these two cast instructions. The values |
| 2361 | // in this matrix determine what to do in a given situation and select the |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2362 | // case in the switch below. The rows correspond to firstOp, the columns |
Sanjay Patel | 46e81ea | 2015-12-11 18:12:01 +0000 | [diff] [blame] | 2363 | // correspond to secondOp. In looking at the table below, keep in mind |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2364 | // the following cast properties: |
| 2365 | // |
| 2366 | // Size Compare Source Destination |
| 2367 | // Operator Src ? Size Type Sign Type Sign |
| 2368 | // -------- ------------ ------------------- --------------------- |
| 2369 | // TRUNC > Integer Any Integral Any |
| 2370 | // ZEXT < Integral Unsigned Integer Any |
| 2371 | // SEXT < Integral Signed Integer Any |
| 2372 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2373 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 2374 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 2375 | // SITOFP n/a Integral Signed FloatPt n/a |
| 2376 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 2377 | // FPEXT < FloatPt n/a FloatPt n/a |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2378 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 2379 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2380 | // BITCAST = FirstClass n/a FirstClass n/a |
| 2381 | // ADDRSPCST n/a Pointer n/a Pointer n/a |
Chris Lattner | 518f6fa | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2382 | // |
| 2383 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 2384 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", |
| 2385 | // into "fptoui double to i64", but this loses information about the range |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2386 | // of the produced value (we no longer know the top-part is all zeros). |
Chris Lattner | 518f6fa | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2387 | // Further this conversion is often much more expensive for typical hardware, |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2388 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
Chris Lattner | 518f6fa | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2389 | // same reason. |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2390 | const unsigned numCastOps = |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2391 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 2392 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2393 | // T F F U S F F P I B A -+ |
| 2394 | // R Z S P P I I T P 2 N T S | |
| 2395 | // U E E 2 2 2 2 R E I T C C +- secondOp |
| 2396 | // N X X U S F F N X N 2 V V | |
| 2397 | // C T T I I P P C T T P T T -+ |
| 2398 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+ |
Fiona Glaser | b575056 | 2015-04-21 00:05:41 +0000 | [diff] [blame] | 2399 | { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt | |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2400 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt | |
| 2401 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI | |
| 2402 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI | |
| 2403 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp |
| 2404 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP | |
Ahmed Bougacha | c0d1ce5 | 2015-05-29 00:04:30 +0000 | [diff] [blame] | 2405 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc | |
Craig Topper | 5391b3c | 2018-03-02 18:16:51 +0000 | [diff] [blame] | 2406 | { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt | |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2407 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt | |
| 2408 | { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr | |
| 2409 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast | |
| 2410 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+ |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2411 | }; |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2412 | |
Sanjay Patel | 63b2982 | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2413 | // TODO: This logic could be encoded into the table above and handled in the |
| 2414 | // switch below. |
Chris Lattner | dfd3626 | 2010-07-12 01:19:22 +0000 | [diff] [blame] | 2415 | // If either of the casts are a bitcast from scalar to vector, disallow the |
Sanjay Patel | 63b2982 | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2416 | // merging. However, any pair of bitcasts are allowed. |
| 2417 | bool IsFirstBitcast = (firstOp == Instruction::BitCast); |
| 2418 | bool IsSecondBitcast = (secondOp == Instruction::BitCast); |
| 2419 | bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast; |
Nadav Rotem | 89879ec | 2011-08-29 19:58:36 +0000 | [diff] [blame] | 2420 | |
Sanjay Patel | 63b2982 | 2015-12-12 00:33:36 +0000 | [diff] [blame] | 2421 | // Check if any of the casts convert scalars <-> vectors. |
| 2422 | if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || |
| 2423 | (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) |
| 2424 | if (!AreBothBitcasts) |
| 2425 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2426 | |
| 2427 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 2428 | [secondOp-Instruction::CastOpsBegin]; |
| 2429 | switch (ElimCase) { |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2430 | case 0: |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2431 | // Categorically disallowed. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2432 | return 0; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2433 | case 1: |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2434 | // Allowed, use first cast's opcode. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2435 | return firstOp; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2436 | case 2: |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2437 | // Allowed, use second cast's opcode. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2438 | return secondOp; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2439 | case 3: |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2440 | // No-op cast in second op implies firstOp as long as the DestTy |
Mon P Wang | e4a0a15 | 2010-01-23 04:35:57 +0000 | [diff] [blame] | 2441 | // is integer and we are not converting between a vector and a |
Alp Toker | 087ab61 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 2442 | // non-vector type. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2443 | if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2444 | return firstOp; |
| 2445 | return 0; |
| 2446 | case 4: |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2447 | // No-op cast in second op implies firstOp as long as the DestTy |
Chris Lattner | 33b1758 | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2448 | // is floating point. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2449 | if (DstTy->isFloatingPointTy()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2450 | return firstOp; |
| 2451 | return 0; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2452 | case 5: |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2453 | // No-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 33b1758 | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2454 | // is an integer. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2455 | if (SrcTy->isIntegerTy()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2456 | return secondOp; |
| 2457 | return 0; |
| 2458 | case 6: |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2459 | // No-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 33b1758 | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2460 | // is a floating point. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2461 | if (SrcTy->isFloatingPointTy()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2462 | return secondOp; |
| 2463 | return 0; |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2464 | case 7: { |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2465 | // Cannot simplify if address spaces are different! |
| 2466 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) |
| 2467 | return 0; |
| 2468 | |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2469 | unsigned MidSize = MidTy->getScalarSizeInBits(); |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2470 | // We can still fold this without knowing the actual sizes as long we |
| 2471 | // know that the intermediate pointer is the largest possible |
| 2472 | // pointer size. |
| 2473 | // FIXME: Is this always true? |
| 2474 | if (MidSize == 64) |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2475 | return Instruction::BitCast; |
| 2476 | |
| 2477 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size. |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2478 | if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) |
Dan Gohman | 295643b | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2479 | return 0; |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2480 | unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2481 | if (MidSize >= PtrSize) |
| 2482 | return Instruction::BitCast; |
| 2483 | return 0; |
| 2484 | } |
| 2485 | case 8: { |
| 2486 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 2487 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 2488 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2489 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2490 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2491 | if (SrcSize == DstSize) |
| 2492 | return Instruction::BitCast; |
| 2493 | else if (SrcSize < DstSize) |
| 2494 | return firstOp; |
| 2495 | return secondOp; |
| 2496 | } |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2497 | case 9: |
| 2498 | // zext, sext -> zext, because sext can't sign extend after zext |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2499 | return Instruction::ZExt; |
Matt Arsenault | 3181f59 | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2500 | case 11: { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2501 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2502 | if (!MidIntPtrTy) |
Dan Gohman | 295643b | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2503 | return 0; |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2504 | unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2505 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2506 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2507 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 2508 | return Instruction::BitCast; |
| 2509 | return 0; |
| 2510 | } |
Eugene Zelenko | 4679522 | 2017-05-15 21:57:41 +0000 | [diff] [blame] | 2511 | case 12: |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2512 | // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS |
| 2513 | // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS |
| 2514 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) |
| 2515 | return Instruction::AddrSpaceCast; |
| 2516 | return Instruction::BitCast; |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2517 | case 13: |
| 2518 | // FIXME: this state can be merged with (1), but the following assert |
| 2519 | // is useful to check the correcteness of the sequence due to semantic |
| 2520 | // change of bitcast. |
| 2521 | assert( |
| 2522 | SrcTy->isPtrOrPtrVectorTy() && |
| 2523 | MidTy->isPtrOrPtrVectorTy() && |
| 2524 | DstTy->isPtrOrPtrVectorTy() && |
| 2525 | SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() && |
| 2526 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && |
| 2527 | "Illegal addrspacecast, bitcast sequence!"); |
| 2528 | // Allowed, use first cast's opcode |
| 2529 | return firstOp; |
| 2530 | case 14: |
Jingyue Wu | c77dec9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 2531 | // bitcast, addrspacecast -> addrspacecast if the element type of |
| 2532 | // bitcast's source is the same as that of addrspacecast's destination. |
Peter Collingbourne | e1f5c81 | 2016-11-13 06:58:45 +0000 | [diff] [blame] | 2533 | if (SrcTy->getScalarType()->getPointerElementType() == |
| 2534 | DstTy->getScalarType()->getPointerElementType()) |
Jingyue Wu | c77dec9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 2535 | return Instruction::AddrSpaceCast; |
| 2536 | return 0; |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2537 | case 15: |
| 2538 | // FIXME: this state can be merged with (1), but the following assert |
| 2539 | // is useful to check the correcteness of the sequence due to semantic |
| 2540 | // change of bitcast. |
| 2541 | assert( |
| 2542 | SrcTy->isIntOrIntVectorTy() && |
| 2543 | MidTy->isPtrOrPtrVectorTy() && |
| 2544 | DstTy->isPtrOrPtrVectorTy() && |
| 2545 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && |
| 2546 | "Illegal inttoptr, bitcast sequence!"); |
| 2547 | // Allowed, use first cast's opcode |
| 2548 | return firstOp; |
| 2549 | case 16: |
| 2550 | // FIXME: this state can be merged with (2), but the following assert |
| 2551 | // is useful to check the correcteness of the sequence due to semantic |
| 2552 | // change of bitcast. |
| 2553 | assert( |
| 2554 | SrcTy->isPtrOrPtrVectorTy() && |
| 2555 | MidTy->isPtrOrPtrVectorTy() && |
| 2556 | DstTy->isIntOrIntVectorTy() && |
| 2557 | SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() && |
| 2558 | "Illegal bitcast, ptrtoint sequence!"); |
| 2559 | // Allowed, use second cast's opcode |
| 2560 | return secondOp; |
Fiona Glaser | b575056 | 2015-04-21 00:05:41 +0000 | [diff] [blame] | 2561 | case 17: |
| 2562 | // (sitofp (zext x)) -> (uitofp x) |
| 2563 | return Instruction::UIToFP; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2564 | case 99: |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2565 | // Cast combination can't happen (error in input). This is for all cases |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2566 | // where the MidTy is not the same for the two cast instructions. |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2567 | llvm_unreachable("Invalid Cast Combination"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2568 | default: |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2569 | llvm_unreachable("Error in CastResults table!!!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2570 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2573 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2574 | const Twine &Name, Instruction *InsertBefore) { |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2575 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2576 | // Construct and return the appropriate CastInst subclass |
| 2577 | switch (op) { |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2578 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 2579 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 2580 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 2581 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 2582 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 2583 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 2584 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 2585 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 2586 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 2587 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 2588 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 2589 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 2590 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore); |
| 2591 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2592 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2595 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2596 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2597 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2598 | // Construct and return the appropriate CastInst subclass |
| 2599 | switch (op) { |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2600 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 2601 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 2602 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 2603 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 2604 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 2605 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2606 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2607 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 2608 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 2609 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 2610 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 2611 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 2612 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd); |
| 2613 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2614 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2617 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2618 | const Twine &Name, |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2619 | Instruction *InsertBefore) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2620 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2621 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2622 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2625 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2626 | const Twine &Name, |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2627 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2628 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2629 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2630 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2633 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2634 | const Twine &Name, |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2635 | Instruction *InsertBefore) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2636 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2637 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2638 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2641 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2642 | const Twine &Name, |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2643 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2644 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2645 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2646 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2649 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2650 | const Twine &Name, |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2651 | Instruction *InsertBefore) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2652 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2653 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2654 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2657 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2658 | const Twine &Name, |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2659 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2660 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2661 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2662 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2665 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2666 | const Twine &Name, |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2667 | BasicBlock *InsertAtEnd) { |
Matt Arsenault | 1bf0ec4 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2668 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2669 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
| 2670 | "Invalid cast"); |
| 2671 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); |
Richard Trieu | 5b62bb0 | 2013-07-31 04:07:28 +0000 | [diff] [blame] | 2672 | assert((!Ty->isVectorTy() || |
| 2673 | Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2674 | "Invalid cast"); |
| 2675 | |
Matt Arsenault | 1bf0ec4 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2676 | if (Ty->isIntOrIntVectorTy()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2677 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2678 | |
Matt Arsenault | 9f40cd7 | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2679 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd); |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Adrian Prantl | 0b24b74 | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 2682 | /// Create a BitCast or a PtrToInt cast instruction |
Matt Arsenault | 1bf0ec4 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2683 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
| 2684 | const Twine &Name, |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2685 | Instruction *InsertBefore) { |
Evgeniy Stepanov | 344d3fb | 2013-01-15 16:43:00 +0000 | [diff] [blame] | 2686 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2687 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2688 | "Invalid cast"); |
Matt Arsenault | 1bf0ec4 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2689 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); |
Richard Trieu | 5b62bb0 | 2013-07-31 04:07:28 +0000 | [diff] [blame] | 2690 | assert((!Ty->isVectorTy() || |
| 2691 | Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && |
Matt Arsenault | 1bf0ec4 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2692 | "Invalid cast"); |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2693 | |
Evgeniy Stepanov | 344d3fb | 2013-01-15 16:43:00 +0000 | [diff] [blame] | 2694 | if (Ty->isIntOrIntVectorTy()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2695 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2696 | |
Matt Arsenault | 9f40cd7 | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2697 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore); |
| 2698 | } |
| 2699 | |
| 2700 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( |
| 2701 | Value *S, Type *Ty, |
| 2702 | const Twine &Name, |
| 2703 | BasicBlock *InsertAtEnd) { |
| 2704 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2705 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2706 | |
| 2707 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
| 2708 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd); |
| 2709 | |
| 2710 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2711 | } |
| 2712 | |
| 2713 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( |
| 2714 | Value *S, Type *Ty, |
| 2715 | const Twine &Name, |
| 2716 | Instruction *InsertBefore) { |
| 2717 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2718 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2719 | |
| 2720 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2721 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore); |
| 2722 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2723 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
Chandler Carruth | a87c354 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2726 | CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, |
| 2727 | const Twine &Name, |
| 2728 | Instruction *InsertBefore) { |
| 2729 | if (S->getType()->isPointerTy() && Ty->isIntegerTy()) |
| 2730 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 2731 | if (S->getType()->isIntegerTy() && Ty->isPointerTy()) |
| 2732 | return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); |
| 2733 | |
| 2734 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2735 | } |
| 2736 | |
Matt Arsenault | 9f40cd7 | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2737 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2738 | bool isSigned, const Twine &Name, |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2739 | Instruction *InsertBefore) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2740 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Chris Lattner | 859c372 | 2010-01-10 20:21:42 +0000 | [diff] [blame] | 2741 | "Invalid integer cast"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2742 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2743 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2744 | Instruction::CastOps opcode = |
| 2745 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2746 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2747 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2748 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2751 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2752 | bool isSigned, const Twine &Name, |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2753 | BasicBlock *InsertAtEnd) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2754 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2755 | "Invalid cast"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2756 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2757 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2758 | Instruction::CastOps opcode = |
| 2759 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2760 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2761 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2762 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2765 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
| 2766 | const Twine &Name, |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2767 | Instruction *InsertBefore) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2768 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2769 | "Invalid cast"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2770 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2771 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2772 | Instruction::CastOps opcode = |
| 2773 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2774 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2775 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2778 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
| 2779 | const Twine &Name, |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2780 | BasicBlock *InsertAtEnd) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2781 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2782 | "Invalid cast"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2783 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2784 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2785 | Instruction::CastOps opcode = |
| 2786 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2787 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2788 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Matt Arsenault | 485c7fd | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2791 | // Check whether it is valid to call getCastOpcode for these types. |
| 2792 | // This routine must be kept in sync with getCastOpcode. |
| 2793 | bool CastInst::isCastable(Type *SrcTy, Type *DestTy) { |
| 2794 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2795 | return false; |
| 2796 | |
| 2797 | if (SrcTy == DestTy) |
| 2798 | return true; |
| 2799 | |
| 2800 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2801 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
| 2802 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2803 | // An element by element cast. Valid if casting the elements is valid. |
| 2804 | SrcTy = SrcVecTy->getElementType(); |
| 2805 | DestTy = DestVecTy->getElementType(); |
| 2806 | } |
| 2807 | |
| 2808 | // Get the bit sizes, we'll need these |
| 2809 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2810 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2811 | |
| 2812 | // Run through the possibilities ... |
| 2813 | if (DestTy->isIntegerTy()) { // Casting to integral |
David Blaikie | 16ff2da | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2814 | if (SrcTy->isIntegerTy()) // Casting from integral |
Matt Arsenault | 485c7fd | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2815 | return true; |
David Blaikie | 16ff2da | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2816 | if (SrcTy->isFloatingPointTy()) // Casting from floating pt |
Matt Arsenault | 485c7fd | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2817 | return true; |
David Blaikie | 16ff2da | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2818 | if (SrcTy->isVectorTy()) // Casting from vector |
Matt Arsenault | 485c7fd | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2819 | return DestBits == SrcBits; |
David Blaikie | 16ff2da | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2820 | // Casting from something else |
| 2821 | return SrcTy->isPointerTy(); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2822 | } |
David Blaikie | 16ff2da | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2823 | if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2824 | if (SrcTy->isIntegerTy()) // Casting from integral |
Matt Arsenault | 485c7fd | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2825 | return true; |
David Blaikie | 16ff2da | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2826 | if (SrcTy->isFloatingPointTy()) // Casting from floating pt |
Matt Arsenault | 485c7fd | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2827 | return true; |
David Blaikie | 16ff2da | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2828 | if (SrcTy->isVectorTy()) // Casting from vector |
Matt Arsenault | 485c7fd | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2829 | return DestBits == SrcBits; |
David Blaikie | 16ff2da | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2830 | // Casting from something else |
Matt Arsenault | 485c7fd | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2831 | return false; |
| 2832 | } |
David Blaikie | 16ff2da | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2833 | if (DestTy->isVectorTy()) // Casting to vector |
| 2834 | return DestBits == SrcBits; |
| 2835 | if (DestTy->isPointerTy()) { // Casting to pointer |
| 2836 | if (SrcTy->isPointerTy()) // Casting from pointer |
| 2837 | return true; |
| 2838 | return SrcTy->isIntegerTy(); // Casting from integral |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2839 | } |
David Blaikie | 16ff2da | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2840 | if (DestTy->isX86_MMXTy()) { |
| 2841 | if (SrcTy->isVectorTy()) |
| 2842 | return DestBits == SrcBits; // 64-bit vector to MMX |
| 2843 | return false; |
| 2844 | } // Casting to something else |
| 2845 | return false; |
Matt Arsenault | 485c7fd | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Matt Arsenault | f34dc42 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2848 | bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { |
| 2849 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2850 | return false; |
| 2851 | |
| 2852 | if (SrcTy == DestTy) |
| 2853 | return true; |
| 2854 | |
| 2855 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 2856 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) { |
| 2857 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2858 | // An element by element cast. Valid if casting the elements is valid. |
| 2859 | SrcTy = SrcVecTy->getElementType(); |
| 2860 | DestTy = DestVecTy->getElementType(); |
| 2861 | } |
| 2862 | } |
| 2863 | } |
| 2864 | |
| 2865 | if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) { |
| 2866 | if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) { |
| 2867 | return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); |
| 2868 | } |
| 2869 | } |
| 2870 | |
| 2871 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2872 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2873 | |
| 2874 | // Could still have vectors of pointers if the number of elements doesn't |
| 2875 | // match |
| 2876 | if (SrcBits == 0 || DestBits == 0) |
| 2877 | return false; |
| 2878 | |
| 2879 | if (SrcBits != DestBits) |
| 2880 | return false; |
| 2881 | |
| 2882 | if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) |
| 2883 | return false; |
| 2884 | |
| 2885 | return true; |
| 2886 | } |
| 2887 | |
Chandler Carruth | a87c354 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2888 | bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2889 | const DataLayout &DL) { |
Yichao Yu | 689fbe0 | 2017-10-22 20:28:17 +0000 | [diff] [blame] | 2890 | // ptrtoint and inttoptr are not allowed on non-integral pointers |
Chandler Carruth | a87c354 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2891 | if (auto *PtrTy = dyn_cast<PointerType>(SrcTy)) |
| 2892 | if (auto *IntTy = dyn_cast<IntegerType>(DestTy)) |
Yichao Yu | 689fbe0 | 2017-10-22 20:28:17 +0000 | [diff] [blame] | 2893 | return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && |
| 2894 | !DL.isNonIntegralPointerType(PtrTy)); |
Chandler Carruth | a87c354 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2895 | if (auto *PtrTy = dyn_cast<PointerType>(DestTy)) |
| 2896 | if (auto *IntTy = dyn_cast<IntegerType>(SrcTy)) |
Yichao Yu | 689fbe0 | 2017-10-22 20:28:17 +0000 | [diff] [blame] | 2897 | return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && |
| 2898 | !DL.isNonIntegralPointerType(PtrTy)); |
Chandler Carruth | a87c354 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2899 | |
| 2900 | return isBitCastable(SrcTy, DestTy); |
| 2901 | } |
| 2902 | |
Matt Arsenault | f34dc42 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2903 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 2904 | // types and size of the operand. This, basically, is a parallel of the |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2905 | // logic in the castIsValid function below. This axiom should hold: |
| 2906 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 2907 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2908 | // casting opcode for the arguments passed to it. |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2909 | // This routine must be kept in sync with isCastable. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2910 | Instruction::CastOps |
Reid Spencer | 5666712 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2911 | CastInst::getCastOpcode( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2912 | const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { |
| 2913 | Type *SrcTy = Src->getType(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2914 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2915 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && |
| 2916 | "Only first class types are castable!"); |
| 2917 | |
Duncan Sands | 117feba | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2918 | if (SrcTy == DestTy) |
| 2919 | return BitCast; |
| 2920 | |
Matt Arsenault | f34dc42 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2921 | // FIXME: Check address space sizes here |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2922 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2923 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
Duncan Sands | 117feba | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2924 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2925 | // An element by element cast. Find the appropriate opcode based on the |
| 2926 | // element types. |
| 2927 | SrcTy = SrcVecTy->getElementType(); |
| 2928 | DestTy = DestVecTy->getElementType(); |
| 2929 | } |
| 2930 | |
| 2931 | // Get the bit sizes, we'll need these |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2932 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2933 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
Duncan Sands | 117feba | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2934 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2935 | // Run through the possibilities ... |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2936 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 2937 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2938 | if (DestBits < SrcBits) |
| 2939 | return Trunc; // int -> smaller int |
| 2940 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | 5666712 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2941 | if (SrcIsSigned) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2942 | return SExt; // signed -> SEXT |
| 2943 | else |
| 2944 | return ZExt; // unsigned -> ZEXT |
| 2945 | } else { |
| 2946 | return BitCast; // Same size, No-op cast |
| 2947 | } |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2948 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2949 | if (DestIsSigned) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2950 | return FPToSI; // FP -> sint |
| 2951 | else |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2952 | return FPToUI; // FP -> uint |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2953 | } else if (SrcTy->isVectorTy()) { |
| 2954 | assert(DestBits == SrcBits && |
| 2955 | "Casting vector to integer of different width"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2956 | return BitCast; // Same size, no-op cast |
| 2957 | } else { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2958 | assert(SrcTy->isPointerTy() && |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2959 | "Casting from a value that is not first-class type"); |
| 2960 | return PtrToInt; // ptr -> int |
| 2961 | } |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2962 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2963 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | 5666712 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2964 | if (SrcIsSigned) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2965 | return SIToFP; // sint -> FP |
| 2966 | else |
| 2967 | return UIToFP; // uint -> FP |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2968 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2969 | if (DestBits < SrcBits) { |
| 2970 | return FPTrunc; // FP -> smaller FP |
| 2971 | } else if (DestBits > SrcBits) { |
| 2972 | return FPExt; // FP -> larger FP |
| 2973 | } else { |
| 2974 | return BitCast; // same size, no-op cast |
| 2975 | } |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2976 | } else if (SrcTy->isVectorTy()) { |
| 2977 | assert(DestBits == SrcBits && |
Dan Gohman | 86296cc | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2978 | "Casting vector to floating point of different width"); |
Devang Patel | 8c3b47f | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 2979 | return BitCast; // same size, no-op cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2980 | } |
Ahmed Charles | b0934ab | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2981 | llvm_unreachable("Casting pointer or non-first class to float"); |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2982 | } else if (DestTy->isVectorTy()) { |
| 2983 | assert(DestBits == SrcBits && |
| 2984 | "Illegal cast to vector (wrong type or size)"); |
| 2985 | return BitCast; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2986 | } else if (DestTy->isPointerTy()) { |
| 2987 | if (SrcTy->isPointerTy()) { |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2988 | if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace()) |
| 2989 | return AddrSpaceCast; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2990 | return BitCast; // ptr -> ptr |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2991 | } else if (SrcTy->isIntegerTy()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2992 | return IntToPtr; // int -> ptr |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2993 | } |
Ahmed Charles | b0934ab | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2994 | llvm_unreachable("Casting pointer to other than pointer or int"); |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2995 | } else if (DestTy->isX86_MMXTy()) { |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2996 | if (SrcTy->isVectorTy()) { |
| 2997 | assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2998 | return BitCast; // 64-bit vector to MMX |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2999 | } |
Ahmed Charles | b0934ab | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3000 | llvm_unreachable("Illegal cast to X86_MMX"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3001 | } |
Ahmed Charles | b0934ab | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3002 | llvm_unreachable("Casting to type that is not first-class"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | //===----------------------------------------------------------------------===// |
| 3006 | // CastInst SubClass Constructors |
| 3007 | //===----------------------------------------------------------------------===// |
| 3008 | |
| 3009 | /// Check that the construction parameters for a CastInst are correct. This |
| 3010 | /// could be broken out into the separate constructors but it is useful to have |
| 3011 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 3012 | /// of the types involved. |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3013 | bool |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3014 | CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3015 | // Check for type sanity on the arguments |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3016 | Type *SrcTy = S->getType(); |
Evan Cheng | 582e4f2 | 2013-01-10 23:22:53 +0000 | [diff] [blame] | 3017 | |
Chris Lattner | 0b68a00 | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 3018 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || |
| 3019 | SrcTy->isAggregateType() || DstTy->isAggregateType()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3020 | return false; |
| 3021 | |
| 3022 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3023 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 3024 | unsigned DstBitSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3025 | |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3026 | // If these are vector types, get the lengths of the vectors (using zero for |
| 3027 | // scalar types means that checking that vector lengths match also checks that |
| 3028 | // scalars are not being converted to vectors or vectors to scalars). |
| 3029 | unsigned SrcLength = SrcTy->isVectorTy() ? |
| 3030 | cast<VectorType>(SrcTy)->getNumElements() : 0; |
| 3031 | unsigned DstLength = DstTy->isVectorTy() ? |
| 3032 | cast<VectorType>(DstTy)->getNumElements() : 0; |
| 3033 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3034 | // Switch on the opcode provided |
| 3035 | switch (op) { |
| 3036 | default: return false; // This is an input error |
| 3037 | case Instruction::Trunc: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3038 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3039 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3040 | case Instruction::ZExt: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3041 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3042 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3043 | case Instruction::SExt: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3044 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3045 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3046 | case Instruction::FPTrunc: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3047 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3048 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3049 | case Instruction::FPExt: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3050 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3051 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3052 | case Instruction::UIToFP: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3053 | case Instruction::SIToFP: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3054 | return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3055 | SrcLength == DstLength; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3056 | case Instruction::FPToUI: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3057 | case Instruction::FPToSI: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3058 | return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3059 | SrcLength == DstLength; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3060 | case Instruction::PtrToInt: |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3061 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3062 | return false; |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3063 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 3064 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 3065 | return false; |
Craig Topper | 1060082 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 3066 | return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3067 | case Instruction::IntToPtr: |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3068 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3069 | return false; |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3070 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 3071 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 3072 | return false; |
Craig Topper | 1060082 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 3073 | return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy(); |
Matt Arsenault | 79e3fb5 | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3074 | case Instruction::BitCast: { |
| 3075 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); |
| 3076 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); |
| 3077 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3078 | // BitCast implies a no-op cast of type only. No bits change. |
| 3079 | // However, you can't cast pointers to anything but pointers. |
Matt Arsenault | 79e3fb5 | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3080 | if (!SrcPtrTy != !DstPtrTy) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3081 | return false; |
| 3082 | |
Alp Toker | 087ab61 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 3083 | // For non-pointer cases, the cast is okay if the source and destination bit |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3084 | // widths are identical. |
Matt Arsenault | 79e3fb5 | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3085 | if (!SrcPtrTy) |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3086 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); |
| 3087 | |
Matt Arsenault | 79e3fb5 | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3088 | // If both are pointers then the address spaces must match. |
| 3089 | if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) |
| 3090 | return false; |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3091 | |
Matt Arsenault | 79e3fb5 | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3092 | // A vector of pointers must have the same number of elements. |
Serguei Katkov | 69344b0 | 2018-08-21 04:27:07 +0000 | [diff] [blame] | 3093 | VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy); |
| 3094 | VectorType *DstVecTy = dyn_cast<VectorType>(DstTy); |
| 3095 | if (SrcVecTy && DstVecTy) |
| 3096 | return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); |
| 3097 | if (SrcVecTy) |
| 3098 | return SrcVecTy->getNumElements() == 1; |
| 3099 | if (DstVecTy) |
| 3100 | return DstVecTy->getNumElements() == 1; |
Matt Arsenault | 79e3fb5 | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3101 | |
| 3102 | return true; |
| 3103 | } |
| 3104 | case Instruction::AddrSpaceCast: { |
| 3105 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); |
| 3106 | if (!SrcPtrTy) |
| 3107 | return false; |
| 3108 | |
| 3109 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); |
| 3110 | if (!DstPtrTy) |
| 3111 | return false; |
| 3112 | |
| 3113 | if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) |
| 3114 | return false; |
| 3115 | |
| 3116 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 3117 | if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy)) |
| 3118 | return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); |
| 3119 | |
| 3120 | return false; |
| 3121 | } |
| 3122 | |
| 3123 | return true; |
| 3124 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | TruncInst::TruncInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3129 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3130 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3131 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | TruncInst::TruncInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3135 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3136 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3137 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | ZExtInst::ZExtInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3141 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3142 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3143 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | ZExtInst::ZExtInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3147 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3148 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3149 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3150 | } |
| 3151 | SExtInst::SExtInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3152 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3153 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3154 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
Jeff Cohen | 97af751 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 3157 | SExtInst::SExtInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3158 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3159 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3160 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | FPTruncInst::FPTruncInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3164 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3165 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3166 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | FPTruncInst::FPTruncInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3170 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3171 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3172 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | FPExtInst::FPExtInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3176 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3177 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3178 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | FPExtInst::FPExtInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3182 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3183 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3184 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | UIToFPInst::UIToFPInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3188 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3189 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3190 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | UIToFPInst::UIToFPInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3194 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3195 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3196 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
| 3199 | SIToFPInst::SIToFPInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3200 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3201 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3202 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | SIToFPInst::SIToFPInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3206 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3207 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3208 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | FPToUIInst::FPToUIInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3212 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3213 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3214 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | FPToUIInst::FPToUIInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3218 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3219 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3220 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | FPToSIInst::FPToSIInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3224 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3225 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3226 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | FPToSIInst::FPToSIInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3230 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3231 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3232 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3236 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3237 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3238 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3242 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3243 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3244 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3248 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3249 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3250 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3254 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3255 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3256 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | BitCastInst::BitCastInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3260 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3261 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3262 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | BitCastInst::BitCastInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3266 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3267 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3268 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3269 | } |
Chris Lattner | 2f46386 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3270 | |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3271 | AddrSpaceCastInst::AddrSpaceCastInst( |
| 3272 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
| 3273 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) { |
| 3274 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); |
| 3275 | } |
| 3276 | |
| 3277 | AddrSpaceCastInst::AddrSpaceCastInst( |
| 3278 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
| 3279 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) { |
| 3280 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); |
| 3281 | } |
| 3282 | |
Chris Lattner | 2f46386 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3283 | //===----------------------------------------------------------------------===// |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3284 | // CmpInst Classes |
| 3285 | //===----------------------------------------------------------------------===// |
| 3286 | |
Craig Topper | 240b0e1 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3287 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, |
Sanjay Patel | be9ce13 | 2018-11-07 00:00:42 +0000 | [diff] [blame] | 3288 | Value *RHS, const Twine &Name, Instruction *InsertBefore, |
| 3289 | Instruction *FlagsSource) |
Nate Begeman | c83ad0d | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3290 | : Instruction(ty, op, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3291 | OperandTraits<CmpInst>::op_begin(this), |
| 3292 | OperandTraits<CmpInst>::operands(this), |
| 3293 | InsertBefore) { |
Sanjay Patel | be9ce13 | 2018-11-07 00:00:42 +0000 | [diff] [blame] | 3294 | Op<0>() = LHS; |
| 3295 | Op<1>() = RHS; |
Chris Lattner | cafe9bb | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3296 | setPredicate((Predicate)predicate); |
Reid Spencer | 97c0e21 | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3297 | setName(Name); |
Sanjay Patel | be9ce13 | 2018-11-07 00:00:42 +0000 | [diff] [blame] | 3298 | if (FlagsSource) |
| 3299 | copyIRFlags(FlagsSource); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3300 | } |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3301 | |
Craig Topper | 240b0e1 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3302 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, |
| 3303 | Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd) |
Nate Begeman | c83ad0d | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3304 | : Instruction(ty, op, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3305 | OperandTraits<CmpInst>::op_begin(this), |
| 3306 | OperandTraits<CmpInst>::operands(this), |
| 3307 | InsertAtEnd) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3308 | Op<0>() = LHS; |
| 3309 | Op<1>() = RHS; |
Chris Lattner | cafe9bb | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3310 | setPredicate((Predicate)predicate); |
Reid Spencer | 97c0e21 | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3311 | setName(Name); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | CmpInst * |
Craig Topper | 240b0e1 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3315 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3316 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3317 | if (Op == Instruction::ICmp) { |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3318 | if (InsertBefore) |
| 3319 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3320 | S1, S2, Name); |
| 3321 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3322 | return new ICmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3323 | S1, S2, Name); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3324 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3325 | |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3326 | if (InsertBefore) |
| 3327 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3328 | S1, S2, Name); |
| 3329 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3330 | return new FCmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3331 | S1, S2, Name); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | CmpInst * |
Craig Topper | 240b0e1 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3335 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3336 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3337 | if (Op == Instruction::ICmp) { |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3338 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3339 | S1, S2, Name); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3340 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3341 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3342 | S1, S2, Name); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | void CmpInst::swapOperands() { |
| 3346 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 3347 | IC->swapOperands(); |
| 3348 | else |
| 3349 | cast<FCmpInst>(this)->swapOperands(); |
| 3350 | } |
| 3351 | |
Duncan Sands | c449a22 | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3352 | bool CmpInst::isCommutative() const { |
| 3353 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3354 | return IC->isCommutative(); |
| 3355 | return cast<FCmpInst>(this)->isCommutative(); |
| 3356 | } |
| 3357 | |
Duncan Sands | c449a22 | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3358 | bool CmpInst::isEquality() const { |
| 3359 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3360 | return IC->isEquality(); |
| 3361 | return cast<FCmpInst>(this)->isEquality(); |
| 3362 | } |
| 3363 | |
Dan Gohman | 7e2dd66 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3364 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3365 | switch (pred) { |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3366 | default: llvm_unreachable("Unknown cmp predicate!"); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3367 | case ICMP_EQ: return ICMP_NE; |
| 3368 | case ICMP_NE: return ICMP_EQ; |
| 3369 | case ICMP_UGT: return ICMP_ULE; |
| 3370 | case ICMP_ULT: return ICMP_UGE; |
| 3371 | case ICMP_UGE: return ICMP_ULT; |
| 3372 | case ICMP_ULE: return ICMP_UGT; |
| 3373 | case ICMP_SGT: return ICMP_SLE; |
| 3374 | case ICMP_SLT: return ICMP_SGE; |
| 3375 | case ICMP_SGE: return ICMP_SLT; |
| 3376 | case ICMP_SLE: return ICMP_SGT; |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3377 | |
Dan Gohman | 7e2dd66 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3378 | case FCMP_OEQ: return FCMP_UNE; |
| 3379 | case FCMP_ONE: return FCMP_UEQ; |
| 3380 | case FCMP_OGT: return FCMP_ULE; |
| 3381 | case FCMP_OLT: return FCMP_UGE; |
| 3382 | case FCMP_OGE: return FCMP_ULT; |
| 3383 | case FCMP_OLE: return FCMP_UGT; |
| 3384 | case FCMP_UEQ: return FCMP_ONE; |
| 3385 | case FCMP_UNE: return FCMP_OEQ; |
| 3386 | case FCMP_UGT: return FCMP_OLE; |
| 3387 | case FCMP_ULT: return FCMP_OGE; |
| 3388 | case FCMP_UGE: return FCMP_OLT; |
| 3389 | case FCMP_ULE: return FCMP_OGT; |
| 3390 | case FCMP_ORD: return FCMP_UNO; |
| 3391 | case FCMP_UNO: return FCMP_ORD; |
| 3392 | case FCMP_TRUE: return FCMP_FALSE; |
| 3393 | case FCMP_FALSE: return FCMP_TRUE; |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3394 | } |
| 3395 | } |
| 3396 | |
Tim Northover | 3ed44cd | 2016-08-17 20:25:25 +0000 | [diff] [blame] | 3397 | StringRef CmpInst::getPredicateName(Predicate Pred) { |
| 3398 | switch (Pred) { |
| 3399 | default: return "unknown"; |
| 3400 | case FCmpInst::FCMP_FALSE: return "false"; |
| 3401 | case FCmpInst::FCMP_OEQ: return "oeq"; |
| 3402 | case FCmpInst::FCMP_OGT: return "ogt"; |
| 3403 | case FCmpInst::FCMP_OGE: return "oge"; |
| 3404 | case FCmpInst::FCMP_OLT: return "olt"; |
| 3405 | case FCmpInst::FCMP_OLE: return "ole"; |
| 3406 | case FCmpInst::FCMP_ONE: return "one"; |
| 3407 | case FCmpInst::FCMP_ORD: return "ord"; |
| 3408 | case FCmpInst::FCMP_UNO: return "uno"; |
| 3409 | case FCmpInst::FCMP_UEQ: return "ueq"; |
| 3410 | case FCmpInst::FCMP_UGT: return "ugt"; |
| 3411 | case FCmpInst::FCMP_UGE: return "uge"; |
| 3412 | case FCmpInst::FCMP_ULT: return "ult"; |
| 3413 | case FCmpInst::FCMP_ULE: return "ule"; |
| 3414 | case FCmpInst::FCMP_UNE: return "une"; |
| 3415 | case FCmpInst::FCMP_TRUE: return "true"; |
| 3416 | case ICmpInst::ICMP_EQ: return "eq"; |
| 3417 | case ICmpInst::ICMP_NE: return "ne"; |
| 3418 | case ICmpInst::ICMP_SGT: return "sgt"; |
| 3419 | case ICmpInst::ICMP_SGE: return "sge"; |
| 3420 | case ICmpInst::ICMP_SLT: return "slt"; |
| 3421 | case ICmpInst::ICMP_SLE: return "sle"; |
| 3422 | case ICmpInst::ICMP_UGT: return "ugt"; |
| 3423 | case ICmpInst::ICMP_UGE: return "uge"; |
| 3424 | case ICmpInst::ICMP_ULT: return "ult"; |
| 3425 | case ICmpInst::ICMP_ULE: return "ule"; |
| 3426 | } |
| 3427 | } |
| 3428 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3429 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 3430 | switch (pred) { |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3431 | default: llvm_unreachable("Unknown icmp predicate!"); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3432 | case ICMP_EQ: case ICMP_NE: |
| 3433 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3434 | return pred; |
| 3435 | case ICMP_UGT: return ICMP_SGT; |
| 3436 | case ICMP_ULT: return ICMP_SLT; |
| 3437 | case ICMP_UGE: return ICMP_SGE; |
| 3438 | case ICMP_ULE: return ICMP_SLE; |
| 3439 | } |
| 3440 | } |
| 3441 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3442 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { |
| 3443 | switch (pred) { |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3444 | default: llvm_unreachable("Unknown icmp predicate!"); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3445 | case ICMP_EQ: case ICMP_NE: |
| 3446 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3447 | return pred; |
| 3448 | case ICMP_SGT: return ICMP_UGT; |
| 3449 | case ICMP_SLT: return ICMP_ULT; |
| 3450 | case ICMP_SGE: return ICMP_UGE; |
| 3451 | case ICMP_SLE: return ICMP_ULE; |
| 3452 | } |
| 3453 | } |
| 3454 | |
Serguei Katkov | 5e12a21 | 2018-02-09 07:59:07 +0000 | [diff] [blame] | 3455 | CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) { |
| 3456 | switch (pred) { |
| 3457 | default: llvm_unreachable("Unknown or unsupported cmp predicate!"); |
| 3458 | case ICMP_SGT: return ICMP_SGE; |
| 3459 | case ICMP_SLT: return ICMP_SLE; |
| 3460 | case ICMP_SGE: return ICMP_SGT; |
| 3461 | case ICMP_SLE: return ICMP_SLT; |
| 3462 | case ICMP_UGT: return ICMP_UGE; |
| 3463 | case ICMP_ULT: return ICMP_ULE; |
| 3464 | case ICMP_UGE: return ICMP_UGT; |
| 3465 | case ICMP_ULE: return ICMP_ULT; |
| 3466 | |
| 3467 | case FCMP_OGT: return FCMP_OGE; |
| 3468 | case FCMP_OLT: return FCMP_OLE; |
| 3469 | case FCMP_OGE: return FCMP_OGT; |
| 3470 | case FCMP_OLE: return FCMP_OLT; |
| 3471 | case FCMP_UGT: return FCMP_UGE; |
| 3472 | case FCMP_ULT: return FCMP_ULE; |
| 3473 | case FCMP_UGE: return FCMP_UGT; |
| 3474 | case FCMP_ULE: return FCMP_ULT; |
| 3475 | } |
| 3476 | } |
| 3477 | |
Dan Gohman | 7e2dd66 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3478 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3479 | switch (pred) { |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3480 | default: llvm_unreachable("Unknown cmp predicate!"); |
Dan Gohman | 7e2dd66 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3481 | case ICMP_EQ: case ICMP_NE: |
| 3482 | return pred; |
| 3483 | case ICMP_SGT: return ICMP_SLT; |
| 3484 | case ICMP_SLT: return ICMP_SGT; |
| 3485 | case ICMP_SGE: return ICMP_SLE; |
| 3486 | case ICMP_SLE: return ICMP_SGE; |
| 3487 | case ICMP_UGT: return ICMP_ULT; |
| 3488 | case ICMP_ULT: return ICMP_UGT; |
| 3489 | case ICMP_UGE: return ICMP_ULE; |
| 3490 | case ICMP_ULE: return ICMP_UGE; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3491 | |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3492 | case FCMP_FALSE: case FCMP_TRUE: |
| 3493 | case FCMP_OEQ: case FCMP_ONE: |
| 3494 | case FCMP_UEQ: case FCMP_UNE: |
| 3495 | case FCMP_ORD: case FCMP_UNO: |
| 3496 | return pred; |
| 3497 | case FCMP_OGT: return FCMP_OLT; |
| 3498 | case FCMP_OLT: return FCMP_OGT; |
| 3499 | case FCMP_OGE: return FCMP_OLE; |
| 3500 | case FCMP_OLE: return FCMP_OGE; |
| 3501 | case FCMP_UGT: return FCMP_ULT; |
| 3502 | case FCMP_ULT: return FCMP_UGT; |
| 3503 | case FCMP_UGE: return FCMP_ULE; |
| 3504 | case FCMP_ULE: return FCMP_UGE; |
| 3505 | } |
| 3506 | } |
| 3507 | |
Max Kazantsev | 38f2c0f | 2018-02-07 11:16:29 +0000 | [diff] [blame] | 3508 | CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) { |
| 3509 | switch (pred) { |
| 3510 | case ICMP_SGT: return ICMP_SGE; |
| 3511 | case ICMP_SLT: return ICMP_SLE; |
| 3512 | case ICMP_UGT: return ICMP_UGE; |
| 3513 | case ICMP_ULT: return ICMP_ULE; |
| 3514 | case FCMP_OGT: return FCMP_OGE; |
| 3515 | case FCMP_OLT: return FCMP_OLE; |
| 3516 | case FCMP_UGT: return FCMP_UGE; |
| 3517 | case FCMP_ULT: return FCMP_ULE; |
| 3518 | default: return pred; |
| 3519 | } |
| 3520 | } |
| 3521 | |
Sanjoy Das | b9d057d | 2015-10-22 19:57:34 +0000 | [diff] [blame] | 3522 | CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) { |
| 3523 | assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!"); |
| 3524 | |
| 3525 | switch (pred) { |
| 3526 | default: |
| 3527 | llvm_unreachable("Unknown predicate!"); |
| 3528 | case CmpInst::ICMP_ULT: |
| 3529 | return CmpInst::ICMP_SLT; |
| 3530 | case CmpInst::ICMP_ULE: |
| 3531 | return CmpInst::ICMP_SLE; |
| 3532 | case CmpInst::ICMP_UGT: |
| 3533 | return CmpInst::ICMP_SGT; |
| 3534 | case CmpInst::ICMP_UGE: |
| 3535 | return CmpInst::ICMP_SGE; |
| 3536 | } |
| 3537 | } |
| 3538 | |
Craig Topper | 240b0e1 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3539 | bool CmpInst::isUnsigned(Predicate predicate) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3540 | switch (predicate) { |
| 3541 | default: return false; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3542 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3543 | case ICmpInst::ICMP_UGE: return true; |
| 3544 | } |
| 3545 | } |
| 3546 | |
Craig Topper | 240b0e1 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3547 | bool CmpInst::isSigned(Predicate predicate) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3548 | switch (predicate) { |
| 3549 | default: return false; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3550 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3551 | case ICmpInst::ICMP_SGE: return true; |
| 3552 | } |
| 3553 | } |
| 3554 | |
Craig Topper | 240b0e1 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3555 | bool CmpInst::isOrdered(Predicate predicate) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3556 | switch (predicate) { |
| 3557 | default: return false; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3558 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 3559 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3560 | case FCmpInst::FCMP_ORD: return true; |
| 3561 | } |
| 3562 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3563 | |
Craig Topper | 240b0e1 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3564 | bool CmpInst::isUnordered(Predicate predicate) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3565 | switch (predicate) { |
| 3566 | default: return false; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3567 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 3568 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3569 | case FCmpInst::FCMP_UNO: return true; |
| 3570 | } |
| 3571 | } |
| 3572 | |
Craig Topper | 240b0e1 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3573 | bool CmpInst::isTrueWhenEqual(Predicate predicate) { |
Nick Lewycky | 44df023 | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3574 | switch(predicate) { |
| 3575 | default: return false; |
| 3576 | case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: |
| 3577 | case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; |
| 3578 | } |
| 3579 | } |
| 3580 | |
Craig Topper | 240b0e1 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 3581 | bool CmpInst::isFalseWhenEqual(Predicate predicate) { |
Nick Lewycky | 44df023 | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3582 | switch(predicate) { |
| 3583 | case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: |
| 3584 | case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; |
| 3585 | default: return false; |
| 3586 | } |
| 3587 | } |
| 3588 | |
Chad Rosier | 615a6cf | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 3589 | bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) { |
Chad Rosier | 88419b2 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3590 | // If the predicates match, then we know the first condition implies the |
| 3591 | // second is true. |
| 3592 | if (Pred1 == Pred2) |
| 3593 | return true; |
| 3594 | |
| 3595 | switch (Pred1) { |
| 3596 | default: |
| 3597 | break; |
Chad Rosier | 6d282e5 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3598 | case ICMP_EQ: |
Chad Rosier | 4d87cf6 | 2016-04-25 13:25:14 +0000 | [diff] [blame] | 3599 | // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true. |
Chad Rosier | 6d282e5 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3600 | return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE || |
| 3601 | Pred2 == ICMP_SLE; |
Chad Rosier | 14f36d7 | 2016-04-22 17:14:12 +0000 | [diff] [blame] | 3602 | case ICMP_UGT: // A >u B implies A != B and A >=u B are true. |
| 3603 | return Pred2 == ICMP_NE || Pred2 == ICMP_UGE; |
| 3604 | case ICMP_ULT: // A <u B implies A != B and A <=u B are true. |
| 3605 | return Pred2 == ICMP_NE || Pred2 == ICMP_ULE; |
| 3606 | case ICMP_SGT: // A >s B implies A != B and A >=s B are true. |
| 3607 | return Pred2 == ICMP_NE || Pred2 == ICMP_SGE; |
| 3608 | case ICMP_SLT: // A <s B implies A != B and A <=s B are true. |
| 3609 | return Pred2 == ICMP_NE || Pred2 == ICMP_SLE; |
Chad Rosier | 88419b2 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3610 | } |
| 3611 | return false; |
| 3612 | } |
| 3613 | |
Chad Rosier | 615a6cf | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 3614 | bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) { |
Chad Rosier | 6d282e5 | 2016-04-22 17:57:34 +0000 | [diff] [blame] | 3615 | return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2)); |
Chad Rosier | 88419b2 | 2016-04-21 14:04:54 +0000 | [diff] [blame] | 3616 | } |
Nick Lewycky | 44df023 | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3617 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3618 | //===----------------------------------------------------------------------===// |
| 3619 | // SwitchInst Implementation |
| 3620 | //===----------------------------------------------------------------------===// |
| 3621 | |
Chris Lattner | aa6e350 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3622 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { |
| 3623 | assert(Value && Default && NumReserved); |
| 3624 | ReservedSpace = NumReserved; |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3625 | setNumHungOffUseOperands(2); |
Pete Cooper | ea42367 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3626 | allocHungoffUses(ReservedSpace); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3627 | |
Pete Cooper | 1052042 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 3628 | Op<0>() = Value; |
| 3629 | Op<1>() = Default; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3632 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3633 | /// switch on and a default destination. The number of additional cases can |
| 3634 | /// be specified here to make memory allocation more efficient. This |
| 3635 | /// constructor can also autoinsert before another instruction. |
| 3636 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3637 | Instruction *InsertBefore) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3638 | : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 3639 | nullptr, 0, InsertBefore) { |
Chris Lattner | aa6e350 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3640 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
| 3643 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3644 | /// switch on and a default destination. The number of additional cases can |
| 3645 | /// be specified here to make memory allocation more efficient. This |
| 3646 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 3647 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3648 | BasicBlock *InsertAtEnd) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3649 | : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 3650 | nullptr, 0, InsertAtEnd) { |
Chris Lattner | aa6e350 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3651 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3654 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3655 | : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) { |
Chris Lattner | aa6e350 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3656 | init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3657 | setNumHungOffUseOperands(SI.getNumOperands()); |
Pete Cooper | cff40fc | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3658 | Use *OL = getOperandList(); |
| 3659 | const Use *InOL = SI.getOperandList(); |
Chris Lattner | aa6e350 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3660 | for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3661 | OL[i] = InOL[i]; |
| 3662 | OL[i+1] = InOL[i+1]; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3663 | } |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3664 | SubclassOptionalData = SI.SubclassOptionalData; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | /// addCase - Add an entry to the switch instruction... |
| 3668 | /// |
Chris Lattner | d1a3260 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 3669 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3670 | unsigned NewCaseIdx = getNumCases(); |
| 3671 | unsigned OpNo = getNumOperands(); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3672 | if (OpNo+2 > ReservedSpace) |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3673 | growOperands(); // Get more space! |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3674 | // Initialize some new operands. |
Chris Lattner | 1f377fc | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 3675 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3676 | setNumHungOffUseOperands(OpNo+2); |
Chandler Carruth | ddfada2 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 3677 | CaseHandle Case(this, NewCaseIdx); |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3678 | Case.setValue(OnVal); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 3679 | Case.setSuccessor(Dest); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3682 | /// removeCase - This method removes the specified case and its successor |
| 3683 | /// from the switch instruction. |
Chandler Carruth | ddfada2 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 3684 | SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) { |
| 3685 | unsigned idx = I->getCaseIndex(); |
| 3686 | |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3687 | assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3688 | |
| 3689 | unsigned NumOps = getNumOperands(); |
Pete Cooper | cff40fc | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3690 | Use *OL = getOperandList(); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3691 | |
Jay Foad | 0faa609 | 2011-02-01 09:22:34 +0000 | [diff] [blame] | 3692 | // Overwrite this case with the end of the list. |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3693 | if (2 + (idx + 1) * 2 != NumOps) { |
| 3694 | OL[2 + idx * 2] = OL[NumOps - 2]; |
| 3695 | OL[2 + idx * 2 + 1] = OL[NumOps - 1]; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | // Nuke the last value. |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3699 | OL[NumOps-2].set(nullptr); |
| 3700 | OL[NumOps-2+1].set(nullptr); |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3701 | setNumHungOffUseOperands(NumOps-2); |
Chandler Carruth | 627e0f9 | 2017-03-26 02:49:23 +0000 | [diff] [blame] | 3702 | |
| 3703 | return CaseIt(this, idx); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3704 | } |
| 3705 | |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3706 | /// growOperands - grow operands - This grows the operand list in response |
| 3707 | /// to a push_back style of operation. This grows the number of ops by 3 times. |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3708 | /// |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3709 | void SwitchInst::growOperands() { |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3710 | unsigned e = getNumOperands(); |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3711 | unsigned NumOps = e*3; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3712 | |
| 3713 | ReservedSpace = NumOps; |
Pete Cooper | 33102d2 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 3714 | growHungoffUses(ReservedSpace); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3717 | //===----------------------------------------------------------------------===// |
Jay Foad | 1ed26ac | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3718 | // IndirectBrInst Implementation |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3719 | //===----------------------------------------------------------------------===// |
| 3720 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3721 | void IndirectBrInst::init(Value *Address, unsigned NumDests) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3722 | assert(Address && Address->getType()->isPointerTy() && |
Chris Lattner | a4c206f | 2009-10-29 05:53:32 +0000 | [diff] [blame] | 3723 | "Address of indirectbr must be a pointer"); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3724 | ReservedSpace = 1+NumDests; |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3725 | setNumHungOffUseOperands(1); |
Pete Cooper | ea42367 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3726 | allocHungoffUses(ReservedSpace); |
| 3727 | |
Pete Cooper | 1052042 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 3728 | Op<0>() = Address; |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3732 | /// growOperands - grow operands - This grows the operand list in response |
| 3733 | /// to a push_back style of operation. This grows the number of ops by 2 times. |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3734 | /// |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3735 | void IndirectBrInst::growOperands() { |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3736 | unsigned e = getNumOperands(); |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3737 | unsigned NumOps = e*2; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3738 | |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3739 | ReservedSpace = NumOps; |
Pete Cooper | 33102d2 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 3740 | growHungoffUses(ReservedSpace); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3743 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3744 | Instruction *InsertBefore) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3745 | : Instruction(Type::getVoidTy(Address->getContext()), |
| 3746 | Instruction::IndirectBr, nullptr, 0, InsertBefore) { |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3747 | init(Address, NumCases); |
| 3748 | } |
| 3749 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3750 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3751 | BasicBlock *InsertAtEnd) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3752 | : Instruction(Type::getVoidTy(Address->getContext()), |
| 3753 | Instruction::IndirectBr, nullptr, 0, InsertAtEnd) { |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3754 | init(Address, NumCases); |
| 3755 | } |
| 3756 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3757 | IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) |
Chandler Carruth | 09ebf7a | 2018-10-19 00:22:37 +0000 | [diff] [blame] | 3758 | : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, |
| 3759 | nullptr, IBI.getNumOperands()) { |
Pete Cooper | ea42367 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3760 | allocHungoffUses(IBI.getNumOperands()); |
Pete Cooper | cff40fc | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3761 | Use *OL = getOperandList(); |
| 3762 | const Use *InOL = IBI.getOperandList(); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3763 | for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) |
| 3764 | OL[i] = InOL[i]; |
| 3765 | SubclassOptionalData = IBI.SubclassOptionalData; |
| 3766 | } |
| 3767 | |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3768 | /// addDestination - Add a destination. |
| 3769 | /// |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3770 | void IndirectBrInst::addDestination(BasicBlock *DestBB) { |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3771 | unsigned OpNo = getNumOperands(); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3772 | if (OpNo+1 > ReservedSpace) |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3773 | growOperands(); // Get more space! |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3774 | // Initialize some new operands. |
| 3775 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3776 | setNumHungOffUseOperands(OpNo+1); |
Pete Cooper | cff40fc | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3777 | getOperandList()[OpNo] = DestBB; |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
| 3780 | /// removeDestination - This method removes the specified successor from the |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3781 | /// indirectbr instruction. |
| 3782 | void IndirectBrInst::removeDestination(unsigned idx) { |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3783 | assert(idx < getNumOperands()-1 && "Successor index out of range!"); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3784 | |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3785 | unsigned NumOps = getNumOperands(); |
Pete Cooper | cff40fc | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3786 | Use *OL = getOperandList(); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3787 | |
| 3788 | // Replace this value with the last one. |
| 3789 | OL[idx+1] = OL[NumOps-1]; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3790 | |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3791 | // Nuke the last value. |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3792 | OL[NumOps-1].set(nullptr); |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3793 | setNumHungOffUseOperands(NumOps-1); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3796 | //===----------------------------------------------------------------------===// |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3797 | // cloneImpl() implementations |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3798 | //===----------------------------------------------------------------------===// |
| 3799 | |
Chris Lattner | 795948a | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3800 | // Define these methods here so vtables don't get emitted into every translation |
| 3801 | // unit that uses these classes. |
| 3802 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3803 | GetElementPtrInst *GetElementPtrInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3804 | return new (getNumOperands()) GetElementPtrInst(*this); |
Chris Lattner | 795948a | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
Cameron McInally | ca8cb68 | 2018-11-13 18:15:47 +0000 | [diff] [blame] | 3807 | UnaryOperator *UnaryOperator::cloneImpl() const { |
| 3808 | return Create(getOpcode(), Op<0>()); |
| 3809 | } |
| 3810 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3811 | BinaryOperator *BinaryOperator::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3812 | return Create(getOpcode(), Op<0>(), Op<1>()); |
Chris Lattner | 795948a | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3815 | FCmpInst *FCmpInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3816 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3819 | ICmpInst *ICmpInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3820 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3823 | ExtractValueInst *ExtractValueInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3824 | return new ExtractValueInst(*this); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3827 | InsertValueInst *InsertValueInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3828 | return new InsertValueInst(*this); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3831 | AllocaInst *AllocaInst::cloneImpl() const { |
David Majnemer | c16fa88 | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3832 | AllocaInst *Result = new AllocaInst(getAllocatedType(), |
Matt Arsenault | e0b3c33 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 3833 | getType()->getAddressSpace(), |
David Majnemer | c16fa88 | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3834 | (Value *)getOperand(0), getAlignment()); |
| 3835 | Result->setUsedWithInAlloca(isUsedWithInAlloca()); |
Manman Ren | 4bda882 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 3836 | Result->setSwiftError(isSwiftError()); |
David Majnemer | c16fa88 | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3837 | return Result; |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3840 | LoadInst *LoadInst::cloneImpl() const { |
James Y Knight | 4ed9d9e | 2019-01-14 21:37:53 +0000 | [diff] [blame] | 3841 | return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(), |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3842 | getAlignment(), getOrdering(), getSyncScopeID()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3845 | StoreInst *StoreInst::cloneImpl() const { |
Eli Friedman | 1cc5e38 | 2011-08-10 17:39:11 +0000 | [diff] [blame] | 3846 | return new StoreInst(getOperand(0), getOperand(1), isVolatile(), |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3847 | getAlignment(), getOrdering(), getSyncScopeID()); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3848 | |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3851 | AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const { |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3852 | AtomicCmpXchgInst *Result = |
| 3853 | new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2), |
Tim Northover | ca396e3 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 3854 | getSuccessOrdering(), getFailureOrdering(), |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3855 | getSyncScopeID()); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3856 | Result->setVolatile(isVolatile()); |
Tim Northover | 8f2a85e | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 3857 | Result->setWeak(isWeak()); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3858 | return Result; |
| 3859 | } |
| 3860 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3861 | AtomicRMWInst *AtomicRMWInst::cloneImpl() const { |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3862 | AtomicRMWInst *Result = |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3863 | new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1), |
| 3864 | getOrdering(), getSyncScopeID()); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3865 | Result->setVolatile(isVolatile()); |
| 3866 | return Result; |
| 3867 | } |
| 3868 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3869 | FenceInst *FenceInst::cloneImpl() const { |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 3870 | return new FenceInst(getContext(), getOrdering(), getSyncScopeID()); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3873 | TruncInst *TruncInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3874 | return new TruncInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3877 | ZExtInst *ZExtInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3878 | return new ZExtInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3881 | SExtInst *SExtInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3882 | return new SExtInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3883 | } |
| 3884 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3885 | FPTruncInst *FPTruncInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3886 | return new FPTruncInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3889 | FPExtInst *FPExtInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3890 | return new FPExtInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3893 | UIToFPInst *UIToFPInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3894 | return new UIToFPInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3897 | SIToFPInst *SIToFPInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3898 | return new SIToFPInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3901 | FPToUIInst *FPToUIInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3902 | return new FPToUIInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3905 | FPToSIInst *FPToSIInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3906 | return new FPToSIInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3909 | PtrToIntInst *PtrToIntInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3910 | return new PtrToIntInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3913 | IntToPtrInst *IntToPtrInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3914 | return new IntToPtrInst(getOperand(0), getType()); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3915 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3916 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3917 | BitCastInst *BitCastInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3918 | return new BitCastInst(getOperand(0), getType()); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3919 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3920 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3921 | AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const { |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3922 | return new AddrSpaceCastInst(getOperand(0), getType()); |
| 3923 | } |
| 3924 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3925 | CallInst *CallInst::cloneImpl() const { |
Sanjoy Das | 4afa7f1 | 2015-11-10 20:13:21 +0000 | [diff] [blame] | 3926 | if (hasOperandBundles()) { |
| 3927 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); |
| 3928 | return new(getNumOperands(), DescriptorBytes) CallInst(*this); |
| 3929 | } |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3930 | return new(getNumOperands()) CallInst(*this); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3933 | SelectInst *SelectInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3934 | return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3935 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3936 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3937 | VAArgInst *VAArgInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3938 | return new VAArgInst(getOperand(0), getType()); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3939 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3940 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3941 | ExtractElementInst *ExtractElementInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3942 | return ExtractElementInst::Create(getOperand(0), getOperand(1)); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3943 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3944 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3945 | InsertElementInst *InsertElementInst::cloneImpl() const { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 3946 | return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3949 | ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 3950 | return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2)); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3951 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3952 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3953 | PHINode *PHINode::cloneImpl() const { return new PHINode(*this); } |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3954 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3955 | LandingPadInst *LandingPadInst::cloneImpl() const { |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3956 | return new LandingPadInst(*this); |
| 3957 | } |
| 3958 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3959 | ReturnInst *ReturnInst::cloneImpl() const { |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3960 | return new(getNumOperands()) ReturnInst(*this); |
| 3961 | } |
| 3962 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3963 | BranchInst *BranchInst::cloneImpl() const { |
Jay Foad | 8e3914d | 2011-01-07 20:29:02 +0000 | [diff] [blame] | 3964 | return new(getNumOperands()) BranchInst(*this); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3965 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3966 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3967 | SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3968 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3969 | IndirectBrInst *IndirectBrInst::cloneImpl() const { |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3970 | return new IndirectBrInst(*this); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3973 | InvokeInst *InvokeInst::cloneImpl() const { |
Sanjoy Das | 4afa7f1 | 2015-11-10 20:13:21 +0000 | [diff] [blame] | 3974 | if (hasOperandBundles()) { |
| 3975 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); |
| 3976 | return new(getNumOperands(), DescriptorBytes) InvokeInst(*this); |
| 3977 | } |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3978 | return new(getNumOperands()) InvokeInst(*this); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3979 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3980 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3981 | ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); } |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3982 | |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3983 | CleanupReturnInst *CleanupReturnInst::cloneImpl() const { |
| 3984 | return new (getNumOperands()) CleanupReturnInst(*this); |
| 3985 | } |
| 3986 | |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3987 | CatchReturnInst *CatchReturnInst::cloneImpl() const { |
David Majnemer | de17e77 | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 3988 | return new (getNumOperands()) CatchReturnInst(*this); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3991 | CatchSwitchInst *CatchSwitchInst::cloneImpl() const { |
| 3992 | return new CatchSwitchInst(*this); |
| 3993 | } |
| 3994 | |
| 3995 | FuncletPadInst *FuncletPadInst::cloneImpl() const { |
| 3996 | return new (getNumOperands()) FuncletPadInst(*this); |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
Pete Cooper | f79d253 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3999 | UnreachableInst *UnreachableInst::cloneImpl() const { |
Nick Lewycky | 6776064 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 4000 | LLVMContext &Context = getContext(); |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4001 | return new UnreachableInst(Context); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4002 | } |