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